-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinsert_into_table.sh
More file actions
139 lines (111 loc) · 2.79 KB
/
insert_into_table.sh
File metadata and controls
139 lines (111 loc) · 2.79 KB
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
#!/usr/bin/bash
# $1 is the name of the database you are currently using
clear
echo
list_tables $1
echo
echo -e "${GREEN}Inserting into table in ${LBLUE}$1${GREEN} database${NC}"
echo
while [[ true ]]; do
echo -ne "${PROMPT}Enter The Name Of The Table You Want To Insert into: ${NC}"
#back
if ! read table_name; then
return
fi
#check for empty string
if ! is_not_null $table_name; then
echo
echo -e "${RED}Name Cannot Be Null!${NC}"
continue
fi
#check for existing table name
if ! find_table $1 $table_name; then
echo
echo -e "${RED}Table Doesnot Exist!${NC}"
continue
fi
break
done
#array that has the values of the insert statement
record=()
# Load The 2 arrays from the meta data file of the selected table
load_template $1 $table_name
echo
echo "Your Table's schema"
echo
awk -v var=$table_name 'BEGIN {FS=":"; print "\t\tTable Name: " var "\n"} {if(NR>1) printf $1"<"$2">\t\t"} END{printf "\n"}' "./Databases/$1/${table_name}_template"
echo
echo -e "${YELLOW}Please Pay Attention To Column Name And Datatype\nWhile Inserting To Avoid ${RED}Data Corruption${NC}"
while [[ true ]]; do
echo
echo -ne "${PROMPT}Enter Value Of Primary Key ($pkName) : ${Nc}"
if ! read pk_value; then
return
fi
#check for empty string
if ! is_not_null "$pk_value"; then
echo
echo -e "${RED}Value Cannot Be Null!${NC}"
continue
fi
#rejecting colons in column name
if [[ "$pk_value" = *:* ]]; then
echo
echo -e "${RED}Colons Are Not Allowed!${NC}"
continue
fi
#check data type of pk
if ! check_type "${types[0]}" "$pk_value" ; then
echo
echo -e "${RED}Type Didnot Match, Please Refer to The Schema!${NC}"
continue
fi
#check uniqueness of Pk
if is_not_unique "$1" "$table_name" "$pk_value"; then
echo
echo -e "${RED}Primary Key Must Be Unique!${NC}"
continue
fi
break
done
record+=("$pk_value")
for (( i = 1; i < $numColumns-1; i++ )); do
echo
while [[ true ]]; do
echo -ne "${PROMPT}Enter Value Of (${names[$i]}) : ${Nc}"
if ! read val; then
return
fi
#check for empty string
if ! is_not_null "$val"; then
echo
echo -e "${RED}Value Cannot Be Null!${NC}"
continue
fi
#rejecting colons in column name
if [[ "$val" = *:* ]]; then
echo
echo -e "${RED}Colons Are Not Allowed In Column Name!${NC}"
continue
fi
#check data type of pk
if ! check_type "${types[$i]}" "$val" ; then
echo
echo -e "${RED}Type Didnot Match, Please Refer to The Schema!${NC}"
continue
fi
break
done
# replacing every space with _
val="${val// /_}"
record+=("$val")
done
for (( i = 0; i < $numColumns-1; i++ )); do
echo -n "${record[$i]}:" >> "./Databases/$1/$table_name"
done
echo "" >> "./Databases/$1/$table_name"
echo
echo -e "${GREEN}Data Inserted to $table_name Successfully${NC}"
echo "Press Enter to Return!"
read
return