-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDBMSBashProject.sh
More file actions
executable file
·528 lines (499 loc) · 19.9 KB
/
DBMSBashProject.sh
File metadata and controls
executable file
·528 lines (499 loc) · 19.9 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
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
#!/bin/bash
clear
echo "|--------------------------------------------------|"
echo "| Hello to our DBMS project |"
echo "|--------------------------------------------------|"
RED='\033[0;31m'
GREEN='\033[0;32m'
NOCOLOR='\033[0m'
YELLOW='\033[1;32m'
BLUE='\033[0;34m'
function MainMenu {
echo -e "\n |-------------------Main Menu-------------------------| \n "
select option in "Create Database" "Connect to Database " "List Databases " "Drop Database" "Exit"
do
case $REPLY in
1) CreateDatabases ;;
2) ConnectToDatabase ;;
3) ListDatabases ;;
4) DropDatabases ;;
5) exit ;;
*) echo $REPLY is not one of the choices ;;
esac
done
}
function CreateDatabases {
echo -e "\n Enter your database name : \c"
read dbname
if [[ $dbname = "" ]]; then
echo -e "${RED}You entered empty value ${NOCOLOR}\n"
elif [[ $dbname == *['!'@#\$%^\&*()_+/.#+=-{}[]:?]* ]];
then
echo -e "${RED} You can't enter these characters => . / : * $ @ ^ % ( ) ! + _ # & - | ${NOCOLOR}\n"
elif [[ $dbname =~ ^[0-9] ]];
then
echo -e "\n ${RED}You can't start Database name with number ${NOCOLOR}\n"
elif [ -d $dbname ]
then
echo -e "\n ${RED} Database already exists ${NOCOLOR}\n"
else
mkdir ./$dbname
echo -e "\n ${GREEN} $dbname Created Successfully ${NOCOLOR}\n"
fi
MainMenu
}
function ListDatabases {
ls -F | grep /
echo -e "\n ${GREEN} Listed Successfully ${NOCOLOR}\n"
MainMenu
}
function ConnectToDatabase {
echo -e "\n Enter your database name : \c"
read dbname
if [ -d $dbname ] && [[ $dbname != "" ]]
then
cd ./$dbname
echo -e "\n ${GREEN}Connected Successfully to $dbname ${NOCOLOR}\n"
TableMenu
else
echo -e "\n ${RED}Connecting Failed, please enter valid database name ${NOCOLOR}\n"
ConnectToDatabase
fi
}
function DropDatabases {
echo -e "\n Enter database name you want to drop : \c"
read dbname
if [ -d $dbname ]
then
rm -r ./$dbname
echo -e "${GREEN}Database Droped Successfully ${NOCOLOR}\n"
else
echo -e "${RED} There is no database named ($dbname) ${NOCOLOR}\n "
fi
MainMenu
}
function TableMenu {
echo -e "\n |-------------------Table Menu-------------------------| \n"
select option in "Create Table" "List Table" "Drop Table " "Insert into Table" "Select from Table" "Delete From Table" "Update Table" "Back to Main Menu" "Exit"
do
case $REPLY in
1) CreateTable ;;
2) ListTables ;;
3) DropTable ;;
4) InsertintoTable ;;
5) SelectMenu ;;
6) DeleteFromTable ;;
7) UpdateTable ;;
8) MainMenu ;;
9) exit ;;
*) echo $REPLY is not one of the choices ;;
esac
done
}
function CreateTable {
echo -e "\n Enter your table name : \c"
read tablename
if [[ $tablename == *['!'@#\$%^\&*()_+]* ]];
then
echo -e "\n ${RED}You can't enter these characters => . / : * $ @ ^ % ( ) ! + _ # & - | ${NOCOLOR}\n"
CreateTable
elif [[ $tablename =~ ^[0-9] ]];
then
echo -e "\n ${RED}You can't start Table name with number ${NOCOLOR} \n"
CreateTable
elif [[ $tablename == "" ]]; then
echo -e "\n ${RED}You can't enter empty value ${NOCOLOR} \n"
CreateTable
elif [ -f ./$tablename/$tablename ]
then
echo -e "\n ${RED}Table already exists${NOCOLOR}"
TableMenu
fi
echo -e "Enter Number of Fields (Column) : \n"
read colno
if [[ $colno != *[0-9]* ]]; then
echo -e "${RED}\n Number of Fields (Column) must be Integar \n ${NOCOLOR}"
CreateTable
fi
separator=":"
metaDataFormate="Field Name|Field Type|Primary key"
typeset -i i=1
primarykey=""
data=""
fname=""
space="\n"
while [ $i -le $colno ]
do
echo -e "\n Enter name of Field (Column $i) : \n"
read Fieldname
if [[ $Fieldname == *['!'@#\$%^\&*()_+]* ]];
then
echo -e "\n ${RED}You can't enter these characters in field name => . / : * $ @ ^ % ( ) ! + _ # & - | ${NOCOLOR}\n"
continue
elif [[ $Fieldname == "" ]]; then
echo -e "\n ${RED}You can't enter empty value ${NOCOLOR} \n"
continue
elif [[ $Fieldname =~ ^[0-9] ]];
then
echo -e "\n ${RED}You can't start Field name with number ${NOCOLOR}\n"
continue
else
echo -e "Enter Type of $Fieldname (Column $i) : \n"
select var in "int" "str"
do
case $var in
int ) FieldType="int"; break;;
str ) FieldType="str"; break;;
* ) echo -e "${RED} Wrong Choice ${NOCOLOR}" ;;
esac
done
if [[ $primarykey == "" ]]; then
echo -e "\n Make This Field PrimaryKey ??? "
select var in "yes" "no"
do
case $var in
yes ) primarykey="PK";
data+=$Fieldname$separator$FieldType$separator$primarykey;
break;;
no )
data+=$Fieldname$separator$FieldType$separator""$space;
break;;
* ) echo -e "${RED} Wrong Choice ${NOCOLOR}" ;;
esac
done
else
data+=$space$Fieldname$separator$FieldType$separator"";
fi
((i++))
fi
fname+=$Fieldname"|"
done
mkdir -p $tablename
touch ./$tablename/$tablename
touch ./$tablename/$tablename-metadata
echo "No of columns:$colno" >> ./$tablename/$tablename-metadata
echo $metaDataFormate >> ./$tablename/$tablename-metadata
echo -e $data >> ./$tablename/$tablename-metadata;
echo -e "${GREEN}$tablename created successfully ${NOCOLOR}\n"
echo $fname >> ./$tablename/$tablename
TableMenu
}
function ListTables {
ls -F | grep /
echo -e "${GREEN}\n Tables listed Successfully ${NOCOLOR}\n"
TableMenu
}
function DropTable {
echo -e "\n Enter the table name you want to drop : \c"
read DropT
if [ -d $DropT ] && [[ $DropT != "" ]]
then
rm -r $DropT
echo -e "\n ${GREEN}The Table $DropT Droped Successfully ${NOCOLOR} \n"
else
echo -e "${RED}\n You entered invalid Table Name ${NOCOLOR} \n "
DropTable
fi
TableMenu
}
function InsertintoTable {
echo -e "Enter the table name you want to insert into : \n"
read tablename
if ! [ -d ./$tablename ];then
echo -e "\n ${RED}The Table $tablename doesn't exists .${NOCOLOR} \n"
InsertintoTable
elif [[ $tablename == "" ]]; then
echo -e "${RED} You can't enter empty value .${NOCOLOR} \n"
InsertintoTable
else
NoOfColumns=`awk 'END{print NR}' ./$tablename/$tablename-metadata `
separator=":"
typeset -i i
data=""
alldata=""
fname=""
for (( i=3; i <= $NoOfColumns ; i++ )); do
Fieldname=`awk 'BEGIN{FS=":"}{ if(NR=='$i') print $1}' ./$tablename/$tablename-metadata`
FieldType=`awk 'BEGIN{FS=":"}{if(NR=='$i') print $2}' ./$tablename/$tablename-metadata`
PKorNOT=`awk 'BEGIN{FS=":"}{if(NR=='$i') print $3}' ./$tablename/$tablename-metadata`
fname+=Fieldname"|"
echo -e "\n Enter value of $Fieldname of type $FieldType : \c"
read data
if [[ $data == "" ]];then
echo -e "${RED} You entered empty value ${NOCOLOR}\n"
InsertintoTable
else
if [[ $FieldType == "int" ]] ; then
while ! [[ $data == *[0-9]* ]]; do
echo -e "${RED} $data isn't integar ${NOCOLOR}"
echo -e "Enter value of $Fieldname of type $FieldType : \c"
read data
done
fi
FieldColNum=`cut -d: -f3 ./$tablename/$tablename-metadata | grep -n -w "^PK$" | cut -d: -f1`
Key=$(awk 'BEGIN{FS=":"}{if(NR=='$i') print $3}' ./$tablename/$tablename-metadata)
if [[ $Key == 'PK' ]]; then
while [ true ]; do
countPk=`cut -d '|' -f"$FieldColNum" ./$tablename/$tablename | grep -c -w "$data"`
if [[ $countPk != 0 ]]; then
echo -e "\t ${RED} $data is already used (Duplicated primary key)! ${NOCOLOR} \n"
echo -e "Enter value of $Fieldname of type $FieldType : \c"
read data
else
break
fi
done
fi
fi
alldata+=$data$separator
done
fi
if [[ $data == "" ]]; then
echo -e "\n ${RED} Error in Inserting data ${NOCOLOR} \n "
InsertintoTable
else
echo $alldata >>./$tablename/$tablename
echo -e "${GREEN} \n Data inserted Successfully :) ${NOCOLOR}"
fi
TableMenu
}
function DeleteFromTable {
echo -e "\n Enter the table name : \c"
read TableName
if ! [[ -d $TableName ]];then
echo -e "${RED} Table is not Exist!!! ${NOCOLOR}"
DeleteFromTable
fi
if [[ $TableName == "" ]];then
echo -e "${RED} You can't enter empty value ${NOCOLOR}"
DeleteFromTable
fi
echo -e "\n Enter Column name : \c"
read colName
field=$(
awk '
BEGIN{FS="|"}
{
if(NR==1)
{
for(i=1;i<=NF;i++)
{
if("'$colName'"==$i) print i
}
}
}' ./$TableName/$TableName 2>> /dev/null)
if [[ $field == "" ]];then
echo -e "${RED} Table is not Exist!!! ${NOCOLOR}"
DeleteFromTable
else
echo -e "\nEnter the value: \c"
read value
result=$(awk 'BEGIN{FS=":"} { if ( $'$field' == "'$value'") print $'$field' }' ./$TableName/$TableName)
if [[ $result == "" ]]
then
echo -e "${RED} Table is not Exist!!! ${NOCOLOR}"
DeleteFromTable
else
NR=$(awk 'BEGIN{FS=":"}{ if ($'$field'=="'$value'") print NR }' ./$TableName/$TableName)
sed -i ''$NR'd' ./$TableName/$TableName
echo -e "\n ${GREEN} Row Deleted Successfully ${NOCOLOR}"
TableMenu
fi
fi
}
function SelectMenu {
echo -e "\n |-------------------Select Menu-------------------------| \n"
select option in "Select All (*)" "Select By Column Name" "Select By Condition " "Back to Main Menu" "Back to Table Menu" "Exit"
do
case $REPLY in
1) SelectAll ;;
2) SelectByColName ;;
3) SelectByCondition ;;
4) MainMenu ;;
5) TableMenu ;;
6) exit ;;
*) echo $REPLY is not one of the choices ;;
esac
done
echo -e "\n |-------------------------------------------------------| \n"
}
function SelectAll {
typeset -i i
data=""
echo -e "\n Enter Table name : \c"
read tableName
if [[ $tableName != "" ]] && [[ -d $tableName ]]; then
echo -e "\n"
column -t -s '|' ./$tableName/$tableName 2>> /dev/null | head -1
echo -e "*----------------------------------------------*\n"
column -t -s ':' ./$tableName/$tableName 2>> /dev/null | tail -n +2
echo -e "\n"
echo -e "*----------------------------------------------*\n"
else
echo -e "\n ${RED} You entered invalid value ${NOCOLOR} \n "
SelectAll
fi
SelectMenu
}
function SelectByCondition {
echo -e "Enter the table name : \c"
read TableName
if [[ $TableName != "" ]] && [[ -d $TableName ]]; then
echo -e "\n Please, Enter Column name : \c"
read colName
field=$(
awk '
BEGIN{FS="|"}
{
if(NR==1)
{
for(i=1;i<=NF;i++)
{
if("'$colName'"==$i) print i
}
}
}' ./$TableName/$TableName 2>> /dev/null)
if [[ $colName == "" ]];then
echo -e "\n ${RED} You entered empty value ${NOCOLOR} \n "
SelectByCondition
else
if [[ $field == "" ]];then
echo -e "\n ${RED} Column is not exist!!! ${NOCOLOR} \n "
SelectByCondition
else
echo -e "\n Enter the value : \c \n"
read value
result=$(awk 'BEGIN{FS=":"} { if ( $'$field' == "'$value'") print $'$field' }' ./$TableName/$TableName)
if [[ $result == "" ]]
then
echo -e "\n ${RED} The Value is not Exist!!! ${NOCOLOR} \n "
SelectByCondition
else
echo -e "\n"
NR=$(awk 'BEGIN{FS=":"}{ if ($'$field'=="'$value'") print NR }' ./$TableName/$TableName)
sed -n ''$NR'p' ./$TableName/$TableName
echo -e "\n ${GREEN}Row selected Successfully ${NOCOLOR} \n "
SelectMenu
fi
fi
fi
else
echo -e "\n ${RED} You entered invalid tablename ${NOCOLOR} \n"
SelectByCondition
fi
SelectMenu
}
function SelectByColName {
echo -e "\n Enter Table Name :"
read tableName
if [[ $tableName != "" ]] && [[ -d ./$tableName ]];then
echo -e "\n Enter Column Name :"
read colName
for (( i=3; i <= $NoOfColumns ; i++ )); do
Fieldname=$(awk 'BEGIN{FS="|"}{if(NR==1){for(i=1;i<=NF;i++){if($i=="'$colName'") print i}}}' ./$tableName/$tableName)
done
if [[ $colName == "" ]] && [[ $Fieldname != $colName ]]
then
echo -e "\n \t ${RED} You entered invalid value ${NOCOLOR} \n"
else
field=$(
awk '
BEGIN{FS="|"}
{
if(NR==1)
{
for(i=1;i<=NF;i++)
{
if("'$colName'"==$i) print i
}
}
}' ./$tableName/$tableName)
awk 'BEGIN{FS=":"; ORS = " \n "}{print $'$colName'}' ./$tableName/$tableName | tail -n +2
fi
else
echo -e "\n ${RED} You entered invalid tablename ${NOCOLOR} \n"
fi
SelectMenu
}
function UpdateTable {
echo -e "\n Enter Table Name: \c "
read tableName
if [ -d $tableName ]
then
echo -e "\n\n ${GREEN}Table accessed successfully :) ${NOCOLOR} \n\n"
else
echo -e "\n \t ${RED} There is no Table named ($tableName) ${NOCOLOR} \n"
UpdateTable
fi
echo -e "Enter Column name: \c"
read field
if [[ $field = "" ]];then
echo -e "\n ${RED} You entered empty value ${NOCOLOR} \n"
UpdateTable
else
checkfid=$(awk 'BEGIN{FS="|"}{if(NR==1){for(i=1;i<=NF;i++){if($i=="'$field'") print i}}}' ./$tableName/$tableName)
if [[ $checkfid == "" ]]
then
echo -e "\n \t ${RED} You entered invalid data ${NOCOLOR} \n"
UpdateTable
else
echo -e "\n Enter The Condition Value: \c"
read value
result=$(awk 'BEGIN{FS=":"}{if ($'$checkfid'=="'$value'") print $'$checkfid'}' ./$tableName/$tableName 2>>./.error.log)
if [[ $result == "" ]] && [[ $value == "" ]]
then
echo -e "\n \t ${RED} You entered invalid data ${NOCOLOR} \n"
UpdateTable
else
echo -e "\n Enter Column name to set into : \c"
read setField
Fieldname=$(awk 'BEGIN{FS="|"}{if(NR==1){for(i=1;i<=NF;i++){if($i=="'$setField'") print i}}}' ./$tableName/$tableName)
if [[ $Fieldname == "" ]]
then
echo -e "\n \t ${RED} You entered invalid data ${NOCOLOR} \n"
UpdateTable
elif [[ $setField == "" ]];then
echo -e "\n \t ${RED} You entered empty value ${NOCOLOR} \n"
UpdateTable
else
echo -e "\n Enter new Value to set: \c"
read newValue
if [[ $newValue == "" ]];then
echo -e "\n \t ${RED} You entered empty value ${NOCOLOR}"
else
FieldColNum=`awk 'END{print NR}' ./$tableName/$tableName-metadata`
for (( i = 2; i <= $FieldColNum; i++ )); do
FieldType=`awk 'BEGIN{FS=":"}{if(NR=='$i') print $2}' ./$tableName/$tableName-metadata`
ColNum=`cut -d: -f3 ./$tableName/$tableName-metadata | grep -n -w "^PK$" | cut -d: -f1`
Key=$(awk 'BEGIN{FS=":"}{if(NR=='$i') print $3}' ./$tableName/$tableName-metadata)
if [[ $FieldType == "int" ]] ; then
while ! [[ $newValue == *[0-9]* ]]; do
echo -e "${RED} $newValue isn't integar ${NOCOLOR}"
echo -e "Enter new Value to set: \c"
read newValue
done
elif [[ $Key == 'PK' ]]; then
while [ true ]; do
countPk=`cut -d '|' -f"$ColNum" ./$tableName/$tableName | grep -c -w "$newValue"`
if [[ $countPk != 0 ]]; then
echo -e "\n \t ${RED} Duplcated PK ${NOCOLOR} \n"
UpdateTable
else
break;
fi
done
fi
done
NR=$(awk 'BEGIN{FS=":"}{if ($'$checkfid' == "'$value'") print NR}' ./$tableName/$tableName 2>>./.error.log)
oldValue=$(awk 'BEGIN{FS=":"}{if(NR=='$NR'){for(i=1;i<=NF;i++){if(i=='$Fieldname') print $i}}}' ./$tableName/$tableName 2>>./.error.log)
echo $oldValue
sed -i ''$NR's/'$oldValue'/'$newValue'/g' ./$tableName/$tableName 2>>./.error.log
echo -e "${GREEN}Row Updated Successfully ${NOCOLOR} \n"
TableMenu
fi
fi
fi
fi
fi
}
MainMenu