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
: "${table="$( cd "$top_dir"   ; basename "$PWD")"}"
29
export schema table
30

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

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

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

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

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

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

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