-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_table.sh
executable file
·147 lines (113 loc) · 2.69 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
clear
re='^[a-zA-Z]+$'
column_names=()
data_types=()
source $(pwd)/functions
source $(pwd)/use_Database.sh
function check_column_number() {
while true; do
echo "Enter Number of columns:"
read columnNo
if [[ $columnNo =~ ^[1-9]+$ ]]; then
echo "you columns number is $columnNo"
break
elif [[ "$columnNo" =~ $re ]]; then
echo "your column number must be a number"
elif [[ -z $columnNo ]]; then
echo "your column number must not be empty"
else
echo "invalid number"
fi
done
}
function check_index_primary_key() {
while true; do
echo "Enter location of primary key "
read pk_index
if [[ $pk_index =~ ^[1-9]+$ ]]; then
echo "$pk_index" >>"$filename"
break
elif [[ -z $pk_index ]]; then
echo "your column number is empty"
else
echo "invalid "
fi
done
}
function primary_key() {
check_index_primary_key
while true; do
echo "Enter your column name :"
read primary_key
#if not empty and not repeated
if test -z "$primary_key"; then
echo "your key is empty"
elif [[ $primary_key =~ ^[0-9]+$ ]]; then
echo "your key must be alphabatic"
else
column_names+=("$primary_key")
#array[${#column_names[@]}] = "$primary_key"
break
fi
done
check_data_type
}
function read_column_data() {
for ((i = 1; i < columnNo; i++)); do
echo "Enter the $i column name:"
read column
while true; do
if [[ -z "$column" ]]; then
echo "your column name must not be empty"
elif [[ $primary_key =~ ^[0-9]+$ ]]; then
echo "your key must be alphabatic"
elif [[ $primary_key =~ ^[a-zA-Z-9]+$ ]]; then
echo "your key is valid"
else
check_data_type
column_names+=("$column")
break
fi
done
done
}
function print_names() {
for i in "${column_names[@]}"; do
printf '%s' "$i:" >>"$filename"
printf '%s' "$i |"
done
printf "\n" >>"$filename"
}
function print_data_types() {
for i in "${data_types[@]}"; do
printf '%s' "$i:" >>"$filename"
done
}
echo "your table name must be string only"
echo "------------------------------------"
while true; do
echo "Enter your table name:"
read filename
if [ -f "$filename" ]; then
echo "you enterd an existed table "
echo "try again !!"
elif [[ "$filename" =~ $re ]]; then
echo $(touch $filename)
check_column_number
primary_key
read_column_data
print_names
print_data_types
printf "\n"
echo "your meta data saved successfully to $filename"
echo "------------------------------------------------"
break
elif [ -z "$filename" ]; then
echo "your file name is empty"
else
echo "invalid"
fi
done
cd ..
source "DatabaseEngine.sh"