Project

General

Profile

1 8291 aaronmk
#!/bin/bash -e
2 8273 aaronmk
3
if false; then #### run script template:
4 8291 aaronmk
#!/bin/bash -e
5 8273 aaronmk
. "$(dirname "${BASH_SOURCE[0]}")"/path/to/table.run
6 8161 aaronmk
7 8273 aaronmk
import ()
8
{
9 8463 aaronmk
	echo_func "$@"
10 8273 aaronmk
	before_import_cmds
11 8301 aaronmk
	"$(dirname "${BASH_SOURCE[0]}")"/path/to/table.run "$FUNCNAME" "$@"
12 8273 aaronmk
	after_import_cmds
13
}
14
fi ####
15 8213 aaronmk
16 8296 aaronmk
. "$(dirname "${BASH_SOURCE[0]}")"/import.run
17 8701 aaronmk
. "$(dirname "${BASH_SOURCE[0]}")"/local.run
18 8273 aaronmk
19 8695 aaronmk
quote='"'
20
21
esc_name () { echo "$quote${1//$quote/$quote$quote}$quote"; }
22
23
mk_esc_name () { set_var "$1"_esc "$(esc_name "${!1}")"; }
24
25 8273 aaronmk
: "${schema="$(cd "$top_dir"/..; basename "$PWD")"}"
26 8696 aaronmk
mk_esc_name schema
27 8273 aaronmk
: "${table="$( cd "$top_dir"   ; basename "$PWD")"}"
28 8696 aaronmk
mk_esc_name table
29 8195 aaronmk
export schema table
30 8161 aaronmk
31 8698 aaronmk
mysql ()
32
{
33
	echo_func "$@"
34
	echo_run "$bin_dir"/mysql_bien --database="$schema" --verbose "$@"
35
}
36
37
mysql_ANSI ()
38
{
39
	echo_func "$@"
40
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
41
}
42
43 8273 aaronmk
psql () # usage: ([file=...] [dir=...]; self)
44 8161 aaronmk
{
45 8463 aaronmk
	echo_func "$@"
46 8273 aaronmk
	local dir="$dir"
47
	if test -n "$file"; then
48
		set -- --file "$file" "$@"
49
		: "${dir:=$(dirname "$file")}"
50
	fi
51
	: "${dir:=$top_dir}"
52
53 8196 aaronmk
	(cat <<EOF
54 8273 aaronmk
\cd $dir
55 8196 aaronmk
\set schema "$schema"
56
\set table "$table"
57
\set table_str '''"$table"'''
58
SET search_path TO "$schema", util;
59
EOF
60
	cat)|
61 8277 aaronmk
	echo_run env no_search_path=1 "$bin_dir"/psql_verbose_vegbien "$@"
62 8161 aaronmk
}
63
64 8273 aaronmk
input_make ()
65 8242 aaronmk
{
66 8463 aaronmk
	echo_func "$@"
67 8277 aaronmk
	echo_run env make --directory="$top_dir"/.. "${@/#/$table/}"
68 8242 aaronmk
}
69
70 8161 aaronmk
map_table ()
71
{
72 8463 aaronmk
	echo_func "$@"
73 8273 aaronmk
	input_make map.csv
74 8214 aaronmk
	psql <<EOF
75 8191 aaronmk
SELECT util.reset_map_table('pg_temp.map');
76 8161 aaronmk
\copy pg_temp.map FROM 'map.csv' CSV HEADER;
77 8191 aaronmk
SELECT util.set_col_names('"$table"', 'pg_temp.map'::regclass);
78 8161 aaronmk
EOF
79
}
80 8198 aaronmk
81 8282 aaronmk
mk_derived ()
82
{
83 8463 aaronmk
	echo_func "$@"
84 8282 aaronmk
	"$root_dir"/schemas/VegCore/mk_derived
85
}
86 8245 aaronmk
87
remake_VegBIEN_mappings ()
88
{
89 8463 aaronmk
	echo_func "$@"
90 8277 aaronmk
	echo_run rm header.csv map.csv # remake them
91 8273 aaronmk
	yes|input_make test
92 8245 aaronmk
}
93 8273 aaronmk
94
postprocess () # overridable
95
{
96 8463 aaronmk
	echo_func "$@"
97 8273 aaronmk
	local file="$top_dir"/postprocess.sql
98
	if test -e "$file"; then psql "$@"; fi
99
}
100
101
import ()
102
{
103 8463 aaronmk
	echo_func "$@"
104 8282 aaronmk
	map_table
105
	postprocess
106
	mk_derived
107 8273 aaronmk
}