Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[prepared statements] fix for auto_encode_arrays, refactoring #59

Merged
merged 6 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/mini_sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module Postgres
autoload :PreparedConnection, "mini_sql/postgres/prepared_connection"
autoload :PreparedCache, "mini_sql/postgres/prepared_cache"
autoload :PreparedBinds, "mini_sql/postgres/prepared_binds"
autoload :PreparedBindsAutoArray, "mini_sql/postgres/prepared_binds_auto_array"
end

module ActiveRecordPostgres
Expand Down
5 changes: 2 additions & 3 deletions lib/mini_sql/abstract/prepared_binds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ def bind_hash(sql, hash)

def bind_array(sql, array)
sql = sql.dup
param_i = 0
param_i = -1
i = 0
binds = []
bind_names = []
sql.gsub!("?") do
param_i += 1
array_wrap(array[param_i - 1]).map do |vv|
array_wrap(array[param_i += 1]).map do |vv|
binds << vv
i += 1
bind_names << [BindName.new("$#{i}")]
Expand Down
6 changes: 5 additions & 1 deletion lib/mini_sql/abstract/prepared_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(connection, max_size = nil)
end

def prepare_statement(sql)
stm_key = "#{@connection.object_id}-#{sql}"
stm_key = "#{raw_connection.object_id}-#{sql}"
statement = @cache.delete(stm_key)
if statement
@cache[stm_key] = statement
Expand All @@ -28,6 +28,10 @@ def prepare_statement(sql)

private

def raw_connection
@connection.raw_connection
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for usage with ActiveRecord

end

def next_key
"s#{@counter += 1}"
end
Expand Down
24 changes: 13 additions & 11 deletions lib/mini_sql/inline_param_encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,21 @@ def quoted_time(value)
value.utc.iso8601
end

EMPTY_ARRAY = [].freeze

def quote_val(value)
case value
when String then "'#{conn.escape_string(value.to_s)}'"
when Numeric then value.to_s
when BigDecimal then value.to_s("F")
when Time then "'#{quoted_time(value)}'"
when Date then "'#{value.to_s}'"
when Symbol then "'#{conn.escape_string(value.to_s)}'"
when true then "true"
when false then "false"
when nil then "NULL"
when [] then "NULL"
when Array then array_encoder ? "'#{array_encoder.encode(value)}'" : value.map { |v| quote_val(v) }.join(', ')
when String then "'#{conn.escape_string(value.to_s)}'"
when Numeric then value.to_s
when BigDecimal then value.to_s("F")
when Time then "'#{quoted_time(value)}'"
when Date then "'#{value.to_s}'"
when Symbol then "'#{conn.escape_string(value.to_s)}'"
when true then "true"
when false then "false"
when nil then "NULL"
when EMPTY_ARRAY then array_encoder ? "'{}'" : "NULL"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arr = []
freeze_arr = [].freeze
Benchmark.ips do |x|
  x.report("new") {
    case arr
    when [] then true
    else false
    end
  }
  x.report("inline freeze") {
    case arr
    when [].freeze then true
    else false
    end
  }
  x.report("var freeze") {
    case arr
    when freeze_arr then true
    else false
    end
  }
  x.compare!
end;

# Comparison:
#           var freeze:  6412829.6 i/s
#                  new:  5822569.7 i/s - 1.10x  slower
#        inline freeze:  5379263.0 i/s - 1.19x  slower

when Array then array_encoder ? "'#{array_encoder.encode(value)}'" : value.map { |v| quote_val(v) }.join(', ')
else raise TypeError, "can't quote #{value.class.name}"
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mini_sql/mysql/prepared_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PreparedCache < ::MiniSql::Abstract::PreparedCache
private

def alloc(sql)
@connection.prepare(sql)
raw_connection.prepare(sql)
end

def dealloc(statement)
Expand Down
10 changes: 4 additions & 6 deletions lib/mini_sql/mysql/prepared_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ class PreparedConnection < Connection
attr_reader :unprepared

def initialize(unprepared_connection)
@unprepared = unprepared_connection
@raw_connection = unprepared_connection.raw_connection
@param_encoder = unprepared_connection.param_encoder

@prepared_cache = PreparedCache.new(@raw_connection)
@param_binder = PreparedBinds.new
@unprepared = unprepared_connection
@param_binder = PreparedBinds.new
end

def build(_)
Expand All @@ -29,6 +25,8 @@ def deserializer_cache

private def run(sql, as, params)
prepared_sql, binds, _bind_names = @param_binder.bind(sql, *params)

@prepared_cache ||= PreparedCache.new(unprepared)
statement = @prepared_cache.prepare_statement(prepared_sql)
statement.execute(
*binds,
Expand Down
4 changes: 2 additions & 2 deletions lib/mini_sql/postgres/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module MiniSql
module Postgres
class Connection < MiniSql::Connection
attr_reader :raw_connection, :param_encoder, :deserializer_cache
attr_reader :raw_connection, :param_encoder, :deserializer_cache, :array_encoder

def self.default_deserializer_cache
@deserializer_cache ||= DeserializerCache.new
Expand Down Expand Up @@ -52,7 +52,7 @@ def self.type_map(conn)
def initialize(raw_connection, args = nil)
@raw_connection = raw_connection
@deserializer_cache = (args && args[:deserializer_cache]) || self.class.default_deserializer_cache
array_encoder = PG::TextEncoder::Array.new if args && args[:auto_encode_arrays]
@array_encoder = PG::TextEncoder::Array.new if args && args[:auto_encode_arrays]
@param_encoder = (args && args[:param_encoder]) || InlineParamEncoder.new(self, array_encoder)
@type_map = args && args[:type_map]
end
Expand Down
61 changes: 61 additions & 0 deletions lib/mini_sql/postgres/prepared_binds_auto_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

require "mini_sql/abstract/prepared_binds"

module MiniSql
module Postgres
class PreparedBindsAutoArray < ::MiniSql::Abstract::PreparedBinds

attr_reader :array_encoder

def initialize(array_encoder)
@array_encoder = array_encoder
end

def bind_hash(sql, hash)
sql = sql.dup
binds = []
bind_names = []
i = 0

hash.each do |k, v|
binds << (v.is_a?(Array) ? array_encoder.encode(v) : v)
bind_names << [BindName.new(k)]
bind_outputs = bind_output(i += 1)

sql.gsub!(":#{k}") do
# ignore ::int and stuff like that
# $` is previous to match
if $` && $`[-1] != ":"
bind_outputs
else
":#{k}"
end
end
end
[sql, binds, bind_names]
end

def bind_array(sql, array)
sql = sql.dup
param_i = -1
i = 0
binds = []
bind_names = []
sql.gsub!("?") do
v = array[param_i += 1]
binds << (v.is_a?(Array) ? array_encoder.encode(v) : v)
i += 1
bind_names << [BindName.new("$#{i}")]
bind_output(i)
end
[sql, binds, bind_names]
end

def bind_output(i)
"$#{i}"
end

end
end
end
4 changes: 2 additions & 2 deletions lib/mini_sql/postgres/prepared_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class PreparedCache < ::MiniSql::Abstract::PreparedCache

def alloc(sql)
alloc_key = next_key
@connection.prepare(alloc_key, sql)
raw_connection.prepare(alloc_key, sql)

alloc_key
end

def dealloc(key)
@connection.query "DEALLOCATE #{key}" if @connection.status == PG::CONNECTION_OK
raw_connection.query "DEALLOCATE #{key}" if raw_connection.status == PG::CONNECTION_OK
rescue PG::Error
end

Expand Down
13 changes: 5 additions & 8 deletions lib/mini_sql/postgres/prepared_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ class PreparedConnection < Connection
attr_reader :unprepared

def initialize(unprepared_connection)
@unprepared = unprepared_connection
@raw_connection = unprepared_connection.raw_connection
@type_map = unprepared_connection.type_map
@param_encoder = unprepared_connection.param_encoder

@prepared_cache = PreparedCache.new(@raw_connection)
@param_binder = PreparedBinds.new
@unprepared = unprepared_connection
@type_map = unprepared_connection.type_map
@param_binder = unprepared.array_encoder ? PreparedBindsAutoArray.new(unprepared.array_encoder) : PreparedBinds.new
end

def build(_)
Expand All @@ -30,8 +26,9 @@ def deserializer_cache

private def run(sql, params)
prepared_sql, binds, _bind_names = @param_binder.bind(sql, *params)
@prepared_cache ||= PreparedCache.new(unprepared)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lazy evaluate

prepare_statement_key = @prepared_cache.prepare_statement(prepared_sql)
raw_connection.exec_prepared(prepare_statement_key, binds)
unprepared.raw_connection.exec_prepared(prepare_statement_key, binds)
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/mini_sql/sqlite/prepared_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PreparedCache < MiniSql::Abstract::PreparedCache
private

def alloc(sql)
@connection.prepare(sql)
raw_connection.prepare(sql)
end

def dealloc(statement)
Expand Down
9 changes: 3 additions & 6 deletions lib/mini_sql/sqlite/prepared_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ class PreparedConnection < Connection
attr_reader :unprepared

def initialize(unprepared_connection)
@unprepared = unprepared_connection
@raw_connection = unprepared_connection.raw_connection
@param_encoder = unprepared_connection.param_encoder

@prepared_cache = PreparedCache.new(@raw_connection)
@param_binder = PreparedBinds.new
@unprepared = unprepared_connection
@param_binder = PreparedBinds.new
end

def build(_)
Expand All @@ -29,6 +25,7 @@ def deserializer_cache

private def run(sql, params)
prepared_sql, binds, _bind_names = @param_binder.bind(sql, *params)
@prepared_cache ||= PreparedCache.new(unprepared)
statement = @prepared_cache.prepare_statement(prepared_sql)
statement.bind_params(binds)
if block_given?
Expand Down
19 changes: 15 additions & 4 deletions test/mini_sql/postgres/connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,26 @@ def test_unamed_query
assert_equal(row.column2, 3)
end

def test_encode_array
def test_array_with_auto_encode_arrays
connection = pg_connection(auto_encode_arrays: true)

ints = [1, 2, 3]
strings = %w[a b c]
row = connection.query("select ?::int[] ints, ?::text[] strings", ints, strings).first
empty_array = []
row = connection.query_single("select ?::int[], ?::text[], ?::int[]", ints, strings, empty_array)

assert_equal(row.ints, ints)
assert_equal(row.strings, strings)
assert_equal(row, [ints, strings, empty_array])
end

def test_simple_with_auto_encode_arrays
connection = pg_connection(auto_encode_arrays: true)

int = 1
str = "str"
date = Date.new(2020, 10, 10)
row = connection.query_single("select ?, ?, ?::date", int, str, date)

assert_equal(row, [int, str, date])
end

end
40 changes: 35 additions & 5 deletions test/mini_sql/postgres/prepared_connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,11 @@ def test_numbers_param
end

def test_limit_prepared_cache
@prepared_connection.instance_variable_get(
:@prepared_cache
).instance_variable_set(:@max_size, 1)
@prepared_connection.instance_variable_set(:@prepared_cache, MiniSql::Postgres::PreparedCache.new(@unprepared_connection, 1))

assert_equal @prepared_connection.query_single("SELECT ?", 1), %w[1]
assert_equal @prepared_connection.query_single("SELECT ?, ?", 1, 2), %w[1 2]
assert_equal @prepared_connection.query_single("SELECT ?, ?, ?", 1, 2, 3),
%w[1 2 3]
assert_equal @prepared_connection.query_single("SELECT ?, ?, ?", 1, 2, 3), %w[1 2 3]

ps = @unprepared_connection.query("select * from pg_prepared_statements")
assert_equal ps.size, 1
Expand All @@ -82,4 +79,37 @@ def test_single_named_param
assert_last_stmt "select $1, $1, $1"
assert_equal %w[test test test], r
end

def test_array_with_auto_encode_arrays
connection = pg_connection(auto_encode_arrays: true).prepared

ints = [1, 2, 3]
strings = %w[a b c]
empty_array = []
row = connection.query_single("select ?::int[], ?::text[], ?::int[]", ints, strings, empty_array)

assert_equal(row, [ints, strings, empty_array])
end

def test_simple_with_auto_encode_arrays
connection = pg_connection(auto_encode_arrays: true).prepared

int = 1
str = "str"
date = Date.new(2020, 10, 10)
row = connection.query_single("select ?::int, ?, ?::date", int, str, date)

assert_equal(row, [int, str, date])
end

def test_hash_params_with_auto_encode_arrays
connection = pg_connection(auto_encode_arrays: true).prepared

num = 1
date = Date.new(2020, 10, 10)
ints = [1, 2, 3]
row = connection.query_single("select :num::int, :date::date, :ints::int[]", num: num, date: date, ints: ints)

assert_equal(row, [num, date, ints])
end
end
Loading