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
|
mysql_cmd() # usage: mysql*() { ...; mysql_cmd "$@"; }
|
39
|
{
|
40
|
echo_func
|
41
|
local ssh_server="$(localize_url "$ssh_server")"
|
42
|
local server="$(localize_url "$server")"
|
43
|
if test "$ssh_server"; then
|
44
|
local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
|
45
|
fi
|
46
|
set_database
|
47
|
|
48
|
local var=ssh_dest; local_inv
|
49
|
command ${ssh_dest:+ssh "$ssh_dest" }"${FUNCNAME[1]}" \
|
50
|
${server:+ --host="$server" }${user:+--user="$user" } --password\
|
51
|
${password+="$password"} "$@"
|
52
|
}
|
53
|
|
54
|
mysql()
|
55
|
{
|
56
|
echo_func
|
57
|
set_database
|
58
|
mysql_cmd ${database:+--database="$database" }--verbose "$@"
|
59
|
}
|
60
|
|
61
|
mysql_ANSI()
|
62
|
{
|
63
|
echo_func
|
64
|
(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
|
65
|
}
|
66
|
|
67
|
mysqldump() # usage: [schema=1 | data=1] mysqldump db [table...]
|
68
|
{
|
69
|
echo_func
|
70
|
mysql_cmd ${database:+--databases "$database" --tables } \
|
71
|
--quick --lock-tables=false --set-charset \
|
72
|
${postgres_compat:+--compatible=postgresql --add-locks=false }\
|
73
|
${schema:+--no-data }${data:+--no-create-info }"$@"
|
74
|
}
|
75
|
|
76
|
mysqldump_diffable()
|
77
|
{
|
78
|
echo_func
|
79
|
mysqldump "$@"|{ pipe_delay; sed 's/^(-- Dump completed).*$/\1/'; }
|
80
|
}
|
81
|
|
82
|
|
83
|
### PostgreSQL
|
84
|
|
85
|
pg_export()
|
86
|
{
|
87
|
echo_func
|
88
|
mk_select
|
89
|
local pg_copy_format="${pg_copy_format-CSV HEADER}"
|
90
|
|
91
|
psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
|
92
|
}
|
93
|
|
94
|
pg_header()
|
95
|
{
|
96
|
echo_func
|
97
|
local pg_copy_format="CSV HEADER" limit=0
|
98
|
pg_export "$@"|echo_stdout
|
99
|
}
|
100
|
|
101
|
pg_export_table_no_header()
|
102
|
{
|
103
|
echo_func
|
104
|
local pg_copy_format="CSV"
|
105
|
pg_export "$@"
|
106
|
}
|
107
|
|
108
|
pg_export_table_to_dir_no_header()
|
109
|
{
|
110
|
echo_func
|
111
|
local table="$1"; shift; mk_table_esc
|
112
|
local cols="$(pg_header)"
|
113
|
stdout="$exports_dir/$table.no_header.cols=$cols.csv" to_file \
|
114
|
pg_export_table_no_header "$@"
|
115
|
}
|
116
|
|
117
|
fi
|