Class: ActiveRecord::ConnectionAdapters::AbstractAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/abstract_adapter.rb

Overview

All the concrete database adapters follow the interface laid down in this class. You can use this interface directly by borrowing the database connection from the Base with Base.connection.

Direct Known Subclasses

PostgreSQLAdapter, SQLServerAdapter

Constant Summary collapse

@@row_even =
true

Instance Method Summary collapse

Constructor Details

#initialize(connection, logger = nil) ⇒ AbstractAdapter

:nodoc:



267
268
269
270
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 267

def initialize(connection, logger = nil) # :nodoc:
  @connection, @logger = connection, logger
  @runtime = 0
end

Instance Method Details

#adapter_name ⇒ Object

Returns the human-readable name of the adapter. Use mixed case - one can always use downcase if needed.



348
349
350
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 348

def adapter_name()
  'Abstract'
end

#add_column(table_name, column_name, type, options = {}) ⇒ Object



392
393
394
395
396
397
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 392

def add_column(table_name, column_name, type, options = {})
  native_type = native_database_types[type]
  add_column_sql = "ALTER TABLE #{table_name} ADD #{column_name} #{type_to_sql(type, options[:limit])}"
  add_column_options!(add_column_sql, options)
  execute(add_column_sql)
end

#add_index(table_name, column_name, index_type = '') ⇒ Object



419
420
421
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 419

def add_index(table_name, column_name, index_type = '')
  execute "CREATE #{index_type} INDEX #{table_name}_#{column_name.to_a.first}_index ON #{table_name} (#{column_name.to_a.join(", ")})"
end

#add_limit!(sql, options) ⇒ Object



355
356
357
358
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 355

def add_limit!(sql, options)
  return unless options
  add_limit_offset!(sql, options)
end

#add_limit_offset!(sql, options) ⇒ Object



360
361
362
363
364
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 360

def add_limit_offset!(sql, options)
  return if options[:limit].nil?
  sql << " LIMIT #{options[:limit]}"
  sql << " OFFSET #{options[:offset]}" if options.has_key?(:offset) and !options[:offset].nil?
end

#begin_db_transaction ⇒ Object

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



312
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 312

def begin_db_transaction()    end

#change_column(table_name, column_name, type, options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


403
404
405
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 403

def change_column(table_name, column_name, type, options = {})
  raise NotImplementedError, "change_column is not implemented"
end

#change_column_default(table_name, column_name, default) ⇒ Object

Raises:

  • (NotImplementedError)


407
408
409
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 407

def change_column_default(table_name, column_name, default)
  raise NotImplementedError, "change_column_default is not implemented"
end

#columns(table_name, name = nil) ⇒ Object

Returns an array of column objects for the table specified by table_name.



279
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 279

def columns(table_name, name = nil) end

#commit_db_transaction ⇒ Object

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



315
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 315

def commit_db_transaction()   end

#create_table(name, options = {}) {|table_definition| ... } ⇒ Object

Yields:

  • (table_definition)


376
377
378
379
380
381
382
383
384
385
386
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 376

def create_table(name, options = {})
  table_definition = TableDefinition.new(self)
  table_definition.primary_key(options[:primary_key] || "id") unless options[:id] == false

  yield table_definition
  create_sql = "CREATE TABLE #{name} ("
  create_sql << table_definition.to_sql
  create_sql << ") #{options[:options]}"
          
  execute create_sql
end

#delete(sql, name = nil) ⇒ Object

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



288
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 288

def delete(sql, name = nil) end

#drop_table(name) ⇒ Object



388
389
390
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 388

def drop_table(name)
  execute "DROP TABLE #{name}"
end

#initialize_schema_information ⇒ Object



367
368
369
370
371
372
373
374
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 367

def initialize_schema_information
  begin
    execute "CREATE TABLE schema_info (version #{type_to_sql(:integer)})"
    execute "INSERT INTO schema_info (version) VALUES(0)"
  rescue ActiveRecord::StatementInvalid
    # Schema has been intialized
  end
end

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

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



282
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 282

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

#native_database_types ⇒ Object



431
432
433
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 431

def native_database_types
  {}
end

#quote(value, column = nil) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 321

def quote(value, column = nil)
  case value
    when String
      if column && column.type == :binary
        "'#{quote_string(column.string_to_binary(value))}'" # ' (for ruby-mode)
      else
        "'#{quote_string(value)}'" # ' (for ruby-mode)
      end
    when NilClass              then "NULL"
    when TrueClass             then (column && column.type == :boolean ? "'t'" : "1")
    when FalseClass            then (column && column.type == :boolean ? "'f'" : "0")
    when Float, Fixnum, Bignum then value.to_s
    when Date                  then "'#{value.to_s}'"
    when Time, DateTime        then "'#{value.strftime("%Y-%m-%d %H:%M:%S")}'"
    else                            "'#{quote_string(value.to_yaml)}'"
  end
end

#quote_column_name(name) ⇒ Object



343
344
345
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 343

def quote_column_name(name)
  name
end

#quote_string(s) ⇒ Object



339
340
341
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 339

def quote_string(s)
  s.gsub(/\\/, '\&\&').gsub(/'/, "''") # ' (for ruby-mode)
end

#remove_column(table_name, column_name) ⇒ Object



399
400
401
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 399

def remove_column(table_name, column_name)
  execute "ALTER TABLE #{table_name} DROP #{column_name}"
end

#remove_index(table_name, column_name) ⇒ Object



423
424
425
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 423

def remove_index(table_name, column_name)
  execute "DROP INDEX #{table_name}_#{column_name}_index ON #{table_name}"
end

#rename_column(table_name, column_name, new_column_name) ⇒ Object

Raises:

  • (NotImplementedError)


415
416
417
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 415

def rename_column(table_name, column_name, new_column_name)
  raise NotImplementedError, "rename_column is not implemented"
end

#reset_runtime ⇒ Object

:nodoc:



290
291
292
293
294
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 290

def reset_runtime # :nodoc:
  rt = @runtime
  @runtime = 0
  return rt
end

#rollback_db_transaction ⇒ Object

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



319
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 319

def rollback_db_transaction() end

#select_all(sql, name = nil) ⇒ Object

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



273
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 273

def select_all(sql, name = nil) end

#select_one(sql, name = nil) ⇒ Object

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



276
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 276

def select_one(sql, name = nil) end

#structure_dump ⇒ Object

Returns a string of the CREATE TABLE SQL statements for recreating the entire structure of the database.



353
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 353

def structure_dump() end

#supports_migrations? ⇒ Boolean

Returns:

  • (Boolean)


411
412
413
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 411

def supports_migrations?
  false
end

#transaction(start_db_transaction = true) ⇒ Object

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



297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 297

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

#type_to_sql(type, limit = nil) ⇒ Object



435
436
437
438
439
440
441
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 435

def type_to_sql(type, limit = nil)
  native = native_database_types[type]
  limit ||= native[:limit]
  column_type_sql = native[:name]
  column_type_sql << "(#{limit})" if limit
  column_type_sql
end

#update(sql, name = nil) ⇒ Object

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



285
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 285

def update(sql, name = nil) end