Class: MiniSql::Mysql::Connection

Inherits:
Connection show all
Defined in:
lib/mini_sql/mysql/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/mysql/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/mysql/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/mysql/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/mysql/connection.rb', line 6

def raw_connection
  @raw_connection
end

Instance Method Details

#escape_string(str) ⇒ Object



50
51
52
# File 'lib/mini_sql/mysql/connection.rb', line 50

def escape_string(str)
  raw_connection.escape(str)
end

#exec(sql, *params) ⇒ Object



35
36
37
38
# File 'lib/mini_sql/mysql/connection.rb', line 35

def exec(sql, *params)
  run(sql, :array, params)
  raw_connection.affected_rows
end

#prepared(condition = true) ⇒ Object



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

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

#query(sql, *params) ⇒ Object



40
41
42
43
# File 'lib/mini_sql/mysql/connection.rb', line 40

def query(sql, *params)
  result = run(sql, :array, params)
  deserializer_cache.materialize(result)
end

#query_array(sql, *params) ⇒ Object



31
32
33
# File 'lib/mini_sql/mysql/connection.rb', line 31

def query_array(sql, *params)
  run(sql, :array, params).to_a
end

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



45
46
47
48
# File 'lib/mini_sql/mysql/connection.rb', line 45

def query_decorator(decorator, sql, *params)
  result = run(sql, :array, params)
  deserializer_cache.materialize(result, decorator)
end

#query_hash(sql, *params) ⇒ Object



26
27
28
29
# File 'lib/mini_sql/mysql/connection.rb', line 26

def query_hash(sql, *params)
  result = run(sql, :hash, params)
  result.to_a
end

#query_single(sql, *params) ⇒ Object



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

def query_single(sql, *params)
  run(sql, :array, params).to_a.flatten!
end