Skip to content

Commit

Permalink
refactor PreparedConnection for more useful ActiveRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
ermolaev committed Aug 22, 2024
1 parent 7fc32ce commit 36635d7
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 30 deletions.
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
end

def next_key
"s#{@counter += 1}"
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/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 = unprepared.array_encoder ? PreparedBindsAutoArray.new(unprepared.array_encoder) : 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)
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
7 changes: 2 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 Down

0 comments on commit 36635d7

Please sign in to comment.