Class: MiniSql::Connection
- Inherits:
-
Object
- Object
- MiniSql::Connection
- Defined in:
- lib/mini_sql/connection.rb
Instance Attribute Summary collapse
-
#raw_connection ⇒ Object
readonly
Returns the value of attribute raw_connection.
Class Method Summary collapse
Instance Method Summary collapse
- #build(sql) ⇒ Object
- #escape_string(str) ⇒ Object
- #exec(sql, *params) ⇒ Object
-
#initialize(raw_connection, deserializer_cache: nil, type_map: nil, param_encoder: nil) ⇒ Connection
constructor
Initialize a new MiniSql::Connection object.
- #query(sql, *params) ⇒ Object
- #query_hash(sql, *params) ⇒ Object
-
#query_single(sql, *params) ⇒ Object
Returns a flat array containing all results.
Constructor Details
#initialize(raw_connection, deserializer_cache: nil, type_map: nil, param_encoder: nil) ⇒ Connection
Initialize a new MiniSql::Connection object
27 28 29 30 31 32 33 |
# File 'lib/mini_sql/connection.rb', line 27 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_connection ⇒ Object (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_cache ⇒ Object
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 19 20 |
# 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)) map.add_coder(MiniSql::Coders::IPAddrCoder.new(name: "cidr", oid: 650, format: 0)) map.add_coder(PG::TextDecoder::String.new(name: "tsvector", oid: 3614, format: 0)) end end |
Instance Method Details
#build(sql) ⇒ Object
87 88 89 |
# File 'lib/mini_sql/connection.rb', line 87 def build(sql) Builder.new(self, sql) end |
#escape_string(str) ⇒ Object
91 92 93 |
# File 'lib/mini_sql/connection.rb', line 91 def escape_string(str) raw_connection.escape_string(str) end |
#exec(sql, *params) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/mini_sql/connection.rb', line 72 def exec(sql, *params) result = run(sql, params) result.cmd_tuples ensure result.clear if result end |
#query(sql, *params) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/mini_sql/connection.rb', line 64 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
79 80 81 82 83 84 85 |
# File 'lib/mini_sql/connection.rb', line 79 def query_hash(sql, *params) result = run(sql, params) result.type_map = @type_map result.to_a ensure result.clear end |
#query_single(sql, *params) ⇒ Object
Returns a flat array containing all results. Note, if selecting multiple columns array will be flattened
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/mini_sql/connection.rb', line 41 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 |