Class: ActiveRecord::ConnectionAdapters::AbstractAdapter
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::AbstractAdapter
- Includes:
- Benchmark
- 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
Constant Summary collapse
- @@row_even =
true
Instance Method Summary collapse
-
#adapter_name ⇒ Object
Returns the human-readable name of the adapter.
- #add_column(table_name, column_name, type, options = {}) ⇒ Object
- #add_limit!(sql, limit) ⇒ Object
- #add_limit_with_offset!(sql, limit, offset) ⇒ Object
- #add_limit_without_offset!(sql, limit) ⇒ Object
-
#begin_db_transaction ⇒ Object
Begins the transaction (and turns off auto-committing).
-
#columns(table_name, name = nil) ⇒ Object
Returns an array of column objects for the table specified by
table_name. -
#commit_db_transaction ⇒ Object
Commits the transaction (and turns on auto-committing).
- #create_table(name, options = "") ⇒ Object
-
#delete(sql, name = nil) ⇒ Object
Executes the delete statement and returns the number of rows affected.
- #drop_table(name) ⇒ Object
-
#initialize(connection, logger = nil) ⇒ AbstractAdapter
constructor
:nodoc:.
- #initialize_schema_information ⇒ Object
-
#insert(sql, name = nil, pk = nil, id_value = nil) ⇒ Object
Returns the last auto-generated ID from the affected table.
- #quote(value, column = nil) ⇒ Object
- #quote_column_name(name) ⇒ Object
- #quote_string(s) ⇒ Object
- #remove_column(table_name, column_name) ⇒ Object
-
#reset_runtime ⇒ Object
:nodoc:.
-
#rollback_db_transaction ⇒ Object
Rolls back the transaction (and turns on auto-committing).
-
#select_all(sql, name = nil) ⇒ Object
Returns an array of record hashes with the column names as a keys and fields as values.
-
#select_one(sql, name = nil) ⇒ Object
Returns a record hash with the column names as a keys and fields as values.
-
#structure_dump ⇒ Object
Returns a string of the CREATE TABLE SQL statements for recreating the entire structure of the database.
-
#transaction(start_db_transaction = true) ⇒ Object
Wrap a block in a transaction.
-
#update(sql, name = nil) ⇒ Object
Executes the update statement and returns the number of rows affected.
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
391 392 393 394 395 396 397 |
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 391 def add_column(table_name, column_name, type, = {}) native_type = native_database_types[type] add_column_sql = "ALTER TABLE #{table_name} ADD #{column_name} #{native_type[:name]}" add_column_sql << "(#{[:limit] || native_type[:limit]})" if [:limit] || native_type[:limit] add_column_sql << " DEFAULT '#{[:default]}'" if [:default] execute(add_column_sql) end |
#add_limit!(sql, limit) ⇒ Object
355 356 357 358 359 360 361 362 |
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 355 def add_limit!(sql, limit) if limit.is_a? Array limit, offset = *limit add_limit_with_offset!(sql, limit.to_i, offset.to_i) else add_limit_without_offset!(sql, limit) end end |
#add_limit_with_offset!(sql, limit, offset) ⇒ Object
364 365 366 |
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 364 def add_limit_with_offset!(sql, limit, offset) sql << " LIMIT #{limit} OFFSET #{offset}" end |
#add_limit_without_offset!(sql, limit) ⇒ Object
368 369 370 |
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 368 def add_limit_without_offset!(sql, limit) sql << " LIMIT #{limit}" 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 |
#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 = "") ⇒ Object
381 382 383 384 385 |
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 381 def create_table(name, = "") execute "CREATE TABLE #{name} (id #{native_database_types[:primary_key]}) #{}" table_definition = yield TableDefinition.new table_definition.columns.each { |column_name, type, | add_column(name, column_name, type, ) } 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
387 388 389 |
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 387 def drop_table(name) execute "DROP TABLE #{name}" end |
#initialize_schema_information ⇒ Object
372 373 374 375 376 377 378 379 |
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 372 def initialize_schema_information begin execute "CREATE TABLE schema_info (version #{native_database_types[:integer][:name]}#{native_database_types[:integer][:limit]})" 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.
282 |
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 282 def insert(sql, name = nil, pk = nil, id_value = nil) 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 |
#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 |
#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 |
#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 |