-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.sh
677 lines (613 loc) · 17.7 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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
#!/bin/bash
# This is a script used to simulate a simple crud operations on database engine
__Author__="Islam Youssief"
#/******************** Creating the main dir of all dbs *********************************/
if [[ ! -d "DBs" ]]; then
mkdir "DBs";
fi
#/************************ Seperator between any taken actions **************************/
function seperator {
echo "--------------------------------------------------------------"
}
#/***************************** Main Gui Of the script *********************************/
function getMainMenu {
seperator;
echo -e "\n+~~~~~~~~~~Main Menu~~~~~~~~~~~~+"
echo "| 1. Create New Database |"
echo "| 2. Use A Database |"
echo "| 3. Rename A Database |"
echo "| 4. Drop A Database |"
echo "| 5. Show All Database |"
echo "| 6. Exit |"
echo "+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+"
echo -e " Enter Your Choice: \c"
read choice
case $choice in
1) createDB
getMainMenu;
;;
2) listAllDBs;
useDB;
;;
3) #listAllDBs;
renameDB
getMainMenu;
;;
4) seperator;
dropDB;
getMainMenu;
;;
5) listAllDBs;
getMainMenu;
;;
6) exit ;;
*) echo " This Is Not A Valid Choice " ;
getMainMenu;
esac
}
#/***************************** Creating a Database *********************************/
function createDB {
path="DBs";
printf 'Enter Your Database Name : '
IFS= read -r name rest
case $name in
([[:alpha:]][[:alpha:]][[:alpha:]]*)
if [[ ! -d $path/$name ]]; then
mkdir $path/$name;
if [[ $? -eq 0 ]]; then
echo $name "Was Created Successfully" ;
else
echo "Error While Creating $name Database" ;
fi
else
echo "Sorry But This Database is already Exists";
fi
;;
([[:alpha:]]*) echo "DB Must start with at least 3 Characters !";;
([[:digit:]]*) echo "DB can't start with a decimal digit !";;
("") echo "Make sure to put a valid db name !";;
(*) echo "Special Characters are not allowed in db name !";;
esac
}
#/***************************** list all Databases *********************************/
function listAllDBs {
path="DBs";
dbItem=1;
if [[ `ls $path` = "" ]];then
echo "There Is No Database In Your DMBS !";
getMainMenu;
return 0;
fi
# putting all the dbs in the dblist
for DBName in `ls $path`
do
DBList[$dbItem]=$DBName;
let dbItem=dbItem+1;
done
# listing all the dbs stored in dblist
echo "These Are All The Available Databases : ";
dbItem=1;
for DBName in `ls $path`
do
DBList[$dbItem]=$DBName;
echo $dbItem") "$DBName;
let dbItem=dbItem+1;
done
}
#/***************************** Rename DataBase *********************************/
function renameDB {
path="DBs";
isExist="false";
if [[ `ls $path` = "" ]]; then
echo "There Is No Database To Be Renamed";
else
echo -e " Enter Database Name You want to change : \c"
read currentDB
for DB in `ls $path`
do
if [[ $currentDB == $DB ]]; then
echo -e " Enter New Database Name: \c"
IFS= read -r newDB rest
case $newDB in
([[:alpha:]][[:alpha:]][[:alpha:]]*)
if [[ ! -d $path/$newDB ]]; then
mv ./$path/$currentDB ./$path/$newDB
if [[ $? -eq 0 ]]; then
echo "Database Renamed Successfully with :"{ $newDB }
else
echo "Error While renaming $newDB Database" ;
fi
else
echo "Sorry But There Is Already A Database With The Same Name !";
fi
;;
([[:alpha:]]*) echo "DB Must start with at least 3 Characters !";;
([[:digit:]]*) echo "DB can't start with a decimal digit !";;
("") echo "Make sure to put a valid db name !";;
(*) echo "DB can't start with a Special Characters !";;
esac
fi
done
fi
}
#/***************************** Drop DataBase *********************************/
function dropDB {
path="DBs";
listAllDBs;
read -p " Enter a choice of which db to drop : " choise ;
is_in ${DBList[$choise]} "${DBList[@]}";
if [[ "$?" == "1" ]]; then
read -p "Sure You want to Drop ${DBList[$choise]} [Y/N] " response;
case $response in
[yY][eE][sS]|[yY])
rm -r $path/${DBList[$choise]};
echo "${DBList[$choise]} db was removed Successfully.."
DBList[$choise]="";
;;
*)
seperator;
getMainMenu;
;;
esac
else
{
seperator;
echo "Please enter a valid choice !";
listAllDBs;
}
fi
}
# Check existance of value in a list
function is_in {
local value
for value in "${@:2}"
do
if [[ "$value" == "$1" ]]
then
return 1;
fi
done
# value is not in the list
return 0
}
#/***************************** After Selecting A Database *********************************/
#/**************************** Creating New Table *********************************/
function createTable {
read -p " Enter Table Name : " tableName ;
DBPATH="DBs"
if [[ ! -e $DBPATH/${DBList[$dbChoice]}/$tableName.tbl ]] && [[ $tableName != "" ]]
then
touch $DBPATH/${DBList[$dbChoice]}/$tableName;
chmod +x $DBPATH/${DBList[$dbChoice]}/$tableName;
if [[ $? -eq 0 ]]; then
echo -e "~~~~~~~~~~~~~~~~~ ~> $tableName Structure <~ ~~~~~~~~~~~" > $DBPATH/${DBList[$dbChoice]}/$tableName;
read -p "Enter Columns Number : " tableCol;
if [[ $tableCol -eq 0 ]]
then
echo -e "You must enter the column number !"
rm $DBPATH/${DBList[$dbChoice]}/$tableName
createTable;
return $dbChoice;
fi
echo "Number Of Columns ~> : $tableCol" >> $DBPATH/${DBList[$dbChoice]}/$tableName;
# echo -e "~~~~~~~~~~~~~~~~~ $tableName Columns ~~~~~~~~~~"
for (( i = 1; i <= tableCol ; i++ )); do
read -p "Enter name for column $i : " ColName ;
if [[ -n $ColName ]]
then
echo " "
else
echo -e "\nPlease enter a valid column name"
rm $DBPATH/${DBList[$dbChoice]}/$tableName
createTable;
return $Cho;
fi
colArr[$i]=$ColName ;
echo -n "$ColName" >> $DBPATH/${DBList[$dbChoice]}/$tableName ;
PS3=" Enter Column Type : ";
select colType in Number String
do
case $colType in
"Number")
echo -e ":Number" >> $DBPATH/${DBList[$dbChoice]}/$tableName;
break ;
;;
"String")
echo -e ":String" >> $DBPATH/${DBList[$dbChoice]}/$tableName;
break ;
;;
*)
echo "Please Choose One Of The Available Types !"
esac
done
done
while true; do
i=1;
for col in "${colArr[@]}"; do
echo $i")"$col;
i=$((i+1)) ;
done
echo "Primarykey is necessary in any table So :"
read -p "Choose A Primarykey Column : " Primarykey;
if [ $Primarykey -le $tableCol -a $Primarykey -gt 0 ]
then
echo $Primarykey >> $DBPATH/${DBList[$dbChoice]}/$tableName;
break ;
else
echo "You Have To Choose A Primarykey For Table $tableName ";
continue ;
fi
done
colArrIndex=1
while [ $colArrIndex -le $tableCol ]
do
if [ $colArrIndex -eq $tableCol ]
then echo -e "${colArr[colArrIndex]}" >> $DBPATH/${DBList[$dbChoice]}/$tableName;
else
echo -n "${colArr[colArrIndex]}:" >> $DBPATH/${DBList[$dbChoice]}/$tableName;
fi
colArrIndex=$((colArrIndex+1));
done
echo "-----------------------------------------" >> $DBPATH/${DBList[$dbChoice]}/$tableName;
seperator;
clear;
echo "Table was created successfully..";
else
echo "There was an Error While Creating the Table" ;
fi
else
echo "Sorry But This Table Is Already Existed !";
fi
return $dbChoice;
}
#/**************************** List All Tables *********************************/
function listAllTables {
i=1;
DBPATH="DBs";
if [[ `ls $DBPATH/${DBList[$dbChoice]}/` = "" ]];then
echo "There Is No Table In This DB !";
useDB $dbChoice;
return 0;
fi
# Pushing all the tables into table list
for TB in `ls $DBPATH/${DBList[$dbChoice]}/`
do
tableList[$i]=$TB;
let i=i+1;
done
# Showing all the tables in this db
echo "These are All The Available Tables : ";
i=1;
for TB in `ls $DBPATH/${DBList[$dbChoice]}/`
do
tableList[$i]=$TB;
echo $i") "$TB;
let i=i+1;
done
}
#/**************************** Drop Table *********************************/
function dropTable {
DBPATH="DBs"
read -p "Choose Which Table To Drop : " choosenTable ;
is_in ${tableList[$choosenTable]} "${tableList[@]}";
if [[ "$?" == "1" ]]; then
read -p "Are You Sure Droping ${tableList[$choosenTable]} Table (Y/N) " response;
case $response in
[yY][eE][sS]|[yY])
rm $DBPATH/${DBList[$dbChoice]}/${tableList[$choosenTable]};
echo "Table Removed Successfully.."
;;
*)
seperator;
useDB $dbChoice;
;;
esac
else
{
seperator;
echo "Please Enter A Valid Choice !";
listAllTables;
}
fi
return $dbChoice;
}
#/****************** Table Operations****************************
function TableOperations {
temp=$1;
if [[ "$1" == "" ]]; then
read -p "Choose Your Table : " tableChoice ;
else
let tableChoice=temp;
fi
seperator;
is_in ${tableList[$tableChoice]} "${tableList[@]}";
if [[ $? == 1 ]]; then
echo -e "You Are Using ${tableList[$tableChoice]} Table\n";
echo -e "\n+ ~~~Table Operations Menu ~~~~+"
echo "| 1. Insert Row |"
echo "| 2. Update Row |"
echo "| 3. Display Table Details |"
echo "| 4. Display Specific Record |"
echo "| 5. Delete Specific Record |"
echo "| 6. Back To Main GUI |"
echo "| 7. Exit |"
echo "+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+"
echo -e " Enter Your Choice: \c"
read choice
case $choice in
1) seperator;
displayTB
seperator;
insertNewRow;
TableOperations $?;
getMainMenu;
;;
2) seperator;
displayTB;
updateSpecificRow;
TableOperations $?;
getMainMenu;
;;
3) seperator;
displayTB;
TableOperations $?;
;;
4) seperator;
displayTB;
displaySpecificRow;
TableOperations $?;
;;
5) seperator;
displayTB;
deleteSpecificRow;
TableOperations $?;
;;
6) getMainMenu;
;;
7) exit -1 ;
;;
*) echo " This Is Not A Valid Choice " ;
getMainMenu;
esac
fi
}
#/*********************************Display All Coumns **********************
function displayAllCols()
{
declare -a colList
choosenTable=$1;
colArrIndex=1;
noCols=`awk -F: '{if (NR == 2) print $2 }' $choosenTable`;
ColName=$((noCols+4));
pkVal=`cut -f1 -d: $choosenTable | head -$((noCols+3)) | tail -1 `
pkCol=$((pkVal+2))
pkColName=`cut -f1 -d: $choosenTable | head -$pkCol | tail -1 `
echo "These are All The Table Columns : "
while [ $colArrIndex -le $noCols ]
do
tempColName=`cut -f$colArrIndex -d: $choosenTable | head -$ColName | tail -1 `
colList[$colArrIndex]=$tempColName
echo " $((colArrIndex)). $tempColName"
colArrIndex=$((colArrIndex+1))
done
echo "Primary Key : $pkColName "
seperator;
}
#/****************************** primary key functions *******************
function getPKNum()
{
TblName=$1;
rowToDisplay=$2;
noCols=`awk -F: '{if (NR == 3) print $2 }' $TblName`;
ignoredLines=$(($noCols+7));
ignoredLines=$((`cat $TblName | wc -l `-ignoredLines));
pkVal=$((noCols+5));
pkVal=`cut -f1 -d: $TblName | head -$pkVal | tail -1 `;
pkFndLine=`tail -$ignoredLines $TblName | grep -wn $rowToDisplay | cut -f1 -d: `;
pkFndLine=$(($pkFndLine+$noCols+7));
echo $pkFndLine;
}
function checkPrimaryKey()
{
sendPkVal=$1
noCols=`awk -F: '{if (NR == 2) print $2 }' $TblName`
ignoredLines=$(($noCols+5))
ignoredLines=$((`cat $TblName | wc -l `-ignoredLines))
pkVal=$((noCols+3))
pkVal=`cut -f1 -d: $TblName | head -$pkVal | tail -1 `
tstFound=` tail -$ignoredLines $TblName | cut -f$pkVal -d: | grep -w $sendPkVal `
[ $tstFound ] && echo 1 || echo 0
}
# /*****************Check Column DataType*****************************
function checkColumnType()
{
ColVal=$1;
ColValType=${ColVal//[^0-9]/}
if [[ $ColVal == $ColValType ]]
then
echo 1;
else
echo 0;
fi
}
function getColumnType()
{
curNoCols=$1
noCols=$((`awk -F: '{if (NR == 2) print $2 }' $TblName`))
curCellDataType=` cut -f2 -d: $TblName | head -$((noCols+2)) | tail -$noCols | head -$curNoCols | tail -1 `
if [ $curCellDataType = "Number" ]
then
echo 1;
else
echo 0;
fi
}
# /************************** Insert A new Row TO specific Table *********************************
function insertNewRow {
DBPATH="DBs";
noCols=$((`awk -F: '{if (NR == 2) print $2 }' $DBPATH/${DBList[$dbChoice]}/${tableList[$tableChoice]}`));
ignoredLines=$(($noCols+5))
ignoredLines=$((`cat $DBPATH/${DBList[$dbChoice]}/${tableList[$tableChoice]} | wc -l `-ignoredLines))
pkVal=$((noCols+5))
pkVal=`cut -f1 -d: $DBPATH/${DBList[$dbChoice]}/${tableList[$tableChoice]} | head -$pkVal | tail -1 `
curNoCols=1;
echo "Insert Your Row : ";
displayAllCols $DBPATH/${DBList[$dbChoice]}/${tableList[$tableChoice]}
while [ $curNoCols -le $noCols ]
do
while true
do
read -p "Enter Value For Record Num.$curNoCols : " cellValu
curCellDataType=$(getColumnType $curNoCols )
curColDataType=$(checkColumnType $cellValu )
if [[ $cellValu ]] && [[ $curCellDataType -eq $curColDataType ]] && [[ $curCellDataType -eq 1 ]]
then break
elif [[ $cellValu ]] && [[ $curCellDataType -eq $curColDataType ]] && [[ $curCellDataType -eq 0 ]]
then break
else
echo "Make Sure OF Your DataType of This Column";
fi
done
if [ $curNoCols -eq $pkVal ]
then
{
chkPkRtrn=$(checkPrimaryKey $cellValu)
if [ $chkPkRtrn -eq 1 ]
then
{
echo -e "\tSorry This row is already existed with this primary key";
echo "\tplease choose different one !";
break
}
fi
}
fi
if [ $curNoCols -eq $noCols ]
then echo -e "$cellValu" >> $DBPATH/${DBList[$dbChoice]}/${tableList[$tableChoice]}
else
echo -n "$cellValu:" >> $DBPATH/${DBList[$dbChoice]}/${tableList[$tableChoice]}
fi
curNoCols=$((curNoCols+1))
done
echo -e "\t Your new row was inserted successfully..";
return $tChoice;
}
#/********************* Update Specific Row ********************
function updateSpecificRow
{
DBPATH="DBs";
TblName=$DBPATH/${DBList[$dbChoice]}/${tableList[$tableChoice]}
echo "update will be done later";
echo -e "\t You have updated your record successfully.. ";
}
#/*********************************** Display Row **********************************
function displaySpecificRow()
{
DBPATH="DBs";
TblName=$DBPATH/${DBList[$dbChoice]}/${tableList[$tableChoice]}
noCols=$((`awk -F: '{if (NR == 2) print $2 }' $DBPATH/${DBList[$dbChoice]}/${tableList[$tableChoice]}`));
ignoredLines=$(($noCols+5))
ignoredLines=$((`cat $TblName | wc -l `-ignoredLines))
pkVal=$((noCols+3))
pkVal=`cut -f1 -d: $DBPATH/${DBList[$dbChoice]}/${tableList[$tableChoice]} | head -$pkVal | tail -1 `
rowCounter=$(($noCols+6))
while true
do
read -p "Enter your row : " rowToDisplay
if [ $rowToDisplay ]
then break
fi
done
if [ $(checkPrimaryKey $rowToDisplay) == 1 ]
then
{
seperator;
echo "Matched Rows : ";
pkFndLine=`tail -$ignoredLines $TblName | grep -wn $rowToDisplay | cut -f1 -d: `;
pkFndLine=$(($pkFndLine+$noCols+5));
sed -n "${pkFndLine}p" $TblName ;
seperator;
}
else
echo "Sorry There Is No Match !";
fi
return $tableChoice
}
#/**************************** Delete Specific Row *********************
function deleteSpecificRow()
{
echo "will be implemented later !"
return $tableChoice;
}
#/**************************** Display Table Data *********************
function displayTB()
{
DBPATH="DBs";
echo "${tableList[$tableChoice]}";
TblName=$DBPATH/${DBList[$dbChoice]}/${tableList[$tableChoice]};
seperator;
noCols=$((`awk -F: '{if (NR == 2) print $2 }' $DBPATH/${DBList[$dbChoice]}/${tableList[$tableChoice]}`));
ignoredLines=$(($noCols+4))
echo "~~ ~> This Is A Description Of Your Choosen Table <~ ~~~";
tail -n +$ignoredLines $TblName
return $tableChoice
}
#/***************************** Use Database ********************************/
function useDB {
oldChoise=$1;
if [[ "$1" == "" ]]; then
read -p " Choose Database To Use : " dbChoice ;
else
let dbChoice=oldChoise;
fi
seperator;
is_in ${DBList[$dbChoice]} "${DBList[@]}";
if [[ $? == 1 ]]; then
echo " You Are Using ${DBList[$dbChoice]} Database";
else
echo " Please Enter A valid Choice !"
fi
echo -e "\n+~~~~~~~~ Tables Menu ~~~~~~~~~~+"
echo "| 1. Create New Table |"
echo "| 2. Do Operations on Table |"
echo "| 3. Rename A Table |"
echo "| 4. Drop A Table |"
echo "| 5. Show All Tables |"
echo "| 6. Get Back To DB Menu |"
echo "| 7. Exit |"
echo "+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+"
echo -e " Enter Your Choice: \c"
read choice
case $choice in
1) createTable;
useDB $?;
;;
2) seperator;
listAllTables;
TableOperations;
;;
3) #rename table
getMainMenu;
;;
4) seperator;
listAllTables;
dropTable;
useDB $?;
;;
5) seperator;
listAllTables
useDB $dbChoice;
;;
6) getMainMenu;
;;
7) exit ;;
*) echo " This Is Not A Valid Choice " ;
listAllDBs ;
useDB;
esac
}
#/****************************************************************************
# Starting the script
seperator;
echo -e "\t\t\tWelcome To ITI DB Engine";
getMainMenu;