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" }--column-names "$@"
72
	if test "$output_data"; then echo_stdin|mysql_cmd --batch "$@"
73
	else cmd_log_fd=1 mysql_cmd --verbose "$@"
74
	fi
75
}
76

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

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

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

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

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

    
133

    
134
### PostgreSQL
135

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

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

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

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

    
168
fi
(2-2/5)