-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsert_Table.sh
executable file
·114 lines (87 loc) · 2.53 KB
/
Insert_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
#! /usr/bin/bash
#Validate Input type
function check_input_type {
local type=$1
local field=$2
if [[ $type == "Integer" ]]; then
if ! [[ $field =~ ^[0-9]+$ ]]; then
echo "Invalid Integer"
return 1
fi
elif [[ $type == "String" ]]; then
if ! [[ $field =~ ^[a-zA-Z]+$ ]]; then
echo "Invalid String"
return 1
fi
fi
return 0
}
#Validate PK
function check_pk_input {
local arr_PK=($1)
local counter=$2
local PK_values=($(awk -F : '{print $'$counter'}' "$table_name"))
for i in "${PK_values[@]}"; do
if [[ $i == "$TableParameter" ]]; then
echo "---------------------------------"
echo "Error: Value duplication in Primary Key!"
echo "---------------------------------"
echo "Please Try Again"
return 1
fi
done
return 0
}
function Insert_Table {
local table_name=$1
if [ $# -eq 1 ]; then
if [ -f "$table_name" ]; then
# Get Number of columns
local column_numbers=$(awk 'END{print NR}' "$table_name.meta")
#Decrement of array index
((column_numbers--))
# init counter
typeset -i counter=1
#init row tring
TableContent=""
#init columns delimiter
ColumnSep=":"
#init row delimter
RowSep="\n"
# Get column names
ArrField=($(cut -d ":" -f 1 <"$table_name.meta"))
# Get Columns type
ArrType=($(cut -d ":" -f 2 <"$table_name.meta"))
# Get Pk flags
ArrPK=($(cut -d ":" -f 3 <"$table_name.meta"))
#loop over columns
while [[ $counter -le $column_numbers ]]; do
read -p "Enter Value for parameter ${ArrField[counter]} (${ArrType[counter]}): " TableParameter
# Validate data type
if ! check_input_type "${ArrType[$counter]}" "$TableParameter"; then
continue
fi
# Validate Uniqu PK
if [[ ${ArrPK[$counter]} == "Yes" ]]; then
if ! check_pk_input "${ArrPK[*]}" "$counter"; then
continue 2
fi
fi
# Build row string
if [[ $counter == $column_numbers ]]; then
TableContent+=$TableParameter
else
TableContent+=$TableParameter$ColumnSep
fi
(( counter++ ))
done
# Insert new row
echo "$TableContent" >> "$table_name"
else
echo "This is not a valid table name"
echo "Please try again with the right name"
fi
else
echo "You need to enter one table name"
fi
}