-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertTable.sh
93 lines (60 loc) · 1.5 KB
/
InsertTable.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
85
86
87
#!/bin/bash
source ./validationfuncs.sh
function InsertTable {
typeset tableName colsNumber counter=0 insertVal=""
if [ -z "$(ls)" ]
then
echo "empty database, no tables to insert into."
return
fi
while true
do
read -p "Enter table name: " tableName
ValInputName $tableName
if [ $? -eq 0 ]
then
break
fi
done
if [ ! -d "$tableName" ]
then
echo "Table doesn't exist"
return
fi
colsNumber=$(head -1 ${tableName}/${tableName}-meta.txt | awk -F ':' '{print NF}')
while [ $counter -lt $colsNumber ]
do
typeset colName=$(tail -1 ${tableName}/${tableName}-meta.txt | cut -d ":" -f $((counter+1)))
typeset colDataType=$(head -1 ${tableName}/${tableName}-meta.txt | cut -d ":" -f $((counter+1)))
while true
do
read -p "enter value of ${colName} in ${colDataType}: " colValue
ValDataType "$colValue" "$colDataType"
if [ $? -eq 1 ]
then
continue
fi
pkCompare=$(awk -F: '{print $1}' "${tableName}/${tableName}.txt" | grep -w ${colValue})
if [ $counter -eq 0 ]
then
if [ "$colValue" == "$pkCompare" ]
then
echo "primary key value exists, choose another one."
continue
fi
fi
if [ $? -eq 0 ]
then
break
fi
done
if [ $counter -eq $((colsNumber-1)) ]
then
insertVal="${insertVal}${colValue}"
else
insertVal="${insertVal}${colValue}:"
fi
let counter=$counter+1
done
echo ${insertVal} >> "${tableName}/${tableName}.txt"
}