-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.sh
538 lines (443 loc) · 11.4 KB
/
script.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#!/bin/bash
#----------------------- Start Utils Fuctions---------------------------------------
function validateParamName {
if [ -z "$1" ]
then
echo "The name field cannot be left empty"
return 1
elif [[ "$1" =~ ^[0-9] ]]
then
echo "Name should not begin with a number"
return 1
elif [[ "$1" = *" "* ]]
then
echo "Name Shouldn't Have Spaces"
return 1
elif [[ "$1" =~ [^a-zA-Z0-9_] ]]
then
echo "Name Shouldn't Have Special Characters"
return 1
fi
}
#$1 -> Value
#$2 -> datatype
function validateDataType {
if [ -z "$1" ]
then
echo " value can't be empty."
return 1
fi
if [[ "$1" =~ ^[0-9]+$ ]]
then
if [ "$2" == "integer" ]
then
return 0
else
echo "The value should be a String."
return 1
fi
fi
if [[ "$1" =~ ^[a-zA-Z0-9_]+$ ]];
then
if [ "$2" == "string" ]
then
return 0
else
echo "The value should be an Integer."
return 1
fi
fi
}
#----------------------- End Utils Fuctions-----------------------------------------
#----------------------- Start Fuctions Area-----------------------------------------
function createDb {
typeset status DbName
while true
do
read -p "Enter the Database name: " DbName
validateParamName $DbName
if [ $? -eq 0 ]
then
break
fi
done
if [ -d "databases/$DbName" ]
then
echo "A Database with same name already exist"
else
mkdir databases/$DbName
echo "Database Created Successfully"
fi
}
function listDbs {
if [ -n "$(ls databases/)" ]
then
echo "Databases List : "
ls databases/
else
echo "No Databases Found"
fi
}
function connectDb {
typeset DbName
if [ -z "$(ls databases/ )" ]
then
echo "No Databases Found To connect"
return
fi
while true
do
read -p "Enter Database name: " DbName
validateParamName $DbName
if [ $? -eq 0 ]
then
break
fi
done
if [ ! -d "databases/$DbName" ]
then
echo "Database Not Found"
else
cd databases/$DbName
echo "You are connected to $DbName database"
showTablesMenu
fi
}
function DropDb {
typeset DbName
if [ -z "$(ls databases/ )" ]
then
echo "No Databases Found To Remove"
return
fi
while true
do
read -p "Enter Database Name: " DbName
validateParamName $DbName
if [ $? -eq 0 ]
then
break
fi
done
if [ ! -d "databases/$DbName" ]
then
echo "Database Not Found"
else
rm -r databases/$DbName
echo "$DbName deleted successfully"
fi
}
function createTable {
typeset tableName cols num=0 nameRecord="" dataTypeRecord=""
while true
do
read -p "Enter Table Name: " tableName
validateParamName $tableName
if [ $? -eq 0 ]
then
break
fi
done
if [ -d "$tableName" ]
then
echo "Table Already Exists"
return
fi
mkdir $tableName
cd $tableName
touch "${tableName}.txt"
touch "${tableName}-meta.txt"
while true
do
read -p "Enter Number Of Columns: " cols
if [[ ! $cols =~ ^[0-9]+$ ]]
then
echo "Cols number must be a number"
exit
elif [ $cols -eq 0 ]
then
echo "Cols number should be greater than 0"
exit
fi
break
done
typeset colName colType
while [ $num -lt $cols ]
do
if [ $num -eq 0 ]
then
read -p "Enter The PK Column: " colName
else
read -p "Enter Column Name: " colName
fi
echo "Choose an option: "
select colType in "string" "integer"
do
case $colType in
"integer" | "string" ) break ;;
*) echo "Invalid Choice" ;;
esac
done
if [ $num -eq $((cols-1)) ]
then
nameRecord="${nameRecord}${colName}"
dataTypeRecord="${dataTypeRecord}${colType}"
else
nameRecord="${nameRecord}${colName}:"
dataTypeRecord="${dataTypeRecord}${colType}:"
fi
let num=$num+1
done
echo $dataTypeRecord >> "${tableName}-meta.txt"
echo $nameRecord >> "${tableName}-meta.txt"
cd ../
}
function listTables {
if [ -z "$(ls)" ]
then
echo "No Tables To Show, Database Is Empty."
else
ls
fi
}
function dropTable {
typeset tableName
if [ -z "$(ls)" ]
then
echo "No Tables To Drop, Database Is Empty."
else
while true
do
read -p "Enter Table Name: " tableName
validateParamName $tableName
if [ $? -eq 0 ]
then
break
fi
done
if [ -d "$tableName" ]
then
rm -r "$tableName"
echo "Table ${tableName} deleted successfully"
else
echo "Table ${tableName} Doesn't Exist"
return
fi
fi
}
function insertTable {
if [ -z "$(ls)" ]
then
echo "No Tables To Insert, Database Is Empty."
return
fi
typeset tableName
while true
do
read -p "Enter Table Name: " tableName
validateParamName $tableName
if [ $? -eq 0 ]
then
break
fi
done
if [ ! -d "$tableName" ]
then
echo "Table Doesn't Exist"
return
fi
typeset colNum
colNum=$( head -1 ${tableName}/${tableName}-meta.txt | awk -F':' '{print NF}')
typeset num=0
typeset insertVal=""
while [ $num -lt $colNum ]
do
typeset colName=$(tail -1 ${tableName}/${tableName}-meta.txt | cut -d ':' -f $((num+1)))
typeset colDatatype=$(head -1 ${tableName}/${tableName}-meta.txt | cut -d ':' -f $((num+1)))
while true
do
read -p "Enter value of ${colName} in ${colDatatype}: " colValue
validateDataType $colValue $colDatatype
if [ $num -eq 0 ]
then
if [ ! -z "$(grep ^${colValue} ${tableName}/${tableName}.txt)" ]
then
echo "This Pk Exist, choose another one"
continue
fi
fi
if [ $? -eq 0 ]
then
break
fi
done
if [ $num -eq $((colNum-1)) ]
then
insertVal="${insertVal}${colValue}"
else
insertVal="${insertVal}${colValue}:"
fi
let num=$num+1
done
echo ${insertVal} >> "${tableName}/${tableName}.txt"
}
function deleteRecord {
typeset pk tableName
if [ -z "$(ls)" ]
then
echo "No Tables To Remove, Database Is Empty."
return
fi
while true
do
read -p "Enter Table Name: " tableName
validateParamName $tableName
if [ $? -eq 0 ]
then
break
fi
done
if [ ! -d "$tableName" ]
then
echo "Table Doesn't Exist"
return
fi
if [ -s "$tableName/$$tableName.txt" ]
then
echo "The $tableName is empty."
return
fi
read -p "Enter the pk of the table to delete: " pk
while true
do
if [ ! -z "$(grep ^${pk} ${tableName}/${tableName}.txt)" ]
then
sed -i "/^${pk}/d" "${tableName}/${tableName}.txt"
echo "The record of Pk = ${pk} has been deleted successfully."
break
else
echo "The PK doesn't Exist"
return
fi
done
}
function selectTable {
typeset tableName colsNum
if [ -z "$(ls)" ]
then
echo "No Tables To Select From, Database Is Empty."
return
fi
while true
do
read -p "Enter Table Name: " tableName
validateParamName $tableName
if [ $? -eq 0 ]
then
break
fi
done
if [ ! -d "$tableName" ]
then
echo "Table Doesn't Exist"
return
fi
if [ -s "$tableName/$$tableName.txt" ]
then
echo "The $tableName is empty."
return
fi
tail -1 ${tableName}/${tableName}-meta.txt | sed 's/:/\t/g'
sed 's/:/\t/g' ${tableName}/${tableName}.txt && echo
}
function updateTable {
typeset tableName pk colName oldValue newValue colnum
if [ -z "$(ls)" ]
then
echo "No Tables To Delete, Database Is Empty."
return
fi
while true
do
read -p "Enter Table Name: " tableName
validateParamName $tableName
if [ $? -eq 0 ]
then
break
fi
done
if [ ! -d "$tableName" ]
then
echo "${tableName} Doesn't Exist"
return
fi
if [ -z "$(cat "$tableName/$tableName.txt")" ]
then
echo "The $tableName is empty."
return
fi
read -p "Enter Pk: " pk
if [ -z "$(grep ^${pk} ${tableName}/${tableName}.txt)" ]
then
echo "The PK doesn't Exist"
return
fi
read -p "Enter column name: " colName
if [ -z "$(grep ${colName} ${tableName}/${tableName}-meta.txt)" ]
then
echo "The ${colName} column doesn't Exist"
return
fi
colnum=$(awk -F: '{ for (i=1; i<=NF; i++) { if ($i == "'"$colName"'") { print i; exit } } }' ${tableName}/${tableName}-meta.txt)
oldValue=$( grep "^${pk}" ${tableName}/${tableName}.txt | cut -d ':' -f $colnum )
read -p "Enter New Value: " newValue
sed -i "/^$pkValue/s/$oldValue/$newValue/" ${tableName}/${tableName}.txt
}
function showTablesMenu {
select choice2 in "Create Table" "List Tables" "Drop Tables" "Insert" "Select" "Delete" "Update" "Quit"
do
case $choice2 in
"Create Table") createTable
;;
"List Tables") listTables
;;
"Drop Tables") dropTable
;;
"Insert") insertTable
;;
"Select") selectTable
;;
"Delete") deleteRecord
;;
"Update") updateTable
;;
"Quit")
cd ../..
break
;;
*) echo "$choice2 is not valid"
;;
esac
done
}
#----------------------- End Fuctions Area-----------------------------------------
#----------------------- Start Script Main body------------------------------------
PS3="Select Option: "
select choice in "Create Database" "List Databases" "Connect Database" "Drop Database" "Exit"
do
case $choice in
"Create Database") createDb
;;
"List Databases") listDbs
;;
"Connect Database") connectDb
;;
"Drop Database") DropDb
;;
"Exit") exit
;;
*) echo "$choice is not valid"
;;
esac
done
#----------------------- End Script Main body------------------------------------