@@ -59,10 +59,22 @@ def self.insert (table_name)
59
59
MySqliteRequest . new . insert ( table_name )
60
60
end
61
61
62
+ def update ( table_name )
63
+ @request = 'update'
64
+ @table_name = table_name
65
+ self
66
+ end
67
+ def self . update ( table_name )
68
+ MySqliteRequest . new . update ( table_name )
69
+ end
62
70
def values ( *data ) #a hash of data key => value
63
71
@dataToInsert = data
64
72
self
65
73
end
74
+ def set ( *data ) #a hash of data key => value
75
+ @dataToInsert = data
76
+ self
77
+ end
66
78
67
79
def run
68
80
@@ -80,11 +92,12 @@ def run
80
92
if @isJoin
81
93
joined_tables = join_tables ( @hashedDataB , @hashedDataA , @filename_db_b , @column_on_db_b , @column_on_db_a )
82
94
@hashedDataA = joined_tables
83
- end #is join
95
+ end
84
96
85
97
86
98
#convert table data in CSV::Row to a HashedData
87
- if @request ==='select' # select the columns from the each table row and corresponding data
99
+ case @request
100
+ when 'select' # select the columns from the each table row and corresponding data
88
101
@hashedDataA . each do |current_row |
89
102
result_hash = { }
90
103
@all_conditions_met = true ;
@@ -116,17 +129,57 @@ def run
116
129
end #isOrder
117
130
end #end of table loop
118
131
119
- end #of request='select'
132
+
120
133
121
- if @request == 'insert'
134
+ when 'insert'
122
135
insert_row ( @table_name , @dataToInsert )
123
- end #of request='insert'
136
+
124
137
125
-
138
+ when 'update'
139
+
140
+ #@where_conditions [{"name"=>"Alaa Abdelnaby"}, {"year_start"=>"1991"}]
141
+ updated_array = [ ]
142
+ header = CSV . read ( @table_name , headers :true ) . headers
143
+ CSV . foreach ( @table_name , headers :true ) do |current_csv_row |
144
+ all_conditions_met = true
145
+
146
+ @where_conditions . each do |current_condition |
147
+ current_condition . each do |key , value |
148
+ if current_csv_row [ key ] !=value
149
+ all_conditions_met = false
150
+ break
151
+ end
152
+ end
153
+ end #where
154
+
155
+ if all_conditions_met
156
+ #then update the row before pushing to the csv
157
+ @dataToInsert . each do |current_condition |#[{"name"=>"Alaa Renamed", "college"=>"Renamed University"}]
158
+ current_condition . each do |key , value |
159
+ current_csv_row [ key ] = value #update the value of the current_csv_row
160
+ end
161
+ end
162
+ end
163
+ updated_array <<current_csv_row . to_h
164
+
165
+ end#CSV.foreach
166
+
167
+ CSV.open(@table_name,'w+',write_headers:true,headers:header) do |csv|
168
+
169
+ updated_array.each do |current_updated_row|
170
+ csv<<current_updated_row
171
+ end
172
+ end
173
+ @final=updated_array
174
+ end#case
175
+
176
+
177
+
178
+
126
179
127
180
puts '..........................final result'
128
181
puts @final.inspect
129
- end #of def run
182
+ end#of def run
130
183
end#of class
131
184
132
185
#HELPER FUNCTIONS
@@ -145,13 +198,13 @@ def create_csv_file(table_name,dataToInsert)
145
198
end
146
199
147
200
def table_to_hashed(table_name)
148
- hashedData=CSV.parse(File.read(table_name),headers:true).map(&:to_h).take(5)
201
+ hashedData=CSV.parse(File.read(table_name),headers:true).map(&:to_h)
149
202
return hashedData
150
203
end
151
204
152
205
def join_tables(tableB,tableA,tableB_file,tableB_column,tableA_column)
153
206
joined_table_array=[]
154
- tableB = CSV.parse(File.read(tableB_file), headers: true).map(&:to_h).take(5)
207
+ tableB = CSV.parse(File.read(tableB_file), headers: true).map(&:to_h)
155
208
tableA.each do |rowA|
156
209
result_hash={}
157
210
tableB.each do |rowB|
@@ -175,6 +228,7 @@ def insert_row(table_name,dataToInsert)
175
228
end
176
229
end
177
230
end
231
+
178
232
def process_row(row,result_hash, selected_hash_array,columns)
179
233
if columns[0]==='*'
180
234
selected_hash_array<<row
@@ -189,6 +243,7 @@ def process_row(row,result_hash, selected_hash_array,columns)
189
243
190
244
end
191
245
246
+
192
247
def merge_sort(array, &block)
193
248
return array if array.length <= 1
194
249
@@ -219,9 +274,8 @@ def merge(left, right, &block)
219
274
result
220
275
end
221
276
222
-
223
277
request = MySqliteRequest.new
224
- request = request.from ('nba_player_data.csv')
225
- request = request.select ('name')
226
- request = request.order(:asc, 'name')
227
- request.run
278
+ request = request.update ('nba_player_data.csv')
279
+ request = request.set ('name' => 'Jimmy agabaje ')
280
+ request = request.where( 'name', 'Jim Zoet Renamed2 ')
281
+ request.run
0 commit comments