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; }'
10
	# *must* be run inside a function
11

    
12
quote='"'
13

    
14
esc_name() { echo "$quote${1//$quote/$quote$quote}$quote"; }
15

    
16
mk_esc_name() { set_var "$1"_esc "$(esc_name "${!1}")"; }
17

    
18
alias mk_schema_esc='declare schema_esc; mk_esc_name schema'
19
alias mk_table_esc='declare table_esc; mk_esc_name table'
20

    
21
alias mk_select='mk_table_esc
22
declare query="$(rtrim "${query:-SELECT * FROM $table_esc${filter:+
23
WHERE $filter
24
}}${limit:+
25
LIMIT $limit}")"
26
'
27

    
28
fi # load new aliases
29
if self_being_included; then
30

    
31
log_sql() { test "$verbosity" -ge 2; }
32

    
33

    
34
### MySQL
35

    
36
alias set_database=\
37
'if test "$schema"; then declare database="${database-$schema}"; fi'
38

    
39
fi # load new aliases
40
if self_being_included; then
41

    
42
# usage: mysql*() { ...; mysql_cmd "$@"; } (with alias)
43
function mysql_cmd() # usage: mysql*() { ...; mysql_cmd cmd "$@"; }
44
# auto-adds connection/login opts when specified
45
{
46
	echo_func
47
	local cmd="$1"; shift
48
	local ssh_server="$(localize_url "$ssh_server")"
49
	local server="$(localize_url "$server")"
50
	if test "$ssh_server"; then
51
		local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
52
	fi
53
	
54
	local var=ssh_dest; local_inv
55
	command ${ssh_dest:+ssh "$ssh_dest" }"$cmd" \
56
${server:+ --host="$server" }${user:+--user="$user" } --password\
57
${password+="$password"} "$@"
58
}
59
alias mysql_cmd='mysql_cmd "$FUNCNAME"'
60

    
61
fi # load new aliases
62
if self_being_included; then
63

    
64
mysql() # usage: [output_data=1] mysql ...
65
{
66
	echo_func; kw_params output_data
67
	set_database
68
	
69
	set -- ${database:+--database="$database" }--column-names "$@"
70
	if test "$output_data"; then echo_stdin|mysql_cmd --batch "$@"
71
	else cmd_log_fd=1 mysql_cmd --verbose "$@"
72
	fi
73
}
74

    
75
mysql_ANSI()
76
{
77
	echo_func
78
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
79
}
80

    
81
mysql_export() # does not support CSV
82
{
83
	echo_func
84
	mk_select
85
	
86
	output_data=1 mysql_ANSI "$@" <<<"$query"
87
}
88

    
89
mysql_export_outfile() # supports CSV, but requires the FILE privilege
90
{
91
	echo_func
92
	: "${file:?}"
93
	mk_select
94
	local mysql_load_data_format="${mysql_load_data_format-\
95
FIELDS TERMINATED BY ','
96
OPTIONALLY ENCLOSED BY '\"'
97
}"
98
	
99
	local head="${query%%FROM*}" # includes trailing newline
100
	head="${head%
101
}"
102
	local tail="${query#$head}"
103
	mysql_load_data_format="${mysql_load_data_format%
104
}"
105
	mysql_ANSI "$@" <<EOF
106
$head
107
INTO OUTFILE '$file'
108
$mysql_load_data_format
109
$tail
110
EOF
111
}
112

    
113
mysqldump() # usage: [schema=1 | data=1] mysqldump db [table...]
114
{
115
	echo_func; kw_params schema data
116
	
117
	mysql_cmd ${database:+--databases "$database" --tables } \
118
--quick --lock-tables=false --set-charset \
119
${postgres_compat:+--compatible=postgresql --add-locks=false }\
120
${schema:+--no-data }${data:+--no-create-info }"$@"
121
}
122

    
123
mysqldump_diffable()
124
{
125
	echo_func
126
	mysqldump "$@"|{ pipe_delay; sed 's/^(-- Dump completed).*$/\1/'; }
127
}
128

    
129

    
130
### PostgreSQL
131

    
132
pg_export()
133
{
134
	echo_func
135
	mk_select
136
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
137
	
138
	psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
139
}
140

    
141
pg_header()
142
{
143
	echo_func
144
	local pg_copy_format="CSV HEADER" limit=0
145
	pg_export "$@"|echo_stdout
146
}
147

    
148
pg_export_table_no_header()
149
{
150
	echo_func
151
	local pg_copy_format="CSV"
152
	pg_export "$@"
153
}
154

    
155
pg_export_table_to_dir_no_header()
156
{
157
	echo_func
158
	local table="$1"; shift; mk_table_esc
159
	local cols="$(pg_header)"
160
	stdout="$exports_dir/$table.no_header.cols=$cols.csv" to_file \
161
pg_export_table_no_header "$@"
162
}
163

    
164
fi
(2-2/5)