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