Module: ActiveRecord::ConnectionAdapters::DatabaseStatements

Included in:
AbstractAdapter
Defined in:
lib/active_record/connection_adapters/abstract/database_statements.rb

Instance Method Summary collapse

Instance Method Details

#add_limit!(sql, options) ⇒ Object

Alias for #add_limit_offset!.



75
76
77
# File 'lib/active_record/connection_adapters/abstract/database_statements.rb', line 75

def add_limit!(sql, options)
  add_limit_offset!(sql, options) if options
end

#add_limit_offset!(sql, options) ⇒ Object

Appends LIMIT and OFFSET options to a SQL statement. This method modifies the sql parameter.

Examples
add_limit_offset!('SELECT * FROM suppliers', {:limit => 10, :offset => 50})

generates

SELECT * FROM suppliers LIMIT 10 OFFSET 50


85
86
87
88
89
90
91
92
# File 'lib/active_record/connection_adapters/abstract/database_statements.rb', line 85

def add_limit_offset!(sql, options)
  if limit = options[:limit]
    sql << " LIMIT #{limit}"
    if offset = options[:offset]
      sql << " OFFSET #{offset}"
    end
  end
end

#begin_db_transactionObject

Begins the transaction (and turns off auto-committing).



65
# File 'lib/active_record/connection_adapters/abstract/database_statements.rb', line 65

def begin_db_transaction()    end

#commit_db_transactionObject

Commits the transaction (and turns on auto-committing).



68
# File 'lib/active_record/connection_adapters/abstract/database_statements.rb', line 68

def commit_db_transaction()   end

#default_sequence_name(table, column) ⇒ Object



94
95
96
# File 'lib/active_record/connection_adapters/abstract/database_statements.rb', line 94

def default_sequence_name(table, column)
  nil
end

#delete(sql, name = nil) ⇒ Object

Executes the delete statement and returns the number of rows affected.



40
# File 'lib/active_record/connection_adapters/abstract/database_statements.rb', line 40

def delete(sql, name = nil) end

#execute(sql, name = nil) ⇒ Object

Executes the SQL statement in the context of this connection. This abstract method raises a NotImplementedError.

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/active_record/connection_adapters/abstract/database_statements.rb', line 29

def execute(sql, name = nil)
  raise NotImplementedError, "execute is an abstract method"
end

#insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) ⇒ Object

Returns the last auto-generated ID from the affected table.



34
# File 'lib/active_record/connection_adapters/abstract/database_statements.rb', line 34

def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) end

#reset_sequence!(table, column, sequence = nil) ⇒ Object

Set the sequence to the max value of the table’s column.



99
100
101
# File 'lib/active_record/connection_adapters/abstract/database_statements.rb', line 99

def reset_sequence!(table, column, sequence = nil)
  # Do nothing by default.  Implement for PostgreSQL, Oracle, ...
end

#rollback_db_transactionObject

Rolls back the transaction (and turns on auto-committing). Must be done if the transaction block raises an exception or returns false.



72
# File 'lib/active_record/connection_adapters/abstract/database_statements.rb', line 72

def rollback_db_transaction() end

#select_all(sql, name = nil) ⇒ Object

Returns an array of record hashes with the column names as keys and column values as values.



6
7
# File 'lib/active_record/connection_adapters/abstract/database_statements.rb', line 6

def select_all(sql, name = nil)
end

#select_one(sql, name = nil) ⇒ Object

Returns a record hash with the column names as keys and column values as values.



11
12
# File 'lib/active_record/connection_adapters/abstract/database_statements.rb', line 11

def select_one(sql, name = nil)
end

#select_value(sql, name = nil) ⇒ Object

Returns a single value from a record



15
16
17
18
# File 'lib/active_record/connection_adapters/abstract/database_statements.rb', line 15

def select_value(sql, name = nil)
  result = select_one(sql, name)
  result.nil? ? nil : result.values.first
end

#select_values(sql, name = nil) ⇒ Object

Returns an array of the values of the first column in a select:

select_values("SELECT id FROM companies LIMIT 3") => [1,2,3]


22
23
24
25
# File 'lib/active_record/connection_adapters/abstract/database_statements.rb', line 22

def select_values(sql, name = nil)
  result = select_all(sql, name)
  result.map{ |v| v.values.first }
end

#transaction(start_db_transaction = true) ⇒ Object

Wrap a block in a transaction. Returns result of block.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/active_record/connection_adapters/abstract/database_statements.rb', line 43

def transaction(start_db_transaction = true)
  transaction_open = false
  begin
    if block_given?
      if start_db_transaction
        begin_db_transaction 
        transaction_open = true
      end
      yield
    end
  rescue Exception => database_transaction_rollback
    if transaction_open
      transaction_open = false
      rollback_db_transaction
    end
    raise
  end
ensure
  commit_db_transaction if transaction_open
end

#update(sql, name = nil) ⇒ Object

Executes the update statement and returns the number of rows affected.



37
# File 'lib/active_record/connection_adapters/abstract/database_statements.rb', line 37

def update(sql, name = nil) end