Revision 9074
Added by Aaron Marcuse-Kubitza over 11 years ago
db.sh | ||
---|---|---|
10 | 10 |
|
11 | 11 |
quote='"' |
12 | 12 |
|
13 |
esc_name () { echo "$quote${1//$quote/$quote$quote}$quote"; }
|
|
13 |
esc_name() { echo "$quote${1//$quote/$quote$quote}$quote"; } |
|
14 | 14 |
|
15 |
mk_esc_name () { set_var "$1"_esc "$(esc_name "${!1}")"; }
|
|
15 |
mk_esc_name() { set_var "$1"_esc "$(esc_name "${!1}")"; } |
|
16 | 16 |
|
17 | 17 |
alias mk_schema_esc='declare schema_esc; mk_esc_name schema' |
18 | 18 |
alias mk_table_esc='declare table_esc; mk_esc_name table' |
... | ... | |
20 | 20 |
fi # load new aliases |
21 | 21 |
if self_being_included; then |
22 | 22 |
|
23 |
log_sql () { test "$verbosity" -ge 2; }
|
|
23 |
log_sql() { test "$verbosity" -ge 2; } |
|
24 | 24 |
|
25 | 25 |
|
26 | 26 |
### MySQL |
27 | 27 |
|
28 | 28 |
# auto-adds connection/login opts when specified |
29 |
mysql_cmd () # usage: mysql* () { ...; mysql_cmd "$@"; }
|
|
29 |
mysql_cmd() # usage: mysql*() { ...; mysql_cmd "$@"; }
|
|
30 | 30 |
{ |
31 | 31 |
echo_func |
32 | 32 |
local ssh_server="$(localize_url "$ssh_server")" |
... | ... | |
42 | 42 |
${password+="$password"} ${database:+--databases "$database" --tables } "$@" |
43 | 43 |
} |
44 | 44 |
|
45 |
mysql () { echo_func; mysql_cmd --verbose "$@"; }
|
|
45 |
mysql() { echo_func; mysql_cmd --verbose "$@"; } |
|
46 | 46 |
|
47 |
mysql_ANSI ()
|
|
47 |
mysql_ANSI() |
|
48 | 48 |
{ |
49 | 49 |
echo_func |
50 | 50 |
(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@" |
51 | 51 |
} |
52 | 52 |
|
53 |
mysqldump () # usage: [schema=1 | data=1] mysqldump db [table...]
|
|
53 |
mysqldump() # usage: [schema=1 | data=1] mysqldump db [table...] |
|
54 | 54 |
{ |
55 | 55 |
echo_func |
56 | 56 |
mysql_cmd --quick --lock-tables=false --set-charset \ |
... | ... | |
58 | 58 |
${schema:+--no-data }${data:+--no-create-info }"$@" |
59 | 59 |
} |
60 | 60 |
|
61 |
mysqldump_diffable ()
|
|
61 |
mysqldump_diffable() |
|
62 | 62 |
{ |
63 | 63 |
echo_func |
64 | 64 |
mysqldump "$@"|{ pipe_delay; sed 's/^(-- Dump completed).*$/\1/'; } |
... | ... | |
67 | 67 |
|
68 | 68 |
### PostgreSQL |
69 | 69 |
|
70 |
pg_copy_to ()
|
|
70 |
pg_copy_to() |
|
71 | 71 |
{ |
72 | 72 |
echo_func |
73 | 73 |
if test ! "$source"; then |
... | ... | |
81 | 81 |
psql "$@" <<<"COPY $source TO STDOUT $pg_copy_format;" |
82 | 82 |
} |
83 | 83 |
|
84 |
pg_header ()
|
|
84 |
pg_header() |
|
85 | 85 |
{ |
86 | 86 |
echo_func |
87 | 87 |
local pg_copy_format="CSV HEADER" limit=0 |
88 | 88 |
pg_copy_to "$@"|echo_stdout |
89 | 89 |
} |
90 | 90 |
|
91 |
pg_export_table_no_header ()
|
|
91 |
pg_export_table_no_header() |
|
92 | 92 |
{ |
93 | 93 |
echo_func |
94 | 94 |
local pg_copy_format="CSV" |
95 | 95 |
pg_copy_to "$@" |
96 | 96 |
} |
97 | 97 |
|
98 |
pg_export_table_to_dir_no_header ()
|
|
98 |
pg_export_table_to_dir_no_header() |
|
99 | 99 |
{ |
100 | 100 |
echo_func |
101 | 101 |
local table="$1"; shift; mk_table_esc |
Also available in: Unified diff
*{.sh,run}: removed extra space between function name and (), which is apparently not needed even though `help function` includes it. this greatly improves readability, including when function names are pasted into commit messages!