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:

  • :dbfile – 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:



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

def adapter_name #:nodoc:
  'SQLite'
end

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

:nodoc:



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

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:



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

def begin_db_transaction #:nodoc:
  @connection.transaction
end

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

:nodoc:



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

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:



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

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:



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

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:



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

def commit_db_transaction #:nodoc:
  @connection.commit
end

#delete(sql, name = nil) ⇒ Object

:nodoc:



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

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



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

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

#indexes(table_name, name = nil) ⇒ Object

:nodoc:



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

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:



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

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:



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

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:



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

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:



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

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

#quote_string(s) ⇒ Object

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



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

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

#remove_column(table_name, column_name) ⇒ Object

:nodoc:



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

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:



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

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:



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

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



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

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

#rollback_db_transactionObject

:nodoc:



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

def rollback_db_transaction #:nodoc:
  @connection.rollback
end

#select_all(sql, name = nil) ⇒ Object

:nodoc:



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

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:



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

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

#supports_migrations?Boolean

:nodoc:

Returns:

  • (Boolean)


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

def supports_migrations? #:nodoc:
  true
end

#tables(name = nil) ⇒ Object

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



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

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:



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

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