Class: MiniSql::Sqlite::Connection

Inherits:
Connection show all
Defined in:
lib/mini_sql/sqlite/connection.rb

Direct Known Subclasses

PreparedConnection

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Connection

#build, get, #query_each, #query_each_hash, #to_sql

Constructor Details

#initialize(raw_connection, args = nil) ⇒ Connection

Returns a new instance of Connection.



8
9
10
11
12
# File 'lib/mini_sql/sqlite/connection.rb', line 8

def initialize(raw_connection, args = nil)
  @raw_connection = raw_connection
  @param_encoder = (args && args[:param_encoder]) || InlineParamEncoder.new(self)
  @deserializer_cache = (args && args[:deserializer_cache]) || DeserializerCache.new
end

Instance Attribute Details

#deserializer_cacheObject (readonly)

Returns the value of attribute deserializer_cache.



6
7
8
# File 'lib/mini_sql/sqlite/connection.rb', line 6

def deserializer_cache
  @deserializer_cache
end

#param_encoderObject (readonly)

Returns the value of attribute param_encoder.



6
7
8
# File 'lib/mini_sql/sqlite/connection.rb', line 6

def param_encoder
  @param_encoder
end

#raw_connectionObject (readonly)

Returns the value of attribute raw_connection.



6
7
8
# File 'lib/mini_sql/sqlite/connection.rb', line 6

def raw_connection
  @raw_connection
end

Instance Method Details

#escape_string(str) ⇒ Object



67
68
69
# File 'lib/mini_sql/sqlite/connection.rb', line 67

def escape_string(str)
  str.gsub("'", "''")
end

#exec(sql, *params) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mini_sql/sqlite/connection.rb', line 41

def exec(sql, *params)

  start = raw_connection.total_changes

  r = run(sql, params)
  # this is not safe for multithreading, also for DELETE from TABLE will return
  # incorrect data
  if r.length > 0
    r.length
  else
    raw_connection.total_changes - start
  end
end

#prepared(condition = true) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/mini_sql/sqlite/connection.rb', line 14

def prepared(condition = true)
  if condition
    @prepared ||= PreparedConnection.new(self)
  else
    self
  end
end

#query(sql, *params) ⇒ Object



55
56
57
58
59
# File 'lib/mini_sql/sqlite/connection.rb', line 55

def query(sql, *params)
  run(sql, params) do |set|
    deserializer_cache.materialize(set)
  end
end

#query_array(sql, *params) ⇒ Object



37
38
39
# File 'lib/mini_sql/sqlite/connection.rb', line 37

def query_array(sql, *params)
  run(sql, params)
end

#query_decorator(decorator, sql, *params) ⇒ Object



61
62
63
64
65
# File 'lib/mini_sql/sqlite/connection.rb', line 61

def query_decorator(decorator, sql, *params)
  run(sql, params) do |set|
    deserializer_cache.materialize(set, decorator)
  end
end

#query_hash(sql, *params) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/mini_sql/sqlite/connection.rb', line 27

def query_hash(sql, *params)
  r = []
  run(sql, params) do |set|
    set.each_hash do |h|
      r << h
    end
  end
  r
end

#query_single(sql, *params) ⇒ Object



22
23
24
25
# File 'lib/mini_sql/sqlite/connection.rb', line 22

def query_single(sql, *params)
  # a bit lazy can be optimized
  run(sql, params).flatten!
end