Class: MiniSql::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_sql/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_connection, deserializer_cache: nil, type_map: nil, param_encoder: nil) ⇒ Connection

Initialize a new MiniSql::Connection object

Parameters:

  • raw_connection (PG::Connection)

    an active connection to PG

  • deserializer_cache (MiniSql::DeserializerCache) (defaults to: nil)

    a cache of field names to deserializer, can be nil

  • type_map (PG::TypeMap) (defaults to: nil)

    a type mapper for all results returned, can be nil



25
26
27
28
29
30
31
# File 'lib/mini_sql/connection.rb', line 25

def initialize(raw_connection, deserializer_cache: nil, type_map: nil, param_encoder: nil)
  # TODO adapter to support other databases
  @raw_connection = raw_connection
  @deserializer_cache = deserializer_cache || Connection.default_deserializer_cache
  @type_map = type_map || Connection.type_map(raw_connection)
  @param_encoder = param_encoder || InlineParamEncoder.new(self)
end

Instance Attribute Details

#raw_connectionObject (readonly)

Returns the value of attribute raw_connection.



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

def raw_connection
  @raw_connection
end

Class Method Details

.default_deserializer_cacheObject



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

def self.default_deserializer_cache
  @deserializer_cache ||= DeserializerCache.new
end

.type_map(conn) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/mini_sql/connection.rb', line 11

def self.type_map(conn)
  @type_map ||=
    begin
      map = PG::BasicTypeMapForResults.new(conn)
      map.add_coder(MiniSql::Coders::NumericCoder.new(name: "numeric", oid: 1700, format: 0))
      map.add_coder(MiniSql::Coders::IPAddrCoder.new(name: "inet", oid: 869, format: 0))
    end
end

Instance Method Details

#build(sql) ⇒ Object



77
78
79
# File 'lib/mini_sql/connection.rb', line 77

def build(sql)
  Builder.new(self, sql)
end

#escape_string(str) ⇒ Object



81
82
83
# File 'lib/mini_sql/connection.rb', line 81

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

#exec(sql, *params) ⇒ Object



70
71
72
73
74
75
# File 'lib/mini_sql/connection.rb', line 70

def exec(sql, *params)
  result = run(sql, params)
  result.cmd_tuples
ensure
  result.clear if result
end

#query(sql, *params) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/mini_sql/connection.rb', line 62

def query(sql, *params)
  result = run(sql, params)
  result.type_map = @type_map
  @deserializer_cache.materialize(result)
ensure
  result.clear if result
end

#query_single(sql, *params) ⇒ Object

Returns a flat array containing all results. Note, if selecting multiple columns array will be flattened

Parameters:

  • sql (String)

    the query to run

  • params (Array or Hash)

    , params to apply to query

Returns:

  • (Object)

    a flat array containing all results



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mini_sql/connection.rb', line 39

def query_single(sql, *params)
  result = run(sql, params)
  result.type_map = @type_map
  if result.nfields == 1
    result.column_values(0)
  else
    array = []
    f = 0
    row = 0
    while row < result.ntuples
      while f < result.nfields
        array << result.getvalue(row, f)
        f += 1
      end
      f = 0
      row += 1
    end
    array
  end
ensure
  result.clear if result
end