From 2fafeb6bdc1ac8c171938796e0a8d393b3e7f679 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:05:51 +0000 Subject: [PATCH] Fix bugs found by the second pre-release check - pg: prepared statement names include the module, so modules sharing a connection no longer run each other's SQL - pg: unnamed result columns (SELECT 1, now()) are read by position and named column_1, column_2, ... (also on mysql2/sqlite3, where two of them failed generation) - pg: aliases with a quote no longer generate invalid Ruby - pg: top-level Float json/jsonb parameters keep their exact value - pg: a query abandoned by Timeout / Thread#raise is cancelled before the connection is reused - mysql2/sqlite3: a transaction already ended by the database (deadlock, ON CONFLICT ROLLBACK) raises the original error instead of the failed ROLLBACK's - mysql2/sqlite3: NOT IN (sqlc.slice(ids)) with [] matches every row - mysql2: Symbol parameters are bound as their name Co-Authored-By: Doron Eli Rachman --- README.md | 3 +- docs/Coverage.md | 5 + examples/authors/mysql/query.sql | 9 + examples/authors/postgresql/query.sql | 6 + examples/authors/sqlite/query.sql | 12 + examples/mysql2/query_sql.rb | 72 ++- examples/mysql2/query_sql.rbs | 34 ++ examples/pg/query_sql.rb | 528 +++++++++-------- examples/pg/query_sql.rbs | 23 + examples/sqlite3/query_sql.rb | 96 ++- examples/sqlite3/query_sql.rbs | 38 ++ lib/sqlc_gen_ruby/code.rb | 3 + lib/sqlc_gen_ruby/drivers/mysql2.rb | 10 +- lib/sqlc_gen_ruby/drivers/pg.rb | 16 +- lib/sqlc_gen_ruby/drivers/sqlite3.rb | 2 +- lib/sqlc_gen_ruby/generator.rb | 2 +- lib/sqlc_gen_ruby/method_gen.rb | 9 +- lib/sqlc_gen_ruby/row_layout.rb | 16 +- steep_expectations.yml | 188 +++--- test/features_test.rb | 6 +- test/fixtures/mysql2.bin | 10 +- test/fixtures/pg.bin | 5 +- test/fixtures/sqlite3.bin | Bin 16298 -> 17014 bytes test/golden/mysql2/default/my_queries_sql.rb | 10 +- test/golden/mysql2/default/other_sql.rb | 10 +- test/golden/mysql2/default/query_sql.rb | 72 ++- test/golden/mysql2/default/query_sql.rbs | 34 ++ .../mysql2/ruby31_gemfile/my_queries_sql.rb | 10 +- .../golden/mysql2/ruby31_gemfile/other_sql.rb | 10 +- .../golden/mysql2/ruby31_gemfile/query_sql.rb | 69 ++- .../mysql2/ruby31_gemfile/query_sql.rbs | 37 ++ .../mysql2/ruby32_notypes/my_queries_sql.rb | 10 +- .../golden/mysql2/ruby32_notypes/other_sql.rb | 10 +- .../golden/mysql2/ruby32_notypes/query_sql.rb | 72 ++- test/golden/pg/default/my_queries_sql.rb | 17 +- test/golden/pg/default/other_sql.rb | 33 +- test/golden/pg/default/query_sql.rb | 552 ++++++++++-------- test/golden/pg/default/query_sql.rbs | 23 + .../pg/ruby31_gemfile/my_queries_sql.rb | 17 +- test/golden/pg/ruby31_gemfile/other_sql.rb | 33 +- test/golden/pg/ruby31_gemfile/query_sql.rb | 550 +++++++++-------- test/golden/pg/ruby31_gemfile/query_sql.rbs | 25 + .../pg/ruby32_notypes/my_queries_sql.rb | 17 +- test/golden/pg/ruby32_notypes/other_sql.rb | 33 +- test/golden/pg/ruby32_notypes/query_sql.rb | 552 ++++++++++-------- test/golden/sqlite3/default/my_queries_sql.rb | 2 +- test/golden/sqlite3/default/other_sql.rb | 2 +- test/golden/sqlite3/default/query_sql.rb | 96 ++- test/golden/sqlite3/default/query_sql.rbs | 38 ++ .../sqlite3/ruby31_gemfile/my_queries_sql.rb | 2 +- .../sqlite3/ruby31_gemfile/other_sql.rb | 2 +- .../sqlite3/ruby31_gemfile/query_sql.rb | 93 ++- .../sqlite3/ruby31_gemfile/query_sql.rbs | 41 ++ .../sqlite3/ruby32_notypes/my_queries_sql.rb | 2 +- .../sqlite3/ruby32_notypes/other_sql.rb | 2 +- .../sqlite3/ruby32_notypes/query_sql.rb | 96 ++- tests/end2end_mysql2.rb | 45 +- tests/end2end_options.rb | 8 + tests/end2end_pg.rb | 34 ++ tests/end2end_sqlite3.rb | 31 + tests/options/pg_second_queries/first.sql | 5 + tests/options/sqlc.yaml | 22 + 62 files changed, 2645 insertions(+), 1165 deletions(-) create mode 100644 tests/options/pg_second_queries/first.sql diff --git a/README.md b/README.md index 9f09c74..6e4948a 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,7 @@ per table, one module of constants per enum), `connection.rb` (shared helpers) a * **Style:** SQL lives in frozen heredoc constants (`GET_AUTHOR_SQL`), comments above a query become its doc comment, table / column / enum comments of the schema become doc comments of the models. The code is RuboCop-clean; where a name from your schema breaks a RuboCop naming rule (`"UserId"`, `naïve`), that class or method gets a `rubocop:disable`. + A result column without a usable name (`SELECT 1, now()`) is named by its position: `column_1`, `column_2`, ... ### Transactions ```ruby @@ -160,7 +161,7 @@ server; duplicate keys and invalid values raise `Mysql2::Error` instead of being |---|:---:|:---:|:---:|---| | `sqlc.arg` | ✅ | ✅ | ✅ | names a parameter (unnamed ones such as `ANY($1::BIGINT[])` become `param1`, ...) | | `sqlc.narg` | ✅ | ✅ | ✅ | a nullable parameter (`T?`, defaults to `nil`) | -| `sqlc.slice` | 🚫 | ✅ | ✅ | `IN (sqlc.slice(ids))` takes an `Array` (an empty one matches nothing). PostgreSQL: use `= ANY($1::bigint[])` with an array | +| `sqlc.slice` | 🚫 | ✅ | ✅ | `IN (sqlc.slice(ids))` takes an `Array` (an empty one: `IN` matches nothing, `NOT IN` everything). PostgreSQL: use `= ANY($1::bigint[])` with an array | | `sqlc.embed` | ✅ | ✅ | ✅ | the row gets an attribute holding the table's model (`author`, `author2` for the same table twice); `nil` when an outer join found no row | 🚫 = not offered by sqlc for that database. diff --git a/docs/Coverage.md b/docs/Coverage.md index 20a08d7..ae5efe2 100644 --- a/docs/Coverage.md +++ b/docs/Coverage.md @@ -28,5 +28,10 @@ only when the behavior is rejected or decided during generation and generated co | Fiber scheduler concurrency / MySQL no prepared-statement leaks | [`test_queries_run_concurrently_under_the_fiber_scheduler`](../tests/end2end_pg.rb#test_queries_run_concurrently_under_the_fiber_scheduler) | [`test_generated_queries_overlap_under_sync_including_transactions`](../tests/end2end_mysql2.rb#test_generated_queries_overlap_under_sync_including_transactions), [`test_generated_queries_do_not_create_server_side_prepared_statements`](../tests/end2end_mysql2.rb#test_generated_queries_do_not_create_server_side_prepared_statements) | [`test_fibers_sharing_a_database_take_turns`](../tests/end2end_sqlite3.rb#test_fibers_sharing_a_database_take_turns) | | | Duplicate result columns, unsupported options, invalid `:copyfrom` clauses and generation errors | E2E cannot reach generation-time rejection | E2E cannot reach generation-time rejection | E2E cannot reach generation-time rejection | [`test_duplicate_result_columns_are_rejected`](../test/features_test.rb#test_duplicate_result_columns_are_rejected), [`test_copyfrom_keeps_or_rejects_conflict_clauses`](../test/features_test.rb#test_copyfrom_keeps_or_rejects_conflict_clauses), [`test_unknown_options_are_rejected_with_a_hint`](../test/options_test.rb#test_unknown_options_are_rejected_with_a_hint), [`test_query_names_cannot_replace_generated_methods`](../test/features_test.rb#test_query_names_cannot_replace_generated_methods) | | RuboCop style, query/schema comments and source SQL constants | [`test_flow`](../tests/end2end_pg.rb#test_flow) | [`test_flow`](../tests/end2end_mysql2.rb#test_flow) | [`test_flow`](../tests/end2end_sqlite3.rb#test_flow) | [`test_generated_ruby_is_formatted_like_ruby`](../test/features_test.rb#test_generated_ruby_is_formatted_like_ruby), [`test_query_comments_become_doc_comments`](../test/features_test.rb#test_query_comments_become_doc_comments), [`test_table_column_and_enum_comments_become_doc_comments`](../test/features_test.rb#test_table_column_and_enum_comments_become_doc_comments), [`test_sql_is_a_frozen_raw_heredoc`](../test/features_test.rb#test_sql_is_a_frozen_raw_heredoc) | +| Unnamed result columns (`column_1`, ...) and aliases with quotes | [`test_unnamed_result_columns_are_read_by_position`](../tests/end2end_pg.rb#test_unnamed_result_columns_are_read_by_position), [`test_alias_with_a_quote_is_read`](../tests/end2end_pg.rb#test_alias_with_a_quote_is_read) | [`test_unnamed_result_columns_are_read_by_position`](../tests/end2end_mysql2.rb#test_unnamed_result_columns_are_read_by_position), [`test_alias_with_a_quote_is_read`](../tests/end2end_mysql2.rb#test_alias_with_a_quote_is_read) | [`test_unnamed_result_columns_are_read_by_position`](../tests/end2end_sqlite3.rb#test_unnamed_result_columns_are_read_by_position), [`test_alias_with_a_quote_is_read`](../tests/end2end_sqlite3.rb#test_alias_with_a_quote_is_read) | | +| Empty `sqlc.slice`: `IN` matches nothing, `NOT IN` everything | unit: PostgreSQL uses arrays instead of slices | [`test_empty_slice_matches_nothing`](../tests/end2end_mysql2.rb#test_empty_slice_matches_nothing), [`test_not_in_an_empty_slice_matches_everything`](../tests/end2end_mysql2.rb#test_not_in_an_empty_slice_matches_everything) | [`test_empty_slice_matches_nothing`](../tests/end2end_sqlite3.rb#test_empty_slice_matches_nothing), [`test_not_in_an_empty_slice_matches_everything`](../tests/end2end_sqlite3.rb#test_not_in_an_empty_slice_matches_everything) | | +| 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) | 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/authors/mysql/query.sql b/examples/authors/mysql/query.sql index e8a4d61..44d67bb 100644 --- a/examples/authors/mysql/query.sql +++ b/examples/authors/mysql/query.sql @@ -199,3 +199,12 @@ SELECT * FROM name_cases WHERE id = sqlc.arg(id); -- name: TruncateNameCases :exec TRUNCATE TABLE name_cases; + +-- name: GetUnnamedColumns :one +SELECT 1, 'two'; + +-- name: GetQuotedAlias :one +SELECT 1 AS `it's`; + +-- name: GetAuthorsNotInIds :many +SELECT * FROM authors WHERE id NOT IN (sqlc.slice('ids')) ORDER BY id; diff --git a/examples/authors/postgresql/query.sql b/examples/authors/postgresql/query.sql index 89a04d9..1eb44a2 100644 --- a/examples/authors/postgresql/query.sql +++ b/examples/authors/postgresql/query.sql @@ -238,3 +238,9 @@ SELECT * FROM name_cases WHERE id = sqlc.arg(id); -- name: TruncateNameCases :exec TRUNCATE TABLE name_cases; + +-- name: GetUnnamedColumns :one +SELECT 1, 'two'; + +-- name: GetQuotedAlias :one +SELECT 1 AS "it's"; diff --git a/examples/authors/sqlite/query.sql b/examples/authors/sqlite/query.sql index 9100a64..c2cfe57 100644 --- a/examples/authors/sqlite/query.sql +++ b/examples/authors/sqlite/query.sql @@ -198,3 +198,15 @@ SELECT * FROM name_cases WHERE id = sqlc.arg(id); -- name: TruncateNameCases :exec DELETE FROM name_cases; + +-- name: GetUnnamedColumns :one +SELECT 1, 'two'; + +-- name: GetQuotedAlias :one +SELECT 1 AS "it's"; + +-- name: GetAuthorsNotInIds :many +SELECT * FROM authors WHERE id NOT IN (sqlc.slice('ids')) ORDER BY id; + +-- name: CreateAuthorOrRollback :exec +INSERT OR ROLLBACK INTO authors (id, name) VALUES (?, ?); diff --git a/examples/mysql2/query_sql.rb b/examples/mysql2/query_sql.rb index 18c82ad..d706042 100644 --- a/examples/mysql2/query_sql.rb +++ b/examples/mysql2/query_sql.rb @@ -438,6 +438,29 @@ class GetHTTPServerRow < Data.define( TRUNCATE TABLE name_cases SQL + GET_UNNAMED_COLUMNS_SQL = <<~'SQL' + SELECT 1, 'two' + SQL + + # rubocop:disable Naming/VariableNumber + class GetUnnamedColumnsRow < Data.define(:column_1, :column_2) + end + # rubocop:enable Naming/VariableNumber + + GET_QUOTED_ALIAS_SQL = <<~'SQL' + SELECT 1 AS `it's` + SQL + + class GetQuotedAliasRow < Data.define(:it_s) + end + + GET_AUTHORS_NOT_IN_IDS_SQL = <<~'SQL' + SELECT id, name, bio FROM authors WHERE id NOT IN (/*SLICE:ids*/?) ORDER BY id + SQL + + class GetAuthorsNotInIdsRow < Data.define(:id, :name, :bio) + end + class QuerySql # Raised by the bang variants (get_author!) when the query returns no row. class NotFound < StandardError; end @@ -475,9 +498,14 @@ def transaction(isolation: nil) failed = false begin yield self - rescue Exception # rubocop:disable Lint/RescueException + rescue Exception => e # rubocop:disable Lint/RescueException failed = true - client.query(rollback) + 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) + end raise ensure begin @@ -1142,6 +1170,43 @@ def truncate_name_cases end end + def get_unnamed_columns + with_client('get_unnamed_columns') do |client| + result = client.query(bind_params(client, GET_UNNAMED_COLUMNS_SQL, []), as: :array) + row = result.first + row && GetUnnamedColumnsRow.new(row[0], row[1]) + end + end + + # Like #get_unnamed_columns, but raises NotFound when there is no row. + def get_unnamed_columns!(...) + get_unnamed_columns(...) || raise(NotFound, 'GetUnnamedColumns returned no row') + end + + def get_quoted_alias + with_client('get_quoted_alias') do |client| + result = client.query(bind_params(client, GET_QUOTED_ALIAS_SQL, []), as: :array) + row = result.first + row && GetQuotedAliasRow.new(row[0]) + end + end + + # Like #get_quoted_alias, but raises NotFound when there is no row. + def get_quoted_alias!(...) + get_quoted_alias(...) || raise(NotFound, 'GetQuotedAlias returned no row') + end + + def get_authors_not_in_ids(ids:) + with_client('get_authors_not_in_ids') do |client| + query_params = [*ids] + sql = GET_AUTHORS_NOT_IN_IDS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) + result = client.query(bind_params(client, sql, query_params), as: :array) + result.map do |row| + GetAuthorsNotInIdsRow.new(row[0], row[1], row[2]) + end + end + end + private def instrument(name) @@ -1174,6 +1239,7 @@ def with_client(name) def quote(client, value) case value when NilClass then 'NULL' + when Symbol then quote(client, value.to_s) when TrueClass then 'TRUE' when FalseClass then 'FALSE' when Integer then value.to_s @@ -1291,7 +1357,7 @@ def load_data(client, sql, strict: true) end def slice_placeholders(values) - values.empty? ? 'NULL' : Array.new(values.size, '?').join(', ') + values.empty? ? 'SELECT NULL WHERE FALSE' : Array.new(values.size, '?').join(', ') end end end diff --git a/examples/mysql2/query_sql.rbs b/examples/mysql2/query_sql.rbs index 6e6234a..93f6717 100644 --- a/examples/mysql2/query_sql.rbs +++ b/examples/mysql2/query_sql.rbs @@ -483,6 +483,30 @@ module Mysql2Codegen TRUNCATE_NAME_CASES_SQL: String + GET_UNNAMED_COLUMNS_SQL: String + + class GetUnnamedColumnsRow + attr_reader column_1: Integer + attr_reader column_2: String + def initialize: ( Integer column_1, String column_2) -> void + end + + GET_QUOTED_ALIAS_SQL: String + + class GetQuotedAliasRow + attr_reader it_s: Integer + def initialize: ( Integer it_s) -> void + end + + GET_AUTHORS_NOT_IN_IDS_SQL: String + + class GetAuthorsNotInIdsRow + attr_reader id: Integer + attr_reader name: String + attr_reader bio: String? + def initialize: ( Integer id, String name, String? bio) -> void + end + class QuerySql class NotFound < StandardError end @@ -615,6 +639,16 @@ module Mysql2Codegen def truncate_name_cases: -> void + def get_unnamed_columns: -> GetUnnamedColumnsRow? + + def get_unnamed_columns!: -> GetUnnamedColumnsRow + + def get_quoted_alias: -> GetQuotedAliasRow? + + def get_quoted_alias!: -> GetQuotedAliasRow + + def get_authors_not_in_ids: (ids: Array[Integer]) -> Array[GetAuthorsNotInIdsRow] + private def instrument: [T] (String name) { () -> T } -> T diff --git a/examples/pg/query_sql.rb b/examples/pg/query_sql.rb index 6b52d98..44500f7 100644 --- a/examples/pg/query_sql.rb +++ b/examples/pg/query_sql.rb @@ -527,6 +527,22 @@ class GetHTTPServerRow < Data.define( TRUNCATE TABLE name_cases SQL + GET_UNNAMED_COLUMNS_SQL = <<~'SQL' + SELECT 1, 'two' + SQL + + # rubocop:disable Naming/VariableNumber + class GetUnnamedColumnsRow < Data.define(:column_1, :column_2) + end + # rubocop:enable Naming/VariableNumber + + GET_QUOTED_ALIAS_SQL = <<~'SQL' + SELECT 1 AS "it's" + SQL + + class GetQuotedAliasRow < Data.define(:it_s) + end + class QuerySql # Raised by the bang variants (get_author!) when the query returns no row. class NotFound < StandardError; end @@ -564,6 +580,7 @@ def transaction(isolation: nil) yield self rescue Exception # rubocop:disable Lint/RescueException failed = true + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE client.exec(rollback) raise ensure @@ -586,11 +603,11 @@ class ExecResult < Data.define(:rows_affected, :last_insert_id) def get_author(id:) with_client('get_author') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_get_author') - client.prepare('query_sql_get_author', GET_AUTHOR_SQL) - client.prepared_statements.add('query_sql_get_author') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_author') + client.prepare('pg_codegen_query_sql_get_author', GET_AUTHOR_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_author') end - result = client.exec_prepared('query_sql_get_author', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_author', query_params) row = result.first row && GetAuthorRow.new(row['id'], row['name'], row['bio']) end @@ -603,11 +620,11 @@ def get_author!(...) def list_authors with_client('list_authors') do |client| - unless client.prepared_statements.include?('query_sql_list_authors') - client.prepare('query_sql_list_authors', LIST_AUTHORS_SQL) - client.prepared_statements.add('query_sql_list_authors') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors') + client.prepare('pg_codegen_query_sql_list_authors', LIST_AUTHORS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors') end - result = client.exec_prepared('query_sql_list_authors') + result = client.exec_prepared('pg_codegen_query_sql_list_authors') result.map do |row| ListAuthorsRow.new(row['id'], row['name'], row['bio']) end @@ -617,11 +634,11 @@ def list_authors def create_author(name:, bio:) with_client('create_author') do |client| query_params = [name, bio] - unless client.prepared_statements.include?('query_sql_create_author') - client.prepare('query_sql_create_author', CREATE_AUTHOR_SQL) - client.prepared_statements.add('query_sql_create_author') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_author') + client.prepare('pg_codegen_query_sql_create_author', CREATE_AUTHOR_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_author') end - result = client.exec_prepared('query_sql_create_author', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_author', query_params) row = result.first row && CreateAuthorRow.new(row['id'], row['name'], row['bio']) end @@ -635,22 +652,22 @@ def create_author!(...) def delete_author(id:) with_client('delete_author') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_delete_author') - client.prepare('query_sql_delete_author', DELETE_AUTHOR_SQL) - client.prepared_statements.add('query_sql_delete_author') + unless client.prepared_statements.include?('pg_codegen_query_sql_delete_author') + client.prepare('pg_codegen_query_sql_delete_author', DELETE_AUTHOR_SQL) + client.prepared_statements.add('pg_codegen_query_sql_delete_author') end - client.exec_prepared('query_sql_delete_author', query_params) + client.exec_prepared('pg_codegen_query_sql_delete_author', query_params) nil end end def test with_client('test') do |client| - unless client.prepared_statements.include?('query_sql_test') - client.prepare('query_sql_test', TEST_SQL) - client.prepared_statements.add('query_sql_test') + unless client.prepared_statements.include?('pg_codegen_query_sql_test') + client.prepare('pg_codegen_query_sql_test', TEST_SQL) + client.prepared_statements.add('pg_codegen_query_sql_test') end - result = client.exec_prepared('query_sql_test') + result = client.exec_prepared('pg_codegen_query_sql_test') row = result.first row && TestRow.new( row['c_bit'], @@ -683,11 +700,11 @@ def test!(...) def set_json_round_trip(id:, json_value:, jsonb_value:) with_client('set_json_round_trip') do |client| query_params = [id, encode_json(json_value), encode_json(jsonb_value)] - unless client.prepared_statements.include?('query_sql_set_json_round_trip') - client.prepare('query_sql_set_json_round_trip', SET_JSON_ROUND_TRIP_SQL) - client.prepared_statements.add('query_sql_set_json_round_trip') + unless client.prepared_statements.include?('pg_codegen_query_sql_set_json_round_trip') + client.prepare('pg_codegen_query_sql_set_json_round_trip', SET_JSON_ROUND_TRIP_SQL) + client.prepared_statements.add('pg_codegen_query_sql_set_json_round_trip') end - client.exec_prepared('query_sql_set_json_round_trip', query_params) + client.exec_prepared('pg_codegen_query_sql_set_json_round_trip', query_params) nil end end @@ -695,11 +712,11 @@ def set_json_round_trip(id:, json_value:, jsonb_value:) def get_json_round_trip(id:) with_client('get_json_round_trip') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_get_json_round_trip') - client.prepare('query_sql_get_json_round_trip', GET_JSON_ROUND_TRIP_SQL) - client.prepared_statements.add('query_sql_get_json_round_trip') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_json_round_trip') + client.prepare('pg_codegen_query_sql_get_json_round_trip', GET_JSON_ROUND_TRIP_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_json_round_trip') end - result = client.exec_prepared('query_sql_get_json_round_trip', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_json_round_trip', query_params) row = result.first row && GetJSONRoundTripRow.new(row['json_value'], row['jsonb_value']) end @@ -713,22 +730,22 @@ def get_json_round_trip!(...) def toggle_jsonb_round_trip(id:) with_client('toggle_jsonb_round_trip') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_toggle_jsonb_round_trip') - client.prepare('query_sql_toggle_jsonb_round_trip', TOGGLE_JSONB_ROUND_TRIP_SQL) - client.prepared_statements.add('query_sql_toggle_jsonb_round_trip') + unless client.prepared_statements.include?('pg_codegen_query_sql_toggle_jsonb_round_trip') + client.prepare('pg_codegen_query_sql_toggle_jsonb_round_trip', TOGGLE_JSONB_ROUND_TRIP_SQL) + client.prepared_statements.add('pg_codegen_query_sql_toggle_jsonb_round_trip') end - client.exec_prepared('query_sql_toggle_jsonb_round_trip', query_params) + client.exec_prepared('pg_codegen_query_sql_toggle_jsonb_round_trip', query_params) nil end end def truncate_json_round_trips with_client('truncate_json_round_trips') do |client| - unless client.prepared_statements.include?('query_sql_truncate_json_round_trips') - client.prepare('query_sql_truncate_json_round_trips', TRUNCATE_JSON_ROUND_TRIPS_SQL) - client.prepared_statements.add('query_sql_truncate_json_round_trips') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_json_round_trips') + client.prepare('pg_codegen_query_sql_truncate_json_round_trips', TRUNCATE_JSON_ROUND_TRIPS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_json_round_trips') end - client.exec_prepared('query_sql_truncate_json_round_trips') + client.exec_prepared('pg_codegen_query_sql_truncate_json_round_trips') nil end end @@ -736,11 +753,11 @@ def truncate_json_round_trips def pause_query(seconds:) with_client('pause_query') do |client| query_params = [encode_float(seconds)] - unless client.prepared_statements.include?('query_sql_pause_query') - client.prepare('query_sql_pause_query', PAUSE_QUERY_SQL) - client.prepared_statements.add('query_sql_pause_query') + unless client.prepared_statements.include?('pg_codegen_query_sql_pause_query') + client.prepare('pg_codegen_query_sql_pause_query', PAUSE_QUERY_SQL) + client.prepared_statements.add('pg_codegen_query_sql_pause_query') end - client.exec_prepared('query_sql_pause_query', query_params) + client.exec_prepared('pg_codegen_query_sql_pause_query', query_params) nil end end @@ -748,11 +765,11 @@ def pause_query(seconds:) def list_authors_paged(offset:, limit:) with_client('list_authors_paged') do |client| query_params = [offset, limit] - unless client.prepared_statements.include?('query_sql_list_authors_paged') - client.prepare('query_sql_list_authors_paged', LIST_AUTHORS_PAGED_SQL) - client.prepared_statements.add('query_sql_list_authors_paged') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors_paged') + client.prepare('pg_codegen_query_sql_list_authors_paged', LIST_AUTHORS_PAGED_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors_paged') end - result = client.exec_prepared('query_sql_list_authors_paged', query_params) + result = client.exec_prepared('pg_codegen_query_sql_list_authors_paged', query_params) result.map do |row| ListAuthorsPagedRow.new(row['id'], row['name'], row['bio']) end @@ -762,11 +779,11 @@ def list_authors_paged(offset:, limit:) def get_author_by_name_pattern(name_pattern: nil) with_client('get_author_by_name_pattern') do |client| query_params = [name_pattern] - unless client.prepared_statements.include?('query_sql_get_author_by_name_pattern') - client.prepare('query_sql_get_author_by_name_pattern', GET_AUTHOR_BY_NAME_PATTERN_SQL) - client.prepared_statements.add('query_sql_get_author_by_name_pattern') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_author_by_name_pattern') + client.prepare('pg_codegen_query_sql_get_author_by_name_pattern', GET_AUTHOR_BY_NAME_PATTERN_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_author_by_name_pattern') end - result = client.exec_prepared('query_sql_get_author_by_name_pattern', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_author_by_name_pattern', query_params) result.map do |row| GetAuthorByNamePatternRow.new(row['id'], row['name'], row['bio']) end @@ -776,11 +793,11 @@ def get_author_by_name_pattern(name_pattern: nil) def get_authors_by_optional_name(lim:, name_filter: nil) with_client('get_authors_by_optional_name') do |client| query_params = [name_filter, lim] - unless client.prepared_statements.include?('query_sql_get_authors_by_optional_name') - client.prepare('query_sql_get_authors_by_optional_name', GET_AUTHORS_BY_OPTIONAL_NAME_SQL) - client.prepared_statements.add('query_sql_get_authors_by_optional_name') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_authors_by_optional_name') + client.prepare('pg_codegen_query_sql_get_authors_by_optional_name', GET_AUTHORS_BY_OPTIONAL_NAME_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_authors_by_optional_name') end - result = client.exec_prepared('query_sql_get_authors_by_optional_name', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_authors_by_optional_name', query_params) result.map do |row| GetAuthorsByOptionalNameRow.new(row['id'], row['name'], row['bio']) end @@ -790,11 +807,11 @@ def get_authors_by_optional_name(lim:, name_filter: nil) def create_author_return_id(name:, bio:) with_client('create_author_return_id') do |client| query_params = [name, bio] - unless client.prepared_statements.include?('query_sql_create_author_return_id') - client.prepare('query_sql_create_author_return_id', CREATE_AUTHOR_RETURN_ID_SQL) - client.prepared_statements.add('query_sql_create_author_return_id') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_author_return_id') + client.prepare('pg_codegen_query_sql_create_author_return_id', CREATE_AUTHOR_RETURN_ID_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_author_return_id') end - result = client.exec_prepared('query_sql_create_author_return_id', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_author_return_id', query_params) row = result.first row && row['id'] end @@ -804,11 +821,11 @@ def create_author_return_id(name:, bio:) def update_author_bios(bio:) with_client('update_author_bios') do |client| query_params = [bio] - unless client.prepared_statements.include?('query_sql_update_author_bios') - client.prepare('query_sql_update_author_bios', UPDATE_AUTHOR_BIOS_SQL) - client.prepared_statements.add('query_sql_update_author_bios') + unless client.prepared_statements.include?('pg_codegen_query_sql_update_author_bios') + client.prepare('pg_codegen_query_sql_update_author_bios', UPDATE_AUTHOR_BIOS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_update_author_bios') end - result = client.exec_prepared('query_sql_update_author_bios', query_params) + result = client.exec_prepared('pg_codegen_query_sql_update_author_bios', query_params) result.cmd_tuples end end @@ -816,11 +833,11 @@ def update_author_bios(bio:) def get_authors_by_ids(param1:) with_client('get_authors_by_ids') do |client| query_params = [encode_array(param1)] - unless client.prepared_statements.include?('query_sql_get_authors_by_ids') - client.prepare('query_sql_get_authors_by_ids', GET_AUTHORS_BY_IDS_SQL) - client.prepared_statements.add('query_sql_get_authors_by_ids') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_authors_by_ids') + client.prepare('pg_codegen_query_sql_get_authors_by_ids', GET_AUTHORS_BY_IDS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_authors_by_ids') end - result = client.exec_prepared('query_sql_get_authors_by_ids', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_authors_by_ids', query_params) result.map do |row| GetAuthorsByIdsRow.new(row['id'], row['name'], row['bio']) end @@ -830,11 +847,11 @@ def get_authors_by_ids(param1:) def get_authors_by_ids_and_names(param1:, param2:) with_client('get_authors_by_ids_and_names') do |client| query_params = [encode_array(param1), encode_array(param2)] - unless client.prepared_statements.include?('query_sql_get_authors_by_ids_and_names') - client.prepare('query_sql_get_authors_by_ids_and_names', GET_AUTHORS_BY_IDS_AND_NAMES_SQL) - client.prepared_statements.add('query_sql_get_authors_by_ids_and_names') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_authors_by_ids_and_names') + client.prepare('pg_codegen_query_sql_get_authors_by_ids_and_names', GET_AUTHORS_BY_IDS_AND_NAMES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_authors_by_ids_and_names') end - result = client.exec_prepared('query_sql_get_authors_by_ids_and_names', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_authors_by_ids_and_names', query_params) result.map do |row| GetAuthorsByIdsAndNamesRow.new(row['id'], row['name'], row['bio']) end @@ -856,11 +873,11 @@ def copy_authors(rows) def truncate_authors with_client('truncate_authors') do |client| - unless client.prepared_statements.include?('query_sql_truncate_authors') - client.prepare('query_sql_truncate_authors', TRUNCATE_AUTHORS_SQL) - client.prepared_statements.add('query_sql_truncate_authors') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_authors') + client.prepare('pg_codegen_query_sql_truncate_authors', TRUNCATE_AUTHORS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_authors') end - client.exec_prepared('query_sql_truncate_authors') + client.exec_prepared('pg_codegen_query_sql_truncate_authors') nil end end @@ -868,11 +885,11 @@ def truncate_authors def create_book(name:, author_id:) with_client('create_book') do |client| query_params = [name, author_id] - unless client.prepared_statements.include?('query_sql_create_book') - client.prepare('query_sql_create_book', CREATE_BOOK_SQL) - client.prepared_statements.add('query_sql_create_book') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_book') + client.prepare('pg_codegen_query_sql_create_book', CREATE_BOOK_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_book') end - result = client.exec_prepared('query_sql_create_book', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_book', query_params) row = result.first row && row['id'] end @@ -881,33 +898,33 @@ def create_book(name:, author_id:) def create_deferred_book(author_id:) with_client('create_deferred_book') do |client| query_params = [author_id] - unless client.prepared_statements.include?('query_sql_create_deferred_book') - client.prepare('query_sql_create_deferred_book', CREATE_DEFERRED_BOOK_SQL) - client.prepared_statements.add('query_sql_create_deferred_book') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_deferred_book') + client.prepare('pg_codegen_query_sql_create_deferred_book', CREATE_DEFERRED_BOOK_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_deferred_book') end - client.exec_prepared('query_sql_create_deferred_book', query_params) + client.exec_prepared('pg_codegen_query_sql_create_deferred_book', query_params) nil end end def truncate_deferred_books with_client('truncate_deferred_books') do |client| - unless client.prepared_statements.include?('query_sql_truncate_deferred_books') - client.prepare('query_sql_truncate_deferred_books', TRUNCATE_DEFERRED_BOOKS_SQL) - client.prepared_statements.add('query_sql_truncate_deferred_books') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_deferred_books') + client.prepare('pg_codegen_query_sql_truncate_deferred_books', TRUNCATE_DEFERRED_BOOKS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_deferred_books') end - client.exec_prepared('query_sql_truncate_deferred_books') + client.exec_prepared('pg_codegen_query_sql_truncate_deferred_books') nil end end def list_all_authors_books with_client('list_all_authors_books') do |client| - unless client.prepared_statements.include?('query_sql_list_all_authors_books') - client.prepare('query_sql_list_all_authors_books', LIST_ALL_AUTHORS_BOOKS_SQL) - client.prepared_statements.add('query_sql_list_all_authors_books') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_all_authors_books') + client.prepare('pg_codegen_query_sql_list_all_authors_books', LIST_ALL_AUTHORS_BOOKS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_all_authors_books') end - result = client.exec_prepared('query_sql_list_all_authors_books').values + result = client.exec_prepared('pg_codegen_query_sql_list_all_authors_books').values result.map do |row| ListAllAuthorsBooksRow.new( Author.new(row[0], row[1], row[2]), @@ -919,11 +936,11 @@ def list_all_authors_books def get_duplicate_authors with_client('get_duplicate_authors') do |client| - unless client.prepared_statements.include?('query_sql_get_duplicate_authors') - client.prepare('query_sql_get_duplicate_authors', GET_DUPLICATE_AUTHORS_SQL) - client.prepared_statements.add('query_sql_get_duplicate_authors') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_duplicate_authors') + client.prepare('pg_codegen_query_sql_get_duplicate_authors', GET_DUPLICATE_AUTHORS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_duplicate_authors') end - result = client.exec_prepared('query_sql_get_duplicate_authors').values + result = client.exec_prepared('pg_codegen_query_sql_get_duplicate_authors').values result.map do |row| GetDuplicateAuthorsRow.new(Author.new(row[0], row[1], row[2]), Author.new(row[3], row[4], row[5])) end @@ -933,11 +950,11 @@ def get_duplicate_authors def get_authors_by_book_name(name:) with_client('get_authors_by_book_name') do |client| query_params = [name] - unless client.prepared_statements.include?('query_sql_get_authors_by_book_name') - client.prepare('query_sql_get_authors_by_book_name', GET_AUTHORS_BY_BOOK_NAME_SQL) - client.prepared_statements.add('query_sql_get_authors_by_book_name') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_authors_by_book_name') + client.prepare('pg_codegen_query_sql_get_authors_by_book_name', GET_AUTHORS_BY_BOOK_NAME_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_authors_by_book_name') end - result = client.exec_prepared('query_sql_get_authors_by_book_name', query_params).values + result = client.exec_prepared('pg_codegen_query_sql_get_authors_by_book_name', query_params).values result.map do |row| GetAuthorsByBookNameRow.new(row[0], row[1], row[2], Book.new(row[3], row[4], row[5], row[6])) end @@ -947,11 +964,11 @@ def get_authors_by_book_name(name:) def create_extended_bio(author_name:, name:, bio_type:) with_client('create_extended_bio') do |client| query_params = [author_name, name, bio_type] - unless client.prepared_statements.include?('query_sql_create_extended_bio') - client.prepare('query_sql_create_extended_bio', CREATE_EXTENDED_BIO_SQL) - client.prepared_statements.add('query_sql_create_extended_bio') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_extended_bio') + client.prepare('pg_codegen_query_sql_create_extended_bio', CREATE_EXTENDED_BIO_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_extended_bio') end - client.exec_prepared('query_sql_create_extended_bio', query_params) + client.exec_prepared('pg_codegen_query_sql_create_extended_bio', query_params) nil end end @@ -959,11 +976,11 @@ def create_extended_bio(author_name:, name:, bio_type:) def get_first_extended_bio_by_type(bio_type:) with_client('get_first_extended_bio_by_type') do |client| query_params = [bio_type] - unless client.prepared_statements.include?('query_sql_get_first_extended_bio_by_type') - client.prepare('query_sql_get_first_extended_bio_by_type', GET_FIRST_EXTENDED_BIO_BY_TYPE_SQL) - client.prepared_statements.add('query_sql_get_first_extended_bio_by_type') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_first_extended_bio_by_type') + client.prepare('pg_codegen_query_sql_get_first_extended_bio_by_type', GET_FIRST_EXTENDED_BIO_BY_TYPE_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_first_extended_bio_by_type') end - result = client.exec_prepared('query_sql_get_first_extended_bio_by_type', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_first_extended_bio_by_type', query_params) row = result.first row && GetFirstExtendedBioByTypeRow.new(row['author_name'], row['name'], row['bio_type']) end @@ -976,11 +993,11 @@ def get_first_extended_bio_by_type!(...) def truncate_extended_bios with_client('truncate_extended_bios') do |client| - unless client.prepared_statements.include?('query_sql_truncate_extended_bios') - client.prepare('query_sql_truncate_extended_bios', TRUNCATE_EXTENDED_BIOS_SQL) - client.prepared_statements.add('query_sql_truncate_extended_bios') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_extended_bios') + client.prepare('pg_codegen_query_sql_truncate_extended_bios', TRUNCATE_EXTENDED_BIOS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_extended_bios') end - client.exec_prepared('query_sql_truncate_extended_bios') + client.exec_prepared('pg_codegen_query_sql_truncate_extended_bios') nil end end @@ -997,22 +1014,22 @@ def insert_extra_types(c_uuid:, c_jsonb:, c_text_array:, c_integer_array:, c_tim c_money, c_not_null ] - unless client.prepared_statements.include?('query_sql_insert_extra_types') - client.prepare('query_sql_insert_extra_types', INSERT_EXTRA_TYPES_SQL) - client.prepared_statements.add('query_sql_insert_extra_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_extra_types') + client.prepare('pg_codegen_query_sql_insert_extra_types', INSERT_EXTRA_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_extra_types') end - client.exec_prepared('query_sql_insert_extra_types', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_extra_types', query_params) nil end end def get_extra_types with_client('get_extra_types') do |client| - unless client.prepared_statements.include?('query_sql_get_extra_types') - client.prepare('query_sql_get_extra_types', GET_EXTRA_TYPES_SQL) - client.prepared_statements.add('query_sql_get_extra_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_extra_types') + client.prepare('pg_codegen_query_sql_get_extra_types', GET_EXTRA_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_extra_types') end - result = client.exec_prepared('query_sql_get_extra_types') + result = client.exec_prepared('pg_codegen_query_sql_get_extra_types') row = result.first row && GetExtraTypesRow.new( row['c_uuid'], @@ -1034,11 +1051,11 @@ def get_extra_types!(...) def truncate_extra_types with_client('truncate_extra_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_extra_types') - client.prepare('query_sql_truncate_extra_types', TRUNCATE_EXTRA_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_extra_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_extra_types') + client.prepare('pg_codegen_query_sql_truncate_extra_types', TRUNCATE_EXTRA_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_extra_types') end - client.exec_prepared('query_sql_truncate_extra_types') + client.exec_prepared('pg_codegen_query_sql_truncate_extra_types') nil end end @@ -1060,22 +1077,22 @@ def insert_remaining_types(c_hstore:, c_ltree:, c_regclass:, c_cidr:, c_macaddr: c_timestamptz, encode_array(c_text_matrix) ] - unless client.prepared_statements.include?('query_sql_insert_remaining_types') - client.prepare('query_sql_insert_remaining_types', INSERT_REMAINING_TYPES_SQL) - client.prepared_statements.add('query_sql_insert_remaining_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_remaining_types') + client.prepare('pg_codegen_query_sql_insert_remaining_types', INSERT_REMAINING_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_remaining_types') end - client.exec_prepared('query_sql_insert_remaining_types', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_remaining_types', query_params) nil end end def get_remaining_types with_client('get_remaining_types') do |client| - unless client.prepared_statements.include?('query_sql_get_remaining_types') - client.prepare('query_sql_get_remaining_types', GET_REMAINING_TYPES_SQL) - client.prepared_statements.add('query_sql_get_remaining_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_remaining_types') + client.prepare('pg_codegen_query_sql_get_remaining_types', GET_REMAINING_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_remaining_types') end - result = client.exec_prepared('query_sql_get_remaining_types') + result = client.exec_prepared('pg_codegen_query_sql_get_remaining_types') row = result.first row && GetRemainingTypesRow.new( row['c_hstore'], @@ -1102,11 +1119,11 @@ def get_remaining_types!(...) def truncate_remaining_types with_client('truncate_remaining_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_remaining_types') - client.prepare('query_sql_truncate_remaining_types', TRUNCATE_REMAINING_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_remaining_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_remaining_types') + client.prepare('pg_codegen_query_sql_truncate_remaining_types', TRUNCATE_REMAINING_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_remaining_types') end - client.exec_prepared('query_sql_truncate_remaining_types') + client.exec_prepared('pg_codegen_query_sql_truncate_remaining_types') nil end end @@ -1114,22 +1131,22 @@ def truncate_remaining_types def insert_array_types(c_uuid_array:, c_bio_type_array:, c_integer_matrix:) with_client('insert_array_types') do |client| query_params = [encode_array(c_uuid_array), encode_array(c_bio_type_array), encode_array(c_integer_matrix)] - unless client.prepared_statements.include?('query_sql_insert_array_types') - client.prepare('query_sql_insert_array_types', INSERT_ARRAY_TYPES_SQL) - client.prepared_statements.add('query_sql_insert_array_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_array_types') + client.prepare('pg_codegen_query_sql_insert_array_types', INSERT_ARRAY_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_array_types') end - client.exec_prepared('query_sql_insert_array_types', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_array_types', query_params) nil end end def get_array_types with_client('get_array_types') do |client| - unless client.prepared_statements.include?('query_sql_get_array_types') - client.prepare('query_sql_get_array_types', GET_ARRAY_TYPES_SQL) - client.prepared_statements.add('query_sql_get_array_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_array_types') + client.prepare('pg_codegen_query_sql_get_array_types', GET_ARRAY_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_array_types') end - result = client.exec_prepared('query_sql_get_array_types') + result = client.exec_prepared('pg_codegen_query_sql_get_array_types') row = result.first row && GetArrayTypesRow.new(row['c_uuid_array'], row['c_bio_type_array'], row['c_integer_matrix']) end @@ -1142,22 +1159,22 @@ def get_array_types!(...) def truncate_array_types with_client('truncate_array_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_array_types') - client.prepare('query_sql_truncate_array_types', TRUNCATE_ARRAY_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_array_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_array_types') + client.prepare('pg_codegen_query_sql_truncate_array_types', TRUNCATE_ARRAY_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_array_types') end - client.exec_prepared('query_sql_truncate_array_types') + client.exec_prepared('pg_codegen_query_sql_truncate_array_types') nil end end def list_authors_with_optional_book with_client('list_authors_with_optional_book') do |client| - unless client.prepared_statements.include?('query_sql_list_authors_with_optional_book') - client.prepare('query_sql_list_authors_with_optional_book', LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL) - client.prepared_statements.add('query_sql_list_authors_with_optional_book') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors_with_optional_book') + client.prepare('pg_codegen_query_sql_list_authors_with_optional_book', LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors_with_optional_book') end - result = client.exec_prepared('query_sql_list_authors_with_optional_book').values + result = client.exec_prepared('pg_codegen_query_sql_list_authors_with_optional_book').values result.map do |row| ListAuthorsWithOptionalBookRow.new( row[0].nil? && row[1].nil? ? nil : Author.new(row[0], row[1], row[2]), @@ -1170,11 +1187,11 @@ def list_authors_with_optional_book def list_authors_between_ids(id:, id_2:) with_client('list_authors_between_ids') do |client| query_params = [id, id_2] - unless client.prepared_statements.include?('query_sql_list_authors_between_ids') - client.prepare('query_sql_list_authors_between_ids', LIST_AUTHORS_BETWEEN_IDS_SQL) - client.prepared_statements.add('query_sql_list_authors_between_ids') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors_between_ids') + client.prepare('pg_codegen_query_sql_list_authors_between_ids', LIST_AUTHORS_BETWEEN_IDS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors_between_ids') end - result = client.exec_prepared('query_sql_list_authors_between_ids', query_params) + result = client.exec_prepared('pg_codegen_query_sql_list_authors_between_ids', query_params) result.map do |row| ListAuthorsBetweenIdsRow.new(row['id'], row['name'], row['bio']) end @@ -1192,22 +1209,22 @@ def insert_special_types(c_timetz:, c_tsvector:, c_point:, c_int4range:, c_oid:, c_dimensions, c_positive ] - unless client.prepared_statements.include?('query_sql_insert_special_types') - client.prepare('query_sql_insert_special_types', INSERT_SPECIAL_TYPES_SQL) - client.prepared_statements.add('query_sql_insert_special_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_special_types') + client.prepare('pg_codegen_query_sql_insert_special_types', INSERT_SPECIAL_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_special_types') end - client.exec_prepared('query_sql_insert_special_types', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_special_types', query_params) nil end end def get_special_types with_client('get_special_types') do |client| - unless client.prepared_statements.include?('query_sql_get_special_types') - client.prepare('query_sql_get_special_types', GET_SPECIAL_TYPES_SQL) - client.prepared_statements.add('query_sql_get_special_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_special_types') + client.prepare('pg_codegen_query_sql_get_special_types', GET_SPECIAL_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_special_types') end - result = client.exec_prepared('query_sql_get_special_types') + result = client.exec_prepared('pg_codegen_query_sql_get_special_types') row = result.first row && GetSpecialTypesRow.new( row['c_timetz'], @@ -1228,11 +1245,11 @@ def get_special_types!(...) def truncate_special_types with_client('truncate_special_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_special_types') - client.prepare('query_sql_truncate_special_types', TRUNCATE_SPECIAL_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_special_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_special_types') + client.prepare('pg_codegen_query_sql_truncate_special_types', TRUNCATE_SPECIAL_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_special_types') end - client.exec_prepared('query_sql_truncate_special_types') + client.exec_prepared('pg_codegen_query_sql_truncate_special_types') nil end end @@ -1240,24 +1257,24 @@ def truncate_special_types def create_author_result(name:, bio:) with_client('create_author_result') do |client| query_params = [name, bio] - unless client.prepared_statements.include?('query_sql_create_author_result') - client.prepare('query_sql_create_author_result', CREATE_AUTHOR_RESULT_SQL) - client.prepared_statements.add('query_sql_create_author_result') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_author_result') + client.prepare('pg_codegen_query_sql_create_author_result', CREATE_AUTHOR_RESULT_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_author_result') end - result = client.exec_prepared('query_sql_create_author_result', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_author_result', query_params) ExecResult.new(result.cmd_tuples, nil) end end def create_authors_batch(rows) with_client('create_authors_batch') do |client| - unless client.prepared_statements.include?('query_sql_create_authors_batch') - client.prepare('query_sql_create_authors_batch', CREATE_AUTHORS_BATCH_SQL) - client.prepared_statements.add('query_sql_create_authors_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_authors_batch') + client.prepare('pg_codegen_query_sql_create_authors_batch', CREATE_AUTHORS_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_authors_batch') end rows.each do |args| query_params = [args.name, args.bio] - client.exec_prepared('query_sql_create_authors_batch', query_params) + client.exec_prepared('pg_codegen_query_sql_create_authors_batch', query_params) end nil end @@ -1265,13 +1282,13 @@ def create_authors_batch(rows) def get_author_batch(rows) with_client('get_author_batch') do |client| - unless client.prepared_statements.include?('query_sql_get_author_batch') - client.prepare('query_sql_get_author_batch', GET_AUTHOR_BATCH_SQL) - client.prepared_statements.add('query_sql_get_author_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_author_batch') + client.prepare('pg_codegen_query_sql_get_author_batch', GET_AUTHOR_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_author_batch') end rows.map do |args| query_params = [args.id] - result = client.exec_prepared('query_sql_get_author_batch', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_author_batch', query_params) row = result.first row && GetAuthorBatchRow.new(row['id'], row['name'], row['bio']) end @@ -1280,13 +1297,13 @@ def get_author_batch(rows) def list_authors_by_name_batch(rows) with_client('list_authors_by_name_batch') do |client| - unless client.prepared_statements.include?('query_sql_list_authors_by_name_batch') - client.prepare('query_sql_list_authors_by_name_batch', LIST_AUTHORS_BY_NAME_BATCH_SQL) - client.prepared_statements.add('query_sql_list_authors_by_name_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors_by_name_batch') + client.prepare('pg_codegen_query_sql_list_authors_by_name_batch', LIST_AUTHORS_BY_NAME_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors_by_name_batch') end rows.map do |args| query_params = [args.name] - result = client.exec_prepared('query_sql_list_authors_by_name_batch', query_params) + result = client.exec_prepared('pg_codegen_query_sql_list_authors_by_name_batch', query_params) result.map do |row| ListAuthorsByNameBatchRow.new(row['id'], row['name'], row['bio']) end @@ -1304,11 +1321,11 @@ def create_regression(id:, order:, payload:, doc:, bio_types:, uids:) encode_array(bio_types), encode_array(uids) ] - unless client.prepared_statements.include?('query_sql_create_regression') - client.prepare('query_sql_create_regression', CREATE_REGRESSION_SQL) - client.prepared_statements.add('query_sql_create_regression') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_regression') + client.prepare('pg_codegen_query_sql_create_regression', CREATE_REGRESSION_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_regression') end - result = client.exec_prepared('query_sql_create_regression', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_regression', query_params) row = result.first row && CreateRegressionRow.new( row['id'], @@ -1329,11 +1346,11 @@ def create_regression!(...) def create_regression_if_new(id:, order:) with_client('create_regression_if_new') do |client| query_params = [id, order] - unless client.prepared_statements.include?('query_sql_create_regression_if_new') - client.prepare('query_sql_create_regression_if_new', CREATE_REGRESSION_IF_NEW_SQL) - client.prepared_statements.add('query_sql_create_regression_if_new') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_regression_if_new') + client.prepare('pg_codegen_query_sql_create_regression_if_new', CREATE_REGRESSION_IF_NEW_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_regression_if_new') end - result = client.exec_prepared('query_sql_create_regression_if_new', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_regression_if_new', query_params) row = result.first row && row['id'] end @@ -1353,13 +1370,13 @@ def copy_regressions(rows) def create_regressions_batch(rows) with_client('create_regressions_batch') do |client| - unless client.prepared_statements.include?('query_sql_create_regressions_batch') - client.prepare('query_sql_create_regressions_batch', CREATE_REGRESSIONS_BATCH_SQL) - client.prepared_statements.add('query_sql_create_regressions_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_regressions_batch') + client.prepare('pg_codegen_query_sql_create_regressions_batch', CREATE_REGRESSIONS_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_regressions_batch') end rows.each do |args| query_params = [args.id, args.order, encode_bytea(args.payload), encode_json(args.doc)] - client.exec_prepared('query_sql_create_regressions_batch', query_params) + client.exec_prepared('pg_codegen_query_sql_create_regressions_batch', query_params) end nil end @@ -1368,11 +1385,11 @@ def create_regressions_batch(rows) def list_regressions(order_name: nil) with_client('list_regressions') do |client| query_params = [order_name] - unless client.prepared_statements.include?('query_sql_list_regressions') - client.prepare('query_sql_list_regressions', LIST_REGRESSIONS_SQL) - client.prepared_statements.add('query_sql_list_regressions') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_regressions') + client.prepare('pg_codegen_query_sql_list_regressions', LIST_REGRESSIONS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_regressions') end - result = client.exec_prepared('query_sql_list_regressions', query_params) + result = client.exec_prepared('pg_codegen_query_sql_list_regressions', query_params) result.map do |row| ListRegressionsRow.new( row['id'], @@ -1388,20 +1405,20 @@ def list_regressions(order_name: nil) def truncate_regressions with_client('truncate_regressions') do |client| - unless client.prepared_statements.include?('query_sql_truncate_regressions') - client.prepare('query_sql_truncate_regressions', TRUNCATE_REGRESSIONS_SQL) - client.prepared_statements.add('query_sql_truncate_regressions') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_regressions') + client.prepare('pg_codegen_query_sql_truncate_regressions', TRUNCATE_REGRESSIONS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_regressions') end - client.exec_prepared('query_sql_truncate_regressions') + client.exec_prepared('pg_codegen_query_sql_truncate_regressions') nil end end def create_batch_types_batch(rows) with_client('create_batch_types_batch') do |client| - unless client.prepared_statements.include?('query_sql_create_batch_types_batch') - client.prepare('query_sql_create_batch_types_batch', CREATE_BATCH_TYPES_BATCH_SQL) - client.prepared_statements.add('query_sql_create_batch_types_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_batch_types_batch') + client.prepare('pg_codegen_query_sql_create_batch_types_batch', CREATE_BATCH_TYPES_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_batch_types_batch') end rows.each do |args| query_params = [ @@ -1414,7 +1431,7 @@ def create_batch_types_batch(rows) encode_array(args.amounts), encode_array(args.stamps) ] - client.exec_prepared('query_sql_create_batch_types_batch', query_params) + client.exec_prepared('pg_codegen_query_sql_create_batch_types_batch', query_params) end nil end @@ -1434,11 +1451,11 @@ def copy_batch_types(rows) def list_batch_types with_client('list_batch_types') do |client| - unless client.prepared_statements.include?('query_sql_list_batch_types') - client.prepare('query_sql_list_batch_types', LIST_BATCH_TYPES_SQL) - client.prepared_statements.add('query_sql_list_batch_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_batch_types') + client.prepare('pg_codegen_query_sql_list_batch_types', LIST_BATCH_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_batch_types') end - result = client.exec_prepared('query_sql_list_batch_types') + result = client.exec_prepared('pg_codegen_query_sql_list_batch_types') result.map do |row| ListBatchTypesRow.new( row['id'], @@ -1457,11 +1474,11 @@ def list_batch_types def count_batch_types_above(min_id:) with_client('count_batch_types_above') do |client| query_params = [min_id] - unless client.prepared_statements.include?('query_sql_count_batch_types_above') - client.prepare('query_sql_count_batch_types_above', COUNT_BATCH_TYPES_ABOVE_SQL) - client.prepared_statements.add('query_sql_count_batch_types_above') + unless client.prepared_statements.include?('pg_codegen_query_sql_count_batch_types_above') + client.prepare('pg_codegen_query_sql_count_batch_types_above', COUNT_BATCH_TYPES_ABOVE_SQL) + client.prepared_statements.add('pg_codegen_query_sql_count_batch_types_above') end - result = client.exec_prepared('query_sql_count_batch_types_above', query_params) + result = client.exec_prepared('pg_codegen_query_sql_count_batch_types_above', query_params) row = result.first row && CountBatchTypesAboveRow.new(row['count']) end @@ -1474,11 +1491,11 @@ def count_batch_types_above!(...) def truncate_batch_types with_client('truncate_batch_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_batch_types') - client.prepare('query_sql_truncate_batch_types', TRUNCATE_BATCH_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_batch_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_batch_types') + client.prepare('pg_codegen_query_sql_truncate_batch_types', TRUNCATE_BATCH_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_batch_types') end - client.exec_prepared('query_sql_truncate_batch_types') + client.exec_prepared('pg_codegen_query_sql_truncate_batch_types') nil end end @@ -1495,11 +1512,11 @@ def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, h camel_case, http_server ] - unless client.prepared_statements.include?('query_sql_insert_name_case') - client.prepare('query_sql_insert_name_case', INSERT_NAME_CASE_SQL) - client.prepared_statements.add('query_sql_insert_name_case') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_name_case') + client.prepare('pg_codegen_query_sql_insert_name_case', INSERT_NAME_CASE_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_name_case') end - client.exec_prepared('query_sql_insert_name_case', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_name_case', query_params) nil end end @@ -1516,11 +1533,11 @@ def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, cam http_server, camel_case ] - unless client.prepared_statements.include?('query_sql_get_name_case') - client.prepare('query_sql_get_name_case', GET_NAME_CASE_SQL) - client.prepared_statements.add('query_sql_get_name_case') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_name_case') + client.prepare('pg_codegen_query_sql_get_name_case', GET_NAME_CASE_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_name_case') end - result = client.exec_prepared('query_sql_get_name_case', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_name_case', query_params) row = result.first row && GetNameCaseRow.new( row['id'], @@ -1543,11 +1560,11 @@ def get_name_case!(...) def get_http_server(id:) with_client('get_http_server') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_get_http_server') - client.prepare('query_sql_get_http_server', GET_HTTP_SERVER_SQL) - client.prepared_statements.add('query_sql_get_http_server') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_http_server') + client.prepare('pg_codegen_query_sql_get_http_server', GET_HTTP_SERVER_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_http_server') end - result = client.exec_prepared('query_sql_get_http_server', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_http_server', query_params) row = result.first row && GetHTTPServerRow.new( row['id'], @@ -1569,15 +1586,49 @@ def get_http_server!(...) def truncate_name_cases with_client('truncate_name_cases') do |client| - unless client.prepared_statements.include?('query_sql_truncate_name_cases') - client.prepare('query_sql_truncate_name_cases', TRUNCATE_NAME_CASES_SQL) - client.prepared_statements.add('query_sql_truncate_name_cases') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_name_cases') + client.prepare('pg_codegen_query_sql_truncate_name_cases', TRUNCATE_NAME_CASES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_name_cases') end - client.exec_prepared('query_sql_truncate_name_cases') + client.exec_prepared('pg_codegen_query_sql_truncate_name_cases') nil end end + def get_unnamed_columns + with_client('get_unnamed_columns') do |client| + unless client.prepared_statements.include?('pg_codegen_query_sql_get_unnamed_columns') + client.prepare('pg_codegen_query_sql_get_unnamed_columns', GET_UNNAMED_COLUMNS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_unnamed_columns') + end + result = client.exec_prepared('pg_codegen_query_sql_get_unnamed_columns').values + row = result.first + row && GetUnnamedColumnsRow.new(row[0], row[1]) + end + end + + # Like #get_unnamed_columns, but raises NotFound when there is no row. + def get_unnamed_columns!(...) + get_unnamed_columns(...) || raise(NotFound, 'GetUnnamedColumns returned no row') + end + + def get_quoted_alias + with_client('get_quoted_alias') do |client| + unless client.prepared_statements.include?('pg_codegen_query_sql_get_quoted_alias') + client.prepare('pg_codegen_query_sql_get_quoted_alias', GET_QUOTED_ALIAS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_quoted_alias') + end + result = client.exec_prepared('pg_codegen_query_sql_get_quoted_alias') + row = result.first + row && GetQuotedAliasRow.new(row['it\'s']) + end + end + + # Like #get_quoted_alias, but raises NotFound when there is no row. + def get_quoted_alias!(...) + get_quoted_alias(...) || raise(NotFound, 'GetQuotedAlias returned no row') + end + private def instrument(name) @@ -1597,7 +1648,13 @@ def with_client(name) @db.with do |client| client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) configure(client) unless client.is_a?(ConnectionState) - yield client + begin + yield client + rescue Exception # rubocop:disable Lint/RescueException + # a query left by Timeout / Thread#raise keeps running: cancel it before the connection is reused + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE + raise + end end end end @@ -1631,11 +1688,12 @@ def encode_bytea(value) value && PG::TextEncoder::Bytea.new.encode(value) end - # json / jsonb parameters: booleans and Arrays need JSON encoding, not PostgreSQL's boolean / array encoding. - # Hashes and JSON text pass through; nil stays nil. + # json / jsonb parameters: booleans and Arrays need JSON encoding, not PostgreSQL's boolean / array encoding, + # and Floats their shortest exact text. Hashes and JSON text pass through; nil stays nil. def encode_json(value) case value when Array, TrueClass, FalseClass then PG::TextEncoder::JSON.new.encode(value) + when Float then value.to_s else value end end diff --git a/examples/pg/query_sql.rbs b/examples/pg/query_sql.rbs index 3809e46..948598e 100644 --- a/examples/pg/query_sql.rbs +++ b/examples/pg/query_sql.rbs @@ -553,6 +553,21 @@ module PgCodegen TRUNCATE_NAME_CASES_SQL: String + GET_UNNAMED_COLUMNS_SQL: String + + class GetUnnamedColumnsRow + attr_reader column_1: Integer + attr_reader column_2: String + def initialize: ( Integer column_1, String column_2) -> void + end + + GET_QUOTED_ALIAS_SQL: String + + class GetQuotedAliasRow + attr_reader it_s: Integer + def initialize: ( Integer it_s) -> void + end + class QuerySql class NotFound < StandardError end @@ -717,6 +732,14 @@ module PgCodegen def truncate_name_cases: -> void + def get_unnamed_columns: -> GetUnnamedColumnsRow? + + def get_unnamed_columns!: -> GetUnnamedColumnsRow + + def get_quoted_alias: -> GetQuotedAliasRow? + + def get_quoted_alias!: -> GetQuotedAliasRow + private def instrument: [T] (String name) { () -> T } -> T diff --git a/examples/sqlite3/query_sql.rb b/examples/sqlite3/query_sql.rb index 8ee07ff..e7b69b5 100644 --- a/examples/sqlite3/query_sql.rb +++ b/examples/sqlite3/query_sql.rb @@ -373,6 +373,33 @@ class GetHTTPServerRow < Data.define( DELETE FROM name_cases SQL + GET_UNNAMED_COLUMNS_SQL = <<~'SQL' + SELECT 1, 'two' + SQL + + # rubocop:disable Naming/VariableNumber + class GetUnnamedColumnsRow < Data.define(:column_1, :column_2) + end + # rubocop:enable Naming/VariableNumber + + GET_QUOTED_ALIAS_SQL = <<~'SQL' + SELECT 1 AS "it's" + SQL + + class GetQuotedAliasRow < Data.define(:it_s) + end + + GET_AUTHORS_NOT_IN_IDS_SQL = <<~'SQL' + SELECT id, name, bio FROM authors WHERE id NOT IN (/*SLICE:ids*/?) ORDER BY id + SQL + + class GetAuthorsNotInIdsRow < Data.define(:id, :name, :bio) + end + + CREATE_AUTHOR_OR_ROLLBACK_SQL = <<~'SQL' + INSERT OR ROLLBACK INTO authors (id, name) VALUES (?, ?) + SQL + class QuerySql # Raised by the bang variants (get_author!) when the query returns no row. class NotFound < StandardError; end @@ -405,7 +432,7 @@ def transaction(mode: nil) yield self rescue Exception # rubocop:disable Lint/RescueException failed = true - @db.execute(rollback) + @db.execute(rollback) if @db.transaction_active? # ON CONFLICT ROLLBACK already ended it raise ensure unless failed @@ -1172,6 +1199,71 @@ def truncate_name_cases end end + def get_unnamed_columns + with_client('get_unnamed_columns') do |client| + stmt = client.prepare(GET_UNNAMED_COLUMNS_SQL) + begin + result = stmt.execute + row = result.first + row && GetUnnamedColumnsRow.new(row[0], row[1]) + ensure + stmt.close + end + end + end + + # Like #get_unnamed_columns, but raises NotFound when there is no row. + def get_unnamed_columns!(...) + get_unnamed_columns(...) || raise(NotFound, 'GetUnnamedColumns returned no row') + end + + def get_quoted_alias + with_client('get_quoted_alias') do |client| + stmt = client.prepare(GET_QUOTED_ALIAS_SQL) + begin + result = stmt.execute + row = result.first + row && GetQuotedAliasRow.new(row[0]) + ensure + stmt.close + end + end + end + + # Like #get_quoted_alias, but raises NotFound when there is no row. + def get_quoted_alias!(...) + get_quoted_alias(...) || raise(NotFound, 'GetQuotedAlias returned no row') + end + + def get_authors_not_in_ids(ids:) + with_client('get_authors_not_in_ids') do |client| + query_params = [*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.map do |row| + GetAuthorsNotInIdsRow.new(row[0], row[1], row[2]) + end + ensure + stmt.close + end + end + end + + def create_author_or_rollback(id:, name:) + with_client('create_author_or_rollback') do |client| + query_params = [id, name] + stmt = client.prepare(CREATE_AUTHOR_OR_ROLLBACK_SQL) + begin + stmt.execute(*query_params) + nil + ensure + stmt.close + end + end + end + private def instrument(name) @@ -1203,7 +1295,7 @@ def atomically(client) end def slice_placeholders(values) - values.empty? ? 'NULL' : Array.new(values.size, '?').join(', ') + values.empty? ? 'SELECT NULL WHERE FALSE' : Array.new(values.size, '?').join(', ') end end end diff --git a/examples/sqlite3/query_sql.rbs b/examples/sqlite3/query_sql.rbs index 581c04e..3aa0f02 100644 --- a/examples/sqlite3/query_sql.rbs +++ b/examples/sqlite3/query_sql.rbs @@ -371,6 +371,32 @@ module Sqlite3Codegen TRUNCATE_NAME_CASES_SQL: String + GET_UNNAMED_COLUMNS_SQL: String + + class GetUnnamedColumnsRow + attr_reader column_1: Integer + attr_reader column_2: String + def initialize: ( Integer column_1, String column_2) -> void + end + + GET_QUOTED_ALIAS_SQL: String + + class GetQuotedAliasRow + attr_reader it_s: Integer + def initialize: ( Integer it_s) -> void + end + + GET_AUTHORS_NOT_IN_IDS_SQL: String + + class GetAuthorsNotInIdsRow + attr_reader id: Integer + attr_reader name: String + attr_reader bio: String? + def initialize: ( Integer id, String name, String? bio) -> void + end + + CREATE_AUTHOR_OR_ROLLBACK_SQL: String + class QuerySql class NotFound < StandardError end @@ -491,6 +517,18 @@ module Sqlite3Codegen def truncate_name_cases: -> void + def get_unnamed_columns: -> GetUnnamedColumnsRow? + + def get_unnamed_columns!: -> GetUnnamedColumnsRow + + def get_quoted_alias: -> GetQuotedAliasRow? + + def get_quoted_alias!: -> GetQuotedAliasRow + + def get_authors_not_in_ids: (ids: Array[Integer]) -> Array[GetAuthorsNotInIdsRow] + + def create_author_or_rollback: (id: Integer, name: String) -> void + private def instrument: [T] (String name) { () -> T } -> T diff --git a/lib/sqlc_gen_ruby/code.rb b/lib/sqlc_gen_ruby/code.rb index 935ed37..1f80240 100644 --- a/lib/sqlc_gen_ruby/code.rb +++ b/lib/sqlc_gen_ruby/code.rb @@ -137,6 +137,9 @@ def comment(text) end # GetAuthor => GET_AUTHOR_SQL + # Single-quoted Ruby String literal of any text (`it's` -> 'it\\'s'). + def string_literal(text) = "'#{text.gsub(/['\\\\]/) { |c| "\\#{c}" }}'" + def sql_constant(query_name) = "#{snake_case(query_name).upcase}_SQL" def block(header, body) diff --git a/lib/sqlc_gen_ruby/drivers/mysql2.rb b/lib/sqlc_gen_ruby/drivers/mysql2.rb index 8e5f0d1..e795aaf 100644 --- a/lib/sqlc_gen_ruby/drivers/mysql2.rb +++ b/lib/sqlc_gen_ruby/drivers/mysql2.rb @@ -156,9 +156,14 @@ def transaction(isolation: nil) failed = false begin yield self - rescue Exception # rubocop:disable Lint/RescueException + rescue Exception => e # rubocop:disable Lint/RescueException failed = true - client.query(rollback) + 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) + end raise ensure begin @@ -243,6 +248,7 @@ def csv_line(values) def quote(client, value) case value when NilClass then 'NULL' + when Symbol then quote(client, value.to_s) when TrueClass then 'TRUE' when FalseClass then 'FALSE' when Integer then value.to_s diff --git a/lib/sqlc_gen_ruby/drivers/pg.rb b/lib/sqlc_gen_ruby/drivers/pg.rb index aa8a314..0909d9e 100644 --- a/lib/sqlc_gen_ruby/drivers/pg.rb +++ b/lib/sqlc_gen_ruby/drivers/pg.rb @@ -75,7 +75,7 @@ def exec_last_id_lines(exec, query) raise ArgumentError, "#{query.name}: :execlastid on PostgreSQL needs a RETURNING clause" end - ["result = #{exec}", 'row = result.first', "row && row['#{query.columns.first.name}']"] + ["result = #{exec}", 'row = result.first', "row && row[#{Code.string_literal(query.columns.first.name)}]"] end # COPY ... FROM STDIN, one row at a time through the pg gem's CopyRow encoder. @@ -115,11 +115,12 @@ def encode_bytea(value) end RUBY 'encode_json' => [<<~'RUBY'.chomp, 'def encode_json: (untyped value) -> untyped'], - # json / jsonb parameters: booleans and Arrays need JSON encoding, not PostgreSQL's boolean / array encoding. - # Hashes and JSON text pass through; nil stays nil. + # json / jsonb parameters: booleans and Arrays need JSON encoding, not PostgreSQL's boolean / array encoding, + # and Floats their shortest exact text. Hashes and JSON text pass through; nil stays nil. def encode_json(value) case value when Array, TrueClass, FalseClass then PG::TextEncoder::JSON.new.encode(value) + when Float then value.to_s else value end end @@ -224,6 +225,7 @@ def transaction(isolation: nil) yield self rescue Exception # rubocop:disable Lint/RescueException failed = true + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE client.exec(rollback) raise ensure @@ -246,7 +248,13 @@ def with_client(name) @db.with do |client| client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) configure(client) unless client.is_a?(ConnectionState) - yield client + begin + yield client + rescue Exception # rubocop:disable Lint/RescueException + # a query left by Timeout / Thread#raise keeps running: cancel it before the connection is reused + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE + raise + end end end end diff --git a/lib/sqlc_gen_ruby/drivers/sqlite3.rb b/lib/sqlc_gen_ruby/drivers/sqlite3.rb index 1e89273..9722a92 100644 --- a/lib/sqlc_gen_ruby/drivers/sqlite3.rb +++ b/lib/sqlc_gen_ruby/drivers/sqlite3.rb @@ -202,7 +202,7 @@ def transaction(mode: nil) yield self rescue Exception # rubocop:disable Lint/RescueException failed = true - @db.execute(rollback) + @db.execute(rollback) if @db.transaction_active? # ON CONFLICT ROLLBACK already ended it raise ensure unless failed diff --git a/lib/sqlc_gen_ruby/generator.rb b/lib/sqlc_gen_ruby/generator.rb index e8b7b2b..8de9479 100644 --- a/lib/sqlc_gen_ruby/generator.rb +++ b/lib/sqlc_gen_ruby/generator.rb @@ -110,7 +110,7 @@ def reject_colliding_files(class_names) end def generate_files(queries, class_name, with_models:, with_connection:) - method_gen = MethodGen.new(@driver, @layout, @types, class_name) + method_gen = MethodGen.new(@driver, @layout, @types, "#{module_name}::#{class_name}") members = queries.flat_map { |q| query_members(q, method_gen) } klass = class_members(queries, method_gen) base = Code.snake_case(class_name) diff --git a/lib/sqlc_gen_ruby/method_gen.rb b/lib/sqlc_gen_ruby/method_gen.rb index 8e35e1b..34f3a3c 100644 --- a/lib/sqlc_gen_ruby/method_gen.rb +++ b/lib/sqlc_gen_ruby/method_gen.rb @@ -8,7 +8,7 @@ module SqlcGenRuby # queries.get_author(id: 1) queries.create_author(name: 'x', bio: nil) queries.get_authors_by_ids(ids: [1, 2]) class MethodGen SLICE_HELPER = [ - "def slice_placeholders(values)\n values.empty? ? 'NULL' : Array.new(values.size, '?').join(', ')\nend", + "def slice_placeholders(values)\n values.empty? ? 'SELECT NULL WHERE FALSE' : Array.new(values.size, '?').join(', ')\nend", 'def slice_placeholders: (Array[untyped] values) -> String' ].freeze @@ -24,12 +24,13 @@ class MethodGen # Locals of the generated query methods; parameters with these names get a trailing underscore. GENERATED_LOCALS = %w[client query_params sql stmt result row].freeze - # layout: RowLayout, types: TypeResolver, class_name: the generated query class (prefixes statement names) + # layout: RowLayout, types: TypeResolver, class_name: the generated query class with its module (prefixes + # statement names, which must differ between modules that share a connection) def initialize(driver, layout, types, class_name) @driver = driver @layout = layout @types = types - @statement_prefix = Code.snake_case(class_name) + @statement_prefix = class_name.split('::').map { |part| Code.snake_case(part) }.join('_') end # `created_at > $1 AND created_at < $2` -> created_at:, created_at_2: @@ -292,7 +293,7 @@ def new_row(query) entry.optional ? "#{missing_row_check(entry.table, values)} ? nil : #{model}" : model else index += 1 - array ? "row[#{index - 1}]" : "row['#{entry.column.name}']" + array ? "row[#{index - 1}]" : "row[#{Code.string_literal(entry.column.name)}]" end [entry.attr, value] end diff --git a/lib/sqlc_gen_ruby/row_layout.rb b/lib/sqlc_gen_ruby/row_layout.rb index 1048b77..8513218 100644 --- a/lib/sqlc_gen_ruby/row_layout.rb +++ b/lib/sqlc_gen_ruby/row_layout.rb @@ -22,9 +22,9 @@ def entries(query) @cache[query.object_id] ||= build(query) end - # Names of result columns can be ambiguous (embed, joins), positions never are. + # Names of result columns can be ambiguous (embed, joins, unnamed `SELECT 1`), positions never are. def array_rows?(query) - @driver.row_data_type == :array || query.columns.any? { |c| embed?(c) } + @driver.row_data_type == :array || query.columns.any? { |c| embed?(c) || c.name.empty? } end private @@ -32,21 +32,27 @@ def array_rows?(query) def embed?(column) = !column.embed_table.nil? && !column.embed_table.name.empty? def build(query) - taken = query.columns.reject { |c| embed?(c) }.map { |c| @types.member_name(@catalog.column_name(c.name)) } + taken = query.columns.each_with_index.reject { |c, _| embed?(c) }.map { |c, i| column_attr(c, i) } # sqlc does not say which side of a join an embedded table is on: in an outer join any of them may be missing outer = query.text.match?(OUTER_JOIN) - query.columns.map do |column| + query.columns.each_with_index.map do |column, index| if embed?(column) table = @catalog.table_for(column.embed_table) or raise ArgumentError, "#{query.name}: sqlc.embed of unknown table #{column.embed_table.name}" name = unique_name(taken, @types.member_name(Code.singularize(column.name))) Entry.new(name, outer ? "#{table.model_name}?" : table.model_name, column, table, outer) else - Entry.new(@types.member_name(@catalog.column_name(column.name)), @types.type(column, query_name: query.name), column, nil, false) + Entry.new(column_attr(column, index), @types.type(column, query_name: query.name), column, nil, false) end end.tap { |list| reject_duplicate_columns(query, list) } end + # A column without a usable name (`SELECT 1, now()`) is named by its position: column_1, column_2, ... + def column_attr(column, index) + name = @types.member_name(@catalog.column_name(column.name)) + name.delete('_').empty? ? "column_#{index + 1}" : name + end + # first `author`, then `author2`, `author3`, ... (only embedded tables are renamed) def unique_name(taken, base) name = base diff --git a/steep_expectations.yml b/steep_expectations.yml index 8f0a535..bb17196 100644 --- a/steep_expectations.yml +++ b/steep_expectations.yml @@ -3,50 +3,70 @@ diagnostics: - range: start: - line: 448 + line: 471 character: 10 end: - line: 448 + line: 471 character: 24 severity: WARNING message: 'Cannot find the declaration of constant: `ConnectionPool`' code: Ruby::UnknownConstant - range: start: - line: 448 + line: 471 character: 47 end: - line: 448 + line: 471 character: 53 severity: WARNING message: 'Cannot find the declaration of constant: `Mysql2`' code: Ruby::UnknownConstant - range: start: - line: 448 + line: 471 character: 66 end: - line: 448 + line: 471 character: 81 severity: WARNING message: Empty kwargs doesn't have type annotation code: Ruby::UnannotatedEmptyCollection - range: start: - line: 1165 + line: 505 character: 17 end: - line: 1165 + line: 505 character: 23 severity: WARNING message: 'Cannot find the declaration of constant: `Mysql2`' code: Ruby::UnknownConstant - range: start: - line: 1227 + line: 507 + character: 33 + end: + line: 507 + 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 character: 20 end: - line: 1227 + line: 1293 character: 38 severity: ERROR message: |- @@ -59,20 +79,20 @@ code: Ruby::ArgumentTypeMismatch - range: start: - line: 1228 + line: 1294 character: 78 end: - line: 1228 + line: 1294 character: 84 severity: ERROR message: Type `(::String | nil)` does not have method `match?` code: Ruby::NoMethod - range: start: - line: 1230 + line: 1296 character: 20 end: - line: 1230 + line: 1296 character: 39 severity: ERROR message: |- @@ -85,10 +105,10 @@ code: Ruby::ArgumentTypeMismatch - range: start: - line: 1234 + line: 1300 character: 20 end: - line: 1234 + line: 1300 character: 39 severity: ERROR message: |- @@ -101,10 +121,10 @@ code: Ruby::ArgumentTypeMismatch - range: start: - line: 1239 + line: 1305 character: 20 end: - line: 1239 + line: 1305 character: 39 severity: ERROR message: |- @@ -117,10 +137,10 @@ code: Ruby::ArgumentTypeMismatch - range: start: - line: 1248 + line: 1314 character: 20 end: - line: 1248 + line: 1314 character: 24 severity: ERROR message: |- @@ -133,10 +153,10 @@ code: Ruby::ArgumentTypeMismatch - range: start: - line: 1281 + line: 1347 character: 16 end: - line: 1281 + line: 1347 character: 22 severity: WARNING message: 'Cannot find the declaration of constant: `Mysql2`' @@ -145,260 +165,280 @@ diagnostics: - range: start: - line: 537 + line: 553 character: 10 end: - line: 537 + line: 553 character: 24 severity: WARNING message: 'Cannot find the declaration of constant: `ConnectionPool`' code: Ruby::UnknownConstant - range: start: - line: 537 + line: 553 character: 47 end: - line: 537 + line: 553 character: 49 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 537 + line: 553 character: 58 end: - line: 537 + line: 553 character: 69 severity: WARNING message: Empty kwargs doesn't have type annotation code: Ruby::UnannotatedEmptyCollection - range: start: - line: 552 + line: 568 character: 49 end: - line: 552 + line: 568 character: 51 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 572 + line: 583 + character: 56 + end: + line: 583 + character: 58 + severity: WARNING + message: 'Cannot find the declaration of constant: `PG`' + code: Ruby::UnknownConstant + - range: + start: + line: 589 character: 51 end: - line: 572 + line: 589 character: 53 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 576 + line: 593 character: 18 end: - line: 576 + line: 593 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 847 + line: 864 character: 18 end: - line: 847 + line: 864 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1344 + line: 1361 character: 18 end: - line: 1344 + line: 1361 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1425 + line: 1442 character: 18 end: - line: 1425 + line: 1442 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1598 + line: 1649 character: 43 end: - line: 1598 + line: 1649 character: 45 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1607 + line: 1655 + character: 58 + end: + line: 1655 + character: 60 + severity: WARNING + message: 'Cannot find the declaration of constant: `PG`' + code: Ruby::UnknownConstant + - range: + start: + line: 1664 character: 17 end: - line: 1607 + line: 1664 character: 19 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1610 + line: 1667 character: 16 end: - line: 1610 + line: 1667 character: 18 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1612 + line: 1669 character: 49 end: - line: 1612 + line: 1669 character: 51 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1619 + line: 1676 character: 16 end: - line: 1619 + line: 1676 character: 18 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1620 + line: 1677 character: 15 end: - line: 1620 + line: 1677 character: 17 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1621 + line: 1678 character: 79 end: - line: 1621 + line: 1678 character: 81 severity: WARNING message: Empty array doesn't have type annotation code: Ruby::UnannotatedEmptyCollection - range: start: - line: 1622 + line: 1679 character: 25 end: - line: 1622 + line: 1679 character: 27 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1623 + line: 1680 character: 67 end: - line: 1623 + line: 1680 character: 69 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1631 + line: 1688 character: 15 end: - line: 1631 + line: 1688 character: 17 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1638 + line: 1695 character: 45 end: - line: 1638 + line: 1695 character: 47 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1653 + line: 1711 character: 6 end: - line: 1653 + line: 1711 character: 8 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1653 + line: 1711 character: 48 end: - line: 1653 + line: 1711 character: 50 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1660 + line: 1718 character: 40 end: - line: 1660 + line: 1718 character: 42 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1661 + line: 1719 character: 23 end: - line: 1661 + line: 1719 character: 25 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1662 + line: 1720 character: 42 end: - line: 1662 + line: 1720 character: 44 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1663 + line: 1721 character: 23 end: - line: 1663 + line: 1721 character: 25 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' diff --git a/test/features_test.rb b/test/features_test.rb index 5340a3a..798fa30 100644 --- a/test/features_test.rb +++ b/test/features_test.rb @@ -233,9 +233,11 @@ def test_query_comments_become_doc_comments assert_includes rb, " # Returns the author with the given id, or nil when there is none.\n def get_author(id:)" end - def test_statements_are_named_after_the_query_class + def test_statements_are_named_after_the_module_and_query_class rb = generate(load_request('pg')).fetch('query_sql.rb') - assert_includes rb, "client.exec_prepared('query_sql_get_author', query_params)" + assert_includes rb, "client.exec_prepared('pg_codegen_query_sql_get_author', query_params)" + rb = generate(load_request('pg', moduleName: 'Shop::DB')).fetch('query_sql.rb') + assert_includes rb, "client.exec_prepared('shop_db_query_sql_get_author', query_params)" end def test_pg_can_skip_prepared_statements diff --git a/test/fixtures/mysql2.bin b/test/fixtures/mysql2.bin index 8547fca..e6f8719 100644 --- a/test/fixtures/mysql2.bin +++ b/test/fixtures/mysql2.bin @@ -585,4 +585,12 @@ name_casesbtextz httpserver*3/ id0ÿÿÿÿÿÿÿÿÿ8R  name_casesbbigintzid: query.sql@ -TRUNCATE TABLE name_casesTruncateNameCases:exec: query.sql"v1.27.0*T{"driver":"mysql2","generateGemfile":false,"generateTypes":true,"rubyVersion":"3.3"} \ No newline at end of file +TRUNCATE TABLE name_casesTruncateNameCases:exec: query.sqlb +SELECT 1, 'two'GetUnnamedColumns:one"0ÿÿÿÿÿÿÿÿÿbint"0ÿÿÿÿÿÿÿÿÿbtext: query.sqlQ +SELECT 1 AS `it's`GetQuotedAlias:one" +it's0ÿÿÿÿÿÿÿÿÿbint: query.sql¯ +NSELECT id, name, bio FROM authors WHERE id NOT IN (/*SLICE:ids*/?) ORDER BY idGetAuthorsNotInIds:many"* +id0ÿÿÿÿÿÿÿÿÿR authorsbbigintzid", +name0ÿÿÿÿÿÿÿÿÿR authorsbtextzname"( +bio0ÿÿÿÿÿÿÿÿÿR authorsbtextzbio*3/ +ids0ÿÿÿÿÿÿÿÿÿ8R authorsbbiginthzid: query.sql"v1.27.0*T{"driver":"mysql2","generateGemfile":false,"generateTypes":true,"rubyVersion":"3.3"} \ No newline at end of file diff --git a/test/fixtures/pg.bin b/test/fixtures/pg.bin index 4300157..2c3ce87 100644 --- a/test/fixtures/pg.bin +++ b/test/fixtures/pg.bin @@ -644,4 +644,7 @@ name_casesbtextz HTTPServer*<8 id0ÿÿÿÿÿÿÿÿÿ8R  name_casesbpg_catalog.int8zid: query.sql@ -TRUNCATE TABLE name_casesTruncateNameCases:exec: query.sql"v1.27.0*P{"driver":"pg","generateGemfile":false,"generateTypes":true,"rubyVersion":"3.3"} \ No newline at end of file +TRUNCATE TABLE name_casesTruncateNameCases:exec: query.sqlb +SELECT 1, 'two'GetUnnamedColumns:one"0ÿÿÿÿÿÿÿÿÿbint"0ÿÿÿÿÿÿÿÿÿbtext: query.sqlQ +SELECT 1 AS "it's"GetQuotedAlias:one" +it's0ÿÿÿÿÿÿÿÿÿbint: query.sql"v1.27.0*P{"driver":"pg","generateGemfile":false,"generateTypes":true,"rubyVersion":"3.3"} \ No newline at end of file diff --git a/test/fixtures/sqlite3.bin b/test/fixtures/sqlite3.bin index 3251dc72b14030fa8e41c14edfb5e8309b8c619e..e20f78753bd16c078be39622097a636fb44f84b6 100644 GIT binary patch delta 386 zcmY+AQA>kR7>04VSYB*&s1XNhU*2r9(Hg7>A;g)4OO&Y#x{2I8kmQu#)=U*?(TVI;v5m&KQT~!?Dw4hF(qB`2XB6Jvr?u2+s z6r__d!K@gCgx}d?PJY(MjEZ=}4=KO+`TfNTTr(|W?A za;~`;q!A^a9Qf{9f;o2C|A($rf0^$E&d3(!qpo2(pc&9;3%0H)s_1)((3YBDT9>K? zy<6}-gmRUf(uXKcjEHJswqIG0Plh|!3|qCFd<&MT>pfYy&RLEL?rIDT-)q706HU00 z^`UA*Lu!H4L{%jwE+s3d8Cy{hjNI`fE{bGE#=>cdK^?B(d)fCvE&Dhuv%*=akU^=N L-PNjDU%LMV2Eu~T delta 12 Tcmey?!nmq_gO~keF{cCoCUpfV diff --git a/test/golden/mysql2/default/my_queries_sql.rb b/test/golden/mysql2/default/my_queries_sql.rb index 8cae2fb..bbafea7 100644 --- a/test/golden/mysql2/default/my_queries_sql.rb +++ b/test/golden/mysql2/default/my_queries_sql.rb @@ -51,9 +51,14 @@ def transaction(isolation: nil) failed = false begin yield self - rescue Exception # rubocop:disable Lint/RescueException + rescue Exception => e # rubocop:disable Lint/RescueException failed = true - client.query(rollback) + 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) + end raise ensure begin @@ -108,6 +113,7 @@ def with_client(name) def quote(client, value) case value when NilClass then 'NULL' + when Symbol then quote(client, value.to_s) when TrueClass then 'TRUE' when FalseClass then 'FALSE' when Integer then value.to_s diff --git a/test/golden/mysql2/default/other_sql.rb b/test/golden/mysql2/default/other_sql.rb index 3428538..9c65531 100644 --- a/test/golden/mysql2/default/other_sql.rb +++ b/test/golden/mysql2/default/other_sql.rb @@ -64,9 +64,14 @@ def transaction(isolation: nil) failed = false begin yield self - rescue Exception # rubocop:disable Lint/RescueException + rescue Exception => e # rubocop:disable Lint/RescueException failed = true - client.query(rollback) + 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) + end raise ensure begin @@ -148,6 +153,7 @@ def with_client(name) def quote(client, value) case value when NilClass then 'NULL' + when Symbol then quote(client, value.to_s) when TrueClass then 'TRUE' when FalseClass then 'FALSE' when Integer then value.to_s diff --git a/test/golden/mysql2/default/query_sql.rb b/test/golden/mysql2/default/query_sql.rb index 39d167b..a4df2c2 100644 --- a/test/golden/mysql2/default/query_sql.rb +++ b/test/golden/mysql2/default/query_sql.rb @@ -438,6 +438,29 @@ class GetHTTPServerRow < Data.define( TRUNCATE TABLE name_cases SQL + GET_UNNAMED_COLUMNS_SQL = <<~'SQL' + SELECT 1, 'two' + SQL + + # rubocop:disable Naming/VariableNumber + class GetUnnamedColumnsRow < Data.define(:column_1, :column_2) + end + # rubocop:enable Naming/VariableNumber + + GET_QUOTED_ALIAS_SQL = <<~'SQL' + SELECT 1 AS `it's` + SQL + + class GetQuotedAliasRow < Data.define(:it_s) + end + + GET_AUTHORS_NOT_IN_IDS_SQL = <<~'SQL' + SELECT id, name, bio FROM authors WHERE id NOT IN (/*SLICE:ids*/?) ORDER BY id + SQL + + class GetAuthorsNotInIdsRow < Data.define(:id, :name, :bio) + end + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -505,9 +528,14 @@ def transaction(isolation: nil) failed = false begin yield self - rescue Exception # rubocop:disable Lint/RescueException + rescue Exception => e # rubocop:disable Lint/RescueException failed = true - client.query(rollback) + 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) + end raise ensure begin @@ -1172,6 +1200,43 @@ def truncate_name_cases end end + def get_unnamed_columns + with_client('get_unnamed_columns') do |client| + result = client.query(bind_params(client, GET_UNNAMED_COLUMNS_SQL, []), as: :array) + row = result.first + row && GetUnnamedColumnsRow.new(row[0], row[1]) + end + end + + # Like #get_unnamed_columns, but raises NotFound when there is no row. + def get_unnamed_columns!(...) + get_unnamed_columns(...) || raise(NotFound, 'GetUnnamedColumns returned no row') + end + + def get_quoted_alias + with_client('get_quoted_alias') do |client| + result = client.query(bind_params(client, GET_QUOTED_ALIAS_SQL, []), as: :array) + row = result.first + row && GetQuotedAliasRow.new(row[0]) + end + end + + # Like #get_quoted_alias, but raises NotFound when there is no row. + def get_quoted_alias!(...) + get_quoted_alias(...) || raise(NotFound, 'GetQuotedAlias returned no row') + end + + def get_authors_not_in_ids(ids:) + with_client('get_authors_not_in_ids') do |client| + query_params = [*ids] + sql = GET_AUTHORS_NOT_IN_IDS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) + result = client.query(bind_params(client, sql, query_params), as: :array) + result.map do |row| + GetAuthorsNotInIdsRow.new(row[0], row[1], row[2]) + end + end + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ @@ -1249,6 +1314,7 @@ def with_client(name) def quote(client, value) case value when NilClass then 'NULL' + when Symbol then quote(client, value.to_s) when TrueClass then 'TRUE' when FalseClass then 'FALSE' when Integer then value.to_s @@ -1366,7 +1432,7 @@ def load_data(client, sql, strict: true) end def slice_placeholders(values) - values.empty? ? 'NULL' : Array.new(values.size, '?').join(', ') + values.empty? ? 'SELECT NULL WHERE FALSE' : Array.new(values.size, '?').join(', ') end end end diff --git a/test/golden/mysql2/default/query_sql.rbs b/test/golden/mysql2/default/query_sql.rbs index 615543b..7e2da97 100644 --- a/test/golden/mysql2/default/query_sql.rbs +++ b/test/golden/mysql2/default/query_sql.rbs @@ -483,6 +483,30 @@ module Mysql2Codegen TRUNCATE_NAME_CASES_SQL: String + GET_UNNAMED_COLUMNS_SQL: String + + class GetUnnamedColumnsRow + attr_reader column_1: Integer + attr_reader column_2: String + def initialize: ( Integer column_1, String column_2) -> void + end + + GET_QUOTED_ALIAS_SQL: String + + class GetQuotedAliasRow + attr_reader it_s: Integer + def initialize: ( Integer it_s) -> void + end + + GET_AUTHORS_NOT_IN_IDS_SQL: String + + class GetAuthorsNotInIdsRow + attr_reader id: Integer + attr_reader name: String + attr_reader bio: String? + def initialize: ( Integer id, String name, String? bio) -> void + end + MANY_PARAMS_SQL: String class ManyParamsRow @@ -650,6 +674,16 @@ module Mysql2Codegen def truncate_name_cases: -> void + def get_unnamed_columns: -> GetUnnamedColumnsRow? + + def get_unnamed_columns!: -> GetUnnamedColumnsRow + + def get_quoted_alias: -> GetQuotedAliasRow? + + def get_quoted_alias!: -> GetQuotedAliasRow + + def get_authors_not_in_ids: (ids: Array[Integer]) -> Array[GetAuthorsNotInIdsRow] + def many_params: (c0: Integer, c1: String, c2: Float, c3: String, c4: Integer) -> ManyParamsRow? def many_params!: (c0: Integer, c1: String, c2: Float, c3: String, c4: Integer) -> ManyParamsRow diff --git a/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rb b/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rb index 13d5741..4dcead1 100644 --- a/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rb +++ b/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rb @@ -54,9 +54,14 @@ def transaction(isolation: nil) result = yield self completed = true result - rescue Exception # rubocop:disable Lint/RescueException + rescue Exception => e # rubocop:disable Lint/RescueException failed = true - client.query(rollback) + 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) + end raise ensure begin @@ -116,6 +121,7 @@ def timeout_exits_with_throw? def quote(client, value) case value when NilClass then 'NULL' + when Symbol then quote(client, value.to_s) when TrueClass then 'TRUE' when FalseClass then 'FALSE' when Integer then value.to_s diff --git a/test/golden/mysql2/ruby31_gemfile/other_sql.rb b/test/golden/mysql2/ruby31_gemfile/other_sql.rb index 3216306..1cfa51c 100644 --- a/test/golden/mysql2/ruby31_gemfile/other_sql.rb +++ b/test/golden/mysql2/ruby31_gemfile/other_sql.rb @@ -66,9 +66,14 @@ def transaction(isolation: nil) result = yield self completed = true result - rescue Exception # rubocop:disable Lint/RescueException + rescue Exception => e # rubocop:disable Lint/RescueException failed = true - client.query(rollback) + 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) + end raise ensure begin @@ -155,6 +160,7 @@ def timeout_exits_with_throw? def quote(client, value) case value when NilClass then 'NULL' + when Symbol then quote(client, value.to_s) when TrueClass then 'TRUE' when FalseClass then 'FALSE' when Integer then value.to_s diff --git a/test/golden/mysql2/ruby31_gemfile/query_sql.rb b/test/golden/mysql2/ruby31_gemfile/query_sql.rb index 20394f5..595f4fd 100644 --- a/test/golden/mysql2/ruby31_gemfile/query_sql.rb +++ b/test/golden/mysql2/ruby31_gemfile/query_sql.rb @@ -436,6 +436,26 @@ module Mysql2Codegen TRUNCATE TABLE name_cases SQL + GET_UNNAMED_COLUMNS_SQL = <<~'SQL' + SELECT 1, 'two' + SQL + + # rubocop:disable Naming/VariableNumber + GetUnnamedColumnsRow = Struct.new(:column_1, :column_2, keyword_init: true) + # rubocop:enable Naming/VariableNumber + + GET_QUOTED_ALIAS_SQL = <<~'SQL' + SELECT 1 AS `it's` + SQL + + GetQuotedAliasRow = Struct.new(:it_s, keyword_init: true) + + GET_AUTHORS_NOT_IN_IDS_SQL = <<~'SQL' + SELECT id, name, bio FROM authors WHERE id NOT IN (/*SLICE:ids*/?) ORDER BY id + SQL + + GetAuthorsNotInIdsRow = Struct.new(:id, :name, :bio, keyword_init: true) + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -510,9 +530,14 @@ def transaction(isolation: nil) result = yield self completed = true result - rescue Exception # rubocop:disable Lint/RescueException + rescue Exception => e # rubocop:disable Lint/RescueException failed = true - client.query(rollback) + 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) + end raise ensure begin @@ -1189,6 +1214,43 @@ def truncate_name_cases end end + def get_unnamed_columns + with_client('get_unnamed_columns') do |client| + result = client.query(bind_params(client, GET_UNNAMED_COLUMNS_SQL, []), as: :array) + row = result.first + row && GetUnnamedColumnsRow.new(column_1: row[0], column_2: row[1]) + end + end + + # Like #get_unnamed_columns, but raises NotFound when there is no row. + def get_unnamed_columns!(...) + get_unnamed_columns(...) || raise(NotFound, 'GetUnnamedColumns returned no row') + end + + def get_quoted_alias + with_client('get_quoted_alias') do |client| + result = client.query(bind_params(client, GET_QUOTED_ALIAS_SQL, []), as: :array) + row = result.first + row && GetQuotedAliasRow.new(it_s: row[0]) + end + end + + # Like #get_quoted_alias, but raises NotFound when there is no row. + def get_quoted_alias!(...) + get_quoted_alias(...) || raise(NotFound, 'GetQuotedAlias returned no row') + end + + def get_authors_not_in_ids(ids:) + with_client('get_authors_not_in_ids') do |client| + query_params = [*ids] + sql = GET_AUTHORS_NOT_IN_IDS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) + result = client.query(bind_params(client, sql, query_params), as: :array) + result.map do |row| + GetAuthorsNotInIdsRow.new(id: row[0], name: row[1], bio: row[2]) + end + end + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ @@ -1271,6 +1333,7 @@ def timeout_exits_with_throw? def quote(client, value) case value when NilClass then 'NULL' + when Symbol then quote(client, value.to_s) when TrueClass then 'TRUE' when FalseClass then 'FALSE' when Integer then value.to_s @@ -1388,7 +1451,7 @@ def load_data(client, sql, strict: true) end def slice_placeholders(values) - values.empty? ? 'NULL' : Array.new(values.size, '?').join(', ') + values.empty? ? 'SELECT NULL WHERE FALSE' : Array.new(values.size, '?').join(', ') end end end diff --git a/test/golden/mysql2/ruby31_gemfile/query_sql.rbs b/test/golden/mysql2/ruby31_gemfile/query_sql.rbs index dcfc87f..3326420 100644 --- a/test/golden/mysql2/ruby31_gemfile/query_sql.rbs +++ b/test/golden/mysql2/ruby31_gemfile/query_sql.rbs @@ -606,6 +606,33 @@ module Mysql2Codegen TRUNCATE_NAME_CASES_SQL: String + GET_UNNAMED_COLUMNS_SQL: String + + class GetUnnamedColumnsRow < ::Struct[untyped] + attr_accessor column_1: Integer + attr_accessor column_2: String + def initialize: ( column_1: Integer, column_2: String) -> void + def self.new: ( column_1: Integer, column_2: String) -> GetUnnamedColumnsRow + end + + GET_QUOTED_ALIAS_SQL: String + + class GetQuotedAliasRow < ::Struct[untyped] + attr_accessor it_s: Integer + def initialize: ( it_s: Integer) -> void + def self.new: ( it_s: Integer) -> GetQuotedAliasRow + end + + GET_AUTHORS_NOT_IN_IDS_SQL: String + + class GetAuthorsNotInIdsRow < ::Struct[untyped] + attr_accessor id: Integer + attr_accessor name: String + attr_accessor bio: String? + def initialize: ( id: Integer, name: String, bio: String?) -> void + def self.new: ( id: Integer, name: String, bio: String?) -> GetAuthorsNotInIdsRow + end + MANY_PARAMS_SQL: String class ManyParamsRow < ::Struct[untyped] @@ -783,6 +810,16 @@ module Mysql2Codegen def truncate_name_cases: -> void + def get_unnamed_columns: -> GetUnnamedColumnsRow? + + def get_unnamed_columns!: -> GetUnnamedColumnsRow + + def get_quoted_alias: -> GetQuotedAliasRow? + + def get_quoted_alias!: -> GetQuotedAliasRow + + def get_authors_not_in_ids: (ids: Array[Integer]) -> Array[GetAuthorsNotInIdsRow] + def many_params: (c0: Integer, c1: String, c2: Float, c3: String, c4: Integer) -> ManyParamsRow? def many_params!: (c0: Integer, c1: String, c2: Float, c3: String, c4: Integer) -> ManyParamsRow diff --git a/test/golden/mysql2/ruby32_notypes/my_queries_sql.rb b/test/golden/mysql2/ruby32_notypes/my_queries_sql.rb index 13d5741..4dcead1 100644 --- a/test/golden/mysql2/ruby32_notypes/my_queries_sql.rb +++ b/test/golden/mysql2/ruby32_notypes/my_queries_sql.rb @@ -54,9 +54,14 @@ def transaction(isolation: nil) result = yield self completed = true result - rescue Exception # rubocop:disable Lint/RescueException + rescue Exception => e # rubocop:disable Lint/RescueException failed = true - client.query(rollback) + 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) + end raise ensure begin @@ -116,6 +121,7 @@ def timeout_exits_with_throw? def quote(client, value) case value when NilClass then 'NULL' + when Symbol then quote(client, value.to_s) when TrueClass then 'TRUE' when FalseClass then 'FALSE' when Integer then value.to_s diff --git a/test/golden/mysql2/ruby32_notypes/other_sql.rb b/test/golden/mysql2/ruby32_notypes/other_sql.rb index 66c30fd..803d65c 100644 --- a/test/golden/mysql2/ruby32_notypes/other_sql.rb +++ b/test/golden/mysql2/ruby32_notypes/other_sql.rb @@ -67,9 +67,14 @@ def transaction(isolation: nil) result = yield self completed = true result - rescue Exception # rubocop:disable Lint/RescueException + rescue Exception => e # rubocop:disable Lint/RescueException failed = true - client.query(rollback) + 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) + end raise ensure begin @@ -156,6 +161,7 @@ def timeout_exits_with_throw? def quote(client, value) case value when NilClass then 'NULL' + when Symbol then quote(client, value.to_s) when TrueClass then 'TRUE' when FalseClass then 'FALSE' when Integer then value.to_s diff --git a/test/golden/mysql2/ruby32_notypes/query_sql.rb b/test/golden/mysql2/ruby32_notypes/query_sql.rb index 47e9599..557e3d6 100644 --- a/test/golden/mysql2/ruby32_notypes/query_sql.rb +++ b/test/golden/mysql2/ruby32_notypes/query_sql.rb @@ -438,6 +438,29 @@ class GetHTTPServerRow < Data.define( TRUNCATE TABLE name_cases SQL + GET_UNNAMED_COLUMNS_SQL = <<~'SQL' + SELECT 1, 'two' + SQL + + # rubocop:disable Naming/VariableNumber + class GetUnnamedColumnsRow < Data.define(:column_1, :column_2) + end + # rubocop:enable Naming/VariableNumber + + GET_QUOTED_ALIAS_SQL = <<~'SQL' + SELECT 1 AS `it's` + SQL + + class GetQuotedAliasRow < Data.define(:it_s) + end + + GET_AUTHORS_NOT_IN_IDS_SQL = <<~'SQL' + SELECT id, name, bio FROM authors WHERE id NOT IN (/*SLICE:ids*/?) ORDER BY id + SQL + + class GetAuthorsNotInIdsRow < Data.define(:id, :name, :bio) + end + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -508,9 +531,14 @@ def transaction(isolation: nil) result = yield self completed = true result - rescue Exception # rubocop:disable Lint/RescueException + rescue Exception => e # rubocop:disable Lint/RescueException failed = true - client.query(rollback) + 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) + end raise ensure begin @@ -1175,6 +1203,43 @@ def truncate_name_cases end end + def get_unnamed_columns + with_client('get_unnamed_columns') do |client| + result = client.query(bind_params(client, GET_UNNAMED_COLUMNS_SQL, []), as: :array) + row = result.first + row && GetUnnamedColumnsRow.new(row[0], row[1]) + end + end + + # Like #get_unnamed_columns, but raises NotFound when there is no row. + def get_unnamed_columns!(...) + get_unnamed_columns(...) || raise(NotFound, 'GetUnnamedColumns returned no row') + end + + def get_quoted_alias + with_client('get_quoted_alias') do |client| + result = client.query(bind_params(client, GET_QUOTED_ALIAS_SQL, []), as: :array) + row = result.first + row && GetQuotedAliasRow.new(row[0]) + end + end + + # Like #get_quoted_alias, but raises NotFound when there is no row. + def get_quoted_alias!(...) + get_quoted_alias(...) || raise(NotFound, 'GetQuotedAlias returned no row') + end + + def get_authors_not_in_ids(ids:) + with_client('get_authors_not_in_ids') do |client| + query_params = [*ids] + sql = GET_AUTHORS_NOT_IN_IDS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) + result = client.query(bind_params(client, sql, query_params), as: :array) + result.map do |row| + GetAuthorsNotInIdsRow.new(row[0], row[1], row[2]) + end + end + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ @@ -1257,6 +1322,7 @@ def timeout_exits_with_throw? def quote(client, value) case value when NilClass then 'NULL' + when Symbol then quote(client, value.to_s) when TrueClass then 'TRUE' when FalseClass then 'FALSE' when Integer then value.to_s @@ -1374,7 +1440,7 @@ def load_data(client, sql, strict: true) end def slice_placeholders(values) - values.empty? ? 'NULL' : Array.new(values.size, '?').join(', ') + values.empty? ? 'SELECT NULL WHERE FALSE' : Array.new(values.size, '?').join(', ') end end end diff --git a/test/golden/pg/default/my_queries_sql.rb b/test/golden/pg/default/my_queries_sql.rb index 9c3f794..d5c9b11 100644 --- a/test/golden/pg/default/my_queries_sql.rb +++ b/test/golden/pg/default/my_queries_sql.rb @@ -49,6 +49,7 @@ def transaction(isolation: nil) yield self rescue Exception # rubocop:disable Lint/RescueException failed = true + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE client.exec(rollback) raise ensure @@ -67,11 +68,11 @@ def transaction(isolation: nil) def with_numbers2_go(c0:) with_client('with_numbers2_go') do |client| query_params = [c0] - unless client.prepared_statements.include?('my_queries_sql_with_numbers2_go') - client.prepare('my_queries_sql_with_numbers2_go', WITH_NUMBERS2_GO_SQL) - client.prepared_statements.add('my_queries_sql_with_numbers2_go') + unless client.prepared_statements.include?('pg_codegen_my_queries_sql_with_numbers2_go') + client.prepare('pg_codegen_my_queries_sql_with_numbers2_go', WITH_NUMBERS2_GO_SQL) + client.prepared_statements.add('pg_codegen_my_queries_sql_with_numbers2_go') end - client.exec_prepared('my_queries_sql_with_numbers2_go', query_params) + client.exec_prepared('pg_codegen_my_queries_sql_with_numbers2_go', query_params) nil end end @@ -95,7 +96,13 @@ def with_client(name) @db.with do |client| client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) configure(client) unless client.is_a?(ConnectionState) - yield client + begin + yield client + rescue Exception # rubocop:disable Lint/RescueException + # a query left by Timeout / Thread#raise keeps running: cancel it before the connection is reused + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE + raise + end end end end diff --git a/test/golden/pg/default/other_sql.rb b/test/golden/pg/default/other_sql.rb index 21eb5b6..350c52d 100644 --- a/test/golden/pg/default/other_sql.rb +++ b/test/golden/pg/default/other_sql.rb @@ -62,6 +62,7 @@ def transaction(isolation: nil) yield self rescue Exception # rubocop:disable Lint/RescueException failed = true + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE client.exec(rollback) raise ensure @@ -86,22 +87,22 @@ def no_cols(c0:, c1:, c2:, c3:, c4:) encode_float(c3), encode_bytea(c4) ] - unless client.prepared_statements.include?('other_sql_no_cols') - client.prepare('other_sql_no_cols', NO_COLS_SQL) - client.prepared_statements.add('other_sql_no_cols') + unless client.prepared_statements.include?('pg_codegen_other_sql_no_cols') + client.prepare('pg_codegen_other_sql_no_cols', NO_COLS_SQL) + client.prepared_statements.add('pg_codegen_other_sql_no_cols') end - client.exec_prepared('other_sql_no_cols', query_params) + client.exec_prepared('pg_codegen_other_sql_no_cols', query_params) nil end end def other_one with_client('other_one') do |client| - unless client.prepared_statements.include?('other_sql_other_one') - client.prepare('other_sql_other_one', OTHER_ONE_SQL) - client.prepared_statements.add('other_sql_other_one') + unless client.prepared_statements.include?('pg_codegen_other_sql_other_one') + client.prepare('pg_codegen_other_sql_other_one', OTHER_ONE_SQL) + client.prepared_statements.add('pg_codegen_other_sql_other_one') end - result = client.exec_prepared('other_sql_other_one') + result = client.exec_prepared('pg_codegen_other_sql_other_one') row = result.first row && OtherOneRow.new(row['c0']) end @@ -115,11 +116,11 @@ def other_one!(...) def count_rows(c0:) with_client('count_rows') do |client| query_params = [c0] - unless client.prepared_statements.include?('other_sql_count_rows') - client.prepare('other_sql_count_rows', COUNT_ROWS_SQL) - client.prepared_statements.add('other_sql_count_rows') + unless client.prepared_statements.include?('pg_codegen_other_sql_count_rows') + client.prepare('pg_codegen_other_sql_count_rows', COUNT_ROWS_SQL) + client.prepared_statements.add('pg_codegen_other_sql_count_rows') end - result = client.exec_prepared('other_sql_count_rows', query_params) + result = client.exec_prepared('pg_codegen_other_sql_count_rows', query_params) result.cmd_tuples end end @@ -143,7 +144,13 @@ def with_client(name) @db.with do |client| client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) configure(client) unless client.is_a?(ConnectionState) - yield client + begin + yield client + rescue Exception # rubocop:disable Lint/RescueException + # a query left by Timeout / Thread#raise keeps running: cancel it before the connection is reused + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE + raise + end end end end diff --git a/test/golden/pg/default/query_sql.rb b/test/golden/pg/default/query_sql.rb index d761b02..6d73997 100644 --- a/test/golden/pg/default/query_sql.rb +++ b/test/golden/pg/default/query_sql.rb @@ -527,6 +527,22 @@ class GetHTTPServerRow < Data.define( TRUNCATE TABLE name_cases SQL + GET_UNNAMED_COLUMNS_SQL = <<~'SQL' + SELECT 1, 'two' + SQL + + # rubocop:disable Naming/VariableNumber + class GetUnnamedColumnsRow < Data.define(:column_1, :column_2) + end + # rubocop:enable Naming/VariableNumber + + GET_QUOTED_ALIAS_SQL = <<~'SQL' + SELECT 1 AS "it's" + SQL + + class GetQuotedAliasRow < Data.define(:it_s) + end + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -594,6 +610,7 @@ def transaction(isolation: nil) yield self rescue Exception # rubocop:disable Lint/RescueException failed = true + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE client.exec(rollback) raise ensure @@ -616,11 +633,11 @@ class ExecResult < Data.define(:rows_affected, :last_insert_id) def get_author(id:) with_client('get_author') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_get_author') - client.prepare('query_sql_get_author', GET_AUTHOR_SQL) - client.prepared_statements.add('query_sql_get_author') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_author') + client.prepare('pg_codegen_query_sql_get_author', GET_AUTHOR_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_author') end - result = client.exec_prepared('query_sql_get_author', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_author', query_params) row = result.first row && GetAuthorRow.new(row['id'], row['name'], row['bio']) end @@ -633,11 +650,11 @@ def get_author!(...) def list_authors with_client('list_authors') do |client| - unless client.prepared_statements.include?('query_sql_list_authors') - client.prepare('query_sql_list_authors', LIST_AUTHORS_SQL) - client.prepared_statements.add('query_sql_list_authors') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors') + client.prepare('pg_codegen_query_sql_list_authors', LIST_AUTHORS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors') end - result = client.exec_prepared('query_sql_list_authors') + result = client.exec_prepared('pg_codegen_query_sql_list_authors') result.map do |row| ListAuthorsRow.new(row['id'], row['name'], row['bio']) end @@ -647,11 +664,11 @@ def list_authors def create_author(name:, bio:) with_client('create_author') do |client| query_params = [name, bio] - unless client.prepared_statements.include?('query_sql_create_author') - client.prepare('query_sql_create_author', CREATE_AUTHOR_SQL) - client.prepared_statements.add('query_sql_create_author') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_author') + client.prepare('pg_codegen_query_sql_create_author', CREATE_AUTHOR_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_author') end - result = client.exec_prepared('query_sql_create_author', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_author', query_params) row = result.first row && CreateAuthorRow.new(row['id'], row['name'], row['bio']) end @@ -665,22 +682,22 @@ def create_author!(...) def delete_author(id:) with_client('delete_author') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_delete_author') - client.prepare('query_sql_delete_author', DELETE_AUTHOR_SQL) - client.prepared_statements.add('query_sql_delete_author') + unless client.prepared_statements.include?('pg_codegen_query_sql_delete_author') + client.prepare('pg_codegen_query_sql_delete_author', DELETE_AUTHOR_SQL) + client.prepared_statements.add('pg_codegen_query_sql_delete_author') end - client.exec_prepared('query_sql_delete_author', query_params) + client.exec_prepared('pg_codegen_query_sql_delete_author', query_params) nil end end def test with_client('test') do |client| - unless client.prepared_statements.include?('query_sql_test') - client.prepare('query_sql_test', TEST_SQL) - client.prepared_statements.add('query_sql_test') + unless client.prepared_statements.include?('pg_codegen_query_sql_test') + client.prepare('pg_codegen_query_sql_test', TEST_SQL) + client.prepared_statements.add('pg_codegen_query_sql_test') end - result = client.exec_prepared('query_sql_test') + result = client.exec_prepared('pg_codegen_query_sql_test') row = result.first row && TestRow.new( row['c_bit'], @@ -713,11 +730,11 @@ def test!(...) def set_json_round_trip(id:, json_value:, jsonb_value:) with_client('set_json_round_trip') do |client| query_params = [id, encode_json(json_value), encode_json(jsonb_value)] - unless client.prepared_statements.include?('query_sql_set_json_round_trip') - client.prepare('query_sql_set_json_round_trip', SET_JSON_ROUND_TRIP_SQL) - client.prepared_statements.add('query_sql_set_json_round_trip') + unless client.prepared_statements.include?('pg_codegen_query_sql_set_json_round_trip') + client.prepare('pg_codegen_query_sql_set_json_round_trip', SET_JSON_ROUND_TRIP_SQL) + client.prepared_statements.add('pg_codegen_query_sql_set_json_round_trip') end - client.exec_prepared('query_sql_set_json_round_trip', query_params) + client.exec_prepared('pg_codegen_query_sql_set_json_round_trip', query_params) nil end end @@ -725,11 +742,11 @@ def set_json_round_trip(id:, json_value:, jsonb_value:) def get_json_round_trip(id:) with_client('get_json_round_trip') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_get_json_round_trip') - client.prepare('query_sql_get_json_round_trip', GET_JSON_ROUND_TRIP_SQL) - client.prepared_statements.add('query_sql_get_json_round_trip') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_json_round_trip') + client.prepare('pg_codegen_query_sql_get_json_round_trip', GET_JSON_ROUND_TRIP_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_json_round_trip') end - result = client.exec_prepared('query_sql_get_json_round_trip', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_json_round_trip', query_params) row = result.first row && GetJSONRoundTripRow.new(row['json_value'], row['jsonb_value']) end @@ -743,22 +760,22 @@ def get_json_round_trip!(...) def toggle_jsonb_round_trip(id:) with_client('toggle_jsonb_round_trip') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_toggle_jsonb_round_trip') - client.prepare('query_sql_toggle_jsonb_round_trip', TOGGLE_JSONB_ROUND_TRIP_SQL) - client.prepared_statements.add('query_sql_toggle_jsonb_round_trip') + unless client.prepared_statements.include?('pg_codegen_query_sql_toggle_jsonb_round_trip') + client.prepare('pg_codegen_query_sql_toggle_jsonb_round_trip', TOGGLE_JSONB_ROUND_TRIP_SQL) + client.prepared_statements.add('pg_codegen_query_sql_toggle_jsonb_round_trip') end - client.exec_prepared('query_sql_toggle_jsonb_round_trip', query_params) + client.exec_prepared('pg_codegen_query_sql_toggle_jsonb_round_trip', query_params) nil end end def truncate_json_round_trips with_client('truncate_json_round_trips') do |client| - unless client.prepared_statements.include?('query_sql_truncate_json_round_trips') - client.prepare('query_sql_truncate_json_round_trips', TRUNCATE_JSON_ROUND_TRIPS_SQL) - client.prepared_statements.add('query_sql_truncate_json_round_trips') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_json_round_trips') + client.prepare('pg_codegen_query_sql_truncate_json_round_trips', TRUNCATE_JSON_ROUND_TRIPS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_json_round_trips') end - client.exec_prepared('query_sql_truncate_json_round_trips') + client.exec_prepared('pg_codegen_query_sql_truncate_json_round_trips') nil end end @@ -766,11 +783,11 @@ def truncate_json_round_trips def pause_query(seconds:) with_client('pause_query') do |client| query_params = [encode_float(seconds)] - unless client.prepared_statements.include?('query_sql_pause_query') - client.prepare('query_sql_pause_query', PAUSE_QUERY_SQL) - client.prepared_statements.add('query_sql_pause_query') + unless client.prepared_statements.include?('pg_codegen_query_sql_pause_query') + client.prepare('pg_codegen_query_sql_pause_query', PAUSE_QUERY_SQL) + client.prepared_statements.add('pg_codegen_query_sql_pause_query') end - client.exec_prepared('query_sql_pause_query', query_params) + client.exec_prepared('pg_codegen_query_sql_pause_query', query_params) nil end end @@ -778,11 +795,11 @@ def pause_query(seconds:) def list_authors_paged(offset:, limit:) with_client('list_authors_paged') do |client| query_params = [offset, limit] - unless client.prepared_statements.include?('query_sql_list_authors_paged') - client.prepare('query_sql_list_authors_paged', LIST_AUTHORS_PAGED_SQL) - client.prepared_statements.add('query_sql_list_authors_paged') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors_paged') + client.prepare('pg_codegen_query_sql_list_authors_paged', LIST_AUTHORS_PAGED_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors_paged') end - result = client.exec_prepared('query_sql_list_authors_paged', query_params) + result = client.exec_prepared('pg_codegen_query_sql_list_authors_paged', query_params) result.map do |row| ListAuthorsPagedRow.new(row['id'], row['name'], row['bio']) end @@ -792,11 +809,11 @@ def list_authors_paged(offset:, limit:) def get_author_by_name_pattern(name_pattern: nil) with_client('get_author_by_name_pattern') do |client| query_params = [name_pattern] - unless client.prepared_statements.include?('query_sql_get_author_by_name_pattern') - client.prepare('query_sql_get_author_by_name_pattern', GET_AUTHOR_BY_NAME_PATTERN_SQL) - client.prepared_statements.add('query_sql_get_author_by_name_pattern') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_author_by_name_pattern') + client.prepare('pg_codegen_query_sql_get_author_by_name_pattern', GET_AUTHOR_BY_NAME_PATTERN_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_author_by_name_pattern') end - result = client.exec_prepared('query_sql_get_author_by_name_pattern', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_author_by_name_pattern', query_params) result.map do |row| GetAuthorByNamePatternRow.new(row['id'], row['name'], row['bio']) end @@ -806,11 +823,11 @@ def get_author_by_name_pattern(name_pattern: nil) def get_authors_by_optional_name(lim:, name_filter: nil) with_client('get_authors_by_optional_name') do |client| query_params = [name_filter, lim] - unless client.prepared_statements.include?('query_sql_get_authors_by_optional_name') - client.prepare('query_sql_get_authors_by_optional_name', GET_AUTHORS_BY_OPTIONAL_NAME_SQL) - client.prepared_statements.add('query_sql_get_authors_by_optional_name') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_authors_by_optional_name') + client.prepare('pg_codegen_query_sql_get_authors_by_optional_name', GET_AUTHORS_BY_OPTIONAL_NAME_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_authors_by_optional_name') end - result = client.exec_prepared('query_sql_get_authors_by_optional_name', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_authors_by_optional_name', query_params) result.map do |row| GetAuthorsByOptionalNameRow.new(row['id'], row['name'], row['bio']) end @@ -820,11 +837,11 @@ def get_authors_by_optional_name(lim:, name_filter: nil) def create_author_return_id(name:, bio:) with_client('create_author_return_id') do |client| query_params = [name, bio] - unless client.prepared_statements.include?('query_sql_create_author_return_id') - client.prepare('query_sql_create_author_return_id', CREATE_AUTHOR_RETURN_ID_SQL) - client.prepared_statements.add('query_sql_create_author_return_id') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_author_return_id') + client.prepare('pg_codegen_query_sql_create_author_return_id', CREATE_AUTHOR_RETURN_ID_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_author_return_id') end - result = client.exec_prepared('query_sql_create_author_return_id', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_author_return_id', query_params) row = result.first row && row['id'] end @@ -834,11 +851,11 @@ def create_author_return_id(name:, bio:) def update_author_bios(bio:) with_client('update_author_bios') do |client| query_params = [bio] - unless client.prepared_statements.include?('query_sql_update_author_bios') - client.prepare('query_sql_update_author_bios', UPDATE_AUTHOR_BIOS_SQL) - client.prepared_statements.add('query_sql_update_author_bios') + unless client.prepared_statements.include?('pg_codegen_query_sql_update_author_bios') + client.prepare('pg_codegen_query_sql_update_author_bios', UPDATE_AUTHOR_BIOS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_update_author_bios') end - result = client.exec_prepared('query_sql_update_author_bios', query_params) + result = client.exec_prepared('pg_codegen_query_sql_update_author_bios', query_params) result.cmd_tuples end end @@ -846,11 +863,11 @@ def update_author_bios(bio:) def get_authors_by_ids(param1:) with_client('get_authors_by_ids') do |client| query_params = [encode_array(param1)] - unless client.prepared_statements.include?('query_sql_get_authors_by_ids') - client.prepare('query_sql_get_authors_by_ids', GET_AUTHORS_BY_IDS_SQL) - client.prepared_statements.add('query_sql_get_authors_by_ids') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_authors_by_ids') + client.prepare('pg_codegen_query_sql_get_authors_by_ids', GET_AUTHORS_BY_IDS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_authors_by_ids') end - result = client.exec_prepared('query_sql_get_authors_by_ids', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_authors_by_ids', query_params) result.map do |row| GetAuthorsByIdsRow.new(row['id'], row['name'], row['bio']) end @@ -860,11 +877,11 @@ def get_authors_by_ids(param1:) def get_authors_by_ids_and_names(param1:, param2:) with_client('get_authors_by_ids_and_names') do |client| query_params = [encode_array(param1), encode_array(param2)] - unless client.prepared_statements.include?('query_sql_get_authors_by_ids_and_names') - client.prepare('query_sql_get_authors_by_ids_and_names', GET_AUTHORS_BY_IDS_AND_NAMES_SQL) - client.prepared_statements.add('query_sql_get_authors_by_ids_and_names') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_authors_by_ids_and_names') + client.prepare('pg_codegen_query_sql_get_authors_by_ids_and_names', GET_AUTHORS_BY_IDS_AND_NAMES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_authors_by_ids_and_names') end - result = client.exec_prepared('query_sql_get_authors_by_ids_and_names', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_authors_by_ids_and_names', query_params) result.map do |row| GetAuthorsByIdsAndNamesRow.new(row['id'], row['name'], row['bio']) end @@ -886,11 +903,11 @@ def copy_authors(rows) def truncate_authors with_client('truncate_authors') do |client| - unless client.prepared_statements.include?('query_sql_truncate_authors') - client.prepare('query_sql_truncate_authors', TRUNCATE_AUTHORS_SQL) - client.prepared_statements.add('query_sql_truncate_authors') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_authors') + client.prepare('pg_codegen_query_sql_truncate_authors', TRUNCATE_AUTHORS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_authors') end - client.exec_prepared('query_sql_truncate_authors') + client.exec_prepared('pg_codegen_query_sql_truncate_authors') nil end end @@ -898,11 +915,11 @@ def truncate_authors def create_book(name:, author_id:) with_client('create_book') do |client| query_params = [name, author_id] - unless client.prepared_statements.include?('query_sql_create_book') - client.prepare('query_sql_create_book', CREATE_BOOK_SQL) - client.prepared_statements.add('query_sql_create_book') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_book') + client.prepare('pg_codegen_query_sql_create_book', CREATE_BOOK_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_book') end - result = client.exec_prepared('query_sql_create_book', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_book', query_params) row = result.first row && row['id'] end @@ -911,33 +928,33 @@ def create_book(name:, author_id:) def create_deferred_book(author_id:) with_client('create_deferred_book') do |client| query_params = [author_id] - unless client.prepared_statements.include?('query_sql_create_deferred_book') - client.prepare('query_sql_create_deferred_book', CREATE_DEFERRED_BOOK_SQL) - client.prepared_statements.add('query_sql_create_deferred_book') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_deferred_book') + client.prepare('pg_codegen_query_sql_create_deferred_book', CREATE_DEFERRED_BOOK_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_deferred_book') end - client.exec_prepared('query_sql_create_deferred_book', query_params) + client.exec_prepared('pg_codegen_query_sql_create_deferred_book', query_params) nil end end def truncate_deferred_books with_client('truncate_deferred_books') do |client| - unless client.prepared_statements.include?('query_sql_truncate_deferred_books') - client.prepare('query_sql_truncate_deferred_books', TRUNCATE_DEFERRED_BOOKS_SQL) - client.prepared_statements.add('query_sql_truncate_deferred_books') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_deferred_books') + client.prepare('pg_codegen_query_sql_truncate_deferred_books', TRUNCATE_DEFERRED_BOOKS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_deferred_books') end - client.exec_prepared('query_sql_truncate_deferred_books') + client.exec_prepared('pg_codegen_query_sql_truncate_deferred_books') nil end end def list_all_authors_books with_client('list_all_authors_books') do |client| - unless client.prepared_statements.include?('query_sql_list_all_authors_books') - client.prepare('query_sql_list_all_authors_books', LIST_ALL_AUTHORS_BOOKS_SQL) - client.prepared_statements.add('query_sql_list_all_authors_books') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_all_authors_books') + client.prepare('pg_codegen_query_sql_list_all_authors_books', LIST_ALL_AUTHORS_BOOKS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_all_authors_books') end - result = client.exec_prepared('query_sql_list_all_authors_books').values + result = client.exec_prepared('pg_codegen_query_sql_list_all_authors_books').values result.map do |row| ListAllAuthorsBooksRow.new( Author.new(row[0], row[1], row[2]), @@ -949,11 +966,11 @@ def list_all_authors_books def get_duplicate_authors with_client('get_duplicate_authors') do |client| - unless client.prepared_statements.include?('query_sql_get_duplicate_authors') - client.prepare('query_sql_get_duplicate_authors', GET_DUPLICATE_AUTHORS_SQL) - client.prepared_statements.add('query_sql_get_duplicate_authors') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_duplicate_authors') + client.prepare('pg_codegen_query_sql_get_duplicate_authors', GET_DUPLICATE_AUTHORS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_duplicate_authors') end - result = client.exec_prepared('query_sql_get_duplicate_authors').values + result = client.exec_prepared('pg_codegen_query_sql_get_duplicate_authors').values result.map do |row| GetDuplicateAuthorsRow.new(Author.new(row[0], row[1], row[2]), Author.new(row[3], row[4], row[5])) end @@ -963,11 +980,11 @@ def get_duplicate_authors def get_authors_by_book_name(name:) with_client('get_authors_by_book_name') do |client| query_params = [name] - unless client.prepared_statements.include?('query_sql_get_authors_by_book_name') - client.prepare('query_sql_get_authors_by_book_name', GET_AUTHORS_BY_BOOK_NAME_SQL) - client.prepared_statements.add('query_sql_get_authors_by_book_name') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_authors_by_book_name') + client.prepare('pg_codegen_query_sql_get_authors_by_book_name', GET_AUTHORS_BY_BOOK_NAME_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_authors_by_book_name') end - result = client.exec_prepared('query_sql_get_authors_by_book_name', query_params).values + result = client.exec_prepared('pg_codegen_query_sql_get_authors_by_book_name', query_params).values result.map do |row| GetAuthorsByBookNameRow.new(row[0], row[1], row[2], Book.new(row[3], row[4], row[5], row[6])) end @@ -977,11 +994,11 @@ def get_authors_by_book_name(name:) def create_extended_bio(author_name:, name:, bio_type:) with_client('create_extended_bio') do |client| query_params = [author_name, name, bio_type] - unless client.prepared_statements.include?('query_sql_create_extended_bio') - client.prepare('query_sql_create_extended_bio', CREATE_EXTENDED_BIO_SQL) - client.prepared_statements.add('query_sql_create_extended_bio') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_extended_bio') + client.prepare('pg_codegen_query_sql_create_extended_bio', CREATE_EXTENDED_BIO_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_extended_bio') end - client.exec_prepared('query_sql_create_extended_bio', query_params) + client.exec_prepared('pg_codegen_query_sql_create_extended_bio', query_params) nil end end @@ -989,11 +1006,11 @@ def create_extended_bio(author_name:, name:, bio_type:) def get_first_extended_bio_by_type(bio_type:) with_client('get_first_extended_bio_by_type') do |client| query_params = [bio_type] - unless client.prepared_statements.include?('query_sql_get_first_extended_bio_by_type') - client.prepare('query_sql_get_first_extended_bio_by_type', GET_FIRST_EXTENDED_BIO_BY_TYPE_SQL) - client.prepared_statements.add('query_sql_get_first_extended_bio_by_type') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_first_extended_bio_by_type') + client.prepare('pg_codegen_query_sql_get_first_extended_bio_by_type', GET_FIRST_EXTENDED_BIO_BY_TYPE_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_first_extended_bio_by_type') end - result = client.exec_prepared('query_sql_get_first_extended_bio_by_type', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_first_extended_bio_by_type', query_params) row = result.first row && GetFirstExtendedBioByTypeRow.new(row['author_name'], row['name'], row['bio_type']) end @@ -1006,11 +1023,11 @@ def get_first_extended_bio_by_type!(...) def truncate_extended_bios with_client('truncate_extended_bios') do |client| - unless client.prepared_statements.include?('query_sql_truncate_extended_bios') - client.prepare('query_sql_truncate_extended_bios', TRUNCATE_EXTENDED_BIOS_SQL) - client.prepared_statements.add('query_sql_truncate_extended_bios') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_extended_bios') + client.prepare('pg_codegen_query_sql_truncate_extended_bios', TRUNCATE_EXTENDED_BIOS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_extended_bios') end - client.exec_prepared('query_sql_truncate_extended_bios') + client.exec_prepared('pg_codegen_query_sql_truncate_extended_bios') nil end end @@ -1027,22 +1044,22 @@ def insert_extra_types(c_uuid:, c_jsonb:, c_text_array:, c_integer_array:, c_tim c_money, c_not_null ] - unless client.prepared_statements.include?('query_sql_insert_extra_types') - client.prepare('query_sql_insert_extra_types', INSERT_EXTRA_TYPES_SQL) - client.prepared_statements.add('query_sql_insert_extra_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_extra_types') + client.prepare('pg_codegen_query_sql_insert_extra_types', INSERT_EXTRA_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_extra_types') end - client.exec_prepared('query_sql_insert_extra_types', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_extra_types', query_params) nil end end def get_extra_types with_client('get_extra_types') do |client| - unless client.prepared_statements.include?('query_sql_get_extra_types') - client.prepare('query_sql_get_extra_types', GET_EXTRA_TYPES_SQL) - client.prepared_statements.add('query_sql_get_extra_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_extra_types') + client.prepare('pg_codegen_query_sql_get_extra_types', GET_EXTRA_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_extra_types') end - result = client.exec_prepared('query_sql_get_extra_types') + result = client.exec_prepared('pg_codegen_query_sql_get_extra_types') row = result.first row && GetExtraTypesRow.new( row['c_uuid'], @@ -1064,11 +1081,11 @@ def get_extra_types!(...) def truncate_extra_types with_client('truncate_extra_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_extra_types') - client.prepare('query_sql_truncate_extra_types', TRUNCATE_EXTRA_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_extra_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_extra_types') + client.prepare('pg_codegen_query_sql_truncate_extra_types', TRUNCATE_EXTRA_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_extra_types') end - client.exec_prepared('query_sql_truncate_extra_types') + client.exec_prepared('pg_codegen_query_sql_truncate_extra_types') nil end end @@ -1090,22 +1107,22 @@ def insert_remaining_types(c_hstore:, c_ltree:, c_regclass:, c_cidr:, c_macaddr: c_timestamptz, encode_array(c_text_matrix) ] - unless client.prepared_statements.include?('query_sql_insert_remaining_types') - client.prepare('query_sql_insert_remaining_types', INSERT_REMAINING_TYPES_SQL) - client.prepared_statements.add('query_sql_insert_remaining_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_remaining_types') + client.prepare('pg_codegen_query_sql_insert_remaining_types', INSERT_REMAINING_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_remaining_types') end - client.exec_prepared('query_sql_insert_remaining_types', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_remaining_types', query_params) nil end end def get_remaining_types with_client('get_remaining_types') do |client| - unless client.prepared_statements.include?('query_sql_get_remaining_types') - client.prepare('query_sql_get_remaining_types', GET_REMAINING_TYPES_SQL) - client.prepared_statements.add('query_sql_get_remaining_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_remaining_types') + client.prepare('pg_codegen_query_sql_get_remaining_types', GET_REMAINING_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_remaining_types') end - result = client.exec_prepared('query_sql_get_remaining_types') + result = client.exec_prepared('pg_codegen_query_sql_get_remaining_types') row = result.first row && GetRemainingTypesRow.new( row['c_hstore'], @@ -1132,11 +1149,11 @@ def get_remaining_types!(...) def truncate_remaining_types with_client('truncate_remaining_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_remaining_types') - client.prepare('query_sql_truncate_remaining_types', TRUNCATE_REMAINING_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_remaining_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_remaining_types') + client.prepare('pg_codegen_query_sql_truncate_remaining_types', TRUNCATE_REMAINING_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_remaining_types') end - client.exec_prepared('query_sql_truncate_remaining_types') + client.exec_prepared('pg_codegen_query_sql_truncate_remaining_types') nil end end @@ -1144,22 +1161,22 @@ def truncate_remaining_types def insert_array_types(c_uuid_array:, c_bio_type_array:, c_integer_matrix:) with_client('insert_array_types') do |client| query_params = [encode_array(c_uuid_array), encode_array(c_bio_type_array), encode_array(c_integer_matrix)] - unless client.prepared_statements.include?('query_sql_insert_array_types') - client.prepare('query_sql_insert_array_types', INSERT_ARRAY_TYPES_SQL) - client.prepared_statements.add('query_sql_insert_array_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_array_types') + client.prepare('pg_codegen_query_sql_insert_array_types', INSERT_ARRAY_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_array_types') end - client.exec_prepared('query_sql_insert_array_types', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_array_types', query_params) nil end end def get_array_types with_client('get_array_types') do |client| - unless client.prepared_statements.include?('query_sql_get_array_types') - client.prepare('query_sql_get_array_types', GET_ARRAY_TYPES_SQL) - client.prepared_statements.add('query_sql_get_array_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_array_types') + client.prepare('pg_codegen_query_sql_get_array_types', GET_ARRAY_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_array_types') end - result = client.exec_prepared('query_sql_get_array_types') + result = client.exec_prepared('pg_codegen_query_sql_get_array_types') row = result.first row && GetArrayTypesRow.new(row['c_uuid_array'], row['c_bio_type_array'], row['c_integer_matrix']) end @@ -1172,22 +1189,22 @@ def get_array_types!(...) def truncate_array_types with_client('truncate_array_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_array_types') - client.prepare('query_sql_truncate_array_types', TRUNCATE_ARRAY_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_array_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_array_types') + client.prepare('pg_codegen_query_sql_truncate_array_types', TRUNCATE_ARRAY_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_array_types') end - client.exec_prepared('query_sql_truncate_array_types') + client.exec_prepared('pg_codegen_query_sql_truncate_array_types') nil end end def list_authors_with_optional_book with_client('list_authors_with_optional_book') do |client| - unless client.prepared_statements.include?('query_sql_list_authors_with_optional_book') - client.prepare('query_sql_list_authors_with_optional_book', LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL) - client.prepared_statements.add('query_sql_list_authors_with_optional_book') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors_with_optional_book') + client.prepare('pg_codegen_query_sql_list_authors_with_optional_book', LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors_with_optional_book') end - result = client.exec_prepared('query_sql_list_authors_with_optional_book').values + result = client.exec_prepared('pg_codegen_query_sql_list_authors_with_optional_book').values result.map do |row| ListAuthorsWithOptionalBookRow.new( row[0].nil? && row[1].nil? ? nil : Author.new(row[0], row[1], row[2]), @@ -1200,11 +1217,11 @@ def list_authors_with_optional_book def list_authors_between_ids(id:, id_2:) with_client('list_authors_between_ids') do |client| query_params = [id, id_2] - unless client.prepared_statements.include?('query_sql_list_authors_between_ids') - client.prepare('query_sql_list_authors_between_ids', LIST_AUTHORS_BETWEEN_IDS_SQL) - client.prepared_statements.add('query_sql_list_authors_between_ids') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors_between_ids') + client.prepare('pg_codegen_query_sql_list_authors_between_ids', LIST_AUTHORS_BETWEEN_IDS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors_between_ids') end - result = client.exec_prepared('query_sql_list_authors_between_ids', query_params) + result = client.exec_prepared('pg_codegen_query_sql_list_authors_between_ids', query_params) result.map do |row| ListAuthorsBetweenIdsRow.new(row['id'], row['name'], row['bio']) end @@ -1222,22 +1239,22 @@ def insert_special_types(c_timetz:, c_tsvector:, c_point:, c_int4range:, c_oid:, c_dimensions, c_positive ] - unless client.prepared_statements.include?('query_sql_insert_special_types') - client.prepare('query_sql_insert_special_types', INSERT_SPECIAL_TYPES_SQL) - client.prepared_statements.add('query_sql_insert_special_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_special_types') + client.prepare('pg_codegen_query_sql_insert_special_types', INSERT_SPECIAL_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_special_types') end - client.exec_prepared('query_sql_insert_special_types', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_special_types', query_params) nil end end def get_special_types with_client('get_special_types') do |client| - unless client.prepared_statements.include?('query_sql_get_special_types') - client.prepare('query_sql_get_special_types', GET_SPECIAL_TYPES_SQL) - client.prepared_statements.add('query_sql_get_special_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_special_types') + client.prepare('pg_codegen_query_sql_get_special_types', GET_SPECIAL_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_special_types') end - result = client.exec_prepared('query_sql_get_special_types') + result = client.exec_prepared('pg_codegen_query_sql_get_special_types') row = result.first row && GetSpecialTypesRow.new( row['c_timetz'], @@ -1258,11 +1275,11 @@ def get_special_types!(...) def truncate_special_types with_client('truncate_special_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_special_types') - client.prepare('query_sql_truncate_special_types', TRUNCATE_SPECIAL_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_special_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_special_types') + client.prepare('pg_codegen_query_sql_truncate_special_types', TRUNCATE_SPECIAL_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_special_types') end - client.exec_prepared('query_sql_truncate_special_types') + client.exec_prepared('pg_codegen_query_sql_truncate_special_types') nil end end @@ -1270,24 +1287,24 @@ def truncate_special_types def create_author_result(name:, bio:) with_client('create_author_result') do |client| query_params = [name, bio] - unless client.prepared_statements.include?('query_sql_create_author_result') - client.prepare('query_sql_create_author_result', CREATE_AUTHOR_RESULT_SQL) - client.prepared_statements.add('query_sql_create_author_result') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_author_result') + client.prepare('pg_codegen_query_sql_create_author_result', CREATE_AUTHOR_RESULT_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_author_result') end - result = client.exec_prepared('query_sql_create_author_result', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_author_result', query_params) ExecResult.new(result.cmd_tuples, nil) end end def create_authors_batch(rows) with_client('create_authors_batch') do |client| - unless client.prepared_statements.include?('query_sql_create_authors_batch') - client.prepare('query_sql_create_authors_batch', CREATE_AUTHORS_BATCH_SQL) - client.prepared_statements.add('query_sql_create_authors_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_authors_batch') + client.prepare('pg_codegen_query_sql_create_authors_batch', CREATE_AUTHORS_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_authors_batch') end rows.each do |args| query_params = [args.name, args.bio] - client.exec_prepared('query_sql_create_authors_batch', query_params) + client.exec_prepared('pg_codegen_query_sql_create_authors_batch', query_params) end nil end @@ -1295,13 +1312,13 @@ def create_authors_batch(rows) def get_author_batch(rows) with_client('get_author_batch') do |client| - unless client.prepared_statements.include?('query_sql_get_author_batch') - client.prepare('query_sql_get_author_batch', GET_AUTHOR_BATCH_SQL) - client.prepared_statements.add('query_sql_get_author_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_author_batch') + client.prepare('pg_codegen_query_sql_get_author_batch', GET_AUTHOR_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_author_batch') end rows.map do |args| query_params = [args.id] - result = client.exec_prepared('query_sql_get_author_batch', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_author_batch', query_params) row = result.first row && GetAuthorBatchRow.new(row['id'], row['name'], row['bio']) end @@ -1310,13 +1327,13 @@ def get_author_batch(rows) def list_authors_by_name_batch(rows) with_client('list_authors_by_name_batch') do |client| - unless client.prepared_statements.include?('query_sql_list_authors_by_name_batch') - client.prepare('query_sql_list_authors_by_name_batch', LIST_AUTHORS_BY_NAME_BATCH_SQL) - client.prepared_statements.add('query_sql_list_authors_by_name_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors_by_name_batch') + client.prepare('pg_codegen_query_sql_list_authors_by_name_batch', LIST_AUTHORS_BY_NAME_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors_by_name_batch') end rows.map do |args| query_params = [args.name] - result = client.exec_prepared('query_sql_list_authors_by_name_batch', query_params) + result = client.exec_prepared('pg_codegen_query_sql_list_authors_by_name_batch', query_params) result.map do |row| ListAuthorsByNameBatchRow.new(row['id'], row['name'], row['bio']) end @@ -1334,11 +1351,11 @@ def create_regression(id:, order:, payload:, doc:, bio_types:, uids:) encode_array(bio_types), encode_array(uids) ] - unless client.prepared_statements.include?('query_sql_create_regression') - client.prepare('query_sql_create_regression', CREATE_REGRESSION_SQL) - client.prepared_statements.add('query_sql_create_regression') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_regression') + client.prepare('pg_codegen_query_sql_create_regression', CREATE_REGRESSION_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_regression') end - result = client.exec_prepared('query_sql_create_regression', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_regression', query_params) row = result.first row && CreateRegressionRow.new( row['id'], @@ -1359,11 +1376,11 @@ def create_regression!(...) def create_regression_if_new(id:, order:) with_client('create_regression_if_new') do |client| query_params = [id, order] - unless client.prepared_statements.include?('query_sql_create_regression_if_new') - client.prepare('query_sql_create_regression_if_new', CREATE_REGRESSION_IF_NEW_SQL) - client.prepared_statements.add('query_sql_create_regression_if_new') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_regression_if_new') + client.prepare('pg_codegen_query_sql_create_regression_if_new', CREATE_REGRESSION_IF_NEW_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_regression_if_new') end - result = client.exec_prepared('query_sql_create_regression_if_new', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_regression_if_new', query_params) row = result.first row && row['id'] end @@ -1383,13 +1400,13 @@ def copy_regressions(rows) def create_regressions_batch(rows) with_client('create_regressions_batch') do |client| - unless client.prepared_statements.include?('query_sql_create_regressions_batch') - client.prepare('query_sql_create_regressions_batch', CREATE_REGRESSIONS_BATCH_SQL) - client.prepared_statements.add('query_sql_create_regressions_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_regressions_batch') + client.prepare('pg_codegen_query_sql_create_regressions_batch', CREATE_REGRESSIONS_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_regressions_batch') end rows.each do |args| query_params = [args.id, args.order, encode_bytea(args.payload), encode_json(args.doc)] - client.exec_prepared('query_sql_create_regressions_batch', query_params) + client.exec_prepared('pg_codegen_query_sql_create_regressions_batch', query_params) end nil end @@ -1398,11 +1415,11 @@ def create_regressions_batch(rows) def list_regressions(order_name: nil) with_client('list_regressions') do |client| query_params = [order_name] - unless client.prepared_statements.include?('query_sql_list_regressions') - client.prepare('query_sql_list_regressions', LIST_REGRESSIONS_SQL) - client.prepared_statements.add('query_sql_list_regressions') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_regressions') + client.prepare('pg_codegen_query_sql_list_regressions', LIST_REGRESSIONS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_regressions') end - result = client.exec_prepared('query_sql_list_regressions', query_params) + result = client.exec_prepared('pg_codegen_query_sql_list_regressions', query_params) result.map do |row| ListRegressionsRow.new( row['id'], @@ -1418,20 +1435,20 @@ def list_regressions(order_name: nil) def truncate_regressions with_client('truncate_regressions') do |client| - unless client.prepared_statements.include?('query_sql_truncate_regressions') - client.prepare('query_sql_truncate_regressions', TRUNCATE_REGRESSIONS_SQL) - client.prepared_statements.add('query_sql_truncate_regressions') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_regressions') + client.prepare('pg_codegen_query_sql_truncate_regressions', TRUNCATE_REGRESSIONS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_regressions') end - client.exec_prepared('query_sql_truncate_regressions') + client.exec_prepared('pg_codegen_query_sql_truncate_regressions') nil end end def create_batch_types_batch(rows) with_client('create_batch_types_batch') do |client| - unless client.prepared_statements.include?('query_sql_create_batch_types_batch') - client.prepare('query_sql_create_batch_types_batch', CREATE_BATCH_TYPES_BATCH_SQL) - client.prepared_statements.add('query_sql_create_batch_types_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_batch_types_batch') + client.prepare('pg_codegen_query_sql_create_batch_types_batch', CREATE_BATCH_TYPES_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_batch_types_batch') end rows.each do |args| query_params = [ @@ -1444,7 +1461,7 @@ def create_batch_types_batch(rows) encode_array(args.amounts), encode_array(args.stamps) ] - client.exec_prepared('query_sql_create_batch_types_batch', query_params) + client.exec_prepared('pg_codegen_query_sql_create_batch_types_batch', query_params) end nil end @@ -1464,11 +1481,11 @@ def copy_batch_types(rows) def list_batch_types with_client('list_batch_types') do |client| - unless client.prepared_statements.include?('query_sql_list_batch_types') - client.prepare('query_sql_list_batch_types', LIST_BATCH_TYPES_SQL) - client.prepared_statements.add('query_sql_list_batch_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_batch_types') + client.prepare('pg_codegen_query_sql_list_batch_types', LIST_BATCH_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_batch_types') end - result = client.exec_prepared('query_sql_list_batch_types') + result = client.exec_prepared('pg_codegen_query_sql_list_batch_types') result.map do |row| ListBatchTypesRow.new( row['id'], @@ -1487,11 +1504,11 @@ def list_batch_types def count_batch_types_above(min_id:) with_client('count_batch_types_above') do |client| query_params = [min_id] - unless client.prepared_statements.include?('query_sql_count_batch_types_above') - client.prepare('query_sql_count_batch_types_above', COUNT_BATCH_TYPES_ABOVE_SQL) - client.prepared_statements.add('query_sql_count_batch_types_above') + unless client.prepared_statements.include?('pg_codegen_query_sql_count_batch_types_above') + client.prepare('pg_codegen_query_sql_count_batch_types_above', COUNT_BATCH_TYPES_ABOVE_SQL) + client.prepared_statements.add('pg_codegen_query_sql_count_batch_types_above') end - result = client.exec_prepared('query_sql_count_batch_types_above', query_params) + result = client.exec_prepared('pg_codegen_query_sql_count_batch_types_above', query_params) row = result.first row && CountBatchTypesAboveRow.new(row['count']) end @@ -1504,11 +1521,11 @@ def count_batch_types_above!(...) def truncate_batch_types with_client('truncate_batch_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_batch_types') - client.prepare('query_sql_truncate_batch_types', TRUNCATE_BATCH_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_batch_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_batch_types') + client.prepare('pg_codegen_query_sql_truncate_batch_types', TRUNCATE_BATCH_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_batch_types') end - client.exec_prepared('query_sql_truncate_batch_types') + client.exec_prepared('pg_codegen_query_sql_truncate_batch_types') nil end end @@ -1525,11 +1542,11 @@ def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, h camel_case, http_server ] - unless client.prepared_statements.include?('query_sql_insert_name_case') - client.prepare('query_sql_insert_name_case', INSERT_NAME_CASE_SQL) - client.prepared_statements.add('query_sql_insert_name_case') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_name_case') + client.prepare('pg_codegen_query_sql_insert_name_case', INSERT_NAME_CASE_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_name_case') end - client.exec_prepared('query_sql_insert_name_case', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_name_case', query_params) nil end end @@ -1546,11 +1563,11 @@ def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, cam http_server, camel_case ] - unless client.prepared_statements.include?('query_sql_get_name_case') - client.prepare('query_sql_get_name_case', GET_NAME_CASE_SQL) - client.prepared_statements.add('query_sql_get_name_case') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_name_case') + client.prepare('pg_codegen_query_sql_get_name_case', GET_NAME_CASE_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_name_case') end - result = client.exec_prepared('query_sql_get_name_case', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_name_case', query_params) row = result.first row && GetNameCaseRow.new( row['id'], @@ -1573,11 +1590,11 @@ def get_name_case!(...) def get_http_server(id:) with_client('get_http_server') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_get_http_server') - client.prepare('query_sql_get_http_server', GET_HTTP_SERVER_SQL) - client.prepared_statements.add('query_sql_get_http_server') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_http_server') + client.prepare('pg_codegen_query_sql_get_http_server', GET_HTTP_SERVER_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_http_server') end - result = client.exec_prepared('query_sql_get_http_server', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_http_server', query_params) row = result.first row && GetHTTPServerRow.new( row['id'], @@ -1599,15 +1616,49 @@ def get_http_server!(...) def truncate_name_cases with_client('truncate_name_cases') do |client| - unless client.prepared_statements.include?('query_sql_truncate_name_cases') - client.prepare('query_sql_truncate_name_cases', TRUNCATE_NAME_CASES_SQL) - client.prepared_statements.add('query_sql_truncate_name_cases') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_name_cases') + client.prepare('pg_codegen_query_sql_truncate_name_cases', TRUNCATE_NAME_CASES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_name_cases') end - client.exec_prepared('query_sql_truncate_name_cases') + client.exec_prepared('pg_codegen_query_sql_truncate_name_cases') nil end end + def get_unnamed_columns + with_client('get_unnamed_columns') do |client| + unless client.prepared_statements.include?('pg_codegen_query_sql_get_unnamed_columns') + client.prepare('pg_codegen_query_sql_get_unnamed_columns', GET_UNNAMED_COLUMNS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_unnamed_columns') + end + result = client.exec_prepared('pg_codegen_query_sql_get_unnamed_columns').values + row = result.first + row && GetUnnamedColumnsRow.new(row[0], row[1]) + end + end + + # Like #get_unnamed_columns, but raises NotFound when there is no row. + def get_unnamed_columns!(...) + get_unnamed_columns(...) || raise(NotFound, 'GetUnnamedColumns returned no row') + end + + def get_quoted_alias + with_client('get_quoted_alias') do |client| + unless client.prepared_statements.include?('pg_codegen_query_sql_get_quoted_alias') + client.prepare('pg_codegen_query_sql_get_quoted_alias', GET_QUOTED_ALIAS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_quoted_alias') + end + result = client.exec_prepared('pg_codegen_query_sql_get_quoted_alias') + row = result.first + row && GetQuotedAliasRow.new(row['it\'s']) + end + end + + # Like #get_quoted_alias, but raises NotFound when there is no row. + def get_quoted_alias!(...) + get_quoted_alias(...) || raise(NotFound, 'GetQuotedAlias returned no row') + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ @@ -1617,11 +1668,11 @@ def many_params(c0:, c1:, c2:, c3:, c4:) encode_float(c3), encode_bytea(c4) ] - unless client.prepared_statements.include?('query_sql_many_params') - client.prepare('query_sql_many_params', MANY_PARAMS_SQL) - client.prepared_statements.add('query_sql_many_params') + unless client.prepared_statements.include?('pg_codegen_query_sql_many_params') + client.prepare('pg_codegen_query_sql_many_params', MANY_PARAMS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_many_params') end - result = client.exec_prepared('query_sql_many_params', query_params) + result = client.exec_prepared('pg_codegen_query_sql_many_params', query_params) row = result.first row && ManyParamsRow.new(row['c0'], row['c1']) end @@ -1635,11 +1686,11 @@ def many_params!(...) def many_rows(c0:) with_client('many_rows') do |client| query_params = [c0] - unless client.prepared_statements.include?('query_sql_many_rows') - client.prepare('query_sql_many_rows', MANY_ROWS_SQL) - client.prepared_statements.add('query_sql_many_rows') + unless client.prepared_statements.include?('pg_codegen_query_sql_many_rows') + client.prepare('pg_codegen_query_sql_many_rows', MANY_ROWS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_many_rows') end - result = client.exec_prepared('query_sql_many_rows', query_params) + result = client.exec_prepared('pg_codegen_query_sql_many_rows', query_params) result.map do |row| ManyRowsRow.new( row['c0'], @@ -1654,11 +1705,11 @@ def many_rows(c0:) def four_cols with_client('four_cols') do |client| - unless client.prepared_statements.include?('query_sql_four_cols') - client.prepare('query_sql_four_cols', FOUR_COLS_SQL) - client.prepared_statements.add('query_sql_four_cols') + unless client.prepared_statements.include?('pg_codegen_query_sql_four_cols') + client.prepare('pg_codegen_query_sql_four_cols', FOUR_COLS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_four_cols') end - result = client.exec_prepared('query_sql_four_cols') + result = client.exec_prepared('pg_codegen_query_sql_four_cols') result.map do |row| FourColsRow.new(row['c0'], row['c1'], row['c2'], row['c3']) end @@ -1684,7 +1735,13 @@ def with_client(name) @db.with do |client| client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) configure(client) unless client.is_a?(ConnectionState) - yield client + begin + yield client + rescue Exception # rubocop:disable Lint/RescueException + # a query left by Timeout / Thread#raise keeps running: cancel it before the connection is reused + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE + raise + end end end end @@ -1718,11 +1775,12 @@ def encode_bytea(value) value && PG::TextEncoder::Bytea.new.encode(value) end - # json / jsonb parameters: booleans and Arrays need JSON encoding, not PostgreSQL's boolean / array encoding. - # Hashes and JSON text pass through; nil stays nil. + # json / jsonb parameters: booleans and Arrays need JSON encoding, not PostgreSQL's boolean / array encoding, + # and Floats their shortest exact text. Hashes and JSON text pass through; nil stays nil. def encode_json(value) case value when Array, TrueClass, FalseClass then PG::TextEncoder::JSON.new.encode(value) + when Float then value.to_s else value end end diff --git a/test/golden/pg/default/query_sql.rbs b/test/golden/pg/default/query_sql.rbs index c6bd118..39bdcb6 100644 --- a/test/golden/pg/default/query_sql.rbs +++ b/test/golden/pg/default/query_sql.rbs @@ -553,6 +553,21 @@ module PgCodegen TRUNCATE_NAME_CASES_SQL: String + GET_UNNAMED_COLUMNS_SQL: String + + class GetUnnamedColumnsRow + attr_reader column_1: Integer + attr_reader column_2: String + def initialize: ( Integer column_1, String column_2) -> void + end + + GET_QUOTED_ALIAS_SQL: String + + class GetQuotedAliasRow + attr_reader it_s: Integer + def initialize: ( Integer it_s) -> void + end + MANY_PARAMS_SQL: String class ManyParamsRow @@ -752,6 +767,14 @@ module PgCodegen def truncate_name_cases: -> void + def get_unnamed_columns: -> GetUnnamedColumnsRow? + + def get_unnamed_columns!: -> GetUnnamedColumnsRow + + def get_quoted_alias: -> GetQuotedAliasRow? + + def get_quoted_alias!: -> GetQuotedAliasRow + def many_params: (c0: bool, c1: Integer, c2: String, c3: Float, c4: String) -> ManyParamsRow? def many_params!: (c0: bool, c1: Integer, c2: String, c3: Float, c4: String) -> ManyParamsRow diff --git a/test/golden/pg/ruby31_gemfile/my_queries_sql.rb b/test/golden/pg/ruby31_gemfile/my_queries_sql.rb index 903216a..416d24d 100644 --- a/test/golden/pg/ruby31_gemfile/my_queries_sql.rb +++ b/test/golden/pg/ruby31_gemfile/my_queries_sql.rb @@ -53,6 +53,7 @@ def transaction(isolation: nil) result rescue Exception # rubocop:disable Lint/RescueException failed = true + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE client.exec(rollback) raise ensure @@ -71,11 +72,11 @@ def transaction(isolation: nil) def with_numbers2_go(c0:) with_client('with_numbers2_go') do |client| query_params = [c0] - unless client.prepared_statements.include?('my_queries_sql_with_numbers2_go') - client.prepare('my_queries_sql_with_numbers2_go', WITH_NUMBERS2_GO_SQL) - client.prepared_statements.add('my_queries_sql_with_numbers2_go') + unless client.prepared_statements.include?('pg_codegen_my_queries_sql_with_numbers2_go') + client.prepare('pg_codegen_my_queries_sql_with_numbers2_go', WITH_NUMBERS2_GO_SQL) + client.prepared_statements.add('pg_codegen_my_queries_sql_with_numbers2_go') end - client.exec_prepared('my_queries_sql_with_numbers2_go', query_params) + client.exec_prepared('pg_codegen_my_queries_sql_with_numbers2_go', query_params) nil end end @@ -99,7 +100,13 @@ def with_client(name) @db.with do |client| client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) configure(client) unless client.is_a?(ConnectionState) - yield client + begin + yield client + rescue Exception # rubocop:disable Lint/RescueException + # a query left by Timeout / Thread#raise keeps running: cancel it before the connection is reused + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE + raise + end end end end diff --git a/test/golden/pg/ruby31_gemfile/other_sql.rb b/test/golden/pg/ruby31_gemfile/other_sql.rb index 533d650..ca7c0ad 100644 --- a/test/golden/pg/ruby31_gemfile/other_sql.rb +++ b/test/golden/pg/ruby31_gemfile/other_sql.rb @@ -65,6 +65,7 @@ def transaction(isolation: nil) result rescue Exception # rubocop:disable Lint/RescueException failed = true + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE client.exec(rollback) raise ensure @@ -89,22 +90,22 @@ def no_cols(c0:, c1:, c2:, c3:, c4:) encode_float(c3), encode_bytea(c4) ] - unless client.prepared_statements.include?('other_sql_no_cols') - client.prepare('other_sql_no_cols', NO_COLS_SQL) - client.prepared_statements.add('other_sql_no_cols') + unless client.prepared_statements.include?('pg_codegen_other_sql_no_cols') + client.prepare('pg_codegen_other_sql_no_cols', NO_COLS_SQL) + client.prepared_statements.add('pg_codegen_other_sql_no_cols') end - client.exec_prepared('other_sql_no_cols', query_params) + client.exec_prepared('pg_codegen_other_sql_no_cols', query_params) nil end end def other_one with_client('other_one') do |client| - unless client.prepared_statements.include?('other_sql_other_one') - client.prepare('other_sql_other_one', OTHER_ONE_SQL) - client.prepared_statements.add('other_sql_other_one') + unless client.prepared_statements.include?('pg_codegen_other_sql_other_one') + client.prepare('pg_codegen_other_sql_other_one', OTHER_ONE_SQL) + client.prepared_statements.add('pg_codegen_other_sql_other_one') end - result = client.exec_prepared('other_sql_other_one') + result = client.exec_prepared('pg_codegen_other_sql_other_one') row = result.first row && OtherOneRow.new(c0: row['c0']) end @@ -118,11 +119,11 @@ def other_one!(...) def count_rows(c0:) with_client('count_rows') do |client| query_params = [c0] - unless client.prepared_statements.include?('other_sql_count_rows') - client.prepare('other_sql_count_rows', COUNT_ROWS_SQL) - client.prepared_statements.add('other_sql_count_rows') + unless client.prepared_statements.include?('pg_codegen_other_sql_count_rows') + client.prepare('pg_codegen_other_sql_count_rows', COUNT_ROWS_SQL) + client.prepared_statements.add('pg_codegen_other_sql_count_rows') end - result = client.exec_prepared('other_sql_count_rows', query_params) + result = client.exec_prepared('pg_codegen_other_sql_count_rows', query_params) result.cmd_tuples end end @@ -146,7 +147,13 @@ def with_client(name) @db.with do |client| client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) configure(client) unless client.is_a?(ConnectionState) - yield client + begin + yield client + rescue Exception # rubocop:disable Lint/RescueException + # a query left by Timeout / Thread#raise keeps running: cancel it before the connection is reused + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE + raise + end end end end diff --git a/test/golden/pg/ruby31_gemfile/query_sql.rb b/test/golden/pg/ruby31_gemfile/query_sql.rb index 4e27bf6..12b9924 100644 --- a/test/golden/pg/ruby31_gemfile/query_sql.rb +++ b/test/golden/pg/ruby31_gemfile/query_sql.rb @@ -517,6 +517,20 @@ module PgCodegen TRUNCATE TABLE name_cases SQL + GET_UNNAMED_COLUMNS_SQL = <<~'SQL' + SELECT 1, 'two' + SQL + + # rubocop:disable Naming/VariableNumber + GetUnnamedColumnsRow = Struct.new(:column_1, :column_2, keyword_init: true) + # rubocop:enable Naming/VariableNumber + + GET_QUOTED_ALIAS_SQL = <<~'SQL' + SELECT 1 AS "it's" + SQL + + GetQuotedAliasRow = Struct.new(:it_s, keyword_init: true) + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -591,6 +605,7 @@ def transaction(isolation: nil) result rescue Exception # rubocop:disable Lint/RescueException failed = true + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE client.exec(rollback) raise ensure @@ -612,11 +627,11 @@ def transaction(isolation: nil) def get_author(id:) with_client('get_author') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_get_author') - client.prepare('query_sql_get_author', GET_AUTHOR_SQL) - client.prepared_statements.add('query_sql_get_author') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_author') + client.prepare('pg_codegen_query_sql_get_author', GET_AUTHOR_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_author') end - result = client.exec_prepared('query_sql_get_author', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_author', query_params) row = result.first row && GetAuthorRow.new(id: row['id'], name: row['name'], bio: row['bio']) end @@ -629,11 +644,11 @@ def get_author!(...) def list_authors with_client('list_authors') do |client| - unless client.prepared_statements.include?('query_sql_list_authors') - client.prepare('query_sql_list_authors', LIST_AUTHORS_SQL) - client.prepared_statements.add('query_sql_list_authors') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors') + client.prepare('pg_codegen_query_sql_list_authors', LIST_AUTHORS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors') end - result = client.exec_prepared('query_sql_list_authors') + result = client.exec_prepared('pg_codegen_query_sql_list_authors') result.map do |row| ListAuthorsRow.new(id: row['id'], name: row['name'], bio: row['bio']) end @@ -643,11 +658,11 @@ def list_authors def create_author(name:, bio:) with_client('create_author') do |client| query_params = [name, bio] - unless client.prepared_statements.include?('query_sql_create_author') - client.prepare('query_sql_create_author', CREATE_AUTHOR_SQL) - client.prepared_statements.add('query_sql_create_author') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_author') + client.prepare('pg_codegen_query_sql_create_author', CREATE_AUTHOR_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_author') end - result = client.exec_prepared('query_sql_create_author', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_author', query_params) row = result.first row && CreateAuthorRow.new(id: row['id'], name: row['name'], bio: row['bio']) end @@ -661,22 +676,22 @@ def create_author!(...) def delete_author(id:) with_client('delete_author') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_delete_author') - client.prepare('query_sql_delete_author', DELETE_AUTHOR_SQL) - client.prepared_statements.add('query_sql_delete_author') + unless client.prepared_statements.include?('pg_codegen_query_sql_delete_author') + client.prepare('pg_codegen_query_sql_delete_author', DELETE_AUTHOR_SQL) + client.prepared_statements.add('pg_codegen_query_sql_delete_author') end - client.exec_prepared('query_sql_delete_author', query_params) + client.exec_prepared('pg_codegen_query_sql_delete_author', query_params) nil end end def test with_client('test') do |client| - unless client.prepared_statements.include?('query_sql_test') - client.prepare('query_sql_test', TEST_SQL) - client.prepared_statements.add('query_sql_test') + unless client.prepared_statements.include?('pg_codegen_query_sql_test') + client.prepare('pg_codegen_query_sql_test', TEST_SQL) + client.prepared_statements.add('pg_codegen_query_sql_test') end - result = client.exec_prepared('query_sql_test') + result = client.exec_prepared('pg_codegen_query_sql_test') row = result.first row && TestRow.new( c_bit: row['c_bit'], @@ -709,11 +724,11 @@ def test!(...) def set_json_round_trip(id:, json_value:, jsonb_value:) with_client('set_json_round_trip') do |client| query_params = [id, encode_json(json_value), encode_json(jsonb_value)] - unless client.prepared_statements.include?('query_sql_set_json_round_trip') - client.prepare('query_sql_set_json_round_trip', SET_JSON_ROUND_TRIP_SQL) - client.prepared_statements.add('query_sql_set_json_round_trip') + unless client.prepared_statements.include?('pg_codegen_query_sql_set_json_round_trip') + client.prepare('pg_codegen_query_sql_set_json_round_trip', SET_JSON_ROUND_TRIP_SQL) + client.prepared_statements.add('pg_codegen_query_sql_set_json_round_trip') end - client.exec_prepared('query_sql_set_json_round_trip', query_params) + client.exec_prepared('pg_codegen_query_sql_set_json_round_trip', query_params) nil end end @@ -721,11 +736,11 @@ def set_json_round_trip(id:, json_value:, jsonb_value:) def get_json_round_trip(id:) with_client('get_json_round_trip') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_get_json_round_trip') - client.prepare('query_sql_get_json_round_trip', GET_JSON_ROUND_TRIP_SQL) - client.prepared_statements.add('query_sql_get_json_round_trip') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_json_round_trip') + client.prepare('pg_codegen_query_sql_get_json_round_trip', GET_JSON_ROUND_TRIP_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_json_round_trip') end - result = client.exec_prepared('query_sql_get_json_round_trip', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_json_round_trip', query_params) row = result.first row && GetJSONRoundTripRow.new(json_value: row['json_value'], jsonb_value: row['jsonb_value']) end @@ -739,22 +754,22 @@ def get_json_round_trip!(...) def toggle_jsonb_round_trip(id:) with_client('toggle_jsonb_round_trip') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_toggle_jsonb_round_trip') - client.prepare('query_sql_toggle_jsonb_round_trip', TOGGLE_JSONB_ROUND_TRIP_SQL) - client.prepared_statements.add('query_sql_toggle_jsonb_round_trip') + unless client.prepared_statements.include?('pg_codegen_query_sql_toggle_jsonb_round_trip') + client.prepare('pg_codegen_query_sql_toggle_jsonb_round_trip', TOGGLE_JSONB_ROUND_TRIP_SQL) + client.prepared_statements.add('pg_codegen_query_sql_toggle_jsonb_round_trip') end - client.exec_prepared('query_sql_toggle_jsonb_round_trip', query_params) + client.exec_prepared('pg_codegen_query_sql_toggle_jsonb_round_trip', query_params) nil end end def truncate_json_round_trips with_client('truncate_json_round_trips') do |client| - unless client.prepared_statements.include?('query_sql_truncate_json_round_trips') - client.prepare('query_sql_truncate_json_round_trips', TRUNCATE_JSON_ROUND_TRIPS_SQL) - client.prepared_statements.add('query_sql_truncate_json_round_trips') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_json_round_trips') + client.prepare('pg_codegen_query_sql_truncate_json_round_trips', TRUNCATE_JSON_ROUND_TRIPS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_json_round_trips') end - client.exec_prepared('query_sql_truncate_json_round_trips') + client.exec_prepared('pg_codegen_query_sql_truncate_json_round_trips') nil end end @@ -762,11 +777,11 @@ def truncate_json_round_trips def pause_query(seconds:) with_client('pause_query') do |client| query_params = [encode_float(seconds)] - unless client.prepared_statements.include?('query_sql_pause_query') - client.prepare('query_sql_pause_query', PAUSE_QUERY_SQL) - client.prepared_statements.add('query_sql_pause_query') + unless client.prepared_statements.include?('pg_codegen_query_sql_pause_query') + client.prepare('pg_codegen_query_sql_pause_query', PAUSE_QUERY_SQL) + client.prepared_statements.add('pg_codegen_query_sql_pause_query') end - client.exec_prepared('query_sql_pause_query', query_params) + client.exec_prepared('pg_codegen_query_sql_pause_query', query_params) nil end end @@ -774,11 +789,11 @@ def pause_query(seconds:) def list_authors_paged(offset:, limit:) with_client('list_authors_paged') do |client| query_params = [offset, limit] - unless client.prepared_statements.include?('query_sql_list_authors_paged') - client.prepare('query_sql_list_authors_paged', LIST_AUTHORS_PAGED_SQL) - client.prepared_statements.add('query_sql_list_authors_paged') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors_paged') + client.prepare('pg_codegen_query_sql_list_authors_paged', LIST_AUTHORS_PAGED_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors_paged') end - result = client.exec_prepared('query_sql_list_authors_paged', query_params) + result = client.exec_prepared('pg_codegen_query_sql_list_authors_paged', query_params) result.map do |row| ListAuthorsPagedRow.new(id: row['id'], name: row['name'], bio: row['bio']) end @@ -788,11 +803,11 @@ def list_authors_paged(offset:, limit:) def get_author_by_name_pattern(name_pattern: nil) with_client('get_author_by_name_pattern') do |client| query_params = [name_pattern] - unless client.prepared_statements.include?('query_sql_get_author_by_name_pattern') - client.prepare('query_sql_get_author_by_name_pattern', GET_AUTHOR_BY_NAME_PATTERN_SQL) - client.prepared_statements.add('query_sql_get_author_by_name_pattern') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_author_by_name_pattern') + client.prepare('pg_codegen_query_sql_get_author_by_name_pattern', GET_AUTHOR_BY_NAME_PATTERN_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_author_by_name_pattern') end - result = client.exec_prepared('query_sql_get_author_by_name_pattern', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_author_by_name_pattern', query_params) result.map do |row| GetAuthorByNamePatternRow.new(id: row['id'], name: row['name'], bio: row['bio']) end @@ -802,11 +817,11 @@ def get_author_by_name_pattern(name_pattern: nil) def get_authors_by_optional_name(lim:, name_filter: nil) with_client('get_authors_by_optional_name') do |client| query_params = [name_filter, lim] - unless client.prepared_statements.include?('query_sql_get_authors_by_optional_name') - client.prepare('query_sql_get_authors_by_optional_name', GET_AUTHORS_BY_OPTIONAL_NAME_SQL) - client.prepared_statements.add('query_sql_get_authors_by_optional_name') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_authors_by_optional_name') + client.prepare('pg_codegen_query_sql_get_authors_by_optional_name', GET_AUTHORS_BY_OPTIONAL_NAME_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_authors_by_optional_name') end - result = client.exec_prepared('query_sql_get_authors_by_optional_name', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_authors_by_optional_name', query_params) result.map do |row| GetAuthorsByOptionalNameRow.new(id: row['id'], name: row['name'], bio: row['bio']) end @@ -816,11 +831,11 @@ def get_authors_by_optional_name(lim:, name_filter: nil) def create_author_return_id(name:, bio:) with_client('create_author_return_id') do |client| query_params = [name, bio] - unless client.prepared_statements.include?('query_sql_create_author_return_id') - client.prepare('query_sql_create_author_return_id', CREATE_AUTHOR_RETURN_ID_SQL) - client.prepared_statements.add('query_sql_create_author_return_id') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_author_return_id') + client.prepare('pg_codegen_query_sql_create_author_return_id', CREATE_AUTHOR_RETURN_ID_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_author_return_id') end - result = client.exec_prepared('query_sql_create_author_return_id', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_author_return_id', query_params) row = result.first row && row['id'] end @@ -830,11 +845,11 @@ def create_author_return_id(name:, bio:) def update_author_bios(bio:) with_client('update_author_bios') do |client| query_params = [bio] - unless client.prepared_statements.include?('query_sql_update_author_bios') - client.prepare('query_sql_update_author_bios', UPDATE_AUTHOR_BIOS_SQL) - client.prepared_statements.add('query_sql_update_author_bios') + unless client.prepared_statements.include?('pg_codegen_query_sql_update_author_bios') + client.prepare('pg_codegen_query_sql_update_author_bios', UPDATE_AUTHOR_BIOS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_update_author_bios') end - result = client.exec_prepared('query_sql_update_author_bios', query_params) + result = client.exec_prepared('pg_codegen_query_sql_update_author_bios', query_params) result.cmd_tuples end end @@ -842,11 +857,11 @@ def update_author_bios(bio:) def get_authors_by_ids(param1:) with_client('get_authors_by_ids') do |client| query_params = [encode_array(param1)] - unless client.prepared_statements.include?('query_sql_get_authors_by_ids') - client.prepare('query_sql_get_authors_by_ids', GET_AUTHORS_BY_IDS_SQL) - client.prepared_statements.add('query_sql_get_authors_by_ids') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_authors_by_ids') + client.prepare('pg_codegen_query_sql_get_authors_by_ids', GET_AUTHORS_BY_IDS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_authors_by_ids') end - result = client.exec_prepared('query_sql_get_authors_by_ids', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_authors_by_ids', query_params) result.map do |row| GetAuthorsByIdsRow.new(id: row['id'], name: row['name'], bio: row['bio']) end @@ -856,11 +871,11 @@ def get_authors_by_ids(param1:) def get_authors_by_ids_and_names(param1:, param2:) with_client('get_authors_by_ids_and_names') do |client| query_params = [encode_array(param1), encode_array(param2)] - unless client.prepared_statements.include?('query_sql_get_authors_by_ids_and_names') - client.prepare('query_sql_get_authors_by_ids_and_names', GET_AUTHORS_BY_IDS_AND_NAMES_SQL) - client.prepared_statements.add('query_sql_get_authors_by_ids_and_names') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_authors_by_ids_and_names') + client.prepare('pg_codegen_query_sql_get_authors_by_ids_and_names', GET_AUTHORS_BY_IDS_AND_NAMES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_authors_by_ids_and_names') end - result = client.exec_prepared('query_sql_get_authors_by_ids_and_names', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_authors_by_ids_and_names', query_params) result.map do |row| GetAuthorsByIdsAndNamesRow.new(id: row['id'], name: row['name'], bio: row['bio']) end @@ -882,11 +897,11 @@ def copy_authors(rows) def truncate_authors with_client('truncate_authors') do |client| - unless client.prepared_statements.include?('query_sql_truncate_authors') - client.prepare('query_sql_truncate_authors', TRUNCATE_AUTHORS_SQL) - client.prepared_statements.add('query_sql_truncate_authors') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_authors') + client.prepare('pg_codegen_query_sql_truncate_authors', TRUNCATE_AUTHORS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_authors') end - client.exec_prepared('query_sql_truncate_authors') + client.exec_prepared('pg_codegen_query_sql_truncate_authors') nil end end @@ -894,11 +909,11 @@ def truncate_authors def create_book(name:, author_id:) with_client('create_book') do |client| query_params = [name, author_id] - unless client.prepared_statements.include?('query_sql_create_book') - client.prepare('query_sql_create_book', CREATE_BOOK_SQL) - client.prepared_statements.add('query_sql_create_book') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_book') + client.prepare('pg_codegen_query_sql_create_book', CREATE_BOOK_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_book') end - result = client.exec_prepared('query_sql_create_book', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_book', query_params) row = result.first row && row['id'] end @@ -907,33 +922,33 @@ def create_book(name:, author_id:) def create_deferred_book(author_id:) with_client('create_deferred_book') do |client| query_params = [author_id] - unless client.prepared_statements.include?('query_sql_create_deferred_book') - client.prepare('query_sql_create_deferred_book', CREATE_DEFERRED_BOOK_SQL) - client.prepared_statements.add('query_sql_create_deferred_book') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_deferred_book') + client.prepare('pg_codegen_query_sql_create_deferred_book', CREATE_DEFERRED_BOOK_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_deferred_book') end - client.exec_prepared('query_sql_create_deferred_book', query_params) + client.exec_prepared('pg_codegen_query_sql_create_deferred_book', query_params) nil end end def truncate_deferred_books with_client('truncate_deferred_books') do |client| - unless client.prepared_statements.include?('query_sql_truncate_deferred_books') - client.prepare('query_sql_truncate_deferred_books', TRUNCATE_DEFERRED_BOOKS_SQL) - client.prepared_statements.add('query_sql_truncate_deferred_books') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_deferred_books') + client.prepare('pg_codegen_query_sql_truncate_deferred_books', TRUNCATE_DEFERRED_BOOKS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_deferred_books') end - client.exec_prepared('query_sql_truncate_deferred_books') + client.exec_prepared('pg_codegen_query_sql_truncate_deferred_books') nil end end def list_all_authors_books with_client('list_all_authors_books') do |client| - unless client.prepared_statements.include?('query_sql_list_all_authors_books') - client.prepare('query_sql_list_all_authors_books', LIST_ALL_AUTHORS_BOOKS_SQL) - client.prepared_statements.add('query_sql_list_all_authors_books') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_all_authors_books') + client.prepare('pg_codegen_query_sql_list_all_authors_books', LIST_ALL_AUTHORS_BOOKS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_all_authors_books') end - result = client.exec_prepared('query_sql_list_all_authors_books').values + result = client.exec_prepared('pg_codegen_query_sql_list_all_authors_books').values result.map do |row| ListAllAuthorsBooksRow.new( author: Author.new(id: row[0], name: row[1], bio: row[2]), @@ -945,11 +960,11 @@ def list_all_authors_books def get_duplicate_authors with_client('get_duplicate_authors') do |client| - unless client.prepared_statements.include?('query_sql_get_duplicate_authors') - client.prepare('query_sql_get_duplicate_authors', GET_DUPLICATE_AUTHORS_SQL) - client.prepared_statements.add('query_sql_get_duplicate_authors') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_duplicate_authors') + client.prepare('pg_codegen_query_sql_get_duplicate_authors', GET_DUPLICATE_AUTHORS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_duplicate_authors') end - result = client.exec_prepared('query_sql_get_duplicate_authors').values + result = client.exec_prepared('pg_codegen_query_sql_get_duplicate_authors').values result.map do |row| GetDuplicateAuthorsRow.new( author: Author.new(id: row[0], name: row[1], bio: row[2]), @@ -962,11 +977,11 @@ def get_duplicate_authors def get_authors_by_book_name(name:) with_client('get_authors_by_book_name') do |client| query_params = [name] - unless client.prepared_statements.include?('query_sql_get_authors_by_book_name') - client.prepare('query_sql_get_authors_by_book_name', GET_AUTHORS_BY_BOOK_NAME_SQL) - client.prepared_statements.add('query_sql_get_authors_by_book_name') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_authors_by_book_name') + client.prepare('pg_codegen_query_sql_get_authors_by_book_name', GET_AUTHORS_BY_BOOK_NAME_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_authors_by_book_name') end - result = client.exec_prepared('query_sql_get_authors_by_book_name', query_params).values + result = client.exec_prepared('pg_codegen_query_sql_get_authors_by_book_name', query_params).values result.map do |row| GetAuthorsByBookNameRow.new( id: row[0], @@ -981,11 +996,11 @@ def get_authors_by_book_name(name:) def create_extended_bio(author_name:, name:, bio_type:) with_client('create_extended_bio') do |client| query_params = [author_name, name, bio_type] - unless client.prepared_statements.include?('query_sql_create_extended_bio') - client.prepare('query_sql_create_extended_bio', CREATE_EXTENDED_BIO_SQL) - client.prepared_statements.add('query_sql_create_extended_bio') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_extended_bio') + client.prepare('pg_codegen_query_sql_create_extended_bio', CREATE_EXTENDED_BIO_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_extended_bio') end - client.exec_prepared('query_sql_create_extended_bio', query_params) + client.exec_prepared('pg_codegen_query_sql_create_extended_bio', query_params) nil end end @@ -993,11 +1008,11 @@ def create_extended_bio(author_name:, name:, bio_type:) def get_first_extended_bio_by_type(bio_type:) with_client('get_first_extended_bio_by_type') do |client| query_params = [bio_type] - unless client.prepared_statements.include?('query_sql_get_first_extended_bio_by_type') - client.prepare('query_sql_get_first_extended_bio_by_type', GET_FIRST_EXTENDED_BIO_BY_TYPE_SQL) - client.prepared_statements.add('query_sql_get_first_extended_bio_by_type') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_first_extended_bio_by_type') + client.prepare('pg_codegen_query_sql_get_first_extended_bio_by_type', GET_FIRST_EXTENDED_BIO_BY_TYPE_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_first_extended_bio_by_type') end - result = client.exec_prepared('query_sql_get_first_extended_bio_by_type', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_first_extended_bio_by_type', query_params) row = result.first row && GetFirstExtendedBioByTypeRow.new( author_name: row['author_name'], @@ -1014,11 +1029,11 @@ def get_first_extended_bio_by_type!(...) def truncate_extended_bios with_client('truncate_extended_bios') do |client| - unless client.prepared_statements.include?('query_sql_truncate_extended_bios') - client.prepare('query_sql_truncate_extended_bios', TRUNCATE_EXTENDED_BIOS_SQL) - client.prepared_statements.add('query_sql_truncate_extended_bios') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_extended_bios') + client.prepare('pg_codegen_query_sql_truncate_extended_bios', TRUNCATE_EXTENDED_BIOS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_extended_bios') end - client.exec_prepared('query_sql_truncate_extended_bios') + client.exec_prepared('pg_codegen_query_sql_truncate_extended_bios') nil end end @@ -1035,22 +1050,22 @@ def insert_extra_types(c_uuid:, c_jsonb:, c_text_array:, c_integer_array:, c_tim c_money, c_not_null ] - unless client.prepared_statements.include?('query_sql_insert_extra_types') - client.prepare('query_sql_insert_extra_types', INSERT_EXTRA_TYPES_SQL) - client.prepared_statements.add('query_sql_insert_extra_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_extra_types') + client.prepare('pg_codegen_query_sql_insert_extra_types', INSERT_EXTRA_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_extra_types') end - client.exec_prepared('query_sql_insert_extra_types', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_extra_types', query_params) nil end end def get_extra_types with_client('get_extra_types') do |client| - unless client.prepared_statements.include?('query_sql_get_extra_types') - client.prepare('query_sql_get_extra_types', GET_EXTRA_TYPES_SQL) - client.prepared_statements.add('query_sql_get_extra_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_extra_types') + client.prepare('pg_codegen_query_sql_get_extra_types', GET_EXTRA_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_extra_types') end - result = client.exec_prepared('query_sql_get_extra_types') + result = client.exec_prepared('pg_codegen_query_sql_get_extra_types') row = result.first row && GetExtraTypesRow.new( c_uuid: row['c_uuid'], @@ -1072,11 +1087,11 @@ def get_extra_types!(...) def truncate_extra_types with_client('truncate_extra_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_extra_types') - client.prepare('query_sql_truncate_extra_types', TRUNCATE_EXTRA_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_extra_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_extra_types') + client.prepare('pg_codegen_query_sql_truncate_extra_types', TRUNCATE_EXTRA_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_extra_types') end - client.exec_prepared('query_sql_truncate_extra_types') + client.exec_prepared('pg_codegen_query_sql_truncate_extra_types') nil end end @@ -1098,22 +1113,22 @@ def insert_remaining_types(c_hstore:, c_ltree:, c_regclass:, c_cidr:, c_macaddr: c_timestamptz, encode_array(c_text_matrix) ] - unless client.prepared_statements.include?('query_sql_insert_remaining_types') - client.prepare('query_sql_insert_remaining_types', INSERT_REMAINING_TYPES_SQL) - client.prepared_statements.add('query_sql_insert_remaining_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_remaining_types') + client.prepare('pg_codegen_query_sql_insert_remaining_types', INSERT_REMAINING_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_remaining_types') end - client.exec_prepared('query_sql_insert_remaining_types', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_remaining_types', query_params) nil end end def get_remaining_types with_client('get_remaining_types') do |client| - unless client.prepared_statements.include?('query_sql_get_remaining_types') - client.prepare('query_sql_get_remaining_types', GET_REMAINING_TYPES_SQL) - client.prepared_statements.add('query_sql_get_remaining_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_remaining_types') + client.prepare('pg_codegen_query_sql_get_remaining_types', GET_REMAINING_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_remaining_types') end - result = client.exec_prepared('query_sql_get_remaining_types') + result = client.exec_prepared('pg_codegen_query_sql_get_remaining_types') row = result.first row && GetRemainingTypesRow.new( c_hstore: row['c_hstore'], @@ -1140,11 +1155,11 @@ def get_remaining_types!(...) def truncate_remaining_types with_client('truncate_remaining_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_remaining_types') - client.prepare('query_sql_truncate_remaining_types', TRUNCATE_REMAINING_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_remaining_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_remaining_types') + client.prepare('pg_codegen_query_sql_truncate_remaining_types', TRUNCATE_REMAINING_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_remaining_types') end - client.exec_prepared('query_sql_truncate_remaining_types') + client.exec_prepared('pg_codegen_query_sql_truncate_remaining_types') nil end end @@ -1152,22 +1167,22 @@ def truncate_remaining_types def insert_array_types(c_uuid_array:, c_bio_type_array:, c_integer_matrix:) with_client('insert_array_types') do |client| query_params = [encode_array(c_uuid_array), encode_array(c_bio_type_array), encode_array(c_integer_matrix)] - unless client.prepared_statements.include?('query_sql_insert_array_types') - client.prepare('query_sql_insert_array_types', INSERT_ARRAY_TYPES_SQL) - client.prepared_statements.add('query_sql_insert_array_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_array_types') + client.prepare('pg_codegen_query_sql_insert_array_types', INSERT_ARRAY_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_array_types') end - client.exec_prepared('query_sql_insert_array_types', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_array_types', query_params) nil end end def get_array_types with_client('get_array_types') do |client| - unless client.prepared_statements.include?('query_sql_get_array_types') - client.prepare('query_sql_get_array_types', GET_ARRAY_TYPES_SQL) - client.prepared_statements.add('query_sql_get_array_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_array_types') + client.prepare('pg_codegen_query_sql_get_array_types', GET_ARRAY_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_array_types') end - result = client.exec_prepared('query_sql_get_array_types') + result = client.exec_prepared('pg_codegen_query_sql_get_array_types') row = result.first row && GetArrayTypesRow.new( c_uuid_array: row['c_uuid_array'], @@ -1184,22 +1199,22 @@ def get_array_types!(...) def truncate_array_types with_client('truncate_array_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_array_types') - client.prepare('query_sql_truncate_array_types', TRUNCATE_ARRAY_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_array_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_array_types') + client.prepare('pg_codegen_query_sql_truncate_array_types', TRUNCATE_ARRAY_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_array_types') end - client.exec_prepared('query_sql_truncate_array_types') + client.exec_prepared('pg_codegen_query_sql_truncate_array_types') nil end end def list_authors_with_optional_book with_client('list_authors_with_optional_book') do |client| - unless client.prepared_statements.include?('query_sql_list_authors_with_optional_book') - client.prepare('query_sql_list_authors_with_optional_book', LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL) - client.prepared_statements.add('query_sql_list_authors_with_optional_book') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors_with_optional_book') + client.prepare('pg_codegen_query_sql_list_authors_with_optional_book', LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors_with_optional_book') end - result = client.exec_prepared('query_sql_list_authors_with_optional_book').values + result = client.exec_prepared('pg_codegen_query_sql_list_authors_with_optional_book').values 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]), @@ -1212,11 +1227,11 @@ def list_authors_with_optional_book def list_authors_between_ids(id:, id_2:) with_client('list_authors_between_ids') do |client| query_params = [id, id_2] - unless client.prepared_statements.include?('query_sql_list_authors_between_ids') - client.prepare('query_sql_list_authors_between_ids', LIST_AUTHORS_BETWEEN_IDS_SQL) - client.prepared_statements.add('query_sql_list_authors_between_ids') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors_between_ids') + client.prepare('pg_codegen_query_sql_list_authors_between_ids', LIST_AUTHORS_BETWEEN_IDS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors_between_ids') end - result = client.exec_prepared('query_sql_list_authors_between_ids', query_params) + result = client.exec_prepared('pg_codegen_query_sql_list_authors_between_ids', query_params) result.map do |row| ListAuthorsBetweenIdsRow.new(id: row['id'], name: row['name'], bio: row['bio']) end @@ -1234,22 +1249,22 @@ def insert_special_types(c_timetz:, c_tsvector:, c_point:, c_int4range:, c_oid:, c_dimensions, c_positive ] - unless client.prepared_statements.include?('query_sql_insert_special_types') - client.prepare('query_sql_insert_special_types', INSERT_SPECIAL_TYPES_SQL) - client.prepared_statements.add('query_sql_insert_special_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_special_types') + client.prepare('pg_codegen_query_sql_insert_special_types', INSERT_SPECIAL_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_special_types') end - client.exec_prepared('query_sql_insert_special_types', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_special_types', query_params) nil end end def get_special_types with_client('get_special_types') do |client| - unless client.prepared_statements.include?('query_sql_get_special_types') - client.prepare('query_sql_get_special_types', GET_SPECIAL_TYPES_SQL) - client.prepared_statements.add('query_sql_get_special_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_special_types') + client.prepare('pg_codegen_query_sql_get_special_types', GET_SPECIAL_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_special_types') end - result = client.exec_prepared('query_sql_get_special_types') + result = client.exec_prepared('pg_codegen_query_sql_get_special_types') row = result.first row && GetSpecialTypesRow.new( c_timetz: row['c_timetz'], @@ -1270,11 +1285,11 @@ def get_special_types!(...) def truncate_special_types with_client('truncate_special_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_special_types') - client.prepare('query_sql_truncate_special_types', TRUNCATE_SPECIAL_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_special_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_special_types') + client.prepare('pg_codegen_query_sql_truncate_special_types', TRUNCATE_SPECIAL_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_special_types') end - client.exec_prepared('query_sql_truncate_special_types') + client.exec_prepared('pg_codegen_query_sql_truncate_special_types') nil end end @@ -1282,24 +1297,24 @@ def truncate_special_types def create_author_result(name:, bio:) with_client('create_author_result') do |client| query_params = [name, bio] - unless client.prepared_statements.include?('query_sql_create_author_result') - client.prepare('query_sql_create_author_result', CREATE_AUTHOR_RESULT_SQL) - client.prepared_statements.add('query_sql_create_author_result') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_author_result') + client.prepare('pg_codegen_query_sql_create_author_result', CREATE_AUTHOR_RESULT_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_author_result') end - result = client.exec_prepared('query_sql_create_author_result', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_author_result', query_params) ExecResult.new(rows_affected: result.cmd_tuples, last_insert_id: nil) end end def create_authors_batch(rows) with_client('create_authors_batch') do |client| - unless client.prepared_statements.include?('query_sql_create_authors_batch') - client.prepare('query_sql_create_authors_batch', CREATE_AUTHORS_BATCH_SQL) - client.prepared_statements.add('query_sql_create_authors_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_authors_batch') + client.prepare('pg_codegen_query_sql_create_authors_batch', CREATE_AUTHORS_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_authors_batch') end rows.each do |args| query_params = [args.name, args.bio] - client.exec_prepared('query_sql_create_authors_batch', query_params) + client.exec_prepared('pg_codegen_query_sql_create_authors_batch', query_params) end nil end @@ -1307,13 +1322,13 @@ def create_authors_batch(rows) def get_author_batch(rows) with_client('get_author_batch') do |client| - unless client.prepared_statements.include?('query_sql_get_author_batch') - client.prepare('query_sql_get_author_batch', GET_AUTHOR_BATCH_SQL) - client.prepared_statements.add('query_sql_get_author_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_author_batch') + client.prepare('pg_codegen_query_sql_get_author_batch', GET_AUTHOR_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_author_batch') end rows.map do |args| query_params = [args.id] - result = client.exec_prepared('query_sql_get_author_batch', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_author_batch', query_params) row = result.first row && GetAuthorBatchRow.new(id: row['id'], name: row['name'], bio: row['bio']) end @@ -1322,13 +1337,13 @@ def get_author_batch(rows) def list_authors_by_name_batch(rows) with_client('list_authors_by_name_batch') do |client| - unless client.prepared_statements.include?('query_sql_list_authors_by_name_batch') - client.prepare('query_sql_list_authors_by_name_batch', LIST_AUTHORS_BY_NAME_BATCH_SQL) - client.prepared_statements.add('query_sql_list_authors_by_name_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors_by_name_batch') + client.prepare('pg_codegen_query_sql_list_authors_by_name_batch', LIST_AUTHORS_BY_NAME_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors_by_name_batch') end rows.map do |args| query_params = [args.name] - result = client.exec_prepared('query_sql_list_authors_by_name_batch', query_params) + result = client.exec_prepared('pg_codegen_query_sql_list_authors_by_name_batch', query_params) result.map do |row| ListAuthorsByNameBatchRow.new(id: row['id'], name: row['name'], bio: row['bio']) end @@ -1346,11 +1361,11 @@ def create_regression(id:, order:, payload:, doc:, bio_types:, uids:) encode_array(bio_types), encode_array(uids) ] - unless client.prepared_statements.include?('query_sql_create_regression') - client.prepare('query_sql_create_regression', CREATE_REGRESSION_SQL) - client.prepared_statements.add('query_sql_create_regression') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_regression') + client.prepare('pg_codegen_query_sql_create_regression', CREATE_REGRESSION_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_regression') end - result = client.exec_prepared('query_sql_create_regression', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_regression', query_params) row = result.first row && CreateRegressionRow.new( id: row['id'], @@ -1371,11 +1386,11 @@ def create_regression!(...) def create_regression_if_new(id:, order:) with_client('create_regression_if_new') do |client| query_params = [id, order] - unless client.prepared_statements.include?('query_sql_create_regression_if_new') - client.prepare('query_sql_create_regression_if_new', CREATE_REGRESSION_IF_NEW_SQL) - client.prepared_statements.add('query_sql_create_regression_if_new') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_regression_if_new') + client.prepare('pg_codegen_query_sql_create_regression_if_new', CREATE_REGRESSION_IF_NEW_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_regression_if_new') end - result = client.exec_prepared('query_sql_create_regression_if_new', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_regression_if_new', query_params) row = result.first row && row['id'] end @@ -1395,13 +1410,13 @@ def copy_regressions(rows) def create_regressions_batch(rows) with_client('create_regressions_batch') do |client| - unless client.prepared_statements.include?('query_sql_create_regressions_batch') - client.prepare('query_sql_create_regressions_batch', CREATE_REGRESSIONS_BATCH_SQL) - client.prepared_statements.add('query_sql_create_regressions_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_regressions_batch') + client.prepare('pg_codegen_query_sql_create_regressions_batch', CREATE_REGRESSIONS_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_regressions_batch') end rows.each do |args| query_params = [args.id, args.order, encode_bytea(args.payload), encode_json(args.doc)] - client.exec_prepared('query_sql_create_regressions_batch', query_params) + client.exec_prepared('pg_codegen_query_sql_create_regressions_batch', query_params) end nil end @@ -1410,11 +1425,11 @@ def create_regressions_batch(rows) def list_regressions(order_name: nil) with_client('list_regressions') do |client| query_params = [order_name] - unless client.prepared_statements.include?('query_sql_list_regressions') - client.prepare('query_sql_list_regressions', LIST_REGRESSIONS_SQL) - client.prepared_statements.add('query_sql_list_regressions') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_regressions') + client.prepare('pg_codegen_query_sql_list_regressions', LIST_REGRESSIONS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_regressions') end - result = client.exec_prepared('query_sql_list_regressions', query_params) + result = client.exec_prepared('pg_codegen_query_sql_list_regressions', query_params) result.map do |row| ListRegressionsRow.new( id: row['id'], @@ -1430,20 +1445,20 @@ def list_regressions(order_name: nil) def truncate_regressions with_client('truncate_regressions') do |client| - unless client.prepared_statements.include?('query_sql_truncate_regressions') - client.prepare('query_sql_truncate_regressions', TRUNCATE_REGRESSIONS_SQL) - client.prepared_statements.add('query_sql_truncate_regressions') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_regressions') + client.prepare('pg_codegen_query_sql_truncate_regressions', TRUNCATE_REGRESSIONS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_regressions') end - client.exec_prepared('query_sql_truncate_regressions') + client.exec_prepared('pg_codegen_query_sql_truncate_regressions') nil end end def create_batch_types_batch(rows) with_client('create_batch_types_batch') do |client| - unless client.prepared_statements.include?('query_sql_create_batch_types_batch') - client.prepare('query_sql_create_batch_types_batch', CREATE_BATCH_TYPES_BATCH_SQL) - client.prepared_statements.add('query_sql_create_batch_types_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_batch_types_batch') + client.prepare('pg_codegen_query_sql_create_batch_types_batch', CREATE_BATCH_TYPES_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_batch_types_batch') end rows.each do |args| query_params = [ @@ -1456,7 +1471,7 @@ def create_batch_types_batch(rows) encode_array(args.amounts), encode_array(args.stamps) ] - client.exec_prepared('query_sql_create_batch_types_batch', query_params) + client.exec_prepared('pg_codegen_query_sql_create_batch_types_batch', query_params) end nil end @@ -1476,11 +1491,11 @@ def copy_batch_types(rows) def list_batch_types with_client('list_batch_types') do |client| - unless client.prepared_statements.include?('query_sql_list_batch_types') - client.prepare('query_sql_list_batch_types', LIST_BATCH_TYPES_SQL) - client.prepared_statements.add('query_sql_list_batch_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_batch_types') + client.prepare('pg_codegen_query_sql_list_batch_types', LIST_BATCH_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_batch_types') end - result = client.exec_prepared('query_sql_list_batch_types') + result = client.exec_prepared('pg_codegen_query_sql_list_batch_types') result.map do |row| ListBatchTypesRow.new( id: row['id'], @@ -1499,11 +1514,11 @@ def list_batch_types def count_batch_types_above(min_id:) with_client('count_batch_types_above') do |client| query_params = [min_id] - unless client.prepared_statements.include?('query_sql_count_batch_types_above') - client.prepare('query_sql_count_batch_types_above', COUNT_BATCH_TYPES_ABOVE_SQL) - client.prepared_statements.add('query_sql_count_batch_types_above') + unless client.prepared_statements.include?('pg_codegen_query_sql_count_batch_types_above') + client.prepare('pg_codegen_query_sql_count_batch_types_above', COUNT_BATCH_TYPES_ABOVE_SQL) + client.prepared_statements.add('pg_codegen_query_sql_count_batch_types_above') end - result = client.exec_prepared('query_sql_count_batch_types_above', query_params) + result = client.exec_prepared('pg_codegen_query_sql_count_batch_types_above', query_params) row = result.first row && CountBatchTypesAboveRow.new(count_: row['count']) end @@ -1516,11 +1531,11 @@ def count_batch_types_above!(...) def truncate_batch_types with_client('truncate_batch_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_batch_types') - client.prepare('query_sql_truncate_batch_types', TRUNCATE_BATCH_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_batch_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_batch_types') + client.prepare('pg_codegen_query_sql_truncate_batch_types', TRUNCATE_BATCH_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_batch_types') end - client.exec_prepared('query_sql_truncate_batch_types') + client.exec_prepared('pg_codegen_query_sql_truncate_batch_types') nil end end @@ -1537,11 +1552,11 @@ def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, h camel_case, http_server ] - unless client.prepared_statements.include?('query_sql_insert_name_case') - client.prepare('query_sql_insert_name_case', INSERT_NAME_CASE_SQL) - client.prepared_statements.add('query_sql_insert_name_case') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_name_case') + client.prepare('pg_codegen_query_sql_insert_name_case', INSERT_NAME_CASE_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_name_case') end - client.exec_prepared('query_sql_insert_name_case', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_name_case', query_params) nil end end @@ -1558,11 +1573,11 @@ def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, cam http_server, camel_case ] - unless client.prepared_statements.include?('query_sql_get_name_case') - client.prepare('query_sql_get_name_case', GET_NAME_CASE_SQL) - client.prepared_statements.add('query_sql_get_name_case') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_name_case') + client.prepare('pg_codegen_query_sql_get_name_case', GET_NAME_CASE_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_name_case') end - result = client.exec_prepared('query_sql_get_name_case', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_name_case', query_params) row = result.first row && GetNameCaseRow.new( id: row['id'], @@ -1585,11 +1600,11 @@ def get_name_case!(...) def get_http_server(id:) with_client('get_http_server') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_get_http_server') - client.prepare('query_sql_get_http_server', GET_HTTP_SERVER_SQL) - client.prepared_statements.add('query_sql_get_http_server') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_http_server') + client.prepare('pg_codegen_query_sql_get_http_server', GET_HTTP_SERVER_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_http_server') end - result = client.exec_prepared('query_sql_get_http_server', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_http_server', query_params) row = result.first row && GetHTTPServerRow.new( id: row['id'], @@ -1611,15 +1626,49 @@ def get_http_server!(...) def truncate_name_cases with_client('truncate_name_cases') do |client| - unless client.prepared_statements.include?('query_sql_truncate_name_cases') - client.prepare('query_sql_truncate_name_cases', TRUNCATE_NAME_CASES_SQL) - client.prepared_statements.add('query_sql_truncate_name_cases') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_name_cases') + client.prepare('pg_codegen_query_sql_truncate_name_cases', TRUNCATE_NAME_CASES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_name_cases') end - client.exec_prepared('query_sql_truncate_name_cases') + client.exec_prepared('pg_codegen_query_sql_truncate_name_cases') nil end end + def get_unnamed_columns + with_client('get_unnamed_columns') do |client| + unless client.prepared_statements.include?('pg_codegen_query_sql_get_unnamed_columns') + client.prepare('pg_codegen_query_sql_get_unnamed_columns', GET_UNNAMED_COLUMNS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_unnamed_columns') + end + result = client.exec_prepared('pg_codegen_query_sql_get_unnamed_columns').values + row = result.first + row && GetUnnamedColumnsRow.new(column_1: row[0], column_2: row[1]) + end + end + + # Like #get_unnamed_columns, but raises NotFound when there is no row. + def get_unnamed_columns!(...) + get_unnamed_columns(...) || raise(NotFound, 'GetUnnamedColumns returned no row') + end + + def get_quoted_alias + with_client('get_quoted_alias') do |client| + unless client.prepared_statements.include?('pg_codegen_query_sql_get_quoted_alias') + client.prepare('pg_codegen_query_sql_get_quoted_alias', GET_QUOTED_ALIAS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_quoted_alias') + end + result = client.exec_prepared('pg_codegen_query_sql_get_quoted_alias') + row = result.first + row && GetQuotedAliasRow.new(it_s: row['it\'s']) + end + end + + # Like #get_quoted_alias, but raises NotFound when there is no row. + def get_quoted_alias!(...) + get_quoted_alias(...) || raise(NotFound, 'GetQuotedAlias returned no row') + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ @@ -1629,11 +1678,11 @@ def many_params(c0:, c1:, c2:, c3:, c4:) encode_float(c3), encode_bytea(c4) ] - unless client.prepared_statements.include?('query_sql_many_params') - client.prepare('query_sql_many_params', MANY_PARAMS_SQL) - client.prepared_statements.add('query_sql_many_params') + unless client.prepared_statements.include?('pg_codegen_query_sql_many_params') + client.prepare('pg_codegen_query_sql_many_params', MANY_PARAMS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_many_params') end - result = client.exec_prepared('query_sql_many_params', query_params) + result = client.exec_prepared('pg_codegen_query_sql_many_params', query_params) row = result.first row && ManyParamsRow.new(c0: row['c0'], c1: row['c1']) end @@ -1647,11 +1696,11 @@ def many_params!(...) def many_rows(c0:) with_client('many_rows') do |client| query_params = [c0] - unless client.prepared_statements.include?('query_sql_many_rows') - client.prepare('query_sql_many_rows', MANY_ROWS_SQL) - client.prepared_statements.add('query_sql_many_rows') + unless client.prepared_statements.include?('pg_codegen_query_sql_many_rows') + client.prepare('pg_codegen_query_sql_many_rows', MANY_ROWS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_many_rows') end - result = client.exec_prepared('query_sql_many_rows', query_params) + result = client.exec_prepared('pg_codegen_query_sql_many_rows', query_params) result.map do |row| ManyRowsRow.new( c0: row['c0'], @@ -1666,11 +1715,11 @@ def many_rows(c0:) def four_cols with_client('four_cols') do |client| - unless client.prepared_statements.include?('query_sql_four_cols') - client.prepare('query_sql_four_cols', FOUR_COLS_SQL) - client.prepared_statements.add('query_sql_four_cols') + unless client.prepared_statements.include?('pg_codegen_query_sql_four_cols') + client.prepare('pg_codegen_query_sql_four_cols', FOUR_COLS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_four_cols') end - result = client.exec_prepared('query_sql_four_cols') + result = client.exec_prepared('pg_codegen_query_sql_four_cols') result.map do |row| FourColsRow.new(c0: row['c0'], c1: row['c1'], c2: row['c2'], c3: row['c3']) end @@ -1696,7 +1745,13 @@ def with_client(name) @db.with do |client| client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) configure(client) unless client.is_a?(ConnectionState) - yield client + begin + yield client + rescue Exception # rubocop:disable Lint/RescueException + # a query left by Timeout / Thread#raise keeps running: cancel it before the connection is reused + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE + raise + end end end end @@ -1735,11 +1790,12 @@ def encode_bytea(value) value && PG::TextEncoder::Bytea.new.encode(value) end - # json / jsonb parameters: booleans and Arrays need JSON encoding, not PostgreSQL's boolean / array encoding. - # Hashes and JSON text pass through; nil stays nil. + # json / jsonb parameters: booleans and Arrays need JSON encoding, not PostgreSQL's boolean / array encoding, + # and Floats their shortest exact text. Hashes and JSON text pass through; nil stays nil. def encode_json(value) case value when Array, TrueClass, FalseClass then PG::TextEncoder::JSON.new.encode(value) + when Float then value.to_s else value end end diff --git a/test/golden/pg/ruby31_gemfile/query_sql.rbs b/test/golden/pg/ruby31_gemfile/query_sql.rbs index 0e50262..d11eea7 100644 --- a/test/golden/pg/ruby31_gemfile/query_sql.rbs +++ b/test/golden/pg/ruby31_gemfile/query_sql.rbs @@ -702,6 +702,23 @@ module PgCodegen TRUNCATE_NAME_CASES_SQL: String + GET_UNNAMED_COLUMNS_SQL: String + + class GetUnnamedColumnsRow < ::Struct[untyped] + attr_accessor column_1: Integer + attr_accessor column_2: String + def initialize: ( column_1: Integer, column_2: String) -> void + def self.new: ( column_1: Integer, column_2: String) -> GetUnnamedColumnsRow + end + + GET_QUOTED_ALIAS_SQL: String + + class GetQuotedAliasRow < ::Struct[untyped] + attr_accessor it_s: Integer + def initialize: ( it_s: Integer) -> void + def self.new: ( it_s: Integer) -> GetQuotedAliasRow + end + MANY_PARAMS_SQL: String class ManyParamsRow < ::Struct[untyped] @@ -911,6 +928,14 @@ module PgCodegen def truncate_name_cases: -> void + def get_unnamed_columns: -> GetUnnamedColumnsRow? + + def get_unnamed_columns!: -> GetUnnamedColumnsRow + + def get_quoted_alias: -> GetQuotedAliasRow? + + def get_quoted_alias!: -> GetQuotedAliasRow + def many_params: (c0: bool, c1: Integer, c2: String, c3: Float, c4: String) -> ManyParamsRow? def many_params!: (c0: bool, c1: Integer, c2: String, c3: Float, c4: String) -> ManyParamsRow diff --git a/test/golden/pg/ruby32_notypes/my_queries_sql.rb b/test/golden/pg/ruby32_notypes/my_queries_sql.rb index c60c07d..c410b59 100644 --- a/test/golden/pg/ruby32_notypes/my_queries_sql.rb +++ b/test/golden/pg/ruby32_notypes/my_queries_sql.rb @@ -52,6 +52,7 @@ def transaction(isolation: nil) result rescue Exception # rubocop:disable Lint/RescueException failed = true + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE client.exec(rollback) raise ensure @@ -70,11 +71,11 @@ def transaction(isolation: nil) def with_numbers2_go(c0:) with_client('with_numbers2_go') do |client| query_params = [c0] - unless client.prepared_statements.include?('my_queries_sql_with_numbers2_go') - client.prepare('my_queries_sql_with_numbers2_go', WITH_NUMBERS2_GO_SQL) - client.prepared_statements.add('my_queries_sql_with_numbers2_go') + unless client.prepared_statements.include?('pg_codegen_my_queries_sql_with_numbers2_go') + client.prepare('pg_codegen_my_queries_sql_with_numbers2_go', WITH_NUMBERS2_GO_SQL) + client.prepared_statements.add('pg_codegen_my_queries_sql_with_numbers2_go') end - client.exec_prepared('my_queries_sql_with_numbers2_go', query_params) + client.exec_prepared('pg_codegen_my_queries_sql_with_numbers2_go', query_params) nil end end @@ -98,7 +99,13 @@ def with_client(name) @db.with do |client| client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) configure(client) unless client.is_a?(ConnectionState) - yield client + begin + yield client + rescue Exception # rubocop:disable Lint/RescueException + # a query left by Timeout / Thread#raise keeps running: cancel it before the connection is reused + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE + raise + end end end end diff --git a/test/golden/pg/ruby32_notypes/other_sql.rb b/test/golden/pg/ruby32_notypes/other_sql.rb index e415ab8..73e5cfe 100644 --- a/test/golden/pg/ruby32_notypes/other_sql.rb +++ b/test/golden/pg/ruby32_notypes/other_sql.rb @@ -65,6 +65,7 @@ def transaction(isolation: nil) result rescue Exception # rubocop:disable Lint/RescueException failed = true + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE client.exec(rollback) raise ensure @@ -89,22 +90,22 @@ def no_cols(c0:, c1:, c2:, c3:, c4:) encode_float(c3), encode_bytea(c4) ] - unless client.prepared_statements.include?('other_sql_no_cols') - client.prepare('other_sql_no_cols', NO_COLS_SQL) - client.prepared_statements.add('other_sql_no_cols') + unless client.prepared_statements.include?('pg_codegen_other_sql_no_cols') + client.prepare('pg_codegen_other_sql_no_cols', NO_COLS_SQL) + client.prepared_statements.add('pg_codegen_other_sql_no_cols') end - client.exec_prepared('other_sql_no_cols', query_params) + client.exec_prepared('pg_codegen_other_sql_no_cols', query_params) nil end end def other_one with_client('other_one') do |client| - unless client.prepared_statements.include?('other_sql_other_one') - client.prepare('other_sql_other_one', OTHER_ONE_SQL) - client.prepared_statements.add('other_sql_other_one') + unless client.prepared_statements.include?('pg_codegen_other_sql_other_one') + client.prepare('pg_codegen_other_sql_other_one', OTHER_ONE_SQL) + client.prepared_statements.add('pg_codegen_other_sql_other_one') end - result = client.exec_prepared('other_sql_other_one') + result = client.exec_prepared('pg_codegen_other_sql_other_one') row = result.first row && OtherOneRow.new(row['c0']) end @@ -118,11 +119,11 @@ def other_one!(...) def count_rows(c0:) with_client('count_rows') do |client| query_params = [c0] - unless client.prepared_statements.include?('other_sql_count_rows') - client.prepare('other_sql_count_rows', COUNT_ROWS_SQL) - client.prepared_statements.add('other_sql_count_rows') + unless client.prepared_statements.include?('pg_codegen_other_sql_count_rows') + client.prepare('pg_codegen_other_sql_count_rows', COUNT_ROWS_SQL) + client.prepared_statements.add('pg_codegen_other_sql_count_rows') end - result = client.exec_prepared('other_sql_count_rows', query_params) + result = client.exec_prepared('pg_codegen_other_sql_count_rows', query_params) result.cmd_tuples end end @@ -146,7 +147,13 @@ def with_client(name) @db.with do |client| client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) configure(client) unless client.is_a?(ConnectionState) - yield client + begin + yield client + rescue Exception # rubocop:disable Lint/RescueException + # a query left by Timeout / Thread#raise keeps running: cancel it before the connection is reused + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE + raise + end end end end diff --git a/test/golden/pg/ruby32_notypes/query_sql.rb b/test/golden/pg/ruby32_notypes/query_sql.rb index 7c91085..fc136c5 100644 --- a/test/golden/pg/ruby32_notypes/query_sql.rb +++ b/test/golden/pg/ruby32_notypes/query_sql.rb @@ -527,6 +527,22 @@ class GetHTTPServerRow < Data.define( TRUNCATE TABLE name_cases SQL + GET_UNNAMED_COLUMNS_SQL = <<~'SQL' + SELECT 1, 'two' + SQL + + # rubocop:disable Naming/VariableNumber + class GetUnnamedColumnsRow < Data.define(:column_1, :column_2) + end + # rubocop:enable Naming/VariableNumber + + GET_QUOTED_ALIAS_SQL = <<~'SQL' + SELECT 1 AS "it's" + SQL + + class GetQuotedAliasRow < Data.define(:it_s) + end + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -597,6 +613,7 @@ def transaction(isolation: nil) result rescue Exception # rubocop:disable Lint/RescueException failed = true + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE client.exec(rollback) raise ensure @@ -619,11 +636,11 @@ class ExecResult < Data.define(:rows_affected, :last_insert_id) def get_author(id:) with_client('get_author') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_get_author') - client.prepare('query_sql_get_author', GET_AUTHOR_SQL) - client.prepared_statements.add('query_sql_get_author') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_author') + client.prepare('pg_codegen_query_sql_get_author', GET_AUTHOR_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_author') end - result = client.exec_prepared('query_sql_get_author', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_author', query_params) row = result.first row && GetAuthorRow.new(row['id'], row['name'], row['bio']) end @@ -636,11 +653,11 @@ def get_author!(...) def list_authors with_client('list_authors') do |client| - unless client.prepared_statements.include?('query_sql_list_authors') - client.prepare('query_sql_list_authors', LIST_AUTHORS_SQL) - client.prepared_statements.add('query_sql_list_authors') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors') + client.prepare('pg_codegen_query_sql_list_authors', LIST_AUTHORS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors') end - result = client.exec_prepared('query_sql_list_authors') + result = client.exec_prepared('pg_codegen_query_sql_list_authors') result.map do |row| ListAuthorsRow.new(row['id'], row['name'], row['bio']) end @@ -650,11 +667,11 @@ def list_authors def create_author(name:, bio:) with_client('create_author') do |client| query_params = [name, bio] - unless client.prepared_statements.include?('query_sql_create_author') - client.prepare('query_sql_create_author', CREATE_AUTHOR_SQL) - client.prepared_statements.add('query_sql_create_author') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_author') + client.prepare('pg_codegen_query_sql_create_author', CREATE_AUTHOR_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_author') end - result = client.exec_prepared('query_sql_create_author', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_author', query_params) row = result.first row && CreateAuthorRow.new(row['id'], row['name'], row['bio']) end @@ -668,22 +685,22 @@ def create_author!(...) def delete_author(id:) with_client('delete_author') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_delete_author') - client.prepare('query_sql_delete_author', DELETE_AUTHOR_SQL) - client.prepared_statements.add('query_sql_delete_author') + unless client.prepared_statements.include?('pg_codegen_query_sql_delete_author') + client.prepare('pg_codegen_query_sql_delete_author', DELETE_AUTHOR_SQL) + client.prepared_statements.add('pg_codegen_query_sql_delete_author') end - client.exec_prepared('query_sql_delete_author', query_params) + client.exec_prepared('pg_codegen_query_sql_delete_author', query_params) nil end end def test with_client('test') do |client| - unless client.prepared_statements.include?('query_sql_test') - client.prepare('query_sql_test', TEST_SQL) - client.prepared_statements.add('query_sql_test') + unless client.prepared_statements.include?('pg_codegen_query_sql_test') + client.prepare('pg_codegen_query_sql_test', TEST_SQL) + client.prepared_statements.add('pg_codegen_query_sql_test') end - result = client.exec_prepared('query_sql_test') + result = client.exec_prepared('pg_codegen_query_sql_test') row = result.first row && TestRow.new( row['c_bit'], @@ -716,11 +733,11 @@ def test!(...) def set_json_round_trip(id:, json_value:, jsonb_value:) with_client('set_json_round_trip') do |client| query_params = [id, encode_json(json_value), encode_json(jsonb_value)] - unless client.prepared_statements.include?('query_sql_set_json_round_trip') - client.prepare('query_sql_set_json_round_trip', SET_JSON_ROUND_TRIP_SQL) - client.prepared_statements.add('query_sql_set_json_round_trip') + unless client.prepared_statements.include?('pg_codegen_query_sql_set_json_round_trip') + client.prepare('pg_codegen_query_sql_set_json_round_trip', SET_JSON_ROUND_TRIP_SQL) + client.prepared_statements.add('pg_codegen_query_sql_set_json_round_trip') end - client.exec_prepared('query_sql_set_json_round_trip', query_params) + client.exec_prepared('pg_codegen_query_sql_set_json_round_trip', query_params) nil end end @@ -728,11 +745,11 @@ def set_json_round_trip(id:, json_value:, jsonb_value:) def get_json_round_trip(id:) with_client('get_json_round_trip') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_get_json_round_trip') - client.prepare('query_sql_get_json_round_trip', GET_JSON_ROUND_TRIP_SQL) - client.prepared_statements.add('query_sql_get_json_round_trip') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_json_round_trip') + client.prepare('pg_codegen_query_sql_get_json_round_trip', GET_JSON_ROUND_TRIP_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_json_round_trip') end - result = client.exec_prepared('query_sql_get_json_round_trip', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_json_round_trip', query_params) row = result.first row && GetJSONRoundTripRow.new(row['json_value'], row['jsonb_value']) end @@ -746,22 +763,22 @@ def get_json_round_trip!(...) def toggle_jsonb_round_trip(id:) with_client('toggle_jsonb_round_trip') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_toggle_jsonb_round_trip') - client.prepare('query_sql_toggle_jsonb_round_trip', TOGGLE_JSONB_ROUND_TRIP_SQL) - client.prepared_statements.add('query_sql_toggle_jsonb_round_trip') + unless client.prepared_statements.include?('pg_codegen_query_sql_toggle_jsonb_round_trip') + client.prepare('pg_codegen_query_sql_toggle_jsonb_round_trip', TOGGLE_JSONB_ROUND_TRIP_SQL) + client.prepared_statements.add('pg_codegen_query_sql_toggle_jsonb_round_trip') end - client.exec_prepared('query_sql_toggle_jsonb_round_trip', query_params) + client.exec_prepared('pg_codegen_query_sql_toggle_jsonb_round_trip', query_params) nil end end def truncate_json_round_trips with_client('truncate_json_round_trips') do |client| - unless client.prepared_statements.include?('query_sql_truncate_json_round_trips') - client.prepare('query_sql_truncate_json_round_trips', TRUNCATE_JSON_ROUND_TRIPS_SQL) - client.prepared_statements.add('query_sql_truncate_json_round_trips') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_json_round_trips') + client.prepare('pg_codegen_query_sql_truncate_json_round_trips', TRUNCATE_JSON_ROUND_TRIPS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_json_round_trips') end - client.exec_prepared('query_sql_truncate_json_round_trips') + client.exec_prepared('pg_codegen_query_sql_truncate_json_round_trips') nil end end @@ -769,11 +786,11 @@ def truncate_json_round_trips def pause_query(seconds:) with_client('pause_query') do |client| query_params = [encode_float(seconds)] - unless client.prepared_statements.include?('query_sql_pause_query') - client.prepare('query_sql_pause_query', PAUSE_QUERY_SQL) - client.prepared_statements.add('query_sql_pause_query') + unless client.prepared_statements.include?('pg_codegen_query_sql_pause_query') + client.prepare('pg_codegen_query_sql_pause_query', PAUSE_QUERY_SQL) + client.prepared_statements.add('pg_codegen_query_sql_pause_query') end - client.exec_prepared('query_sql_pause_query', query_params) + client.exec_prepared('pg_codegen_query_sql_pause_query', query_params) nil end end @@ -781,11 +798,11 @@ def pause_query(seconds:) def list_authors_paged(offset:, limit:) with_client('list_authors_paged') do |client| query_params = [offset, limit] - unless client.prepared_statements.include?('query_sql_list_authors_paged') - client.prepare('query_sql_list_authors_paged', LIST_AUTHORS_PAGED_SQL) - client.prepared_statements.add('query_sql_list_authors_paged') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors_paged') + client.prepare('pg_codegen_query_sql_list_authors_paged', LIST_AUTHORS_PAGED_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors_paged') end - result = client.exec_prepared('query_sql_list_authors_paged', query_params) + result = client.exec_prepared('pg_codegen_query_sql_list_authors_paged', query_params) result.map do |row| ListAuthorsPagedRow.new(row['id'], row['name'], row['bio']) end @@ -795,11 +812,11 @@ def list_authors_paged(offset:, limit:) def get_author_by_name_pattern(name_pattern: nil) with_client('get_author_by_name_pattern') do |client| query_params = [name_pattern] - unless client.prepared_statements.include?('query_sql_get_author_by_name_pattern') - client.prepare('query_sql_get_author_by_name_pattern', GET_AUTHOR_BY_NAME_PATTERN_SQL) - client.prepared_statements.add('query_sql_get_author_by_name_pattern') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_author_by_name_pattern') + client.prepare('pg_codegen_query_sql_get_author_by_name_pattern', GET_AUTHOR_BY_NAME_PATTERN_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_author_by_name_pattern') end - result = client.exec_prepared('query_sql_get_author_by_name_pattern', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_author_by_name_pattern', query_params) result.map do |row| GetAuthorByNamePatternRow.new(row['id'], row['name'], row['bio']) end @@ -809,11 +826,11 @@ def get_author_by_name_pattern(name_pattern: nil) def get_authors_by_optional_name(lim:, name_filter: nil) with_client('get_authors_by_optional_name') do |client| query_params = [name_filter, lim] - unless client.prepared_statements.include?('query_sql_get_authors_by_optional_name') - client.prepare('query_sql_get_authors_by_optional_name', GET_AUTHORS_BY_OPTIONAL_NAME_SQL) - client.prepared_statements.add('query_sql_get_authors_by_optional_name') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_authors_by_optional_name') + client.prepare('pg_codegen_query_sql_get_authors_by_optional_name', GET_AUTHORS_BY_OPTIONAL_NAME_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_authors_by_optional_name') end - result = client.exec_prepared('query_sql_get_authors_by_optional_name', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_authors_by_optional_name', query_params) result.map do |row| GetAuthorsByOptionalNameRow.new(row['id'], row['name'], row['bio']) end @@ -823,11 +840,11 @@ def get_authors_by_optional_name(lim:, name_filter: nil) def create_author_return_id(name:, bio:) with_client('create_author_return_id') do |client| query_params = [name, bio] - unless client.prepared_statements.include?('query_sql_create_author_return_id') - client.prepare('query_sql_create_author_return_id', CREATE_AUTHOR_RETURN_ID_SQL) - client.prepared_statements.add('query_sql_create_author_return_id') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_author_return_id') + client.prepare('pg_codegen_query_sql_create_author_return_id', CREATE_AUTHOR_RETURN_ID_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_author_return_id') end - result = client.exec_prepared('query_sql_create_author_return_id', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_author_return_id', query_params) row = result.first row && row['id'] end @@ -837,11 +854,11 @@ def create_author_return_id(name:, bio:) def update_author_bios(bio:) with_client('update_author_bios') do |client| query_params = [bio] - unless client.prepared_statements.include?('query_sql_update_author_bios') - client.prepare('query_sql_update_author_bios', UPDATE_AUTHOR_BIOS_SQL) - client.prepared_statements.add('query_sql_update_author_bios') + unless client.prepared_statements.include?('pg_codegen_query_sql_update_author_bios') + client.prepare('pg_codegen_query_sql_update_author_bios', UPDATE_AUTHOR_BIOS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_update_author_bios') end - result = client.exec_prepared('query_sql_update_author_bios', query_params) + result = client.exec_prepared('pg_codegen_query_sql_update_author_bios', query_params) result.cmd_tuples end end @@ -849,11 +866,11 @@ def update_author_bios(bio:) def get_authors_by_ids(param1:) with_client('get_authors_by_ids') do |client| query_params = [encode_array(param1)] - unless client.prepared_statements.include?('query_sql_get_authors_by_ids') - client.prepare('query_sql_get_authors_by_ids', GET_AUTHORS_BY_IDS_SQL) - client.prepared_statements.add('query_sql_get_authors_by_ids') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_authors_by_ids') + client.prepare('pg_codegen_query_sql_get_authors_by_ids', GET_AUTHORS_BY_IDS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_authors_by_ids') end - result = client.exec_prepared('query_sql_get_authors_by_ids', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_authors_by_ids', query_params) result.map do |row| GetAuthorsByIdsRow.new(row['id'], row['name'], row['bio']) end @@ -863,11 +880,11 @@ def get_authors_by_ids(param1:) def get_authors_by_ids_and_names(param1:, param2:) with_client('get_authors_by_ids_and_names') do |client| query_params = [encode_array(param1), encode_array(param2)] - unless client.prepared_statements.include?('query_sql_get_authors_by_ids_and_names') - client.prepare('query_sql_get_authors_by_ids_and_names', GET_AUTHORS_BY_IDS_AND_NAMES_SQL) - client.prepared_statements.add('query_sql_get_authors_by_ids_and_names') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_authors_by_ids_and_names') + client.prepare('pg_codegen_query_sql_get_authors_by_ids_and_names', GET_AUTHORS_BY_IDS_AND_NAMES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_authors_by_ids_and_names') end - result = client.exec_prepared('query_sql_get_authors_by_ids_and_names', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_authors_by_ids_and_names', query_params) result.map do |row| GetAuthorsByIdsAndNamesRow.new(row['id'], row['name'], row['bio']) end @@ -889,11 +906,11 @@ def copy_authors(rows) def truncate_authors with_client('truncate_authors') do |client| - unless client.prepared_statements.include?('query_sql_truncate_authors') - client.prepare('query_sql_truncate_authors', TRUNCATE_AUTHORS_SQL) - client.prepared_statements.add('query_sql_truncate_authors') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_authors') + client.prepare('pg_codegen_query_sql_truncate_authors', TRUNCATE_AUTHORS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_authors') end - client.exec_prepared('query_sql_truncate_authors') + client.exec_prepared('pg_codegen_query_sql_truncate_authors') nil end end @@ -901,11 +918,11 @@ def truncate_authors def create_book(name:, author_id:) with_client('create_book') do |client| query_params = [name, author_id] - unless client.prepared_statements.include?('query_sql_create_book') - client.prepare('query_sql_create_book', CREATE_BOOK_SQL) - client.prepared_statements.add('query_sql_create_book') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_book') + client.prepare('pg_codegen_query_sql_create_book', CREATE_BOOK_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_book') end - result = client.exec_prepared('query_sql_create_book', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_book', query_params) row = result.first row && row['id'] end @@ -914,33 +931,33 @@ def create_book(name:, author_id:) def create_deferred_book(author_id:) with_client('create_deferred_book') do |client| query_params = [author_id] - unless client.prepared_statements.include?('query_sql_create_deferred_book') - client.prepare('query_sql_create_deferred_book', CREATE_DEFERRED_BOOK_SQL) - client.prepared_statements.add('query_sql_create_deferred_book') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_deferred_book') + client.prepare('pg_codegen_query_sql_create_deferred_book', CREATE_DEFERRED_BOOK_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_deferred_book') end - client.exec_prepared('query_sql_create_deferred_book', query_params) + client.exec_prepared('pg_codegen_query_sql_create_deferred_book', query_params) nil end end def truncate_deferred_books with_client('truncate_deferred_books') do |client| - unless client.prepared_statements.include?('query_sql_truncate_deferred_books') - client.prepare('query_sql_truncate_deferred_books', TRUNCATE_DEFERRED_BOOKS_SQL) - client.prepared_statements.add('query_sql_truncate_deferred_books') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_deferred_books') + client.prepare('pg_codegen_query_sql_truncate_deferred_books', TRUNCATE_DEFERRED_BOOKS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_deferred_books') end - client.exec_prepared('query_sql_truncate_deferred_books') + client.exec_prepared('pg_codegen_query_sql_truncate_deferred_books') nil end end def list_all_authors_books with_client('list_all_authors_books') do |client| - unless client.prepared_statements.include?('query_sql_list_all_authors_books') - client.prepare('query_sql_list_all_authors_books', LIST_ALL_AUTHORS_BOOKS_SQL) - client.prepared_statements.add('query_sql_list_all_authors_books') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_all_authors_books') + client.prepare('pg_codegen_query_sql_list_all_authors_books', LIST_ALL_AUTHORS_BOOKS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_all_authors_books') end - result = client.exec_prepared('query_sql_list_all_authors_books').values + result = client.exec_prepared('pg_codegen_query_sql_list_all_authors_books').values result.map do |row| ListAllAuthorsBooksRow.new( Author.new(row[0], row[1], row[2]), @@ -952,11 +969,11 @@ def list_all_authors_books def get_duplicate_authors with_client('get_duplicate_authors') do |client| - unless client.prepared_statements.include?('query_sql_get_duplicate_authors') - client.prepare('query_sql_get_duplicate_authors', GET_DUPLICATE_AUTHORS_SQL) - client.prepared_statements.add('query_sql_get_duplicate_authors') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_duplicate_authors') + client.prepare('pg_codegen_query_sql_get_duplicate_authors', GET_DUPLICATE_AUTHORS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_duplicate_authors') end - result = client.exec_prepared('query_sql_get_duplicate_authors').values + result = client.exec_prepared('pg_codegen_query_sql_get_duplicate_authors').values result.map do |row| GetDuplicateAuthorsRow.new(Author.new(row[0], row[1], row[2]), Author.new(row[3], row[4], row[5])) end @@ -966,11 +983,11 @@ def get_duplicate_authors def get_authors_by_book_name(name:) with_client('get_authors_by_book_name') do |client| query_params = [name] - unless client.prepared_statements.include?('query_sql_get_authors_by_book_name') - client.prepare('query_sql_get_authors_by_book_name', GET_AUTHORS_BY_BOOK_NAME_SQL) - client.prepared_statements.add('query_sql_get_authors_by_book_name') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_authors_by_book_name') + client.prepare('pg_codegen_query_sql_get_authors_by_book_name', GET_AUTHORS_BY_BOOK_NAME_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_authors_by_book_name') end - result = client.exec_prepared('query_sql_get_authors_by_book_name', query_params).values + result = client.exec_prepared('pg_codegen_query_sql_get_authors_by_book_name', query_params).values result.map do |row| GetAuthorsByBookNameRow.new(row[0], row[1], row[2], Book.new(row[3], row[4], row[5], row[6])) end @@ -980,11 +997,11 @@ def get_authors_by_book_name(name:) def create_extended_bio(author_name:, name:, bio_type:) with_client('create_extended_bio') do |client| query_params = [author_name, name, bio_type] - unless client.prepared_statements.include?('query_sql_create_extended_bio') - client.prepare('query_sql_create_extended_bio', CREATE_EXTENDED_BIO_SQL) - client.prepared_statements.add('query_sql_create_extended_bio') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_extended_bio') + client.prepare('pg_codegen_query_sql_create_extended_bio', CREATE_EXTENDED_BIO_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_extended_bio') end - client.exec_prepared('query_sql_create_extended_bio', query_params) + client.exec_prepared('pg_codegen_query_sql_create_extended_bio', query_params) nil end end @@ -992,11 +1009,11 @@ def create_extended_bio(author_name:, name:, bio_type:) def get_first_extended_bio_by_type(bio_type:) with_client('get_first_extended_bio_by_type') do |client| query_params = [bio_type] - unless client.prepared_statements.include?('query_sql_get_first_extended_bio_by_type') - client.prepare('query_sql_get_first_extended_bio_by_type', GET_FIRST_EXTENDED_BIO_BY_TYPE_SQL) - client.prepared_statements.add('query_sql_get_first_extended_bio_by_type') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_first_extended_bio_by_type') + client.prepare('pg_codegen_query_sql_get_first_extended_bio_by_type', GET_FIRST_EXTENDED_BIO_BY_TYPE_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_first_extended_bio_by_type') end - result = client.exec_prepared('query_sql_get_first_extended_bio_by_type', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_first_extended_bio_by_type', query_params) row = result.first row && GetFirstExtendedBioByTypeRow.new(row['author_name'], row['name'], row['bio_type']) end @@ -1009,11 +1026,11 @@ def get_first_extended_bio_by_type!(...) def truncate_extended_bios with_client('truncate_extended_bios') do |client| - unless client.prepared_statements.include?('query_sql_truncate_extended_bios') - client.prepare('query_sql_truncate_extended_bios', TRUNCATE_EXTENDED_BIOS_SQL) - client.prepared_statements.add('query_sql_truncate_extended_bios') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_extended_bios') + client.prepare('pg_codegen_query_sql_truncate_extended_bios', TRUNCATE_EXTENDED_BIOS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_extended_bios') end - client.exec_prepared('query_sql_truncate_extended_bios') + client.exec_prepared('pg_codegen_query_sql_truncate_extended_bios') nil end end @@ -1030,22 +1047,22 @@ def insert_extra_types(c_uuid:, c_jsonb:, c_text_array:, c_integer_array:, c_tim c_money, c_not_null ] - unless client.prepared_statements.include?('query_sql_insert_extra_types') - client.prepare('query_sql_insert_extra_types', INSERT_EXTRA_TYPES_SQL) - client.prepared_statements.add('query_sql_insert_extra_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_extra_types') + client.prepare('pg_codegen_query_sql_insert_extra_types', INSERT_EXTRA_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_extra_types') end - client.exec_prepared('query_sql_insert_extra_types', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_extra_types', query_params) nil end end def get_extra_types with_client('get_extra_types') do |client| - unless client.prepared_statements.include?('query_sql_get_extra_types') - client.prepare('query_sql_get_extra_types', GET_EXTRA_TYPES_SQL) - client.prepared_statements.add('query_sql_get_extra_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_extra_types') + client.prepare('pg_codegen_query_sql_get_extra_types', GET_EXTRA_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_extra_types') end - result = client.exec_prepared('query_sql_get_extra_types') + result = client.exec_prepared('pg_codegen_query_sql_get_extra_types') row = result.first row && GetExtraTypesRow.new( row['c_uuid'], @@ -1067,11 +1084,11 @@ def get_extra_types!(...) def truncate_extra_types with_client('truncate_extra_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_extra_types') - client.prepare('query_sql_truncate_extra_types', TRUNCATE_EXTRA_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_extra_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_extra_types') + client.prepare('pg_codegen_query_sql_truncate_extra_types', TRUNCATE_EXTRA_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_extra_types') end - client.exec_prepared('query_sql_truncate_extra_types') + client.exec_prepared('pg_codegen_query_sql_truncate_extra_types') nil end end @@ -1093,22 +1110,22 @@ def insert_remaining_types(c_hstore:, c_ltree:, c_regclass:, c_cidr:, c_macaddr: c_timestamptz, encode_array(c_text_matrix) ] - unless client.prepared_statements.include?('query_sql_insert_remaining_types') - client.prepare('query_sql_insert_remaining_types', INSERT_REMAINING_TYPES_SQL) - client.prepared_statements.add('query_sql_insert_remaining_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_remaining_types') + client.prepare('pg_codegen_query_sql_insert_remaining_types', INSERT_REMAINING_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_remaining_types') end - client.exec_prepared('query_sql_insert_remaining_types', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_remaining_types', query_params) nil end end def get_remaining_types with_client('get_remaining_types') do |client| - unless client.prepared_statements.include?('query_sql_get_remaining_types') - client.prepare('query_sql_get_remaining_types', GET_REMAINING_TYPES_SQL) - client.prepared_statements.add('query_sql_get_remaining_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_remaining_types') + client.prepare('pg_codegen_query_sql_get_remaining_types', GET_REMAINING_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_remaining_types') end - result = client.exec_prepared('query_sql_get_remaining_types') + result = client.exec_prepared('pg_codegen_query_sql_get_remaining_types') row = result.first row && GetRemainingTypesRow.new( row['c_hstore'], @@ -1135,11 +1152,11 @@ def get_remaining_types!(...) def truncate_remaining_types with_client('truncate_remaining_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_remaining_types') - client.prepare('query_sql_truncate_remaining_types', TRUNCATE_REMAINING_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_remaining_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_remaining_types') + client.prepare('pg_codegen_query_sql_truncate_remaining_types', TRUNCATE_REMAINING_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_remaining_types') end - client.exec_prepared('query_sql_truncate_remaining_types') + client.exec_prepared('pg_codegen_query_sql_truncate_remaining_types') nil end end @@ -1147,22 +1164,22 @@ def truncate_remaining_types def insert_array_types(c_uuid_array:, c_bio_type_array:, c_integer_matrix:) with_client('insert_array_types') do |client| query_params = [encode_array(c_uuid_array), encode_array(c_bio_type_array), encode_array(c_integer_matrix)] - unless client.prepared_statements.include?('query_sql_insert_array_types') - client.prepare('query_sql_insert_array_types', INSERT_ARRAY_TYPES_SQL) - client.prepared_statements.add('query_sql_insert_array_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_array_types') + client.prepare('pg_codegen_query_sql_insert_array_types', INSERT_ARRAY_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_array_types') end - client.exec_prepared('query_sql_insert_array_types', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_array_types', query_params) nil end end def get_array_types with_client('get_array_types') do |client| - unless client.prepared_statements.include?('query_sql_get_array_types') - client.prepare('query_sql_get_array_types', GET_ARRAY_TYPES_SQL) - client.prepared_statements.add('query_sql_get_array_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_array_types') + client.prepare('pg_codegen_query_sql_get_array_types', GET_ARRAY_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_array_types') end - result = client.exec_prepared('query_sql_get_array_types') + result = client.exec_prepared('pg_codegen_query_sql_get_array_types') row = result.first row && GetArrayTypesRow.new(row['c_uuid_array'], row['c_bio_type_array'], row['c_integer_matrix']) end @@ -1175,22 +1192,22 @@ def get_array_types!(...) def truncate_array_types with_client('truncate_array_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_array_types') - client.prepare('query_sql_truncate_array_types', TRUNCATE_ARRAY_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_array_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_array_types') + client.prepare('pg_codegen_query_sql_truncate_array_types', TRUNCATE_ARRAY_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_array_types') end - client.exec_prepared('query_sql_truncate_array_types') + client.exec_prepared('pg_codegen_query_sql_truncate_array_types') nil end end def list_authors_with_optional_book with_client('list_authors_with_optional_book') do |client| - unless client.prepared_statements.include?('query_sql_list_authors_with_optional_book') - client.prepare('query_sql_list_authors_with_optional_book', LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL) - client.prepared_statements.add('query_sql_list_authors_with_optional_book') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors_with_optional_book') + client.prepare('pg_codegen_query_sql_list_authors_with_optional_book', LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors_with_optional_book') end - result = client.exec_prepared('query_sql_list_authors_with_optional_book').values + result = client.exec_prepared('pg_codegen_query_sql_list_authors_with_optional_book').values result.map do |row| ListAuthorsWithOptionalBookRow.new( row[0].nil? && row[1].nil? ? nil : Author.new(row[0], row[1], row[2]), @@ -1203,11 +1220,11 @@ def list_authors_with_optional_book def list_authors_between_ids(id:, id_2:) with_client('list_authors_between_ids') do |client| query_params = [id, id_2] - unless client.prepared_statements.include?('query_sql_list_authors_between_ids') - client.prepare('query_sql_list_authors_between_ids', LIST_AUTHORS_BETWEEN_IDS_SQL) - client.prepared_statements.add('query_sql_list_authors_between_ids') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors_between_ids') + client.prepare('pg_codegen_query_sql_list_authors_between_ids', LIST_AUTHORS_BETWEEN_IDS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors_between_ids') end - result = client.exec_prepared('query_sql_list_authors_between_ids', query_params) + result = client.exec_prepared('pg_codegen_query_sql_list_authors_between_ids', query_params) result.map do |row| ListAuthorsBetweenIdsRow.new(row['id'], row['name'], row['bio']) end @@ -1225,22 +1242,22 @@ def insert_special_types(c_timetz:, c_tsvector:, c_point:, c_int4range:, c_oid:, c_dimensions, c_positive ] - unless client.prepared_statements.include?('query_sql_insert_special_types') - client.prepare('query_sql_insert_special_types', INSERT_SPECIAL_TYPES_SQL) - client.prepared_statements.add('query_sql_insert_special_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_special_types') + client.prepare('pg_codegen_query_sql_insert_special_types', INSERT_SPECIAL_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_special_types') end - client.exec_prepared('query_sql_insert_special_types', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_special_types', query_params) nil end end def get_special_types with_client('get_special_types') do |client| - unless client.prepared_statements.include?('query_sql_get_special_types') - client.prepare('query_sql_get_special_types', GET_SPECIAL_TYPES_SQL) - client.prepared_statements.add('query_sql_get_special_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_special_types') + client.prepare('pg_codegen_query_sql_get_special_types', GET_SPECIAL_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_special_types') end - result = client.exec_prepared('query_sql_get_special_types') + result = client.exec_prepared('pg_codegen_query_sql_get_special_types') row = result.first row && GetSpecialTypesRow.new( row['c_timetz'], @@ -1261,11 +1278,11 @@ def get_special_types!(...) def truncate_special_types with_client('truncate_special_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_special_types') - client.prepare('query_sql_truncate_special_types', TRUNCATE_SPECIAL_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_special_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_special_types') + client.prepare('pg_codegen_query_sql_truncate_special_types', TRUNCATE_SPECIAL_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_special_types') end - client.exec_prepared('query_sql_truncate_special_types') + client.exec_prepared('pg_codegen_query_sql_truncate_special_types') nil end end @@ -1273,24 +1290,24 @@ def truncate_special_types def create_author_result(name:, bio:) with_client('create_author_result') do |client| query_params = [name, bio] - unless client.prepared_statements.include?('query_sql_create_author_result') - client.prepare('query_sql_create_author_result', CREATE_AUTHOR_RESULT_SQL) - client.prepared_statements.add('query_sql_create_author_result') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_author_result') + client.prepare('pg_codegen_query_sql_create_author_result', CREATE_AUTHOR_RESULT_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_author_result') end - result = client.exec_prepared('query_sql_create_author_result', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_author_result', query_params) ExecResult.new(result.cmd_tuples, nil) end end def create_authors_batch(rows) with_client('create_authors_batch') do |client| - unless client.prepared_statements.include?('query_sql_create_authors_batch') - client.prepare('query_sql_create_authors_batch', CREATE_AUTHORS_BATCH_SQL) - client.prepared_statements.add('query_sql_create_authors_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_authors_batch') + client.prepare('pg_codegen_query_sql_create_authors_batch', CREATE_AUTHORS_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_authors_batch') end rows.each do |args| query_params = [args.name, args.bio] - client.exec_prepared('query_sql_create_authors_batch', query_params) + client.exec_prepared('pg_codegen_query_sql_create_authors_batch', query_params) end nil end @@ -1298,13 +1315,13 @@ def create_authors_batch(rows) def get_author_batch(rows) with_client('get_author_batch') do |client| - unless client.prepared_statements.include?('query_sql_get_author_batch') - client.prepare('query_sql_get_author_batch', GET_AUTHOR_BATCH_SQL) - client.prepared_statements.add('query_sql_get_author_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_author_batch') + client.prepare('pg_codegen_query_sql_get_author_batch', GET_AUTHOR_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_author_batch') end rows.map do |args| query_params = [args.id] - result = client.exec_prepared('query_sql_get_author_batch', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_author_batch', query_params) row = result.first row && GetAuthorBatchRow.new(row['id'], row['name'], row['bio']) end @@ -1313,13 +1330,13 @@ def get_author_batch(rows) def list_authors_by_name_batch(rows) with_client('list_authors_by_name_batch') do |client| - unless client.prepared_statements.include?('query_sql_list_authors_by_name_batch') - client.prepare('query_sql_list_authors_by_name_batch', LIST_AUTHORS_BY_NAME_BATCH_SQL) - client.prepared_statements.add('query_sql_list_authors_by_name_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_authors_by_name_batch') + client.prepare('pg_codegen_query_sql_list_authors_by_name_batch', LIST_AUTHORS_BY_NAME_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_authors_by_name_batch') end rows.map do |args| query_params = [args.name] - result = client.exec_prepared('query_sql_list_authors_by_name_batch', query_params) + result = client.exec_prepared('pg_codegen_query_sql_list_authors_by_name_batch', query_params) result.map do |row| ListAuthorsByNameBatchRow.new(row['id'], row['name'], row['bio']) end @@ -1337,11 +1354,11 @@ def create_regression(id:, order:, payload:, doc:, bio_types:, uids:) encode_array(bio_types), encode_array(uids) ] - unless client.prepared_statements.include?('query_sql_create_regression') - client.prepare('query_sql_create_regression', CREATE_REGRESSION_SQL) - client.prepared_statements.add('query_sql_create_regression') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_regression') + client.prepare('pg_codegen_query_sql_create_regression', CREATE_REGRESSION_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_regression') end - result = client.exec_prepared('query_sql_create_regression', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_regression', query_params) row = result.first row && CreateRegressionRow.new( row['id'], @@ -1362,11 +1379,11 @@ def create_regression!(...) def create_regression_if_new(id:, order:) with_client('create_regression_if_new') do |client| query_params = [id, order] - unless client.prepared_statements.include?('query_sql_create_regression_if_new') - client.prepare('query_sql_create_regression_if_new', CREATE_REGRESSION_IF_NEW_SQL) - client.prepared_statements.add('query_sql_create_regression_if_new') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_regression_if_new') + client.prepare('pg_codegen_query_sql_create_regression_if_new', CREATE_REGRESSION_IF_NEW_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_regression_if_new') end - result = client.exec_prepared('query_sql_create_regression_if_new', query_params) + result = client.exec_prepared('pg_codegen_query_sql_create_regression_if_new', query_params) row = result.first row && row['id'] end @@ -1386,13 +1403,13 @@ def copy_regressions(rows) def create_regressions_batch(rows) with_client('create_regressions_batch') do |client| - unless client.prepared_statements.include?('query_sql_create_regressions_batch') - client.prepare('query_sql_create_regressions_batch', CREATE_REGRESSIONS_BATCH_SQL) - client.prepared_statements.add('query_sql_create_regressions_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_regressions_batch') + client.prepare('pg_codegen_query_sql_create_regressions_batch', CREATE_REGRESSIONS_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_regressions_batch') end rows.each do |args| query_params = [args.id, args.order, encode_bytea(args.payload), encode_json(args.doc)] - client.exec_prepared('query_sql_create_regressions_batch', query_params) + client.exec_prepared('pg_codegen_query_sql_create_regressions_batch', query_params) end nil end @@ -1401,11 +1418,11 @@ def create_regressions_batch(rows) def list_regressions(order_name: nil) with_client('list_regressions') do |client| query_params = [order_name] - unless client.prepared_statements.include?('query_sql_list_regressions') - client.prepare('query_sql_list_regressions', LIST_REGRESSIONS_SQL) - client.prepared_statements.add('query_sql_list_regressions') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_regressions') + client.prepare('pg_codegen_query_sql_list_regressions', LIST_REGRESSIONS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_regressions') end - result = client.exec_prepared('query_sql_list_regressions', query_params) + result = client.exec_prepared('pg_codegen_query_sql_list_regressions', query_params) result.map do |row| ListRegressionsRow.new( row['id'], @@ -1421,20 +1438,20 @@ def list_regressions(order_name: nil) def truncate_regressions with_client('truncate_regressions') do |client| - unless client.prepared_statements.include?('query_sql_truncate_regressions') - client.prepare('query_sql_truncate_regressions', TRUNCATE_REGRESSIONS_SQL) - client.prepared_statements.add('query_sql_truncate_regressions') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_regressions') + client.prepare('pg_codegen_query_sql_truncate_regressions', TRUNCATE_REGRESSIONS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_regressions') end - client.exec_prepared('query_sql_truncate_regressions') + client.exec_prepared('pg_codegen_query_sql_truncate_regressions') nil end end def create_batch_types_batch(rows) with_client('create_batch_types_batch') do |client| - unless client.prepared_statements.include?('query_sql_create_batch_types_batch') - client.prepare('query_sql_create_batch_types_batch', CREATE_BATCH_TYPES_BATCH_SQL) - client.prepared_statements.add('query_sql_create_batch_types_batch') + unless client.prepared_statements.include?('pg_codegen_query_sql_create_batch_types_batch') + client.prepare('pg_codegen_query_sql_create_batch_types_batch', CREATE_BATCH_TYPES_BATCH_SQL) + client.prepared_statements.add('pg_codegen_query_sql_create_batch_types_batch') end rows.each do |args| query_params = [ @@ -1447,7 +1464,7 @@ def create_batch_types_batch(rows) encode_array(args.amounts), encode_array(args.stamps) ] - client.exec_prepared('query_sql_create_batch_types_batch', query_params) + client.exec_prepared('pg_codegen_query_sql_create_batch_types_batch', query_params) end nil end @@ -1467,11 +1484,11 @@ def copy_batch_types(rows) def list_batch_types with_client('list_batch_types') do |client| - unless client.prepared_statements.include?('query_sql_list_batch_types') - client.prepare('query_sql_list_batch_types', LIST_BATCH_TYPES_SQL) - client.prepared_statements.add('query_sql_list_batch_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_list_batch_types') + client.prepare('pg_codegen_query_sql_list_batch_types', LIST_BATCH_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_list_batch_types') end - result = client.exec_prepared('query_sql_list_batch_types') + result = client.exec_prepared('pg_codegen_query_sql_list_batch_types') result.map do |row| ListBatchTypesRow.new( row['id'], @@ -1490,11 +1507,11 @@ def list_batch_types def count_batch_types_above(min_id:) with_client('count_batch_types_above') do |client| query_params = [min_id] - unless client.prepared_statements.include?('query_sql_count_batch_types_above') - client.prepare('query_sql_count_batch_types_above', COUNT_BATCH_TYPES_ABOVE_SQL) - client.prepared_statements.add('query_sql_count_batch_types_above') + unless client.prepared_statements.include?('pg_codegen_query_sql_count_batch_types_above') + client.prepare('pg_codegen_query_sql_count_batch_types_above', COUNT_BATCH_TYPES_ABOVE_SQL) + client.prepared_statements.add('pg_codegen_query_sql_count_batch_types_above') end - result = client.exec_prepared('query_sql_count_batch_types_above', query_params) + result = client.exec_prepared('pg_codegen_query_sql_count_batch_types_above', query_params) row = result.first row && CountBatchTypesAboveRow.new(row['count']) end @@ -1507,11 +1524,11 @@ def count_batch_types_above!(...) def truncate_batch_types with_client('truncate_batch_types') do |client| - unless client.prepared_statements.include?('query_sql_truncate_batch_types') - client.prepare('query_sql_truncate_batch_types', TRUNCATE_BATCH_TYPES_SQL) - client.prepared_statements.add('query_sql_truncate_batch_types') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_batch_types') + client.prepare('pg_codegen_query_sql_truncate_batch_types', TRUNCATE_BATCH_TYPES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_batch_types') end - client.exec_prepared('query_sql_truncate_batch_types') + client.exec_prepared('pg_codegen_query_sql_truncate_batch_types') nil end end @@ -1528,11 +1545,11 @@ def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, h camel_case, http_server ] - unless client.prepared_statements.include?('query_sql_insert_name_case') - client.prepare('query_sql_insert_name_case', INSERT_NAME_CASE_SQL) - client.prepared_statements.add('query_sql_insert_name_case') + unless client.prepared_statements.include?('pg_codegen_query_sql_insert_name_case') + client.prepare('pg_codegen_query_sql_insert_name_case', INSERT_NAME_CASE_SQL) + client.prepared_statements.add('pg_codegen_query_sql_insert_name_case') end - client.exec_prepared('query_sql_insert_name_case', query_params) + client.exec_prepared('pg_codegen_query_sql_insert_name_case', query_params) nil end end @@ -1549,11 +1566,11 @@ def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, cam http_server, camel_case ] - unless client.prepared_statements.include?('query_sql_get_name_case') - client.prepare('query_sql_get_name_case', GET_NAME_CASE_SQL) - client.prepared_statements.add('query_sql_get_name_case') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_name_case') + client.prepare('pg_codegen_query_sql_get_name_case', GET_NAME_CASE_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_name_case') end - result = client.exec_prepared('query_sql_get_name_case', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_name_case', query_params) row = result.first row && GetNameCaseRow.new( row['id'], @@ -1576,11 +1593,11 @@ def get_name_case!(...) def get_http_server(id:) with_client('get_http_server') do |client| query_params = [id] - unless client.prepared_statements.include?('query_sql_get_http_server') - client.prepare('query_sql_get_http_server', GET_HTTP_SERVER_SQL) - client.prepared_statements.add('query_sql_get_http_server') + unless client.prepared_statements.include?('pg_codegen_query_sql_get_http_server') + client.prepare('pg_codegen_query_sql_get_http_server', GET_HTTP_SERVER_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_http_server') end - result = client.exec_prepared('query_sql_get_http_server', query_params) + result = client.exec_prepared('pg_codegen_query_sql_get_http_server', query_params) row = result.first row && GetHTTPServerRow.new( row['id'], @@ -1602,15 +1619,49 @@ def get_http_server!(...) def truncate_name_cases with_client('truncate_name_cases') do |client| - unless client.prepared_statements.include?('query_sql_truncate_name_cases') - client.prepare('query_sql_truncate_name_cases', TRUNCATE_NAME_CASES_SQL) - client.prepared_statements.add('query_sql_truncate_name_cases') + unless client.prepared_statements.include?('pg_codegen_query_sql_truncate_name_cases') + client.prepare('pg_codegen_query_sql_truncate_name_cases', TRUNCATE_NAME_CASES_SQL) + client.prepared_statements.add('pg_codegen_query_sql_truncate_name_cases') end - client.exec_prepared('query_sql_truncate_name_cases') + client.exec_prepared('pg_codegen_query_sql_truncate_name_cases') nil end end + def get_unnamed_columns + with_client('get_unnamed_columns') do |client| + unless client.prepared_statements.include?('pg_codegen_query_sql_get_unnamed_columns') + client.prepare('pg_codegen_query_sql_get_unnamed_columns', GET_UNNAMED_COLUMNS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_unnamed_columns') + end + result = client.exec_prepared('pg_codegen_query_sql_get_unnamed_columns').values + row = result.first + row && GetUnnamedColumnsRow.new(row[0], row[1]) + end + end + + # Like #get_unnamed_columns, but raises NotFound when there is no row. + def get_unnamed_columns!(...) + get_unnamed_columns(...) || raise(NotFound, 'GetUnnamedColumns returned no row') + end + + def get_quoted_alias + with_client('get_quoted_alias') do |client| + unless client.prepared_statements.include?('pg_codegen_query_sql_get_quoted_alias') + client.prepare('pg_codegen_query_sql_get_quoted_alias', GET_QUOTED_ALIAS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_get_quoted_alias') + end + result = client.exec_prepared('pg_codegen_query_sql_get_quoted_alias') + row = result.first + row && GetQuotedAliasRow.new(row['it\'s']) + end + end + + # Like #get_quoted_alias, but raises NotFound when there is no row. + def get_quoted_alias!(...) + get_quoted_alias(...) || raise(NotFound, 'GetQuotedAlias returned no row') + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ @@ -1620,11 +1671,11 @@ def many_params(c0:, c1:, c2:, c3:, c4:) encode_float(c3), encode_bytea(c4) ] - unless client.prepared_statements.include?('query_sql_many_params') - client.prepare('query_sql_many_params', MANY_PARAMS_SQL) - client.prepared_statements.add('query_sql_many_params') + unless client.prepared_statements.include?('pg_codegen_query_sql_many_params') + client.prepare('pg_codegen_query_sql_many_params', MANY_PARAMS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_many_params') end - result = client.exec_prepared('query_sql_many_params', query_params) + result = client.exec_prepared('pg_codegen_query_sql_many_params', query_params) row = result.first row && ManyParamsRow.new(row['c0'], row['c1']) end @@ -1638,11 +1689,11 @@ def many_params!(...) def many_rows(c0:) with_client('many_rows') do |client| query_params = [c0] - unless client.prepared_statements.include?('query_sql_many_rows') - client.prepare('query_sql_many_rows', MANY_ROWS_SQL) - client.prepared_statements.add('query_sql_many_rows') + unless client.prepared_statements.include?('pg_codegen_query_sql_many_rows') + client.prepare('pg_codegen_query_sql_many_rows', MANY_ROWS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_many_rows') end - result = client.exec_prepared('query_sql_many_rows', query_params) + result = client.exec_prepared('pg_codegen_query_sql_many_rows', query_params) result.map do |row| ManyRowsRow.new( row['c0'], @@ -1657,11 +1708,11 @@ def many_rows(c0:) def four_cols with_client('four_cols') do |client| - unless client.prepared_statements.include?('query_sql_four_cols') - client.prepare('query_sql_four_cols', FOUR_COLS_SQL) - client.prepared_statements.add('query_sql_four_cols') + unless client.prepared_statements.include?('pg_codegen_query_sql_four_cols') + client.prepare('pg_codegen_query_sql_four_cols', FOUR_COLS_SQL) + client.prepared_statements.add('pg_codegen_query_sql_four_cols') end - result = client.exec_prepared('query_sql_four_cols') + result = client.exec_prepared('pg_codegen_query_sql_four_cols') result.map do |row| FourColsRow.new(row['c0'], row['c1'], row['c2'], row['c3']) end @@ -1687,7 +1738,13 @@ def with_client(name) @db.with do |client| client.reset if client.status == PG::CONNECTION_BAD # closed by the server (restart, failover, killed) configure(client) unless client.is_a?(ConnectionState) - yield client + begin + yield client + rescue Exception # rubocop:disable Lint/RescueException + # a query left by Timeout / Thread#raise keeps running: cancel it before the connection is reused + client.cancel if client.transaction_status == PG::PQTRANS_ACTIVE + raise + end end end end @@ -1726,11 +1783,12 @@ def encode_bytea(value) value && PG::TextEncoder::Bytea.new.encode(value) end - # json / jsonb parameters: booleans and Arrays need JSON encoding, not PostgreSQL's boolean / array encoding. - # Hashes and JSON text pass through; nil stays nil. + # json / jsonb parameters: booleans and Arrays need JSON encoding, not PostgreSQL's boolean / array encoding, + # and Floats their shortest exact text. Hashes and JSON text pass through; nil stays nil. def encode_json(value) case value when Array, TrueClass, FalseClass then PG::TextEncoder::JSON.new.encode(value) + when Float then value.to_s else value end end diff --git a/test/golden/sqlite3/default/my_queries_sql.rb b/test/golden/sqlite3/default/my_queries_sql.rb index 5298177..a78a324 100644 --- a/test/golden/sqlite3/default/my_queries_sql.rb +++ b/test/golden/sqlite3/default/my_queries_sql.rb @@ -45,7 +45,7 @@ def transaction(mode: nil) yield self rescue Exception # rubocop:disable Lint/RescueException failed = true - @db.execute(rollback) + @db.execute(rollback) if @db.transaction_active? # ON CONFLICT ROLLBACK already ended it raise ensure unless failed diff --git a/test/golden/sqlite3/default/other_sql.rb b/test/golden/sqlite3/default/other_sql.rb index 5d31abc..855c566 100644 --- a/test/golden/sqlite3/default/other_sql.rb +++ b/test/golden/sqlite3/default/other_sql.rb @@ -58,7 +58,7 @@ def transaction(mode: nil) yield self rescue Exception # rubocop:disable Lint/RescueException failed = true - @db.execute(rollback) + @db.execute(rollback) if @db.transaction_active? # ON CONFLICT ROLLBACK already ended it raise ensure unless failed diff --git a/test/golden/sqlite3/default/query_sql.rb b/test/golden/sqlite3/default/query_sql.rb index e55cd17..24c2bff 100644 --- a/test/golden/sqlite3/default/query_sql.rb +++ b/test/golden/sqlite3/default/query_sql.rb @@ -373,6 +373,33 @@ class GetHTTPServerRow < Data.define( DELETE FROM name_cases SQL + GET_UNNAMED_COLUMNS_SQL = <<~'SQL' + SELECT 1, 'two' + SQL + + # rubocop:disable Naming/VariableNumber + class GetUnnamedColumnsRow < Data.define(:column_1, :column_2) + end + # rubocop:enable Naming/VariableNumber + + GET_QUOTED_ALIAS_SQL = <<~'SQL' + SELECT 1 AS "it's" + SQL + + class GetQuotedAliasRow < Data.define(:it_s) + end + + GET_AUTHORS_NOT_IN_IDS_SQL = <<~'SQL' + SELECT id, name, bio FROM authors WHERE id NOT IN (/*SLICE:ids*/?) ORDER BY id + SQL + + class GetAuthorsNotInIdsRow < Data.define(:id, :name, :bio) + end + + CREATE_AUTHOR_OR_ROLLBACK_SQL = <<~'SQL' + INSERT OR ROLLBACK INTO authors (id, name) VALUES (?, ?) + SQL + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -435,7 +462,7 @@ def transaction(mode: nil) yield self rescue Exception # rubocop:disable Lint/RescueException failed = true - @db.execute(rollback) + @db.execute(rollback) if @db.transaction_active? # ON CONFLICT ROLLBACK already ended it raise ensure unless failed @@ -1202,6 +1229,71 @@ def truncate_name_cases end end + def get_unnamed_columns + with_client('get_unnamed_columns') do |client| + stmt = client.prepare(GET_UNNAMED_COLUMNS_SQL) + begin + result = stmt.execute + row = result.first + row && GetUnnamedColumnsRow.new(row[0], row[1]) + ensure + stmt.close + end + end + end + + # Like #get_unnamed_columns, but raises NotFound when there is no row. + def get_unnamed_columns!(...) + get_unnamed_columns(...) || raise(NotFound, 'GetUnnamedColumns returned no row') + end + + def get_quoted_alias + with_client('get_quoted_alias') do |client| + stmt = client.prepare(GET_QUOTED_ALIAS_SQL) + begin + result = stmt.execute + row = result.first + row && GetQuotedAliasRow.new(row[0]) + ensure + stmt.close + end + end + end + + # Like #get_quoted_alias, but raises NotFound when there is no row. + def get_quoted_alias!(...) + get_quoted_alias(...) || raise(NotFound, 'GetQuotedAlias returned no row') + end + + def get_authors_not_in_ids(ids:) + with_client('get_authors_not_in_ids') do |client| + query_params = [*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.map do |row| + GetAuthorsNotInIdsRow.new(row[0], row[1], row[2]) + end + ensure + stmt.close + end + end + end + + def create_author_or_rollback(id:, name:) + with_client('create_author_or_rollback') do |client| + query_params = [id, name] + stmt = client.prepare(CREATE_AUTHOR_OR_ROLLBACK_SQL) + begin + stmt.execute(*query_params) + nil + ensure + stmt.close + end + end + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ @@ -1293,7 +1385,7 @@ def atomically(client) end def slice_placeholders(values) - values.empty? ? 'NULL' : Array.new(values.size, '?').join(', ') + values.empty? ? 'SELECT NULL WHERE FALSE' : Array.new(values.size, '?').join(', ') end end end diff --git a/test/golden/sqlite3/default/query_sql.rbs b/test/golden/sqlite3/default/query_sql.rbs index 0da7229..3b0db6c 100644 --- a/test/golden/sqlite3/default/query_sql.rbs +++ b/test/golden/sqlite3/default/query_sql.rbs @@ -371,6 +371,32 @@ module Sqlite3Codegen TRUNCATE_NAME_CASES_SQL: String + GET_UNNAMED_COLUMNS_SQL: String + + class GetUnnamedColumnsRow + attr_reader column_1: Integer + attr_reader column_2: String + def initialize: ( Integer column_1, String column_2) -> void + end + + GET_QUOTED_ALIAS_SQL: String + + class GetQuotedAliasRow + attr_reader it_s: Integer + def initialize: ( Integer it_s) -> void + end + + GET_AUTHORS_NOT_IN_IDS_SQL: String + + class GetAuthorsNotInIdsRow + attr_reader id: Integer + attr_reader name: String + attr_reader bio: String? + def initialize: ( Integer id, String name, String? bio) -> void + end + + CREATE_AUTHOR_OR_ROLLBACK_SQL: String + MANY_PARAMS_SQL: String class ManyParamsRow @@ -526,6 +552,18 @@ module Sqlite3Codegen def truncate_name_cases: -> void + def get_unnamed_columns: -> GetUnnamedColumnsRow? + + def get_unnamed_columns!: -> GetUnnamedColumnsRow + + def get_quoted_alias: -> GetQuotedAliasRow? + + def get_quoted_alias!: -> GetQuotedAliasRow + + def get_authors_not_in_ids: (ids: Array[Integer]) -> Array[GetAuthorsNotInIdsRow] + + def create_author_or_rollback: (id: Integer, name: String) -> void + def many_params: (c0: Integer, c1: String, c2: Float, c3: String, c4: Integer) -> ManyParamsRow? def many_params!: (c0: Integer, c1: String, c2: Float, c3: String, c4: Integer) -> ManyParamsRow diff --git a/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rb b/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rb index 7b7f5eb..4af03a4 100644 --- a/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rb +++ b/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rb @@ -48,7 +48,7 @@ def transaction(mode: nil) result rescue Exception # rubocop:disable Lint/RescueException failed = true - @db.execute(rollback) + @db.execute(rollback) if @db.transaction_active? # ON CONFLICT ROLLBACK already ended it raise ensure unless failed diff --git a/test/golden/sqlite3/ruby31_gemfile/other_sql.rb b/test/golden/sqlite3/ruby31_gemfile/other_sql.rb index 46c7b7c..866aa54 100644 --- a/test/golden/sqlite3/ruby31_gemfile/other_sql.rb +++ b/test/golden/sqlite3/ruby31_gemfile/other_sql.rb @@ -60,7 +60,7 @@ def transaction(mode: nil) result rescue Exception # rubocop:disable Lint/RescueException failed = true - @db.execute(rollback) + @db.execute(rollback) if @db.transaction_active? # ON CONFLICT ROLLBACK already ended it raise ensure unless failed diff --git a/test/golden/sqlite3/ruby31_gemfile/query_sql.rb b/test/golden/sqlite3/ruby31_gemfile/query_sql.rb index 7a8a70e..ab4a48b 100644 --- a/test/golden/sqlite3/ruby31_gemfile/query_sql.rb +++ b/test/golden/sqlite3/ruby31_gemfile/query_sql.rb @@ -355,6 +355,30 @@ module Sqlite3Codegen DELETE FROM name_cases SQL + GET_UNNAMED_COLUMNS_SQL = <<~'SQL' + SELECT 1, 'two' + SQL + + # rubocop:disable Naming/VariableNumber + GetUnnamedColumnsRow = Struct.new(:column_1, :column_2, keyword_init: true) + # rubocop:enable Naming/VariableNumber + + GET_QUOTED_ALIAS_SQL = <<~'SQL' + SELECT 1 AS "it's" + SQL + + GetQuotedAliasRow = Struct.new(:it_s, keyword_init: true) + + GET_AUTHORS_NOT_IN_IDS_SQL = <<~'SQL' + SELECT id, name, bio FROM authors WHERE id NOT IN (/*SLICE:ids*/?) ORDER BY id + SQL + + GetAuthorsNotInIdsRow = Struct.new(:id, :name, :bio, keyword_init: true) + + CREATE_AUTHOR_OR_ROLLBACK_SQL = <<~'SQL' + INSERT OR ROLLBACK INTO authors (id, name) VALUES (?, ?) + SQL + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -424,7 +448,7 @@ def transaction(mode: nil) result rescue Exception # rubocop:disable Lint/RescueException failed = true - @db.execute(rollback) + @db.execute(rollback) if @db.transaction_active? # ON CONFLICT ROLLBACK already ended it raise ensure unless failed @@ -1198,6 +1222,71 @@ def truncate_name_cases end end + def get_unnamed_columns + with_client('get_unnamed_columns') do |client| + stmt = client.prepare(GET_UNNAMED_COLUMNS_SQL) + begin + result = stmt.execute + row = result.first + row && GetUnnamedColumnsRow.new(column_1: row[0], column_2: row[1]) + ensure + stmt.close + end + end + end + + # Like #get_unnamed_columns, but raises NotFound when there is no row. + def get_unnamed_columns!(...) + get_unnamed_columns(...) || raise(NotFound, 'GetUnnamedColumns returned no row') + end + + def get_quoted_alias + with_client('get_quoted_alias') do |client| + stmt = client.prepare(GET_QUOTED_ALIAS_SQL) + begin + result = stmt.execute + row = result.first + row && GetQuotedAliasRow.new(it_s: row[0]) + ensure + stmt.close + end + end + end + + # Like #get_quoted_alias, but raises NotFound when there is no row. + def get_quoted_alias!(...) + get_quoted_alias(...) || raise(NotFound, 'GetQuotedAlias returned no row') + end + + def get_authors_not_in_ids(ids:) + with_client('get_authors_not_in_ids') do |client| + query_params = [*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.map do |row| + GetAuthorsNotInIdsRow.new(id: row[0], name: row[1], bio: row[2]) + end + ensure + stmt.close + end + end + end + + def create_author_or_rollback(id:, name:) + with_client('create_author_or_rollback') do |client| + query_params = [id, name] + stmt = client.prepare(CREATE_AUTHOR_OR_ROLLBACK_SQL) + begin + stmt.execute(*query_params) + nil + ensure + stmt.close + end + end + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ @@ -1294,7 +1383,7 @@ def atomically(client) end def slice_placeholders(values) - values.empty? ? 'NULL' : Array.new(values.size, '?').join(', ') + values.empty? ? 'SELECT NULL WHERE FALSE' : Array.new(values.size, '?').join(', ') end end end diff --git a/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs b/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs index e43ff5a..d2acb7f 100644 --- a/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs +++ b/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs @@ -436,6 +436,35 @@ module Sqlite3Codegen TRUNCATE_NAME_CASES_SQL: String + GET_UNNAMED_COLUMNS_SQL: String + + class GetUnnamedColumnsRow < ::Struct[untyped] + attr_accessor column_1: Integer + attr_accessor column_2: String + def initialize: ( column_1: Integer, column_2: String) -> void + def self.new: ( column_1: Integer, column_2: String) -> GetUnnamedColumnsRow + end + + GET_QUOTED_ALIAS_SQL: String + + class GetQuotedAliasRow < ::Struct[untyped] + attr_accessor it_s: Integer + def initialize: ( it_s: Integer) -> void + def self.new: ( it_s: Integer) -> GetQuotedAliasRow + end + + GET_AUTHORS_NOT_IN_IDS_SQL: String + + class GetAuthorsNotInIdsRow < ::Struct[untyped] + attr_accessor id: Integer + attr_accessor name: String + attr_accessor bio: String? + def initialize: ( id: Integer, name: String, bio: String?) -> void + def self.new: ( id: Integer, name: String, bio: String?) -> GetAuthorsNotInIdsRow + end + + CREATE_AUTHOR_OR_ROLLBACK_SQL: String + MANY_PARAMS_SQL: String class ManyParamsRow < ::Struct[untyped] @@ -601,6 +630,18 @@ module Sqlite3Codegen def truncate_name_cases: -> void + def get_unnamed_columns: -> GetUnnamedColumnsRow? + + def get_unnamed_columns!: -> GetUnnamedColumnsRow + + def get_quoted_alias: -> GetQuotedAliasRow? + + def get_quoted_alias!: -> GetQuotedAliasRow + + def get_authors_not_in_ids: (ids: Array[Integer]) -> Array[GetAuthorsNotInIdsRow] + + def create_author_or_rollback: (id: Integer, name: String) -> void + def many_params: (c0: Integer, c1: String, c2: Float, c3: String, c4: Integer) -> ManyParamsRow? def many_params!: (c0: Integer, c1: String, c2: Float, c3: String, c4: Integer) -> ManyParamsRow diff --git a/test/golden/sqlite3/ruby32_notypes/my_queries_sql.rb b/test/golden/sqlite3/ruby32_notypes/my_queries_sql.rb index 7b7f5eb..4af03a4 100644 --- a/test/golden/sqlite3/ruby32_notypes/my_queries_sql.rb +++ b/test/golden/sqlite3/ruby32_notypes/my_queries_sql.rb @@ -48,7 +48,7 @@ def transaction(mode: nil) result rescue Exception # rubocop:disable Lint/RescueException failed = true - @db.execute(rollback) + @db.execute(rollback) if @db.transaction_active? # ON CONFLICT ROLLBACK already ended it raise ensure unless failed diff --git a/test/golden/sqlite3/ruby32_notypes/other_sql.rb b/test/golden/sqlite3/ruby32_notypes/other_sql.rb index bed518c..3aa39ae 100644 --- a/test/golden/sqlite3/ruby32_notypes/other_sql.rb +++ b/test/golden/sqlite3/ruby32_notypes/other_sql.rb @@ -61,7 +61,7 @@ def transaction(mode: nil) result rescue Exception # rubocop:disable Lint/RescueException failed = true - @db.execute(rollback) + @db.execute(rollback) if @db.transaction_active? # ON CONFLICT ROLLBACK already ended it raise ensure unless failed diff --git a/test/golden/sqlite3/ruby32_notypes/query_sql.rb b/test/golden/sqlite3/ruby32_notypes/query_sql.rb index 7ca312c..92d6fb6 100644 --- a/test/golden/sqlite3/ruby32_notypes/query_sql.rb +++ b/test/golden/sqlite3/ruby32_notypes/query_sql.rb @@ -373,6 +373,33 @@ class GetHTTPServerRow < Data.define( DELETE FROM name_cases SQL + GET_UNNAMED_COLUMNS_SQL = <<~'SQL' + SELECT 1, 'two' + SQL + + # rubocop:disable Naming/VariableNumber + class GetUnnamedColumnsRow < Data.define(:column_1, :column_2) + end + # rubocop:enable Naming/VariableNumber + + GET_QUOTED_ALIAS_SQL = <<~'SQL' + SELECT 1 AS "it's" + SQL + + class GetQuotedAliasRow < Data.define(:it_s) + end + + GET_AUTHORS_NOT_IN_IDS_SQL = <<~'SQL' + SELECT id, name, bio FROM authors WHERE id NOT IN (/*SLICE:ids*/?) ORDER BY id + SQL + + class GetAuthorsNotInIdsRow < Data.define(:id, :name, :bio) + end + + CREATE_AUTHOR_OR_ROLLBACK_SQL = <<~'SQL' + INSERT OR ROLLBACK INTO authors (id, name) VALUES (?, ?) + SQL + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -438,7 +465,7 @@ def transaction(mode: nil) result rescue Exception # rubocop:disable Lint/RescueException failed = true - @db.execute(rollback) + @db.execute(rollback) if @db.transaction_active? # ON CONFLICT ROLLBACK already ended it raise ensure unless failed @@ -1205,6 +1232,71 @@ def truncate_name_cases end end + def get_unnamed_columns + with_client('get_unnamed_columns') do |client| + stmt = client.prepare(GET_UNNAMED_COLUMNS_SQL) + begin + result = stmt.execute + row = result.first + row && GetUnnamedColumnsRow.new(row[0], row[1]) + ensure + stmt.close + end + end + end + + # Like #get_unnamed_columns, but raises NotFound when there is no row. + def get_unnamed_columns!(...) + get_unnamed_columns(...) || raise(NotFound, 'GetUnnamedColumns returned no row') + end + + def get_quoted_alias + with_client('get_quoted_alias') do |client| + stmt = client.prepare(GET_QUOTED_ALIAS_SQL) + begin + result = stmt.execute + row = result.first + row && GetQuotedAliasRow.new(row[0]) + ensure + stmt.close + end + end + end + + # Like #get_quoted_alias, but raises NotFound when there is no row. + def get_quoted_alias!(...) + get_quoted_alias(...) || raise(NotFound, 'GetQuotedAlias returned no row') + end + + def get_authors_not_in_ids(ids:) + with_client('get_authors_not_in_ids') do |client| + query_params = [*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.map do |row| + GetAuthorsNotInIdsRow.new(row[0], row[1], row[2]) + end + ensure + stmt.close + end + end + end + + def create_author_or_rollback(id:, name:) + with_client('create_author_or_rollback') do |client| + query_params = [id, name] + stmt = client.prepare(CREATE_AUTHOR_OR_ROLLBACK_SQL) + begin + stmt.execute(*query_params) + nil + ensure + stmt.close + end + end + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ @@ -1301,7 +1393,7 @@ def atomically(client) end def slice_placeholders(values) - values.empty? ? 'NULL' : Array.new(values.size, '?').join(', ') + values.empty? ? 'SELECT NULL WHERE FALSE' : Array.new(values.size, '?').join(', ') end end end diff --git a/tests/end2end_mysql2.rb b/tests/end2end_mysql2.rb index db30b1b..a149854 100755 --- a/tests/end2end_mysql2.rb +++ b/tests/end2end_mysql2.rb @@ -568,7 +568,7 @@ def test_time_values_match_mysql2_prepared_statement_storage def test_injection_and_unsupported_parameters_are_safe create_author('ordinary') assert_empty @query_sql.find_authors_by_exact_name(name: "x' OR '1'='1") - [Hash.new, :symbol, Object.new].each do |value| + [Hash.new, Object.new].each do |value| assert_raises(TypeError) { @query_sql.find_authors_by_exact_name(name: value) } end assert_equal ['ordinary'], @query_sql.list_authors.map(&:name) @@ -896,4 +896,47 @@ def test_seeded_concurrent_transactions_commit_the_exact_planned_set threads.each(&:value) assert_equal expected, @query_sql.list_authors.map(&:name).sort end + # ---- result column names ------------------------------------------------------------------------------------- + + def test_unnamed_result_columns_are_read_by_position + row = @query_sql.get_unnamed_columns + assert_equal({ column_1: 1, column_2: 'two' }, row.to_h) + end + + def test_alias_with_a_quote_is_read + assert_equal({ it_s: 1 }, @query_sql.get_quoted_alias.to_h) + end + + def test_not_in_an_empty_slice_matches_everything + ids = %w[a b].map { |name| create_author(name) } + assert_equal ids.sort, @query_sql.get_authors_not_in_ids(ids: []).map(&:id) + assert_equal [ids.last], @query_sql.get_authors_not_in_ids(ids: [ids.first]).map(&:id) + end + + def test_symbol_enum_value_is_bound_as_its_name + @query_sql.create_extended_bio(author_name: 'a', name: 'n', bio_type: :Memoir, author_type: nil) + assert_equal 'Memoir', @query_sql.get_first_extended_bio_by_type(bio_type: :Memoir).bio_type + end + + def test_deadlock_in_a_nested_transaction_raises_the_deadlock_error + ids = %w[a b].map { |name| create_author(name) } + queries = QuerySql.connect(mysql2_params, size: 2) + locked = Queue.new + threads = [ids, ids.reverse].map do |first, second| + Thread.new do + queries.transaction do |tx| + tx.delete_author(id: first) + locked << first + sleep 0.01 until locked.size == 2 + tx.transaction { |inner| inner.delete_author(id: second) } + end + nil + rescue Mysql2::Error => e + e + end + end + errors = threads.map(&:value).compact + assert_equal [1213], errors.map(&:error_number), errors.map(&:message).inspect + assert_empty @query_sql.list_authors + end end diff --git a/tests/end2end_options.rb b/tests/end2end_options.rb index 5615c39..96ace00 100644 --- a/tests/end2end_options.rb +++ b/tests/end2end_options.rb @@ -106,6 +106,14 @@ def assert_pg_runtime(generated_dir) pool = ConnectionPool.new(size: 1) { PG.connect(**params) } row = Options::PgDb::FirstSql.new(pool).get_pg_option_author(id: 1) assert_equal [1, 'note', 'tag', 'extra'], [row.id, row.memo, row.tag, row.extra] + require File.join(OUTPUT_DIR, 'pg_first/first_sql.rb') + require File.join(OUTPUT_DIR, 'pg_second/first_sql.rb') + first = Options::PgFirst::FirstSql.new(pool) + second = Options::PgSecond::FirstSql.new(pool) + 2.times do + assert_equal 1, first.get_pg_option_author(id: 1).id + assert_equal 'second module', second.get_pg_option_author(id: 1).label + end ensure pool&.shutdown(&:close) conn&.exec('DROP SCHEMA IF EXISTS options_audit CASCADE') diff --git a/tests/end2end_pg.rb b/tests/end2end_pg.rb index 1eb0451..dabf4ab 100755 --- a/tests/end2end_pg.rb +++ b/tests/end2end_pg.rb @@ -997,4 +997,38 @@ def test_seeded_concurrent_transactions_commit_the_exact_planned_set threads.each(&:value) assert_equal expected, @query_sql.list_authors.map(&:name).sort end + # ---- result column names ------------------------------------------------------------------------------------- + + def test_unnamed_result_columns_are_read_by_position + row = @query_sql.get_unnamed_columns + assert_equal({ column_1: 1, column_2: 'two' }, row.to_h) + end + + def test_alias_with_a_quote_is_read + assert_equal({ it_s: 1 }, @query_sql.get_quoted_alias.to_h) + end + + def test_top_level_float_json_parameters_round_trip_exactly + [0.3304, 0.1 + 0.2, 1.0e-7, -2.5].each_with_index do |value, id| + @query_sql.set_json_round_trip(id: id, json_value: value, jsonb_value: value) + row = @query_sql.get_json_round_trip(id: id) + assert_equal [value, value], [row.json_value, row.jsonb_value] + end + end + + # ---- queries abandoned by a timeout ---------------------------------------------------------------------------- + + def test_query_abandoned_by_a_timeout_is_cancelled + queries = QuerySql.connect(pg_params, size: 1) + [false, true].each do |in_transaction| + assert_raises(Timeout::Error) do + Timeout.timeout(0.1) do + in_transaction ? queries.transaction { |tx| tx.pause_query(seconds: 3) } : queries.pause_query(seconds: 3) + end + end + started = Process.clock_gettime(Process::CLOCK_MONOTONIC) + queries.list_authors + assert_operator Process.clock_gettime(Process::CLOCK_MONOTONIC) - started, :<, 1 + end + end end diff --git a/tests/end2end_sqlite3.rb b/tests/end2end_sqlite3.rb index b46a313..108bef7 100644 --- a/tests/end2end_sqlite3.rb +++ b/tests/end2end_sqlite3.rb @@ -697,4 +697,35 @@ def test_seeded_concurrent_transactions_commit_the_exact_planned_set threads.each(&:value) assert_equal expected, @query_sql.list_authors.map(&:name).sort end + # ---- result column names ------------------------------------------------------------------------------------- + + def test_unnamed_result_columns_are_read_by_position + row = @query_sql.get_unnamed_columns + assert_equal({ column_1: 1, column_2: 'two' }, row.to_h) + end + + def test_alias_with_a_quote_is_read + assert_equal({ it_s: 1 }, @query_sql.get_quoted_alias.to_h) + end + + def test_not_in_an_empty_slice_matches_everything + ids = %w[a b].map { |name| create_author(name) } + assert_equal ids.sort, @query_sql.get_authors_not_in_ids(ids: []).map(&:id) + assert_equal [ids.last], @query_sql.get_authors_not_in_ids(ids: [ids.first]).map(&:id) + end + + def test_conflict_that_ends_the_transaction_raises_the_constraint_error + id = create_author('a') + assert_raises(SQLite3::ConstraintException) do + @query_sql.transaction { |tx| tx.create_author_or_rollback(id: id, name: 'dup') } + end + assert_raises(SQLite3::ConstraintException) do + @query_sql.transaction do |tx| + tx.create_author_return_id(name: 'outer', bio: nil) + tx.transaction { |inner| inner.create_author_or_rollback(id: id, name: 'dup') } + end + end + refute_predicate @db, :transaction_active? + assert_equal ['a'], @query_sql.list_authors.map(&:name) + end end diff --git a/tests/options/pg_second_queries/first.sql b/tests/options/pg_second_queries/first.sql new file mode 100644 index 0000000..dc506ed --- /dev/null +++ b/tests/options/pg_second_queries/first.sql @@ -0,0 +1,5 @@ +-- Same file and query name as pg_queries/first.sql, different SQL: prepared statements must not be shared. +-- name: GetPgOptionAuthor :one +SELECT id, 'second module' AS label +FROM options_audit.option_items +WHERE id = sqlc.arg(id); diff --git a/tests/options/sqlc.yaml b/tests/options/sqlc.yaml index 017aea5..ee00194 100644 --- a/tests/options/sqlc.yaml +++ b/tests/options/sqlc.yaml @@ -70,3 +70,25 @@ sql: generateTypes: false rename: "options_audit.option_items": PgItems +- schema: "pg_schema.sql" + queries: + - "pg_queries/first.sql" + engine: "postgresql" + codegen: + - plugin: ruby + out: ".generated/pg_first" + options: + driver: pg + moduleName: Options::PgFirst + generateTypes: false +- schema: "pg_schema.sql" + queries: + - "pg_second_queries/first.sql" + engine: "postgresql" + codegen: + - plugin: ruby + out: ".generated/pg_second" + options: + driver: pg + moduleName: Options::PgSecond + generateTypes: false