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