-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_table.sh
More file actions
executable file
·266 lines (223 loc) · 5.2 KB
/
create_table.sh
File metadata and controls
executable file
·266 lines (223 loc) · 5.2 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/bash
# $1 is the name of the database you are currently using
clear
echo
list_tables $1
echo
echo -e "${GREEN}Creating table in ${LBLUE}$1${GREEN} database${NC}"
echo
while [[ true ]]; do
echo -ne "${PROMPT}Enter The Name Of The Table : ${NC}"
#back
if ! read table_name; then
return
fi
#disallowing special characters in names -- \ must be handled
if [[ $table_name == *['!'@#\$%^\&*()-+\.\/]* ]]; then
echo
echo -e "${RED} ! @ # $ % ^ () + . - are not allowed!${NC}"
continue
fi
#check for empty string
if ! is_not_null $table_name; then
echo
echo -e "${RED}Name Cannot Be Null!${NC}"
continue
fi
#rejecting spaces in table name
if [[ "$table_name" = *" "* ]]; then
echo
echo -e "${RED}Spaces Are Not Allowed!${NC}"
continue
fi
#check for existing table name
if find_table $1 $table_name; then
echo
echo -e "${RED}Table Does Exist!${NC}"
continue
fi
break
done
while [[ true ]]; do
echo -ne "${PROMPT}Enter Number Of Columns : ${NC}"
#back
if ! read num_of_cols; then
return
fi
#check for empty string
if ! is_not_null $table_name; then
echo
echo -e "${RED}Number Cannot Be Null!${NC}"
continue
fi
#check if zero entered
if [ "$num_of_cols" = 0 ]; then
echo
echo -e "${RED}Number Cannot Be Zero!${NC}"
continue
fi
if [[ "$num_of_cols" =~ ^[-][0-9]+$ ]]; then
echo
echo -e "${RED}Number Cannot Be Negative!${NC}"
continue
fi
if [[ "$num_of_cols" =~ ^[-+]?[0-9]+\.[0-9]*$ ]]; then
echo
echo -e "${RED}Number Cannot Be Float!${NC}"
continue
fi
#check if input is not a number
re='^[0-9]+$'
if ! [[ "$num_of_cols" =~ $re ]]; then
echo
echo -e "${RED}Only Numbers Are Needed!${NC}"
continue
fi
break
done
#two arrays for columns names and columns datatypes
cols_names=()
cols_datatypes=()
while [[ true ]]; do
#take the primary key column name
echo -ne "${PROMPT}Please Enter Primary Key Column Name : ${NC}"
#back
if ! read pk_name; then
return
fi
if [[ $pk_name == *['!'@#\$%^\&*()-+\.\/]* ]]; then
echo
echo -e "${RED} ! @ # $ % ^ () + . - are not allowed!${NC}"
continue
fi
#checking for empty string
if ! is_not_null $pk_name ; then
echo
echo -e "${RED}Name Cannot Be Null!${NC}"
continue
fi
#checking for repeated column name
if ! repeated_col_name $pk_name $cols_names ; then
echo
echo -e "${RED}Repeated column name!${NC}"
continue
fi
#rejecting spaces in column name
if [[ "$pk_name" = *" "* ]]; then
echo
echo -e "${RED}Spaces Are Not Allowed!${NC}"
continue
fi
#rejecting colons in column name
if [[ "$pk_name" = *:* ]]; then
echo
echo -e "${RED}Colons Are Not Allowed!${NC}"
continue
fi
break
done
# appending ti the array
cols_names+=("$pk_name")
echo "Please note, supported datatypes are strings <str>, alphanumeric <alnum>, and integers <int>"
echo -ne "${PROMPT}Enter The Primary Key datatype : ${NC}"
if ! read pk_type; then
return
fi
#taking the datatype of the pk column
while [[ true ]]; do
case $pk_type in
str )
break;;
int )
break;;
alnum )
break;;
* )
echo -ne "${RED}Please Enter A Supported Datatype : ${NC}"
if ! read pk_type; then
return
fi;;
esac
done
#appending to the array
cols_datatypes+=("$pk_type")
# taking the names and datatypes of the rest of the columns
# excluding the pk column that was already entered
for (( i = 1; i < $num_of_cols; i++ )); do
echo
while [[ true ]]; do
#take the primary key column name
echo -ne "${PROMPT}Please Enter Column $i Name : ${NC}"
if ! read name; then
return
fi
if [[ $name == *['!'@#\$%^\&*()-+\.\/]* ]]; then
echo
echo -e "${RED} ! @ # $ % ^ () + . - are not allowed!${NC}"
continue
fi
#checking for empty string
if ! is_not_null $name ; then
echo
echo -e "${RED}Name Cannot Be Null!${NC}"
continue
fi
#checking for repeated column name
if ! repeated_col_name $name $cols_names ; then
echo
echo -e "${RED}Repeated column name!${NC}"
continue
fi
#rejecting spaces in column name
if [[ "$name" = *" "* ]]; then
echo
echo -e "${RED}Spaces Are Not Allowed!${NC}"
continue
fi
#rejecting colons in column name
if [[ "$name" = *:* ]]; then
echo
echo -e "${RED}Colons Are Not Allowed!${NC}"
continue
fi
break
done
# appending ti the array
cols_names+=("$name")
#Datatypes of the columns
echo "Please note, supported datatypes are strings <str>, and integers <int>"
echo -ne "${PROMPT}Enter Column $i datatype : ${NC}"
if ! read dtype; then
return
fi
#taking the datatype of the pk column
while [[ true ]]; do
case $dtype in
str )
break;;
int )
break;;
alnum )
break;;
* )
echo -ne "${RED}Please Enter A Supported Datatype : ${NC}"
if ! read dtype; then
return
fi;;
esac
done
#appending to the array
cols_datatypes+=("$dtype")
done
#creating file representing the table
touch "./Databases/$1/$table_name"
#metadata of the table
echo "pk:${cols_names[0]}" > "./Databases/$1/${table_name}_template"
for (( i = 0; i < $num_of_cols; i++ )); do
echo "${cols_names[$i]}:${cols_datatypes[$i]}" >> "./Databases/$1/${table_name}_template"
done
echo
echo -e "${GREEN}Table $table_name created Successfully${NC}"
echo "Press Enter To continue!"
read
return