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

DeprecatedSQLiteAdapter

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:



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

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:



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

def begin_db_transaction #:nodoc:
  @connection.transaction
end

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

:nodoc:



243
244
245
246
247
248
249
250
251
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 243

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:



237
238
239
240
241
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 237

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:



191
192
193
194
195
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 191

def columns(table_name, name = nil) #:nodoc:
  table_structure(table_name).map { |field|
    SQLiteColumn.new(field['name'], field['dflt_value'], field['type'], field['notnull'] == "0")
  }
end

#commit_db_transactionObject

:nodoc:



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

def commit_db_transaction #:nodoc:
  @connection.commit
end

#delete(sql, name = nil) ⇒ Object

:nodoc:



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

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



132
133
134
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 132

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

#indexes(table_name, name = nil) ⇒ Object

:nodoc:



197
198
199
200
201
202
203
204
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 197

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:



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

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:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 102

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:



206
207
208
209
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 206

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:



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

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

#quote_string(s) ⇒ Object

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



121
122
123
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 121

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

#remove_column(table_name, column_name) ⇒ Object

:nodoc:



231
232
233
234
235
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 231

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:



211
212
213
214
215
216
217
218
219
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 211

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:



253
254
255
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 253

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



221
222
223
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 221

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

#rollback_db_transactionObject

:nodoc:



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

def rollback_db_transaction #:nodoc:
  @connection.rollback
end

#select_all(sql, name = nil) ⇒ Object

:nodoc:



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 152

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:



164
165
166
167
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 164

def select_one(sql, name = nil) #:nodoc:
  result = select_all(sql, name)
  result.nil? ? nil : result.first
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 ========================================



185
186
187
188
189
# File 'lib/active_record/connection_adapters/sqlite_adapter.rb', line 185

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:



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

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