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' # *must* be run inside a function
|
10
|
|
11
|
quote='"'
|
12
|
|
13
|
esc_name() { echo "$quote${1//$quote/$quote$quote}$quote"; }
|
14
|
|
15
|
mk_esc_name() { set_var "$1"_esc "$(esc_name "${!1}")"; }
|
16
|
|
17
|
alias mk_schema_esc='declare schema_esc; mk_esc_name schema'
|
18
|
alias mk_table_esc='declare table_esc; mk_esc_name table'
|
19
|
|
20
|
alias mk_select='mk_table_esc
|
21
|
declare query="${query:-SELECT * FROM $table_esc}${limit:+ LIMIT $limit}"'
|
22
|
|
23
|
fi # load new aliases
|
24
|
if self_being_included; then
|
25
|
|
26
|
log_sql() { test "$verbosity" -ge 2; }
|
27
|
|
28
|
|
29
|
### MySQL
|
30
|
|
31
|
alias set_database=\
|
32
|
'if test "$schema"; then declare database="${database-$schema}"; fi'
|
33
|
|
34
|
fi # load new aliases
|
35
|
if self_being_included; then
|
36
|
|
37
|
# auto-adds connection/login opts when specified
|
38
|
# usage: mysql*() { ...; mysql_cmd "$@"; } (with alias)
|
39
|
function mysql_cmd() # usage: mysql*() { ...; mysql_cmd cmd "$@"; }
|
40
|
{
|
41
|
echo_func
|
42
|
local cmd="$1"; shift
|
43
|
local ssh_server="$(localize_url "$ssh_server")"
|
44
|
local server="$(localize_url "$server")"
|
45
|
if test "$ssh_server"; then
|
46
|
local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
|
47
|
fi
|
48
|
|
49
|
local var=ssh_dest; local_inv
|
50
|
command ${ssh_dest:+ssh "$ssh_dest" }"$cmd" \
|
51
|
${server:+ --host="$server" }${user:+--user="$user" } --password\
|
52
|
${password+="$password"} "$@"
|
53
|
}
|
54
|
alias mysql_cmd='mysql_cmd "$FUNCNAME"'
|
55
|
|
56
|
fi # load new aliases
|
57
|
if self_being_included; then
|
58
|
|
59
|
mysql() # usage: [output_data=1] mysql ...
|
60
|
{
|
61
|
echo_func
|
62
|
set_database
|
63
|
|
64
|
set -- ${database:+--database="$database" }"$@"
|
65
|
if test "$output_data"; then echo_stdin|mysql_cmd "$@"
|
66
|
else stdout2stderr=1 limit_stderr_cmd mysql_cmd --verbose "$@"
|
67
|
fi
|
68
|
}
|
69
|
|
70
|
mysql_ANSI()
|
71
|
{
|
72
|
echo_func
|
73
|
(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
|
74
|
}
|
75
|
|
76
|
mysql_export_outfile()
|
77
|
{
|
78
|
echo_func
|
79
|
: "${file:?}"
|
80
|
mk_select
|
81
|
local mysql_load_data_format="${mysql_load_data_format-\
|
82
|
FIELDS TERMINATED BY ','
|
83
|
OPTIONALLY ENCLOSED BY '\"'
|
84
|
}"
|
85
|
|
86
|
local head="${query%%FROM*}" # includes trailing newline
|
87
|
head="${head%
|
88
|
}"
|
89
|
local tail="${query#$head}"
|
90
|
mysql_load_data_format="${mysql_load_data_format%
|
91
|
}"
|
92
|
mysql_ANSI "$@" <<EOF
|
93
|
$head
|
94
|
INTO OUTFILE '$file'
|
95
|
$mysql_load_data_format
|
96
|
$tail
|
97
|
EOF
|
98
|
}
|
99
|
|
100
|
mysqldump() # usage: [schema=1 | data=1] mysqldump db [table...]
|
101
|
{
|
102
|
echo_func
|
103
|
mysql_cmd ${database:+--databases "$database" --tables } \
|
104
|
--quick --lock-tables=false --set-charset \
|
105
|
${postgres_compat:+--compatible=postgresql --add-locks=false }\
|
106
|
${schema:+--no-data }${data:+--no-create-info }"$@"
|
107
|
}
|
108
|
|
109
|
mysqldump_diffable()
|
110
|
{
|
111
|
echo_func
|
112
|
mysqldump "$@"|{ pipe_delay; sed 's/^(-- Dump completed).*$/\1/'; }
|
113
|
}
|
114
|
|
115
|
|
116
|
### PostgreSQL
|
117
|
|
118
|
pg_export()
|
119
|
{
|
120
|
echo_func
|
121
|
mk_select
|
122
|
local pg_copy_format="${pg_copy_format-CSV HEADER}"
|
123
|
|
124
|
psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
|
125
|
}
|
126
|
|
127
|
pg_header()
|
128
|
{
|
129
|
echo_func
|
130
|
local pg_copy_format="CSV HEADER" limit=0
|
131
|
pg_export "$@"|echo_stdout
|
132
|
}
|
133
|
|
134
|
pg_export_table_no_header()
|
135
|
{
|
136
|
echo_func
|
137
|
local pg_copy_format="CSV"
|
138
|
pg_export "$@"
|
139
|
}
|
140
|
|
141
|
pg_export_table_to_dir_no_header()
|
142
|
{
|
143
|
echo_func
|
144
|
local table="$1"; shift; mk_table_esc
|
145
|
local cols="$(pg_header)"
|
146
|
stdout="$exports_dir/$table.no_header.cols=$cols.csv" to_file \
|
147
|
pg_export_table_no_header "$@"
|
148
|
}
|
149
|
|
150
|
fi
|