Skip to content

Commit e166798

Browse files
committed
added set update
1 parent 35abf89 commit e166798

File tree

3 files changed

+4622
-4562
lines changed

3 files changed

+4622
-4562
lines changed

mysqlite/my_sqlite_request.rb

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,22 @@ def self.insert (table_name)
5959
MySqliteRequest.new.insert(table_name)
6060
end
6161

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
6270
def values(*data) #a hash of data key => value
6371
@dataToInsert=data
6472
self
6573
end
74+
def set(*data) #a hash of data key => value
75+
@dataToInsert=data
76+
self
77+
end
6678

6779
def run
6880

@@ -80,11 +92,12 @@ def run
8092
if @isJoin
8193
joined_tables=join_tables(@hashedDataB,@hashedDataA,@filename_db_b,@column_on_db_b,@column_on_db_a)
8294
@hashedDataA=joined_tables
83-
end#is join
95+
end
8496

8597

8698
#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
88101
@hashedDataA.each do |current_row|
89102
result_hash={}
90103
@all_conditions_met=true;
@@ -116,17 +129,57 @@ def run
116129
end#isOrder
117130
end #end of table loop
118131

119-
end #of request='select'
132+
120133

121-
if @request=='insert'
134+
when 'insert'
122135
insert_row(@table_name,@dataToInsert)
123-
end #of request='insert'
136+
124137

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+
126179
127180
puts '..........................final result'
128181
puts @final.inspect
129-
end#of def run
182+
end#of def run
130183
end#of class
131184
132185
#HELPER FUNCTIONS
@@ -145,13 +198,13 @@ def create_csv_file(table_name,dataToInsert)
145198
end
146199
147200
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)
149202
return hashedData
150203
end
151204
152205
def join_tables(tableB,tableA,tableB_file,tableB_column,tableA_column)
153206
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)
155208
tableA.each do |rowA|
156209
result_hash={}
157210
tableB.each do |rowB|
@@ -175,6 +228,7 @@ def insert_row(table_name,dataToInsert)
175228
end
176229
end
177230
end
231+
178232
def process_row(row,result_hash, selected_hash_array,columns)
179233
if columns[0]==='*'
180234
selected_hash_array<<row
@@ -189,6 +243,7 @@ def process_row(row,result_hash, selected_hash_array,columns)
189243
190244
end
191245
246+
192247
def merge_sort(array, &block)
193248
return array if array.length <= 1
194249
@@ -219,9 +274,8 @@ def merge(left, right, &block)
219274
result
220275
end
221276
222-
223277
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

Comments
 (0)