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
mysql_cmd() # usage: mysql*() { ...; mysql_cmd "$@"; }
39
{
40
	echo_func
41
	local ssh_server="$(localize_url "$ssh_server")"
42
	local server="$(localize_url "$server")"
43
	if test "$ssh_server"; then
44
		local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
45
	fi
46
	set_database
47
	
48
	local var=ssh_dest; local_inv
49
	command ${ssh_dest:+ssh "$ssh_dest" }"${FUNCNAME[1]}" \
50
${server:+ --host="$server" }${user:+--user="$user" } --password\
51
${password+="$password"} ${database:+--databases "$database" --tables } "$@"
52
}
53

    
54
mysql() { echo_func; mysql_cmd --verbose "$@"; }
55

    
56
mysql_ANSI()
57
{
58
	echo_func
59
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
60
}
61

    
62
mysqldump() # usage: [schema=1 | data=1] mysqldump db [table...]
63
{
64
	echo_func
65
	mysql_cmd --quick --lock-tables=false --set-charset \
66
${postgres_compat:+--compatible=postgresql --add-locks=false }\
67
${schema:+--no-data }${data:+--no-create-info }"$@"
68
}
69

    
70
mysqldump_diffable()
71
{
72
	echo_func
73
	mysqldump "$@"|{ pipe_delay; sed 's/^(-- Dump completed).*$/\1/'; }
74
}
75

    
76

    
77
### PostgreSQL
78

    
79
pg_export()
80
{
81
	echo_func
82
	mk_select
83
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
84
	
85
	psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
86
}
87

    
88
pg_header()
89
{
90
	echo_func
91
	local pg_copy_format="CSV HEADER" limit=0
92
	pg_export "$@"|echo_stdout
93
}
94

    
95
pg_export_table_no_header()
96
{
97
	echo_func
98
	local pg_copy_format="CSV"
99
	pg_export "$@"
100
}
101

    
102
pg_export_table_to_dir_no_header()
103
{
104
	echo_func
105
	local table="$1"; shift; mk_table_esc
106
	local cols="$(pg_header)"
107
	stdout="$exports_dir/$table.no_header.cols=$cols.csv" to_file \
108
pg_export_table_no_header "$@"
109
}
110

    
111
fi
(2-2/5)