Class: Baza::Driver::Sqlite3

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

Overview

This class handels SQLite3-specific behaviour.

Defined Under Namespace

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

Constant Summary

Constants inherited from BaseSqlDriver

BaseSqlDriver::SELECT_ARGS_ALLOWED_KEYS

Instance Attribute Summary collapse

Attributes inherited from BaseSqlDriver

#cols, #conn, #db, #indexes, #sep_col, #sep_database, #sep_index, #sep_table, #sep_val, #tables

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseSqlDriver

#count, #delete, #escape_column, #escape_database, #escape_index, #escape_table, #insert, #insert_multi, #select, #single, #sql_make_where, #sqlval

Constructor Details

#initialize(db) ⇒ Sqlite3

Constructor. This should not be called manually.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/baza/driver/sqlite3.rb', line 28

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

Instance Attribute Details

#mutex_statement_readerObject (readonly)

Returns the value of attribute mutex_statement_reader.



5
6
7
# File 'lib/baza/driver/sqlite3.rb', line 5

def mutex_statement_reader
  @mutex_statement_reader
end

Class Method Details

.argsObject



7
8
9
10
11
12
# File 'lib/baza/driver/sqlite3.rb', line 7

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

.from_object(args) ⇒ Object

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



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/baza/driver/sqlite3.rb', line 15

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.



64
65
66
# File 'lib/baza/driver/sqlite3.rb', line 64

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

#escape(string) ⇒ Object

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



57
58
59
60
61
# File 'lib/baza/driver/sqlite3.rb', line 57

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

#query(sql) ⇒ Object

Executes a query against the driver.



46
47
48
49
50
# File 'lib/baza/driver/sqlite3.rb', line 46

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

#query_ubuf(sql) ⇒ Object



52
53
54
# File 'lib/baza/driver/sqlite3.rb', line 52

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

#supports_multiple_databases?Boolean

Returns:

  • (Boolean)


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

def supports_multiple_databases?
  false
end

#transactionObject

Starts a transaction, yields the database and commits.



69
70
71
# File 'lib/baza/driver/sqlite3.rb', line 69

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