-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_parser.sh
40 lines (37 loc) · 902 Bytes
/
query_parser.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
#!/bin/bash
function parse_query() {
local query="$1"
local collection
collection=$(echo "$query" | awk 'BEGIN{FS=" "} { for(i=1;i<=NF;i++){ if($i=="FROM") print $(i+1) } }')
local conditions
conditions=$(
echo "$query" \
| sed -n -e '/WHERE/ {
s/.*WHERE //;
s/[[:space:]]ORDER BY.*//;
s/[[:space:]]LIMIT.*//;
p;
}'
)
local order_by
order_by=$(
echo "$query" \
| sed -n -e '/ORDER BY/ {
s/.*ORDER BY *//;
s/[[:space:]]LIMIT.*//;
p;
}'
)
local limit_value
limit_value=$(
echo "$query" \
| sed -n -e '/LIMIT/ {
s/.*LIMIT *//;
p;
}'
)
echo "collection=$collection"
echo "conditions=$conditions"
echo "order_by=$order_by"
echo "limit_value=$limit_value"
}