Module: LightRecord::RelationMethods
- Defined in:
- lib/light_record.rb
Instance Method Summary collapse
-
#light_records(options = {}) ⇒ Object
Executes query and return array of light object (model class extended by LightRecord).
-
#light_records_each(options = {}) ⇒ Object
Same as ‘#light_records` but iterates through result set and call block for each object this uses less memroy because it creates objects one-by-one it uses stream feature of mysql client.
Instance Method Details
#light_records(options = {}) ⇒ Object
Executes query and return array of light object (model class extended by LightRecord)
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/light_record.rb', line 121 def light_records( = {}) client = connection.instance_variable_get(:@connection) sql = self.to_sql = { stream: false, symbolize_keys: true, cache_rows: false, as: :hash, database_timezone: ActiveRecord::Base.default_timezone } result = _light_record_execute_query(connection, sql, ) klass = LightRecord.build_for_class(self.klass, result.fields) if [:set_const] self.klass.const_set(:"LR_#{Time.now.to_i}", klass) end need_symbolize_keys = defined?(PG::Result) && result.is_a?(PG::Result) records = [] result.each do |row| row.symbolize_keys! if need_symbolize_keys records << klass.new(row) end return records end |
#light_records_each(options = {}) ⇒ Object
Same as ‘#light_records` but iterates through result set and call block for each object this uses less memroy because it creates objects one-by-one it uses stream feature of mysql client
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/light_record.rb', line 151 def light_records_each( = {}) conn = ActiveRecord::Base.connection_pool.checkout sql = self.to_sql = { stream: true, symbolize_keys: true, cache_rows: false, as: :hash, database_timezone: ActiveRecord::Base.default_timezone } result = _light_record_execute_query(conn, sql, ) klass = LightRecord.build_for_class(self.klass, result.fields) if [:set_const] self.klass.const_set(:"LR_#{Time.now.to_i}", klass) end need_symbolize_keys = defined?(PG::Result) && result.is_a?(PG::Result) result.each do |row| row.symbolize_keys! if need_symbolize_keys yield klass.new(row) end ensure ActiveRecord::Base.connection_pool.checkin(conn) end |