This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_csv_verwaltungsgebiete.sh
executable file
·84 lines (74 loc) · 1.83 KB
/
export_csv_verwaltungsgebiete.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env bash
if [ -z "${1}" ]; then
echo "At least one ARS argument is mandatory."
exit 1
fi
function build_query {
local type=${1}
local parent_ars=${2}
local ars=${3}
local query=$(cat << EOF
SELECT
ars,
'${parent_ars}' as parent_ars,
'${type}' as type,
gen,
bez
FROM
vg250_${type}
WHERE
ars LIKE '${ars}%'
EOF
)
echo ${query}
}
function build_static_query {
local query=$(cat << EOF
SELECT
ars,
null as parent_ars,
'sta' as type,
gen,
bez
FROM
vg250_sta
WHERE
ars = '${1}'
AND gf = '4'
EOF
)
echo ${query}
}
function export_to_csv {
local ars=${1}
local query=${2}
echo "Starting export for ${ars}"
psql\
-h localhost\
-d postgis\
-U docker\
-c "Copy ($query) To '/app/data/${ars}.csv' With CSV DELIMITER ',' HEADER;"
echo "Exported to data/${ars}.csv"
}
function ensure_data_dir {
mkdir -p /app/data
}
echo "localhost:5432:postgis:docker:docker" > ~/.pgpass
chmod 600 ~/.pgpass
ensure_data_dir
state_ars="000000000000"
export_to_csv "0" "$(build_static_query "${state_ars}")"
for ars in "$@"
do
export_to_csv ${state_ars} "$(build_query "lan" ${state_ars} ${ars:0:2})"
export_to_csv ${ars:0:2} "$(build_query "rbz" ${ars:0:2} ${ars:0:3})"
export_to_csv ${ars:0:3} "$(build_query "krs" ${ars:0:3} ${ars:0:3})"
export_to_csv ${ars} "$(build_query "gem" ${ars} ${ars})"
done
cat << EOF
*******************************************************************************
* *
* Successfully exported the data. *
* *
*******************************************************************************
EOF