Files

Class/Module Index [+]

Quicksearch

ArJdbc::SQLite3

Public Class Methods

arel2_visitors(config) click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 89
def self.arel2_visitors(config)
  {
    'sqlite3' => ::Arel::Visitors::SQLite,
    'jdbcsqlite3' => ::Arel::Visitors::SQLite
  }
end
column_selector() click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 7
def self.column_selector
  [ /sqlite/, lambda { |cfg,col| col.extend(::ArJdbc::SQLite3::Column) } ]
end
jdbc_connection_class() click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 11
def self.jdbc_connection_class
  ::ActiveRecord::ConnectionAdapters::SQLite3JdbcConnection
end

Public Instance Methods

change_column_null(table_name, column_name, null, default = nil) click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 324
def change_column_null(table_name, column_name, null, default = nil)
  unless null || default.nil?
    execute("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
create_savepoint() click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 227
def create_savepoint
  execute("SAVEPOINT #{current_savepoint_name}")
end
default_primary_key_type() click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 129
def default_primary_key_type
  if supports_autoincrement?
    'integer PRIMARY KEY AUTOINCREMENT NOT NULL'
  else
    'integer PRIMARY KEY NOT NULL'
  end
end
empty_insert_statement_value() click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 359
def empty_insert_statement_value
  "VALUES(NULL)"
end
indexes(table_name, name = nil) click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 213
def indexes(table_name, name = nil)
  result = select_rows("SELECT name, sql FROM sqlite_master" <<
  " WHERE tbl_name = #{quote_table_name(table_name)} AND type = 'index'", name)

  result.map do |row|
    name, index_sql = row[0], row[1]
    unique = !! (index_sql =~ /unique/)
    columns = index_sql.match(/\((.*)\)/)[1].gsub(/,/,' ').split.map do |col|
      match = /^"(.+)"$/.match(col); match ? match[1] : col
    end
    IndexDefinition.new(table_name, name, unique, columns)
  end
end
modify_types(types) click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 123
def modify_types(types)
  super(types)
  types.merge! NATIVE_DATABASE_TYPES
  types
end
native_database_types() click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 117
def native_database_types
  types = super.merge(NATIVE_DATABASE_TYPES)
  types[:primary_key] = default_primary_key_type
  types
end
quote(value, column = nil) click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 166
def quote(value, column = nil)
  if value.kind_of?(String)
    column_type = column && column.type
    if column_type == :binary && column.class.respond_to?(:string_to_binary)
      "x'#{column.class.string_to_binary(value).unpack("H*")[0]}'"
    else
      super
    end
  else
    super
  end
end
recreate_database(name, options = {}) click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 239
def recreate_database(name, options = {})
  tables.each { |table| drop_table(table) }
end
release_savepoint() click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 235
def release_savepoint
  execute("RELEASE SAVEPOINT #{current_savepoint_name}")
end
rename_table(name, new_name) click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 283
def rename_table(name, new_name)
  execute "ALTER TABLE #{quote_table_name(name)} RENAME TO #{quote_table_name(new_name)}"
end
rollback_to_savepoint() click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 231
def rollback_to_savepoint
  execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
end
select(sql, name = nil, binds = []) click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 243
def select(sql, name = nil, binds = [])
  execute(sql, name, binds).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
table_exists?(table_name) click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 207
def table_exists?(table_name)
  table_name && tables(nil, table_name).any?
end
table_structure(table_name) click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 255
def table_structure(table_name)
  sql = "PRAGMA table_info(#{quote_table_name(table_name)})"
  log(sql, 'SCHEMA') { @connection.execute_query(sql) }
rescue ActiveRecord::JDBCError => error
  e = ActiveRecord::StatementInvalid.new("Could not find table '#{table_name}'")
  e.set_backtrace error.backtrace
  raise e
end
valid_alter_table_options( type, options) click to toggle source

See: www.sqlite.org/lang_altertable.html SQLite has an additional restriction on the ALTER TABLE statement

# File lib/arjdbc/sqlite3/adapter.rb, line 289
def valid_alter_table_options( type, options)
  type.to_sym != :primary_key
end

Protected Instance Methods

last_insert_id() click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 376
def last_insert_id
  @connection.last_insert_row_id
end
last_inserted_id(result) click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 380
def last_inserted_id(result)
  last_insert_id
end
translate_exception(exception, message) click to toggle source
# File lib/arjdbc/sqlite3/adapter.rb, line 367
def translate_exception(exception, message)
  case exception.message
  when /column(s)? .* (is|are) not unique/
    ActiveRecord::RecordNotUnique.new(message, exception)
  else
    super
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.