Class: FluidDb2::SQLite

Inherits:
Base
  • Object
show all
Defined in:
lib/fluiddb2/sqlite.rb

Overview

SQLite

Instance Attribute Summary

Attributes inherited from Base

#connection, #verbose

Instance Method Summary collapse

Methods inherited from Base

#initialize, #reconnect, #verbose_log

Constructor Details

This class inherits a constructor from FluidDb2::Base

Instance Method Details

#beginObject

Transaction Semantics



93
94
95
# File 'lib/fluiddb2/sqlite.rb', line 93

def begin
  @connection.transaction
end

#closeObject



16
17
18
# File 'lib/fluiddb2/sqlite.rb', line 16

def close
  @connection.close
end

#commitObject

Transaction Semantics



98
99
100
# File 'lib/fluiddb2/sqlite.rb', line 98

def commit
  @connection.commit
end

#connectObject

Connect to Db.

Parameters:

  • uri (String)

    a location for the resource to which we will attach, eg mysql://user:[email protected]/foo



11
12
13
14
# File 'lib/fluiddb2/sqlite.rb', line 11

def connect
  uri = @uri
  @connection = SQLite3::Database.new uri.path
end

#exec_params(sql, params = [], expected_affected_rows = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fluiddb2/sqlite.rb', line 78

def exec_params(sql, params = [], expected_affected_rows = nil)
  parts = sql.split('?')
  sql = ''
  parts.each_with_index do |p, idx|
    sql += p
    sql += "$#{idx + 1}" if idx < parts.length - 1
  end
  r = @connection.exec_params(sql, params)

  if !expected_affected_rows.nil? && r.changes != expected_affected_rows
    fail ExpectedAffectedRowsError, "Expected affected rows, #{expected_affected_rows}, Actual affected rows, #{r.cmd_tuples}"
  end
end

#execute(sql, params = [], expected_affected_rows = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/fluiddb2/sqlite.rb', line 67

def execute(sql, params = [], expected_affected_rows = nil)
  sql = FluidDb2.format_to_sql(sql, params)

  verbose_log "#{self.class.name}.execute. #{sql}"
  r = @connection.execute(sql)

  if !expected_affected_rows.nil? && @connection.changes != expected_affected_rows
    fail ExpectedAffectedRowsError, "Expected affected rows, #{expected_affected_rows}, Actual affected rows, #{@connection.changes}"
  end
end

#insert(_sql, _params) ⇒ Object



107
108
109
# File 'lib/fluiddb2/sqlite.rb', line 107

def insert(_sql, _params)
  fail 'SQLite uses SEQUENCES, so possibly easier to use 2 executes'
end

#query_for_array(sql, params = []) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fluiddb2/sqlite.rb', line 20

def query_for_array(sql, params = [])
  sql = FluidDb2.format_to_sql(sql, params)
  @connection.results_as_hash = true
  results = @connection.execute(sql)

  case results.length
  when -1
    fail FluidDb2::ConnectionError
  when 0
    fail FluidDb2::NoDataFoundError
  when 1
    return results[0]
  else
    fail FluidDb2::TooManyRowsError
  end
end

#query_for_resultset(sql, params = []) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fluiddb2/sqlite.rb', line 54

def query_for_resultset(sql, params = [])
  sql = FluidDb2.format_to_sql(sql, params)
  @connection.results_as_hash = true
  results = @connection.execute(sql)

  case results.length
  when -1
    fail FluidDb2::ConnectionError
  else
    return results
  end
end

#query_for_value(sql, params = []) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fluiddb2/sqlite.rb', line 37

def query_for_value(sql, params = [])
  sql = FluidDb2.format_to_sql(sql, params)
  @connection.results_as_hash = false
  results = @connection.execute(sql)

  case results.length
  when -1
    fail FluidDb2::ConnectionError
  when 0
    fail FluidDb2::NoDataFoundError
  when 1
    return results[0][0]
  else
    fail FluidDb2::TooManyRowsError
  end
end

#rollbackObject

Transaction Semantics



103
104
105
# File 'lib/fluiddb2/sqlite.rb', line 103

def rollback
  @connection.rollback
end