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

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

    
37

    
38
### MySQL
39

    
40
alias set_database=\
41
'if test "$schema"; then declare database="${database-$schema}"; fi'
42

    
43
fi # load new aliases
44
if self_being_included; then
45

    
46
# usage: mysql*() { ...; mysql_cmd "$@"; } (with alias)
47
function mysql_cmd() # usage: mysql*() { ...; mysql_cmd cmd "$@"; }
48
# auto-adds connection/login opts when specified
49
{
50
	echo_func
51
	local cmd="$1"; shift
52
	local ssh_server="$(localize_url "$ssh_server")"
53
	local server="$(localize_url "$server")"
54
	if test "$ssh_server"; then
55
		local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
56
	fi
57
	
58
	local var=ssh_dest; local_inv
59
	command ${ssh_dest:+ssh "$ssh_dest" }"$cmd" \
60
${server:+ --host="$server" }${user:+--user="$user" } --password\
61
${password+="$password"} "$@"
62
}
63
alias mysql_cmd='mysql_cmd "$FUNCNAME"'
64

    
65
fi # load new aliases
66
if self_being_included; then
67

    
68
mysql() # usage: [output_data=1] mysql ...
69
{
70
	echo_func; kw_params output_data
71
	set_database
72
	
73
	set -- ${database:+--database="$database" }--local-infile=1 --column-names \
74
"$@"
75
	if test "$output_data"; then echo_stdin|mysql_cmd --batch "$@"
76
	else cmd_log_fd=1 mysql_cmd --verbose "$@"
77
	fi
78
}
79

    
80
mysql_ANSI()
81
{
82
	echo_func
83
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
84
}
85

    
86
mysql_import() # usage: table=... [cols=...] mysql_import <file
87
{
88
	echo_func
89
	mk_table_esc
90
	local mysql_load_data_format="${mysql_load_data_format-\
91
FIELDS TERMINATED BY ','
92
OPTIONALLY ENCLOSED BY '\"'
93
}"
94
	
95
	mysql_load_data_format="${mysql_load_data_format%
96
}"
97
	mysql_ANSI "$@" 10<&0 <<EOF
98
LOAD DATA LOCAL INFILE '/dev/fd/10'
99
INTO TABLE $table_esc
100
$mysql_load_data_format
101
IGNORE 1 LINES
102
EOF
103
}
104

    
105
mysql_export() # does not support CSV
106
# caller usage: {query=... | table=... [cols=...] [filter=...]} mysql_export
107
# cmd line usage: [limit=...] caller
108
{
109
	echo_func
110
	mk_select
111
	
112
	output_data=1 mysql_ANSI "$@" <<<"$query"
113
}
114

    
115
mysql_export_outfile() # supports CSV, but requires the FILE privilege
116
{
117
	echo_func
118
	: "${file:?}"
119
	mk_select
120
	local mysql_load_data_format="${mysql_load_data_format-\
121
FIELDS TERMINATED BY ','
122
OPTIONALLY ENCLOSED BY '\"'
123
}"
124
	
125
	local head="${query%%FROM*}" # includes trailing newline
126
	head="${head%
127
}"
128
	local tail="${query#$head}"
129
	mysql_load_data_format="${mysql_load_data_format%
130
}"
131
	mysql_ANSI "$@" <<EOF
132
$head
133
INTO OUTFILE '$file'
134
$mysql_load_data_format
135
$tail
136
EOF
137
}
138

    
139
mysqldump() # usage: [schema=1 | data=1] mysqldump db [table...]
140
{
141
	echo_func; kw_params schema data
142
	
143
	mysql_cmd ${database:+--databases "$database" --tables } \
144
--quick --lock-tables=false --set-charset \
145
${postgres_compat:+--compatible=postgresql --add-locks=false }\
146
${schema:+--no-data }${data:+--no-create-info }"$@"
147
}
148

    
149
mysqldump_diffable()
150
{
151
	echo_func
152
	mysqldump "$@"|{ pipe_delay; sed 's/^(-- Dump completed).*$/\1/'; }
153
}
154

    
155

    
156
### PostgreSQL
157

    
158
pg_export()
159
{
160
	echo_func
161
	mk_select
162
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
163
	
164
	psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
165
}
166

    
167
pg_header()
168
{
169
	echo_func
170
	local pg_copy_format="CSV HEADER" limit=0
171
	pg_export "$@"|echo_stdout
172
}
173

    
174
pg_export_table_no_header()
175
{
176
	echo_func
177
	local pg_copy_format="CSV"
178
	pg_export "$@"
179
}
180

    
181
pg_export_table_to_dir_no_header()
182
{
183
	echo_func
184
	local table="$1"; shift; mk_table_esc
185
	local cols="$(pg_header)"
186
	stdout="$exports_dir/$table.no_header.cols=$cols.csv" to_file \
187
pg_export_table_no_header "$@"
188
}
189

    
190
fi
(2-2/5)