Skip to content

Commit 490e642

Browse files
committed
added inset and select
1 parent 0d6ea83 commit 490e642

1 file changed

Lines changed: 57 additions & 55 deletions

File tree

main.sh

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -32,63 +32,16 @@ while true; do
3232
#--------
3333
while true; do
3434
echo "===== Tables Menu for '$dbname' ====="
35-
select table_action in "Create Table" "List Tables" "Drop Table" "Back to Main Menu"
35+
select table_action in "Create Table" "List Tables" "Insert into Table" "Select From Table" "Drop Table" "Back to Main Menu"
3636
do
3737
case $table_action in
38-
#----------
39-
"Create Table")
40-
echo "Enter Table Name:"
41-
read tablename
42-
table_file="$DB_DIR/$dbname/$tablename"
43-
meta_file="$table_file.meta"
44-
45-
if [ -f "$table_file" ]; then
46-
echo "Table already exists."
47-
else
48-
echo "How many columns?"
49-
read col_count
50-
51-
columns=() # array store column names
52-
datatypes=() # array store column datatypes
53-
pk_set=false
54-
55-
for (( i=1; i<=col_count; i++ ))
56-
do
57-
echo "Enter name of column $i:"
58-
read col_name
59-
60-
echo "Enter datatype of $col_name (string/number):"
61-
read col_type
62-
63-
# check if datatype is valid or not
64-
while [[ "$col_type" != "string" && "$col_type" != "number" ]]; do
65-
echo "Invalid datatype. Enter string or number:"
66-
read col_type
67-
done
68-
69-
echo "Is this column the Primary Key? (yes/no):"
70-
read is_pk
71-
72-
if [[ "$is_pk" == "yes" && "$pk_set" == false ]]; then
73-
columns+=("$col_name:$col_type:pk")
74-
pk_set=true
75-
else
76-
columns+=("$col_name:$col_type")
77-
fi
78-
done
79-
80-
# Create file for data to insert into it
81-
touch "$table_file"
82-
83-
# Write meta file
84-
for col in "${columns[@]}"; do
85-
echo "$col" >> "$meta_file"
86-
done
87-
88-
echo "Table '$tablename' created with metadata."
89-
fi
90-
break
91-
;;
38+
"Create Table")
39+
echo "Enter table name:"
40+
read tablename
41+
touch "$DB_DIR/$dbname/$tablename"
42+
echo "Table '$tablename' created."
43+
break
44+
;;
9245
"List Tables")
9346
echo "Tables in '$dbname':"
9447
ls "$DB_DIR/$dbname"
@@ -101,6 +54,55 @@ while true; do
10154
echo "Table '$tablename' deleted."
10255
break
10356
;;
57+
"Insert into Table")
58+
echo "Enter table name:"
59+
read tablename
60+
table_path="$DB_DIR/$dbname/$tablename"
61+
62+
if [ -f "$table_path" ]; then
63+
echo "Enter data (format: name=value:type)"
64+
echo "Types: str, int, or float"
65+
read -p "Example: age=25:int " new_data
66+
67+
if [[ "$new_data" == *":str"* || "$new_data" == *":int"* || "$new_data" == *":float"* ]]; then
68+
echo "$new_data" >> "$table_path"
69+
echo "Data inserted!"
70+
else
71+
echo "Error: Missing or invalid type (:str/:int/:float)"
72+
fi
73+
else
74+
echo "Table doesn't exist"
75+
fi
76+
break
77+
;;
78+
"Select From Table")
79+
echo "Enter table name to view:"
80+
read tablename
81+
table_path="$DB_DIR/$dbname/$tablename"
82+
83+
if [ -f "$table_path" ]; then
84+
echo -e "\nContents of '$tablename':"
85+
echo "==========="
86+
cat "$table_path"
87+
echo "==========="
88+
89+
echo "Enter search term (or leave blank to skip):"
90+
read search_term
91+
if [ -n "$search_term" ]; then
92+
echo -e "\nSearch results:"
93+
echo "============="
94+
if grep -q "$search_term" "$table_path"; then
95+
grep "$search_term" "$table_path"
96+
else
97+
echo "No matches found for '$search_term'"
98+
fi
99+
echo "============="
100+
fi
101+
else
102+
echo "Table '$tablename' does not exist."
103+
fi
104+
break
105+
;;
104106
"Back to Main Menu")
105107
break 2
106108
;;

0 commit comments

Comments
 (0)