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" }--column-names "$@"
65
	if test "$output_data"; then echo_stdin|mysql_cmd --batch "$@"
66
	else limit_stdout_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() # does not support CSV
77
{
78
	echo_func
79
	mk_select
80
	
81
	output_data=1 mysql_ANSI "$@" <<<"$query"
82
}
83

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

    
108
mysqldump() # usage: [schema=1 | data=1] mysqldump db [table...]
109
{
110
	echo_func
111
	mysql_cmd ${database:+--databases "$database" --tables } \
112
--quick --lock-tables=false --set-charset \
113
${postgres_compat:+--compatible=postgresql --add-locks=false }\
114
${schema:+--no-data }${data:+--no-create-info }"$@"
115
}
116

    
117
mysqldump_diffable()
118
{
119
	echo_func
120
	mysqldump "$@"|{ pipe_delay; sed 's/^(-- Dump completed).*$/\1/'; }
121
}
122

    
123

    
124
### PostgreSQL
125

    
126
pg_export()
127
{
128
	echo_func
129
	mk_select
130
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
131
	
132
	psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
133
}
134

    
135
pg_header()
136
{
137
	echo_func
138
	local pg_copy_format="CSV HEADER" limit=0
139
	pg_export "$@"|echo_stdout
140
}
141

    
142
pg_export_table_no_header()
143
{
144
	echo_func
145
	local pg_copy_format="CSV"
146
	pg_export "$@"
147
}
148

    
149
pg_export_table_to_dir_no_header()
150
{
151
	echo_func
152
	local table="$1"; shift; mk_table_esc
153
	local cols="$(pg_header)"
154
	stdout="$exports_dir/$table.no_header.cols=$cols.csv" to_file \
155
pg_export_table_no_header "$@"
156
}
157

    
158
fi
(2-2/5)