From 6f626f245b0949b548f0850221b4b7e2678c5319 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 22:48:51 +0000 Subject: [PATCH 1/2] Add E2E tests for a lost pg connection inside a transaction, a stopped Async task on mysql2 and SQLite hash rows Co-Authored-By: Doron Eli Rachman --- tests/end2end_mysql2.rb | 15 +++++++++++++++ tests/end2end_pg.rb | 21 +++++++++++++++++++++ tests/end2end_sqlite3.rb | 18 ++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/tests/end2end_mysql2.rb b/tests/end2end_mysql2.rb index a149854..7ddd64b 100755 --- a/tests/end2end_mysql2.rb +++ b/tests/end2end_mysql2.rb @@ -820,6 +820,21 @@ def test_killed_backend_inside_transaction_is_replaced assert_empty pooled.list_authors end + def test_a_stopped_async_task_does_not_leave_a_closed_connection_in_the_pool + pooled = QuerySql.connect(mysql2_params, size: 1) + [false, true].each do |in_transaction| + assert_empty pooled.list_authors + Sync do |task| + query = task.async do + in_transaction ? pooled.transaction { |tx| tx.pause_query(seconds: 2) } : pooled.pause_query(seconds: 2) + end + sleep 0.2 + query.stop + end + assert_empty pooled.list_authors, "next call after stopping a query (in_transaction: #{in_transaction})" + end + end + def test_seeded_random_copyfrom_round_trip_and_duplicate_is_atomic random = seeded_random rows = Array.new(300) do |i| diff --git a/tests/end2end_pg.rb b/tests/end2end_pg.rb index dabf4ab..c503342 100755 --- a/tests/end2end_pg.rb +++ b/tests/end2end_pg.rb @@ -914,6 +914,27 @@ def test_terminated_backend_inside_transaction_is_replaced assert_empty pooled.list_authors end + def test_connection_lost_inside_transaction_is_not_reconnected_until_the_transaction_ends + pooled = QuerySql.connect(pg_params, size: 1) + error = assert_raises(PG::ConnectionBad) do + pooled.transaction do |tx| + tx.create_author_return_id(name: 'before the loss', bio: nil) + pid = pooled.instance_variable_get(:@db).with(&:backend_pid) + PG.connect(**pg_params.transform_keys(&:to_sym)) do |admin| + admin.exec_params('SELECT pg_terminate_backend($1)', [pid]) + sleep 0.01 while admin.exec_params('SELECT 1 FROM pg_stat_activity WHERE pid = $1', [pid]).ntuples.positive? + end + assert_raises(PG::Error) { tx.list_authors } + # the block rescued the error: later calls must not run on a fresh connection outside the transaction + assert_raises(PG::ConnectionBad) { tx.create_author_return_id(name: 'after the loss', bio: nil) } + :committed + end + end + assert_match(/connection was lost/, error.message) + assert_empty pooled.list_authors + pooled.instance_variable_get(:@db).with { |client| assert_equal PG::PQTRANS_IDLE, client.transaction_status } + end + def test_seeded_random_copyfrom_round_trip_and_duplicate_is_atomic random = seeded_random rows = Array.new(300) do |i| diff --git a/tests/end2end_sqlite3.rb b/tests/end2end_sqlite3.rb index 108bef7..e0e68e8 100644 --- a/tests/end2end_sqlite3.rb +++ b/tests/end2end_sqlite3.rb @@ -438,6 +438,24 @@ def test_fibers_sharing_a_database_take_turns assert_equal 8, @query_sql.list_authors.size end + def test_a_database_returning_hash_rows_still_builds_rows + db = SQLite3::Database.new(':memory:', results_as_hash: true) + db.execute_batch(SCHEMA) + queries = QuerySql.new(db) + id = queries.create_author_return_id(name: 'hash', bio: 'rows') + assert_kind_of Integer, id + assert_equal row_for(GetAuthorRow, [id, 'hash', 'rows']), queries.get_author(id: id) + queries.create_book(name: 'book', author_id: id) + row = queries.list_all_authors_books.first + assert_equal %w[hash book], [row.author.name, row.book.name] + assert_equal [id], queries.get_authors_by_repeated_named_and_positional_batch( + [GetAuthorsByRepeatedNamedAndPositionalBatchArgs.new(n: 'hash', other: 'x', id: 0)] + ).map { |r| r&.id } + assert_equal [{ 'id' => id, 'name' => 'hash' }], db.execute('SELECT id, name FROM authors') # setting left alone + ensure + db&.close + end + # ---- Ruby conventions: bang finders, logger ---------------------------------------------------------------- def test_bang_finder_raises_not_found From 39e3d305fbf65b952444db1987d49db45f8adb24 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 22:48:51 +0000 Subject: [PATCH 2/2] Fix pg reconnect inside a transaction, mysql2 closed client after an interrupted query, SQLite results_as_hash rows; reject SQLite parameters sqlc does not report Co-Authored-By: Doron Eli Rachman --- README.md | 11 +- docs/Coverage.md | 3 +- examples/mysql2/query_sql.rb | 10 +- examples/pg/connection.rb | 3 + examples/pg/connection.rbs | 2 + examples/pg/query_sql.rb | 19 +- examples/sqlite3/query_sql.rb | 98 +++++---- examples/sqlite3/query_sql.rbs | 2 + lib/sqlc_gen_ruby/drivers/mysql2.rb | 10 +- lib/sqlc_gen_ruby/drivers/pg.rb | 24 ++- lib/sqlc_gen_ruby/drivers/sqlite3.rb | 46 ++++- steep_expectations.yml | 194 +++++++++++------- test/features_test.rb | 21 ++ test/golden/mysql2/default/my_queries_sql.rb | 10 +- test/golden/mysql2/default/other_sql.rb | 12 +- test/golden/mysql2/default/query_sql.rb | 12 +- .../mysql2/ruby31_gemfile/my_queries_sql.rb | 10 +- .../golden/mysql2/ruby31_gemfile/other_sql.rb | 12 +- .../golden/mysql2/ruby31_gemfile/query_sql.rb | 12 +- .../mysql2/ruby32_notypes/my_queries_sql.rb | 10 +- .../golden/mysql2/ruby32_notypes/other_sql.rb | 12 +- .../golden/mysql2/ruby32_notypes/query_sql.rb | 12 +- test/golden/pg/default/connection.rb | 3 + test/golden/pg/default/connection.rbs | 2 + test/golden/pg/default/my_queries_sql.rb | 19 +- test/golden/pg/default/other_sql.rb | 21 +- test/golden/pg/default/query_sql.rb | 21 +- test/golden/pg/ruby31_gemfile/connection.rb | 3 + test/golden/pg/ruby31_gemfile/connection.rbs | 2 + .../pg/ruby31_gemfile/my_queries_sql.rb | 19 +- test/golden/pg/ruby31_gemfile/other_sql.rb | 21 +- test/golden/pg/ruby31_gemfile/query_sql.rb | 21 +- test/golden/pg/ruby32_notypes/connection.rb | 3 + .../pg/ruby32_notypes/my_queries_sql.rb | 19 +- test/golden/pg/ruby32_notypes/other_sql.rb | 21 +- test/golden/pg/ruby32_notypes/query_sql.rb | 21 +- test/golden/sqlite3/default/my_queries_sql.rb | 10 +- .../golden/sqlite3/default/my_queries_sql.rbs | 2 + test/golden/sqlite3/default/other_sql.rb | 16 +- test/golden/sqlite3/default/other_sql.rbs | 2 + test/golden/sqlite3/default/query_sql.rb | 106 +++++----- test/golden/sqlite3/default/query_sql.rbs | 2 + .../sqlite3/ruby31_gemfile/my_queries_sql.rb | 10 +- .../sqlite3/ruby31_gemfile/my_queries_sql.rbs | 2 + .../sqlite3/ruby31_gemfile/other_sql.rb | 16 +- .../sqlite3/ruby31_gemfile/other_sql.rbs | 2 + .../sqlite3/ruby31_gemfile/query_sql.rb | 106 +++++----- .../sqlite3/ruby31_gemfile/query_sql.rbs | 2 + .../sqlite3/ruby32_notypes/my_queries_sql.rb | 10 +- .../sqlite3/ruby32_notypes/other_sql.rb | 16 +- .../sqlite3/ruby32_notypes/query_sql.rb | 106 +++++----- test/synthetic_requests.rb | 4 +- 52 files changed, 776 insertions(+), 377 deletions(-) diff --git a/README.md b/README.md index 6e4948a..62e2bfe 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ per table, one module of constants per enum), `connection.rb` (shared helpers) a * **Bang finders:** every `:one` query also gets a `get_author!` that raises `QuerySql::NotFound` instead of returning `nil`. * **Connections:** `QuerySql.connect(params, **pool_options)` builds a [`connection_pool`](https://github.com/mperham/connection_pool). To use a pool you already have (or a fake in tests) pass it to `QuerySql.new(pool)`; anything with `#with` works. - For SQLite pass a `SQLite3::Database`: `QuerySql.new(db)`. + For SQLite pass a `SQLite3::Database` (opened with any options, `results_as_hash: true` included): `QuerySql.new(db)`. * **Logging:** `connect(params, logger: Rails.logger)` / `new(pool, logger:)` logs `QuerySql#get_author (0.4ms)` at debug level for each query and transaction. Parameter values are never logged. * **Async / fibers:** it is plain synchronous Ruby and works with the Fiber scheduler ([`async`](https://github.com/socketry/async)): @@ -131,7 +131,11 @@ end back, so `transaction` raises `PG::InFailedSqlTransaction` instead of pretending it committed. In a nested `transaction` only the savepoint is rolled back: the outer block can rescue that error and go on. * A connection the server closed (restart, failover, killed backend) is reconnected (`pg`) or replaced in the pool - (`mysql2`) the next time it is checked out; the call that hit the dead connection still raises. + (`mysql2`) the next time it is checked out; the call that hit the dead connection still raises. Inside `transaction` + a lost `pg` connection is not reconnected: every later call in the block and `transaction` itself raise + `PG::ConnectionBad`, so nothing after the loss runs outside the transaction (the server rolled it back). A `mysql2` + client that mysql2 closed because a running query was interrupted (an `Async` task stopped, `Thread#raise`) is + replaced the same way, without failing the next call. * SQLite: threads and fibers sharing one `SQLite3::Database` take turns; a transaction holds the database until it ends. ## Supported features @@ -191,6 +195,9 @@ server; duplicate keys and invalid values raise `Mysql2::Error` instead of being * Two result columns with the same name (e.g. `a.id, b.id`) are an error: alias one (`b.id AS book_id`) or use `sqlc.embed`. * SQLite's `NUMERIC` affinity can return an `Integer` where `Float` is declared. * `:batchexec` / `:batchone` / `:batchmany` are not atomic on their own: wrap them in `transaction` for all-or-nothing. +* SQLite: sqlc does not see parameters in `ON CONFLICT ... DO UPDATE SET` (it leaves `sqlc.arg(...)` in the SQL or + reports fewer parameters than there are `?`), so such queries are generation errors. Use `excluded.` + (`DO UPDATE SET n = counters.n + excluded.n`) or move the value into `VALUES`. * SQLite transactions belong to the thread that opened them; fibers sharing the same database take turns, and a transaction holds it until it ends. * Not applicable to Ruby (they exist in the C# plugin): `withAsyncSuffix` / `withCancellationToken` (use the Fiber diff --git a/docs/Coverage.md b/docs/Coverage.md index ae5efe2..14c1a40 100644 --- a/docs/Coverage.md +++ b/docs/Coverage.md @@ -21,7 +21,7 @@ only when the behavior is rejected or decided during generation and generated co | MySQL unsigned integers, BIT, YEAR, DATETIME/TIME, JSON, enums/SET and client quoting | not MySQL | [`test_client_side_quoting_round_trips_supported_values`](../tests/end2end_mysql2.rb#test_client_side_quoting_round_trips_supported_values), [`test_time_values_match_mysql2_prepared_statement_storage`](../tests/end2end_mysql2.rb#test_time_values_match_mysql2_prepared_statement_storage), [`test_declared_types_match_runtime_values`](../tests/end2end_mysql2.rb#test_declared_types_match_runtime_values) | not MySQL | [`test_mysql_uses_client_side_binding_and_sqlite_closes_prepared_statements`](../test/features_test.rb#test_mysql_uses_client_side_binding_and_sqlite_closes_prepared_statements) | | SQLite affinity, JSON/UUID, and NUMERIC integer/float results | not SQLite | not SQLite | [`test_declared_types_match_runtime_values`](../tests/end2end_sqlite3.rb#test_declared_types_match_runtime_values), [`test_declared_types_with_parameters`](../tests/end2end_sqlite3.rb#test_declared_types_with_parameters) | [`test_sqlite_declared_types_follow_type_affinity`](../test/features_test.rb#test_sqlite_declared_types_follow_type_affinity), [`test_sqlite_json_and_uuid_declarations_are_strings`](../test/features_test.rb#test_sqlite_json_and_uuid_declarations_are_strings) | | Names: keywords, method collisions, acronyms, quoted mixed case, repeated `$n`, plural models | [`test_keyword_and_method_names_keep_distinct_row_and_argument_names`](../tests/end2end_pg.rb#test_keyword_and_method_names_keep_distinct_row_and_argument_names), [`test_repeated_column_parameters_get_distinct_names`](../tests/end2end_pg.rb#test_repeated_column_parameters_get_distinct_names) | [`test_keyword_and_method_names_keep_distinct_row_and_argument_names`](../tests/end2end_mysql2.rb#test_keyword_and_method_names_keep_distinct_row_and_argument_names), [`test_repeated_positional_parameters_get_distinct_names`](../tests/end2end_mysql2.rb#test_repeated_positional_parameters_get_distinct_names) | [`test_keyword_and_method_names_keep_distinct_row_and_argument_names`](../tests/end2end_sqlite3.rb#test_keyword_and_method_names_keep_distinct_row_and_argument_names) | [`test_snake_case_keeps_acronyms_together`](../test/features_test.rb#test_snake_case_keeps_acronyms_together), [`test_query_names_cannot_replace_generated_methods`](../test/features_test.rb#test_query_names_cannot_replace_generated_methods), [`test_tables_with_the_same_singular_get_distinct_models`](../test/features_test.rb#test_tables_with_the_same_singular_get_distinct_models) | -| Connections, injected pools, reconnect behavior and SQLite serialization | [`test_queries_can_share_an_injected_pool`](../tests/end2end_pg.rb#test_queries_can_share_an_injected_pool), [`test_a_connection_closed_by_the_server_is_reconnected`](../tests/end2end_pg.rb#test_a_connection_closed_by_the_server_is_reconnected) | [`test_queries_can_share_an_injected_pool`](../tests/end2end_mysql2.rb#test_queries_can_share_an_injected_pool), [`test_a_connection_closed_by_the_server_is_replaced`](../tests/end2end_mysql2.rb#test_a_connection_closed_by_the_server_is_replaced) | [`test_fibers_sharing_a_database_take_turns`](../tests/end2end_sqlite3.rb#test_fibers_sharing_a_database_take_turns), [`test_threads_sharing_a_database_take_turns_in_transactions`](../tests/end2end_sqlite3.rb#test_threads_sharing_a_database_take_turns_in_transactions) | [`test_engine_must_match_the_driver`](../test/features_test.rb#test_engine_must_match_the_driver) | +| Connections, injected pools, reconnect behavior and SQLite serialization | [`test_queries_can_share_an_injected_pool`](../tests/end2end_pg.rb#test_queries_can_share_an_injected_pool), [`test_a_connection_closed_by_the_server_is_reconnected`](../tests/end2end_pg.rb#test_a_connection_closed_by_the_server_is_reconnected), [`test_connection_lost_inside_transaction_is_not_reconnected_until_the_transaction_ends`](../tests/end2end_pg.rb#test_connection_lost_inside_transaction_is_not_reconnected_until_the_transaction_ends) | [`test_queries_can_share_an_injected_pool`](../tests/end2end_mysql2.rb#test_queries_can_share_an_injected_pool), [`test_a_connection_closed_by_the_server_is_replaced`](../tests/end2end_mysql2.rb#test_a_connection_closed_by_the_server_is_replaced), [`test_a_stopped_async_task_does_not_leave_a_closed_connection_in_the_pool`](../tests/end2end_mysql2.rb#test_a_stopped_async_task_does_not_leave_a_closed_connection_in_the_pool) | [`test_fibers_sharing_a_database_take_turns`](../tests/end2end_sqlite3.rb#test_fibers_sharing_a_database_take_turns), [`test_threads_sharing_a_database_take_turns_in_transactions`](../tests/end2end_sqlite3.rb#test_threads_sharing_a_database_take_turns_in_transactions), [`test_a_database_returning_hash_rows_still_builds_rows`](../tests/end2end_sqlite3.rb#test_a_database_returning_hash_rows_still_builds_rows) | [`test_engine_must_match_the_driver`](../test/features_test.rb#test_engine_must_match_the_driver) | | Transaction result, commit/rollback, nested savepoint, exit semantics and rescued PostgreSQL error | [`test_nested_savepoint_rolls_back_while_outer_transaction_commits`](../tests/end2end_pg.rb#test_nested_savepoint_rolls_back_while_outer_transaction_commits), [`test_transaction_nonlocal_exit_semantics_follow_timeout_version`](../tests/end2end_pg.rb#test_transaction_nonlocal_exit_semantics_follow_timeout_version), [`test_transaction_whose_failed_statement_was_rescued_raises_instead_of_committing`](../tests/end2end_pg.rb#test_transaction_whose_failed_statement_was_rescued_raises_instead_of_committing) | [`test_nested_savepoint_rolls_back_while_outer_transaction_commits`](../tests/end2end_mysql2.rb#test_nested_savepoint_rolls_back_while_outer_transaction_commits), [`test_transaction_nonlocal_exit_semantics_follow_timeout_version`](../tests/end2end_mysql2.rb#test_transaction_nonlocal_exit_semantics_follow_timeout_version) | [`test_nested_transaction_rolls_back_to_a_savepoint`](../tests/end2end_sqlite3.rb#test_nested_transaction_rolls_back_to_a_savepoint), [`test_transaction_nonlocal_exit_semantics_follow_timeout_version`](../tests/end2end_sqlite3.rb#test_transaction_nonlocal_exit_semantics_follow_timeout_version) | [`test_nested_transactions_reject_isolation_and_mode`](../test/features_test.rb#test_nested_transactions_reject_isolation_and_mode) | | Isolation applied; invalid isolation/mode rejected before begin; SQLite locks | [`test_transaction_isolation_level`](../tests/end2end_pg.rb#test_transaction_isolation_level) | [`test_transaction_isolation_level_is_applied`](../tests/end2end_mysql2.rb#test_transaction_isolation_level_is_applied) | [`test_transaction_mode`](../tests/end2end_sqlite3.rb#test_transaction_mode), [`test_immediate_and_exclusive_transactions_block_a_second_connection`](../tests/end2end_sqlite3.rb#test_immediate_and_exclusive_transactions_block_a_second_connection), [`test_unknown_transaction_mode_is_rejected_before_beginning`](../tests/end2end_sqlite3.rb#test_unknown_transaction_mode_is_rejected_before_beginning) | [`test_nested_transactions_reject_isolation_and_mode`](../test/features_test.rb#test_nested_transactions_reject_isolation_and_mode) | | Logger debug format, transaction logs, secret-free parameters | [`test_logger_receives_query_names_and_durations`](../tests/end2end_pg.rb#test_logger_receives_query_names_and_durations) | [`test_logger_receives_query_names_and_durations`](../tests/end2end_mysql2.rb#test_logger_receives_query_names_and_durations) | [`test_logger_receives_query_names_and_durations`](../tests/end2end_sqlite3.rb#test_logger_receives_query_names_and_durations) | [`test_logger_support`](../test/features_test.rb#test_logger_support) | @@ -33,5 +33,6 @@ only when the behavior is rejected or decided during generation and generated co | A transaction ended by the database keeps the original error; a query abandoned by Timeout is cancelled | [`test_query_abandoned_by_a_timeout_is_cancelled`](../tests/end2end_pg.rb#test_query_abandoned_by_a_timeout_is_cancelled) | [`test_deadlock_in_a_nested_transaction_raises_the_deadlock_error`](../tests/end2end_mysql2.rb#test_deadlock_in_a_nested_transaction_raises_the_deadlock_error) | [`test_conflict_that_ends_the_transaction_raises_the_constraint_error`](../tests/end2end_sqlite3.rb#test_conflict_that_ends_the_transaction_raises_the_constraint_error) | | | Top-level Float JSON parameters, Symbol enum values | [`test_top_level_float_json_parameters_round_trip_exactly`](../tests/end2end_pg.rb#test_top_level_float_json_parameters_round_trip_exactly) | [`test_symbol_enum_value_is_bound_as_its_name`](../tests/end2end_mysql2.rb#test_symbol_enum_value_is_bound_as_its_name) | not SQLite (no enums / JSON encoding) | | | Modules sharing a connection keep separate prepared statements | [`test_generator_options_preserve_runtime_values_and_generate_expected_layouts`](../tests/end2end_options.rb#test_generator_options_preserve_runtime_values_and_generate_expected_layouts) | not MySQL (no server-side prepared statements) | not SQLite (statements are per call) | [`test_statements_are_named_after_the_module_and_query_class`](../test/features_test.rb#test_statements_are_named_after_the_module_and_query_class) | +| SQLite parameters sqlc does not report (`ON CONFLICT ... DO UPDATE SET`) are generation errors | not PostgreSQL | not MySQL | unit: rejected at generation | [`test_sqlite_parameters_sqlc_does_not_report_are_generation_errors`](../test/features_test.rb#test_sqlite_parameters_sqlc_does_not_report_are_generation_errors), [`test_sqlite_placeholders_in_literals_and_comments_are_not_parameters`](../test/features_test.rb#test_sqlite_placeholders_in_literals_and_comments_are_not_parameters) | CI runs `test/coverage_map_test.rb`, which checks every linked test method exists in its referenced `tests/` or `test/` file. diff --git a/examples/mysql2/query_sql.rb b/examples/mysql2/query_sql.rb index d706042..82917e4 100644 --- a/examples/mysql2/query_sql.rb +++ b/examples/mysql2/query_sql.rb @@ -503,8 +503,9 @@ def transaction(isolation: nil) begin client.query(rollback) rescue Mysql2::Error - # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone - raise unless e.is_a?(Mysql2::Error) + # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone; + # an interrupted query closed the connection: the server rolls back + raise unless e.is_a?(Mysql2::Error) || client.closed? end raise ensure @@ -1227,8 +1228,9 @@ def with_client(name) client.extend(ConnectionState) unless client.is_a?(ConnectionState) begin yield client - rescue Mysql2::Error - # a connection closed by the server (restart, KILL, wait_timeout) is replaced by the pool + rescue Exception # rubocop:disable Lint/RescueException + # a connection closed by the server (restart, KILL, wait_timeout) or by mysql2 when a running query + # was interrupted (Async task stopped, Thread#raise) is replaced by the pool @db.discard_current_connection if client.closed? && @db.respond_to?(:discard_current_connection) raise end diff --git a/examples/pg/connection.rb b/examples/pg/connection.rb index 22ecd81..7b07520 100644 --- a/examples/pg/connection.rb +++ b/examples/pg/connection.rb @@ -14,6 +14,9 @@ def prepared_statements end @prepared_statements end + + # Set inside #transaction: a lost connection is then not reconnected until the transaction block ends. + attr_accessor :in_transaction end ISOLATION_LEVELS = { diff --git a/examples/pg/connection.rbs b/examples/pg/connection.rbs index af2278b..cc10f48 100644 --- a/examples/pg/connection.rbs +++ b/examples/pg/connection.rbs @@ -9,6 +9,8 @@ module PgCodegen @prepared_statements_pid: Integer def prepared_statements: () -> Set[String] + + attr_accessor in_transaction: bool? end ISOLATION_LEVELS: Hash[Symbol, String] diff --git a/examples/pg/query_sql.rb b/examples/pg/query_sql.rb index 44500f7..5aedbdf 100644 --- a/examples/pg/query_sql.rb +++ b/examples/pg/query_sql.rb @@ -574,6 +574,8 @@ def transaction(isolation: nil) else client.exec("SAVEPOINT #{savepoint}") end + entered = !client.in_transaction + client.in_transaction = true rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false begin @@ -581,10 +583,16 @@ def transaction(isolation: nil) rescue Exception # rubocop:disable Lint/RescueException failed = true client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE - client.exec(rollback) + client.exec(rollback) unless client.status == PG::CONNECTION_BAD # the server already rolled back raise ensure - unless failed + client.in_transaction = false if entered + if failed + nil + elsif client.status == PG::CONNECTION_BAD + # the block rescued the error of a lost connection: the server rolled the transaction back + raise PG::ConnectionBad, 'transaction rolled back: the connection was lost' if Thread.current.status != 'aborting' + else # a statement failed and the block rescued the error: PostgreSQL can only roll back rescued = client.transaction_status == PG::PQTRANS_INERROR commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" @@ -1646,7 +1654,12 @@ def instrument(name) def with_client(name) instrument(name) do @db.with do |client| - client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + # inside #transaction a new session would run the rest of the block outside the transaction + raise PG::ConnectionBad, 'the connection was lost inside a transaction' if client.is_a?(ConnectionState) && client.in_transaction + + client.reset + end configure(client) unless client.is_a?(ConnectionState) begin yield client diff --git a/examples/sqlite3/query_sql.rb b/examples/sqlite3/query_sql.rb index e7b69b5..812384d 100644 --- a/examples/sqlite3/query_sql.rb +++ b/examples/sqlite3/query_sql.rb @@ -453,7 +453,7 @@ def get_author(id:) query_params = [id] stmt = client.prepare(GET_AUTHOR_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetAuthorRow.new(row[0], row[1], row[2]) ensure @@ -471,7 +471,7 @@ def list_authors with_client('list_authors') do |client| stmt = client.prepare(LIST_AUTHORS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListAuthorsRow.new(row[0], row[1], row[2]) end @@ -486,7 +486,7 @@ def create_author(name:, bio:) query_params = [name, bio] stmt = client.prepare(CREATE_AUTHOR_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -499,7 +499,7 @@ def delete_author(id:) query_params = [id] stmt = client.prepare(DELETE_AUTHOR_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -512,7 +512,7 @@ def list_authors_paged(offset:, limit:) query_params = [limit, offset] stmt = client.prepare(LIST_AUTHORS_PAGED_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| ListAuthorsPagedRow.new(row[0], row[1], row[2]) end @@ -527,7 +527,7 @@ def get_author_by_name_pattern(name_pattern: nil) query_params = [name_pattern] stmt = client.prepare(GET_AUTHOR_BY_NAME_PATTERN_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorByNamePatternRow.new(row[0], row[1], row[2]) end @@ -542,7 +542,7 @@ def get_authors_by_optional_name(lim:, name_filter: nil) query_params = [name_filter, name_filter, lim] stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByOptionalNameRow.new(row[0], row[1], row[2]) end @@ -557,7 +557,7 @@ def create_author_return_id(name:, bio:) query_params = [name, bio] stmt = client.prepare(CREATE_AUTHOR_RETURN_ID_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && row[0] ensure @@ -572,7 +572,7 @@ def update_author_bios(bio:) query_params = [bio] stmt = client.prepare(UPDATE_AUTHOR_BIOS_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) client.changes ensure stmt.close @@ -586,7 +586,7 @@ def get_authors_by_ids(ids:) sql = GET_AUTHORS_BY_IDS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) stmt = client.prepare(sql) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByIdsRow.new(row[0], row[1], row[2]) end @@ -603,7 +603,7 @@ def get_authors_by_ids_and_names(ids:, names:) sql = sql.gsub('/*SLICE:names*/?', slice_placeholders(names)) stmt = client.prepare(sql) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByIdsAndNamesRow.new(row[0], row[1], row[2]) end @@ -627,7 +627,7 @@ def get_authors_by_mixed_placeholders(n:, ids:, id:, more:) sql = sql.gsub('/*SLICE:more*/?', slice_placeholders(more)) stmt = client.prepare(sql) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByMixedPlaceholdersRow.new(row[0], row[1], row[2]) end @@ -644,7 +644,7 @@ def get_authors_by_repeated_named_and_positional(n:, other:, id:) query_params = [n, other, id, n] stmt = client.prepare(GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByRepeatedNamedAndPositionalRow.new(row[0], row[1], row[2]) end @@ -661,7 +661,7 @@ def get_authors_by_repeated_named_and_positional_batch(rows) begin rows.map do |args| query_params = [args.n, args.other, args.id, args.n] - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetAuthorsByRepeatedNamedAndPositionalBatchRow.new(row[0], row[1], row[2]) end @@ -700,7 +700,7 @@ def truncate_authors with_client('truncate_authors') do |client| stmt = client.prepare(TRUNCATE_AUTHORS_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -713,7 +713,7 @@ def create_book(name:, author_id:) query_params = [name, author_id] stmt = client.prepare(CREATE_BOOK_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && row[0] ensure @@ -726,7 +726,7 @@ def list_all_authors_books with_client('list_all_authors_books') do |client| stmt = client.prepare(LIST_ALL_AUTHORS_BOOKS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListAllAuthorsBooksRow.new( Author.new(row[0], row[1], row[2]), @@ -743,7 +743,7 @@ def get_duplicate_authors with_client('get_duplicate_authors') do |client| stmt = client.prepare(GET_DUPLICATE_AUTHORS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| GetDuplicateAuthorsRow.new(Author.new(row[0], row[1], row[2]), Author.new(row[3], row[4], row[5])) end @@ -758,7 +758,7 @@ def get_authors_by_book_name(name:) query_params = [name] stmt = client.prepare(GET_AUTHORS_BY_BOOK_NAME_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByBookNameRow.new(row[0], row[1], row[2], Book.new(row[3], row[4], row[5], row[6])) end @@ -773,7 +773,7 @@ def create_author_result(name:, bio:) query_params = [name, bio] stmt = client.prepare(CREATE_AUTHOR_RESULT_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) ExecResult.new(client.changes, client.last_insert_row_id) ensure stmt.close @@ -787,7 +787,7 @@ def create_authors_batch(rows) begin rows.each do |args| query_params = [args.name, args.bio] - stmt.execute(*query_params) + execute_statement(stmt, *query_params) end nil ensure @@ -802,7 +802,7 @@ def get_author_batch(rows) begin rows.map do |args| query_params = [args.id] - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetAuthorBatchRow.new(row[0], row[1], row[2]) end @@ -818,7 +818,7 @@ def list_authors_by_name_batch(rows) begin rows.map do |args| query_params = [args.name] - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| ListAuthorsByNameBatchRow.new(row[0], row[1], row[2]) end @@ -842,7 +842,7 @@ def insert_declared_types(c_varchar:, c_decimal:, c_unsigned_big_int:, c_native: ] stmt = client.prepare(INSERT_DECLARED_TYPES_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -854,7 +854,7 @@ def get_declared_types with_client('get_declared_types') do |client| stmt = client.prepare(GET_DECLARED_TYPES_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && GetDeclaredTypesRow.new( row[0], @@ -880,7 +880,7 @@ def truncate_declared_types with_client('truncate_declared_types') do |client| stmt = client.prepare(TRUNCATE_DECLARED_TYPES_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -893,7 +893,7 @@ def create_regression(order:, price:) query_params = [order, price] stmt = client.prepare(CREATE_REGRESSION_SQL) begin - stmt.execute(*query_params).to_a + execute_statement(stmt, *query_params).to_a nil ensure stmt.close @@ -906,7 +906,7 @@ def update_regression_prices(price:) query_params = [price] stmt = client.prepare(UPDATE_REGRESSION_PRICES_SQL) begin - stmt.execute(*query_params).to_a + execute_statement(stmt, *query_params).to_a client.changes ensure stmt.close @@ -930,7 +930,7 @@ def list_regressions with_client('list_regressions') do |client| stmt = client.prepare(LIST_REGRESSIONS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListRegressionsRow.new(row[0], row[1], row[2]) end @@ -944,7 +944,7 @@ def get_max_regression_price with_client('get_max_regression_price') do |client| stmt = client.prepare(GET_MAX_REGRESSION_PRICE_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && GetMaxRegressionPriceRow.new(row[0]) ensure @@ -962,7 +962,7 @@ def truncate_regressions with_client('truncate_regressions') do |client| stmt = client.prepare(TRUNCATE_REGRESSIONS_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -986,7 +986,7 @@ def list_regression_rows with_client('list_regression_rows') do |client| stmt = client.prepare(LIST_REGRESSION_ROWS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListRegressionRowsRow.new(row[0], row[1], row[2], row[3]) end @@ -1000,7 +1000,7 @@ def truncate_regression_rows with_client('truncate_regression_rows') do |client| stmt = client.prepare(TRUNCATE_REGRESSION_ROWS_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -1013,7 +1013,7 @@ def pause_query(sleep:) query_params = [sleep] stmt = client.prepare(PAUSE_QUERY_SQL) begin - stmt.execute(*query_params).to_a + execute_statement(stmt, *query_params).to_a nil ensure stmt.close @@ -1061,7 +1061,7 @@ def list_tags with_client('list_tags') do |client| stmt = client.prepare(LIST_TAGS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListTagsRow.new(row[0], row[1]) end @@ -1075,7 +1075,7 @@ def truncate_tags with_client('truncate_tags') do |client| stmt = client.prepare(TRUNCATE_TAGS_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -1087,7 +1087,7 @@ def list_authors_with_optional_book with_client('list_authors_with_optional_book') do |client| stmt = client.prepare(LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListAuthorsWithOptionalBookRow.new( row[0].nil? && row[1].nil? ? nil : Author.new(row[0], row[1], row[2]), @@ -1114,7 +1114,7 @@ def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, h ] stmt = client.prepare(INSERT_NAME_CASE_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -1136,7 +1136,7 @@ def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, cam ] stmt = client.prepare(GET_NAME_CASE_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetNameCaseRow.new( row[0], @@ -1164,7 +1164,7 @@ def get_http_server(id:) query_params = [id] stmt = client.prepare(GET_HTTP_SERVER_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetHTTPServerRow.new( row[0], @@ -1191,7 +1191,7 @@ def truncate_name_cases with_client('truncate_name_cases') do |client| stmt = client.prepare(TRUNCATE_NAME_CASES_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -1203,7 +1203,7 @@ def get_unnamed_columns with_client('get_unnamed_columns') do |client| stmt = client.prepare(GET_UNNAMED_COLUMNS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && GetUnnamedColumnsRow.new(row[0], row[1]) ensure @@ -1221,7 +1221,7 @@ def get_quoted_alias with_client('get_quoted_alias') do |client| stmt = client.prepare(GET_QUOTED_ALIAS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && GetQuotedAliasRow.new(row[0]) ensure @@ -1241,7 +1241,7 @@ def get_authors_not_in_ids(ids:) sql = GET_AUTHORS_NOT_IN_IDS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) stmt = client.prepare(sql) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsNotInIdsRow.new(row[0], row[1], row[2]) end @@ -1256,7 +1256,7 @@ def create_author_or_rollback(id:, name:) query_params = [id, name] stmt = client.prepare(CREATE_AUTHOR_OR_ROLLBACK_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -1282,6 +1282,14 @@ def with_client(name, &block) instrument(name) { @lock.synchronize { block.call(@db) } } end + def execute_statement(stmt, *params) + stmt.reset! if stmt.active? || stmt.done? + stmt.bind_params(*params) unless params.empty? + result = SQLite3::ResultSet.new(@db, stmt) + stmt.step if stmt.column_count.zero? + result + end + def atomically(client) client.execute('SAVEPOINT sqlc_copy') done = false diff --git a/examples/sqlite3/query_sql.rbs b/examples/sqlite3/query_sql.rbs index 3aa0f02..678d7a9 100644 --- a/examples/sqlite3/query_sql.rbs +++ b/examples/sqlite3/query_sql.rbs @@ -535,6 +535,8 @@ module Sqlite3Codegen def with_client: [T] (String name) { (untyped) -> T } -> T + def execute_statement: (untyped stmt, *untyped params) -> untyped + def atomically: (untyped client) { () -> void } -> void def slice_placeholders: (Array[untyped] values) -> String diff --git a/lib/sqlc_gen_ruby/drivers/mysql2.rb b/lib/sqlc_gen_ruby/drivers/mysql2.rb index e795aaf..ed54be4 100644 --- a/lib/sqlc_gen_ruby/drivers/mysql2.rb +++ b/lib/sqlc_gen_ruby/drivers/mysql2.rb @@ -161,8 +161,9 @@ def transaction(isolation: nil) begin client.query(rollback) rescue Mysql2::Error - # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone - raise unless e.is_a?(Mysql2::Error) + # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone; + # an interrupted query closed the connection: the server rolls back + raise unless e.is_a?(Mysql2::Error) || client.closed? end raise ensure @@ -186,8 +187,9 @@ def with_client(name) client.extend(ConnectionState) unless client.is_a?(ConnectionState) begin yield client - rescue Mysql2::Error - # a connection closed by the server (restart, KILL, wait_timeout) is replaced by the pool + rescue Exception # rubocop:disable Lint/RescueException + # a connection closed by the server (restart, KILL, wait_timeout) or by mysql2 when a running query + # was interrupted (Async task stopped, Thread#raise) is replaced by the pool @db.discard_current_connection if client.closed? && @db.respond_to?(:discard_current_connection) raise end diff --git a/lib/sqlc_gen_ruby/drivers/pg.rb b/lib/sqlc_gen_ruby/drivers/pg.rb index 0909d9e..3abd598 100644 --- a/lib/sqlc_gen_ruby/drivers/pg.rb +++ b/lib/sqlc_gen_ruby/drivers/pg.rb @@ -171,6 +171,9 @@ def prepared_statements end @prepared_statements end + + # Set inside #transaction: a lost connection is then not reconnected until the transaction block ends. + attr_accessor :in_transaction end RUBY @@ -184,6 +187,8 @@ module ConnectionState : _PgConnection @prepared_statements_pid: Integer def prepared_statements: () -> Set[String] + + attr_accessor in_transaction: bool? end RBS @@ -219,6 +224,8 @@ def transaction(isolation: nil) else client.exec("SAVEPOINT #{savepoint}") end + entered = !client.in_transaction + client.in_transaction = true rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false begin @@ -226,10 +233,16 @@ def transaction(isolation: nil) rescue Exception # rubocop:disable Lint/RescueException failed = true client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE - client.exec(rollback) + client.exec(rollback) unless client.status == PG::CONNECTION_BAD # the server already rolled back raise ensure - unless failed + client.in_transaction = false if entered + if failed + nil + elsif client.status == PG::CONNECTION_BAD + # the block rescued the error of a lost connection: the server rolled the transaction back + raise PG::ConnectionBad, 'transaction rolled back: the connection was lost' if Thread.current.status != 'aborting' + else # a statement failed and the block rescued the error: PostgreSQL can only roll back rescued = client.transaction_status == PG::PQTRANS_INERROR commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" @@ -246,7 +259,12 @@ def transaction(isolation: nil) def with_client(name) instrument(name) do @db.with do |client| - client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + # inside #transaction a new session would run the rest of the block outside the transaction + raise PG::ConnectionBad, 'the connection was lost inside a transaction' if client.is_a?(ConnectionState) && client.in_transaction + + client.reset + end configure(client) unless client.is_a?(ConnectionState) begin yield client diff --git a/lib/sqlc_gen_ruby/drivers/sqlite3.rb b/lib/sqlc_gen_ruby/drivers/sqlite3.rb index 9722a92..f13d535 100644 --- a/lib/sqlc_gen_ruby/drivers/sqlite3.rb +++ b/lib/sqlc_gen_ruby/drivers/sqlite3.rb @@ -40,6 +40,7 @@ def private_methods(ruby_version) [INSTRUMENT, INSTRUMENT_RBS], ["def with_client(name, &block)\n instrument(name) { @lock.synchronize { block.call(@db) } }\nend", 'def with_client: [T] (String name) { (untyped) -> T } -> T'], + [EXECUTE_STATEMENT, 'def execute_statement: (untyped stmt, *untyped params) -> untyped'], *timeout_exit_methods(ruby_version) ] end @@ -47,15 +48,58 @@ def private_methods(ruby_version) def prepare_stmt(_stmt_name, const) = "stmt = client.prepare(#{const})" def execute_stmt(_stmt_name, _const, has_params, array_rows: false) - has_params ? 'stmt.execute(*query_params)' : 'stmt.execute' + has_params ? 'execute_statement(stmt, *query_params)' : 'execute_statement(stmt)' end + # Statement#execute, but rows are always Arrays (they are read by position), also when the database was + # opened with results_as_hash: true. + EXECUTE_STATEMENT = <<~'RUBY'.chomp + def execute_statement(stmt, *params) + stmt.reset! if stmt.active? || stmt.done? + stmt.bind_params(*params) unless params.empty? + result = SQLite3::ResultSet.new(@db, stmt) + stmt.step if stmt.column_count.zero? + result + end + RUBY + def row_data_type = :array def closes_statements? = true def supports_slice? = true + # sqlc misses parameters in some SQLite clauses (ON CONFLICT ... DO UPDATE SET): it leaves sqlc.arg(...) in the + # SQL or reports fewer parameters than there are placeholders, and the method would bind too few values. + def check_placeholders(query) + hint = 'sqlc does not see parameters there (e.g. in SQLite ON CONFLICT ... DO UPDATE SET); ' \ + 'use excluded. or move the value into VALUES' + if /\bsqlc\s*\.\s*(?:arg|narg|slice|embed)\s*\(/i.match?(without_literals(query.text)) + raise ArgumentError, "#{query.name}: sqlc left a sqlc.* macro in the SQL: #{hint}" + end + + occurrences = placeholder_occurrences(query.text) + referenced = occurrences.filter_map { |kind, number,| number if kind == :numbered } + slices = occurrences.filter_map { |kind, name,| name if kind == :slice } + expected = query.params.count do |source| + !referenced.include?(source.number) && !(source.column.is_sqlc_slice && slices.include?(source.column.name)) + end + unreported = [occurrences.count { |kind,| kind == :positional } - expected, 0].max + + (referenced.uniq - query.params.map(&:number)).size + return if unreported.zero? + + raise ArgumentError, "#{query.name}: the SQL has #{unreported} placeholder(s) that sqlc reported no parameter for: #{hint}" + end + + def without_literals(sql) + sql.gsub(/'(?:[^']|'')*'|"(?:[^"]|"")*"|`(?:[^`]|``)*`|\[[^\]]*\]|--[^\n]*|\/\*.*?\*\//m, ' ') + end + + def query_text_constant(query) + check_placeholders(query) + super + end + def query_text(query) replacements = placeholder_occurrences(query.text).filter_map do |kind, number, first, last| [first, last] if kind == :numbered diff --git a/steep_expectations.yml b/steep_expectations.yml index bb17196..ef68e07 100644 --- a/steep_expectations.yml +++ b/steep_expectations.yml @@ -43,30 +43,20 @@ code: Ruby::UnknownConstant - range: start: - line: 507 + line: 508 character: 33 end: - line: 507 + line: 508 character: 39 severity: WARNING message: 'Cannot find the declaration of constant: `Mysql2`' code: Ruby::UnknownConstant - range: start: - line: 1230 - character: 17 - end: - line: 1230 - character: 23 - severity: WARNING - message: 'Cannot find the declaration of constant: `Mysql2`' - code: Ruby::UnknownConstant - - range: - start: - line: 1293 + line: 1295 character: 20 end: - line: 1293 + line: 1295 character: 38 severity: ERROR message: |- @@ -79,20 +69,20 @@ code: Ruby::ArgumentTypeMismatch - range: start: - line: 1294 + line: 1296 character: 78 end: - line: 1294 + line: 1296 character: 84 severity: ERROR message: Type `(::String | nil)` does not have method `match?` code: Ruby::NoMethod - range: start: - line: 1296 + line: 1298 character: 20 end: - line: 1296 + line: 1298 character: 39 severity: ERROR message: |- @@ -105,10 +95,10 @@ code: Ruby::ArgumentTypeMismatch - range: start: - line: 1300 + line: 1302 character: 20 end: - line: 1300 + line: 1302 character: 39 severity: ERROR message: |- @@ -121,10 +111,10 @@ code: Ruby::ArgumentTypeMismatch - range: start: - line: 1305 + line: 1307 character: 20 end: - line: 1305 + line: 1307 character: 39 severity: ERROR message: |- @@ -137,10 +127,10 @@ code: Ruby::ArgumentTypeMismatch - range: start: - line: 1314 + line: 1316 character: 20 end: - line: 1314 + line: 1316 character: 24 severity: ERROR message: |- @@ -153,10 +143,10 @@ code: Ruby::ArgumentTypeMismatch - range: start: - line: 1347 + line: 1349 character: 16 end: - line: 1347 + line: 1349 character: 22 severity: WARNING message: 'Cannot find the declaration of constant: `Mysql2`' @@ -205,241 +195,293 @@ code: Ruby::UnknownConstant - range: start: - line: 583 + line: 585 + character: 56 + end: + line: 585 + character: 58 + severity: WARNING + message: 'Cannot find the declaration of constant: `PG`' + code: Ruby::UnknownConstant + - range: + start: + line: 586 character: 56 end: - line: 583 + line: 586 character: 58 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 589 + line: 592 + character: 33 + end: + line: 592 + character: 35 + severity: WARNING + message: 'Cannot find the declaration of constant: `PG`' + code: Ruby::UnknownConstant + - range: + start: + line: 594 + character: 18 + end: + line: 594 + character: 20 + severity: WARNING + message: 'Cannot find the declaration of constant: `PG`' + code: Ruby::UnknownConstant + - range: + start: + line: 597 character: 51 end: - line: 589 + line: 597 character: 53 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 593 + line: 601 character: 18 end: - line: 593 + line: 601 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 864 + line: 872 character: 18 end: - line: 864 + line: 872 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1361 + line: 1369 character: 18 end: - line: 1361 + line: 1369 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1442 + line: 1450 character: 18 end: - line: 1442 + line: 1450 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1649 - character: 43 + line: 1657 + character: 30 end: - line: 1649 - character: 45 + line: 1657 + character: 32 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1655 + line: 1659 + character: 18 + end: + line: 1659 + character: 20 + severity: WARNING + message: 'Cannot find the declaration of constant: `PG`' + code: Ruby::UnknownConstant + - range: + start: + line: 1668 character: 58 end: - line: 1655 + line: 1668 character: 60 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1664 + line: 1677 character: 17 end: - line: 1664 + line: 1677 character: 19 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1667 + line: 1680 character: 16 end: - line: 1667 + line: 1680 character: 18 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1669 + line: 1682 character: 49 end: - line: 1669 + line: 1682 character: 51 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1676 + line: 1689 character: 16 end: - line: 1676 + line: 1689 character: 18 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1677 + line: 1690 character: 15 end: - line: 1677 + line: 1690 character: 17 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1678 + line: 1691 character: 79 end: - line: 1678 + line: 1691 character: 81 severity: WARNING message: Empty array doesn't have type annotation code: Ruby::UnannotatedEmptyCollection - range: start: - line: 1679 + line: 1692 character: 25 end: - line: 1679 + line: 1692 character: 27 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1680 + line: 1693 character: 67 end: - line: 1680 + line: 1693 character: 69 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1688 + line: 1701 character: 15 end: - line: 1688 + line: 1701 character: 17 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1695 + line: 1708 character: 45 end: - line: 1695 + line: 1708 character: 47 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1711 + line: 1724 character: 6 end: - line: 1711 + line: 1724 character: 8 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1711 + line: 1724 character: 48 end: - line: 1711 + line: 1724 character: 50 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1718 + line: 1731 character: 40 end: - line: 1718 + line: 1731 character: 42 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1719 + line: 1732 character: 23 end: - line: 1719 + line: 1732 character: 25 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1720 + line: 1733 character: 42 end: - line: 1720 + line: 1733 character: 44 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1721 + line: 1734 character: 23 end: - line: 1721 + line: 1734 character: 25 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant +- file: examples/sqlite3/query_sql.rb + diagnostics: + - range: + start: + line: 1288 + character: 15 + end: + line: 1288 + character: 22 + severity: WARNING + message: 'Cannot find the declaration of constant: `SQLite3`' + code: Ruby::UnknownConstant diff --git a/test/features_test.rb b/test/features_test.rb index 798fa30..831e987 100644 --- a/test/features_test.rb +++ b/test/features_test.rb @@ -164,6 +164,27 @@ def test_pg_and_sqlite_execlastid_need_returning end end + def test_sqlite_parameters_sqlc_does_not_report_are_generation_errors + { + 'ON CONFLICT (id) DO UPDATE SET name = ? RETURNING' => /CreateBook: the SQL has 1 placeholder\(s\) that sqlc reported no parameter for/, + 'ON CONFLICT (id) DO UPDATE SET name = sqlc.arg(name) RETURNING' => /CreateBook: sqlc left a sqlc\.\* macro in the SQL/ + }.each do |clause, message| + request = load_request('sqlite3') + book = query(request, 'CreateBook') + book.text = book.text.sub('RETURNING', clause) + error = assert_raises(RuntimeError) { generate(request) } + assert_match message, error.message + assert_match(/excluded\./, error.message) + end + end + + def test_sqlite_placeholders_in_literals_and_comments_are_not_parameters + request = load_request('sqlite3') + book = query(request, 'CreateBook') + book.text = book.text.sub('RETURNING', "ON CONFLICT (id) DO UPDATE SET name = '? sqlc.arg(x)' /* ? */ RETURNING") + assert_includes generate(request).fetch('query_sql.rb'), "name = '? sqlc.arg(x)' /* ? */" + end + def test_returning_last_id_is_optional_when_no_row_is_returned %w[pg sqlite3].each do |driver| rbs = generate(load_request(driver)).fetch('query_sql.rbs') diff --git a/test/golden/mysql2/default/my_queries_sql.rb b/test/golden/mysql2/default/my_queries_sql.rb index bbafea7..37ee840 100644 --- a/test/golden/mysql2/default/my_queries_sql.rb +++ b/test/golden/mysql2/default/my_queries_sql.rb @@ -56,8 +56,9 @@ def transaction(isolation: nil) begin client.query(rollback) rescue Mysql2::Error - # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone - raise unless e.is_a?(Mysql2::Error) + # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone; + # an interrupted query closed the connection: the server rolls back + raise unless e.is_a?(Mysql2::Error) || client.closed? end raise ensure @@ -101,8 +102,9 @@ def with_client(name) client.extend(ConnectionState) unless client.is_a?(ConnectionState) begin yield client - rescue Mysql2::Error - # a connection closed by the server (restart, KILL, wait_timeout) is replaced by the pool + rescue Exception # rubocop:disable Lint/RescueException + # a connection closed by the server (restart, KILL, wait_timeout) or by mysql2 when a running query + # was interrupted (Async task stopped, Thread#raise) is replaced by the pool @db.discard_current_connection if client.closed? && @db.respond_to?(:discard_current_connection) raise end diff --git a/test/golden/mysql2/default/other_sql.rb b/test/golden/mysql2/default/other_sql.rb index 9c65531..b10b920 100644 --- a/test/golden/mysql2/default/other_sql.rb +++ b/test/golden/mysql2/default/other_sql.rb @@ -16,7 +16,7 @@ module Mysql2Codegen OTHER_ONE_SQL = <<~'SQL' SELECT 1 -- ? ? - WHERE a = ? + WHERE a = 1 SQL class OtherOneRow < Data.define(:c0) @@ -69,8 +69,9 @@ def transaction(isolation: nil) begin client.query(rollback) rescue Mysql2::Error - # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone - raise unless e.is_a?(Mysql2::Error) + # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone; + # an interrupted query closed the connection: the server rolls back + raise unless e.is_a?(Mysql2::Error) || client.closed? end raise ensure @@ -141,8 +142,9 @@ def with_client(name) client.extend(ConnectionState) unless client.is_a?(ConnectionState) begin yield client - rescue Mysql2::Error - # a connection closed by the server (restart, KILL, wait_timeout) is replaced by the pool + rescue Exception # rubocop:disable Lint/RescueException + # a connection closed by the server (restart, KILL, wait_timeout) or by mysql2 when a running query + # was interrupted (Async task stopped, Thread#raise) is replaced by the pool @db.discard_current_connection if client.closed? && @db.respond_to?(:discard_current_connection) raise end diff --git a/test/golden/mysql2/default/query_sql.rb b/test/golden/mysql2/default/query_sql.rb index a4df2c2..33382ca 100644 --- a/test/golden/mysql2/default/query_sql.rb +++ b/test/golden/mysql2/default/query_sql.rb @@ -485,7 +485,7 @@ class ManyRowsRow < Data.define( FOUR_COLS_SQL = <<~'SQL' SELECT 1 -- ? ? - WHERE a = ? + WHERE a = 1 SQL class FourColsRow < Data.define(:c0, :c1, :c2, :c3) @@ -533,8 +533,9 @@ def transaction(isolation: nil) begin client.query(rollback) rescue Mysql2::Error - # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone - raise unless e.is_a?(Mysql2::Error) + # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone; + # an interrupted query closed the connection: the server rolls back + raise unless e.is_a?(Mysql2::Error) || client.closed? end raise ensure @@ -1302,8 +1303,9 @@ def with_client(name) client.extend(ConnectionState) unless client.is_a?(ConnectionState) begin yield client - rescue Mysql2::Error - # a connection closed by the server (restart, KILL, wait_timeout) is replaced by the pool + rescue Exception # rubocop:disable Lint/RescueException + # a connection closed by the server (restart, KILL, wait_timeout) or by mysql2 when a running query + # was interrupted (Async task stopped, Thread#raise) is replaced by the pool @db.discard_current_connection if client.closed? && @db.respond_to?(:discard_current_connection) raise end diff --git a/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rb b/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rb index 4dcead1..6853f1a 100644 --- a/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rb +++ b/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rb @@ -59,8 +59,9 @@ def transaction(isolation: nil) begin client.query(rollback) rescue Mysql2::Error - # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone - raise unless e.is_a?(Mysql2::Error) + # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone; + # an interrupted query closed the connection: the server rolls back + raise unless e.is_a?(Mysql2::Error) || client.closed? end raise ensure @@ -104,8 +105,9 @@ def with_client(name) client.extend(ConnectionState) unless client.is_a?(ConnectionState) begin yield client - rescue Mysql2::Error - # a connection closed by the server (restart, KILL, wait_timeout) is replaced by the pool + rescue Exception # rubocop:disable Lint/RescueException + # a connection closed by the server (restart, KILL, wait_timeout) or by mysql2 when a running query + # was interrupted (Async task stopped, Thread#raise) is replaced by the pool @db.discard_current_connection if client.closed? && @db.respond_to?(:discard_current_connection) raise end diff --git a/test/golden/mysql2/ruby31_gemfile/other_sql.rb b/test/golden/mysql2/ruby31_gemfile/other_sql.rb index 1cfa51c..e09bc25 100644 --- a/test/golden/mysql2/ruby31_gemfile/other_sql.rb +++ b/test/golden/mysql2/ruby31_gemfile/other_sql.rb @@ -16,7 +16,7 @@ module Mysql2Codegen OTHER_ONE_SQL = <<~'SQL' SELECT 1 -- ? ? - WHERE a = ? + WHERE a = 1 SQL OtherOneRow = Struct.new(:c0, keyword_init: true) @@ -71,8 +71,9 @@ def transaction(isolation: nil) begin client.query(rollback) rescue Mysql2::Error - # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone - raise unless e.is_a?(Mysql2::Error) + # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone; + # an interrupted query closed the connection: the server rolls back + raise unless e.is_a?(Mysql2::Error) || client.closed? end raise ensure @@ -143,8 +144,9 @@ def with_client(name) client.extend(ConnectionState) unless client.is_a?(ConnectionState) begin yield client - rescue Mysql2::Error - # a connection closed by the server (restart, KILL, wait_timeout) is replaced by the pool + rescue Exception # rubocop:disable Lint/RescueException + # a connection closed by the server (restart, KILL, wait_timeout) or by mysql2 when a running query + # was interrupted (Async task stopped, Thread#raise) is replaced by the pool @db.discard_current_connection if client.closed? && @db.respond_to?(:discard_current_connection) raise end diff --git a/test/golden/mysql2/ruby31_gemfile/query_sql.rb b/test/golden/mysql2/ruby31_gemfile/query_sql.rb index 595f4fd..2a24f5e 100644 --- a/test/golden/mysql2/ruby31_gemfile/query_sql.rb +++ b/test/golden/mysql2/ruby31_gemfile/query_sql.rb @@ -479,7 +479,7 @@ module Mysql2Codegen FOUR_COLS_SQL = <<~'SQL' SELECT 1 -- ? ? - WHERE a = ? + WHERE a = 1 SQL FourColsRow = Struct.new( @@ -535,8 +535,9 @@ def transaction(isolation: nil) begin client.query(rollback) rescue Mysql2::Error - # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone - raise unless e.is_a?(Mysql2::Error) + # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone; + # an interrupted query closed the connection: the server rolls back + raise unless e.is_a?(Mysql2::Error) || client.closed? end raise ensure @@ -1316,8 +1317,9 @@ def with_client(name) client.extend(ConnectionState) unless client.is_a?(ConnectionState) begin yield client - rescue Mysql2::Error - # a connection closed by the server (restart, KILL, wait_timeout) is replaced by the pool + rescue Exception # rubocop:disable Lint/RescueException + # a connection closed by the server (restart, KILL, wait_timeout) or by mysql2 when a running query + # was interrupted (Async task stopped, Thread#raise) is replaced by the pool @db.discard_current_connection if client.closed? && @db.respond_to?(:discard_current_connection) raise end diff --git a/test/golden/mysql2/ruby32_notypes/my_queries_sql.rb b/test/golden/mysql2/ruby32_notypes/my_queries_sql.rb index 4dcead1..6853f1a 100644 --- a/test/golden/mysql2/ruby32_notypes/my_queries_sql.rb +++ b/test/golden/mysql2/ruby32_notypes/my_queries_sql.rb @@ -59,8 +59,9 @@ def transaction(isolation: nil) begin client.query(rollback) rescue Mysql2::Error - # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone - raise unless e.is_a?(Mysql2::Error) + # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone; + # an interrupted query closed the connection: the server rolls back + raise unless e.is_a?(Mysql2::Error) || client.closed? end raise ensure @@ -104,8 +105,9 @@ def with_client(name) client.extend(ConnectionState) unless client.is_a?(ConnectionState) begin yield client - rescue Mysql2::Error - # a connection closed by the server (restart, KILL, wait_timeout) is replaced by the pool + rescue Exception # rubocop:disable Lint/RescueException + # a connection closed by the server (restart, KILL, wait_timeout) or by mysql2 when a running query + # was interrupted (Async task stopped, Thread#raise) is replaced by the pool @db.discard_current_connection if client.closed? && @db.respond_to?(:discard_current_connection) raise end diff --git a/test/golden/mysql2/ruby32_notypes/other_sql.rb b/test/golden/mysql2/ruby32_notypes/other_sql.rb index 803d65c..4f8347d 100644 --- a/test/golden/mysql2/ruby32_notypes/other_sql.rb +++ b/test/golden/mysql2/ruby32_notypes/other_sql.rb @@ -16,7 +16,7 @@ module Mysql2Codegen OTHER_ONE_SQL = <<~'SQL' SELECT 1 -- ? ? - WHERE a = ? + WHERE a = 1 SQL class OtherOneRow < Data.define(:c0) @@ -72,8 +72,9 @@ def transaction(isolation: nil) begin client.query(rollback) rescue Mysql2::Error - # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone - raise unless e.is_a?(Mysql2::Error) + # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone; + # an interrupted query closed the connection: the server rolls back + raise unless e.is_a?(Mysql2::Error) || client.closed? end raise ensure @@ -144,8 +145,9 @@ def with_client(name) client.extend(ConnectionState) unless client.is_a?(ConnectionState) begin yield client - rescue Mysql2::Error - # a connection closed by the server (restart, KILL, wait_timeout) is replaced by the pool + rescue Exception # rubocop:disable Lint/RescueException + # a connection closed by the server (restart, KILL, wait_timeout) or by mysql2 when a running query + # was interrupted (Async task stopped, Thread#raise) is replaced by the pool @db.discard_current_connection if client.closed? && @db.respond_to?(:discard_current_connection) raise end diff --git a/test/golden/mysql2/ruby32_notypes/query_sql.rb b/test/golden/mysql2/ruby32_notypes/query_sql.rb index 557e3d6..68d7d4c 100644 --- a/test/golden/mysql2/ruby32_notypes/query_sql.rb +++ b/test/golden/mysql2/ruby32_notypes/query_sql.rb @@ -485,7 +485,7 @@ class ManyRowsRow < Data.define( FOUR_COLS_SQL = <<~'SQL' SELECT 1 -- ? ? - WHERE a = ? + WHERE a = 1 SQL class FourColsRow < Data.define(:c0, :c1, :c2, :c3) @@ -536,8 +536,9 @@ def transaction(isolation: nil) begin client.query(rollback) rescue Mysql2::Error - # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone - raise unless e.is_a?(Mysql2::Error) + # a deadlock / lock wait timeout already rolled back the transaction: its savepoints are gone; + # an interrupted query closed the connection: the server rolls back + raise unless e.is_a?(Mysql2::Error) || client.closed? end raise ensure @@ -1305,8 +1306,9 @@ def with_client(name) client.extend(ConnectionState) unless client.is_a?(ConnectionState) begin yield client - rescue Mysql2::Error - # a connection closed by the server (restart, KILL, wait_timeout) is replaced by the pool + rescue Exception # rubocop:disable Lint/RescueException + # a connection closed by the server (restart, KILL, wait_timeout) or by mysql2 when a running query + # was interrupted (Async task stopped, Thread#raise) is replaced by the pool @db.discard_current_connection if client.closed? && @db.respond_to?(:discard_current_connection) raise end diff --git a/test/golden/pg/default/connection.rb b/test/golden/pg/default/connection.rb index 22ecd81..7b07520 100644 --- a/test/golden/pg/default/connection.rb +++ b/test/golden/pg/default/connection.rb @@ -14,6 +14,9 @@ def prepared_statements end @prepared_statements end + + # Set inside #transaction: a lost connection is then not reconnected until the transaction block ends. + attr_accessor :in_transaction end ISOLATION_LEVELS = { diff --git a/test/golden/pg/default/connection.rbs b/test/golden/pg/default/connection.rbs index af2278b..cc10f48 100644 --- a/test/golden/pg/default/connection.rbs +++ b/test/golden/pg/default/connection.rbs @@ -9,6 +9,8 @@ module PgCodegen @prepared_statements_pid: Integer def prepared_statements: () -> Set[String] + + attr_accessor in_transaction: bool? end ISOLATION_LEVELS: Hash[Symbol, String] diff --git a/test/golden/pg/default/my_queries_sql.rb b/test/golden/pg/default/my_queries_sql.rb index d5c9b11..ccd8747 100644 --- a/test/golden/pg/default/my_queries_sql.rb +++ b/test/golden/pg/default/my_queries_sql.rb @@ -43,6 +43,8 @@ def transaction(isolation: nil) else client.exec("SAVEPOINT #{savepoint}") end + entered = !client.in_transaction + client.in_transaction = true rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false begin @@ -50,10 +52,16 @@ def transaction(isolation: nil) rescue Exception # rubocop:disable Lint/RescueException failed = true client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE - client.exec(rollback) + client.exec(rollback) unless client.status == PG::CONNECTION_BAD # the server already rolled back raise ensure - unless failed + client.in_transaction = false if entered + if failed + nil + elsif client.status == PG::CONNECTION_BAD + # the block rescued the error of a lost connection: the server rolled the transaction back + raise PG::ConnectionBad, 'transaction rolled back: the connection was lost' if Thread.current.status != 'aborting' + else # a statement failed and the block rescued the error: PostgreSQL can only roll back rescued = client.transaction_status == PG::PQTRANS_INERROR commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" @@ -94,7 +102,12 @@ def instrument(name) def with_client(name) instrument(name) do @db.with do |client| - client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + # inside #transaction a new session would run the rest of the block outside the transaction + raise PG::ConnectionBad, 'the connection was lost inside a transaction' if client.is_a?(ConnectionState) && client.in_transaction + + client.reset + end configure(client) unless client.is_a?(ConnectionState) begin yield client diff --git a/test/golden/pg/default/other_sql.rb b/test/golden/pg/default/other_sql.rb index 350c52d..4bca026 100644 --- a/test/golden/pg/default/other_sql.rb +++ b/test/golden/pg/default/other_sql.rb @@ -14,7 +14,7 @@ module PgCodegen OTHER_ONE_SQL = <<~'SQL' SELECT 1 -- ? ? - WHERE a = ? + WHERE a = 1 SQL class OtherOneRow < Data.define(:c0) @@ -56,6 +56,8 @@ def transaction(isolation: nil) else client.exec("SAVEPOINT #{savepoint}") end + entered = !client.in_transaction + client.in_transaction = true rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false begin @@ -63,10 +65,16 @@ def transaction(isolation: nil) rescue Exception # rubocop:disable Lint/RescueException failed = true client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE - client.exec(rollback) + client.exec(rollback) unless client.status == PG::CONNECTION_BAD # the server already rolled back raise ensure - unless failed + client.in_transaction = false if entered + if failed + nil + elsif client.status == PG::CONNECTION_BAD + # the block rescued the error of a lost connection: the server rolled the transaction back + raise PG::ConnectionBad, 'transaction rolled back: the connection was lost' if Thread.current.status != 'aborting' + else # a statement failed and the block rescued the error: PostgreSQL can only roll back rescued = client.transaction_status == PG::PQTRANS_INERROR commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" @@ -142,7 +150,12 @@ def instrument(name) def with_client(name) instrument(name) do @db.with do |client| - client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + # inside #transaction a new session would run the rest of the block outside the transaction + raise PG::ConnectionBad, 'the connection was lost inside a transaction' if client.is_a?(ConnectionState) && client.in_transaction + + client.reset + end configure(client) unless client.is_a?(ConnectionState) begin yield client diff --git a/test/golden/pg/default/query_sql.rb b/test/golden/pg/default/query_sql.rb index 6d73997..ec76709 100644 --- a/test/golden/pg/default/query_sql.rb +++ b/test/golden/pg/default/query_sql.rb @@ -567,7 +567,7 @@ class ManyRowsRow < Data.define( FOUR_COLS_SQL = <<~'SQL' SELECT 1 -- ? ? - WHERE a = ? + WHERE a = 1 SQL class FourColsRow < Data.define(:c0, :c1, :c2, :c3) @@ -604,6 +604,8 @@ def transaction(isolation: nil) else client.exec("SAVEPOINT #{savepoint}") end + entered = !client.in_transaction + client.in_transaction = true rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false begin @@ -611,10 +613,16 @@ def transaction(isolation: nil) rescue Exception # rubocop:disable Lint/RescueException failed = true client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE - client.exec(rollback) + client.exec(rollback) unless client.status == PG::CONNECTION_BAD # the server already rolled back raise ensure - unless failed + client.in_transaction = false if entered + if failed + nil + elsif client.status == PG::CONNECTION_BAD + # the block rescued the error of a lost connection: the server rolled the transaction back + raise PG::ConnectionBad, 'transaction rolled back: the connection was lost' if Thread.current.status != 'aborting' + else # a statement failed and the block rescued the error: PostgreSQL can only roll back rescued = client.transaction_status == PG::PQTRANS_INERROR commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" @@ -1733,7 +1741,12 @@ def instrument(name) def with_client(name) instrument(name) do @db.with do |client| - client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + # inside #transaction a new session would run the rest of the block outside the transaction + raise PG::ConnectionBad, 'the connection was lost inside a transaction' if client.is_a?(ConnectionState) && client.in_transaction + + client.reset + end configure(client) unless client.is_a?(ConnectionState) begin yield client diff --git a/test/golden/pg/ruby31_gemfile/connection.rb b/test/golden/pg/ruby31_gemfile/connection.rb index 22ecd81..7b07520 100644 --- a/test/golden/pg/ruby31_gemfile/connection.rb +++ b/test/golden/pg/ruby31_gemfile/connection.rb @@ -14,6 +14,9 @@ def prepared_statements end @prepared_statements end + + # Set inside #transaction: a lost connection is then not reconnected until the transaction block ends. + attr_accessor :in_transaction end ISOLATION_LEVELS = { diff --git a/test/golden/pg/ruby31_gemfile/connection.rbs b/test/golden/pg/ruby31_gemfile/connection.rbs index af2278b..cc10f48 100644 --- a/test/golden/pg/ruby31_gemfile/connection.rbs +++ b/test/golden/pg/ruby31_gemfile/connection.rbs @@ -9,6 +9,8 @@ module PgCodegen @prepared_statements_pid: Integer def prepared_statements: () -> Set[String] + + attr_accessor in_transaction: bool? end ISOLATION_LEVELS: Hash[Symbol, String] diff --git a/test/golden/pg/ruby31_gemfile/my_queries_sql.rb b/test/golden/pg/ruby31_gemfile/my_queries_sql.rb index 416d24d..8d6bb0c 100644 --- a/test/golden/pg/ruby31_gemfile/my_queries_sql.rb +++ b/test/golden/pg/ruby31_gemfile/my_queries_sql.rb @@ -44,6 +44,8 @@ def transaction(isolation: nil) else client.exec("SAVEPOINT #{savepoint}") end + entered = !client.in_transaction + client.in_transaction = true rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false completed = false @@ -54,10 +56,16 @@ def transaction(isolation: nil) rescue Exception # rubocop:disable Lint/RescueException failed = true client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE - client.exec(rollback) + client.exec(rollback) unless client.status == PG::CONNECTION_BAD # the server already rolled back raise ensure - unless failed + client.in_transaction = false if entered + if failed + nil + elsif client.status == PG::CONNECTION_BAD + # the block rescued the error of a lost connection: the server rolled the transaction back + raise PG::ConnectionBad, 'transaction rolled back: the connection was lost' if Thread.current.status != 'aborting' + else # a statement failed and the block rescued the error: PostgreSQL can only roll back rescued = client.transaction_status == PG::PQTRANS_INERROR commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" @@ -98,7 +106,12 @@ def instrument(name) def with_client(name) instrument(name) do @db.with do |client| - client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + # inside #transaction a new session would run the rest of the block outside the transaction + raise PG::ConnectionBad, 'the connection was lost inside a transaction' if client.is_a?(ConnectionState) && client.in_transaction + + client.reset + end configure(client) unless client.is_a?(ConnectionState) begin yield client diff --git a/test/golden/pg/ruby31_gemfile/other_sql.rb b/test/golden/pg/ruby31_gemfile/other_sql.rb index ca7c0ad..c45a09e 100644 --- a/test/golden/pg/ruby31_gemfile/other_sql.rb +++ b/test/golden/pg/ruby31_gemfile/other_sql.rb @@ -15,7 +15,7 @@ module PgCodegen OTHER_ONE_SQL = <<~'SQL' SELECT 1 -- ? ? - WHERE a = ? + WHERE a = 1 SQL OtherOneRow = Struct.new(:c0, keyword_init: true) @@ -56,6 +56,8 @@ def transaction(isolation: nil) else client.exec("SAVEPOINT #{savepoint}") end + entered = !client.in_transaction + client.in_transaction = true rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false completed = false @@ -66,10 +68,16 @@ def transaction(isolation: nil) rescue Exception # rubocop:disable Lint/RescueException failed = true client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE - client.exec(rollback) + client.exec(rollback) unless client.status == PG::CONNECTION_BAD # the server already rolled back raise ensure - unless failed + client.in_transaction = false if entered + if failed + nil + elsif client.status == PG::CONNECTION_BAD + # the block rescued the error of a lost connection: the server rolled the transaction back + raise PG::ConnectionBad, 'transaction rolled back: the connection was lost' if Thread.current.status != 'aborting' + else # a statement failed and the block rescued the error: PostgreSQL can only roll back rescued = client.transaction_status == PG::PQTRANS_INERROR commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" @@ -145,7 +153,12 @@ def instrument(name) def with_client(name) instrument(name) do @db.with do |client| - client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + # inside #transaction a new session would run the rest of the block outside the transaction + raise PG::ConnectionBad, 'the connection was lost inside a transaction' if client.is_a?(ConnectionState) && client.in_transaction + + client.reset + end configure(client) unless client.is_a?(ConnectionState) begin yield client diff --git a/test/golden/pg/ruby31_gemfile/query_sql.rb b/test/golden/pg/ruby31_gemfile/query_sql.rb index 12b9924..a625c21 100644 --- a/test/golden/pg/ruby31_gemfile/query_sql.rb +++ b/test/golden/pg/ruby31_gemfile/query_sql.rb @@ -554,7 +554,7 @@ module PgCodegen FOUR_COLS_SQL = <<~'SQL' SELECT 1 -- ? ? - WHERE a = ? + WHERE a = 1 SQL FourColsRow = Struct.new( @@ -596,6 +596,8 @@ def transaction(isolation: nil) else client.exec("SAVEPOINT #{savepoint}") end + entered = !client.in_transaction + client.in_transaction = true rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false completed = false @@ -606,10 +608,16 @@ def transaction(isolation: nil) rescue Exception # rubocop:disable Lint/RescueException failed = true client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE - client.exec(rollback) + client.exec(rollback) unless client.status == PG::CONNECTION_BAD # the server already rolled back raise ensure - unless failed + client.in_transaction = false if entered + if failed + nil + elsif client.status == PG::CONNECTION_BAD + # the block rescued the error of a lost connection: the server rolled the transaction back + raise PG::ConnectionBad, 'transaction rolled back: the connection was lost' if Thread.current.status != 'aborting' + else # a statement failed and the block rescued the error: PostgreSQL can only roll back rescued = client.transaction_status == PG::PQTRANS_INERROR commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" @@ -1743,7 +1751,12 @@ def instrument(name) def with_client(name) instrument(name) do @db.with do |client| - client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + # inside #transaction a new session would run the rest of the block outside the transaction + raise PG::ConnectionBad, 'the connection was lost inside a transaction' if client.is_a?(ConnectionState) && client.in_transaction + + client.reset + end configure(client) unless client.is_a?(ConnectionState) begin yield client diff --git a/test/golden/pg/ruby32_notypes/connection.rb b/test/golden/pg/ruby32_notypes/connection.rb index 22ecd81..7b07520 100644 --- a/test/golden/pg/ruby32_notypes/connection.rb +++ b/test/golden/pg/ruby32_notypes/connection.rb @@ -14,6 +14,9 @@ def prepared_statements end @prepared_statements end + + # Set inside #transaction: a lost connection is then not reconnected until the transaction block ends. + attr_accessor :in_transaction end ISOLATION_LEVELS = { diff --git a/test/golden/pg/ruby32_notypes/my_queries_sql.rb b/test/golden/pg/ruby32_notypes/my_queries_sql.rb index c410b59..c132222 100644 --- a/test/golden/pg/ruby32_notypes/my_queries_sql.rb +++ b/test/golden/pg/ruby32_notypes/my_queries_sql.rb @@ -43,6 +43,8 @@ def transaction(isolation: nil) else client.exec("SAVEPOINT #{savepoint}") end + entered = !client.in_transaction + client.in_transaction = true rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false completed = false @@ -53,10 +55,16 @@ def transaction(isolation: nil) rescue Exception # rubocop:disable Lint/RescueException failed = true client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE - client.exec(rollback) + client.exec(rollback) unless client.status == PG::CONNECTION_BAD # the server already rolled back raise ensure - unless failed + client.in_transaction = false if entered + if failed + nil + elsif client.status == PG::CONNECTION_BAD + # the block rescued the error of a lost connection: the server rolled the transaction back + raise PG::ConnectionBad, 'transaction rolled back: the connection was lost' if Thread.current.status != 'aborting' + else # a statement failed and the block rescued the error: PostgreSQL can only roll back rescued = client.transaction_status == PG::PQTRANS_INERROR commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" @@ -97,7 +105,12 @@ def instrument(name) def with_client(name) instrument(name) do @db.with do |client| - client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + # inside #transaction a new session would run the rest of the block outside the transaction + raise PG::ConnectionBad, 'the connection was lost inside a transaction' if client.is_a?(ConnectionState) && client.in_transaction + + client.reset + end configure(client) unless client.is_a?(ConnectionState) begin yield client diff --git a/test/golden/pg/ruby32_notypes/other_sql.rb b/test/golden/pg/ruby32_notypes/other_sql.rb index 73e5cfe..af69fc5 100644 --- a/test/golden/pg/ruby32_notypes/other_sql.rb +++ b/test/golden/pg/ruby32_notypes/other_sql.rb @@ -14,7 +14,7 @@ module PgCodegen OTHER_ONE_SQL = <<~'SQL' SELECT 1 -- ? ? - WHERE a = ? + WHERE a = 1 SQL class OtherOneRow < Data.define(:c0) @@ -56,6 +56,8 @@ def transaction(isolation: nil) else client.exec("SAVEPOINT #{savepoint}") end + entered = !client.in_transaction + client.in_transaction = true rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false completed = false @@ -66,10 +68,16 @@ def transaction(isolation: nil) rescue Exception # rubocop:disable Lint/RescueException failed = true client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE - client.exec(rollback) + client.exec(rollback) unless client.status == PG::CONNECTION_BAD # the server already rolled back raise ensure - unless failed + client.in_transaction = false if entered + if failed + nil + elsif client.status == PG::CONNECTION_BAD + # the block rescued the error of a lost connection: the server rolled the transaction back + raise PG::ConnectionBad, 'transaction rolled back: the connection was lost' if Thread.current.status != 'aborting' + else # a statement failed and the block rescued the error: PostgreSQL can only roll back rescued = client.transaction_status == PG::PQTRANS_INERROR commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" @@ -145,7 +153,12 @@ def instrument(name) def with_client(name) instrument(name) do @db.with do |client| - client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + # inside #transaction a new session would run the rest of the block outside the transaction + raise PG::ConnectionBad, 'the connection was lost inside a transaction' if client.is_a?(ConnectionState) && client.in_transaction + + client.reset + end configure(client) unless client.is_a?(ConnectionState) begin yield client diff --git a/test/golden/pg/ruby32_notypes/query_sql.rb b/test/golden/pg/ruby32_notypes/query_sql.rb index fc136c5..3fcd1dc 100644 --- a/test/golden/pg/ruby32_notypes/query_sql.rb +++ b/test/golden/pg/ruby32_notypes/query_sql.rb @@ -567,7 +567,7 @@ class ManyRowsRow < Data.define( FOUR_COLS_SQL = <<~'SQL' SELECT 1 -- ? ? - WHERE a = ? + WHERE a = 1 SQL class FourColsRow < Data.define(:c0, :c1, :c2, :c3) @@ -604,6 +604,8 @@ def transaction(isolation: nil) else client.exec("SAVEPOINT #{savepoint}") end + entered = !client.in_transaction + client.in_transaction = true rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false completed = false @@ -614,10 +616,16 @@ def transaction(isolation: nil) rescue Exception # rubocop:disable Lint/RescueException failed = true client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE - client.exec(rollback) + client.exec(rollback) unless client.status == PG::CONNECTION_BAD # the server already rolled back raise ensure - unless failed + client.in_transaction = false if entered + if failed + nil + elsif client.status == PG::CONNECTION_BAD + # the block rescued the error of a lost connection: the server rolled the transaction back + raise PG::ConnectionBad, 'transaction rolled back: the connection was lost' if Thread.current.status != 'aborting' + else # a statement failed and the block rescued the error: PostgreSQL can only roll back rescued = client.transaction_status == PG::PQTRANS_INERROR commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" @@ -1736,7 +1744,12 @@ def instrument(name) def with_client(name) instrument(name) do @db.with do |client| - client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) + # inside #transaction a new session would run the rest of the block outside the transaction + raise PG::ConnectionBad, 'the connection was lost inside a transaction' if client.is_a?(ConnectionState) && client.in_transaction + + client.reset + end configure(client) unless client.is_a?(ConnectionState) begin yield client diff --git a/test/golden/sqlite3/default/my_queries_sql.rb b/test/golden/sqlite3/default/my_queries_sql.rb index a78a324..07ab615 100644 --- a/test/golden/sqlite3/default/my_queries_sql.rb +++ b/test/golden/sqlite3/default/my_queries_sql.rb @@ -62,7 +62,7 @@ def with_numbers2_go(c0:) query_params = [c0] stmt = client.prepare(WITH_NUMBERS2_GO_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -87,5 +87,13 @@ def instrument(name) def with_client(name, &block) instrument(name) { @lock.synchronize { block.call(@db) } } end + + def execute_statement(stmt, *params) + stmt.reset! if stmt.active? || stmt.done? + stmt.bind_params(*params) unless params.empty? + result = SQLite3::ResultSet.new(@db, stmt) + stmt.step if stmt.column_count.zero? + result + end end end diff --git a/test/golden/sqlite3/default/my_queries_sql.rbs b/test/golden/sqlite3/default/my_queries_sql.rbs index c5a960b..6391a6d 100644 --- a/test/golden/sqlite3/default/my_queries_sql.rbs +++ b/test/golden/sqlite3/default/my_queries_sql.rbs @@ -17,5 +17,7 @@ module Sqlite3Codegen def instrument: [T] (String name) { () -> T } -> T def with_client: [T] (String name) { (untyped) -> T } -> T + + def execute_statement: (untyped stmt, *untyped params) -> untyped end end diff --git a/test/golden/sqlite3/default/other_sql.rb b/test/golden/sqlite3/default/other_sql.rb index 855c566..11d4625 100644 --- a/test/golden/sqlite3/default/other_sql.rb +++ b/test/golden/sqlite3/default/other_sql.rb @@ -15,7 +15,7 @@ module Sqlite3Codegen OTHER_ONE_SQL = <<~'SQL' SELECT 1 -- ? ? - WHERE a = ? + WHERE a = 1 SQL class OtherOneRow < Data.define(:c0) @@ -81,7 +81,7 @@ def no_cols(c0:, c1:, c2:, c3:, c4:) ] stmt = client.prepare(NO_COLS_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -93,7 +93,7 @@ def other_one with_client('other_one') do |client| stmt = client.prepare(OTHER_ONE_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && OtherOneRow.new(row[0]) ensure @@ -112,7 +112,7 @@ def count_rows(c0:) query_params = [c0] stmt = client.prepare(COUNT_ROWS_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) client.changes ensure stmt.close @@ -137,5 +137,13 @@ def instrument(name) def with_client(name, &block) instrument(name) { @lock.synchronize { block.call(@db) } } end + + def execute_statement(stmt, *params) + stmt.reset! if stmt.active? || stmt.done? + stmt.bind_params(*params) unless params.empty? + result = SQLite3::ResultSet.new(@db, stmt) + stmt.step if stmt.column_count.zero? + result + end end end diff --git a/test/golden/sqlite3/default/other_sql.rbs b/test/golden/sqlite3/default/other_sql.rbs index f6f64ea..b73a101 100644 --- a/test/golden/sqlite3/default/other_sql.rbs +++ b/test/golden/sqlite3/default/other_sql.rbs @@ -32,5 +32,7 @@ module Sqlite3Codegen def instrument: [T] (String name) { () -> T } -> T def with_client: [T] (String name) { (untyped) -> T } -> T + + def execute_statement: (untyped stmt, *untyped params) -> untyped end end diff --git a/test/golden/sqlite3/default/query_sql.rb b/test/golden/sqlite3/default/query_sql.rb index 24c2bff..2ecaaff 100644 --- a/test/golden/sqlite3/default/query_sql.rb +++ b/test/golden/sqlite3/default/query_sql.rb @@ -424,7 +424,7 @@ class ManyRowsRow < Data.define( FOUR_COLS_SQL = <<~'SQL' SELECT 1 -- ? ? - WHERE a = ? + WHERE a = 1 SQL class FourColsRow < Data.define(:c0, :c1, :c2, :c3) @@ -483,7 +483,7 @@ def get_author(id:) query_params = [id] stmt = client.prepare(GET_AUTHOR_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetAuthorRow.new(row[0], row[1], row[2]) ensure @@ -501,7 +501,7 @@ def list_authors with_client('list_authors') do |client| stmt = client.prepare(LIST_AUTHORS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListAuthorsRow.new(row[0], row[1], row[2]) end @@ -516,7 +516,7 @@ def create_author(name:, bio:) query_params = [name, bio] stmt = client.prepare(CREATE_AUTHOR_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -529,7 +529,7 @@ def delete_author(id:) query_params = [id] stmt = client.prepare(DELETE_AUTHOR_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -542,7 +542,7 @@ def list_authors_paged(offset:, limit:) query_params = [limit, offset] stmt = client.prepare(LIST_AUTHORS_PAGED_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| ListAuthorsPagedRow.new(row[0], row[1], row[2]) end @@ -557,7 +557,7 @@ def get_author_by_name_pattern(name_pattern: nil) query_params = [name_pattern] stmt = client.prepare(GET_AUTHOR_BY_NAME_PATTERN_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorByNamePatternRow.new(row[0], row[1], row[2]) end @@ -572,7 +572,7 @@ def get_authors_by_optional_name(lim:, name_filter: nil) query_params = [name_filter, name_filter, lim] stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByOptionalNameRow.new(row[0], row[1], row[2]) end @@ -587,7 +587,7 @@ def create_author_return_id(name:, bio:) query_params = [name, bio] stmt = client.prepare(CREATE_AUTHOR_RETURN_ID_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && row[0] ensure @@ -602,7 +602,7 @@ def update_author_bios(bio:) query_params = [bio] stmt = client.prepare(UPDATE_AUTHOR_BIOS_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) client.changes ensure stmt.close @@ -616,7 +616,7 @@ def get_authors_by_ids(ids:) sql = GET_AUTHORS_BY_IDS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) stmt = client.prepare(sql) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByIdsRow.new(row[0], row[1], row[2]) end @@ -633,7 +633,7 @@ def get_authors_by_ids_and_names(ids:, names:) sql = sql.gsub('/*SLICE:names*/?', slice_placeholders(names)) stmt = client.prepare(sql) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByIdsAndNamesRow.new(row[0], row[1], row[2]) end @@ -657,7 +657,7 @@ def get_authors_by_mixed_placeholders(n:, ids:, id:, more:) sql = sql.gsub('/*SLICE:more*/?', slice_placeholders(more)) stmt = client.prepare(sql) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByMixedPlaceholdersRow.new(row[0], row[1], row[2]) end @@ -674,7 +674,7 @@ def get_authors_by_repeated_named_and_positional(n:, other:, id:) query_params = [n, other, id, n] stmt = client.prepare(GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByRepeatedNamedAndPositionalRow.new(row[0], row[1], row[2]) end @@ -691,7 +691,7 @@ def get_authors_by_repeated_named_and_positional_batch(rows) begin rows.map do |args| query_params = [args.n, args.other, args.id, args.n] - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetAuthorsByRepeatedNamedAndPositionalBatchRow.new(row[0], row[1], row[2]) end @@ -730,7 +730,7 @@ def truncate_authors with_client('truncate_authors') do |client| stmt = client.prepare(TRUNCATE_AUTHORS_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -743,7 +743,7 @@ def create_book(name:, author_id:) query_params = [name, author_id] stmt = client.prepare(CREATE_BOOK_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && row[0] ensure @@ -756,7 +756,7 @@ def list_all_authors_books with_client('list_all_authors_books') do |client| stmt = client.prepare(LIST_ALL_AUTHORS_BOOKS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListAllAuthorsBooksRow.new( Author.new(row[0], row[1], row[2]), @@ -773,7 +773,7 @@ def get_duplicate_authors with_client('get_duplicate_authors') do |client| stmt = client.prepare(GET_DUPLICATE_AUTHORS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| GetDuplicateAuthorsRow.new(Author.new(row[0], row[1], row[2]), Author.new(row[3], row[4], row[5])) end @@ -788,7 +788,7 @@ def get_authors_by_book_name(name:) query_params = [name] stmt = client.prepare(GET_AUTHORS_BY_BOOK_NAME_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByBookNameRow.new(row[0], row[1], row[2], Book.new(row[3], row[4], row[5], row[6])) end @@ -803,7 +803,7 @@ def create_author_result(name:, bio:) query_params = [name, bio] stmt = client.prepare(CREATE_AUTHOR_RESULT_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) ExecResult.new(client.changes, client.last_insert_row_id) ensure stmt.close @@ -817,7 +817,7 @@ def create_authors_batch(rows) begin rows.each do |args| query_params = [args.name, args.bio] - stmt.execute(*query_params) + execute_statement(stmt, *query_params) end nil ensure @@ -832,7 +832,7 @@ def get_author_batch(rows) begin rows.map do |args| query_params = [args.id] - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetAuthorBatchRow.new(row[0], row[1], row[2]) end @@ -848,7 +848,7 @@ def list_authors_by_name_batch(rows) begin rows.map do |args| query_params = [args.name] - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| ListAuthorsByNameBatchRow.new(row[0], row[1], row[2]) end @@ -872,7 +872,7 @@ def insert_declared_types(c_varchar:, c_decimal:, c_unsigned_big_int:, c_native: ] stmt = client.prepare(INSERT_DECLARED_TYPES_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -884,7 +884,7 @@ def get_declared_types with_client('get_declared_types') do |client| stmt = client.prepare(GET_DECLARED_TYPES_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && GetDeclaredTypesRow.new( row[0], @@ -910,7 +910,7 @@ def truncate_declared_types with_client('truncate_declared_types') do |client| stmt = client.prepare(TRUNCATE_DECLARED_TYPES_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -923,7 +923,7 @@ def create_regression(order:, price:) query_params = [order, price] stmt = client.prepare(CREATE_REGRESSION_SQL) begin - stmt.execute(*query_params).to_a + execute_statement(stmt, *query_params).to_a nil ensure stmt.close @@ -936,7 +936,7 @@ def update_regression_prices(price:) query_params = [price] stmt = client.prepare(UPDATE_REGRESSION_PRICES_SQL) begin - stmt.execute(*query_params).to_a + execute_statement(stmt, *query_params).to_a client.changes ensure stmt.close @@ -960,7 +960,7 @@ def list_regressions with_client('list_regressions') do |client| stmt = client.prepare(LIST_REGRESSIONS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListRegressionsRow.new(row[0], row[1], row[2]) end @@ -974,7 +974,7 @@ def get_max_regression_price with_client('get_max_regression_price') do |client| stmt = client.prepare(GET_MAX_REGRESSION_PRICE_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && GetMaxRegressionPriceRow.new(row[0]) ensure @@ -992,7 +992,7 @@ def truncate_regressions with_client('truncate_regressions') do |client| stmt = client.prepare(TRUNCATE_REGRESSIONS_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -1016,7 +1016,7 @@ def list_regression_rows with_client('list_regression_rows') do |client| stmt = client.prepare(LIST_REGRESSION_ROWS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListRegressionRowsRow.new(row[0], row[1], row[2], row[3]) end @@ -1030,7 +1030,7 @@ def truncate_regression_rows with_client('truncate_regression_rows') do |client| stmt = client.prepare(TRUNCATE_REGRESSION_ROWS_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -1043,7 +1043,7 @@ def pause_query(sleep:) query_params = [sleep] stmt = client.prepare(PAUSE_QUERY_SQL) begin - stmt.execute(*query_params).to_a + execute_statement(stmt, *query_params).to_a nil ensure stmt.close @@ -1091,7 +1091,7 @@ def list_tags with_client('list_tags') do |client| stmt = client.prepare(LIST_TAGS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListTagsRow.new(row[0], row[1]) end @@ -1105,7 +1105,7 @@ def truncate_tags with_client('truncate_tags') do |client| stmt = client.prepare(TRUNCATE_TAGS_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -1117,7 +1117,7 @@ def list_authors_with_optional_book with_client('list_authors_with_optional_book') do |client| stmt = client.prepare(LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListAuthorsWithOptionalBookRow.new( row[0].nil? && row[1].nil? ? nil : Author.new(row[0], row[1], row[2]), @@ -1144,7 +1144,7 @@ def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, h ] stmt = client.prepare(INSERT_NAME_CASE_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -1166,7 +1166,7 @@ def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, cam ] stmt = client.prepare(GET_NAME_CASE_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetNameCaseRow.new( row[0], @@ -1194,7 +1194,7 @@ def get_http_server(id:) query_params = [id] stmt = client.prepare(GET_HTTP_SERVER_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetHTTPServerRow.new( row[0], @@ -1221,7 +1221,7 @@ def truncate_name_cases with_client('truncate_name_cases') do |client| stmt = client.prepare(TRUNCATE_NAME_CASES_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -1233,7 +1233,7 @@ def get_unnamed_columns with_client('get_unnamed_columns') do |client| stmt = client.prepare(GET_UNNAMED_COLUMNS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && GetUnnamedColumnsRow.new(row[0], row[1]) ensure @@ -1251,7 +1251,7 @@ def get_quoted_alias with_client('get_quoted_alias') do |client| stmt = client.prepare(GET_QUOTED_ALIAS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && GetQuotedAliasRow.new(row[0]) ensure @@ -1271,7 +1271,7 @@ def get_authors_not_in_ids(ids:) sql = GET_AUTHORS_NOT_IN_IDS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) stmt = client.prepare(sql) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsNotInIdsRow.new(row[0], row[1], row[2]) end @@ -1286,7 +1286,7 @@ def create_author_or_rollback(id:, name:) query_params = [id, name] stmt = client.prepare(CREATE_AUTHOR_OR_ROLLBACK_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -1305,7 +1305,7 @@ def many_params(c0:, c1:, c2:, c3:, c4:) ] stmt = client.prepare(MANY_PARAMS_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && ManyParamsRow.new(row[0], row[1]) ensure @@ -1324,7 +1324,7 @@ def many_rows(c0:) query_params = [c0] stmt = client.prepare(MANY_ROWS_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| ManyRowsRow.new( row[0], @@ -1344,7 +1344,7 @@ def four_cols with_client('four_cols') do |client| stmt = client.prepare(FOUR_COLS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| FourColsRow.new(row[0], row[1], row[2], row[3]) end @@ -1372,6 +1372,14 @@ def with_client(name, &block) instrument(name) { @lock.synchronize { block.call(@db) } } end + def execute_statement(stmt, *params) + stmt.reset! if stmt.active? || stmt.done? + stmt.bind_params(*params) unless params.empty? + result = SQLite3::ResultSet.new(@db, stmt) + stmt.step if stmt.column_count.zero? + result + end + def atomically(client) client.execute('SAVEPOINT sqlc_copy') done = false diff --git a/test/golden/sqlite3/default/query_sql.rbs b/test/golden/sqlite3/default/query_sql.rbs index 3b0db6c..cc44afa 100644 --- a/test/golden/sqlite3/default/query_sql.rbs +++ b/test/golden/sqlite3/default/query_sql.rbs @@ -578,6 +578,8 @@ module Sqlite3Codegen def with_client: [T] (String name) { (untyped) -> T } -> T + def execute_statement: (untyped stmt, *untyped params) -> untyped + def atomically: (untyped client) { () -> void } -> void def slice_placeholders: (Array[untyped] values) -> String diff --git a/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rb b/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rb index 4af03a4..d972637 100644 --- a/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rb +++ b/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rb @@ -65,7 +65,7 @@ def with_numbers2_go(c0:) query_params = [c0] stmt = client.prepare(WITH_NUMBERS2_GO_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -91,6 +91,14 @@ def with_client(name, &block) instrument(name) { @lock.synchronize { block.call(@db) } } end + def execute_statement(stmt, *params) + stmt.reset! if stmt.active? || stmt.done? + stmt.bind_params(*params) unless params.empty? + result = SQLite3::ResultSet.new(@db, stmt) + stmt.step if stmt.column_count.zero? + result + end + def timeout_exits_with_throw? !!(defined?(::Timeout::VERSION) && (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) diff --git a/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rbs b/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rbs index 9d5d645..bee79ff 100644 --- a/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rbs +++ b/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rbs @@ -18,6 +18,8 @@ module Sqlite3Codegen def with_client: [T] (String name) { (untyped) -> T } -> T + def execute_statement: (untyped stmt, *untyped params) -> untyped + def timeout_exits_with_throw?: () -> bool end end diff --git a/test/golden/sqlite3/ruby31_gemfile/other_sql.rb b/test/golden/sqlite3/ruby31_gemfile/other_sql.rb index 866aa54..8ede44d 100644 --- a/test/golden/sqlite3/ruby31_gemfile/other_sql.rb +++ b/test/golden/sqlite3/ruby31_gemfile/other_sql.rb @@ -15,7 +15,7 @@ module Sqlite3Codegen OTHER_ONE_SQL = <<~'SQL' SELECT 1 -- ? ? - WHERE a = ? + WHERE a = 1 SQL OtherOneRow = Struct.new(:c0, keyword_init: true) @@ -83,7 +83,7 @@ def no_cols(c0:, c1:, c2:, c3:, c4:) ] stmt = client.prepare(NO_COLS_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -95,7 +95,7 @@ def other_one with_client('other_one') do |client| stmt = client.prepare(OTHER_ONE_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && OtherOneRow.new(c0: row[0]) ensure @@ -114,7 +114,7 @@ def count_rows(c0:) query_params = [c0] stmt = client.prepare(COUNT_ROWS_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) client.changes ensure stmt.close @@ -140,6 +140,14 @@ def with_client(name, &block) instrument(name) { @lock.synchronize { block.call(@db) } } end + def execute_statement(stmt, *params) + stmt.reset! if stmt.active? || stmt.done? + stmt.bind_params(*params) unless params.empty? + result = SQLite3::ResultSet.new(@db, stmt) + stmt.step if stmt.column_count.zero? + result + end + def timeout_exits_with_throw? !!(defined?(::Timeout::VERSION) && (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) diff --git a/test/golden/sqlite3/ruby31_gemfile/other_sql.rbs b/test/golden/sqlite3/ruby31_gemfile/other_sql.rbs index a1df20a..e8f88d6 100644 --- a/test/golden/sqlite3/ruby31_gemfile/other_sql.rbs +++ b/test/golden/sqlite3/ruby31_gemfile/other_sql.rbs @@ -34,6 +34,8 @@ module Sqlite3Codegen def with_client: [T] (String name) { (untyped) -> T } -> T + def execute_statement: (untyped stmt, *untyped params) -> untyped + def timeout_exits_with_throw?: () -> bool end end diff --git a/test/golden/sqlite3/ruby31_gemfile/query_sql.rb b/test/golden/sqlite3/ruby31_gemfile/query_sql.rb index ab4a48b..2d2d38b 100644 --- a/test/golden/sqlite3/ruby31_gemfile/query_sql.rb +++ b/test/golden/sqlite3/ruby31_gemfile/query_sql.rb @@ -402,7 +402,7 @@ module Sqlite3Codegen FOUR_COLS_SQL = <<~'SQL' SELECT 1 -- ? ? - WHERE a = ? + WHERE a = 1 SQL FourColsRow = Struct.new( @@ -468,7 +468,7 @@ def get_author(id:) query_params = [id] stmt = client.prepare(GET_AUTHOR_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetAuthorRow.new(id: row[0], name: row[1], bio: row[2]) ensure @@ -486,7 +486,7 @@ def list_authors with_client('list_authors') do |client| stmt = client.prepare(LIST_AUTHORS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListAuthorsRow.new(id: row[0], name: row[1], bio: row[2]) end @@ -501,7 +501,7 @@ def create_author(name:, bio:) query_params = [name, bio] stmt = client.prepare(CREATE_AUTHOR_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -514,7 +514,7 @@ def delete_author(id:) query_params = [id] stmt = client.prepare(DELETE_AUTHOR_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -527,7 +527,7 @@ def list_authors_paged(offset:, limit:) query_params = [limit, offset] stmt = client.prepare(LIST_AUTHORS_PAGED_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| ListAuthorsPagedRow.new(id: row[0], name: row[1], bio: row[2]) end @@ -542,7 +542,7 @@ def get_author_by_name_pattern(name_pattern: nil) query_params = [name_pattern] stmt = client.prepare(GET_AUTHOR_BY_NAME_PATTERN_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorByNamePatternRow.new(id: row[0], name: row[1], bio: row[2]) end @@ -557,7 +557,7 @@ def get_authors_by_optional_name(lim:, name_filter: nil) query_params = [name_filter, name_filter, lim] stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByOptionalNameRow.new(id: row[0], name: row[1], bio: row[2]) end @@ -572,7 +572,7 @@ def create_author_return_id(name:, bio:) query_params = [name, bio] stmt = client.prepare(CREATE_AUTHOR_RETURN_ID_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && row[0] ensure @@ -587,7 +587,7 @@ def update_author_bios(bio:) query_params = [bio] stmt = client.prepare(UPDATE_AUTHOR_BIOS_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) client.changes ensure stmt.close @@ -601,7 +601,7 @@ def get_authors_by_ids(ids:) sql = GET_AUTHORS_BY_IDS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) stmt = client.prepare(sql) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByIdsRow.new(id: row[0], name: row[1], bio: row[2]) end @@ -618,7 +618,7 @@ def get_authors_by_ids_and_names(ids:, names:) sql = sql.gsub('/*SLICE:names*/?', slice_placeholders(names)) stmt = client.prepare(sql) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByIdsAndNamesRow.new(id: row[0], name: row[1], bio: row[2]) end @@ -642,7 +642,7 @@ def get_authors_by_mixed_placeholders(n:, ids:, id:, more:) sql = sql.gsub('/*SLICE:more*/?', slice_placeholders(more)) stmt = client.prepare(sql) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByMixedPlaceholdersRow.new(id: row[0], name: row[1], bio: row[2]) end @@ -659,7 +659,7 @@ def get_authors_by_repeated_named_and_positional(n:, other:, id:) query_params = [n, other, id, n] stmt = client.prepare(GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByRepeatedNamedAndPositionalRow.new(id: row[0], name: row[1], bio: row[2]) end @@ -676,7 +676,7 @@ def get_authors_by_repeated_named_and_positional_batch(rows) begin rows.map do |args| query_params = [args.n, args.other, args.id, args.n] - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetAuthorsByRepeatedNamedAndPositionalBatchRow.new(id: row[0], name: row[1], bio: row[2]) end @@ -715,7 +715,7 @@ def truncate_authors with_client('truncate_authors') do |client| stmt = client.prepare(TRUNCATE_AUTHORS_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -728,7 +728,7 @@ def create_book(name:, author_id:) query_params = [name, author_id] stmt = client.prepare(CREATE_BOOK_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && row[0] ensure @@ -741,7 +741,7 @@ def list_all_authors_books with_client('list_all_authors_books') do |client| stmt = client.prepare(LIST_ALL_AUTHORS_BOOKS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListAllAuthorsBooksRow.new( author: Author.new(id: row[0], name: row[1], bio: row[2]), @@ -758,7 +758,7 @@ def get_duplicate_authors with_client('get_duplicate_authors') do |client| stmt = client.prepare(GET_DUPLICATE_AUTHORS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| GetDuplicateAuthorsRow.new( author: Author.new(id: row[0], name: row[1], bio: row[2]), @@ -776,7 +776,7 @@ def get_authors_by_book_name(name:) query_params = [name] stmt = client.prepare(GET_AUTHORS_BY_BOOK_NAME_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByBookNameRow.new( id: row[0], @@ -796,7 +796,7 @@ def create_author_result(name:, bio:) query_params = [name, bio] stmt = client.prepare(CREATE_AUTHOR_RESULT_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) ExecResult.new(rows_affected: client.changes, last_insert_id: client.last_insert_row_id) ensure stmt.close @@ -810,7 +810,7 @@ def create_authors_batch(rows) begin rows.each do |args| query_params = [args.name, args.bio] - stmt.execute(*query_params) + execute_statement(stmt, *query_params) end nil ensure @@ -825,7 +825,7 @@ def get_author_batch(rows) begin rows.map do |args| query_params = [args.id] - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetAuthorBatchRow.new(id: row[0], name: row[1], bio: row[2]) end @@ -841,7 +841,7 @@ def list_authors_by_name_batch(rows) begin rows.map do |args| query_params = [args.name] - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| ListAuthorsByNameBatchRow.new(id: row[0], name: row[1], bio: row[2]) end @@ -865,7 +865,7 @@ def insert_declared_types(c_varchar:, c_decimal:, c_unsigned_big_int:, c_native: ] stmt = client.prepare(INSERT_DECLARED_TYPES_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -877,7 +877,7 @@ def get_declared_types with_client('get_declared_types') do |client| stmt = client.prepare(GET_DECLARED_TYPES_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && GetDeclaredTypesRow.new( c_varchar: row[0], @@ -903,7 +903,7 @@ def truncate_declared_types with_client('truncate_declared_types') do |client| stmt = client.prepare(TRUNCATE_DECLARED_TYPES_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -916,7 +916,7 @@ def create_regression(order:, price:) query_params = [order, price] stmt = client.prepare(CREATE_REGRESSION_SQL) begin - stmt.execute(*query_params).to_a + execute_statement(stmt, *query_params).to_a nil ensure stmt.close @@ -929,7 +929,7 @@ def update_regression_prices(price:) query_params = [price] stmt = client.prepare(UPDATE_REGRESSION_PRICES_SQL) begin - stmt.execute(*query_params).to_a + execute_statement(stmt, *query_params).to_a client.changes ensure stmt.close @@ -953,7 +953,7 @@ def list_regressions with_client('list_regressions') do |client| stmt = client.prepare(LIST_REGRESSIONS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListRegressionsRow.new(id: row[0], order: row[1], price: row[2]) end @@ -967,7 +967,7 @@ def get_max_regression_price with_client('get_max_regression_price') do |client| stmt = client.prepare(GET_MAX_REGRESSION_PRICE_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && GetMaxRegressionPriceRow.new(max_price: row[0]) ensure @@ -985,7 +985,7 @@ def truncate_regressions with_client('truncate_regressions') do |client| stmt = client.prepare(TRUNCATE_REGRESSIONS_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -1009,7 +1009,7 @@ def list_regression_rows with_client('list_regression_rows') do |client| stmt = client.prepare(LIST_REGRESSION_ROWS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListRegressionRowsRow.new(id: row[0], order: row[1], price: row[2], payload: row[3]) end @@ -1023,7 +1023,7 @@ def truncate_regression_rows with_client('truncate_regression_rows') do |client| stmt = client.prepare(TRUNCATE_REGRESSION_ROWS_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -1036,7 +1036,7 @@ def pause_query(sleep:) query_params = [sleep] stmt = client.prepare(PAUSE_QUERY_SQL) begin - stmt.execute(*query_params).to_a + execute_statement(stmt, *query_params).to_a nil ensure stmt.close @@ -1084,7 +1084,7 @@ def list_tags with_client('list_tags') do |client| stmt = client.prepare(LIST_TAGS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListTagsRow.new(name: row[0], uses: row[1]) end @@ -1098,7 +1098,7 @@ def truncate_tags with_client('truncate_tags') do |client| stmt = client.prepare(TRUNCATE_TAGS_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -1110,7 +1110,7 @@ def list_authors_with_optional_book with_client('list_authors_with_optional_book') do |client| stmt = client.prepare(LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListAuthorsWithOptionalBookRow.new( author: row[0].nil? && row[1].nil? ? nil : Author.new(id: row[0], name: row[1], bio: row[2]), @@ -1137,7 +1137,7 @@ def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, h ] stmt = client.prepare(INSERT_NAME_CASE_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -1159,7 +1159,7 @@ def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, cam ] stmt = client.prepare(GET_NAME_CASE_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetNameCaseRow.new( id: row[0], @@ -1187,7 +1187,7 @@ def get_http_server(id:) query_params = [id] stmt = client.prepare(GET_HTTP_SERVER_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetHTTPServerRow.new( id: row[0], @@ -1214,7 +1214,7 @@ def truncate_name_cases with_client('truncate_name_cases') do |client| stmt = client.prepare(TRUNCATE_NAME_CASES_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -1226,7 +1226,7 @@ def get_unnamed_columns with_client('get_unnamed_columns') do |client| stmt = client.prepare(GET_UNNAMED_COLUMNS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && GetUnnamedColumnsRow.new(column_1: row[0], column_2: row[1]) ensure @@ -1244,7 +1244,7 @@ def get_quoted_alias with_client('get_quoted_alias') do |client| stmt = client.prepare(GET_QUOTED_ALIAS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && GetQuotedAliasRow.new(it_s: row[0]) ensure @@ -1264,7 +1264,7 @@ def get_authors_not_in_ids(ids:) sql = GET_AUTHORS_NOT_IN_IDS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) stmt = client.prepare(sql) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsNotInIdsRow.new(id: row[0], name: row[1], bio: row[2]) end @@ -1279,7 +1279,7 @@ def create_author_or_rollback(id:, name:) query_params = [id, name] stmt = client.prepare(CREATE_AUTHOR_OR_ROLLBACK_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -1298,7 +1298,7 @@ def many_params(c0:, c1:, c2:, c3:, c4:) ] stmt = client.prepare(MANY_PARAMS_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && ManyParamsRow.new(c0: row[0], c1: row[1]) ensure @@ -1317,7 +1317,7 @@ def many_rows(c0:) query_params = [c0] stmt = client.prepare(MANY_ROWS_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| ManyRowsRow.new( c0: row[0], @@ -1337,7 +1337,7 @@ def four_cols with_client('four_cols') do |client| stmt = client.prepare(FOUR_COLS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| FourColsRow.new(c0: row[0], c1: row[1], c2: row[2], c3: row[3]) end @@ -1365,6 +1365,14 @@ def with_client(name, &block) instrument(name) { @lock.synchronize { block.call(@db) } } end + def execute_statement(stmt, *params) + stmt.reset! if stmt.active? || stmt.done? + stmt.bind_params(*params) unless params.empty? + result = SQLite3::ResultSet.new(@db, stmt) + stmt.step if stmt.column_count.zero? + result + end + def timeout_exits_with_throw? !!(defined?(::Timeout::VERSION) && (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) diff --git a/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs b/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs index d2acb7f..3cc7a1f 100644 --- a/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs +++ b/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs @@ -656,6 +656,8 @@ module Sqlite3Codegen def with_client: [T] (String name) { (untyped) -> T } -> T + def execute_statement: (untyped stmt, *untyped params) -> untyped + def timeout_exits_with_throw?: () -> bool def atomically: (untyped client) { () -> void } -> void diff --git a/test/golden/sqlite3/ruby32_notypes/my_queries_sql.rb b/test/golden/sqlite3/ruby32_notypes/my_queries_sql.rb index 4af03a4..d972637 100644 --- a/test/golden/sqlite3/ruby32_notypes/my_queries_sql.rb +++ b/test/golden/sqlite3/ruby32_notypes/my_queries_sql.rb @@ -65,7 +65,7 @@ def with_numbers2_go(c0:) query_params = [c0] stmt = client.prepare(WITH_NUMBERS2_GO_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -91,6 +91,14 @@ def with_client(name, &block) instrument(name) { @lock.synchronize { block.call(@db) } } end + def execute_statement(stmt, *params) + stmt.reset! if stmt.active? || stmt.done? + stmt.bind_params(*params) unless params.empty? + result = SQLite3::ResultSet.new(@db, stmt) + stmt.step if stmt.column_count.zero? + result + end + def timeout_exits_with_throw? !!(defined?(::Timeout::VERSION) && (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) diff --git a/test/golden/sqlite3/ruby32_notypes/other_sql.rb b/test/golden/sqlite3/ruby32_notypes/other_sql.rb index 3aa39ae..85686f8 100644 --- a/test/golden/sqlite3/ruby32_notypes/other_sql.rb +++ b/test/golden/sqlite3/ruby32_notypes/other_sql.rb @@ -15,7 +15,7 @@ module Sqlite3Codegen OTHER_ONE_SQL = <<~'SQL' SELECT 1 -- ? ? - WHERE a = ? + WHERE a = 1 SQL class OtherOneRow < Data.define(:c0) @@ -84,7 +84,7 @@ def no_cols(c0:, c1:, c2:, c3:, c4:) ] stmt = client.prepare(NO_COLS_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -96,7 +96,7 @@ def other_one with_client('other_one') do |client| stmt = client.prepare(OTHER_ONE_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && OtherOneRow.new(row[0]) ensure @@ -115,7 +115,7 @@ def count_rows(c0:) query_params = [c0] stmt = client.prepare(COUNT_ROWS_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) client.changes ensure stmt.close @@ -141,6 +141,14 @@ def with_client(name, &block) instrument(name) { @lock.synchronize { block.call(@db) } } end + def execute_statement(stmt, *params) + stmt.reset! if stmt.active? || stmt.done? + stmt.bind_params(*params) unless params.empty? + result = SQLite3::ResultSet.new(@db, stmt) + stmt.step if stmt.column_count.zero? + result + end + def timeout_exits_with_throw? !!(defined?(::Timeout::VERSION) && (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) diff --git a/test/golden/sqlite3/ruby32_notypes/query_sql.rb b/test/golden/sqlite3/ruby32_notypes/query_sql.rb index 92d6fb6..82f682f 100644 --- a/test/golden/sqlite3/ruby32_notypes/query_sql.rb +++ b/test/golden/sqlite3/ruby32_notypes/query_sql.rb @@ -424,7 +424,7 @@ class ManyRowsRow < Data.define( FOUR_COLS_SQL = <<~'SQL' SELECT 1 -- ? ? - WHERE a = ? + WHERE a = 1 SQL class FourColsRow < Data.define(:c0, :c1, :c2, :c3) @@ -486,7 +486,7 @@ def get_author(id:) query_params = [id] stmt = client.prepare(GET_AUTHOR_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetAuthorRow.new(row[0], row[1], row[2]) ensure @@ -504,7 +504,7 @@ def list_authors with_client('list_authors') do |client| stmt = client.prepare(LIST_AUTHORS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListAuthorsRow.new(row[0], row[1], row[2]) end @@ -519,7 +519,7 @@ def create_author(name:, bio:) query_params = [name, bio] stmt = client.prepare(CREATE_AUTHOR_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -532,7 +532,7 @@ def delete_author(id:) query_params = [id] stmt = client.prepare(DELETE_AUTHOR_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -545,7 +545,7 @@ def list_authors_paged(offset:, limit:) query_params = [limit, offset] stmt = client.prepare(LIST_AUTHORS_PAGED_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| ListAuthorsPagedRow.new(row[0], row[1], row[2]) end @@ -560,7 +560,7 @@ def get_author_by_name_pattern(name_pattern: nil) query_params = [name_pattern] stmt = client.prepare(GET_AUTHOR_BY_NAME_PATTERN_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorByNamePatternRow.new(row[0], row[1], row[2]) end @@ -575,7 +575,7 @@ def get_authors_by_optional_name(lim:, name_filter: nil) query_params = [name_filter, name_filter, lim] stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByOptionalNameRow.new(row[0], row[1], row[2]) end @@ -590,7 +590,7 @@ def create_author_return_id(name:, bio:) query_params = [name, bio] stmt = client.prepare(CREATE_AUTHOR_RETURN_ID_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && row[0] ensure @@ -605,7 +605,7 @@ def update_author_bios(bio:) query_params = [bio] stmt = client.prepare(UPDATE_AUTHOR_BIOS_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) client.changes ensure stmt.close @@ -619,7 +619,7 @@ def get_authors_by_ids(ids:) sql = GET_AUTHORS_BY_IDS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) stmt = client.prepare(sql) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByIdsRow.new(row[0], row[1], row[2]) end @@ -636,7 +636,7 @@ def get_authors_by_ids_and_names(ids:, names:) sql = sql.gsub('/*SLICE:names*/?', slice_placeholders(names)) stmt = client.prepare(sql) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByIdsAndNamesRow.new(row[0], row[1], row[2]) end @@ -660,7 +660,7 @@ def get_authors_by_mixed_placeholders(n:, ids:, id:, more:) sql = sql.gsub('/*SLICE:more*/?', slice_placeholders(more)) stmt = client.prepare(sql) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByMixedPlaceholdersRow.new(row[0], row[1], row[2]) end @@ -677,7 +677,7 @@ def get_authors_by_repeated_named_and_positional(n:, other:, id:) query_params = [n, other, id, n] stmt = client.prepare(GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByRepeatedNamedAndPositionalRow.new(row[0], row[1], row[2]) end @@ -694,7 +694,7 @@ def get_authors_by_repeated_named_and_positional_batch(rows) begin rows.map do |args| query_params = [args.n, args.other, args.id, args.n] - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetAuthorsByRepeatedNamedAndPositionalBatchRow.new(row[0], row[1], row[2]) end @@ -733,7 +733,7 @@ def truncate_authors with_client('truncate_authors') do |client| stmt = client.prepare(TRUNCATE_AUTHORS_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -746,7 +746,7 @@ def create_book(name:, author_id:) query_params = [name, author_id] stmt = client.prepare(CREATE_BOOK_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && row[0] ensure @@ -759,7 +759,7 @@ def list_all_authors_books with_client('list_all_authors_books') do |client| stmt = client.prepare(LIST_ALL_AUTHORS_BOOKS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListAllAuthorsBooksRow.new( Author.new(row[0], row[1], row[2]), @@ -776,7 +776,7 @@ def get_duplicate_authors with_client('get_duplicate_authors') do |client| stmt = client.prepare(GET_DUPLICATE_AUTHORS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| GetDuplicateAuthorsRow.new(Author.new(row[0], row[1], row[2]), Author.new(row[3], row[4], row[5])) end @@ -791,7 +791,7 @@ def get_authors_by_book_name(name:) query_params = [name] stmt = client.prepare(GET_AUTHORS_BY_BOOK_NAME_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsByBookNameRow.new(row[0], row[1], row[2], Book.new(row[3], row[4], row[5], row[6])) end @@ -806,7 +806,7 @@ def create_author_result(name:, bio:) query_params = [name, bio] stmt = client.prepare(CREATE_AUTHOR_RESULT_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) ExecResult.new(client.changes, client.last_insert_row_id) ensure stmt.close @@ -820,7 +820,7 @@ def create_authors_batch(rows) begin rows.each do |args| query_params = [args.name, args.bio] - stmt.execute(*query_params) + execute_statement(stmt, *query_params) end nil ensure @@ -835,7 +835,7 @@ def get_author_batch(rows) begin rows.map do |args| query_params = [args.id] - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetAuthorBatchRow.new(row[0], row[1], row[2]) end @@ -851,7 +851,7 @@ def list_authors_by_name_batch(rows) begin rows.map do |args| query_params = [args.name] - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| ListAuthorsByNameBatchRow.new(row[0], row[1], row[2]) end @@ -875,7 +875,7 @@ def insert_declared_types(c_varchar:, c_decimal:, c_unsigned_big_int:, c_native: ] stmt = client.prepare(INSERT_DECLARED_TYPES_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -887,7 +887,7 @@ def get_declared_types with_client('get_declared_types') do |client| stmt = client.prepare(GET_DECLARED_TYPES_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && GetDeclaredTypesRow.new( row[0], @@ -913,7 +913,7 @@ def truncate_declared_types with_client('truncate_declared_types') do |client| stmt = client.prepare(TRUNCATE_DECLARED_TYPES_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -926,7 +926,7 @@ def create_regression(order:, price:) query_params = [order, price] stmt = client.prepare(CREATE_REGRESSION_SQL) begin - stmt.execute(*query_params).to_a + execute_statement(stmt, *query_params).to_a nil ensure stmt.close @@ -939,7 +939,7 @@ def update_regression_prices(price:) query_params = [price] stmt = client.prepare(UPDATE_REGRESSION_PRICES_SQL) begin - stmt.execute(*query_params).to_a + execute_statement(stmt, *query_params).to_a client.changes ensure stmt.close @@ -963,7 +963,7 @@ def list_regressions with_client('list_regressions') do |client| stmt = client.prepare(LIST_REGRESSIONS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListRegressionsRow.new(row[0], row[1], row[2]) end @@ -977,7 +977,7 @@ def get_max_regression_price with_client('get_max_regression_price') do |client| stmt = client.prepare(GET_MAX_REGRESSION_PRICE_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && GetMaxRegressionPriceRow.new(row[0]) ensure @@ -995,7 +995,7 @@ def truncate_regressions with_client('truncate_regressions') do |client| stmt = client.prepare(TRUNCATE_REGRESSIONS_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -1019,7 +1019,7 @@ def list_regression_rows with_client('list_regression_rows') do |client| stmt = client.prepare(LIST_REGRESSION_ROWS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListRegressionRowsRow.new(row[0], row[1], row[2], row[3]) end @@ -1033,7 +1033,7 @@ def truncate_regression_rows with_client('truncate_regression_rows') do |client| stmt = client.prepare(TRUNCATE_REGRESSION_ROWS_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -1046,7 +1046,7 @@ def pause_query(sleep:) query_params = [sleep] stmt = client.prepare(PAUSE_QUERY_SQL) begin - stmt.execute(*query_params).to_a + execute_statement(stmt, *query_params).to_a nil ensure stmt.close @@ -1094,7 +1094,7 @@ def list_tags with_client('list_tags') do |client| stmt = client.prepare(LIST_TAGS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListTagsRow.new(row[0], row[1]) end @@ -1108,7 +1108,7 @@ def truncate_tags with_client('truncate_tags') do |client| stmt = client.prepare(TRUNCATE_TAGS_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -1120,7 +1120,7 @@ def list_authors_with_optional_book with_client('list_authors_with_optional_book') do |client| stmt = client.prepare(LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| ListAuthorsWithOptionalBookRow.new( row[0].nil? && row[1].nil? ? nil : Author.new(row[0], row[1], row[2]), @@ -1147,7 +1147,7 @@ def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, h ] stmt = client.prepare(INSERT_NAME_CASE_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -1169,7 +1169,7 @@ def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, cam ] stmt = client.prepare(GET_NAME_CASE_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetNameCaseRow.new( row[0], @@ -1197,7 +1197,7 @@ def get_http_server(id:) query_params = [id] stmt = client.prepare(GET_HTTP_SERVER_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && GetHTTPServerRow.new( row[0], @@ -1224,7 +1224,7 @@ def truncate_name_cases with_client('truncate_name_cases') do |client| stmt = client.prepare(TRUNCATE_NAME_CASES_SQL) begin - stmt.execute + execute_statement(stmt) nil ensure stmt.close @@ -1236,7 +1236,7 @@ def get_unnamed_columns with_client('get_unnamed_columns') do |client| stmt = client.prepare(GET_UNNAMED_COLUMNS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && GetUnnamedColumnsRow.new(row[0], row[1]) ensure @@ -1254,7 +1254,7 @@ def get_quoted_alias with_client('get_quoted_alias') do |client| stmt = client.prepare(GET_QUOTED_ALIAS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) row = result.first row && GetQuotedAliasRow.new(row[0]) ensure @@ -1274,7 +1274,7 @@ def get_authors_not_in_ids(ids:) sql = GET_AUTHORS_NOT_IN_IDS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) stmt = client.prepare(sql) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| GetAuthorsNotInIdsRow.new(row[0], row[1], row[2]) end @@ -1289,7 +1289,7 @@ def create_author_or_rollback(id:, name:) query_params = [id, name] stmt = client.prepare(CREATE_AUTHOR_OR_ROLLBACK_SQL) begin - stmt.execute(*query_params) + execute_statement(stmt, *query_params) nil ensure stmt.close @@ -1308,7 +1308,7 @@ def many_params(c0:, c1:, c2:, c3:, c4:) ] stmt = client.prepare(MANY_PARAMS_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) row = result.first row && ManyParamsRow.new(row[0], row[1]) ensure @@ -1327,7 +1327,7 @@ def many_rows(c0:) query_params = [c0] stmt = client.prepare(MANY_ROWS_SQL) begin - result = stmt.execute(*query_params) + result = execute_statement(stmt, *query_params) result.map do |row| ManyRowsRow.new( row[0], @@ -1347,7 +1347,7 @@ def four_cols with_client('four_cols') do |client| stmt = client.prepare(FOUR_COLS_SQL) begin - result = stmt.execute + result = execute_statement(stmt) result.map do |row| FourColsRow.new(row[0], row[1], row[2], row[3]) end @@ -1375,6 +1375,14 @@ def with_client(name, &block) instrument(name) { @lock.synchronize { block.call(@db) } } end + def execute_statement(stmt, *params) + stmt.reset! if stmt.active? || stmt.done? + stmt.bind_params(*params) unless params.empty? + result = SQLite3::ResultSet.new(@db, stmt) + stmt.step if stmt.column_count.zero? + result + end + def timeout_exits_with_throw? !!(defined?(::Timeout::VERSION) && (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) diff --git a/test/synthetic_requests.rb b/test/synthetic_requests.rb index ac8af9e..38d19ae 100644 --- a/test/synthetic_requests.rb +++ b/test/synthetic_requests.rb @@ -19,7 +19,9 @@ def column(name, type, not_null: true) end def query(name, cmd, columns, params, file = 'query.sql') - P::Query.new(name: name, cmd: cmd, text: "SELECT 1 -- ? ?\n\t WHERE a = ?", columns: columns, filename: file, + # a placeholder for real only when the query has parameters (sqlc never reports fewer parameters than that) + text = "SELECT 1 -- ? ?\n\t WHERE a = #{params.empty? ? '1' : '?'}" + P::Query.new(name: name, cmd: cmd, text: text, columns: columns, filename: file, params: params.each_with_index.map { |c, i| P::Parameter.new(number: i + 1, column: c) }) end