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_ANSI()
114
{
115
	echo_func
116
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
117
}
118

    
119
mysql_truncate() { echo_func; mk_truncate|mysql_ANSI; }
120

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

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

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

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

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

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

    
199

    
200
### PostgreSQL
201

    
202
pg_export()
203
{
204
	echo_func
205
	mk_select_var
206
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
207
	
208
	psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
209
}
210

    
211
pg_header()
212
{
213
	echo_func
214
	local pg_copy_format="CSV HEADER" limit=0
215
	pg_export "$@"|echo_stdout
216
}
217

    
218
pg_export_table_no_header()
219
{
220
	echo_func
221
	local pg_copy_format="CSV"
222
	pg_export "$@"
223
}
224

    
225
pg_export_table_to_dir_no_header()
226
{
227
	echo_func
228
	local table="$1"; shift; mk_table_esc
229
	stdout="$exports_dir/$table.no_header.cols=$(pg_header).csv" to_file \
230
pg_export_table_no_header "$@"
231
}
232

    
233
fi
(3-3/8)