class Baza::Driver::Sqlite3

This class handels SQLite3-specific behaviour.

Attributes

mutex_statement_reader[R]

Public Class Methods

args() click to toggle source
# File lib/baza/driver/sqlite3.rb, line 7
def self.args
  [{
    label: "Path",
    name: "path"
  }]
end
from_object(args) click to toggle source

Helper to enable automatic registering of database using Baza::Db.from_object

# File lib/baza/driver/sqlite3.rb, line 15
def self.from_object(args)
  if args[:object].class.name == "SQLite3::Database"
    return {
      type: :success,
      args: {
        type: :sqlite3,
        conn: args[:object]
      }
    }
  end
end
new(db) click to toggle source

Constructor. This should not be called manually.

Calls superclass method Baza::BaseSqlDriver::new
# File lib/baza/driver/sqlite3.rb, line 28
def initialize(db)
  super

  @path = @db.opts[:path] if @db.opts[:path]
  @mutex_statement_reader = Mutex.new

  if @db.opts[:conn]
    @conn = @db.opts[:conn]
  else
    raise "No path was given." unless @path
    require "sqlite3" unless ::Object.const_defined?(:SQLite3)

    @conn = ::SQLite3::Database.open(@path)
    @conn.type_translation = false # Type translation is always done in the C ext for SQLite3
  end
end

Public Instance Methods

close() click to toggle source

Closes the connection to the database.

# File lib/baza/driver/sqlite3.rb, line 76
def close
  @mutex_statement_reader.synchronize { @conn.close }
end
enable_foreign_key_support() click to toggle source
# File lib/baza/driver/sqlite3.rb, line 45
def enable_foreign_key_support
  return true if @foreign_key_support_enabled

  @db.query("PRAGMA foreign_keys = ON")
  @foreign_key_support_enabled = true
  true
end
escape(string) click to toggle source

Escapes a string to be safe to used in a query.

# File lib/baza/driver/sqlite3.rb, line 69
def escape(string)
  # This code is taken directly from the documentation so we dont have to rely on the SQLite3::Database class. This way it can also be used with JRuby and IronRuby...
  # http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html
  string.to_s.gsub(/'/, "''")
end
foreign_key_support?() click to toggle source
# File lib/baza/driver/sqlite3.rb, line 53
def foreign_key_support?
  false
end
query(sql) click to toggle source

Executes a query against the driver.

# File lib/baza/driver/sqlite3.rb, line 58
def query(sql)
  @mutex_statement_reader.synchronize do
    return Baza::Driver::Sqlite3::Result.new(self, @conn.prepare(sql))
  end
end
query_ubuf(sql) click to toggle source
# File lib/baza/driver/sqlite3.rb, line 64
def query_ubuf(sql)
  Baza::Driver::Sqlite3::UnbufferedResult.new(self, @conn.prepare(sql))
end
supports_multiple_databases?() click to toggle source
# File lib/baza/driver/sqlite3.rb, line 85
def supports_multiple_databases?
  false
end
transaction() { |db| ... } click to toggle source

Starts a transaction, yields the database and commits.

# File lib/baza/driver/sqlite3.rb, line 81
def transaction
  @conn.transaction { yield @db }
end