1
+ require "csv"
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
4
+ def initialize
5
+ @isWhere = false
6
+ @isJoin = false
7
+ @where_conditions = [ ]
8
+ end
9
+
10
+ def from ( table ) # expect(MySqliteRequest.new.from('database.csv')).to be_a(MySqliteRequest)
11
+ @table_name = table
12
+ self
13
+ end
14
+
15
+ def self . from ( table ) #expect(MySqliteRequest.from('database.csv')).to be_a(MySqliteRequest)
16
+ MySqliteRequest . new . from ( table )
17
+ end
18
+
19
+ def select ( *variable_columns ) # * this converts our parametrs to an array
20
+ @request = 'select'
21
+ @columns = variable_columns
22
+ self
23
+ end
24
+
25
+ def where ( column_name , value ) # During the run() you will filter the result which match the value.
26
+ @where_conditions <<{ column_name => value }
27
+ @isWhere = true
28
+ self
29
+ end
30
+
31
+ def join ( column_on_db_a , filename_db_b , column_on_db_b )
32
+ # Read filename_db_b table data
33
+ @isJoin = true
34
+ @hashedDataB = CSV . parse ( File . read ( filename_db_b ) , headers : true ) . map ( &:to_h ) . take ( 200 )
35
+ @column_on_db_a = column_on_db_a
36
+ @column_on_db_b = column_on_db_b
37
+
38
+ self
39
+ end
40
+
41
+
42
+
43
+ def run
44
+ @result_hash_array = [ ]
45
+ @joined_hash_array = [ ]
46
+ @filtered_hash_array = [ ]
47
+
48
+
49
+ #convert table data in CSV::Row to a HashedData
50
+ if @request ==='select' # select the columns from the each table row and corresponding data
51
+ @table_data = table_to_hashed ( @table_name )
52
+ table_data . each do |current_row |
53
+ result_hash = { }
54
+ @all_conditions_met = true ;
55
+
56
+ process_row ( current_row , result_hash , @result_hash_array , @columns )
57
+
58
+
59
+ if @isWhere
60
+ @where_conditions . each do |current_condition | #all where condtions will run for the current table row before the process_row runs
61
+ current_condition . each do |key , value |
62
+ if current_row [ key ] != value
63
+ @all_conditions_met = false
64
+ break
65
+ end
66
+ end
67
+ end
68
+ process_row ( current_row , result_hash , @filtered_hash_array , @columns ) if @all_conditions_met
69
+ end
70
+
71
+ end #end of table loop
72
+ puts @result_hash_array . inspect
73
+ # @result_hash_array=@filtered_hash_array
74
+ puts '...................filtered'
75
+ puts @filtered_hash_array
76
+ end #of request='select'
77
+
78
+ # puts @filtered_hash_array.inspect
79
+
80
+ # if @isJoin
81
+ # @result_hash_array.each do |rowA|
82
+ # @hashedDataB.each do |rowB|
83
+ # if rowB[@column_on_db_b] == rowA[@column_on_db_a]
84
+ # merged = rowA.merge(rowB)
85
+ # @joined_hash_array<<merged
86
+ # end
87
+ # end
88
+ # end
89
+ # @result_hash_array=@joined_hash_array
90
+ # end
91
+
92
+ # puts @result_hash_array.inspect
93
+ end #of def run
94
+ end #of class
95
+
96
+ #HELPER FUNCTIONS
97
+
98
+ def table_to_hashed ( table_name )
99
+ hashedData = CSV . parse ( File . read ( table_name ) , headers :true ) . map ( &:to_h ) . take ( 3 )
100
+ return hashedData
101
+ end
102
+
103
+ def process_row ( row , result_hash , result_hash_array , columns )
104
+ if columns [ 0 ] ==='*'
105
+ result_hash_array <<row
106
+
107
+ else
108
+ columns.each do |column_name|
109
+ result_hash[column_name]=row[column_name]
110
+
111
+ end
112
+ end
113
+ result_hash_array << result_hash if result_hash != {}
114
+ end
115
+
116
+ # request = MySqliteRequest.new
117
+ # request = request.from('nba_player_data.csv')
118
+ # request = request.select('name')
119
+ # request.run
120
+
121
+ # request = MySqliteRequest.new
122
+ # request = request.from('nba_player_data.csv')
123
+ # request = request.select('name','college')
124
+ # request = request.where('college', 'University of California')
125
+ # request.run
126
+
127
+ request = MySqliteRequest.new
128
+ request = request.from('nba_player_data.csv')
129
+ request = request.select('name','year_start','college')
130
+ request = request.where('college', 'University of California')
131
+ # request = request.where('year_start', '1997')
132
+ # request =request.join('college','nba_players.csv','college')
133
+ request.run
0 commit comments