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

    
12
quote='"'
13

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

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

    
20
mk_esc_name_alias schema_esc
21
mk_esc_name_alias table_esc
22

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

    
26
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
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
}FROM $table_esc
38
${filter:+WHERE $filter
39
}}")\
40
$(limit)"
41
}
42

    
43
# 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
fi # load new aliases
49
if self_being_included; then
50

    
51
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
mk_truncate() # usage: table=... mk_truncate|*sql_ANSI
58
{
59
	log++; echo_func; kw_params table; : "${table?}"; mk_table_esc
60
	echo "TRUNCATE $table_esc"
61
}
62

    
63

    
64
### MySQL
65

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

    
91
fi # load new aliases
92
if self_being_included; then
93

    
94
mysql() # usage: [output_data=1] mysql ...
95
{
96
	echo_func; kw_params output_data
97
	set_database
98
	
99
	set -- ${database:+--database="$database" }--local-infile=1 --column-names \
100
"$@"
101
	if test "$output_data"; then echo_stdin|mysql_cmd --batch "$@"
102
	else cmd_log_fd=1 mysql_cmd --verbose "$@"
103
	fi
104
}
105

    
106
mysql_ANSI()
107
{
108
	echo_func
109
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
110
}
111

    
112
mysql_truncate() { echo_func; mk_truncate|mysql_ANSI; }
113

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

    
135
mysql_export() # does not support CSV
136
# caller usage: {query=... | table=... [cols=...] [filter=...]} mysql_export
137
# cmd line usage: [limit=...] caller
138
{
139
	echo_func
140
	mk_select_var
141
	
142
	output_data=1 mysql_ANSI "$@" <<<"$query"
143
}
144

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

    
169
mysqldump() # usage: [schema=1 | data=1] mysqldump db [table...]
170
{
171
	echo_func; kw_params schema data
172
	
173
	mysql_cmd ${database:+--databases "$database" --tables } \
174
--quick --lock-tables=false --set-charset \
175
${postgres_compat:+--compatible=postgresql --add-locks=false }\
176
${schema:+--no-data }${data:+--no-create-info }"$@"
177
}
178

    
179
mysqldump_diffable()
180
{
181
	echo_func
182
	mysqldump "$@"|{ pipe_delay; sed 's/^(-- Dump completed).*$/\1/'; }
183
}
184

    
185

    
186
### PostgreSQL
187

    
188
pg_export()
189
{
190
	echo_func
191
	mk_select_var
192
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
193
	
194
	psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
195
}
196

    
197
pg_header()
198
{
199
	echo_func
200
	local pg_copy_format="CSV HEADER" limit=0
201
	pg_export "$@"|echo_stdout
202
}
203

    
204
pg_export_table_no_header()
205
{
206
	echo_func
207
	local pg_copy_format="CSV"
208
	pg_export "$@"
209
}
210

    
211
pg_export_table_to_dir_no_header()
212
{
213
	echo_func
214
	local table="$1"; shift; mk_table_esc
215
	stdout="$exports_dir/$table.no_header.cols=$(pg_header).csv" to_file \
216
pg_export_table_no_header "$@"
217
}
218

    
219
fi
(2-2/5)