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