Class: Baza::Driver::Sqlite3Rhodes

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

Overview

This class handels SQLite3-specific behaviour.

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, #foreign_key_support?, #insert, #insert_multi, #select, #single, #sql_make_where, #sqlval, sqlval, #supports_multiple_databases?

Constructor Details

#initialize(db) ⇒ Sqlite3Rhodes

Constructor. This should not be called manually.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/baza/driver/sqlite3_rhodes.rb', line 21

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
    @conn = ::SQLite3::Database.new(@path, @path)
  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_rhodes.rb', line 5

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



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/baza/driver/sqlite3_rhodes.rb', line 8

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.



52
53
54
55
56
# File 'lib/baza/driver/sqlite3_rhodes.rb', line 52

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.



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

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.



36
37
38
# File 'lib/baza/driver/sqlite3_rhodes.rb', line 36

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

#query_ubuf(sql) ⇒ Object



40
41
42
# File 'lib/baza/driver/sqlite3_rhodes.rb', line 40

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

#transactionObject

Starts a transaction, yields the database and commits.



59
60
61
# File 'lib/baza/driver/sqlite3_rhodes.rb', line 59

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