Module: Property::Db

Extended by:
Db
Included in:
Db
Defined in:
lib/property/db.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#adapterObject



19
20
21
# File 'lib/property/db.rb', line 19

def adapter
  connection.class.to_s[/ConnectionAdapters::(.*)Adapter/,1].downcase
end

#connectionObject



27
28
29
# File 'lib/property/db.rb', line 27

def connection
  ActiveRecord::Base.connection
end

#execute(*args) ⇒ Object



23
24
25
# File 'lib/property/db.rb', line 23

def execute(*args)
  ActiveRecord::Base.connection.execute(*args)
end

#fetch_attributes(attributes, table_name, sql) ⇒ Object



56
57
58
59
# 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) ⇒ Object

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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) ⇒ Object



31
32
33
# File 'lib/property/db.rb', line 31

def quote(value)
  connection.quote(value)
end