Class: Baza::Driver::Sqlite3Rhodes

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

Overview

This class handels SQLite3-specific behaviour.

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) ⇒ Sqlite3Rhodes

Constructor. This should not be called manually.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/baza/drivers/sqlite3_rhodes.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
    @conn = ::SQLite3::Database.new(@path, @path)
  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_rhodes.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_rhodes.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.



68
69
70
71
72
# File 'lib/baza/drivers/sqlite3_rhodes.rb', line 68

def close
  @mutex_statement_reader.synchronize do
    @conn.close
  end
end

#escape(string) ⇒ Object

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



55
56
57
58
59
# File 'lib/baza/drivers/sqlite3_rhodes.rb', line 55

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.



62
63
64
65
# File 'lib/baza/drivers/sqlite3_rhodes.rb', line 62

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.



46
47
48
# File 'lib/baza/drivers/sqlite3_rhodes.rb', line 46

def query(sql)
  return Baza::Driver::Sqlite3::Result.new(self, @conn.execute(sql, sql))
end

#query_ubuf(sql) ⇒ Object



50
51
52
# File 'lib/baza/drivers/sqlite3_rhodes.rb', line 50

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

#transactionObject

Starts a transaction, yields the database and commits.



75
76
77
# File 'lib/baza/drivers/sqlite3_rhodes.rb', line 75

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