Class: ActiveRecord::ConnectionAdapters::SQLiteAdapter

Inherits:
AbstractAdapter show all
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

SQLite2Adapter

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#clear_query_cache

Instance Method Details

#adapter_nameObject

:nodoc:



94
95
96
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 94

def adapter_name #:nodoc:
  'SQLite'
end

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

:nodoc:



229
230
231
232
233
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 229

def add_column(table_name, column_name, type, options = {}) #:nodoc:
  alter_table(table_name) do |definition|
    definition.column(column_name, type, options)
  end
end

#begin_db_transactionObject

:nodoc:



174
175
176
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 174

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

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

:nodoc:



247
248
249
250
251
252
253
254
255
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 247

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

#change_column_default(table_name, column_name, default) ⇒ Object

:nodoc:



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

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:



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

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:



178
179
180
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 178

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

#delete(sql, name = nil) ⇒ Object

:nodoc:



145
146
147
148
149
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 145

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 ======================================



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

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

#indexes(table_name, name = nil) ⇒ Object

:nodoc:



201
202
203
204
205
206
207
208
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 201

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:



151
152
153
154
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 151

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_typesObject

:nodoc:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 106

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" },
    :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:



210
211
212
213
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 210

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:



129
130
131
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 129

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

#quote_string(s) ⇒ Object

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



125
126
127
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 125

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

#remove_column(table_name, column_name) ⇒ Object

:nodoc:



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

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:



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

def remove_index(table_name, options={}) #:nodoc:
  if Hash === options
    index_name = options[:name]
  else
    index_name = "#{table_name}_#{options}_index"
  end

  execute "DROP INDEX #{index_name}"
end

#rename_column(table_name, column_name, new_column_name) ⇒ Object

:nodoc:



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

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



225
226
227
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 225

def rename_table(name, new_name)
  move_table(name, new_name)
end

#rollback_db_transactionObject

:nodoc:



182
183
184
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 182

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

#select_all(sql, name = nil) ⇒ Object

:nodoc:



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 156

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:



168
169
170
171
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 168

def select_one(sql, name = nil) #:nodoc:
  result = select_all(sql, name)
  result.nil? ? nil : result.first
end

#supports_count_distinct?Boolean

:nodoc:

Returns:

  • (Boolean)


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

def supports_count_distinct? #:nodoc:
  false
end

#supports_migrations?Boolean

:nodoc:

Returns:

  • (Boolean)


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

def supports_migrations? #:nodoc:
  true
end

#tables(name = nil) ⇒ Object

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



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

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:



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

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