Class: ActiveRecord::ConnectionAdapters::SQLiteAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- ActiveRecord::ConnectionAdapters::SQLiteAdapter
- 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.
Defined Under Namespace
Classes: StatementPool, Version
Instance Attribute Summary
Attributes inherited from AbstractAdapter
Class Method Summary (collapse)
-
+ (Object) visitor_for(pool)
:nodoc:.
Instance Method Summary (collapse)
-
- (Object) adapter_name
:nodoc:.
-
- (Object) add_column(table_name, column_name, type, options = {})
:nodoc:.
-
- (Object) begin_db_transaction
:nodoc:.
-
- (Object) change_column(table_name, column_name, type, options = {})
:nodoc:.
-
- (Object) change_column_default(table_name, column_name, default)
:nodoc:.
- - (Object) change_column_null(table_name, column_name, null, default = nil)
-
- (Object) clear_cache!
Clears the prepared statements cache.
-
- (Object) columns(table_name, name = nil)
Returns an array of SQLiteColumn objects for the table specified by table_name.
-
- (Object) commit_db_transaction
:nodoc:.
- - (Object) create_savepoint
-
- (Object) delete_sql(sql, name = nil)
:nodoc:.
-
- (Object) disconnect!
Disconnects from the database if already connected.
- - (Object) empty_insert_statement_value
- - (Object) exec_delete(sql, name = 'SQL', binds = []) (also: #exec_update)
-
- (Object) exec_query(sql, name = nil, binds = [])
DATABASE STATEMENTS ======================================.
-
- (Object) execute(sql, name = nil)
:nodoc:.
-
- (Object) indexes(table_name, name = nil)
Returns an array of indexes for the given table.
-
- (SQLiteAdapter) initialize(connection, logger, config)
constructor
A new instance of SQLiteAdapter.
-
- (Object) insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
(also: #create)
:nodoc:.
- - (Object) last_inserted_id(result)
-
- (Object) native_database_types
:nodoc:.
-
- (Object) primary_key(table_name)
:nodoc:.
-
- (Object) quote_column_name(name)
:nodoc:.
-
- (Object) quote_string(s)
QUOTING ==================================================.
-
- (Object) quoted_date(value)
Quote date/time values for use in SQL input.
- - (Object) release_savepoint
-
- (Object) remove_column(table_name, *column_names)
(also: #remove_columns)
:nodoc:.
-
- (Object) remove_index!(table_name, index_name)
:nodoc:.
-
- (Object) rename_column(table_name, column_name, new_column_name)
:nodoc:.
-
- (Object) rename_table(name, new_name)
Renames a table.
- - (Boolean) requires_reloading?
-
- (Object) rollback_db_transaction
:nodoc:.
- - (Object) rollback_to_savepoint
- - (Object) select_rows(sql, name = nil)
-
- (Boolean) supports_add_column?
Returns true if SQLite version is '3.1.6' or greater, false otherwise.
-
- (Boolean) supports_autoincrement?
Returns true if SQLite version is '3.1.0' or greater, false otherwise.
-
- (Boolean) supports_count_distinct?
Returns true if SQLite version is '3.2.6' or greater, false otherwise.
-
- (Boolean) supports_ddl_transactions?
Returns true if SQLite version is '2.0.0' or greater, false otherwise.
-
- (Boolean) supports_migrations?
Returns true, since this connection adapter supports migrations.
-
- (Boolean) supports_primary_key?
Returns true.
-
- (Boolean) supports_savepoints?
Returns true if SQLite version is '3.6.8' or greater, false otherwise.
-
- (Boolean) supports_statement_cache?
Returns true, since this connection adapter supports prepared statement caching.
-
- (Object) tables(name = 'SCHEMA')
SCHEMA STATEMENTS ========================================.
-
- (Object) type_cast(value, column)
:nodoc:.
-
- (Object) update_sql(sql, name = nil)
:nodoc:.
-
- (Object) valid_alter_table_options(type, options)
See: www.sqlite.org/lang_altertable.html SQLite has an additional restriction on the ALTER TABLE statement.
Methods inherited from AbstractAdapter
#active?, #case_sensitive_modifier, #current_savepoint_name, #decrement_open_transactions, #disable_referential_integrity, #increment_open_transactions, #open_transactions, #prefetch_primary_key?, #quote_table_name, #raw_connection, #reconnect!, #reset!, #substitute_at, #supports_bulk_alter?, #transaction_joinable=, #verify!
Methods included from ActiveSupport::Callbacks
Methods included from ActiveSupport::Concern
#append_features, extended, #included
Methods inherited from QueryCache
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 SchemaStatements
#add_column_options!, #add_index, #add_timestamps, #assume_migrated_upto_version, #change_table, #column_exists?, #create_table, #distinct, #drop_table, #dump_schema_information, #index_exists?, #index_name, #index_name_exists?, #initialize_schema_migrations_table, #remove_index, #remove_timestamps, #rename_index, #structure_dump, #table_alias_for, #table_exists?, #type_to_sql
Methods included from DatabaseStatements
#add_limit_offset!, #add_transaction_record, #case_sensitive_equality_operator, #default_sequence_name, #delete, #exec_insert, #insert, #insert_fixture, #join_to_update, #limited_update_conditions, #outside_transaction?, #reset_sequence!, #sanitize_limit, #select_all, #select_one, #select_value, #select_values, #to_sql, #transaction, #update
Methods included from Quoting
#quote, #quote_table_name, #quoted_false, #quoted_true
Constructor Details
- (SQLiteAdapter) initialize(connection, logger, config)
A new instance of SQLiteAdapter
88 89 90 91 92 93 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 88 def initialize(connection, logger, config) super(connection, logger) @statements = StatementPool.new(@connection, config.fetch(:statement_limit) { 1000 }) @config = config end |
Class Method Details
+ (Object) visitor_for(pool)
:nodoc:
95 96 97 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 95 def self.visitor_for(pool) # :nodoc: Arel::Visitors::SQLite.new(pool) end |
Instance Method Details
- (Object) adapter_name
:nodoc:
99 100 101 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 99 def adapter_name #:nodoc: 'SQLite' end |
- (Object) add_column(table_name, column_name, type, options = {})
:nodoc:
374 375 376 377 378 379 380 381 382 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 374 def add_column(table_name, column_name, type, = {}) #:nodoc: if supports_add_column? && ( type, ) super(table_name, column_name, type, ) else alter_table(table_name) do |definition| definition.column(column_name, type, ) end end end |
- (Object) begin_db_transaction
:nodoc:
294 295 296 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 294 def begin_db_transaction #:nodoc: @connection.transaction end |
- (Object) change_column(table_name, column_name, type, options = {})
:nodoc:
409 410 411 412 413 414 415 416 417 418 419 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 409 def change_column(table_name, column_name, type, = {}) #:nodoc: alter_table(table_name) do |definition| include_default = () definition[column_name].instance_eval do self.type = type self.limit = [:limit] if .include?(:limit) self.default = [:default] if include_default self.null = [:null] if .include?(:null) end end end |
- (Object) change_column_default(table_name, column_name, default)
:nodoc:
394 395 396 397 398 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 394 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)
400 401 402 403 404 405 406 407 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 400 def change_column_null(table_name, column_name, null, default = nil) unless null || default.nil? exec_query("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) clear_cache!
Clears the prepared statements cache.
147 148 149 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 147 def clear_cache! @statements.clear end |
- (Object) columns(table_name, name = nil)
Returns an array of SQLiteColumn objects for the table specified by table_name.
321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 321 def columns(table_name, name = nil) #:nodoc: table_structure(table_name).map do |field| case field["dflt_value"] when /^null$/i field["dflt_value"] = nil when /^'(.*)'$/ field["dflt_value"] = $1.gsub(/''/, "'") when /^"(.*)"$/ field["dflt_value"] = $1.gsub(/""/, '"') end SQLiteColumn.new(field['name'], field['dflt_value'], field['type'], field['notnull'].to_i == 0) end end |
- (Object) commit_db_transaction
:nodoc:
298 299 300 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 298 def commit_db_transaction #:nodoc: @connection.commit end |
- (Object) create_savepoint
282 283 284 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 282 def create_savepoint execute("SAVEPOINT #{current_savepoint_name}") end |
- (Object) delete_sql(sql, name = nil)
:nodoc:
267 268 269 270 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 267 def delete_sql(sql, name = nil) #:nodoc: sql += " WHERE 1=1" unless sql =~ /WHERE/i super sql, name end |
- (Object) disconnect!
Disconnects from the database if already connected. Otherwise, this method does nothing.
140 141 142 143 144 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 140 def disconnect! super clear_cache! @connection.close rescue nil end |
- (Object) empty_insert_statement_value
428 429 430 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 428 def empty_insert_statement_value "VALUES(NULL)" end |
- (Object) exec_delete(sql, name = 'SQL', binds = []) Also known as: exec_update
248 249 250 251 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 248 def exec_delete(sql, name = 'SQL', binds = []) exec_query(sql, name, binds) @connection.changes end |
- (Object) exec_query(sql, name = nil, binds = [])
DATABASE STATEMENTS ======================================
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 222 def exec_query(sql, name = nil, binds = []) log(sql, name, binds) do # Don't cache statements without bind values if binds.empty? stmt = @connection.prepare(sql) cols = stmt.columns records = stmt.to_a stmt.close stmt = records else cache = @statements[sql] ||= { :stmt => @connection.prepare(sql) } stmt = cache[:stmt] cols = cache[:cols] ||= stmt.columns stmt.reset! stmt.bind_params binds.map { |col, val| type_cast(val, col) } end ActiveRecord::Result.new(cols, stmt.to_a) end end |
- (Object) execute(sql, name = nil)
:nodoc:
258 259 260 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 258 def execute(sql, name = nil) #:nodoc: log(sql, name) { @connection.execute(sql) } end |
- (Object) indexes(table_name, name = nil)
Returns an array of indexes for the given table.
337 338 339 340 341 342 343 344 345 346 347 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 337 def indexes(table_name, name = nil) #:nodoc: exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", name).map do |row| IndexDefinition.new( table_name, row['name'], row['unique'] != 0, exec_query("PRAGMA index_info('#{row['name']}')").map { |col| col['name'] }) end end |
- (Object) insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) Also known as: create
:nodoc:
272 273 274 275 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 272 def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc: super id_value || @connection.last_insert_row_id end |
- (Object) last_inserted_id(result)
254 255 256 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 254 def last_inserted_id(result) @connection.last_insert_row_id end |
- (Object) native_database_types
:nodoc:
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 161 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:
349 350 351 352 353 354 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 349 def primary_key(table_name) #:nodoc: column = table_structure(table_name).find { |field| field['pk'] == 1 } column && column['name'] end |
- (Object) quote_column_name(name)
:nodoc:
185 186 187 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 185 def quote_column_name(name) #:nodoc: %Q("#{name.to_s.gsub('"', '""')}") end |
- (Object) quote_string(s)
QUOTING ==================================================
181 182 183 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 181 def quote_string(s) #:nodoc: @connection.class.quote(s) end |
- (Object) quoted_date(value)
Quote date/time values for use in SQL input. Includes microseconds if the value is a Time responding to usec.
191 192 193 194 195 196 197 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 191 def quoted_date(value) #:nodoc: if value.respond_to?(:usec) "#{super}.#{sprintf("%06d", value.usec)}" else super end end |
- (Object) release_savepoint
290 291 292 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 290 def release_savepoint execute("RELEASE SAVEPOINT #{current_savepoint_name}") end |
- (Object) remove_column(table_name, *column_names) Also known as: remove_columns
:nodoc:
384 385 386 387 388 389 390 391 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 384 def remove_column(table_name, *column_names) #:nodoc: raise ArgumentError.new("You must specify at least one column name. Example: remove_column(:people, :first_name)") if column_names.empty? 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:
356 357 358 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 356 def remove_index!(table_name, index_name) #:nodoc: exec_query "DROP INDEX #{quote_column_name(index_name)}" end |
- (Object) rename_column(table_name, column_name, new_column_name)
:nodoc:
421 422 423 424 425 426 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 421 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)
Renames a table.
Example:
rename_table('octopuses', 'octopi')
364 365 366 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 364 def rename_table(name, new_name) exec_query "ALTER TABLE #{quote_table_name(name)} RENAME TO #{quote_table_name(new_name)}" end |
- (Boolean) requires_reloading?
129 130 131 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 129 def requires_reloading? true end |
- (Object) rollback_db_transaction
:nodoc:
302 303 304 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 302 def rollback_db_transaction #:nodoc: @connection.rollback end |
- (Object) rollback_to_savepoint
286 287 288 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 286 def rollback_to_savepoint execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}") end |
- (Object) select_rows(sql, name = nil)
278 279 280 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 278 def select_rows(sql, name = nil) exec_query(sql, name).rows end |
- (Boolean) supports_add_column?
Returns true if SQLite version is '3.1.6' or greater, false otherwise.
134 135 136 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 134 def supports_add_column? sqlite_version >= '3.1.6' end |
- (Boolean) supports_autoincrement?
Returns true if SQLite version is '3.1.0' or greater, false otherwise.
157 158 159 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 157 def supports_autoincrement? #:nodoc: sqlite_version >= '3.1.0' end |
- (Boolean) supports_count_distinct?
Returns true if SQLite version is '3.2.6' or greater, false otherwise.
152 153 154 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 152 def supports_count_distinct? #:nodoc: sqlite_version >= '3.2.6' end |
- (Boolean) supports_ddl_transactions?
Returns true if SQLite version is '2.0.0' or greater, false otherwise.
104 105 106 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 104 def supports_ddl_transactions? sqlite_version >= '2.0.0' end |
- (Boolean) supports_migrations?
Returns true, since this connection adapter supports migrations.
120 121 122 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 120 def supports_migrations? #:nodoc: true end |
- (Boolean) supports_primary_key?
Returns true.
125 126 127 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 125 def supports_primary_key? #:nodoc: true end |
- (Boolean) supports_savepoints?
Returns true if SQLite version is '3.6.8' or greater, false otherwise.
109 110 111 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 109 def supports_savepoints? sqlite_version >= '3.6.8' end |
- (Boolean) supports_statement_cache?
Returns true, since this connection adapter supports prepared statement caching.
115 116 117 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 115 def supports_statement_cache? true end |
- (Object) tables(name = 'SCHEMA')
SCHEMA STATEMENTS ========================================
308 309 310 311 312 313 314 315 316 317 318 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 308 def tables(name = 'SCHEMA') #:nodoc: sql = <<-SQL SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence' SQL exec_query(sql, name).map do |row| row['name'] end end |
- (Object) type_cast(value, column)
:nodoc:
213 214 215 216 217 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 213 def type_cast(value, column) # :nodoc: return super unless BigDecimal === value value.to_f end |
- (Object) update_sql(sql, name = nil)
:nodoc:
262 263 264 265 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 262 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
370 371 372 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb', line 370 def ( type, ) type.to_sym != :primary_key end |