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
. "$(dirname "${BASH_SOURCE[0]}")"/local.run
18

    
19
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
: "${schema="$(cd "$top_dir"/..; basename "$PWD")"}"
26
mk_esc_name schema
27
: "${table="$( cd "$top_dir"   ; basename "$PWD")"}"
28
mk_esc_name table
29
export schema table
30

    
31
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
psql () # usage: ([file=...] [dir=...]; self)
44
{
45
	echo_func "$@"
46
	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
	(cat <<EOF
54
\cd $dir
55
\set schema "$schema"
56
\set table "$table"
57
\set table_str '''"$table"'''
58
SET search_path TO "$schema", util;
59
EOF
60
	cat)|
61
	echo_run env no_search_path=1 "$bin_dir"/psql_verbose_vegbien "$@"
62
}
63

    
64
input_make ()
65
{
66
	echo_func "$@"
67
	echo_run env make --directory="$top_dir"/.. "${@/#/$table/}"
68
}
69

    
70
map_table ()
71
{
72
	echo_func "$@"
73
	input_make map.csv
74
	psql <<EOF
75
SELECT util.reset_map_table('pg_temp.map');
76
\copy pg_temp.map FROM 'map.csv' CSV HEADER;
77
SELECT util.set_col_names('"$table"', 'pg_temp.map'::regclass);
78
EOF
79
}
80

    
81
mk_derived ()
82
{
83
	echo_func "$@"
84
	"$root_dir"/schemas/VegCore/mk_derived
85
}
86

    
87
remake_VegBIEN_mappings ()
88
{
89
	echo_func "$@"
90
	echo_run rm header.csv map.csv # remake them
91
	yes|input_make test
92
}
93

    
94
postprocess () # overridable
95
{
96
	echo_func "$@"
97
	local file="$top_dir"/postprocess.sql
98
	if test -e "$file"; then psql "$@"; fi
99
}
100

    
101
import ()
102
{
103
	echo_func "$@"
104
	map_table
105
	postprocess
106
	mk_derived
107
}
(41-41/52)