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