-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateTable.sh
162 lines (125 loc) · 5.49 KB
/
CreateTable.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
#!/usr/bin/bash
#description this script: Create Tables
#fucntion check regex table name
function isValidTB() {
local re='^([A-Za-z]+)[_]*[A-Za-z]*$'
(( ${#1} > 16 )) && return 1 #check lenght first argument is greater than 16 return one
[[ "$1" =~ $re ]] # return value of this comparison is used for the function
}
function testValidTableName() {
if isValidTB "$1"; then
return 0
else
return 1
fi
}
function OnlyNumber()
{
local re='^([0-9]+)*$'
(( ${#1} > 16 )) && return 1 #check lenght first argument is greater than 16 return one
[[ "$1" =~ $re ]] # return value of this comparison is used for the function
}
function testOnlyNumber() {
if OnlyNumber "$1"; then
return 0
else
return 1
fi
}
#ask what is name table--->okay
#vaildation table name ---->okay
#check file table is exist or no -->okay
#check file table.Mdata--->okay
# two cases
# cancat inputs columns id : integer : YES >>table name.Mdata-->okay
# cancat inputs columns fname : string :NO >>table name.Mdata--->okay
#input user columns name --->okay
#input datatype column --->int or string --->ask evey columns-->okay
#input assume first columns is pk--->okay
#don't implem
#validation NColumns --->okay
#assume first columns is pk in all table when created
#validation Name Column is unique --->
read -p "Please,Enter Table Name: " TName
#check Tname is valid or no ---->yes
testValidTableName "$TName"
if [ $? -eq 0 -a ! ${#TName} -eq 0 ]
then
if [ ! -f ./$TName -a ! -f "./${TName}.Mdata" ]
then
touch ./$TName
touch "./${TName}.Mdata"
read -p "Enter number of Columns and You must Number at least 2 Columns: " NColumns
testOnlyNumber "$NColumns"
if [ $? -eq 0 ]
then
if [ $NColumns -gt 1 ] #if not zero and string
then
for i in $(eval echo "{1..$NColumns}")
do
while [ true ]
do
read -p "Enter Column name: " ColName
#check unique Names Column
testValidTableName "$ColName"
if [ $? -eq 0 ]
then
while (( `cut -d":" -f1 "./$TName.Mdata" | grep -x $ColName |wc -w` > 0 ))
do
while [ true ]
do
read -p "$ColName should be unique, please enter another Name to this Column: " ColName
testValidTableName "$ColName"
if [ $? -eq 0 ]
then
break
else
echo "please, Enter Vaild name table"
fi
done
done
echo "Please,choose Datatypes column ?"
select DtypeCol in int string
do
case $DtypeCol in
"int")
break
;;
"string")
break
;;
*)
echo select valid datatype for column
;;
esac
done
if [ $i -eq 1 ]
then
PK=YES
else
Pk=NO
fi
echo $ColName:$DtypeCol:$Pk >>"$TName.Mdata"
break
else
echo "Sorry,Can't enter invalid Characters"
fi
done
done
echo "Table is Created :)"
else
echo "you must enter at least two columns"
rm ./$TName
rm "./${TName}.Mdata"
fi
else
echo "Sorry,Please enter only Numbers"
rm ./$TName
rm "./${TName}.Mdata"
fi
else
echo "Table is already Exist"
fi
else
echo "Please,Enter valid table name"
fi