-
Notifications
You must be signed in to change notification settings - Fork 11
/
dev.sh
executable file
·41 lines (35 loc) · 1.21 KB
/
dev.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
#!/bin/bash
QUERY=$1
PARAMS=$(cat src/queries/$QUERY.sql \
| grep -e "--- [a-z]" \
| sed 's/---//g' \
| sed 's/^/ --parameter /' \
| sed -E 's/: ([0-9]+)$/:INT64:\1/' \
| sed -E 's/: ([0-9.]+)$/:FLOAT:\1/' \
| sed -E 's/: (.*)/::"\1"/' \
| tr -d '\n')
# if second argument starts with -- use that as the format (without the --)
# otherwise use the default format
FORMAT="--format=sparse"
if [[ $2 == --* ]]; then
# replace the -- with --format= in $2
FORMAT="--format=${2:2}"
shift
fi
# override --parameter domainkey::"..." with contents of DOMAINKEY env var
if [ -n "$DOMAINKEY" ]; then
PARAMS=$(echo $PARAMS | sed -E "s/domainkey::\".*\"/domainkey::\"$DOMAINKEY\"/")
fi
# drop the first arg (query name)
shift
# override all other parameters with contents of command line args in the format parameter::value
while [ $# -gt 0 ]; do
# escape forward slashes
ESCAPED=$(echo $2 | sed 's/\//\\\//g')
PARAMS=$(echo $PARAMS | sed -E "s/$1:INT64:[^ ]*/$1:INT64:$2/")
PARAMS=$(echo $PARAMS | sed -E "s/$1:FLOAT:[^ ]*/$1:FLOAT:$2/")
PARAMS=$(echo $PARAMS | sed -E "s/$1::\"[^\"]*\"/$1::\"$ESCAPED\"/")
shift
shift
done
echo "cat src/queries/$QUERY.sql | bq query $PARAMS --use_legacy_sql=false $FORMAT" | sh