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