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:



261
262
263
264
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 261

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

Instance Method Details

#adapter_nameObject

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



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

def adapter_name()
  'Abstract'
end

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



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

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



413
414
415
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 413

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



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

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

#add_limit_offset!(sql, options) ⇒ Object



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

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_transactionObject

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



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

def begin_db_transaction()    end

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

Raises:

  • (NotImplementedError)


397
398
399
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 397

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)


401
402
403
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 401

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.



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

def columns(table_name, name = nil) end

#commit_db_transactionObject

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



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

def commit_db_transaction()   end

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

Yields:

  • (table_definition)


370
371
372
373
374
375
376
377
378
379
380
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 370

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.



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

def delete(sql, name = nil) end

#drop_table(name) ⇒ Object



382
383
384
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 382

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

#initialize_schema_informationObject



361
362
363
364
365
366
367
368
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 361

def initialize_schema_information
  begin
    execute "CREATE TABLE schema_info (version #{type_to_sql(:integer)})"
    insert "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.



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

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

#native_database_typesObject



425
426
427
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 425

def native_database_types
  {}
end

#quote(value, column = nil) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 315

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



337
338
339
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 337

def quote_column_name(name)
  name
end

#quote_string(s) ⇒ Object



333
334
335
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 333

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

#remove_column(table_name, column_name) ⇒ Object



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

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

#remove_index(table_name, column_name) ⇒ Object



417
418
419
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 417

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)


409
410
411
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 409

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

#reset_runtimeObject

:nodoc:



284
285
286
287
288
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 284

def reset_runtime # :nodoc:
  rt = @runtime
  @runtime = 0
  return rt
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.



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

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.



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

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.



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

def select_one(sql, name = nil) end

#structure_dumpObject

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



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

def structure_dump() end

#supports_migrations?Boolean

Returns:

  • (Boolean)


405
406
407
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 405

def supports_migrations?
  false
end

#transaction(start_db_transaction = true) ⇒ Object

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



291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 291

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



429
430
431
432
433
434
435
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 429

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.



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

def update(sql, name = nil) end