Project

General

Profile

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
alias use_root='declare prefix=root_; import_vars; unset prefix'
12

    
13
quote='"'
14

    
15
esc_name() { echo "$quote${1//$quote/$quote$quote}$quote"; }
16

    
17
mk_esc_name_alias() # usage: mk_esc_name_alias schema_esc
18
{ alias mk_"$1"='declare '"$1"'="${'"$1"':-$(esc_name "$'"${1%_esc}"'")}"; '\
19
'echo_vars '"$1"; }
20

    
21
mk_esc_name_alias schema_esc
22
mk_esc_name_alias table_esc
23

    
24
fi # load new aliases
25
if self_being_included; then
26

    
27
skip_table() # usage: create_table || table=... skip_table || return 0
28
{ what="table $table" already_exists_msg; }
29

    
30
limit() # usage: query... $([prefix=$'| |\n'] limit)
31
{
32
	echo_func; kw_params prefix; local prefix="${prefix-
33
}"
34
	echo -n "${limit:+${prefix}LIMIT $limit}"
35
}
36

    
37
mk_select() # usage: {query=... | table=... [cols=...] [filter=...]} mk_select
38
{
39
	echo_func; kw_params query table cols filter; mk_table_esc
40
	echo "$(rtrim "${query:-SELECT ${cols:-*} ${cols:+
41
}FROM $table_esc
42
${filter:+WHERE $filter
43
}}")\
44
$(limit)"
45
}
46

    
47
# export func usage: export_func() { ...; mk_select_var; ... }
48
# caller usage: {query=... | table=... [cols=...] [filter=...]} export_func
49
# cmd line usage: [limit=...] caller
50
alias mk_select_var='declare query="$(mk_select)"'
51

    
52
fi # load new aliases
53
if self_being_included; then
54

    
55
mk_drop() # usage: table=... mk_drop|*sql_ANSI
56
{
57
	log++; echo_func; kw_params table; : "${table?}"; mk_table_esc
58
	echo "DROP TABLE IF EXISTS $table_esc"
59
}
60

    
61
mk_truncate() # usage: table=... mk_truncate|*sql_ANSI
62
{
63
	log++; echo_func; kw_params table; : "${table?}"; mk_table_esc
64
	echo "TRUNCATE $table_esc"
65
}
66

    
67

    
68
### MySQL
69

    
70
alias set_database=\
71
'if test "$schema"; then declare database="${database-$schema}"; fi'
72

    
73
fi # load new aliases
74
if self_being_included; then
75

    
76
# usage: mysql*() { ...; mysql_cmd "$@"; } (with alias)
77
function mysql_cmd() # usage: mysql*() { ...; mysql_cmd cmd "$@"; }
78
# auto-adds connection/login opts when specified
79
{
80
	echo_func
81
	local cmd="$1"; shift
82
	local ssh_server="$(localize_url "$ssh_server")"
83
	local server="$(localize_url "$server")"
84
	if test "$ssh_server"; then
85
		local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
86
	fi
87
	
88
	local var=ssh_dest; local_inv
89
	command ${ssh_dest:+ssh "$ssh_dest" }"$cmd" \
90
${server:+ --host="$server" }${user:+--user="$user" } --password\
91
${password+="$password"} "$@"
92
}
93
alias mysql_cmd='mysql_cmd "$FUNCNAME"'
94

    
95
fi # load new aliases
96
if self_being_included; then
97

    
98
mysql() # usage: [output_data=1] [data_only=1] [log_queries=] mysql ...
99
{
100
	echo_func; kw_params output_data data_only query_verbosity
101
	if test "$data_only"; then local output_data="${output_data-1}"; fi
102
	local log_queries="${log_queries-1}"
103
	set_database
104
	
105
	set -- ${database:+--database="$database" }--local-infile=1 \
106
--${data_only:+skip-}column-names "$@"
107
	if test "$output_data"; then echo_stdin|mysql_cmd --batch "$@"
108
	else cmd_log_fd=1 mysql_cmd ${log_queries:+--verbose --verbose --verbose }\
109
"$@" # --verbose*3: echo runtimes
110
	fi
111
}
112

    
113
mysql_root() { echo_func; use_root; mysql "$@"; }
114

    
115
mysql_ANSI()
116
{
117
	echo_func
118
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
119
}
120

    
121
mysql_truncate() { echo_func; mk_truncate|mysql_ANSI; }
122

    
123
mysql_import() # usage: table=... [cols=...] [append=1] mysql_import <file
124
# without append=1, first ensures the table is empty
125
{
126
	echo_func
127
	mk_table_esc
128
	local mysql_load_data_format="${mysql_load_data_format-\
129
FIELDS TERMINATED BY ','
130
OPTIONALLY ENCLOSED BY '\"'
131
}"
132
	
133
	if test ! "$append"; then mysql_truncate; fi
134
	mysql_load_data_format="${mysql_load_data_format%
135
}"
136
	mysql_ANSI "$@" 10<&0 <<EOF
137
LOAD DATA LOCAL INFILE '/dev/fd/10'
138
INTO TABLE $table_esc
139
$mysql_load_data_format
140
IGNORE 1 LINES
141
EOF
142
}
143

    
144
mysql_export() # does not support CSV
145
# caller usage: {query=... | table=... [cols=...] [filter=...]} mysql_export
146
# cmd line usage: [limit=...] caller
147
{
148
	echo_func
149
	mk_select_var
150
	
151
	output_data=1 mysql_ANSI "$@" <<<"$query"
152
}
153

    
154
mysql_export_outfile() # supports CSV, but requires the FILE privilege
155
{
156
	echo_func
157
	: "${file:?}"
158
	mk_select_var
159
	local mysql_load_data_format="${mysql_load_data_format-\
160
FIELDS TERMINATED BY ','
161
OPTIONALLY ENCLOSED BY '\"'
162
}"
163
	
164
	local head="${query%%FROM*}" # includes trailing newline
165
	head="${head%
166
}"
167
	local tail="${query#$head}"
168
	mysql_load_data_format="${mysql_load_data_format%
169
}"
170
	mysql_ANSI "$@" <<EOF
171
$head
172
INTO OUTFILE '$file'
173
$mysql_load_data_format
174
$tail
175
EOF
176
}
177

    
178
mysqldump() # usage: [schema=1 | data=1] mysqldump db [table...]
179
{
180
	echo_func; kw_params schema data
181
	
182
	mysql_cmd ${database:+--databases "$database" --tables } \
183
--quick --lock-tables=false --set-charset \
184
${postgres_compat:+--compatible=postgresql --add-locks=false }\
185
${schema:+--no-data }${data:+--no-create-info }"$@"
186
}
187

    
188
mysqldump_diffable()
189
{
190
	echo_func
191
	mysqldump "$@"|{ pipe_delay; echo_run sed 's/^(-- Dump completed).*$/\1/'; }
192
}
193

    
194
mysql_rm_privileged_statements()
195
{
196
	echo_func
197
	grep -vE -e '^/\*!40000 ALTER TABLE `.*` (DIS|EN)ABLE KEYS \*/;$' \
198
-e '^(UN)?LOCK TABLES( `.*` WRITE)?;$'
199
}
200

    
201
## permissions
202

    
203
mysql_seal_table() # usage: table=... user=... mysql_seal_table
204
# prevents further modifications to a table by a user
205
{
206
	echo_func; kw_params table user; : "${table:?}" "${user:?}"; mk_table_esc
207
	
208
	benign_error=1 mysql_root <<<"REVOKE ALL PRIVILEGES ON $table FROM '$user'@'%'" || true
209
	benign_error=1 mysql_root <<<"REVOKE GRANT OPTION   ON $table FROM '$user'@'%'" || true
210
}
211

    
212

    
213
### PostgreSQL
214

    
215
pg_export()
216
{
217
	echo_func
218
	mk_select_var
219
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
220
	
221
	psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
222
}
223

    
224
pg_header()
225
{
226
	echo_func
227
	local pg_copy_format="CSV HEADER" limit=0
228
	pg_export "$@"|echo_stdout
229
}
230

    
231
pg_export_table_no_header()
232
{
233
	echo_func
234
	local pg_copy_format="CSV"
235
	pg_export "$@"
236
}
237

    
238
pg_export_table_to_dir_no_header()
239
{
240
	echo_func
241
	local table="$1"; shift; mk_table_esc
242
	stdout="$exports_dir/$table.no_header.cols=$(pg_header).csv" to_file \
243
pg_export_table_no_header "$@"
244
}
245

    
246
fi
(3-3/8)