Class: MiniSql::Connection
- Inherits:
-
Object
- Object
- MiniSql::Connection
- Defined in:
- lib/mini_sql/connection.rb
Instance Attribute Summary collapse
-
#param_encoder ⇒ Object
readonly
Returns the value of attribute param_encoder.
-
#raw_connection ⇒ Object
readonly
Returns the value of attribute raw_connection.
-
#type_map ⇒ Object
readonly
Returns the value of attribute type_map.
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, 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, param_encoder: nil) ⇒ Connection
Initialize a new MiniSql::Connection object
27 28 29 30 31 32 |
# File 'lib/mini_sql/connection.rb', line 27 def initialize(raw_connection, deserializer_cache: nil, param_encoder: nil) # TODO adapter to support other databases @raw_connection = raw_connection @deserializer_cache = deserializer_cache || Connection.default_deserializer_cache @param_encoder = param_encoder || InlineParamEncoder.new(self) end |
Instance Attribute Details
#param_encoder ⇒ Object (readonly)
Returns the value of attribute param_encoder.
5 6 7 |
# File 'lib/mini_sql/connection.rb', line 5 def param_encoder @param_encoder end |
#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 |
#type_map ⇒ Object (readonly)
Returns the value of attribute type_map.
5 6 7 |
# File 'lib/mini_sql/connection.rb', line 5 def type_map @type_map 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
90 91 92 |
# File 'lib/mini_sql/connection.rb', line 90 def build(sql) Builder.new(self, sql) end |
#escape_string(str) ⇒ Object
94 95 96 |
# File 'lib/mini_sql/connection.rb', line 94 def escape_string(str) raw_connection.escape_string(str) end |
#exec(sql, *params) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/mini_sql/connection.rb', line 75 def exec(sql, *params) result = run(sql, params) result.cmd_tuples ensure result.clear if result end |
#query(sql, *params) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/mini_sql/connection.rb', line 67 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
82 83 84 85 86 87 88 |
# File 'lib/mini_sql/connection.rb', line 82 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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/mini_sql/connection.rb', line 44 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 |