Class: ActiveRecord::ConnectionAdapters::SQLiteAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/gems/activerecord-2.2.2/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

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#active?, #decrement_open_transactions, #disable_referential_integrity, #increment_open_transactions, #initialize, #log_info, #open_transactions, #prefetch_primary_key?, #quote_table_name, #raw_connection, #reconnect!, #reset!, #reset_runtime, #supports_ddl_transactions?, #verify!

Methods included from QueryCache

#cache, #uncached

Methods included from Quoting

#quote, #quote_table_name, #quoted_date, #quoted_false, #quoted_string_prefix, #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, #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_name, #initialize_schema_migrations_table, #remove_timestamps, #structure_dump, #table_alias_for, #table_alias_length, #table_exists?, #type_to_sql

Constructor Details

This class inherits a constructor from ActiveRecord::ConnectionAdapters::AbstractAdapter

Instance Method Details

#adapter_nameObject

:nodoc:



75
76
77
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 75

def adapter_name #:nodoc:
  'SQLite'
end

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

:nodoc:



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

def add_column(table_name, column_name, type, options = {}) #:nodoc:
  if @connection.respond_to?(:transaction_active?) && @connection.transaction_active?
    raise StatementInvalid, 'Cannot add columns to a SQLite database while inside a transaction'
  end
  
  super(table_name, column_name, type, options)
  # See last paragraph on http://www.sqlite.org/lang_altertable.html
  execute "VACUUM"
end

#add_lock!(sql, options) ⇒ Object

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



169
170
171
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 169

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

#begin_db_transactionObject

:nodoc:



155
156
157
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 155

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

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

:nodoc:



250
251
252
253
254
255
256
257
258
259
260
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 250

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

#change_column_default(table_name, column_name, default) ⇒ Object

:nodoc:



235
236
237
238
239
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 235

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

#change_column_null(table_name, column_name, null, default = nil) ⇒ Object



241
242
243
244
245
246
247
248
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 241

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

#columns(table_name, name = nil) ⇒ Object

:nodoc:



188
189
190
191
192
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 188

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'] == "0")
  end
end

#commit_db_transactionObject

:nodoc:



159
160
161
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 159

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

#delete_sql(sql, name = nil) ⇒ Object

:nodoc:



140
141
142
143
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 140

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

#disconnect!Object



87
88
89
90
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 87

def disconnect!
  super
  @connection.close rescue nil
end

#empty_insert_statement(table_name) ⇒ Object



269
270
271
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 269

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

#execute(sql, name = nil) ⇒ Object

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



131
132
133
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 131

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

#indexes(table_name, name = nil) ⇒ Object

:nodoc:



194
195
196
197
198
199
200
201
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 194

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'] != '0'
    index.columns = execute("PRAGMA index_info('#{index.name}')").map { |col| col['name'] }
    index
  end
end

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

:nodoc:



145
146
147
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 145

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

#native_database_typesObject

:nodoc:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 100

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

#primary_key(table_name) ⇒ Object

:nodoc:



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

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

#quote_column_name(name) ⇒ Object

:nodoc:



124
125
126
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 124

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

#quote_string(s) ⇒ Object

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



120
121
122
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 120

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

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

:nodoc:



226
227
228
229
230
231
232
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 226

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

#remove_index(table_name, options = {}) ⇒ Object

:nodoc:



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

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

#rename_column(table_name, column_name, new_column_name) ⇒ Object

:nodoc:



262
263
264
265
266
267
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 262

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

#rename_table(name, new_name) ⇒ Object



212
213
214
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 212

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

#requires_reloading?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 83

def requires_reloading?
  true
end

#rollback_db_transactionObject

:nodoc:



163
164
165
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 163

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

#select_rows(sql, name = nil) ⇒ Object



149
150
151
152
153
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 149

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

#supports_autoincrement?Boolean

:nodoc:

Returns:

  • (Boolean)


96
97
98
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 96

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

#supports_count_distinct?Boolean

:nodoc:

Returns:

  • (Boolean)


92
93
94
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 92

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

#supports_migrations?Boolean

:nodoc:

Returns:

  • (Boolean)


79
80
81
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 79

def supports_migrations? #:nodoc:
  true
end

#tables(name = nil) ⇒ Object

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



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/gems/activerecord-2.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb', line 176

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[0]
  end
end

#update_sql(sql, name = nil) ⇒ Object

:nodoc:



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

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