Class: ActiveRecord::ConnectionAdapters::SQLiteAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- ActiveRecord::ConnectionAdapters::SQLiteAdapter
- Defined in:
- 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
Defined Under Namespace
Classes: Version
Instance Attribute Summary
Attributes included from QueryCache
#query_cache, #query_cache_enabled
Instance Method Summary collapse
-
#adapter_name ⇒ Object
:nodoc:.
-
#add_column(table_name, column_name, type, options = {}) ⇒ Object
:nodoc:.
-
#add_lock!(sql, options) ⇒ Object
SELECT …
-
#begin_db_transaction ⇒ Object
:nodoc:.
-
#change_column(table_name, column_name, type, options = {}) ⇒ Object
:nodoc:.
-
#change_column_default(table_name, column_name, default) ⇒ Object
:nodoc:.
- #change_column_null(table_name, column_name, null, default = nil) ⇒ Object
-
#columns(table_name, name = nil) ⇒ Object
:nodoc:.
-
#commit_db_transaction ⇒ Object
:nodoc:.
-
#delete_sql(sql, name = nil) ⇒ Object
:nodoc:.
- #disconnect! ⇒ Object
- #empty_insert_statement(table_name) ⇒ Object
-
#execute(sql, name = nil) ⇒ Object
DATABASE STATEMENTS ======================================.
-
#indexes(table_name, name = nil) ⇒ Object
:nodoc:.
-
#initialize(connection, logger, config) ⇒ SQLiteAdapter
constructor
A new instance of SQLiteAdapter.
-
#insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) ⇒ Object
:nodoc:.
-
#native_database_types ⇒ Object
:nodoc:.
-
#primary_key(table_name) ⇒ Object
:nodoc:.
-
#quote_column_name(name) ⇒ Object
:nodoc:.
-
#quote_string(s) ⇒ Object
QUOTING ==================================================.
-
#remove_column(table_name, *column_names) ⇒ Object
(also: #remove_columns)
:nodoc:.
-
#remove_index(table_name, options = {}) ⇒ Object
:nodoc:.
-
#rename_column(table_name, column_name, new_column_name) ⇒ Object
:nodoc:.
- #rename_table(name, new_name) ⇒ Object
- #requires_reloading? ⇒ Boolean
-
#rollback_db_transaction ⇒ Object
:nodoc:.
- #select_rows(sql, name = nil) ⇒ Object
- #supports_add_column? ⇒ Boolean
-
#supports_autoincrement? ⇒ Boolean
:nodoc:.
-
#supports_count_distinct? ⇒ Boolean
:nodoc:.
- #supports_ddl_transactions? ⇒ Boolean
-
#supports_migrations? ⇒ Boolean
:nodoc:.
-
#tables(name = nil) ⇒ Object
SCHEMA STATEMENTS ========================================.
-
#update_sql(sql, name = nil) ⇒ Object
:nodoc:.
-
#valid_alter_table_options(type, options) ⇒ Object
See: www.sqlite.org/lang_altertable.html SQLite has an additional restriction on the ALTER TABLE statement.
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 QueryCache
#cache, #clear_query_cache, #columns_with_query_cache, dirties_query_cache, included, #select_all_with_query_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, #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_name, #initialize_schema_migrations_table, #remove_timestamps, #structure_dump, #table_alias_for, #table_alias_length, #table_exists?, #type_to_sql
Constructor Details
#initialize(connection, logger, config) ⇒ SQLiteAdapter
Returns a new instance of SQLiteAdapter.
87 88 89 90 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 87 def initialize(connection, logger, config) super(connection, logger) @config = config end |
Instance Method Details
#adapter_name ⇒ Object
:nodoc:
92 93 94 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 92 def adapter_name #:nodoc: 'SQLite' end |
#add_column(table_name, column_name, type, options = {}) ⇒ Object
:nodoc:
246 247 248 249 250 251 252 253 254 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 246 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 |
#add_lock!(sql, options) ⇒ Object
SELECT … FOR UPDATE is redundant since the table is locked.
193 194 195 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 193 def add_lock!(sql, ) #:nodoc: sql end |
#begin_db_transaction ⇒ Object
:nodoc:
180 181 182 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 180 def begin_db_transaction #:nodoc: catch_schema_changes { @connection.transaction } end |
#change_column(table_name, column_name, type, options = {}) ⇒ Object
:nodoc:
280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 280 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 |
#change_column_default(table_name, column_name, default) ⇒ Object
:nodoc:
265 266 267 268 269 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 265 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
271 272 273 274 275 276 277 278 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 271 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:
212 213 214 215 216 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 212 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_transaction ⇒ Object
:nodoc:
184 185 186 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 184 def commit_db_transaction #:nodoc: catch_schema_changes { @connection.commit } end |
#delete_sql(sql, name = nil) ⇒ Object
:nodoc:
165 166 167 168 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 165 def delete_sql(sql, name = nil) #:nodoc: sql += " WHERE 1=1" unless sql =~ /WHERE/i super sql, name end |
#disconnect! ⇒ Object
112 113 114 115 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 112 def disconnect! super @connection.close rescue nil end |
#empty_insert_statement(table_name) ⇒ Object
299 300 301 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 299 def empty_insert_statement(table_name) "INSERT INTO #{table_name} VALUES(NULL)" end |
#execute(sql, name = nil) ⇒ Object
DATABASE STATEMENTS ======================================
156 157 158 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 156 def execute(sql, name = nil) #:nodoc: catch_schema_changes { log(sql, name) { @connection.execute(sql) } } end |
#indexes(table_name, name = nil) ⇒ Object
:nodoc:
218 219 220 221 222 223 224 225 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 218 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:
170 171 172 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 170 def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc: super || @connection.last_insert_row_id end |
#native_database_types ⇒ Object
:nodoc:
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 125 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:
227 228 229 230 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 227 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:
149 150 151 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 149 def quote_column_name(name) #:nodoc: %Q("#{name}") end |
#quote_string(s) ⇒ Object
QUOTING ==================================================
145 146 147 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 145 def quote_string(s) #:nodoc: @connection.class.quote(s) end |
#remove_column(table_name, *column_names) ⇒ Object Also known as: remove_columns
:nodoc:
256 257 258 259 260 261 262 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 256 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:
232 233 234 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 232 def remove_index(table_name, ={}) #:nodoc: execute "DROP INDEX #{quote_column_name(index_name(table_name, ))}" end |
#rename_column(table_name, column_name, new_column_name) ⇒ Object
:nodoc:
292 293 294 295 296 297 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 292 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
236 237 238 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 236 def rename_table(name, new_name) execute "ALTER TABLE #{name} RENAME TO #{new_name}" end |
#requires_reloading? ⇒ Boolean
104 105 106 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 104 def requires_reloading? true end |
#rollback_db_transaction ⇒ Object
:nodoc:
188 189 190 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 188 def rollback_db_transaction #:nodoc: catch_schema_changes { @connection.rollback } end |
#select_rows(sql, name = nil) ⇒ Object
174 175 176 177 178 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 174 def select_rows(sql, name = nil) execute(sql, name).map do |row| (0...(row.size / 2)).map { |i| row[i] } end end |
#supports_add_column? ⇒ Boolean
108 109 110 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 108 def supports_add_column? sqlite_version >= '3.1.6' end |
#supports_autoincrement? ⇒ Boolean
:nodoc:
121 122 123 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 121 def supports_autoincrement? #:nodoc: sqlite_version >= '3.1.0' end |
#supports_count_distinct? ⇒ Boolean
:nodoc:
117 118 119 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 117 def supports_count_distinct? #:nodoc: sqlite_version >= '3.2.6' end |
#supports_ddl_transactions? ⇒ Boolean
96 97 98 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 96 def supports_ddl_transactions? sqlite_version >= '2.0.0' end |
#supports_migrations? ⇒ Boolean
:nodoc:
100 101 102 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 100 def supports_migrations? #:nodoc: true end |
#tables(name = nil) ⇒ Object
SCHEMA STATEMENTS ========================================
200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 200 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:
160 161 162 163 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 160 def update_sql(sql, name = nil) #:nodoc: super @connection.changes end |
#valid_alter_table_options(type, options) ⇒ Object
See: www.sqlite.org/lang_altertable.html SQLite has an additional restriction on the ALTER TABLE statement
242 243 244 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 242 def ( type, ) type.to_sym != :primary_key end |