-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_mang.sh
984 lines (753 loc) · 22.9 KB
/
table_mang.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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
#!/bin/bash
#####################################################
## This script manages the tables related queries ###
#####################################################
###########################################################################################
# Available Queries on Tables #
###########################################################################################
# DDLs #
###### #
# show tables --> list all tables in the DB #
# describe table tableName --> display the difinition of the table #
# create table tableName col1Name col1_dType [col1_pk], col2_nanme col2_dType [col2_pk],..#
# drop table tableName #
###########################################################################################
# DMLs #
###### #
# insert into tableName values col1_value col2_value col3_value ... #
# delete from tableName [where condition] --> where colName = value #
# select colName/all (for all cols) [where condition] --> where colName = value #
# update tableName set colName = newValue [where condition] --> where colName = value #
###########################################################################################
######### Helper Functions##########
function validateName
{ #Expected argument: table name
#Check if the table name is valid
isNameValid $1
#raise error if invalid
raiseError $? "Error: Invalid name."
return 0
}
function validateDtype()
{ #Expected arguments: datatype
if [ -z $1 ] #doesn't exist
then
raiseError 1 "Error: Invalid datatype."
return 1
fi
if [ ${1,,} = int ] || [ ${1,,} = str ]
then
return 0 #valid datatype
else
#raise error if invalid
raiseError 1 "Error: Invalid datatype."
return 1
fi
}
function isConstrValid()
{ #Expected arguments: constraint(pk or nothing)
if [ -z $1 ]
then
echo none
return 0
fi
if [ ${1,,} = pk ]
then
echo pk
return 0
else
return 1
fi
}
function insertRow()
{ #Expected arguments: file path, row
local filePath="$1"
shift
if [ -z "$1" ]
then
return 1
fi
local tmpStr="$1"
shift
for element in $@ #loop through the rows
do
tmpStr=$tmpStr:$element
done
#append the string
echo $tmpStr >> $filePath
return 0
}
function retreiveRow()
{ #Expected arguments: filePath, lineNumber
echo $(head -n"$2" "$1" | tail -n1)
return 0
}
function isPkUnique()
{ #Expected argument: filePath, column number, col value
local filePath=$1
local fieldNo=$2
local pkValue=$3
#Check if the pk is unique
local isUniq=`sed '1,3d' "$filePath" |\
awk -F: -v value="$pkValue" -v fieldNo="$fieldNo" '{if($(fieldNo) == value){print "1"}}'`
if [ -z $isUniq ] #not found
then
isUniq=0
fi
return $isUniq
}
function insertRecord()
{ #Expecsted arguments: filePath, row values
local filePath=$1
shift
#get the number of columns in the file
local colCount=`awk -F':' '{print NF; exit}' $filePath`
#Check if the number of columns is out of boundries
if [ $# -gt $colCount ]
then
raiseError 1 "Invalid insertion: Number of values out of boundry."
fi
#read columns data type definition in an array
declare -a local dtypeArr
local dTypeRow=2
local row=`retreiveRow $filePath $dTypeRow`
readarray -d : -t dtypeArr <<< $row
#read columns constraint definition in an array
declare -a local constrArr
local constrRow=1
local row=`retreiveRow $filePath $constrRow`
IFS=':' read -ra constrArr <<< $row
local numRegex='^[0-9]+$'
local let indx=0
local let pkField
declare -a local recordArr
IFS=' ' read -ra recordArr <<< "$@"
#loop through the columns of the table
for constr in "${constrArr[@]}"
do
#Check if the value doesn't exist
if [ -z ${recordArr[$indx]} ] || [ ${recordArr[$indx],,} = null ]
then
#check if the constraint is pk
if [ $constr = pk ]
then
raiseError 1 "Error: Primary key column cannot be null"
else
recordArr[$indx]=NULL
fi
#the value exists
else
#validate datatype
if [ ${dtypeArr[$indx]} = int ] && \
[[ ! ${recordArr[$indx]} =~ $numRegex ]]
then
raiseError 1 "Error: Datatype mismatch."
fi
#validate primary key
if [ $constr = pk ]
then
let pkField=$indx+1
isPkUnique $filePath $pkField ${recordArr[$indx]}
raiseError $? "Error: Primary key violation."
fi
fi
let indx=$indx+1
done
#Insert the constructed record
insertRow $filePath ${recordArr[@]}
return 0 #valid datatype
}
function isColExist()
{ #Expected arguments: filePath, colName
#returns the field number of the col (if existed)
local filePath=$1
local targetCol=$2
local colNameRow=3
declare -a local colArr
#Fetch the cols names
local row=`retreiveRow "$filePath" "$colNameRow"`
readarray -d : -t colArr <<< $row
#check if the colName exists in the array
local found=1
local let fieldNo=1
for col in ${colArr[@]}
do
if [ $col = $targetCol ] #Found
then
found=0
echo $fieldNo
break
fi
let fieldNo=$fieldNo+1
done
return $found
}
function getCellValue()
{ #Expected arguments: filePath, rowNumber, colNumber
#get the cell value
echo $(sed -n "$2"p $1 | cut -f"$3" -d:)
return 0
}
function getRowNumber()
{ #Expected arguments: filePath, colNumber, value , targetValue
local filePath=$1
local colNum=$2
local targetValue=$3
#extract the targeted column values
local num=$(cut -d: -f$colNum "$filePath" | sed '1,3d' |\
awk -F: -v target="$targetValue" \
'BEGIN{count=1}{if($1 == target){print count} {count++}}')
#Was not found
if [ -z "$num" ]
then
return 1
else
echo $num
return 0
fi
}
function displayColumn()
{ #Expected arguments: filePath, colNum
local filePath=$1
shift
if [ $1 = "all" ] #display all columns
then
#Display the table and execlude the definition rows
printTable ':' "$(sed '1,2d' $filePath)"
else #display one column
cut -d: -f"$1" $filePath | printTable ':' "$(sed '1,2d' )"
fi
return 0
}
function processWhereCond()
{ #Expected arguments: where colName = value
if [ ! ${1,,} = where ] #invalid syntax
then
raiseError 1 "Error: Invalid syntax."
return 1
fi
shift
#check if the condition exists (colName operator value)
if [ "$#" -ne 3 ]
then
raiseError 1 "Error: Invalid syntax."
fi
# Check if the column name is valid
validateName $1
#Check if the column name exist
local fieldNo
fieldNo=`isColExist $filePath $1`
raiseError $? "Error: Unknown column."
#get datatype of this column
local dType=`getCellValue $filePath 1 $fieldNo`
shift
local numRegex='^[0-9]+$'
#Check the operator is equal
if [ ! $1 = "=" ]
then
raiseError 1 "Error: Invalid syntax."
fi
#check if the value is int if the datatype is int
if [ $dType = int ] && [[ ! $2 =~ $numRegex ]]
then
raiseError 1 "Error: Invalid datatype."
fi
shift
#Check the value
local targetValue=$1
#Search for the record number
local targetRowNumber
targetRowNumber=`getRowNumber $filePath $fieldNo $targetValue`
raiseError $? "Error: Record doesn't exist."
#convert to array
IFS=' ' read -ra rowNumArr <<<"$targetRowNumber"
}
function updateTable ()
{ #Expected argumets: filePath, colNum, newValue, arr of row numbers or (all for all)
local filePath=$1
local colNum=$2
local newValue=$3
shift 3
if [ $1 = all ] #update all rows
then
#update all records to the temp file
awk -F: -v value=$newValue -v fieldNo=$colNum 'BEGIN{OFS=":"}\
{if(NR >3){$fieldNo = value};print $0}' $filePath > $tempPath
#save changes and exit
mv $tempPath $filePath
return 0
fi
#read the array of rows to be updated
declare -a local rowNumArr
readarray -d" " -t rowNumArr <<< "$@"
local let rowNumber
#update only the rows in the array
for rowNum in ${rowNumArr[@]}
do
#update the row Number(shifted by 3 for def lines)
let rowNumber=$rowNum+3
#update the given record to the temp file
awk -F: -v rowNumber=$rowNumber -v value=$newValue -v fieldNo=$colNum 'BEGIN{OFS=":"}\
{if(NR == rowNumber){$fieldNo = value};print $0}' $filePath > $tempPath
#save changes
mv $tempPath $filePath
done
return 0
}
########################
### Process Query #####
######################
function procQuery()
{ #Expected arguments: query on tables
#to decide which command to run
#queries are case insensitive
#DDL queries on table
if [ "${1,,} ${2,,}" = "show tables" ]
then
shift 2
showTables $@
elif [ "${1,,}" = "describe" ]
then
shift
describeTable $@
raiseError $? "Error: Table doesn't exist."
elif [ -z $2 ]
then
raiseError 1 "Error: Invalid query syntax."
#table commands (that are identified by table keyword)
elif [ ${2,,} = table ]
then
case ${1,,} in
create)
shift 2
createTable "$@"
raiseError $? "Error : Table already exists"
echo "Table created successfully."
;;
drop)
shift 2
dropTable $@
raiseError $? "Error: Table doesn't exist"
echo "Table dropped successfully."
;;
*)
raiseError 1 "Error: Invalid query syntax."
;;
esac
#DMLs
elif [ ${1,,} = "select" ]
then
shift
selectQuery $@
raiseError $? "Error: Unknown table."
elif [ "${1,,} ${2,,}" = "insert into" ]
then
shift 2
insertQuery $@
raiseError $? "Error: Unknown table."
echo "Record inserted successfully."
elif [ "${1,,} ${2,,}" = "delete from" ]
then
shift 2
deleteQuery $@
raiseError $? "Error: Unknown table."
echo "Records deleted successfully."
elif [ ${1,,} = update ]
then
shift
updateQuery $@
raiseError $? "Error: Unknown table."
echo "Record updated successfully."
else #Not a valid table query
raiseError 1 "Error: Invalid query syntax."
fi
return 0
}
#########################
##### DDL Quieries #####
#######################
function showTables()
{ #Expected arguments: (None)
#Check that there are no arguments passed
isArgNotExist $1
raiseError $? "Error: Invalid syntax too many arguments."
#List all files (tables)
#output in temp file
echo "Tables in ($currDB_name)" > $tempPath
ls $currDB_path >> $tempPath
#print the talbe of tables
printTable ":" "$(cat $tempPath)"
#remove the temp file
rm $tempPath
return 0
}
function describeTable()
{ #(Not Complete)Expected arguments: tableName(only)
#Check if the right number of arguments is passed
isArgExist $1
raiseError $? "Error: Invalid syntax table name missing."
isArgNotExist $2
raiseError $? "Error: Invalid syntax too many arguments."
#Check if the table name is valid
validateName $1
#Check if the table exists!
local filePath=$currDB_path$1
isFound -f $filePath
if [ $? -eq 0 ] #exists
then
#Display the table definition rows
echo $1
printTable ':' "$(sed -n '1,3p' $filePath)"
return 0
else
return 1
fi
}
function createTable()
{ #Expected arguments: tableName, "col1Def" [colsDef]
#columns def syntax: "colName col_dType [col constraint]"
#Check if the right number of arguments is passed
isArgExist $1
raiseError $? "Error: Invalid syntax table name missing."
isArgExist $2
raiseError $? "Error: Invalid syntax columns definition missing."
#Check if the table name is valid
validateName $1
#Check if the table exists
local filePath="$currDB_path$1"
isFound -f $filePath
if [ $? -eq 0 ] #it exsits
then
return 1
fi
shift
#process column definition
declare -a local colDef
#Decalre arrays to store the rows
declare -a local nameArr
declare -a local dtypeArr
declare -a local constrArr
declare -a local row
local currConstr
local pkFlag
#Extract columns definitions
IFS=',' read -ra colDef <<< "$@"
#loop through each column def
for col in "${colDef[@]}"
do
#check the coloumn definition
IFS=' ' read -ra col <<< "$col"
#Check the column name
validateName "${col[0]}"
#Check the data type
validateDtype "${col[1]}"
#validate constraint
currConstr=`isConstrValid "${col[2]}"`
raiseError $? "Error: Invalid constraint."
#Check if the pk was already inserted
if [ $currConstr = pk ]
then
if [ "$pkFlag" = 1 ]
then
raiseError 1 "Error: Multiple primary key defined"
else
pkFlag=1
fi
fi
#add the valid column definition to the array
nameArr+=(${col[0]})
dtypeArr+=(${col[1],,})
constrArr+=(${currConstr,,})
done
#Create table file
touch "$filePath"
#insert the definition rows into the table file
#insert the definition rows
#constraint in the first row
insertRow $filePath ${constrArr[@]}
#datatype in the second row
insertRow $filePath ${dtypeArr[@]}
#column name in the third row
insertRow $filePath ${nameArr[@]}
return 0
}
function dropTable()
{ #Expected arguments: Database Name (only)
#deletes the entire table
#Check if the right number of arguments is passed
isArgExist $1
raiseError $? "Error: Invalid syntax table name missing."
isArgNotExist $2
raiseError $? "Error: Invalid syntax too many arguments."
#Check if the table name is valid
validateName $1
#Check if the table exists
local filePath="$currDB_path$1"
isFound -f $filePath
#delete if it exists
if [ $? -eq 0 ] #it exsits
then
rm $filePath
return 0
else #Doesn't exist
return 1
fi
}
#########################
##### DML Quieries #####
#######################
function insertQuery()
{ #Expected arguments: tableName, values , rows array
#query syntax: insert into table values col1Value col2Value..
#row syntax: col1_value col2_value, [other rows] ...
#Check if the right number of arguments is passed
isArgExist $1
raiseError $? "Error: Invalid syntax table name missing."
isArgExist $2
raiseError $? "Error: Invalid syntax."
#Check syntax
if [ ! ${2,,} = values ]
then
raiseError 1 "Error: Invalid syntax."
fi
#Check if the table name is valid
validateName $1
#Check if the table exists
local filePath="$currDB_path$1"
isFound -f $filePath
if [ $? -eq 1 ] #it doesn't exsit
then
return 1
fi
shift 2
#Check if values are entered
if [ -z "$1" ]
then
raiseError 1 "Error: Empty values."
fi
# read multiple insertion
declare -a local insertionArr
IFS=',' read -ra insertionArr <<< "$@"
#Check the datatype for each element
for record in "${insertionArr[@]}"
do
if [ -z "$record" ] #break if there's nothing left
then
break
fi
insertRecord $filePath "$record"
done
return 0
}
function deleteQuery()
{ #Expected arguments: tableName, [where , colName , operator, value]
#query syntax: delete from tableName where colName = value
#Check if the right number of arguments is passed
isArgExist $1
raiseError $? "Error: Invalid syntax table name missing."
#Check if the table name is valid
validateName $1
#Check if the table exists
local filePath="$currDB_path$1"
isFound -f $filePath
if [ $? -eq 1 ] #it doesn't exist
then
return 1
fi
shift
#Check if where condition exists
if [ -z $1 ] #empty the table
then
#save the definition
sed -n '1,3p' > $tempPath $filePath
mv $tempPath $filePath
return 0
fi
#Delete based on condition
declare -a rowNumArr
processWhereCond "$@"
#Construct the new table (in temp file)
#save the definition
sed -n '1,3p' $filePath > $tempPath
#delete the difinition lines
sed '1,3d' -i $filePath
local let initCount=0
local let lineNum=0
#delete each line
for rowNum in "${rowNumArr[@]}"
do
let lineNum=$rowNum-$initCount
sed "$lineNum"d -i $filePath
let initCount=$initCount+1
done
#move the constructed table to the temp file
sed -n p $filePath >> $tempPath
#save the new values
mv $tempPath $filePath
return 0
}
function selectQuery()
{
#Expected arguments: colName(all for all), From, TableName, [where, colName, operator, value]
#query syntax: select colName from tableName where colName = value
#Check if the right number of arguments is passed
isArgExist $1
raiseError $? "Error: Invalid syntax column name missing."
#Check the number of arguments
if [ "$#" -lt 3 ]
then
raiseError 1 "Error: Invalid syntax."
return 1
fi
#Check if the table name is valid
validateName $3
#Check if the table exists
local filePath="$currDB_path$3"
isFound -f $filePath
if [ $? -eq 1 ] #it doesn't exist
then
return 1
fi
local fieldNo
#check the column name
if [ ${1,,} = "all" ] #Select all columns
then
fieldNo="all"
else #Check the column name
# Check if the column name is valid
validateName $1
#Check if the coloumn name exist
fieldNo=`isColExist $filePath $1`
raiseError $? "Error: Unknown column."
fi
shift
#Check the syntax
if [ ! ${1,,} = from ] #invalid syntax
then
raiseError 1 "Error: Invalid syntax."
return 1
fi
shift 2
#check if where condition exists
#Check if where condition exists
if [ -z $1 ] #no condition (display all)
then
displayColumn $filePath $fieldNo
return 0
fi
#select based on condition
declare -a rowNumArr
processWhereCond "$@"
#Construct the new table to be selected (in temp file)
#move the data to be selected in a temp file
sed -n '1,3'p $filePath > $tempPath
local let lineNum=0
#Move each line
for rowNum in "${rowNumArr[@]}"
do
#Shift for the definition rows
let lineNum=$rowNum+3
#append the line to the new table
sed -n "$lineNum"p $filePath >> $tempPath
done
#Display the new table
displayColumn $tempPath $fieldNo
#remove the temp data
rm $tempPath
return 0
}
function updateQuery()
{ #Expected arguments: tableName, set , colName, = , newValue , [where, colName, operator, value]
#query syntax: update tableName set colName = newValue where colName = value
#Check if the right number of arguments is passed
isArgExist $1
raiseError $? "Error: Invalid syntax table name missing."
isArgExist $2
raiseError $? "Error: Invalid syntax."
#Check syntax
if [ ! ${2,,} = set ]
then
raiseError 1 "Error: Invalid syntax."
fi
#Check if the table name is valid
validateName $1
#Check if the table exists
local filePath="$currDB_path$1"
isFound -f $filePath
if [ $? -eq 1 ] #it doesn't exist
then
return 1
fi
shift 2
#Check the update condition
if [ "$#" -lt 3 ]
then
raiseError 1 "Error! Invalid syntax."
fi
#Check the column name is valid
validateName $1
#Check if the column name exist
local newFieldNo
newFieldNo=`isColExist $filePath $1`
raiseError $? "Error: Unknown column."
#get datatype of this column
local dType=`getCellValue $filePath 2 $newFieldNo`
#get the constraint
local constr=`getCellValue $filePath 1 $newFieldNo`
shift
local numRegex='^[0-9]+$'
#Check the operator is equal
if [ ! $1 = "=" ]
then
raiseError 1 "Error: invalid syntax."
fi
#check if the value is int if the datatype is int
if [ $dType = int ] && [[ ! $2 =~ $numRegex ]]
then
raiseError 1 "Error: Invalid datatype."
fi
#store the new value
local newValue=$2
if [ ${newValue,,} = null ] #Check if the new value is null
then
newValue=NULL
fi
#check if the update is on the primary key
if [ $constr = pk ]
then
#Check if the new value is null
if [ $newValue = NULL ]
then
raiseError 1 "Error: Primary key column cannot be null"
fi
isPkUnique $filePath $newFieldNo $newValue
raiseError $? "Error: Primary key violation."
fi
shift 2
#Check if where condition exists
if [ -z $1 ] #no condition (update all)
then
#trying to update all pk values with the same value
if [ $constr = pk ]
then
raiseError 1 "Error: Primary key violation."
fi
#update all records in the table
updateTable $filePath $newFieldNo $newValue "all"
return 0
fi
#Update based on condition
declare -a rowNumArr
processWhereCond "$@"
#update the selected rows of the table
updateTable $filePath $newFieldNo $newValue "${rowNumArr[@]}"
return 0
}
##### Main #######
function main ()
{ #Expected arguements: table query statments
#Process the query
procQuery "$@"
}
#Call main function
main "$@"