1
1
require "csv"
2
2
class MySqliteRequest
3
- attr_accessor :table_name , :table_data , :hashedDataB , :request , :columns , :result_hash_array , :isWhere , :where_count , :isJoin , :column_on_db_a , :column_on_db_b , :filter_column , :filter_column_value
3
+ attr_accessor :table_name , :hashedDataA , :hashedDataB , :request , :filename_db_b , :columns , :selected_hash_array , :isWhere , :where_count , :isJoin , :column_on_db_a , :column_on_db_b , :filter_column , :filter_column_value
4
4
def initialize
5
5
@isWhere = false
6
6
@isJoin = false
@@ -31,31 +31,47 @@ def where(column_name, value) # During the run() you will filter the result whic
31
31
def join ( column_on_db_a , filename_db_b , column_on_db_b )
32
32
# Read filename_db_b table data
33
33
@isJoin = true
34
- @hashedDataB = CSV . parse ( File . read ( filename_db_b ) , headers : true ) . map ( & :to_h ) . take ( 200 )
34
+
35
35
@column_on_db_a = column_on_db_a
36
36
@column_on_db_b = column_on_db_b
37
+ @filename_db_b = filename_db_b
37
38
38
39
self
39
40
end
40
41
41
42
42
43
43
44
def run
44
- @result_hash_array = [ ]
45
- @joined_hash_array = [ ]
45
+ @hashedDataA = table_to_hashed ( @table_name )
46
+ @selected_hash_array = [ ]
46
47
@filtered_hash_array = [ ]
48
+ @merged_hash_array = [ ]
47
49
@final = [ ]
48
50
49
-
51
+
52
+ if @isJoin
53
+ @hashedDataB = CSV . parse ( File . read ( @filename_db_b ) , headers : true ) . map ( &:to_h ) . take ( 5 )
54
+ @hashedDataA . each do |rowA |
55
+ result_hash = { }
56
+ @hashedDataB . each do |rowB |
57
+ if rowB [ @column_on_db_b ] === rowA [ @column_on_db_a ]
58
+ merged = rowA . merge ( rowB )
59
+ @merged_hash_array <<merged
60
+ end
61
+ end
62
+ end#hashsedDataA
63
+ @hashedDataA=@merged_hash_array
64
+ end#is join
65
+
66
+
50
67
#convert table data in CSV::Row to a HashedData
51
68
if @request==='select' # select the columns from the each table row and corresponding data
52
- @table_data = table_to_hashed ( @table_name )
53
- table_data . each do |current_row |
69
+ @hashedDataA.each do |current_row|
54
70
result_hash={}
55
71
@all_conditions_met=true;
56
72
57
- process_row ( current_row , result_hash , @result_hash_array , @columns )
58
- @final = @result_hash_array
73
+ process_row(current_row, result_hash, @selected_hash_array , @columns)
74
+ @final=@selected_hash_array
59
75
60
76
61
77
if @isWhere
@@ -75,48 +91,43 @@ def run
75
91
76
92
end #of request='select'
77
93
78
- if @isJoin
79
- @result_hash_array . each do |rowA |
80
- @hashedDataB . each do |rowB |
81
- if rowB [ @column_on_db_b ] == rowA [ @column_on_db_a ]
82
- merged = rowA . merge ( rowB )
83
- @joined_hash_array <<merged
84
- end
85
- end
86
- end
87
- @final=@joined_hash_array
88
- end
89
- puts ".......................final............"
90
- puts @final.inspect
94
+
95
+
91
96
97
+ puts '..........................final result'
98
+ puts @final.inspect
92
99
end#of def run
93
100
end#of class
94
101
95
102
#HELPER FUNCTIONS
96
103
97
104
def table_to_hashed(table_name)
98
- hashedData=CSV.parse(File.read(table_name),headers:true).map(&:to_h).take(55 )
105
+ hashedData=CSV.parse(File.read(table_name),headers:true).map(&:to_h).take(5 )
99
106
return hashedData
100
107
end
101
108
102
- def process_row(row,result_hash, result_hash_array ,columns)
109
+ def process_row(row,result_hash, selected_hash_array ,columns)
103
110
if columns[0]==='*'
104
- result_hash_array <<row
111
+ selected_hash_array <<row
105
112
106
113
else
107
114
columns.each do |column_name|
108
115
result_hash[column_name]=row[column_name]
109
116
110
117
end
111
118
end
112
- result_hash_array << result_hash if result_hash != {}
119
+ selected_hash_array << result_hash if result_hash != {}
120
+
113
121
end
114
122
115
123
116
124
request = MySqliteRequest.new
117
125
request = request.from('nba_player_data.csv')
118
- request = request.select(' name',"height" )
126
+ request = request.select(" name",'weight','weight2','college','year_start','player' )
119
127
request = request.where('college', 'University of California')
120
128
request = request.where('year_start', '1997')
121
129
# request =request.join('college','nba_players.csv','college')
122
- request.run
130
+
131
+ request.run
132
+
133
+
0 commit comments