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, BaseSqlDriver::SEPARATOR_COLUMN, BaseSqlDriver::SEPARATOR_DATABASE, BaseSqlDriver::SEPARATOR_INDEX, BaseSqlDriver::SEPARATOR_TABLE, BaseSqlDriver::SEPARATOR_VALUE

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, escape_column, #escape_column, escape_database, #escape_database, escape_index, #escape_index, escape_table, #escape_table, #insert, #insert_multi, #select, #single, #sql_make_where, sqlval, #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.



76
77
78
# File 'lib/baza/driver/sqlite3.rb', line 76

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

#enable_foreign_key_supportObject



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

def enable_foreign_key_support
  return true if @foreign_key_support_enabled

  @db.query("PRAGMA foreign_keys = ON")
  @foreign_key_support_enabled = true
  true
end

#escape(string) ⇒ Object

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



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

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

#foreign_key_support?Boolean

Returns:

  • (Boolean)


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

def foreign_key_support?
  false
end

#query(sql) ⇒ Object

Executes a query against the driver.



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

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

#query_ubuf(sql) ⇒ Object



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

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

#supports_multiple_databases?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/baza/driver/sqlite3.rb', line 85

def supports_multiple_databases?
  false
end

#transactionObject

Starts a transaction, yields the database and commits.



81
82
83
# File 'lib/baza/driver/sqlite3.rb', line 81

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