Class: MiniSql::Postgres::Connection

Inherits:
Connection show all
Defined in:
lib/mini_sql/postgres/connection.rb,
lib/mini_sql/postgres_jdbc/connection.rb

Defined Under Namespace

Classes: IPAddrCoder, NumericCoder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Connection

get

Constructor Details

#initialize(raw_connection, args = nil) ⇒ Connection

Initialize a new MiniSql::Postgres::Connection object

Parameters:

  • raw_connection (PG::Connection)

    an active connection to PG

  • deserializer_cache (MiniSql::DeserializerCache)

    a cache of field names to deserializer, can be nil

  • type_map (PG::TypeMap)

    a type mapper for all results returned, can be nil



38
39
40
41
42
# File 'lib/mini_sql/postgres/connection.rb', line 38

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

Instance Attribute Details

#param_encoderObject (readonly)

Returns the value of attribute param_encoder.



6
7
8
# File 'lib/mini_sql/postgres/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/postgres/connection.rb', line 6

def raw_connection
  @raw_connection
end

#type_mapObject (readonly)

Returns the value of attribute type_map.



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

def type_map
  @type_map
end

Class Method Details

.default_deserializer_cacheObject



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

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

.type_map(conn) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mini_sql/postgres/connection.rb', line 12

def self.type_map(conn)
  @type_map ||=
    begin
      map = PG::BasicTypeMapForResults.new(conn)
      map.add_coder(MiniSql::Postgres::Coders::NumericCoder.new(name: "numeric", oid: 1700, format: 0))
      map.add_coder(MiniSql::Postgres::Coders::IPAddrCoder.new(name: "inet", oid: 869, format: 0))
      map.add_coder(MiniSql::Postgres::Coders::IPAddrCoder.new(name: "cidr", oid: 650, format: 0))
      map.add_coder(PG::TextDecoder::String.new(name: "tsvector", oid: 3614, format: 0))

      map.rm_coder(0, 1114)
      if defined? PG::TextDecoder::TimestampUtc
        # treat timestamp without zone as utc
        # new to PG 1.1
        map.add_coder(PG::TextDecoder::TimestampUtc.new(name: "timestamp", oid: 1114, format: 0))
      else
        map.add_coder(MiniSql::Postgres::Coders::TimestampUtc.new(name: "timestamp", oid: 1114, format: 0))
      end
      map
    end
end

.typemapObject



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

def self.typemap
  @type_map ||= {
      "numeric" => NumericCoder.new,
      "inet" => IPAddrCoder.new,
      "cidr" => IPAddrCoder.new
  }
end

Instance Method Details

#build(sql) ⇒ Object



104
105
106
# File 'lib/mini_sql/postgres/connection.rb', line 104

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

#escape_string(str) ⇒ Object



108
109
110
# File 'lib/mini_sql/postgres/connection.rb', line 108

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

#exec(sql, *params) ⇒ Object



89
90
91
92
93
94
# File 'lib/mini_sql/postgres/connection.rb', line 89

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

#query(sql, *params) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/mini_sql/postgres/connection.rb', line 81

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

#query_hash(sql, *params) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/mini_sql/postgres/connection.rb', line 96

def query_hash(sql, *params)
  result = run(sql, params)
  result.type_map = type_map
  result.to_a
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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mini_sql/postgres/connection.rb', line 54

def query_single(sql, *params)
  result = run(sql, params)
  result.type_map = type_map
  if result.nfields == 1
    result.column_values(0)
  else
    tuples = result.ntuples
    fields = result.nfields

    array = []
    f = 0
    row = 0

    while row < tuples
      while f < fields
        array << result.getvalue(row, f)
        f += 1
      end
      f = 0
      row += 1
    end
    array
  end
ensure
  result.clear if result
end