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 -n "$ssh_server"; then
|
35
|
local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
|
36
|
fi
|
37
|
if test -n "$schema"; then local database="${database-$schema}"; fi
|
38
|
|
39
|
local var=ssh_dest; local_inv
|
40
|
extern ${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 "$@"|sed 's/^(-- Dump completed).*$/\1/'
|
65
|
}
|
66
|
|
67
|
|
68
|
### PostgreSQL
|
69
|
|
70
|
pg_copy_to ()
|
71
|
{
|
72
|
echo_func
|
73
|
if test -z "$source"; then
|
74
|
: "${table:?}"; mk_table_esc
|
75
|
if test -z "$limit"; then local source="$table_esc"
|
76
|
else local source="(SELECT * FROM $table_esc LIMIT $limit)"
|
77
|
fi
|
78
|
fi
|
79
|
local pg_copy_format="${pg_copy_format-CSV HEADER}"
|
80
|
|
81
|
psql "$@" <<<"COPY $source TO STDOUT $pg_copy_format;"
|
82
|
}
|
83
|
|
84
|
pg_header ()
|
85
|
{
|
86
|
echo_func
|
87
|
local pg_copy_format="CSV HEADER" limit=0
|
88
|
pg_copy_to "$@"|echo_stdout
|
89
|
}
|
90
|
|
91
|
pg_export_table_no_header ()
|
92
|
{
|
93
|
echo_func
|
94
|
local pg_copy_format="CSV"
|
95
|
pg_copy_to "$@"
|
96
|
}
|
97
|
|
98
|
pg_export_table_to_dir_no_header ()
|
99
|
{
|
100
|
echo_func
|
101
|
local table="$1"; shift; mk_table_esc
|
102
|
local cols="$(pg_header)"
|
103
|
stdout="$exports_dir/$table.no_header.cols=$cols.csv" to_file \
|
104
|
pg_export_table_no_header "$@"
|
105
|
}
|
106
|
|
107
|
fi
|