Sha256: d69135cd3eab87906877c084d3bb4a56039880f727b1ec4b23988130b502cffd

Contents?: true

Size: 1.97 KB

Versions: 2

Compression:

Stored size: 1.97 KB

Contents

# This class handels SQLite3-specific behaviour.
class Baza::Driver::Sqlite3 < Baza::BaseSqlDriver
  AutoAutoloader.autoload_sub_classes(self, __FILE__)

  attr_reader :mutex_statement_reader

  def self.args
    [{
      label: "Path",
      name: "path"
    }]
  end

  # Helper to enable automatic registering of database using Baza::Db.from_object
  def self.from_object(args)
    if args[:object].class.name == "SQLite3::Database"
      return {
        type: :success,
        args: {
          type: :sqlite3,
          conn: args[:object]
        }
      }
    end
  end

  # Constructor. This should not be called manually.
  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

  # Executes a query against the driver.
  def query(sql)
    @mutex_statement_reader.synchronize do
      return Baza::Driver::Sqlite3::Result.new(self, @conn.prepare(sql))
    end
  end

  def query_ubuf(sql)
    Baza::Driver::Sqlite3::UnbufferedResult.new(self, @conn.prepare(sql))
  end

  # Escapes a string to be safe to used in a query.
  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

  # Closes the connection to the database.
  def close
    @mutex_statement_reader.synchronize { @conn.close }
  end

  # Starts a transaction, yields the database and commits.
  def transaction
    @conn.transaction { yield @db }
  end

  def supports_multiple_databases?
    false
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
baza-0.0.22 lib/baza/driver/sqlite3.rb
baza-0.0.21 lib/baza/driver/sqlite3.rb