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() { set_var "$1"_esc "$(esc_name "${!1}")"; }
17

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

    
22
mk_esc_name_alias schema_esc
23
mk_esc_name_alias table_esc
24

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

    
36
fi # load new aliases
37
if self_being_included; then
38

    
39

    
40
### MySQL
41

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

    
45
fi # load new aliases
46
if self_being_included; then
47

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

    
67
fi # load new aliases
68
if self_being_included; then
69

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

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

    
88
mysql_export() # does not support CSV
89
# caller usage: {query=... | table=... [cols=...] [filter=...]} mysql_export
90
# cmd line usage: [limit=...] caller
91
{
92
	echo_func
93
	mk_select
94
	
95
	output_data=1 mysql_ANSI "$@" <<<"$query"
96
}
97

    
98
mysql_export_outfile() # supports CSV, but requires the FILE privilege
99
{
100
	echo_func
101
	: "${file:?}"
102
	mk_select
103
	local mysql_load_data_format="${mysql_load_data_format-\
104
FIELDS TERMINATED BY ','
105
OPTIONALLY ENCLOSED BY '\"'
106
}"
107
	
108
	local head="${query%%FROM*}" # includes trailing newline
109
	head="${head%
110
}"
111
	local tail="${query#$head}"
112
	mysql_load_data_format="${mysql_load_data_format%
113
}"
114
	mysql_ANSI "$@" <<EOF
115
$head
116
INTO OUTFILE '$file'
117
$mysql_load_data_format
118
$tail
119
EOF
120
}
121

    
122
mysqldump() # usage: [schema=1 | data=1] mysqldump db [table...]
123
{
124
	echo_func; kw_params schema data
125
	
126
	mysql_cmd ${database:+--databases "$database" --tables } \
127
--quick --lock-tables=false --set-charset \
128
${postgres_compat:+--compatible=postgresql --add-locks=false }\
129
${schema:+--no-data }${data:+--no-create-info }"$@"
130
}
131

    
132
mysqldump_diffable()
133
{
134
	echo_func
135
	mysqldump "$@"|{ pipe_delay; sed 's/^(-- Dump completed).*$/\1/'; }
136
}
137

    
138

    
139
### PostgreSQL
140

    
141
pg_export()
142
{
143
	echo_func
144
	mk_select
145
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
146
	
147
	psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
148
}
149

    
150
pg_header()
151
{
152
	echo_func
153
	local pg_copy_format="CSV HEADER" limit=0
154
	pg_export "$@"|echo_stdout
155
}
156

    
157
pg_export_table_no_header()
158
{
159
	echo_func
160
	local pg_copy_format="CSV"
161
	pg_export "$@"
162
}
163

    
164
pg_export_table_to_dir_no_header()
165
{
166
	echo_func
167
	local table="$1"; shift; mk_table_esc
168
	local cols="$(pg_header)"
169
	stdout="$exports_dir/$table.no_header.cols=$cols.csv" to_file \
170
pg_export_table_no_header "$@"
171
}
172

    
173
fi
(2-2/5)