module Property::Db

This module is just a helper to fetch raw data from the database and could be removed in future versions of Rails if the framework provides these methods.

Public Instance Methods

adapter() click to toggle source
# File lib/property/db.rb, line 19
def adapter
  connection.class.to_s[/ConnectionAdapters::(.*)Adapter/,1].downcase
end
connection() click to toggle source
# File lib/property/db.rb, line 27
def connection
  ActiveRecord::Base.connection
end
execute(*args) click to toggle source
# File lib/property/db.rb, line 23
def execute(*args)
  ActiveRecord::Base.connection.execute(*args)
end
fetch_attributes(attributes, table_name, sql) click to toggle source
# File lib/property/db.rb, line 56
def fetch_attributes(attributes, table_name, sql)
  sql = "SELECT #{attributes.map {|a| connection.quote_column_name(a)}.join(',')} FROM #{table_name} WHERE #{sql}"
  connection.select_all(sql)
end
insert_many(table, columns, values) click to toggle source

Insert a list of values (multicolumn insert). The values should be properly escaped before being passed to this method.

# File lib/property/db.rb, line 37
def insert_many(table, columns, values)
  values = values.compact.uniq.map do |list|
    list.map {|e| quote(e)}
  end

  columns = columns.map {|e| connection.quote_column_name(e)}.join(',')

  case adapter
  when 'sqlite3'
    pre_query = "INSERT INTO #{table} (#{columns}) VALUES "
    values.each do |v|
      execute pre_query + "(#{v.join(',')})"
    end
  else
    values = values.map {|v| "(#{v.join(',')})"}.join(', ')
    execute "INSERT INTO #{table} (#{columns}) VALUES #{values}"
  end
end
quote(value) click to toggle source
# File lib/property/db.rb, line 31
def quote(value)
  connection.quote(value)
end