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