Skip to content

Commit 1ada6be

Browse files
committed
implemeted csv file creation if not exist
1 parent d81f47e commit 1ada6be

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

mysqlite/database.csv

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
firstname,lastname,age,password
2-
Thomas,Anderson,33,matrix
32
Rahmat,Abdulfattah,25,matrix
3+
Aisha,Abdulfattah,25,matrix
4+
Maryam,Abdulfattah,25,matrix
5+
Zainab,Abdulfattah,25,matrix

mysqlite/my_sqlite_request.rb

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def order(order, column_name)
5151
def insert(table_name)
5252
@table_name=table_name
5353
@request='insert'
54+
5455
self
5556
end
5657

@@ -64,7 +65,13 @@ def values(*data) #a hash of data key => value
6465
end
6566

6667
def run
67-
@hashedDataA=table_to_hashed(@table_name,@dataToInsert)
68+
69+
70+
if File.exist?(table_name)
71+
@hashedDataA=table_to_hashed(@table_name)
72+
else
73+
@hashedData=create_csv_file(@table_name,@dataToInsert) if !File.exist?(table_name)
74+
end
6875
@selected_hash_array=[]
6976
@filtered_hash_array=[]
7077
@merged_hash_array=[]
@@ -122,46 +129,47 @@ def run
122129
end #of request='select'
123130
124131
if @request=='insert' #dear reviewer, i dont know why these brings error in this ide,kindly
132+
125133
CSV.open(@table_name,"a+") do |csv|#test this in VsCode,it inserts data well without errors
126134
@headers=csv.readline
127-
puts headers.inspect
128135
@dataToInsert.each do |data|
129136
dataValues=@headers.map do |header_name|
130137
data[header_name]
131138
end
132139
csv<< dataValues
133-
puts dataValues.inspect
134140
end
135141
end
136142
end #of request='insert'
137143
138-
if @request =='update'
144+
# if @request =='update'
139145
140-
end
146+
# end
141147
142-
# puts '..........................final result'
143-
# puts @final.inspect
148+
puts '..........................final result'
149+
puts @final.inspect
144150
end#of def run
145151
end#of class
146152
147153
#HELPER FUNCTIONS
148-
def table_to_hashed(table_name,dataToInsert)
149-
if !File.exist?(table_name)
150-
CSV.open(table_name,"w") do |csv|
151-
#extract the headers from the @dataToInsert
152-
dataToInsert.each do |data|
153-
headers= data.map do |key,value| #get the key names as headers into an array,map returns anarray
154-
key
155-
end
156-
csv<<headers
157-
end
158-
end
159-
end
160-
154+
def table_to_hashed(table_name)
161155
hashedData=CSV.parse(File.read(table_name),headers:true).map(&:to_h)
162156
return hashedData
163157
end
164158
159+
def create_csv_file(table_name,dataToInsert)
160+
CSV.open(table_name,"w") do |csv|
161+
#extract the headers from the @dataToInsert
162+
dataToInsert.each do |data|
163+
headers= data.map do |key,value| #get the key names as headers into an array,map returns anarray
164+
key
165+
end
166+
csv<<headers
167+
end
168+
end
169+
table_to_hashed(table_name)
170+
end
171+
172+
165173
166174
167175
def process_row(row,result_hash, selected_hash_array,columns)
@@ -208,8 +216,9 @@ def merge(left, right, &block)
208216
result
209217
end
210218
211-
MySqliteRequest.insert('database.csv').values('firstname' => "Rahmat", 'lastname' => "Abdulfattah", 'age' => 25, 'password' => 'matrix').run()
212-
219+
# MySqliteRequest.insert('database.csv').values('firstname' => "Rahmat", 'lastname' => "Abdulfattah", 'age' => 25, 'password' => 'matrix').run()
220+
MySqliteRequest.new.from('database.csv').select('firstname','lastname').where('firstname','Rahmat').run
221+
213222
# request = MySqliteRequest.new
214223
# request = request.update('nba_player_data.csv')
215224
# request = request.values('name' => 'Alaa Renamed')

0 commit comments

Comments
 (0)