forked from FadyM66/Bash-Project-DBMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_into_table.sh
executable file
·135 lines (108 loc) · 3.91 KB
/
insert_into_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
#!/bin/bash
source ./validation.sh
insert_into_table() {
local DBName="$1"
# Check if the database directory exists
if [[ ! -d "$DBName" ]]; then
echo "Error: Database '$DBName' not found."
return
fi
# List tables in the database
local tables=($(ls -p "$DBName" | grep -v /))
local num_tables=${#tables[@]}
if [ $num_tables -eq 0 ]; then
echo "No tables found in the database."
return
fi
echo "Available tables in database '$DBName':"
for i in "${!tables[@]}"; do
echo "$((i + 1)). ${tables[$i]}"
done
# Prompt user to select a table to insert into
local choice
while true; do
read -p "Enter the number of the table to insert into (or 0 to cancel): " choice
if [[ $choice =~ ^[0-9]+$ ]] && ((choice >= 0 && choice <= num_tables)); then
break
else
echo "Invalid input. Please enter a valid number."
fi
done
if [ $choice -eq 0 ]; then
echo "Insert operation canceled."
return
fi
local TableName="${tables[$((choice - 1))]}"
# Check if the selected table exists
if [[ ! -f "$DBName/$TableName" ]]; then
echo "Error: Table '$TableName' not found."
return
fi
# Read field names, data types, and primary key information from metadata
local metadata
metadata=$(awk '/^# / {print $0}' "$DBName/$TableName" | sed 's/# //')
# Split metadata by semicolon and populate arrays
IFS=';' read -ra fields <<< "$metadata"
local Name=()
local DataType=()
local PK=()
for field in "${fields[@]}"; do
IFS=':' read -r name datatype pk_flag <<< "$field"
Name+=("$name")
DataType+=("$datatype")
PK+=("${pk_flag:-x}")
done
local numOfFields=${#Name[@]}
# Loop to get values for each field
for ((i = 0; i < numOfFields; i++)); do
while true; do
read -p "Enter value for ${Name[$i]}: " FieldName
if [[ ${DataType[$i]} == "int" ]]; then
checkValue=$(is_integer "$FieldName")
if [[ $checkValue == 1 ]]; then
echo "Invalid input. Please enter an integer for ${Name[$i]}."
continue
fi
elif [[ ${DataType[$i]} == "string" ]]; then
FieldName=$(is_valid_name "$FieldName")
if [[ $FieldName == 1 ]]; then
echo "Invalid input. Please enter an string for ${Name[$i]}."
continue
fi
elif [[ ${DataType[$i]} == "boolean" ]]; then
checkValue=$(is_valid_boolean "$FieldName")
if [[ $checkValue == 1 ]]; then
echo "Invalid input. Please enter 'true', 'false', '1', or '0' for ${Name[$i]}."
continue
fi
fi
# Handle primary key uniqueness check
if [[ ${PK[$i]} == "pk" ]]; then
flag=0
values=($(awk -F: -v col=$((i + 1)) '$1 !~ /^#/ {print $col}' "$DBName/$TableName"))
for value in "${values[@]}"; do
if [[ $FieldName == "$value" ]]; then
flag=1
break
fi
done
if [[ $flag == 1 ]]; then
echo "Error: Value for ${Name[$i]} must be unique. It is a primary key."
continue
fi
fi
# Append field value to the table
if [[ $i == 0 ]]; then
echo -n "$FieldName" >>"$DBName/$TableName"
else
echo -n ":$FieldName" >>"$DBName/$TableName"
fi
break
done
done
# Add a newline at the end of the record
echo "" >>"$DBName/$TableName"
echo "Record inserted successfully."
sleep 2
mainmenu
}