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