-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_table.sh
executable file
·125 lines (98 loc) · 2.92 KB
/
create_table.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
#! /usr/bin/bash
CreatTable() {
#Validate table name
#check if users inputs more than one table at a time
if [ $# -ne 1 ]; then
echo "Please Enter One table name."
return 1
elif [[ $1 == *\\* ]]; then
echo -e "Error. Invalid Input, canncot contain backslash.\c"
#check if the table exists
elif [ -f "$1" ]; then
echo "Error: Table $1 already exists"
return 1
elif [[ ! $1 =~ ^[a-zA-Z]+$ ]]; then
echo "Error: Table name can only contain letters"
return 1
fi
#Get Number of columns
while :;
do
read -p "Enter Number Of Columns: " num_columns
if [[ $num_columns =~ ^[0-9]+$ ]] && [[ $num_columns -gt 0 ]]; then
break
else
echo "Error: Please enter a number greater than 0."
fi
done
# Initialize Variables
pk_set=false
metaData="Field:type:PK"
#loop through cols details
for (( i =1; i<=$num_columns; i++ )); do
#Get column name
while :;
do
read -p "Enter Name For Column $1: " col_name
if [[ $col_name =~ ^[a-zA-Z]+$ ]] && [[ ! $prev_name == $col_name ]]; then
break
else
echo "Error: Column name must be unique and only contain letters."
fi
done
#Get Columns type
while :;
do
echo "Select column Type: "
select type in "Integer" "String"; do
case $type in
"Integer" )
col_type="Integer"
break;;
"String" )
col_type="String"
break;;
* )
echo "Invalid Selection"
esac
done
break
done
#Set Primary key
if ! $pk_set; then
while :;
do
echo "Make this columns primary key? (Y/N)"
read choice
case $choice in
[Yy]* )
pk="Yes"
pk_set=true
break;;
[Nn]* )
pk="No"
pk_set=false
break;;
* )
echo "Invalid Choice"
break;;
esac
done
else
pk="No"
fi
#ADD to MetaData
metaData+="\n$col_name:$col_type:$pk"
#Update Previuos name variable
prev_name=$col_name
done
#Validate Primary ky set
if ! $pk_set; then
echo "Error: Must select a primary key columns."
return 1
fi
#Create tabel and meta data file
touch "$1" "$1.meta"
echo -e "$metaData" > "$1.meta"
echo "Table $1 Created Successfully."
}