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"'; mk_esc_name '"${1%_esc}"''; }
20

    
21
mk_esc_name_alias schema_esc
22
mk_esc_name_alias table_esc
23

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

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

    
38

    
39
### MySQL
40

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

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

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

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

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

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

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

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

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

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

    
137

    
138
### PostgreSQL
139

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

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

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

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

    
172
fi
(2-2/5)