Sha256: 1a9fc1fecf1911ee6bf09c475ce3d3af33bfd80a39160303eb3a9294d67427bc

Contents?: true

Size: 1.66 KB

Versions: 18

Compression:

Stored size: 1.66 KB

Contents

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

  attr_reader :mutex_statement_reader

  # 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
      @conn = ::SQLite3::Database.new(@path, @path)
    end
  end

  # Executes a query against the driver.
  def query(sql)
    Baza::Driver::Sqlite3::Result.new(self, @conn.execute(sql, sql))
  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 do
      @conn.close
    end
  end

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

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
baza-0.0.38 lib/baza/driver/sqlite3_rhodes.rb
baza-0.0.37 lib/baza/driver/sqlite3_rhodes.rb
baza-0.0.36 lib/baza/driver/sqlite3_rhodes.rb
baza-0.0.35 lib/baza/driver/sqlite3_rhodes.rb
baza-0.0.34 lib/baza/driver/sqlite3_rhodes.rb
baza-0.0.33 lib/baza/driver/sqlite3_rhodes.rb
baza-0.0.32 lib/baza/driver/sqlite3_rhodes.rb
baza-0.0.31 lib/baza/driver/sqlite3_rhodes.rb
baza-0.0.30 lib/baza/driver/sqlite3_rhodes.rb
baza-0.0.29 lib/baza/driver/sqlite3_rhodes.rb
baza-0.0.28 lib/baza/driver/sqlite3_rhodes.rb
baza-0.0.27 lib/baza/driver/sqlite3_rhodes.rb
baza-0.0.26 lib/baza/driver/sqlite3_rhodes.rb
baza-0.0.25 lib/baza/driver/sqlite3_rhodes.rb
baza-0.0.24 lib/baza/driver/sqlite3_rhodes.rb
baza-0.0.23 lib/baza/driver/sqlite3_rhodes.rb
baza-0.0.22 lib/baza/driver/sqlite3_rhodes.rb
baza-0.0.21 lib/baza/driver/sqlite3_rhodes.rb