Module: ActiveRecord::Tablefree::ClassMethods

Defined in:
lib/activerecord-tablefree.rb

Instance Method Summary collapse

Instance Method Details

#connectionObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/activerecord-tablefree.rb', line 170

def connection
  conn = Object.new()
  def conn.quote_table_name(*_args)
    ""
  end
  def conn.quote_column_name(*_args)
    ""
  end
  def conn.substitute_at(*_args)
    nil
  end
  def conn.schema_cache(*_args)
    schema_cache = Object.new()
    def schema_cache.columns_hash(*_args)
      Hash.new()
    end
    schema_cache
  end
  # Fixes Issue #17. https://github.com/softace/activerecord-tablefree/issues/17
  # The following method is from the ActiveRecord gem: /lib/active_record/connection_adapters/abstract/database_statements.rb .
  # Sanitizes the given LIMIT parameter in order to prevent SQL injection.
  #
  # The +limit+ may be anything that can evaluate to a string via #to_s. It
  # should look like an integer, or a comma-delimited list of integers, or
  # an Arel SQL literal.
  #
  # Returns Integer and Arel::Nodes::SqlLiteral limits as is.
  # Returns the sanitized limit parameter, either as an integer, or as a
  # string which contains a comma-delimited list of integers.
  def conn.sanitize_limit(limit)
    if limit.is_a?(Integer) || limit.is_a?(Arel::Nodes::SqlLiteral)
      limit
    elsif limit.to_s.include?(',')
      Arel.sql limit.to_s.split(',').map{ |i| Integer(i) }.join(',')
    else
      Integer(limit)
    end
  end

  # Called by bound_attributes in /lib/active_record/relation/query_methods.rb
  # Returns a SQL string with the from, join, where, and having clauses, in addition to the limit and offset.
  def conn.combine_bind_parameters(**_args)
    ""
  end

  def conn.lookup_cast_type_from_column(*_args)
    lct = Object.new
    def lct.assert_valid_value(*_args)
      true
    end
    # Needed for Rails 5.0
    def lct.serialize(args)
      args
    end
    def lct.deserialize(args)
      args
    end
    def lct.cast(args)
      args
    end
    def lct.changed?(*_args)
      false
    end
    def lct.changed_in_place?(*_args)
      false
    end
    lct
  end

  # This is used in the StatementCache object. It returns an object that
  # can be used to query the database repeatedly.
  def conn.cacheable_query(arel) # :nodoc:
    if prepared_statements
      ActiveRecord::StatementCache.query visitor, arel.ast
    else
      ActiveRecord::StatementCache.partial_query visitor, arel.ast, collector
    end
  end
  conn
end

#from_query_string(query_string) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/activerecord-tablefree.rb', line 154

def from_query_string(query_string)
  unless query_string.blank?
    params = query_string.split('&').collect do |chunk|
      next if chunk.empty?
      key, value = chunk.split('=', 2)
      next if key.empty?
      value = value.nil? ? nil : CGI.unescape(value)
      [ CGI.unescape(key), value ]
    end.compact.to_h

    new(params)
  else
    new
  end
end