Class: Baza::Driver::Sqlite3Java

Inherits:
JdbcDriver show all
Defined in:
lib/baza/drivers/sqlite3_java.rb

Overview

This class handels SQLite3-specific behaviour.

Defined Under Namespace

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

Instance Attribute Summary collapse

Attributes inherited from JdbcDriver

#conn, #conns

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 JdbcDriver

#close, #query, #query_ubuf, #result_set_killer

Methods inherited from BaseSqlDriver

#esc_col, #insert_multi

Constructor Details

#initialize(baza_db) ⇒ Sqlite3Java

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_java.rb', line 31

def initialize(baza_db)
  super

  @path = @baza.opts[:path] if @baza.opts[:path]
  @preload_results = true

  if @baza.opts[:conn]
    @conn = @baza.opts[:conn]
  else
    org.sqlite.JDBC
    reconnect
  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_java.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_java.rb', line 18

def self.from_object(args)
  if args[:object].class.name == "Java::OrgSqlite::SQLiteConnection"
    return {
      type: :success,
      args: {
        type: :sqlite3_java,
        conn: args[:object]
      }
    }
  end
end

Instance Method Details

#escape(string) ⇒ Object

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



53
54
55
56
57
# File 'lib/baza/drivers/sqlite3_java.rb', line 53

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.



60
61
62
# File 'lib/baza/drivers/sqlite3_java.rb', line 60

def last_id
  return query("SELECT LAST_INSERT_ROWID() AS id").fetch[:id].to_i
end

#reconnectObject



45
46
47
48
49
50
# File 'lib/baza/drivers/sqlite3_java.rb', line 45

def reconnect
  raise "No path was given." unless @path

  @stmt = nil
  @conn = java.sql.DriverManager::getConnection("jdbc:sqlite:#{@path}")
end

#transactionObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/baza/drivers/sqlite3_java.rb', line 64

def transaction
  query_no_result_set("BEGIN TRANSACTION")

  begin
    yield @baza
    query_no_result_set("COMMIT")
  rescue
    query_no_result_set("ROLLBACK")
    raise
  end
end