Project

General

Profile

1 9014 aaronmk
#!/bin/bash -e
2
. "$(dirname "${BASH_SOURCE[0]}")"/util.sh
3
4
if self_not_included; then
5
6
# using prefixed connection vars
7 9641 aaronmk
alias ssh2local='declare prefix=ssh_; import_vars; unset prefix
8
declare $(prefix=ssh_ get_prefix_vars); unset $(prefix=ssh_ get_prefix_vars)'
9 9464 aaronmk
alias use_local='declare prefix=local_; import_vars; unset prefix'
10
alias use_remote='declare prefix=remote_; import_vars; unset prefix'
11 9167 aaronmk
alias use_local_remote='{ use_local; use_remote; }'
12
	# *must* be run inside a function
13 9574 aaronmk
alias use_root='declare prefix=root_; import_vars; unset prefix'
14 9014 aaronmk
15
quote='"'
16
17 9074 aaronmk
esc_name() { echo "$quote${1//$quote/$quote$quote}$quote"; }
18 9014 aaronmk
19 9423 aaronmk
mk_esc_name_alias() # usage: mk_esc_name_alias schema_esc
20 9426 aaronmk
{ alias mk_"$1"='declare '"$1"'="${'"$1"':-$(esc_name "$'"${1%_esc}"'")}"; '\
21
'echo_vars '"$1"; }
22 9014 aaronmk
23 9423 aaronmk
mk_esc_name_alias schema_esc
24
mk_esc_name_alias table_esc
25
26 9470 aaronmk
fi # load new aliases
27
if self_being_included; then
28
29 9623 aaronmk
skip_table() # usage: create_table || table=... skip_table || return 0
30
{ what="table $table" already_exists_msg; }
31
32 9698 aaronmk
# usage: (set_large_table; to_file db_cmd...) || return
33
alias set_large_table='declare del="${del-$(test "$limit"; exit2bool)}"
34
echo_vars del'
35
36 9700 aaronmk
limit() # usage: "query... $([prefix=$'| |\n'] limit)"
37 9467 aaronmk
{
38
	echo_func; kw_params prefix; local prefix="${prefix-
39
}"
40
	echo -n "${limit:+${prefix}LIMIT $limit}"
41
}
42
43 9468 aaronmk
mk_select() # usage: {query=... | table=... [cols=...] [filter=...]} mk_select
44
{
45
	echo_func; kw_params query table cols filter; mk_table_esc
46
	echo "$(rtrim "${query:-SELECT ${cols:-*} ${cols:+
47 9335 aaronmk
}FROM $table_esc
48
${filter:+WHERE $filter
49 9467 aaronmk
}}")\
50
$(limit)"
51 9468 aaronmk
}
52 9088 aaronmk
53 9468 aaronmk
# export func usage: export_func() { ...; mk_select_var; ... }
54
# caller usage: {query=... | table=... [cols=...] [filter=...]} export_func
55
# cmd line usage: [limit=...] caller
56
alias mk_select_var='declare query="$(mk_select)"'
57
58 9014 aaronmk
fi # load new aliases
59
if self_being_included; then
60
61 9482 aaronmk
mk_drop() # usage: table=... mk_drop|*sql_ANSI
62
{
63
	log++; echo_func; kw_params table; : "${table?}"; mk_table_esc
64
	echo "DROP TABLE IF EXISTS $table_esc"
65
}
66
67 9465 aaronmk
mk_truncate() # usage: table=... mk_truncate|*sql_ANSI
68 9432 aaronmk
{
69
	log++; echo_func; kw_params table; : "${table?}"; mk_table_esc
70
	echo "TRUNCATE $table_esc"
71
}
72 9014 aaronmk
73 9432 aaronmk
74 9014 aaronmk
### MySQL
75
76 9094 aaronmk
alias set_database=\
77
'if test "$schema"; then declare database="${database-$schema}"; fi'
78
79
fi # load new aliases
80
if self_being_included; then
81
82 9099 aaronmk
# usage: mysql*() { ...; mysql_cmd "$@"; } (with alias)
83
function mysql_cmd() # usage: mysql*() { ...; mysql_cmd cmd "$@"; }
84 9271 aaronmk
# auto-adds connection/login opts when specified
85 9014 aaronmk
{
86
	echo_func
87 9099 aaronmk
	local cmd="$1"; shift
88 9014 aaronmk
	local ssh_server="$(localize_url "$ssh_server")"
89
	local server="$(localize_url "$server")"
90 9050 aaronmk
	if test "$ssh_server"; then
91 9014 aaronmk
		local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
92
	fi
93
94
	local var=ssh_dest; local_inv
95 9701 aaronmk
	command "time" ${ssh_dest:+ssh "$ssh_dest" }"$cmd" \
96 9014 aaronmk
${server:+ --host="$server" }${user:+--user="$user" } --password\
97 9694 aaronmk
${password+="$password"} --quick "$@" # --quick: don't buffer entire result
98 9014 aaronmk
}
99 9099 aaronmk
alias mysql_cmd='mysql_cmd "$FUNCNAME"'
100 9014 aaronmk
101 9099 aaronmk
fi # load new aliases
102
if self_being_included; then
103
104 9706 aaronmk
function mysql() # usage: [output_data=1] [data_only=1] [log_queries=] mysql ...
105 9095 aaronmk
{
106 9578 aaronmk
	echo_func; kw_params output_data data_only query_verbosity
107 9557 aaronmk
	if test "$data_only"; then local output_data="${output_data-1}"; fi
108 9578 aaronmk
	local log_queries="${log_queries-1}"
109 9095 aaronmk
	set_database
110 9101 aaronmk
111 9557 aaronmk
	set -- ${database:+--database="$database" }--local-infile=1 \
112
--${data_only:+skip-}column-names "$@"
113 9150 aaronmk
	if test "$output_data"; then echo_stdin|mysql_cmd --batch "$@"
114 9591 aaronmk
	else cmd_log_fd=1 mysql_cmd ${log_queries:+--verbose --verbose --verbose }\
115
"$@" # --verbose*3: echo runtimes
116 9101 aaronmk
	fi
117 9095 aaronmk
}
118 9014 aaronmk
119 9074 aaronmk
mysql_ANSI()
120 9014 aaronmk
{
121
	echo_func
122
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
123
}
124
125 9706 aaronmk
# always use ANSI mode, to support "" identifiers. `` are *still* supported in
126
# this mode, so it also works with SHOW CREATE TABLE output and dumpfiles.
127
alias mysql='mysql_ANSI'
128
129 9707 aaronmk
fi # load new aliases
130
if self_being_included; then
131
132
mysql_root() { echo_func; use_root; mysql "$@"; }
133
134 9466 aaronmk
mysql_truncate() { echo_func; mk_truncate|mysql_ANSI; }
135
136 9456 aaronmk
mysql_import() # usage: table=... [cols=...] [append=1] mysql_import <file
137
# without append=1, first ensures the table is empty
138 9430 aaronmk
{
139
	echo_func
140
	mk_table_esc
141
	local mysql_load_data_format="${mysql_load_data_format-\
142
FIELDS TERMINATED BY ','
143
OPTIONALLY ENCLOSED BY '\"'
144
}"
145
146 9466 aaronmk
	if test ! "$append"; then mysql_truncate; fi
147 9430 aaronmk
	mysql_load_data_format="${mysql_load_data_format%
148
}"
149 9642 aaronmk
	ssh2local # ssh does not tunnel nonstandard fds
150 9651 aaronmk
	mysql_ANSI "$@" 40<&0 <<EOF
151
LOAD DATA LOCAL INFILE '/dev/fd/40'
152 9660 aaronmk
${append:+IGNORE }INTO TABLE $table_esc
153 9430 aaronmk
$mysql_load_data_format
154
IGNORE 1 LINES
155
EOF
156
}
157
158 9152 aaronmk
mysql_export() # does not support CSV
159 9348 aaronmk
# caller usage: {query=... | table=... [cols=...] [filter=...]} mysql_export
160
# cmd line usage: [limit=...] caller
161 9102 aaronmk
{
162
	echo_func
163 9463 aaronmk
	mk_select_var
164 9152 aaronmk
165
	output_data=1 mysql_ANSI "$@" <<<"$query"
166
}
167
168
mysql_export_outfile() # supports CSV, but requires the FILE privilege
169
{
170
	echo_func
171 9102 aaronmk
	: "${file:?}"
172 9463 aaronmk
	mk_select_var
173 9102 aaronmk
	local mysql_load_data_format="${mysql_load_data_format-\
174
FIELDS TERMINATED BY ','
175
OPTIONALLY ENCLOSED BY '\"'
176
}"
177
178
	local head="${query%%FROM*}" # includes trailing newline
179 9105 aaronmk
	head="${head%
180
}"
181 9102 aaronmk
	local tail="${query#$head}"
182 9106 aaronmk
	mysql_load_data_format="${mysql_load_data_format%
183
}"
184 9102 aaronmk
	mysql_ANSI "$@" <<EOF
185 9105 aaronmk
$head
186
INTO OUTFILE '$file'
187 9106 aaronmk
$mysql_load_data_format
188
$tail
189 9102 aaronmk
EOF
190
}
191
192 9074 aaronmk
mysqldump() # usage: [schema=1 | data=1] mysqldump db [table...]
193 9014 aaronmk
{
194 9235 aaronmk
	echo_func; kw_params schema data
195 9231 aaronmk
196 9095 aaronmk
	mysql_cmd ${database:+--databases "$database" --tables } \
197 9694 aaronmk
--lock-tables=false --set-charset \
198 9014 aaronmk
${postgres_compat:+--compatible=postgresql --add-locks=false }\
199
${schema:+--no-data }${data:+--no-create-info }"$@"
200
}
201
202 9074 aaronmk
mysqldump_diffable()
203 9014 aaronmk
{
204
	echo_func
205 9550 aaronmk
	mysqldump "$@"|{ pipe_delay; echo_run sed 's/^(-- Dump completed).*$/\1/'; }
206 9014 aaronmk
}
207
208 9588 aaronmk
mysql_rm_privileged_statements()
209
{
210
	echo_func
211
	grep -vE -e '^/\*!40000 ALTER TABLE `.*` (DIS|EN)ABLE KEYS \*/;$' \
212
-e '^(UN)?LOCK TABLES( `.*` WRITE)?;$'
213
}
214 9017 aaronmk
215 9632 aaronmk
## permissions
216 9588 aaronmk
217 9632 aaronmk
mysql_seal_table() # usage: table=... user=... mysql_seal_table
218
# prevents further modifications to a table by a user
219
{
220 9634 aaronmk
	echo_func; kw_params table user; : "${table:?}" "${user:?}"; mk_table_esc
221 9632 aaronmk
222 9635 aaronmk
	benign_error=1 mysql_root <<<"REVOKE ALL PRIVILEGES ON $table FROM '$user'@'%'" || true
223 9636 aaronmk
	benign_error=1 mysql_root <<<"REVOKE GRANT OPTION   ON $table FROM '$user'@'%'" || true
224 9632 aaronmk
}
225
226
227 9014 aaronmk
### PostgreSQL
228
229 9648 aaronmk
alias use_pg='declare prefix=pg_; import_vars; unset prefix'
230
231 9649 aaronmk
fi # load new aliases
232
if self_being_included; then
233
234 9646 aaronmk
psql() # usage: [stdin=copy_pstdin_file] psql <<<"cmds, e.g. \copy from pstdin"
235
{
236
	echo_func; kw_params stdin
237
238 9649 aaronmk
	use_pg
239
	log+ -2
240
	local PGHOST="$server";       export PGHOST
241
	local PGUSER="$user";         export PGUSER
242
	local PGPASSWORD="$password"; export PGPASSWORD
243
	local PGDATABASE="$database"; export PGDATABASE
244
	log+ 2
245
246 9646 aaronmk
	if can_log; then set -- --echo-all --echo-hidden "$@"; fi
247 9651 aaronmk
	local redirs=("${redirs[@]}" '40<&0' "0<${stdin:-&20}" '41>&1')
248 9646 aaronmk
	(
249
		if can_log; then cat <<'EOF'
250
\timing on
251
SET client_min_messages = NOTICE;
252
EOF
253
		fi
254
		cat
255 9651 aaronmk
	)|cmd_log_fd=1 command psql --file /dev/fd/40 --output /dev/fd/41 \
256 9647 aaronmk
--set ON_ERROR_STOP=1 --quiet "$@"
257 9646 aaronmk
		# --output is for query *results*, not echoed statements
258
}
259
260 9083 aaronmk
pg_export()
261 9014 aaronmk
{
262
	echo_func
263 9463 aaronmk
	mk_select_var
264 9014 aaronmk
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
265
266 9084 aaronmk
	psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
267 9014 aaronmk
}
268
269 9074 aaronmk
pg_header()
270 9014 aaronmk
{
271
	echo_func
272
	local pg_copy_format="CSV HEADER" limit=0
273 9083 aaronmk
	pg_export "$@"|echo_stdout
274 9014 aaronmk
}
275
276 9074 aaronmk
pg_export_table_no_header()
277 9014 aaronmk
{
278
	echo_func
279
	local pg_copy_format="CSV"
280 9083 aaronmk
	pg_export "$@"
281 9014 aaronmk
}
282
283 9074 aaronmk
pg_export_table_to_dir_no_header()
284 9014 aaronmk
{
285
	echo_func
286
	local table="$1"; shift; mk_table_esc
287 9476 aaronmk
	stdout="$exports_dir/$table.no_header.cols=$(pg_header).csv" to_file \
288 9042 aaronmk
pg_export_table_no_header "$@"
289 9014 aaronmk
}
290
291
fi