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
alias mk_schema_esc='declare schema_esc; mk_esc_name schema'
19
alias mk_table_esc='declare table_esc; mk_esc_name table'
20

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

    
32
fi # load new aliases
33
if self_being_included; then
34

    
35

    
36
### MySQL
37

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

    
41
fi # load new aliases
42
if self_being_included; then
43

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

    
63
fi # load new aliases
64
if self_being_included; then
65

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

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

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

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

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

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

    
134

    
135
### PostgreSQL
136

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

    
146
pg_header()
147
{
148
	echo_func
149
	local pg_copy_format="CSV HEADER" limit=0
150
	pg_export "$@"|echo_stdout
151
}
152

    
153
pg_export_table_no_header()
154
{
155
	echo_func
156
	local pg_copy_format="CSV"
157
	pg_export "$@"
158
}
159

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

    
169
fi
(2-2/5)