Project

General

Profile

1
#!/bin/bash -e
2
. "$(dirname "${BASH_SOURCE[0]}")"/util.sh
3

    
4
if self_not_included; then
5

    
6
# using prefixed connection vars
7
alias use_local='declare prefix=local_; import_vars'
8
alias use_remote='declare prefix=remote_; import_vars'
9
alias use_local_remote='{ use_local; use_remote; }'
10
	# *must* be run inside a function
11

    
12
quote='"'
13

    
14
esc_name() { echo "$quote${1//$quote/$quote$quote}$quote"; }
15

    
16
mk_esc_name() { set_var "$1"_esc "$(esc_name "${!1}")"; }
17

    
18
alias mk_schema_esc='declare schema_esc; mk_esc_name schema'
19
alias mk_table_esc='declare table_esc; mk_esc_name table'
20

    
21
# export func usage: export_func() { ...; mk_select; ... }
22
# caller usage: {query=... | table=... [cols=...] [filter=...]} export_func
23
# cmd line usage: [limit=...] caller
24
alias mk_select='mk_table_esc
25
declare query="$(rtrim "${query:-SELECT ${cols:-*} ${cols:+
26
}FROM $table_esc
27
${filter:+WHERE $filter
28
}}${limit:+LIMIT $limit
29
}")"
30
'
31

    
32
fi # load new aliases
33
if self_being_included; then
34

    
35
log_sql() { test "$verbosity" -ge 2; }
36

    
37

    
38
### MySQL
39

    
40
alias set_database=\
41
'if test "$schema"; then declare database="${database-$schema}"; fi'
42

    
43
fi # load new aliases
44
if self_being_included; then
45

    
46
# usage: mysql*() { ...; mysql_cmd "$@"; } (with alias)
47
function mysql_cmd() # usage: mysql*() { ...; mysql_cmd cmd "$@"; }
48
# auto-adds connection/login opts when specified
49
{
50
	echo_func
51
	local cmd="$1"; shift
52
	local ssh_server="$(localize_url "$ssh_server")"
53
	local server="$(localize_url "$server")"
54
	if test "$ssh_server"; then
55
		local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
56
	fi
57
	
58
	local var=ssh_dest; local_inv
59
	command ${ssh_dest:+ssh "$ssh_dest" }"$cmd" \
60
${server:+ --host="$server" }${user:+--user="$user" } --password\
61
${password+="$password"} "$@"
62
}
63
alias mysql_cmd='mysql_cmd "$FUNCNAME"'
64

    
65
fi # load new aliases
66
if self_being_included; then
67

    
68
mysql() # usage: [output_data=1] mysql ...
69
{
70
	echo_func; kw_params output_data
71
	set_database
72
	
73
	set -- ${database:+--database="$database" }--column-names "$@"
74
	if test "$output_data"; then echo_stdin|mysql_cmd --batch "$@"
75
	else cmd_log_fd=1 mysql_cmd --verbose "$@"
76
	fi
77
}
78

    
79
mysql_ANSI()
80
{
81
	echo_func
82
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
83
}
84

    
85
mysql_export() # does not support CSV
86
# caller usage: {query=... | table=... [cols=...] [filter=...]} mysql_export
87
# cmd line usage: [limit=...] caller
88
{
89
	echo_func
90
	mk_select
91
	
92
	output_data=1 mysql_ANSI "$@" <<<"$query"
93
}
94

    
95
mysql_export_outfile() # supports CSV, but requires the FILE privilege
96
{
97
	echo_func
98
	: "${file:?}"
99
	mk_select
100
	local mysql_load_data_format="${mysql_load_data_format-\
101
FIELDS TERMINATED BY ','
102
OPTIONALLY ENCLOSED BY '\"'
103
}"
104
	
105
	local head="${query%%FROM*}" # includes trailing newline
106
	head="${head%
107
}"
108
	local tail="${query#$head}"
109
	mysql_load_data_format="${mysql_load_data_format%
110
}"
111
	mysql_ANSI "$@" <<EOF
112
$head
113
INTO OUTFILE '$file'
114
$mysql_load_data_format
115
$tail
116
EOF
117
}
118

    
119
mysqldump() # usage: [schema=1 | data=1] mysqldump db [table...]
120
{
121
	echo_func; kw_params schema data
122
	
123
	mysql_cmd ${database:+--databases "$database" --tables } \
124
--quick --lock-tables=false --set-charset \
125
${postgres_compat:+--compatible=postgresql --add-locks=false }\
126
${schema:+--no-data }${data:+--no-create-info }"$@"
127
}
128

    
129
mysqldump_diffable()
130
{
131
	echo_func
132
	mysqldump "$@"|{ pipe_delay; sed 's/^(-- Dump completed).*$/\1/'; }
133
}
134

    
135

    
136
### PostgreSQL
137

    
138
pg_export()
139
{
140
	echo_func
141
	mk_select
142
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
143
	
144
	psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
145
}
146

    
147
pg_header()
148
{
149
	echo_func
150
	local pg_copy_format="CSV HEADER" limit=0
151
	pg_export "$@"|echo_stdout
152
}
153

    
154
pg_export_table_no_header()
155
{
156
	echo_func
157
	local pg_copy_format="CSV"
158
	pg_export "$@"
159
}
160

    
161
pg_export_table_to_dir_no_header()
162
{
163
	echo_func
164
	local table="$1"; shift; mk_table_esc
165
	local cols="$(pg_header)"
166
	stdout="$exports_dir/$table.no_header.cols=$cols.csv" to_file \
167
pg_export_table_no_header "$@"
168
}
169

    
170
fi
(2-2/5)