Project

General

Profile

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
	local tail="${query#$head}"
88
	mysql_ANSI "$@" <<EOF
89
${head}INTO OUTFILE '$file'
90
$mysql_load_data_format$tail
91
EOF
92
}
93

    
94
mysqldump() # usage: [schema=1 | data=1] mysqldump db [table...]
95
{
96
	echo_func
97
	mysql_cmd ${database:+--databases "$database" --tables } \
98
--quick --lock-tables=false --set-charset \
99
${postgres_compat:+--compatible=postgresql --add-locks=false }\
100
${schema:+--no-data }${data:+--no-create-info }"$@"
101
}
102

    
103
mysqldump_diffable()
104
{
105
	echo_func
106
	mysqldump "$@"|{ pipe_delay; sed 's/^(-- Dump completed).*$/\1/'; }
107
}
108

    
109

    
110
### PostgreSQL
111

    
112
pg_export()
113
{
114
	echo_func
115
	mk_select
116
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
117
	
118
	psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
119
}
120

    
121
pg_header()
122
{
123
	echo_func
124
	local pg_copy_format="CSV HEADER" limit=0
125
	pg_export "$@"|echo_stdout
126
}
127

    
128
pg_export_table_no_header()
129
{
130
	echo_func
131
	local pg_copy_format="CSV"
132
	pg_export "$@"
133
}
134

    
135
pg_export_table_to_dir_no_header()
136
{
137
	echo_func
138
	local table="$1"; shift; mk_table_esc
139
	local cols="$(pg_header)"
140
	stdout="$exports_dir/$table.no_header.cols=$cols.csv" to_file \
141
pg_export_table_no_header "$@"
142
}
143

    
144
fi
(2-2/5)