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
|
|
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
|
fi # load new aliases
|
21
|
if self_being_included; then
|
22
|
|
23
|
log_sql() { test "$verbosity" -ge 2; }
|
24
|
|
25
|
|
26
|
### MySQL
|
27
|
|
28
|
# auto-adds connection/login opts when specified
|
29
|
mysql_cmd() # usage: mysql*() { ...; mysql_cmd "$@"; }
|
30
|
{
|
31
|
echo_func
|
32
|
local ssh_server="$(localize_url "$ssh_server")"
|
33
|
local server="$(localize_url "$server")"
|
34
|
if test "$ssh_server"; then
|
35
|
local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
|
36
|
fi
|
37
|
if test "$schema"; then local database="${database-$schema}"; fi
|
38
|
|
39
|
local var=ssh_dest; local_inv
|
40
|
command ${ssh_dest:+ssh "$ssh_dest" }"${FUNCNAME[1]}" \
|
41
|
${server:+ --host="$server" }${user:+--user="$user" } --password\
|
42
|
${password+="$password"} ${database:+--databases "$database" --tables } "$@"
|
43
|
}
|
44
|
|
45
|
mysql() { echo_func; mysql_cmd --verbose "$@"; }
|
46
|
|
47
|
mysql_ANSI()
|
48
|
{
|
49
|
echo_func
|
50
|
(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
|
51
|
}
|
52
|
|
53
|
mysqldump() # usage: [schema=1 | data=1] mysqldump db [table...]
|
54
|
{
|
55
|
echo_func
|
56
|
mysql_cmd --quick --lock-tables=false --set-charset \
|
57
|
${postgres_compat:+--compatible=postgresql --add-locks=false }\
|
58
|
${schema:+--no-data }${data:+--no-create-info }"$@"
|
59
|
}
|
60
|
|
61
|
mysqldump_diffable()
|
62
|
{
|
63
|
echo_func
|
64
|
mysqldump "$@"|{ pipe_delay; sed 's/^(-- Dump completed).*$/\1/'; }
|
65
|
}
|
66
|
|
67
|
|
68
|
### PostgreSQL
|
69
|
|
70
|
pg_export()
|
71
|
{
|
72
|
echo_func
|
73
|
mk_table_esc
|
74
|
local query="${query:-SELECT * FROM $table_esc ${limit:+LIMIT $limit }}"
|
75
|
query="$(rtrim "$query")"
|
76
|
local pg_copy_format="${pg_copy_format-CSV HEADER}"
|
77
|
|
78
|
psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
|
79
|
}
|
80
|
|
81
|
pg_header()
|
82
|
{
|
83
|
echo_func
|
84
|
local pg_copy_format="CSV HEADER" limit=0
|
85
|
pg_export "$@"|echo_stdout
|
86
|
}
|
87
|
|
88
|
pg_export_table_no_header()
|
89
|
{
|
90
|
echo_func
|
91
|
local pg_copy_format="CSV"
|
92
|
pg_export "$@"
|
93
|
}
|
94
|
|
95
|
pg_export_table_to_dir_no_header()
|
96
|
{
|
97
|
echo_func
|
98
|
local table="$1"; shift; mk_table_esc
|
99
|
local cols="$(pg_header)"
|
100
|
stdout="$exports_dir/$table.no_header.cols=$cols.csv" to_file \
|
101
|
pg_export_table_no_header "$@"
|
102
|
}
|
103
|
|
104
|
fi
|