class MiniSql::Postgres::Connection
Attributes
deserializer_cache[R]
param_encoder[R]
raw_connection[R]
Public Class Methods
default_deserializer_cache()
click to toggle source
# File lib/mini_sql/postgres/connection.rb, line 8 def self.default_deserializer_cache @deserializer_cache ||= DeserializerCache.new end
new(raw_connection, args = nil)
click to toggle source
Initialize a new MiniSql::Postgres::Connection
object
@param raw_connection
[PG::Connection] an active connection to PG @param deserializer_cache
[MiniSql::DeserializerCache] a cache of field names to deserializer, can be nil @param type_map
[PG::TypeMap] a type mapper for all results returned, can be nil
# 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) @type_map = args && args[:type_map] end
type_map(conn)
click to toggle source
# 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
typemap()
click to toggle source
# 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
Public Instance Methods
build(sql)
click to toggle source
# File lib/mini_sql/postgres/connection.rb, line 195 def build(sql) Builder.new(self, sql) end
escape_string(str)
click to toggle source
# File lib/mini_sql/postgres/connection.rb, line 199 def escape_string(str) raw_connection.escape_string(str) end
exec(sql, *params)
click to toggle source
# File lib/mini_sql/postgres/connection.rb, line 180 def exec(sql, *params) result = run(sql, params) result.cmd_tuples ensure result.clear if result end
prepared(condition = true)
click to toggle source
# File lib/mini_sql/postgres/connection.rb, line 49 def prepared(condition = true) if condition @prepared ||= PreparedConnection.new(self) else self end end
query(sql, *params)
click to toggle source
# File lib/mini_sql/postgres/connection.rb, line 98 def query(sql, *params) result = run(sql, params) result.type_map = type_map deserializer_cache.materialize(result) ensure result.clear if result end
query_array(sql, *params)
click to toggle source
# File lib/mini_sql/postgres/connection.rb, line 90 def query_array(sql, *params) result = run(sql, params) result.type_map = type_map result.values ensure result.clear if result end
query_decorator(decorator, sql, *params)
click to toggle source
# File lib/mini_sql/postgres/connection.rb, line 172 def query_decorator(decorator, sql, *params) result = run(sql, params) result.type_map = type_map deserializer_cache.materialize(result, decorator) ensure result.clear if result end
query_each(sql, *params) { |materialize| ... }
click to toggle source
# File lib/mini_sql/postgres/connection.rb, line 106 def query_each(sql, *params) raise StandardError, "Please supply a block when calling query_each" if !block_given? if params && params.length > 0 sql = param_encoder.encode(sql, *params) end raw_connection.send_query(sql) raw_connection.set_single_row_mode loop do result = raw_connection.get_result break if !result result.check if result.ntuples == 0 # skip, this happens at the end when we get totals else materializer ||= deserializer_cache.materializer(result) result.type_map = type_map i = 0 # technically we should only get 1 row here # but protect against future batching changes while i < result.ntuples yield materializer.materialize(result, i) i += 1 end end result.clear end end
query_each_hash(sql, *params) { |result| ... }
click to toggle source
# File lib/mini_sql/postgres/connection.rb, line 139 def query_each_hash(sql, *params) raise StandardError, "Please supply a block when calling query_each_hash" if !block_given? if params && params.length > 0 sql = param_encoder.encode(sql, *params) end raw_connection.send_query(sql) raw_connection.set_single_row_mode loop do result = raw_connection.get_result break if !result result.check if result.ntuples == 0 # skip, this happens at the end when we get totals else result.type_map = type_map i = 0 # technically we should only get 1 row here # but protect against future batching changes while i < result.ntuples yield result[i] i += 1 end end result.clear end end
query_hash(sql, *params)
click to toggle source
# File lib/mini_sql/postgres/connection.rb, line 187 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)
click to toggle source
Returns a flat array containing all results. Note, if selecting multiple columns array will be flattened
@param sql [String] the query to run @param params [Array or Hash], params to apply to query @return [Object] a flat array containing all results
# File lib/mini_sql/postgres/connection.rb, line 63 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
type_map()
click to toggle source
# File lib/mini_sql/postgres/connection.rb, line 45 def type_map @type_map ||= self.class.type_map(raw_connection) end
Private Instance Methods
run(sql, params)
click to toggle source
# File lib/mini_sql/postgres/connection.rb, line 205 def run(sql, params) if params && params.length > 0 sql = param_encoder.encode(sql, *params) end raw_connection.async_exec(sql) end