Class: ActiveRecord::ConnectionAdapters::SQLite2Adapter

Inherits:
SQLiteAdapter show all
Defined in:
lib/active_record/connection_adapters/sqlite_adapter.rb

Overview

:nodoc:

Direct Known Subclasses

DeprecatedSQLiteAdapter

Instance Method Summary collapse

Methods inherited from SQLiteAdapter

#adapter_name, #add_lock!, #begin_db_transaction, #change_column, #change_column_default, #columns, #commit_db_transaction, #delete, #indexes, #insert, #native_database_types, #primary_key, #quote_column_name, #quote_string, #remove_column, #remove_index, #rename_column, #requires_reloading?, #rollback_db_transaction, #select_all, #select_one, #supports_count_distinct?, #supports_migrations?, #tables, #update

Methods inherited from AbstractAdapter

#clear_query_cache

Instance Method Details

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

:nodoc:



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

def add_column(table_name, column_name, type, options = {}) #:nodoc:
  alter_table(table_name) do |definition|
    definition.column(column_name, type, options)
  end
end

#execute(sql, name = nil) ⇒ Object

SQLite 2 does not support COUNT(DISTINCT) queries:

select COUNT(DISTINCT ArtistID) from CDs;

In order to get the number of artists we execute the following statement

SELECT COUNT(ArtistID) FROM (SELECT DISTINCT ArtistID FROM CDs);


373
374
375
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 373

def execute(sql, name = nil) #:nodoc:
  super(rewrite_count_distinct_queries(sql), name)
end

#rename_table(name, new_name) ⇒ Object



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

def rename_table(name, new_name)
  move_table(name, new_name)
end

#rewrite_count_distinct_queries(sql) ⇒ Object



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

def rewrite_count_distinct_queries(sql)
  if sql =~ /count\(distinct ([^\)]+)\)( AS \w+)? (.*)/i
    distinct_column = $1
    distinct_query  = $3
    column_name     = distinct_column.split('.').last
    "SELECT COUNT(#{column_name}) FROM (SELECT DISTINCT #{distinct_column} #{distinct_query})"
  else
    sql
  end
end