lib/mini_sql/connection.rb in mini_sql-0.1.1 vs lib/mini_sql/connection.rb in mini_sql-0.1.2
- old
+ new
@@ -8,10 +8,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
def initialize(conn, deserializer_cache = nil, type_map = nil)
# TODO adapter to support other databases
@@ -41,10 +42,14 @@
result.cmd_tuples
ensure
result.clear if result
end
+ def build(sql)
+ Builder.new(self, sql)
+ end
+
private
def run(sql, params)
if params
@conn.async_exec(*process_params(sql, params))
@@ -53,14 +58,29 @@
end
end
def process_params(sql, params)
sql = sql.dup
- param_array = []
+ param_array = nil
- params.each do |k, v|
- sql.gsub!(":#{k.to_s}", "$#{param_array.length + 1}")
- param_array << v
+ if Hash === params
+ param_array = []
+ params.each do |k, v|
+ sql.gsub!(":#{k.to_s}", "$#{param_array.length + 1}")
+ param_array << v
+ end
+ elsif Array === params
+ i = 0
+ sql.gsub!("?") do
+ i += 1
+ case params[i-1]
+ when Integer then "$#{i}::bigint"
+ when Float then "$#{i}::float8"
+ when String then "$#{i}::text"
+ else "$#{i}::unknown"
+ end
+ end
+ param_array = params
end
[sql, param_array]
end