Project

General

Profile

1 9014 aaronmk
#!/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 9167 aaronmk
alias use_local_remote='{ use_local; use_remote; }'
10
	# *must* be run inside a function
11 9014 aaronmk
12
quote='"'
13
14 9074 aaronmk
esc_name() { echo "$quote${1//$quote/$quote$quote}$quote"; }
15 9014 aaronmk
16 9074 aaronmk
mk_esc_name() { set_var "$1"_esc "$(esc_name "${!1}")"; }
17 9014 aaronmk
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 9348 aaronmk
# export func usage: export_func() { ...; mk_select; ... }
22
# caller usage: {query=... | table=... [cols=...] [filter=...]} export_func
23
# cmd line usage: [limit=...] caller
24 9088 aaronmk
alias mk_select='mk_table_esc
25 9334 aaronmk
declare query="$(rtrim "${query:-SELECT ${cols:-*} ${cols:+
26 9335 aaronmk
}FROM $table_esc
27
${filter:+WHERE $filter
28
}}${limit:+LIMIT $limit
29
}")"
30 9333 aaronmk
'
31 9088 aaronmk
32 9014 aaronmk
fi # load new aliases
33
if self_being_included; then
34
35 9074 aaronmk
log_sql() { test "$verbosity" -ge 2; }
36 9014 aaronmk
37 9017 aaronmk
38 9014 aaronmk
### MySQL
39
40 9094 aaronmk
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 9099 aaronmk
# usage: mysql*() { ...; mysql_cmd "$@"; } (with alias)
47
function mysql_cmd() # usage: mysql*() { ...; mysql_cmd cmd "$@"; }
48 9271 aaronmk
# auto-adds connection/login opts when specified
49 9014 aaronmk
{
50
	echo_func
51 9099 aaronmk
	local cmd="$1"; shift
52 9014 aaronmk
	local ssh_server="$(localize_url "$ssh_server")"
53
	local server="$(localize_url "$server")"
54 9050 aaronmk
	if test "$ssh_server"; then
55 9014 aaronmk
		local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
56
	fi
57
58
	local var=ssh_dest; local_inv
59 9099 aaronmk
	command ${ssh_dest:+ssh "$ssh_dest" }"$cmd" \
60 9014 aaronmk
${server:+ --host="$server" }${user:+--user="$user" } --password\
61 9095 aaronmk
${password+="$password"} "$@"
62 9014 aaronmk
}
63 9099 aaronmk
alias mysql_cmd='mysql_cmd "$FUNCNAME"'
64 9014 aaronmk
65 9099 aaronmk
fi # load new aliases
66
if self_being_included; then
67
68 9101 aaronmk
mysql() # usage: [output_data=1] mysql ...
69 9095 aaronmk
{
70 9235 aaronmk
	echo_func; kw_params output_data
71 9095 aaronmk
	set_database
72 9101 aaronmk
73 9151 aaronmk
	set -- ${database:+--database="$database" }--column-names "$@"
74 9150 aaronmk
	if test "$output_data"; then echo_stdin|mysql_cmd --batch "$@"
75 9214 aaronmk
	else cmd_log_fd=1 mysql_cmd --verbose "$@"
76 9101 aaronmk
	fi
77 9095 aaronmk
}
78 9014 aaronmk
79 9074 aaronmk
mysql_ANSI()
80 9014 aaronmk
{
81
	echo_func
82
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
83
}
84
85 9152 aaronmk
mysql_export() # does not support CSV
86 9348 aaronmk
# caller usage: {query=... | table=... [cols=...] [filter=...]} mysql_export
87
# cmd line usage: [limit=...] caller
88 9102 aaronmk
{
89
	echo_func
90 9152 aaronmk
	mk_select
91
92
	output_data=1 mysql_ANSI "$@" <<<"$query"
93
}
94
95
mysql_export_outfile() # supports CSV, but requires the FILE privilege
96
{
97
	echo_func
98 9102 aaronmk
	: "${file:?}"
99
	mk_select
100
	local mysql_load_data_format="${mysql_load_data_format-\
101
FIELDS TERMINATED BY ','
102
OPTIONALLY ENCLOSED BY '\"'
103
}"
104
105
	local head="${query%%FROM*}" # includes trailing newline
106 9105 aaronmk
	head="${head%
107
}"
108 9102 aaronmk
	local tail="${query#$head}"
109 9106 aaronmk
	mysql_load_data_format="${mysql_load_data_format%
110
}"
111 9102 aaronmk
	mysql_ANSI "$@" <<EOF
112 9105 aaronmk
$head
113
INTO OUTFILE '$file'
114 9106 aaronmk
$mysql_load_data_format
115
$tail
116 9102 aaronmk
EOF
117
}
118
119 9074 aaronmk
mysqldump() # usage: [schema=1 | data=1] mysqldump db [table...]
120 9014 aaronmk
{
121 9235 aaronmk
	echo_func; kw_params schema data
122 9231 aaronmk
123 9095 aaronmk
	mysql_cmd ${database:+--databases "$database" --tables } \
124
--quick --lock-tables=false --set-charset \
125 9014 aaronmk
${postgres_compat:+--compatible=postgresql --add-locks=false }\
126
${schema:+--no-data }${data:+--no-create-info }"$@"
127
}
128
129 9074 aaronmk
mysqldump_diffable()
130 9014 aaronmk
{
131
	echo_func
132 9066 aaronmk
	mysqldump "$@"|{ pipe_delay; sed 's/^(-- Dump completed).*$/\1/'; }
133 9014 aaronmk
}
134
135 9017 aaronmk
136 9014 aaronmk
### PostgreSQL
137
138 9083 aaronmk
pg_export()
139 9014 aaronmk
{
140
	echo_func
141 9088 aaronmk
	mk_select
142 9014 aaronmk
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
143
144 9084 aaronmk
	psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
145 9014 aaronmk
}
146
147 9074 aaronmk
pg_header()
148 9014 aaronmk
{
149
	echo_func
150
	local pg_copy_format="CSV HEADER" limit=0
151 9083 aaronmk
	pg_export "$@"|echo_stdout
152 9014 aaronmk
}
153
154 9074 aaronmk
pg_export_table_no_header()
155 9014 aaronmk
{
156
	echo_func
157
	local pg_copy_format="CSV"
158 9083 aaronmk
	pg_export "$@"
159 9014 aaronmk
}
160
161 9074 aaronmk
pg_export_table_to_dir_no_header()
162 9014 aaronmk
{
163
	echo_func
164
	local table="$1"; shift; mk_table_esc
165
	local cols="$(pg_header)"
166 9042 aaronmk
	stdout="$exports_dir/$table.no_header.cols=$cols.csv" to_file \
167
pg_export_table_no_header "$@"
168 9014 aaronmk
}
169
170
fi