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; unset prefix'
|
8
|
alias use_remote='declare prefix=remote_; import_vars; unset prefix'
|
9
|
alias use_local_remote='{ use_local; use_remote; }'
|
10
|
# *must* be run inside a function
|
11
|
alias use_root='declare prefix=root_; import_vars; unset prefix'
|
12
|
|
13
|
quote='"'
|
14
|
|
15
|
esc_name() { echo "$quote${1//$quote/$quote$quote}$quote"; }
|
16
|
|
17
|
mk_esc_name_alias() # usage: mk_esc_name_alias schema_esc
|
18
|
{ alias mk_"$1"='declare '"$1"'="${'"$1"':-$(esc_name "$'"${1%_esc}"'")}"; '\
|
19
|
'echo_vars '"$1"; }
|
20
|
|
21
|
mk_esc_name_alias schema_esc
|
22
|
mk_esc_name_alias table_esc
|
23
|
|
24
|
fi # load new aliases
|
25
|
if self_being_included; then
|
26
|
|
27
|
limit() # usage: query... $([prefix=$'| |\n'] limit)
|
28
|
{
|
29
|
echo_func; kw_params prefix; local prefix="${prefix-
|
30
|
}"
|
31
|
echo -n "${limit:+${prefix}LIMIT $limit}"
|
32
|
}
|
33
|
|
34
|
mk_select() # usage: {query=... | table=... [cols=...] [filter=...]} mk_select
|
35
|
{
|
36
|
echo_func; kw_params query table cols filter; mk_table_esc
|
37
|
echo "$(rtrim "${query:-SELECT ${cols:-*} ${cols:+
|
38
|
}FROM $table_esc
|
39
|
${filter:+WHERE $filter
|
40
|
}}")\
|
41
|
$(limit)"
|
42
|
}
|
43
|
|
44
|
# export func usage: export_func() { ...; mk_select_var; ... }
|
45
|
# caller usage: {query=... | table=... [cols=...] [filter=...]} export_func
|
46
|
# cmd line usage: [limit=...] caller
|
47
|
alias mk_select_var='declare query="$(mk_select)"'
|
48
|
|
49
|
fi # load new aliases
|
50
|
if self_being_included; then
|
51
|
|
52
|
mk_drop() # usage: table=... mk_drop|*sql_ANSI
|
53
|
{
|
54
|
log++; echo_func; kw_params table; : "${table?}"; mk_table_esc
|
55
|
echo "DROP TABLE IF EXISTS $table_esc"
|
56
|
}
|
57
|
|
58
|
mk_truncate() # usage: table=... mk_truncate|*sql_ANSI
|
59
|
{
|
60
|
log++; echo_func; kw_params table; : "${table?}"; mk_table_esc
|
61
|
echo "TRUNCATE $table_esc"
|
62
|
}
|
63
|
|
64
|
|
65
|
### MySQL
|
66
|
|
67
|
alias set_database=\
|
68
|
'if test "$schema"; then declare database="${database-$schema}"; fi'
|
69
|
|
70
|
fi # load new aliases
|
71
|
if self_being_included; then
|
72
|
|
73
|
# usage: mysql*() { ...; mysql_cmd "$@"; } (with alias)
|
74
|
function mysql_cmd() # usage: mysql*() { ...; mysql_cmd cmd "$@"; }
|
75
|
# auto-adds connection/login opts when specified
|
76
|
{
|
77
|
echo_func
|
78
|
local cmd="$1"; shift
|
79
|
local ssh_server="$(localize_url "$ssh_server")"
|
80
|
local server="$(localize_url "$server")"
|
81
|
if test "$ssh_server"; then
|
82
|
local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
|
83
|
fi
|
84
|
|
85
|
local var=ssh_dest; local_inv
|
86
|
command ${ssh_dest:+ssh "$ssh_dest" }"$cmd" \
|
87
|
${server:+ --host="$server" }${user:+--user="$user" } --password\
|
88
|
${password+="$password"} "$@"
|
89
|
}
|
90
|
alias mysql_cmd='mysql_cmd "$FUNCNAME"'
|
91
|
|
92
|
fi # load new aliases
|
93
|
if self_being_included; then
|
94
|
|
95
|
mysql() # usage: [output_data=1] [data_only=1] [log_queries=] mysql ...
|
96
|
{
|
97
|
echo_func; kw_params output_data data_only query_verbosity
|
98
|
if test "$data_only"; then local output_data="${output_data-1}"; fi
|
99
|
local log_queries="${log_queries-1}"
|
100
|
set_database
|
101
|
|
102
|
set -- ${database:+--database="$database" }--local-infile=1 \
|
103
|
--${data_only:+skip-}column-names "$@"
|
104
|
if test "$output_data"; then echo_stdin|mysql_cmd --batch "$@"
|
105
|
else cmd_log_fd=1 mysql_cmd ${log_queries:+--verbose }"$@"
|
106
|
fi
|
107
|
}
|
108
|
|
109
|
mysql_ANSI()
|
110
|
{
|
111
|
echo_func
|
112
|
(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
|
113
|
}
|
114
|
|
115
|
mysql_truncate() { echo_func; mk_truncate|mysql_ANSI; }
|
116
|
|
117
|
mysql_import() # usage: table=... [cols=...] [append=1] mysql_import <file
|
118
|
# without append=1, first ensures the table is empty
|
119
|
{
|
120
|
echo_func
|
121
|
mk_table_esc
|
122
|
local mysql_load_data_format="${mysql_load_data_format-\
|
123
|
FIELDS TERMINATED BY ','
|
124
|
OPTIONALLY ENCLOSED BY '\"'
|
125
|
}"
|
126
|
|
127
|
if test ! "$append"; then mysql_truncate; fi
|
128
|
mysql_load_data_format="${mysql_load_data_format%
|
129
|
}"
|
130
|
mysql_ANSI "$@" 10<&0 <<EOF
|
131
|
LOAD DATA LOCAL INFILE '/dev/fd/10'
|
132
|
INTO TABLE $table_esc
|
133
|
$mysql_load_data_format
|
134
|
IGNORE 1 LINES
|
135
|
EOF
|
136
|
}
|
137
|
|
138
|
mysql_export() # does not support CSV
|
139
|
# caller usage: {query=... | table=... [cols=...] [filter=...]} mysql_export
|
140
|
# cmd line usage: [limit=...] caller
|
141
|
{
|
142
|
echo_func
|
143
|
mk_select_var
|
144
|
|
145
|
output_data=1 mysql_ANSI "$@" <<<"$query"
|
146
|
}
|
147
|
|
148
|
mysql_export_outfile() # supports CSV, but requires the FILE privilege
|
149
|
{
|
150
|
echo_func
|
151
|
: "${file:?}"
|
152
|
mk_select_var
|
153
|
local mysql_load_data_format="${mysql_load_data_format-\
|
154
|
FIELDS TERMINATED BY ','
|
155
|
OPTIONALLY ENCLOSED BY '\"'
|
156
|
}"
|
157
|
|
158
|
local head="${query%%FROM*}" # includes trailing newline
|
159
|
head="${head%
|
160
|
}"
|
161
|
local tail="${query#$head}"
|
162
|
mysql_load_data_format="${mysql_load_data_format%
|
163
|
}"
|
164
|
mysql_ANSI "$@" <<EOF
|
165
|
$head
|
166
|
INTO OUTFILE '$file'
|
167
|
$mysql_load_data_format
|
168
|
$tail
|
169
|
EOF
|
170
|
}
|
171
|
|
172
|
mysqldump() # usage: [schema=1 | data=1] mysqldump db [table...]
|
173
|
{
|
174
|
echo_func; kw_params schema data
|
175
|
|
176
|
mysql_cmd ${database:+--databases "$database" --tables } \
|
177
|
--quick --lock-tables=false --set-charset \
|
178
|
${postgres_compat:+--compatible=postgresql --add-locks=false }\
|
179
|
${schema:+--no-data }${data:+--no-create-info }"$@"
|
180
|
}
|
181
|
|
182
|
mysqldump_diffable()
|
183
|
{
|
184
|
echo_func
|
185
|
mysqldump "$@"|{ pipe_delay; echo_run sed 's/^(-- Dump completed).*$/\1/'; }
|
186
|
}
|
187
|
|
188
|
mysql_rm_privileged_statements()
|
189
|
{
|
190
|
echo_func
|
191
|
grep -vE -e '^/\*!40000 ALTER TABLE `.*` (DIS|EN)ABLE KEYS \*/;$' \
|
192
|
-e '^(UN)?LOCK TABLES( `.*` WRITE)?;$'
|
193
|
}
|
194
|
|
195
|
|
196
|
### PostgreSQL
|
197
|
|
198
|
pg_export()
|
199
|
{
|
200
|
echo_func
|
201
|
mk_select_var
|
202
|
local pg_copy_format="${pg_copy_format-CSV HEADER}"
|
203
|
|
204
|
psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
|
205
|
}
|
206
|
|
207
|
pg_header()
|
208
|
{
|
209
|
echo_func
|
210
|
local pg_copy_format="CSV HEADER" limit=0
|
211
|
pg_export "$@"|echo_stdout
|
212
|
}
|
213
|
|
214
|
pg_export_table_no_header()
|
215
|
{
|
216
|
echo_func
|
217
|
local pg_copy_format="CSV"
|
218
|
pg_export "$@"
|
219
|
}
|
220
|
|
221
|
pg_export_table_to_dir_no_header()
|
222
|
{
|
223
|
echo_func
|
224
|
local table="$1"; shift; mk_table_esc
|
225
|
stdout="$exports_dir/$table.no_header.cols=$(pg_header).csv" to_file \
|
226
|
pg_export_table_no_header "$@"
|
227
|
}
|
228
|
|
229
|
fi
|