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
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:.
-
#columns(table_name, name = nil) ⇒ Object
:nodoc:.
-
#commit_db_transaction ⇒ Object
:nodoc:.
-
#delete(sql, name = nil) ⇒ Object
:nodoc:.
-
#execute(sql, name = nil) ⇒ Object
DATABASE STATEMENTS ======================================.
-
#indexes(table_name, name = nil) ⇒ Object
:nodoc:.
-
#insert(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_name) ⇒ Object
: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_all(sql, name = nil) ⇒ Object
:nodoc:.
-
#select_one(sql, name = nil) ⇒ Object
:nodoc:.
-
#supports_count_distinct? ⇒ Boolean
:nodoc:.
-
#supports_migrations? ⇒ Boolean
:nodoc:.
-
#tables(name = nil) ⇒ Object
SCHEMA STATEMENTS ========================================.
-
#update(sql, name = nil) ⇒ Object
:nodoc:.
Methods inherited from AbstractAdapter
Instance Method Details
#adapter_name ⇒ Object
:nodoc:
97 98 99 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 97 def adapter_name #:nodoc: 'SQLite' end |
#add_column(table_name, column_name, type, options = {}) ⇒ Object
:nodoc:
237 238 239 240 241 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 237 def add_column(table_name, column_name, type, = {}) #:nodoc: super(table_name, column_name, type, ) # 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.
196 197 198 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 196 def add_lock!(sql, ) #:nodoc: sql end |
#begin_db_transaction ⇒ Object
:nodoc:
182 183 184 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 182 def begin_db_transaction #:nodoc: catch_schema_changes { @connection.transaction } end |
#change_column(table_name, column_name, type, options = {}) ⇒ Object
:nodoc:
255 256 257 258 259 260 261 262 263 264 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 255 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 end end end |
#change_column_default(table_name, column_name, default) ⇒ Object
:nodoc:
249 250 251 252 253 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 249 def change_column_default(table_name, column_name, default) #:nodoc: alter_table(table_name) do |definition| definition[column_name].default = default end end |
#columns(table_name, name = nil) ⇒ Object
:nodoc:
209 210 211 212 213 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 209 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:
186 187 188 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 186 def commit_db_transaction #:nodoc: catch_schema_changes { @connection.commit } end |
#delete(sql, name = nil) ⇒ Object
:nodoc:
153 154 155 156 157 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 153 def delete(sql, name = nil) #:nodoc: sql += " WHERE 1=1" unless sql =~ /WHERE/i execute(sql, name) @connection.changes end |
#execute(sql, name = nil) ⇒ Object
DATABASE STATEMENTS ======================================
144 145 146 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 144 def execute(sql, name = nil) #:nodoc: catch_schema_changes { log(sql, name) { @connection.execute(sql) } } end |
#indexes(table_name, name = nil) ⇒ Object
:nodoc:
215 216 217 218 219 220 221 222 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 215 def indexes(table_name, name = nil) #:nodoc: execute("PRAGMA index_list(#{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, name = nil, pk = nil, id_value = nil, sequence_name = nil) ⇒ Object
:nodoc:
159 160 161 162 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 159 def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc: execute(sql, name = nil) id_value || @connection.last_insert_row_id end |
#native_database_types ⇒ Object
:nodoc:
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 113 def native_database_types #:nodoc: { :primary_key => "INTEGER PRIMARY KEY NOT NULL", :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 => "datetime" }, :date => { :name => "date" }, :binary => { :name => "blob" }, :boolean => { :name => "boolean" } } end |
#primary_key(table_name) ⇒ Object
:nodoc:
224 225 226 227 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 224 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:
137 138 139 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 137 def quote_column_name(name) #:nodoc: %Q("#{name}") end |
#quote_string(s) ⇒ Object
QUOTING ==================================================
133 134 135 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 133 def quote_string(s) #:nodoc: @connection.class.quote(s) end |
#remove_column(table_name, column_name) ⇒ Object
:nodoc:
243 244 245 246 247 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 243 def remove_column(table_name, column_name) #:nodoc: alter_table(table_name) do |definition| definition.columns.delete(definition[column_name]) end end |
#remove_index(table_name, options = {}) ⇒ Object
:nodoc:
229 230 231 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 229 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:
266 267 268 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 266 def rename_column(table_name, column_name, new_column_name) #:nodoc: alter_table(table_name, :rename => {column_name => new_column_name}) end |
#rename_table(name, new_name) ⇒ Object
233 234 235 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 233 def rename_table(name, new_name) execute "ALTER TABLE #{name} RENAME TO #{new_name}" end |
#requires_reloading? ⇒ Boolean
105 106 107 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 105 def requires_reloading? true end |
#rollback_db_transaction ⇒ Object
:nodoc:
190 191 192 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 190 def rollback_db_transaction #:nodoc: catch_schema_changes { @connection.rollback } end |
#select_all(sql, name = nil) ⇒ Object
:nodoc:
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 164 def select_all(sql, name = nil) #:nodoc: execute(sql, name).map do |row| record = {} row.each_key do |key| if key.is_a?(String) record[key.sub(/^\w+\./, '')] = row[key] end end record end end |
#select_one(sql, name = nil) ⇒ Object
:nodoc:
176 177 178 179 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 176 def select_one(sql, name = nil) #:nodoc: result = select_all(sql, name) result.nil? ? nil : result.first end |
#supports_count_distinct? ⇒ Boolean
:nodoc:
109 110 111 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 109 def supports_count_distinct? #:nodoc: false end |
#supports_migrations? ⇒ Boolean
:nodoc:
101 102 103 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 101 def supports_migrations? #:nodoc: true end |
#tables(name = nil) ⇒ Object
SCHEMA STATEMENTS ========================================
203 204 205 206 207 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 203 def tables(name = nil) #:nodoc: execute("SELECT name FROM sqlite_master WHERE type = 'table'", name).map do |row| row[0] end end |
#update(sql, name = nil) ⇒ Object
:nodoc:
148 149 150 151 |
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 148 def update(sql, name = nil) #:nodoc: execute(sql, name) @connection.changes end |