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:
Methods
- C
-
- D
-
- E
-
- L
-
- N
-
- R
-
- S
-
- T
-
- V
-
Classes and Modules
Class Public methods
new(connection, logger, config)
Source: show
| on GitHub
def initialize(connection, logger, config)
super(connection, logger)
@statements = {}
@config = config
end
Instance Public methods
change_column_null(table_name, column_name, null, default = nil)
Source: show
| on GitHub
def change_column_null(table_name, column_name, null, default = nil)
unless null || default.nil?
exec_query("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
Clears the prepared statements cache.
Source: show
| on GitHub
def clear_cache!
@statements.values.each { |hash| hash[:stmt].close }
@statements.clear
end
Source: show
| on GitHub
def create_savepoint
execute("SAVEPOINT #{current_savepoint_name}")
end
Disconnects from the database if already connected. Otherwise, this method
does nothing.
Source: show
| on GitHub
def disconnect!
super
clear_cache!
@connection.close rescue nil
end
empty_insert_statement_value()
Source: show
| on GitHub
def empty_insert_statement_value
"VALUES(NULL)"
end
exec_delete(sql, name = 'SQL', binds = [])
Source: show
| on GitHub
def exec_delete(sql, name = 'SQL', binds = [])
exec_query(sql, name, binds)
@connection.changes
end
exec_query(sql, name = nil, binds = [])
DATABASE STATEMENTS ======================================
Source: show
| on GitHub
def exec_query(sql, name = nil, binds = [])
log(sql, name, binds) do
if binds.empty?
stmt = @connection.prepare(sql)
cols = stmt.columns
records = stmt.to_a
stmt.close
stmt = records
else
cache = @statements[sql] ||= {
:stmt => @connection.prepare(sql)
}
stmt = cache[:stmt]
cols = cache[:cols] ||= stmt.columns
stmt.reset!
stmt.bind_params binds.map { |col, val|
type_cast(val, col)
}
end
ActiveRecord::Result.new(cols, stmt.to_a)
end
end
exec_update(sql, name = 'SQL', binds = [])
Source: show
| on GitHub
def last_inserted_id(result)
@connection.last_insert_row_id
end
Source: show
| on GitHub
def release_savepoint
execute("RELEASE SAVEPOINT #{current_savepoint_name}")
end
rename_table(name, new_name)
Renames a table.
Example:
rename_table('octopuses', 'octopi')
Source: show
| on GitHub
def rename_table(name, new_name)
exec_query "ALTER TABLE #{quote_table_name(name)} RENAME TO #{quote_table_name(new_name)}"
end
Source: show
| on GitHub
def rollback_to_savepoint
execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
end
select_rows(sql, name = nil)
Source: show
| on GitHub
def select_rows(sql, name = nil)
exec_query(sql, name).rows
end
Returns true if SQLite version is ‘3.1.6’ or greater, false otherwise.
Source: show
| on GitHub
def supports_add_column?
sqlite_version >= '3.1.6'
end
supports_ddl_transactions?()
Returns true if SQLite version is ‘2.0.0’ or greater, false otherwise.
Source: show
| on GitHub
def supports_ddl_transactions?
sqlite_version >= '2.0.0'
end
Returns true if SQLite version is ‘3.6.8’ or greater, false otherwise.
Source: show
| on GitHub
def supports_savepoints?
sqlite_version >= '3.6.8'
end
supports_statement_cache?()
Returns true, since this connection adapter supports prepared statement
caching.
valid_alter_table_options( type, options)
Source: show
| on GitHub
def valid_alter_table_options( type, options)
type.to_sym != :primary_key
end
Instance Protected methods
default_primary_key_type()
Source: show
| on GitHub
def default_primary_key_type
if supports_autoincrement?
'INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL'
else
'INTEGER PRIMARY KEY NOT NULL'
end
end
Source: show
| on GitHub
def sqlite_version
@sqlite_version ||= SQLiteAdapter::Version.new(select_value('select sqlite_version(*)'))
end
table_structure(table_name)
Source: show
| on GitHub
def table_structure(table_name)
structure = exec_query("PRAGMA table_info(#{quote_table_name(table_name)})", 'SCHEMA').to_hash
raise(ActiveRecord::StatementInvalid, "Could not find table '#{table_name}'") if structure.empty?
structure
end
translate_exception(exception, message)
Source: show
| on GitHub
def translate_exception(exception, message)
case exception.message
when /column(s)? .* (is|are) not unique/
RecordNotUnique.new(message, exception)
else
super
end
end