Class: Baza::Driver::Sqlite3

Inherits:
BaseSqlDriver show all
Defined in:
lib/baza/drivers/sqlite3.rb

Overview

This class handels SQLite3-specific behaviour.

Defined Under Namespace

Classes: Column, Columns, Index, Indexes, Result, Sqlspecs, Table, Tables, UnbufferedResult

Instance Attribute Summary collapse

Attributes inherited from BaseSqlDriver

#baza, #cols, #conn, #indexes, #sep_col, #sep_table, #sep_val, #tables

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseSqlDriver

#esc_col, #insert_multi

Constructor Details

#initialize(baza_db) ⇒ Sqlite3

Constructor. This should not be called manually.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/baza/drivers/sqlite3.rb', line 31

def initialize(baza_db)
  super

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

  if @baza.opts[:conn]
    @conn = @baza.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

Instance Attribute Details

#mutex_statement_readerObject (readonly)

Returns the value of attribute mutex_statement_reader.



15
16
17
# File 'lib/baza/drivers/sqlite3.rb', line 15

def mutex_statement_reader
  @mutex_statement_reader
end

Class Method Details

.from_object(args) ⇒ Object

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



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/baza/drivers/sqlite3.rb', line 18

def self.from_object(args)
  if args[:object].class.name == "SQLite3::Database"
    return {
      type: :success,
      args: {
        type: :sqlite3,
        conn: args[:object]
      }
    }
  end
end

Instance Method Details

#closeObject

Closes the connection to the database.



73
74
75
# File 'lib/baza/drivers/sqlite3.rb', line 73

def close
  @mutex_statement_reader.synchronize { @conn.close }
end

#escape(string) ⇒ Object

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



60
61
62
63
64
# File 'lib/baza/drivers/sqlite3.rb', line 60

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
  return string.to_s.gsub(/'/, "''")
end

#last_idObject

Returns the last inserted ID.



67
68
69
70
# File 'lib/baza/drivers/sqlite3.rb', line 67

def last_id
  return @conn.last_insert_row_id if @conn.respond_to?(:last_insert_row_id)
  return query("SELECT last_insert_rowid() AS id").fetch[:id].to_i
end

#query(sql) ⇒ Object

Executes a query against the driver.



49
50
51
52
53
# File 'lib/baza/drivers/sqlite3.rb', line 49

def query(sql)
  @mutex_statement_reader.synchronize do
    return Baza::Driver::Sqlite3::Result.new(self, @conn.prepare(sql))
  end
end

#query_ubuf(sql) ⇒ Object



55
56
57
# File 'lib/baza/drivers/sqlite3.rb', line 55

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

#transactionObject

Starts a transaction, yields the database and commits.



78
79
80
# File 'lib/baza/drivers/sqlite3.rb', line 78

def transaction
  @conn.transaction { yield @baza }
end