-
Notifications
You must be signed in to change notification settings - Fork 0
/
Insert.sh
163 lines (134 loc) · 6.68 KB
/
Insert.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/bash
#description: insert row into table
#ask table name to insert row into this table --->okay
#validation is table exist or no --->okay
#approach
#go to file.Mdata --->to count numbers columns and get first column and second column--->okay
#ask user enter $f2 into $f1 Column---> okay
#echo user enter int into id Column--> okay
#validation value is verify datatype or no ---->okay
#validation if first column is pk check is unique and not null---->okay
#Function check datatypes
function checkInt()
{
local re='^([0-9]+)*$'
(( ${#1} > 16 )) && return 1
[[ "$1" =~ $re ]]
}
function checkString()
{
local re1='^([A-Za-z]+)*$'
(( ${#1} > 16 )) && return 1
[[ "$1" =~ $re1 ]]
}
testValidInt() {
if checkInt "$1"; then
return 0
else
return 1
fi
}
testValidString() {
if checkString "$1"; then
return 0
else
return 1
fi
}
echo "Enter table name to insert row into this table: "
read InTable
if [ ! ${#InTable} -eq 0 ]
then
if [ -f "./$InTable" -a -f "./$InTable.Mdata" ]
then
NumRows=($(awk 'BEGIN{FS=":";} END{print NR}' ./$InTable.Mdata))
ColsName=($(awk 'BEGIN{FS=":";} {print $1}' ./$InTable.Mdata))
ColsDataTypes=($(awk 'BEGIN{FS=":";} {print $2}' ./$InTable.Mdata))
output=""
((NumRows--))
for i in $(seq 0 $NumRows);
do
echo "please,Enter" ${ColsName[i]}":"
if [ "${ColsDataTypes[i]}" == "int" ]
then
while [ true ]
do
read inputInt
testValidInt "$inputInt"
if [ $? -eq 0 -a ! ${#inputInt} -eq 0 ]
then
#unique id
if (( $i == 0 )) ;then
while (( `cut -d":" -f1 "./$InTable" | grep -x $inputInt |wc -w` > 0 ))
do
while [ true ]
do
read -p "${ColsName[i]} should be unique, please enter another value: " inputInt
testValidInt "$inputInt"
if [ $? -eq 0 -a ! ${#inputInt} -eq 0 ]
then
break
else
echo "you must enter integer datatype in this "${ColsName[i]}
fi
done
done
fi
output+=$inputInt
if [ $i -ne $NumRows ]
then
output+=:
fi
break
else
echo "you must Input enter integer datatype in this "${ColsName[i]}
echo "Please, Enter another once "${ColsName[i]}
fi
done
else
while [ true ]
do
read inputStr
testValidString "$inputStr"
if [ $? -eq 0 -a ! ${#inputStr} -eq 0 ]
then
#unique id
if (( $i == 0 )) ;then
while (( `cut -d":" -f1 "./$InTable" | grep -x $inputStr |wc -w` > 0 ))
do
while [ true ]
do
read -p "${ColsName[i]} should be unique, please enter another value: " inputStr
testValidString "$inputStr"
if [ $? -eq 0 -a ! ${#inputStr} -eq 0 ]
then
break
else
echo "you must enter String datatype in this "${ColsName[i]}
fi
done
done
fi
output+=$inputStr
if [ $i -ne $NumRows ]
then
output+=:
fi
break
else
echo "you must enter String datatype in this "${ColsName[i]}
echo "Please, Enter another once "${ColsName[i]}
fi
done
fi
done
echo $output >> ./$InTable
echo "----------------------"
echo "Insert Sucessfully :)"
echo "----------------------"
else
echo "You must enter valid table name to insert"
fi
else
echo "You must input , Can't input empty"
fi