Project

General

Profile

1
#!/bin/bash -e
2

    
3
if false; then #### run script template:
4
#!/bin/bash -e
5
. "$(dirname "${BASH_SOURCE[0]}")"/path/to/table.run
6

    
7
import ()
8
{
9
	echo_func "$@"
10
	before_import_cmds
11
	"$(dirname "${BASH_SOURCE[0]}")"/path/to/table.run "$FUNCNAME" "$@"
12
	after_import_cmds
13
}
14
fi ####
15

    
16
. "$(dirname "${BASH_SOURCE[0]}")"/import.run
17

    
18
quote='"'
19

    
20
esc_name () { echo "$quote${1//$quote/$quote$quote}$quote"; }
21

    
22
mk_esc_name () { set_var "$1"_esc "$(esc_name "${!1}")"; }
23

    
24
root_dir="$(dirname "${BASH_SOURCE[0]}")"/..
25
bin_dir="$root_dir"/bin
26

    
27
: "${schema="$(cd "$top_dir"/..; basename "$PWD")"}"
28
mk_esc_name schema
29
: "${table="$( cd "$top_dir"   ; basename "$PWD")"}"
30
mk_esc_name table
31
export schema table
32

    
33
psql () # usage: ([file=...] [dir=...]; self)
34
{
35
	echo_func "$@"
36
	local dir="$dir"
37
	if test -n "$file"; then
38
		set -- --file "$file" "$@"
39
		: "${dir:=$(dirname "$file")}"
40
	fi
41
	: "${dir:=$top_dir}"
42
	
43
	(cat <<EOF
44
\cd $dir
45
\set schema "$schema"
46
\set table "$table"
47
\set table_str '''"$table"'''
48
SET search_path TO "$schema", util;
49
EOF
50
	cat)|
51
	echo_run env no_search_path=1 "$bin_dir"/psql_verbose_vegbien "$@"
52
}
53

    
54
input_make ()
55
{
56
	echo_func "$@"
57
	echo_run env make --directory="$top_dir"/.. "${@/#/$table/}"
58
}
59

    
60
map_table ()
61
{
62
	echo_func "$@"
63
	input_make map.csv
64
	psql <<EOF
65
SELECT util.reset_map_table('pg_temp.map');
66
\copy pg_temp.map FROM 'map.csv' CSV HEADER;
67
SELECT util.set_col_names('"$table"', 'pg_temp.map'::regclass);
68
EOF
69
}
70

    
71
mk_derived ()
72
{
73
	echo_func "$@"
74
	"$root_dir"/schemas/VegCore/mk_derived
75
}
76

    
77
remake_VegBIEN_mappings ()
78
{
79
	echo_func "$@"
80
	echo_run rm header.csv map.csv # remake them
81
	yes|input_make test
82
}
83

    
84
postprocess () # overridable
85
{
86
	echo_func "$@"
87
	local file="$top_dir"/postprocess.sql
88
	if test -e "$file"; then psql "$@"; fi
89
}
90

    
91
import ()
92
{
93
	echo_func "$@"
94
	map_table
95
	postprocess
96
	mk_derived
97
}
(40-40/51)