-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_into_table.sh
executable file
·191 lines (161 loc) · 8.59 KB
/
insert_into_table.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#! /bin/bash
#this file is to insert the data into the table regarding the data type and the table name and making sure that the data will be entered correctlly
list_table.sh
echo "===============Select Table==============="
echo
echo "Enter Table You Want To Select"
read -r tablename
#this condition is used to cheack if the table exist
if [ -f $tablename ]
then
table_meta_data=".${tablename}.meta"
echo "==========$tablename selected=========="
#the input data array or the row data
declare -a input
#the element index that the code standing at
input_index=0
#the worning of the primary key repetance
flag=0
#reading number of columns
num_col=$(head -1 $table_meta_data)
#reading primary key
pkey_index=$(head -2 $table_meta_data | tail -1)
#reading columns names
column_name=$(head -4 $table_meta_data | tail -1) #cloumn names
#reading data types
column_datatype=$(head -3 $table_meta_data | tail -1) #data types
#adding the colmunns name to array
column_name_array=($(echo "$column_name" | sed 's/:/ /g'))
#adding data types to array
column_datatype_array=($(echo "$column_datatype" | sed 's/:/ /g'))
#--------------------------------------------------------------------------------------------------------------------------------------
#looping in every element in the column name array to add records
for element in "${column_name_array[@]}";
do
#reading the record data
read -p "Enter $element of data type ${column_datatype_array[$input_index]} :" user_input
#--------------------------------------------------------------------------------------------------------------------------------------
#reading the datatype of the current column to modify the input correctly
some_data=${column_datatype_array[$input_index]}
#--------------------------------------------------------------------------------------------------------------------------------------
#validate the input is not corupted
while [[ $user_input == *['!''@#/$\\"*{^})(+|,;:~`._%&/=-]>[<?']* && $some_data != "date" || $user_input == "" ]]
do
echo "^
/_!_\ Invalid Input"
echo
read -p "PLease Enter $element Again: " user_input
done
#--------------------------------------------------------------------------------------------------------------------------------------
#if the input is integer
if [[ $some_data == *"int"* ]]
then
#this loop is to make sure that it wont get out with out the correct value
while true
do
if [[ $user_input =~ [0-9] ]]
then
#this condition is used to cheack the if the primary key is repeated or not (unique)
if [ $input_index -eq $pkey_index ] #check primary key uniqueness
then
#this var is used because the array index start with 0 and the awk index start with 1
#adding because there is no pk =0 and the index in the array start by 0
let y=$pkey_index+1
#this loop is to make sure that the primary key will be entered correctly
while true
do
if [ $user_input -eq "0" ] #cheaking the zero case
then
echo "
^
/_!_\ zero is not a valid record for primary key"
read -p "
^
/_!_\ you entered unvalid primary key! please enter valid value : " user_input
echo ""
continue
fi
#if there is any dublication this will return 1 then will be used in the if after it
flag=$(awk -F: '{if ($'$y'=="'$user_input'")print "1"} ' $tablename)
if [ $((flag)) -eq 0 ] #pkey is unique
then
break
else #pkey is not unique
let flag=0
read -p "
^
/_!_\ you entered a duplicate primary key! please enter a unique value :" user_input
fi
done
#saving the data to the array to write it in the file
input[$input_index]="$user_input:"
let input_index=$input_index+1
break
else
#saving the data to the array to write it in the file
input[$input_index]="$user_input:"
let input_index=$input_index+1
break
fi
else
echo "
^
/_!_\ somthing wrong excpected number"
read -p "enter value again: " user_input
fi #end of the regax if
done
#--------------------------------------------------------------------------------------------------------------------------------------
#if the input is string
elif [[ $some_data == *"str"* ]]
then
#this loop is to make sure that it wont get out with out the correct value
while true
do
if [[ $user_input =~ [a-zA-Z] ]]
then
input[$input_index]="$user_input:"
let input_index=$input_index+1
break
else
echo "
^
/_!_\ somthing wrong excpected string"
read -p "enter value again: " user_input
fi #end of the regax if
done
#--------------------------------------------------------------------------------------------------------------------------------------
elif [[ $some_data == *"date"* ]]
then
#this loop is to make sure that it wont get out with out the correct value
while true
do
if [[ $user_input =~ [0-9]{2}\-[0-1]{1}[0-9]{1}\-[0-9]{4} ]]
then
input[$input_index]="$user_input:"
let input_index=$input_index+1
break
else
echo "
^
/_!_\ somthing wrong excpected dd-mm-yyyy"
read -p "enter a valid date: " user_input
fi #end of the regax if
done
#--------------------------------------------------------------------------------------------------------------------------------------
else
echo "
^
/_!_\ somthing wrong datatype cheack the table meta data"
fi #end of data type if
#--------------------------------------------------------------------------------------------------------------------------------------
done
printf %s "${input[@]}" $'\n' >> $tablename
echo "data saved sucsessfully"
#--------------------------------------------------------------------------------------------------------------------------------------
else
echo
echo "
^
/_!_\ $tablename table does not exist"
echo
fi