1
-
2
1
require_relative 'my_sqlite_request'
3
2
4
3
class MySQLite
@@ -8,7 +7,6 @@ def initialize
8
7
end
9
8
end
10
9
11
- #SELECT * FROM students.db
12
10
def queryUserInput
13
11
loop do
14
12
print 'my_sqlite_cli>'
@@ -40,10 +38,18 @@ def queryUserInput
40
38
table_column = $2
41
39
table_data = $3
42
40
hash_data_array = [ ]
43
- split_data = CSV . parse ( table_data ) . first
44
- runInsertQuery ( table_name , table_column , hash_data_array , split_data ) . run
45
-
46
-
41
+ parsed_table_data = CSV . parse ( table_data ) . first
42
+ runInsertQuery ( table_name , table_column , hash_data_array , parsed_table_data ) . run
43
+
44
+ when /(?:UPDATE|update)\s +(\w +)\s +(?:SET|set)\s +(.+?)\s +(?:WHERE|where)\s +(.+)/
45
+
46
+ table_name = "#{ $1} .csv"
47
+ values = $2
48
+ where_conditions = $3
49
+ request = runUpdateQuery ( table_name , values , where_conditions ) . run
50
+
51
+
52
+
47
53
else
48
54
puts 'Invalid query format. Please enter a valid query...e.g..SELECT * FROM database.csv'
49
55
end #when
@@ -62,18 +68,18 @@ def runSelectQuery(table_name,column_name)
62
68
if column_name =="*"
63
69
request = request . select ( column_name )
64
70
else
65
- columNameArray = column_name . split ( "," ) #was "name,age" now ['name','age']
71
+ columNameArray = column_name . split ( "," )
66
72
request = request . select ( *columNameArray )
67
73
end
68
74
return request
69
75
end
70
76
71
77
def runSelectWhereQuery ( conditions , table_name , column_name )
72
78
request = runSelectQuery ( table_name , column_name )
73
- splitted_conditions = conditions . split ( "," ) #["name='Ivica Zubac'", "year_start='1949'"]
79
+ splitted_conditions = conditions . split ( "," )
74
80
75
81
splitted_conditions . each do |current_condition |
76
- current_pair_array = current_condition . split ( "=" ) #["name", "'Matt Zunic'"]
82
+ current_pair_array = current_condition . split ( "=" )
77
83
78
84
condition_array = current_pair_array . map do |condition |
79
85
condition . delete_prefix ( "'" ) . delete_suffix ( "'" )
@@ -90,17 +96,18 @@ def runJoinQuery(join_conditions,table_name,table_name2,column_name)
90
96
return request
91
97
end
92
98
93
- def runInsertQuery ( table_name , table_column , hash_data_array , split_data )
99
+ def runInsertQuery ( table_name , table_column , hash_data_array , parsed_table_data )
94
100
if table_column
95
101
split_column = table_column . split ( "," )
96
102
split_column . each_with_index do |column , i |
97
103
data_hash = { }
98
- data_hash [ column ] = split_data [ i ]
104
+ data_hash [ column ] = parsed_table_data [ i ]
99
105
hash_data_array <<data_hash
100
106
end
101
107
102
108
end
103
109
110
+
104
111
request = MySqliteRequest.new
105
112
request = request.insert(table_name)
106
113
if table_column
@@ -110,8 +117,52 @@ def runInsertQuery(table_name,table_column,hash_data_array,split_data)
110
117
end
111
118
request
112
119
end
120
+ def runUpdateQuery(table_name,values,where_conditions)
121
+ splitted_values=values.split(", ")
122
+ values_to_update={}
123
+
124
+ splitted_values.each do |value_pair|
125
+ value_pair_hash={}
126
+ #create an hash for eaac pair,then push to an array
127
+ value_pair_array=value_pair.split("=")
128
+ edited_pair_array=value_pair_array.map do |element|
129
+ element.strip
130
+ end
131
+
132
+ value_pair_hash[edited_pair_array[0]]=edited_pair_array[1]
133
+ values_to_update.merge!(value_pair_hash)
134
+ end
135
+
136
+
137
+ request = MySqliteRequest.new
138
+ request = request.update(table_name)
139
+ request = request.values(values_to_update)
140
+ request = processWhereConditions(where_conditions,request)
141
+ request
142
+ end
143
+
144
+ def processWhereConditions(conditions,request)
145
+ splitted_conditions=conditions.split(",")
146
+
147
+ splitted_conditions.each do |current_condition|
148
+ current_pair_array=current_condition.split("=")
149
+ edited_pair_array=current_pair_array.map do |element|
150
+ element.strip
151
+ end
152
+ request=request.where(*edited_pair_array)
153
+ end
154
+ return request
155
+ end
113
156
MySQLite.new
157
+ # UPDATE nba_player_data SET name = 'Bill Renamed', year_start = '2330' WHERE name = 'Bill Zopf',year_start='1971'
114
158
159
+ # request = MySqliteRequest.new
160
+ # request = request.update('nba_player_data.csv')
161
+ # request = request.values('name' => 'Alaa Renamed2','year_start'=>'2023')
162
+ # request = request.where('name', 'Alaa Renamed')
163
+ # request = request.where('year_start', '1991')
164
+
165
+ # the values were received as [{"name"=>"Alaa Renamed2", "year_start"=>"2023"}]
115
166
116
167
# INSERT INTO nba_player_data (name,year_start,year_end,position,height,weight,birth_date,college) VALUES (Alaa Abdelnaby34,1991,1995,F-C,6-10,240,"June 24, 1968",Duke University)
117
168
@@ -134,3 +185,5 @@ def runInsertQuery(table_name,table_column,hash_data_array,split_data)
134
185
# request=request.select('*')
135
186
# request=request.join('weight','nba_players.csv','weight2')
136
187
# request.run
188
+
189
+ # UPDATE students SET email = 'jane@janedoe.com', blog = 'https://blog.janedoe.com' WHERE name = 'Mila'
0 commit comments