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
if self_not_included; then
20

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

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

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

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

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

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

    
103
import ()
104
{
105
	echo_func "$@"
106
	map_table
107
	postprocess
108
	mk_derived
109
}
110

    
111
fi
(42-42/54)