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
# export func usage: export_func() { ...; mk_select_var; ... }
24
# caller usage: {query=... | table=... [cols=...] [filter=...]} export_func
25
# cmd line usage: [limit=...] caller
26
alias mk_select_var='mk_table_esc
27
declare query="$(rtrim "${query:-SELECT ${cols:-*} ${cols:+
28
}FROM $table_esc
29
${filter:+WHERE $filter
30
}}")
31
${limit:+LIMIT $limit}"
32
'
33

    
34
fi # load new aliases
35
if self_being_included; then
36

    
37
mk_truncate() # usage: table=... mk_truncate|*sql_ANSI
38
{
39
	log++; echo_func; kw_params table; : "${table?}"; mk_table_esc
40
	echo "TRUNCATE $table_esc"
41
}
42

    
43

    
44
### MySQL
45

    
46
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
# usage: mysql*() { ...; mysql_cmd "$@"; } (with alias)
53
function mysql_cmd() # usage: mysql*() { ...; mysql_cmd cmd "$@"; }
54
# auto-adds connection/login opts when specified
55
{
56
	echo_func
57
	local cmd="$1"; shift
58
	local ssh_server="$(localize_url "$ssh_server")"
59
	local server="$(localize_url "$server")"
60
	if test "$ssh_server"; then
61
		local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
62
	fi
63
	
64
	local var=ssh_dest; local_inv
65
	command ${ssh_dest:+ssh "$ssh_dest" }"$cmd" \
66
${server:+ --host="$server" }${user:+--user="$user" } --password\
67
${password+="$password"} "$@"
68
}
69
alias mysql_cmd='mysql_cmd "$FUNCNAME"'
70

    
71
fi # load new aliases
72
if self_being_included; then
73

    
74
mysql() # usage: [output_data=1] mysql ...
75
{
76
	echo_func; kw_params output_data
77
	set_database
78
	
79
	set -- ${database:+--database="$database" }--local-infile=1 --column-names \
80
"$@"
81
	if test "$output_data"; then echo_stdin|mysql_cmd --batch "$@"
82
	else cmd_log_fd=1 mysql_cmd --verbose "$@"
83
	fi
84
}
85

    
86
mysql_ANSI()
87
{
88
	echo_func
89
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
90
}
91

    
92
mysql_truncate() { echo_func; mk_truncate|mysql_ANSI; }
93

    
94
mysql_import() # usage: table=... [cols=...] [append=1] mysql_import <file
95
# without append=1, first ensures the table is empty
96
{
97
	echo_func
98
	mk_table_esc
99
	local mysql_load_data_format="${mysql_load_data_format-\
100
FIELDS TERMINATED BY ','
101
OPTIONALLY ENCLOSED BY '\"'
102
}"
103
	
104
	if test ! "$append"; then mysql_truncate; fi
105
	mysql_load_data_format="${mysql_load_data_format%
106
}"
107
	mysql_ANSI "$@" 10<&0 <<EOF
108
LOAD DATA LOCAL INFILE '/dev/fd/10'
109
INTO TABLE $table_esc
110
$mysql_load_data_format
111
IGNORE 1 LINES
112
EOF
113
}
114

    
115
mysql_export() # does not support CSV
116
# caller usage: {query=... | table=... [cols=...] [filter=...]} mysql_export
117
# cmd line usage: [limit=...] caller
118
{
119
	echo_func
120
	mk_select_var
121
	
122
	output_data=1 mysql_ANSI "$@" <<<"$query"
123
}
124

    
125
mysql_export_outfile() # supports CSV, but requires the FILE privilege
126
{
127
	echo_func
128
	: "${file:?}"
129
	mk_select_var
130
	local mysql_load_data_format="${mysql_load_data_format-\
131
FIELDS TERMINATED BY ','
132
OPTIONALLY ENCLOSED BY '\"'
133
}"
134
	
135
	local head="${query%%FROM*}" # includes trailing newline
136
	head="${head%
137
}"
138
	local tail="${query#$head}"
139
	mysql_load_data_format="${mysql_load_data_format%
140
}"
141
	mysql_ANSI "$@" <<EOF
142
$head
143
INTO OUTFILE '$file'
144
$mysql_load_data_format
145
$tail
146
EOF
147
}
148

    
149
mysqldump() # usage: [schema=1 | data=1] mysqldump db [table...]
150
{
151
	echo_func; kw_params schema data
152
	
153
	mysql_cmd ${database:+--databases "$database" --tables } \
154
--quick --lock-tables=false --set-charset \
155
${postgres_compat:+--compatible=postgresql --add-locks=false }\
156
${schema:+--no-data }${data:+--no-create-info }"$@"
157
}
158

    
159
mysqldump_diffable()
160
{
161
	echo_func
162
	mysqldump "$@"|{ pipe_delay; sed 's/^(-- Dump completed).*$/\1/'; }
163
}
164

    
165

    
166
### PostgreSQL
167

    
168
pg_export()
169
{
170
	echo_func
171
	mk_select_var
172
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
173
	
174
	psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
175
}
176

    
177
pg_header()
178
{
179
	echo_func
180
	local pg_copy_format="CSV HEADER" limit=0
181
	pg_export "$@"|echo_stdout
182
}
183

    
184
pg_export_table_no_header()
185
{
186
	echo_func
187
	local pg_copy_format="CSV"
188
	pg_export "$@"
189
}
190

    
191
pg_export_table_to_dir_no_header()
192
{
193
	echo_func
194
	local table="$1"; shift; mk_table_esc
195
	local cols="$(pg_header)"
196
	stdout="$exports_dir/$table.no_header.cols=$cols.csv" to_file \
197
pg_export_table_no_header "$@"
198
}
199

    
200
fi
(2-2/5)