Class: ActiveRecord::ConnectionAdapters::SQLiteAdapter

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

Overview

The SQLite adapter works with both the 2.x and 3.x series of SQLite with the sqlite-ruby drivers (available both as gems and from rubyforge.org/projects/sqlite-ruby/).

Options:

  • :database - Path to the database file.

Direct Known Subclasses

SQLite2Adapter, SQLite3Adapter

Defined Under Namespace

Classes: Version

Instance Attribute Summary

Attributes included from QueryCache

#query_cache, #query_cache_enabled

Instance Method Summary (collapse)

Methods inherited from AbstractAdapter

#active?, #create_savepoint, #current_savepoint_name, #decrement_open_transactions, #disable_referential_integrity, #increment_open_transactions, #log_info, #open_transactions, #prefetch_primary_key?, #quote_table_name, #raw_connection, #reconnect!, #release_savepoint, #reset!, #reset_runtime, #rollback_to_savepoint, #supports_savepoints?, #transaction_joinable=, #verify!

Methods included from ActiveSupport::Callbacks

included, #run_callbacks

Methods included from QueryCache

#cache, #clear_query_cache, #columns_with_query_cache, dirties_query_cache, included, #select_all_with_query_cache, #uncached

Methods included from DatabaseLimits

#column_name_length, #columns_per_multicolumn_index, #columns_per_table, #in_clause_length, #index_name_length, #indexes_per_table, #joins_per_query, #sql_query_length, #table_alias_length, #table_name_length

Methods included from Quoting

#quote, #quote_table_name, #quoted_date, #quoted_false, #quoted_true

Methods included from DatabaseStatements

#add_limit!, #add_limit_offset!, #case_sensitive_equality_operator, #default_sequence_name, #delete, #insert, #insert_fixture, #limited_update_conditions, #outside_transaction?, #reset_sequence!, #select_all, #select_one, #select_value, #select_values, #transaction, #update

Methods included from SchemaStatements

#add_column_options!, #add_index, #add_order_by_for_association_limiting!, #add_timestamps, #assume_migrated_upto_version, #change_table, #create_table, #distinct, #drop_table, #dump_schema_information, #index_exists?, #index_name, #initialize_schema_migrations_table, #remove_index, #remove_timestamps, #rename_index, #structure_dump, #table_alias_for, #table_exists?, #type_to_sql

Constructor Details

- (SQLiteAdapter) initialize(connection, logger, config)

Returns a new instance of SQLiteAdapter



98
99
100
101
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 98

def initialize(connection, logger, config)
  super(connection, logger)
  @config = config
end

Instance Method Details

- (Object) adapter_name

:nodoc:



103
104
105
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 103

def adapter_name #:nodoc:
  'SQLite'
end

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

:nodoc:



261
262
263
264
265
266
267
268
269
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 261

def add_column(table_name, column_name, type, options = {}) #:nodoc:
  if supports_add_column? && valid_alter_table_options( type, options )
    super(table_name, column_name, type, options)
  else
    alter_table(table_name) do |definition|
      definition.column(column_name, type, options)
    end
  end
end

- (Object) add_lock!(sql, options)

SELECT … FOR UPDATE is redundant since the table is locked.



208
209
210
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 208

def add_lock!(sql, options) #:nodoc:
  sql
end

- (Object) begin_db_transaction

:nodoc:



195
196
197
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 195

def begin_db_transaction #:nodoc:
  catch_schema_changes { @connection.transaction }
end

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

:nodoc:



295
296
297
298
299
300
301
302
303
304
305
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 295

def change_column(table_name, column_name, type, options = {}) #:nodoc:
  alter_table(table_name) do |definition|
    include_default = options_include_default?(options)
    definition[column_name].instance_eval do
      self.type    = type
      self.limit   = options[:limit] if options.include?(:limit)
      self.default = options[:default] if include_default
      self.null    = options[:null] if options.include?(:null)
    end
  end
end

- (Object) change_column_default(table_name, column_name, default)

:nodoc:



280
281
282
283
284
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 280

def change_column_default(table_name, column_name, default) #:nodoc:
  alter_table(table_name) do |definition|
    definition[column_name].default = default
  end
end

- (Object) change_column_null(table_name, column_name, null, default = nil)



286
287
288
289
290
291
292
293
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 286

def change_column_null(table_name, column_name, null, default = nil)
  unless null || default.nil?
    execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
  end
  alter_table(table_name) do |definition|
    definition[column_name].null = null
  end
end

- (Object) columns(table_name, name = nil)

:nodoc:



227
228
229
230
231
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 227

def columns(table_name, name = nil) #:nodoc:
  table_structure(table_name).map do |field|
    SQLiteColumn.new(field['name'], field['dflt_value'], field['type'], field['notnull'].to_i == 0)
  end
end

- (Object) commit_db_transaction

:nodoc:



199
200
201
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 199

def commit_db_transaction #:nodoc:
  catch_schema_changes { @connection.commit }
end

- (Object) delete_sql(sql, name = nil)

:nodoc:



180
181
182
183
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 180

def delete_sql(sql, name = nil) #:nodoc:
  sql += " WHERE 1=1" unless sql =~ /WHERE/i
  super sql, name
end

- (Object) disconnect!



127
128
129
130
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 127

def disconnect!
  super
  @connection.close rescue nil
end

- (Object) empty_insert_statement(table_name)



314
315
316
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 314

def empty_insert_statement(table_name)
  "INSERT INTO #{table_name} VALUES(NULL)"
end

- (Object) execute(sql, name = nil)

DATABASE STATEMENTS ======================================



171
172
173
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 171

def execute(sql, name = nil) #:nodoc:
  catch_schema_changes { log(sql, name) { @connection.execute(sql) } }
end

- (Object) indexes(table_name, name = nil)

:nodoc:



233
234
235
236
237
238
239
240
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 233

def indexes(table_name, name = nil) #:nodoc:
  execute("PRAGMA index_list(#{quote_table_name(table_name)})", name).map do |row|
    index = IndexDefinition.new(table_name, row['name'])
    index.unique = row['unique'].to_i != 0
    index.columns = execute("PRAGMA index_info('#{index.name}')").map { |col| col['name'] }
    index
  end
end

- (Object) insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)

:nodoc:



185
186
187
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 185

def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
  super || @connection.last_insert_row_id
end

- (Object) native_database_types

:nodoc:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 140

def native_database_types #:nodoc:
  {
    :primary_key => default_primary_key_type,
    :string      => { :name => "varchar", :limit => 255 },
    :text        => { :name => "text" },
    :integer     => { :name => "integer" },
    :float       => { :name => "float" },
    :decimal     => { :name => "decimal" },
    :datetime    => { :name => "datetime" },
    :timestamp   => { :name => "datetime" },
    :time        => { :name => "time" },
    :date        => { :name => "date" },
    :binary      => { :name => "blob" },
    :boolean     => { :name => "boolean" }
  }
end

- (Object) primary_key(table_name)

:nodoc:



242
243
244
245
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 242

def primary_key(table_name) #:nodoc:
  column = table_structure(table_name).find {|field| field['pk'].to_i == 1}
  column ? column['name'] : nil
end

- (Object) quote_column_name(name)

:nodoc:



164
165
166
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 164

def quote_column_name(name) #:nodoc:
  %Q("#{name}")
end

- (Object) quote_string(s)

QUOTING ==================================================



160
161
162
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 160

def quote_string(s) #:nodoc:
  @connection.class.quote(s)
end

- (Object) remove_column(table_name, *column_names) Also known as: remove_columns

:nodoc:



271
272
273
274
275
276
277
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 271

def remove_column(table_name, *column_names) #:nodoc:
  column_names.flatten.each do |column_name|
    alter_table(table_name) do |definition|
      definition.columns.delete(definition[column_name])
    end
  end
end

- (Object) remove_index!(table_name, index_name)

:nodoc:



247
248
249
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 247

def remove_index!(table_name, index_name) #:nodoc:
  execute "DROP INDEX #{quote_column_name(index_name)}"
end

- (Object) rename_column(table_name, column_name, new_column_name)

:nodoc:



307
308
309
310
311
312
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 307

def rename_column(table_name, column_name, new_column_name) #:nodoc:
  unless columns(table_name).detect{|c| c.name == column_name.to_s }
    raise ActiveRecord::ActiveRecordError, "Missing column #{table_name}.#{column_name}"
  end
  alter_table(table_name, :rename => {column_name.to_s => new_column_name.to_s})
end

- (Object) rename_table(name, new_name)



251
252
253
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 251

def rename_table(name, new_name)
  execute "ALTER TABLE #{name} RENAME TO #{new_name}"
end

- (Boolean) requires_reloading?

Returns:

  • (Boolean)


119
120
121
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 119

def requires_reloading?
  true
end

- (Object) rollback_db_transaction

:nodoc:



203
204
205
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 203

def rollback_db_transaction #:nodoc:
  catch_schema_changes { @connection.rollback }
end

- (Object) select_rows(sql, name = nil)



189
190
191
192
193
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 189

def select_rows(sql, name = nil)
  execute(sql, name).map do |row|
    (0...(row.size / 2)).map { |i| row[i] }
  end
end

- (Boolean) supports_add_column?

Returns:

  • (Boolean)


123
124
125
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 123

def supports_add_column?
  sqlite_version >= '3.1.6'
end

- (Boolean) supports_autoincrement?

:nodoc:

Returns:

  • (Boolean)


136
137
138
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 136

def supports_autoincrement? #:nodoc:
  sqlite_version >= '3.1.0'
end

- (Boolean) supports_count_distinct?

:nodoc:

Returns:

  • (Boolean)


132
133
134
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 132

def supports_count_distinct? #:nodoc:
  sqlite_version >= '3.2.6'
end

- (Boolean) supports_ddl_transactions?

Returns:

  • (Boolean)


107
108
109
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 107

def supports_ddl_transactions?
  sqlite_version >= '2.0.0'
end

- (Boolean) supports_migrations?

:nodoc:

Returns:

  • (Boolean)


111
112
113
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 111

def supports_migrations? #:nodoc:
  true
end

- (Boolean) supports_primary_key?

:nodoc:

Returns:

  • (Boolean)


115
116
117
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 115

def supports_primary_key? #:nodoc:
  true
end

- (Object) tables(name = nil)

SCHEMA STATEMENTS ========================================



215
216
217
218
219
220
221
222
223
224
225
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 215

def tables(name = nil) #:nodoc:
  sql = <<-SQL
    SELECT name
    FROM sqlite_master
    WHERE type = 'table' AND NOT name = 'sqlite_sequence'
  SQL

  execute(sql, name).map do |row|
    row['name']
  end
end

- (Object) update_sql(sql, name = nil)

:nodoc:



175
176
177
178
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 175

def update_sql(sql, name = nil) #:nodoc:
  super
  @connection.changes
end

- (Object) valid_alter_table_options(type, options)

See: www.sqlite.org/lang_altertable.html SQLite has an additional restriction on the ALTER TABLE statement



257
258
259
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 257

def valid_alter_table_options( type, options)
  type.to_sym != :primary_key
end