From 261163c488d600e5f517a8b4275f29047a0c4b73 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 19:00:45 +0000 Subject: [PATCH 1/9] Fix Ruby generator regressions Co-Authored-By: Doron Eli Rachman --- .github/workflows/tests.yml | 56 +++- .gitignore | 1 + Gemfile.e2e | 14 + README.md | 8 +- examples/authors/mysql/query.sql | 12 + examples/authors/postgresql/query.sql | 29 ++ examples/authors/postgresql/schema.sql | 11 + examples/authors/sqlite/query.sql | 28 ++ examples/authors/sqlite/schema.sql | 3 +- examples/mysql2/query_sql.rb | 66 ++++ examples/mysql2/query_sql.rbs | 32 ++ examples/pg/models.rb | 6 + examples/pg/models.rbs | 13 + examples/pg/query_sql.rb | 156 ++++++++- examples/pg/query_sql.rbs | 52 +++ examples/sqlite3/models.rb | 2 +- examples/sqlite3/models.rbs | 3 +- examples/sqlite3/query_sql.rb | 136 ++++++++ examples/sqlite3/query_sql.rbs | 57 ++++ lib/sqlc_gen_ruby/code.rb | 1 + lib/sqlc_gen_ruby/drivers/base.rb | 42 ++- lib/sqlc_gen_ruby/drivers/mysql2.rb | 17 +- lib/sqlc_gen_ruby/drivers/pg.rb | 22 +- lib/sqlc_gen_ruby/drivers/sqlite3.rb | 93 +++++- lib/sqlc_gen_ruby/generator.rb | 10 +- lib/sqlc_gen_ruby/method_gen.rb | 14 +- scripts/capture_request.rb | 2 +- scripts/tests/capture_fixtures.rb | 39 +++ scripts/tests/generate_examples.rb | 35 ++ steep_expectations.yml | 128 ++++---- test/features_test.rb | 73 +++++ test/fixtures/mysql2.bin | 21 +- test/fixtures/pg.bin | 56 +++- test/fixtures/sqlite3.bin | Bin 10069 -> 11926 bytes test/golden/mysql2/default/query_sql.rb | 66 ++++ test/golden/mysql2/default/query_sql.rbs | 32 ++ test/golden/mysql2/ruby31_gemfile/Gemfile | 1 + .../mysql2/ruby31_gemfile/my_queries_sql.rb | 12 +- .../mysql2/ruby31_gemfile/my_queries_sql.rbs | 2 + .../golden/mysql2/ruby31_gemfile/other_sql.rb | 12 +- .../mysql2/ruby31_gemfile/other_sql.rbs | 2 + .../golden/mysql2/ruby31_gemfile/query_sql.rb | 81 ++++- .../mysql2/ruby31_gemfile/query_sql.rbs | 37 +++ .../mysql2/ruby32_notypes/my_queries_sql.rb | 12 +- .../golden/mysql2/ruby32_notypes/other_sql.rb | 12 +- .../golden/mysql2/ruby32_notypes/query_sql.rb | 78 ++++- test/golden/pg/default/models.rb | 6 + test/golden/pg/default/models.rbs | 13 + test/golden/pg/default/query_sql.rb | 156 ++++++++- test/golden/pg/default/query_sql.rbs | 52 +++ test/golden/pg/ruby31_gemfile/Gemfile | 1 + test/golden/pg/ruby31_gemfile/models.rb | 4 + test/golden/pg/ruby31_gemfile/models.rbs | 15 + .../pg/ruby31_gemfile/my_queries_sql.rb | 12 +- .../pg/ruby31_gemfile/my_queries_sql.rbs | 2 + test/golden/pg/ruby31_gemfile/other_sql.rb | 12 +- test/golden/pg/ruby31_gemfile/other_sql.rbs | 2 + test/golden/pg/ruby31_gemfile/query_sql.rb | 165 +++++++++- test/golden/pg/ruby31_gemfile/query_sql.rbs | 57 ++++ test/golden/pg/ruby32_notypes/models.rb | 6 + .../pg/ruby32_notypes/my_queries_sql.rb | 12 +- test/golden/pg/ruby32_notypes/other_sql.rb | 12 +- test/golden/pg/ruby32_notypes/query_sql.rb | 168 +++++++++- test/golden/sqlite3/default/models.rb | 2 +- test/golden/sqlite3/default/models.rbs | 3 +- test/golden/sqlite3/default/query_sql.rb | 136 ++++++++ test/golden/sqlite3/default/query_sql.rbs | 57 ++++ test/golden/sqlite3/ruby31_gemfile/Gemfile | 1 + test/golden/sqlite3/ruby31_gemfile/models.rb | 8 +- test/golden/sqlite3/ruby31_gemfile/models.rbs | 5 +- .../sqlite3/ruby31_gemfile/my_queries_sql.rb | 12 +- .../sqlite3/ruby31_gemfile/my_queries_sql.rbs | 2 + .../sqlite3/ruby31_gemfile/other_sql.rb | 12 +- .../sqlite3/ruby31_gemfile/other_sql.rbs | 2 + .../sqlite3/ruby31_gemfile/query_sql.rb | 155 ++++++++- .../sqlite3/ruby31_gemfile/query_sql.rbs | 64 ++++ test/golden/sqlite3/ruby32_notypes/models.rb | 2 +- .../sqlite3/ruby32_notypes/my_queries_sql.rb | 12 +- .../sqlite3/ruby32_notypes/other_sql.rb | 12 +- .../sqlite3/ruby32_notypes/query_sql.rb | 148 ++++++++- test/sqlite_placeholders_test.rb | 61 ++++ tests/end2end_mysql2.rb | 257 +++++++++++++-- tests/end2end_pg.rb | 305 ++++++++++++++++-- tests/end2end_sqlite3.rb | 219 ++++++++++++- tests/rbs_types.rb | 3 +- 85 files changed, 3543 insertions(+), 215 deletions(-) create mode 100644 Gemfile.e2e create mode 100644 scripts/tests/capture_fixtures.rb create mode 100644 scripts/tests/generate_examples.rb create mode 100644 test/sqlite_placeholders_test.rb diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ab56807..4928920 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -69,7 +69,7 @@ jobs: strategy: fail-fast: false matrix: - ruby-version: ['3.3', '4.0'] + ruby-version: ['3.1', '3.2', '3.4', '4.0'] services: postgres: @@ -88,6 +88,7 @@ jobs: env: POSTGRES_HOST: 127.0.0.1 + BUNDLE_GEMFILE: ${{ (matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2') && 'Gemfile.e2e' || 'Gemfile' }} steps: - uses: actions/checkout@v7 @@ -97,6 +98,21 @@ jobs: ruby-version: ${{ matrix.ruby-version }} bundler-cache: true + - name: Load tool versions + if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' + run: grep -E '^[A-Z_]+=' scripts/tool-versions.env >> "$GITHUB_ENV" + + - uses: sqlc-dev/setup-sqlc@v5 + if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' + with: + sqlc-version: '${{ env.SQLC_VERSION }}' + + - name: Generate version-specific examples + if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' + run: | + ruby scripts/tests/generate_examples.rb "${{ matrix.ruby-version }}" sqlc.local.yaml + echo "EXAMPLES_DIR=$PWD/.examples-ruby-${{ matrix.ruby-version }}" >> "$GITHUB_ENV" + - name: Create the schema run: psql -v ON_ERROR_STOP=1 -h 127.0.0.1 -U "${DB_USER}" -d "${DB_NAME}" -f examples/authors/postgresql/schema.sql env: @@ -112,7 +128,7 @@ jobs: strategy: fail-fast: false matrix: - ruby-version: ['3.3', '4.0'] + ruby-version: ['3.1', '3.2', '3.4', '4.0'] services: mysql: @@ -130,6 +146,7 @@ jobs: env: MYSQL_HOST: 127.0.0.1 + BUNDLE_GEMFILE: ${{ (matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2') && 'Gemfile.e2e' || 'Gemfile' }} steps: - uses: actions/checkout@v7 @@ -139,6 +156,21 @@ jobs: ruby-version: ${{ matrix.ruby-version }} bundler-cache: true + - name: Load tool versions + if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' + run: grep -E '^[A-Z_]+=' scripts/tool-versions.env >> "$GITHUB_ENV" + + - uses: sqlc-dev/setup-sqlc@v5 + if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' + with: + sqlc-version: '${{ env.SQLC_VERSION }}' + + - name: Generate version-specific examples + if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' + run: | + ruby scripts/tests/generate_examples.rb "${{ matrix.ruby-version }}" sqlc.local.yaml + echo "EXAMPLES_DIR=$PWD/.examples-ruby-${{ matrix.ruby-version }}" >> "$GITHUB_ENV" + - name: Create the schema (and allow LOAD DATA LOCAL INFILE for :copyfrom) run: | mysql -h 127.0.0.1 -u "${DB_USER}" -e 'SET GLOBAL local_infile = 1' @@ -154,7 +186,10 @@ jobs: strategy: fail-fast: false matrix: - ruby-version: ['3.3', '4.0'] + ruby-version: ['3.1', '3.2', '3.4', '4.0'] + + env: + BUNDLE_GEMFILE: ${{ (matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2') && 'Gemfile.e2e' || 'Gemfile' }} steps: - uses: actions/checkout@v7 @@ -164,5 +199,20 @@ jobs: ruby-version: ${{ matrix.ruby-version }} bundler-cache: true + - name: Load tool versions + if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' + run: grep -E '^[A-Z_]+=' scripts/tool-versions.env >> "$GITHUB_ENV" + + - uses: sqlc-dev/setup-sqlc@v5 + if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' + with: + sqlc-version: '${{ env.SQLC_VERSION }}' + + - name: Generate version-specific examples + if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' + run: | + ruby scripts/tests/generate_examples.rb "${{ matrix.ruby-version }}" sqlc.local.yaml + echo "EXAMPLES_DIR=$PWD/.examples-ruby-${{ matrix.ruby-version }}" >> "$GITHUB_ENV" + - name: End-to-End tests run: bundle exec rake end2end_sqlite3 diff --git a/.gitignore b/.gitignore index a7e7b41..7f693b4 100644 --- a/.gitignore +++ b/.gitignore @@ -144,3 +144,4 @@ plugin.wasm plugins # scratch output of scripts/tests/run_codegen.rb .codegen-test* +.examples-ruby-* diff --git a/Gemfile.e2e b/Gemfile.e2e new file mode 100644 index 0000000..d525ab6 --- /dev/null +++ b/Gemfile.e2e @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +source 'https://rubygems.org' + +gem 'async', RUBY_VERSION.start_with?('3.1') ? '< 2.7' : '>= 2.7' +gem 'bigdecimal' +gem 'connection_pool' +gem 'csv' +gem 'logger' +gem 'minitest' +gem 'mysql2' +gem 'pg', '1.7.0' +gem 'rake' +gem 'sqlite3' diff --git a/README.md b/README.md index 6c5b7af..e5e2baf 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,8 @@ per table, one module of constants per enum), `connection.rb` (shared helpers) a * **Logging:** `connect(params, logger: Rails.logger)` / `new(pool, logger:)` logs `QuerySql#get_author (0.4ms)` at debug level for each query and transaction. Parameter values are never logged. * **Async / fibers:** it is plain synchronous Ruby and works with the Fiber scheduler ([`async`](https://github.com/socketry/async)): - inside `Sync { ... }`, pooled `pg` and `mysql2` queries run concurrently. SQLite queries run one at a time. + inside `Sync { ... }`, pooled `pg` queries run concurrently. `mysql2` prepared statements block the scheduler; use + threads for concurrent `mysql2` queries. SQLite queries run one at a time. * **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`. @@ -115,7 +116,10 @@ queries.transaction(isolation: :serializable) do |tx| end ``` * Everything in the block runs on one connection. It commits when the block ends (also on `break`, `return`, `throw`) - and rolls back when it raises or the thread is killed. A nested `transaction` rolls back only its own part. + and rolls back when it raises or the thread is killed. On Ruby <3.3 with timeout <0.4, `break`, `return`, and + `throw` also roll back because Timeout cannot be told apart; add `gem 'timeout', '>= 0.4'` to the generated Gemfile + to keep those exits committing. A nested `transaction` rolls back only its own part. Query methods called on the + parent query object from the same thread also use the transaction's connection. * `isolation:` (`pg`, `mysql2`) takes `:read_uncommitted`, `:read_committed`, `:repeatable_read`, `:serializable`; `mode:` (`sqlite3`) takes `:deferred`, `:immediate`, `:exclusive`. Other values raise before anything runs, and so do `isolation:` / `mode:` on a nested `transaction` (a savepoint cannot change them). diff --git a/examples/authors/mysql/query.sql b/examples/authors/mysql/query.sql index 9ebbb5e..35b68ca 100644 --- a/examples/authors/mysql/query.sql +++ b/examples/authors/mysql/query.sql @@ -43,6 +43,15 @@ LIMIT ? OFFSET ?; SELECT * FROM authors WHERE name LIKE COALESCE(sqlc.narg('name_pattern'), '%'); +-- name: GetAuthorsByOptionalName :many +SELECT * FROM authors +WHERE (sqlc.narg(name_filter) IS NULL OR name = sqlc.narg(name_filter)) +ORDER BY id +LIMIT ?; + +-- name: PauseQuery :exec +SELECT SLEEP(sqlc.arg(seconds)); + -- name: UpdateAuthorBios :execrows -- Sets the bio of every author that has one and returns how many were changed. UPDATE authors @@ -116,6 +125,9 @@ INSERT INTO regressions (id, `order`, note, payload) VALUES (?, ?, ?, ?); -- name: ListRegressionPayloads :many SELECT id, payload FROM regressions ORDER BY id; +-- name: ListRegressionRows :many +SELECT id, `order`, note, payload FROM regressions ORDER BY id; + -- name: TruncateRegressions :exec TRUNCATE TABLE regressions; diff --git a/examples/authors/postgresql/query.sql b/examples/authors/postgresql/query.sql index cbffa4c..39452aa 100644 --- a/examples/authors/postgresql/query.sql +++ b/examples/authors/postgresql/query.sql @@ -23,6 +23,23 @@ WHERE id = $1; SELECT * FROM node_postgres_types LIMIT 1; +-- name: SetJSONRoundTrip :exec +INSERT INTO json_round_trips (id, json_value, jsonb_value) VALUES ($1, $2, $3); + +-- name: GetJSONRoundTrip :one +SELECT json_value, jsonb_value FROM json_round_trips WHERE id = $1; + +-- name: ToggleJSONBRoundTrip :exec +UPDATE json_round_trips +SET jsonb_value = to_jsonb(NOT (jsonb_value #>> '{}')::boolean) +WHERE id = $1; + +-- name: TruncateJSONRoundTrips :exec +TRUNCATE TABLE json_round_trips; + +-- name: PauseQuery :exec +SELECT pg_sleep(sqlc.arg(seconds)::double precision); + -- name: ListAuthorsPaged :many SELECT * FROM authors ORDER BY name @@ -33,6 +50,12 @@ OFFSET sqlc.arg('offset'); SELECT * FROM authors WHERE name LIKE COALESCE(sqlc.narg('name_pattern'), '%'); +-- name: GetAuthorsByOptionalName :many +SELECT * FROM authors +WHERE (sqlc.narg(name_filter)::text IS NULL OR name = sqlc.narg(name_filter)::text) +ORDER BY id +LIMIT sqlc.arg(lim); + -- name: CreateAuthorReturnId :execlastid INSERT INTO authors (name, bio) VALUES ($1, $2) RETURNING id; @@ -60,6 +83,12 @@ TRUNCATE TABLE authors CASCADE; -- name: CreateBook :execlastid INSERT INTO books (name, author_id) VALUES ($1, $2) RETURNING id; +-- name: CreateDeferredBook :exec +INSERT INTO deferred_books (author_id) VALUES ($1); + +-- name: TruncateDeferredBooks :exec +TRUNCATE TABLE deferred_books; + -- name: ListAllAuthorsBooks :many SELECT sqlc.embed(authors), diff --git a/examples/authors/postgresql/schema.sql b/examples/authors/postgresql/schema.sql index 573e1dc..f694cca 100644 --- a/examples/authors/postgresql/schema.sql +++ b/examples/authors/postgresql/schema.sql @@ -37,6 +37,11 @@ CREATE TABLE books ( FOREIGN KEY (author_id) REFERENCES authors (id) ON DELETE CASCADE ); +CREATE TABLE deferred_books ( + id BIGSERIAL PRIMARY KEY, + author_id BIGINT NOT NULL REFERENCES authors (id) DEFERRABLE INITIALLY DEFERRED +); + CREATE SCHEMA extended; CREATE TYPE extended.bio_type AS ENUM ('Autobiography', 'Biography', 'Memoir'); @@ -59,6 +64,12 @@ CREATE TABLE postgres_extra_types ( c_not_null TEXT NOT NULL DEFAULT 'x' ); +CREATE TABLE json_round_trips ( + id INTEGER PRIMARY KEY, + json_value JSON, + jsonb_value JSONB +); + CREATE TABLE postgres_array_types ( c_uuid_array UUID [], c_bio_type_array extended.bio_type [], diff --git a/examples/authors/sqlite/query.sql b/examples/authors/sqlite/query.sql index faf2146..a6fab79 100644 --- a/examples/authors/sqlite/query.sql +++ b/examples/authors/sqlite/query.sql @@ -27,6 +27,12 @@ LIMIT sqlc.arg('limit') OFFSET sqlc.arg('offset'); SELECT * FROM authors WHERE name LIKE COALESCE(sqlc.narg('name_pattern'), '%'); +-- name: GetAuthorsByOptionalName :many +SELECT * FROM authors +WHERE (sqlc.narg(name_filter) IS NULL OR name = sqlc.narg(name_filter)) +ORDER BY id +LIMIT sqlc.arg(lim); + -- name: CreateAuthorReturnId :execlastid INSERT INTO authors (name, bio) VALUES (?, ?) RETURNING id; @@ -42,6 +48,16 @@ SELECT * FROM authors WHERE id IN (sqlc.slice('ids')); -- name: GetAuthorsByIdsAndNames :many SELECT * FROM authors WHERE id IN (sqlc.slice('ids')) AND name IN (sqlc.slice('names')); +-- name: GetAuthorsByMixedPlaceholders :many +SELECT * FROM authors +WHERE name = sqlc.arg(n) + AND id IN (sqlc.slice(ids)) + AND name = sqlc.arg(n) + AND id > ? + AND id IN (sqlc.slice(more)) + AND 'x?' != '?3' +ORDER BY id; + -- name: CopyAuthors :copyfrom -- Bulk insert: pass an Array of CopyAuthorsArgs. INSERT INTO authors (name, bio) VALUES (?, ?); @@ -113,6 +129,18 @@ SELECT coalesce(max(price), 0) AS max_price FROM regressions; -- name: TruncateRegressions :exec DELETE FROM regressions; +-- name: CopyRegressionRows :copyfrom +INSERT INTO regressions (id, "order", price, payload) VALUES (?, ?, ?, ?); + +-- name: ListRegressionRows :many +SELECT id, "order", price, payload FROM regressions ORDER BY id; + +-- name: TruncateRegressionRows :exec +DELETE FROM regressions; + +-- name: PauseQuery :exec +SELECT sleep(?); + -- name: CopyTagsOrReplace :copyfrom INSERT OR REPLACE INTO tags (name, uses) VALUES (?, ?); diff --git a/examples/authors/sqlite/schema.sql b/examples/authors/sqlite/schema.sql index 5b78e26..6ec86cc 100644 --- a/examples/authors/sqlite/schema.sql +++ b/examples/authors/sqlite/schema.sql @@ -26,7 +26,8 @@ CREATE TABLE declared_types ( CREATE TABLE regressions ( id INTEGER PRIMARY KEY AUTOINCREMENT, "order" VARCHAR(255) NOT NULL, - price DECIMAL(10,2) + price DECIMAL(10,2), + payload BLOB ); CREATE TABLE tags ( diff --git a/examples/mysql2/query_sql.rb b/examples/mysql2/query_sql.rb index 298ad97..076b1eb 100644 --- a/examples/mysql2/query_sql.rb +++ b/examples/mysql2/query_sql.rb @@ -114,6 +114,23 @@ class ListAuthorsPagedRow < Data.define(:id, :name, :bio) class GetAuthorByNamePatternRow < Data.define(:id, :name, :bio) end + GET_AUTHORS_BY_OPTIONAL_NAME_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE (? IS NULL OR name = ?) + ORDER BY id + LIMIT ? + SQL + + class GetAuthorsByOptionalNameRow < Data.define(:id, :name, :bio) + end + + PAUSE_QUERY_SQL = <<~'SQL' + SELECT SLEEP(?) + SQL + + class PauseQueryRow < Data.define(:sleep) + end + UPDATE_AUTHOR_BIOS_SQL = <<~'SQL' UPDATE authors SET bio = ? @@ -245,6 +262,13 @@ class CopyRegressionRowsArgs < Data.define(:id, :order, :note, :payload) class ListRegressionPayloadsRow < Data.define(:id, :payload) end + LIST_REGRESSION_ROWS_SQL = <<~'SQL' + SELECT id, `order`, note, payload FROM regressions ORDER BY id + SQL + + class ListRegressionRowsRow < Data.define(:id, :order, :note, :payload) + end + TRUNCATE_REGRESSIONS_SQL = <<~'SQL' TRUNCATE TABLE regressions SQL @@ -529,6 +553,34 @@ def get_author_by_name_pattern(name_pattern: nil) end end + def get_authors_by_optional_name(limit:, name_filter: nil) + with_client('get_authors_by_optional_name') do |client| + query_params = [name_filter, name_filter, limit] + stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) + begin + result = stmt.execute(*query_params, as: :array) + result.map do |row| + GetAuthorsByOptionalNameRow.new(row[0], row[1], row[2]) + end + ensure + stmt.close + end + end + end + + def pause_query(seconds:) + with_client('pause_query') do |client| + query_params = [seconds] + stmt = client.prepare(PAUSE_QUERY_SQL) + begin + stmt.execute(*query_params, as: :array) + nil + ensure + stmt.close + end + end + end + # Sets the bio of every author that has one and returns how many were changed. def update_author_bios(bio: nil) with_client('update_author_bios') do |client| @@ -846,6 +898,20 @@ def list_regression_payloads end end + def list_regression_rows + with_client('list_regression_rows') do |client| + stmt = client.prepare(LIST_REGRESSION_ROWS_SQL) + begin + result = stmt.execute(as: :array) + result.map do |row| + ListRegressionRowsRow.new(row[0], row[1], row[2], row[3]) + end + ensure + stmt.close + end + end + end + def truncate_regressions with_client('truncate_regressions') do |client| stmt = client.prepare(TRUNCATE_REGRESSIONS_SQL) diff --git a/examples/mysql2/query_sql.rbs b/examples/mysql2/query_sql.rbs index 31d4b61..764e843 100644 --- a/examples/mysql2/query_sql.rbs +++ b/examples/mysql2/query_sql.rbs @@ -125,6 +125,22 @@ module Mysql2Codegen def initialize: ( Integer id, String name, String? bio) -> void end + GET_AUTHORS_BY_OPTIONAL_NAME_SQL: String + + class GetAuthorsByOptionalNameRow + attr_reader id: Integer + attr_reader name: String + attr_reader bio: String? + def initialize: ( Integer id, String name, String? bio) -> void + end + + PAUSE_QUERY_SQL: String + + class PauseQueryRow + attr_reader sleep: Integer + def initialize: ( Integer sleep) -> void + end + UPDATE_AUTHOR_BIOS_SQL: String GET_AUTHORS_BY_IDS_SQL: String @@ -271,6 +287,16 @@ module Mysql2Codegen def initialize: ( Integer id, String? payload) -> void end + LIST_REGRESSION_ROWS_SQL: String + + class ListRegressionRowsRow + attr_reader id: Integer + attr_reader order: String + attr_reader note: String? + attr_reader payload: String? + def initialize: ( Integer id, String order, String? note, String? payload) -> void + end + TRUNCATE_REGRESSIONS_SQL: String class CopyCopyTypesArgs @@ -380,6 +406,10 @@ module Mysql2Codegen def get_author_by_name_pattern: (?name_pattern: String?) -> Array[GetAuthorByNamePatternRow] + def get_authors_by_optional_name: (limit: Integer, ?name_filter: untyped) -> Array[GetAuthorsByOptionalNameRow] + + def pause_query: (seconds: Integer) -> void + def update_author_bios: (?bio: String?) -> Integer def get_authors_by_ids: (ids: Array[Integer]) -> Array[GetAuthorsByIdsRow] @@ -426,6 +456,8 @@ module Mysql2Codegen def list_regression_payloads: -> Array[ListRegressionPayloadsRow] + def list_regression_rows: -> Array[ListRegressionRowsRow] + def truncate_regressions: -> void def copy_copy_types: (Array[CopyCopyTypesArgs] rows) -> void diff --git a/examples/pg/models.rb b/examples/pg/models.rb index b9a3073..6b0fdb0 100644 --- a/examples/pg/models.rb +++ b/examples/pg/models.rb @@ -40,6 +40,9 @@ class NodePostgresType < Data.define( class Book < Data.define(:id, :name, :author_id, :description) end + class DeferredBook < Data.define(:id, :author_id) + end + class PostgresExtraType < Data.define( :c_uuid, :c_jsonb, @@ -52,6 +55,9 @@ class PostgresExtraType < Data.define( ) end + class JsonRoundTrip < Data.define(:id, :json_value, :jsonb_value) + end + class PostgresArrayType < Data.define(:c_uuid_array, :c_bio_type_array, :c_integer_matrix) end diff --git a/examples/pg/models.rbs b/examples/pg/models.rbs index edc0520..e5685fd 100644 --- a/examples/pg/models.rbs +++ b/examples/pg/models.rbs @@ -66,6 +66,12 @@ module PgCodegen def initialize: ( String id, String name, Integer author_id, String? description) -> void end + class DeferredBook + attr_reader id: Integer + attr_reader author_id: Integer + def initialize: ( Integer id, Integer author_id) -> void + end + class PostgresExtraType attr_reader c_uuid: String? attr_reader c_jsonb: untyped @@ -87,6 +93,13 @@ module PgCodegen ) -> void end + class JsonRoundTrip + attr_reader id: Integer + attr_reader json_value: untyped + attr_reader jsonb_value: untyped + def initialize: ( Integer id, untyped json_value, untyped jsonb_value) -> void + end + class PostgresArrayType attr_reader c_uuid_array: Array[String]? attr_reader c_bio_type_array: Array[String]? diff --git a/examples/pg/query_sql.rb b/examples/pg/query_sql.rb index e8bb3ad..6f705bf 100644 --- a/examples/pg/query_sql.rb +++ b/examples/pg/query_sql.rb @@ -67,6 +67,34 @@ class TestRow < Data.define( ) end + SET_JSON_ROUND_TRIP_SQL = <<~'SQL' + INSERT INTO json_round_trips (id, json_value, jsonb_value) VALUES ($1, $2, $3) + SQL + + GET_JSON_ROUND_TRIP_SQL = <<~'SQL' + SELECT json_value, jsonb_value FROM json_round_trips WHERE id = $1 + SQL + + class GetJSONRoundTripRow < Data.define(:json_value, :jsonb_value) + end + + TOGGLE_JSONB_ROUND_TRIP_SQL = <<~'SQL' + UPDATE json_round_trips + SET jsonb_value = to_jsonb(NOT (jsonb_value #>> '{}')::boolean) + WHERE id = $1 + SQL + + TRUNCATE_JSON_ROUND_TRIPS_SQL = <<~'SQL' + TRUNCATE TABLE json_round_trips + SQL + + PAUSE_QUERY_SQL = <<~'SQL' + SELECT pg_sleep($1::double precision) + SQL + + class PauseQueryRow < Data.define(:pg_sleep) + end + LIST_AUTHORS_PAGED_SQL = <<~'SQL' SELECT id, name, bio FROM authors ORDER BY name @@ -85,6 +113,16 @@ class ListAuthorsPagedRow < Data.define(:id, :name, :bio) class GetAuthorByNamePatternRow < Data.define(:id, :name, :bio) end + GET_AUTHORS_BY_OPTIONAL_NAME_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE ($1::text IS NULL OR name = $1::text) + ORDER BY id + LIMIT $2 + SQL + + class GetAuthorsByOptionalNameRow < Data.define(:id, :name, :bio) + end + CREATE_AUTHOR_RETURN_ID_SQL = <<~'SQL' INSERT INTO authors (name, bio) VALUES ($1, $2) RETURNING id SQL @@ -128,6 +166,14 @@ class CopyAuthorsArgs < Data.define(:name, :bio) class CreateBookRow < Data.define(:id) end + CREATE_DEFERRED_BOOK_SQL = <<~'SQL' + INSERT INTO deferred_books (author_id) VALUES ($1) + SQL + + TRUNCATE_DEFERRED_BOOKS_SQL = <<~'SQL' + TRUNCATE TABLE deferred_books + SQL + LIST_ALL_AUTHORS_BOOKS_SQL = <<~'SQL' SELECT authors.id, authors.name, authors.bio, @@ -551,6 +597,71 @@ def test!(...) test(...) || raise(NotFound, 'Test returned no row') end + 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') + end + client.exec_prepared('query_sql_set_json_round_trip', query_params) + nil + end + end + + 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') + end + result = client.exec_prepared('query_sql_get_json_round_trip', query_params) + row = result.first + row && GetJSONRoundTripRow.new(row['json_value'], row['jsonb_value']) + end + end + + # Like #get_json_round_trip, but raises NotFound when there is no row. + def get_json_round_trip!(...) + get_json_round_trip(...) || raise(NotFound, 'GetJSONRoundTrip returned no row') + end + + 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') + end + client.exec_prepared('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') + end + client.exec_prepared('query_sql_truncate_json_round_trips') + nil + end + end + + 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') + end + client.exec_prepared('query_sql_pause_query', query_params) + nil + end + end + def list_authors_paged(offset:, limit:) with_client('list_authors_paged') do |client| query_params = [offset, limit] @@ -579,6 +690,20 @@ def get_author_by_name_pattern(name_pattern: nil) end end + 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') + end + result = client.exec_prepared('query_sql_get_authors_by_optional_name', query_params) + result.map do |row| + GetAuthorsByOptionalNameRow.new(row['id'], row['name'], row['bio']) + end + end + end + def create_author_return_id(name:, bio:) with_client('create_author_return_id') do |client| query_params = [name, bio] @@ -670,6 +795,29 @@ def create_book(name:, author_id:) end end + 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') + end + client.exec_prepared('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') + end + client.exec_prepared('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') @@ -1237,9 +1385,13 @@ def encode_bytea(value) value && PG::TextEncoder::Bytea.new.encode(value) end - # json / jsonb parameters: an Array is a JSON array, not a PG array (Hashes and JSON text pass through). + # json / jsonb parameters: booleans and Arrays need JSON encoding, not PostgreSQL's boolean / array encoding. + # Hashes and JSON text pass through; nil stays nil. def encode_json(value) - value.is_a?(Array) ? PG::TextEncoder::JSON.new.encode(value) : value + case value + when Array, TrueClass, FalseClass then PG::TextEncoder::JSON.new.encode(value) + else value + end end # Float#to_s is the shortest text that reads back as the same Float (the pg gem's encoder drops digits). diff --git a/examples/pg/query_sql.rbs b/examples/pg/query_sql.rbs index dad457d..844a775 100644 --- a/examples/pg/query_sql.rbs +++ b/examples/pg/query_sql.rbs @@ -72,6 +72,27 @@ module PgCodegen ) -> void end + SET_JSON_ROUND_TRIP_SQL: String + + GET_JSON_ROUND_TRIP_SQL: String + + class GetJSONRoundTripRow + attr_reader json_value: untyped + attr_reader jsonb_value: untyped + def initialize: ( untyped json_value, untyped jsonb_value) -> void + end + + TOGGLE_JSONB_ROUND_TRIP_SQL: String + + TRUNCATE_JSON_ROUND_TRIPS_SQL: String + + PAUSE_QUERY_SQL: String + + class PauseQueryRow + attr_reader pg_sleep: untyped + def initialize: ( untyped pg_sleep) -> void + end + LIST_AUTHORS_PAGED_SQL: String class ListAuthorsPagedRow @@ -90,6 +111,15 @@ module PgCodegen def initialize: ( Integer id, String name, String? bio) -> void end + GET_AUTHORS_BY_OPTIONAL_NAME_SQL: String + + class GetAuthorsByOptionalNameRow + attr_reader id: Integer + attr_reader name: String + attr_reader bio: String? + def initialize: ( Integer id, String name, String? bio) -> void + end + CREATE_AUTHOR_RETURN_ID_SQL: String class CreateAuthorReturnIdRow @@ -132,6 +162,10 @@ module PgCodegen def initialize: ( String id) -> void end + CREATE_DEFERRED_BOOK_SQL: String + + TRUNCATE_DEFERRED_BOOKS_SQL: String + LIST_ALL_AUTHORS_BOOKS_SQL: String class ListAllAuthorsBooksRow @@ -464,10 +498,24 @@ module PgCodegen def test!: -> TestRow + def set_json_round_trip: (id: Integer, json_value: untyped, jsonb_value: untyped) -> void + + def get_json_round_trip: (id: Integer) -> GetJSONRoundTripRow? + + def get_json_round_trip!: (id: Integer) -> GetJSONRoundTripRow + + def toggle_jsonb_round_trip: (id: Integer) -> void + + def truncate_json_round_trips: -> void + + def pause_query: (seconds: Float) -> void + def list_authors_paged: (offset: Integer, limit: Integer) -> Array[ListAuthorsPagedRow] def get_author_by_name_pattern: (?name_pattern: String?) -> Array[GetAuthorByNamePatternRow] + def get_authors_by_optional_name: (lim: Integer, ?name_filter: String?) -> Array[GetAuthorsByOptionalNameRow] + def create_author_return_id: (name: String, bio: String?) -> Integer def update_author_bios: (bio: String?) -> Integer @@ -482,6 +530,10 @@ module PgCodegen def create_book: (name: String, author_id: Integer) -> String + def create_deferred_book: (author_id: Integer) -> void + + def truncate_deferred_books: -> void + def list_all_authors_books: -> Array[ListAllAuthorsBooksRow] def get_duplicate_authors: -> Array[GetDuplicateAuthorsRow] diff --git a/examples/sqlite3/models.rb b/examples/sqlite3/models.rb index 498e262..365bef7 100644 --- a/examples/sqlite3/models.rb +++ b/examples/sqlite3/models.rb @@ -20,7 +20,7 @@ class DeclaredType < Data.define( ) end - class Regression < Data.define(:id, :order, :price) + class Regression < Data.define(:id, :order, :price, :payload) end class Tag < Data.define(:name, :uses) diff --git a/examples/sqlite3/models.rbs b/examples/sqlite3/models.rbs index 60532b3..271946e 100644 --- a/examples/sqlite3/models.rbs +++ b/examples/sqlite3/models.rbs @@ -38,7 +38,8 @@ module Sqlite3Codegen attr_reader id: Integer attr_reader order: String attr_reader price: Float? - def initialize: ( Integer id, String order, Float? price) -> void + attr_reader payload: String? + def initialize: ( Integer id, String order, Float? price, String? payload) -> void end class Tag diff --git a/examples/sqlite3/query_sql.rb b/examples/sqlite3/query_sql.rb index 38b041b..9e3c27b 100644 --- a/examples/sqlite3/query_sql.rb +++ b/examples/sqlite3/query_sql.rb @@ -54,6 +54,16 @@ class ListAuthorsPagedRow < Data.define(:id, :name, :bio) class GetAuthorByNamePatternRow < Data.define(:id, :name, :bio) end + GET_AUTHORS_BY_OPTIONAL_NAME_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE (?1 IS NULL OR name = ?1) + ORDER BY id + LIMIT ?2 + SQL + + class GetAuthorsByOptionalNameRow < Data.define(:id, :name, :bio) + end + CREATE_AUTHOR_RETURN_ID_SQL = <<~'SQL' INSERT INTO authors (name, bio) VALUES (?, ?) RETURNING id SQL @@ -81,6 +91,20 @@ class GetAuthorsByIdsRow < Data.define(:id, :name, :bio) class GetAuthorsByIdsAndNamesRow < Data.define(:id, :name, :bio) end + GET_AUTHORS_BY_MIXED_PLACEHOLDERS_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE name = ? + AND id IN (/*SLICE:ids*/?) + AND name = ? + AND id > ? + AND id IN (/*SLICE:more*/?) + AND 'x?' != '?3' + ORDER BY id + SQL + + class GetAuthorsByMixedPlaceholdersRow < Data.define(:id, :name, :bio) + end + class CopyAuthorsArgs < Data.define(:name, :bio) end @@ -218,6 +242,27 @@ class GetMaxRegressionPriceRow < Data.define(:max_price) DELETE FROM regressions SQL + class CopyRegressionRowsArgs < Data.define(:id, :order, :price, :payload) + end + + LIST_REGRESSION_ROWS_SQL = <<~'SQL' + SELECT id, "order", price, payload FROM regressions ORDER BY id + SQL + + class ListRegressionRowsRow < Data.define(:id, :order, :price, :payload) + end + + TRUNCATE_REGRESSION_ROWS_SQL = <<~'SQL' + DELETE FROM regressions + SQL + + PAUSE_QUERY_SQL = <<~'SQL' + SELECT sleep(?) + SQL + + class PauseQueryRow < Data.define(:sleep) + end + class CopyTagsOrReplaceArgs < Data.define(:name, :uses) end @@ -375,6 +420,21 @@ def get_author_by_name_pattern(name_pattern: nil) end end + def get_authors_by_optional_name(lim:, name_filter: nil) + with_client('get_authors_by_optional_name') do |client| + query_params = [name_filter, lim] + stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) + begin + result = stmt.execute(*query_params) + result.map do |row| + GetAuthorsByOptionalNameRow.new(row[0], row[1], row[2]) + end + ensure + stmt.close + end + end + end + def create_author_return_id(name:, bio:) with_client('create_author_return_id') do |client| query_params = [name, bio] @@ -436,6 +496,31 @@ def get_authors_by_ids_and_names(ids:, names:) end end + # rubocop:disable Naming/MethodParameterName + def get_authors_by_mixed_placeholders(n:, ids:, id:, more:) + with_client('get_authors_by_mixed_placeholders') do |client| + query_params = [ + n, + *ids, + n, + id, + *more + ] + sql = GET_AUTHORS_BY_MIXED_PLACEHOLDERS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) + sql = sql.gsub('/*SLICE:more*/?', slice_placeholders(more)) + stmt = client.prepare(sql) + begin + result = stmt.execute(*query_params) + result.map do |row| + GetAuthorsByMixedPlaceholdersRow.new(row[0], row[1], row[2]) + end + ensure + stmt.close + end + end + end + # rubocop:enable Naming/MethodParameterName + # Bulk insert: pass an Array of CopyAuthorsArgs. def copy_authors(rows) with_client('copy_authors') do |client| @@ -723,6 +808,57 @@ def truncate_regressions end end + def copy_regression_rows(rows) + with_client('copy_regression_rows') do |client| + atomically(client) do + rows.each_slice(249) do |batch| + sql = %(INSERT INTO "regressions" ("id", "order", "price", "payload") VALUES #{Array.new(batch.size, '(?, ?, ?, ?)').join(', ')}) + client.execute(sql, batch.flat_map { |row| [row.id, row.order, row.price, row.payload] }) + end + end + nil + end + end + + def list_regression_rows + with_client('list_regression_rows') do |client| + stmt = client.prepare(LIST_REGRESSION_ROWS_SQL) + begin + result = stmt.execute + result.map do |row| + ListRegressionRowsRow.new(row[0], row[1], row[2], row[3]) + end + ensure + stmt.close + end + end + end + + def truncate_regression_rows + with_client('truncate_regression_rows') do |client| + stmt = client.prepare(TRUNCATE_REGRESSION_ROWS_SQL) + begin + stmt.execute + nil + ensure + stmt.close + end + end + end + + def pause_query(sleep:) + with_client('pause_query') do |client| + query_params = [sleep] + stmt = client.prepare(PAUSE_QUERY_SQL) + begin + stmt.execute(*query_params).to_a + nil + ensure + stmt.close + end + end + end + def copy_tags_or_replace(rows) with_client('copy_tags_or_replace') do |client| atomically(client) do diff --git a/examples/sqlite3/query_sql.rbs b/examples/sqlite3/query_sql.rbs index 0f3f568..17d95ff 100644 --- a/examples/sqlite3/query_sql.rbs +++ b/examples/sqlite3/query_sql.rbs @@ -40,6 +40,15 @@ module Sqlite3Codegen def initialize: ( Integer id, String name, String? bio) -> void end + GET_AUTHORS_BY_OPTIONAL_NAME_SQL: String + + class GetAuthorsByOptionalNameRow + attr_reader id: Integer + attr_reader name: String + attr_reader bio: String? + def initialize: ( Integer id, String name, String? bio) -> void + end + CREATE_AUTHOR_RETURN_ID_SQL: String class CreateAuthorReturnIdRow @@ -67,6 +76,15 @@ module Sqlite3Codegen def initialize: ( Integer id, String name, String? bio) -> void end + GET_AUTHORS_BY_MIXED_PLACEHOLDERS_SQL: String + + class GetAuthorsByMixedPlaceholdersRow + attr_reader id: Integer + attr_reader name: String + attr_reader bio: String? + def initialize: ( Integer id, String name, String? bio) -> void + end + class CopyAuthorsArgs attr_reader name: String attr_reader bio: String? @@ -209,6 +227,33 @@ module Sqlite3Codegen TRUNCATE_REGRESSIONS_SQL: String + class CopyRegressionRowsArgs + attr_reader id: Integer + attr_reader order: String + attr_reader price: Float? + attr_reader payload: String? + def initialize: ( Integer id, String order, Float? price, String? payload) -> void + end + + LIST_REGRESSION_ROWS_SQL: String + + class ListRegressionRowsRow + attr_reader id: Integer + attr_reader order: String + attr_reader price: Float? + attr_reader payload: String? + def initialize: ( Integer id, String order, Float? price, String? payload) -> void + end + + TRUNCATE_REGRESSION_ROWS_SQL: String + + PAUSE_QUERY_SQL: String + + class PauseQueryRow + attr_reader sleep: untyped + def initialize: ( untyped sleep) -> void + end + class CopyTagsOrReplaceArgs attr_reader name: String attr_reader uses: Integer @@ -265,6 +310,8 @@ module Sqlite3Codegen def get_author_by_name_pattern: (?name_pattern: String?) -> Array[GetAuthorByNamePatternRow] + def get_authors_by_optional_name: (lim: Integer, ?name_filter: untyped) -> Array[GetAuthorsByOptionalNameRow] + def create_author_return_id: (name: String, bio: String?) -> Integer def update_author_bios: (bio: String?) -> Integer @@ -273,6 +320,8 @@ module Sqlite3Codegen def get_authors_by_ids_and_names: (ids: Array[Integer], names: Array[String]) -> Array[GetAuthorsByIdsAndNamesRow] + def get_authors_by_mixed_placeholders: (n: String, ids: Array[Integer], id: Integer, more: Array[Integer]) -> Array[GetAuthorsByMixedPlaceholdersRow] + def copy_authors: (Array[CopyAuthorsArgs] rows) -> void def truncate_authors: -> void @@ -315,6 +364,14 @@ module Sqlite3Codegen def truncate_regressions: -> void + def copy_regression_rows: (Array[CopyRegressionRowsArgs] rows) -> void + + def list_regression_rows: -> Array[ListRegressionRowsRow] + + def truncate_regression_rows: -> void + + def pause_query: (sleep: untyped) -> void + def copy_tags_or_replace: (Array[CopyTagsOrReplaceArgs] rows) -> void def copy_tags_or_ignore: (Array[CopyTagsOrIgnoreArgs] rows) -> void diff --git a/lib/sqlc_gen_ruby/code.rb b/lib/sqlc_gen_ruby/code.rb index ac40578..935ed37 100644 --- a/lib/sqlc_gen_ruby/code.rb +++ b/lib/sqlc_gen_ruby/code.rb @@ -74,6 +74,7 @@ def param_name(name) instance_variable_get instance_variable_set instance_variables is_a? itself kind_of? members method methods nil? object_id public_method public_send respond_to? send singleton_class singleton_method singleton_methods tap then to_enum to_h to_s with yield_self].freeze + KERNEL_METHODS = %w[format raise rand].freeze # Struct (rows on Ruby < 3.2) also has Enumerable and accessors such as #size / #values. STRUCT_METHODS = (OBJECT_METHODS + %w[all? any? chain chunk chunk_while collect collect_concat compact count cycle diff --git a/lib/sqlc_gen_ruby/drivers/base.rb b/lib/sqlc_gen_ruby/drivers/base.rb index 4ed7447..5f774ef 100644 --- a/lib/sqlc_gen_ruby/drivers/base.rb +++ b/lib/sqlc_gen_ruby/drivers/base.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative '../code' +require_relative '../ruby_version' module SqlcGenRuby module Drivers @@ -33,10 +34,10 @@ def module_members = [] # Public methods of the generated query class that are not queries: constructor, `connect`, `transaction`. # [[code, rbs], ...] - def class_methods = raise(NotImplementedError) + def class_methods(_ruby_version = nil) = raise(NotImplementedError) # Private helpers of the query class. [[code, rbs], ...] - def private_methods = [] + def private_methods(_ruby_version = nil) = [] # How a query method acquires the database connection (yielding `client`). def client_block(func) = "with_client('#{func}') do |client|" @@ -44,6 +45,12 @@ def client_block(func) = "with_client('#{func}') do |client|" # RHS of the generated `Sql` constant (without the constant name). def query_text(query) = query.text + # Ruby expressions for the parameters passed to one generated query method. + def query_params(_query, params) + list = params.map { |p| p.slice ? "*#{p.name}" : encode_param(p.name, p.column) } + "query_params = [#{Code.comma_join(list)}]" + end + # stmt_name: unique per query (e.g. query_sql_get_author), only meaningful for named server-side statements. def prepare_stmt(_stmt_name, _const) = raise(NotImplementedError) @@ -158,6 +165,29 @@ class NotFound < StandardError; end private + def legacy_transaction_template(template, ruby_version, old_condition, guarded_condition) + return template unless ruby_version < RubyVersion.new(3, 3) + + template = template.sub(/^( +)def transaction\b/) do + indent = Regexp.last_match(1) + "#{indent}# On Ruby <3.3 with timeout <0.4, break / return / throw roll back because Timeout cannot be told apart.\n" \ + "#{indent}# Add gem 'timeout', '>= 0.4' to keep these exits committing.\n#{indent}def transaction" + end + template = template.sub(/^( +)failed = false\n\1begin\n( +)yield self\n/) do + indent = Regexp.last_match(1) + yield_indent = Regexp.last_match(2) + "#{indent}failed = false\n#{indent}completed = false\n#{indent}begin\n" \ + "#{yield_indent}result = yield self\n#{yield_indent}completed = true\n#{yield_indent}result\n" + end + template.sub(old_condition, guarded_condition) + end + + def timeout_exit_methods(ruby_version) + return [] unless ruby_version < RubyVersion.new(3, 3) + + [[TIMEOUT_EXIT_HELPER, TIMEOUT_EXIT_HELPER_RBS]] + end + # from sqlc - char(5), int4, pg_catalog.int8, BLOB, VARCHAR(255), ... => char, int4, int8, blob, varchar def normalize_type(type_name) = type_name.downcase.delete_prefix('pg_catalog.').sub(/\s*\(.*\)\z/, '') @@ -176,6 +206,14 @@ def mapping(ruby_type, db_types, prefix_based: false) def table_name(identifier) identifier.schema.to_s.empty? ? identifier.name : "#{identifier.schema}.#{identifier.name}" end + + TIMEOUT_EXIT_HELPER = <<~'RUBY'.chomp + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end + RUBY + TIMEOUT_EXIT_HELPER_RBS = 'def timeout_exits_with_throw?: () -> bool' end end end diff --git a/lib/sqlc_gen_ruby/drivers/mysql2.rb b/lib/sqlc_gen_ruby/drivers/mysql2.rb index dd43338..29cc5e3 100644 --- a/lib/sqlc_gen_ruby/drivers/mysql2.rb +++ b/lib/sqlc_gen_ruby/drivers/mysql2.rb @@ -15,17 +15,26 @@ def module_members ] end - def class_methods + def class_methods(ruby_version) + transaction = legacy_transaction_template( + TRANSACTION, ruby_version, + "Thread.current.status == 'aborting' ? rollback : commit", + "Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit" + ) [ [NOT_FOUND, "class NotFound < StandardError\nend"], [CONNECT, 'def self.connect: (Hash[untyped, untyped] mysql2_params, ?logger: untyped, **untyped pool_options) -> instance'], [INITIALIZE, 'def initialize: (untyped db, ?logger: untyped) -> void'], - [TRANSACTION, TRANSACTION_RBS] + [transaction, TRANSACTION_RBS] ] end - def private_methods - [[INSTRUMENT, INSTRUMENT_RBS], [WITH_CLIENT, 'def with_client: [T] (String name) { (untyped) -> T } -> T']] + def private_methods(ruby_version) + [ + [INSTRUMENT, INSTRUMENT_RBS], + [WITH_CLIENT, 'def with_client: [T] (String name) { (untyped) -> T } -> T'], + *timeout_exit_methods(ruby_version) + ] end def prepare_stmt(_stmt_name, const) = "stmt = client.prepare(#{const})" diff --git a/lib/sqlc_gen_ruby/drivers/pg.rb b/lib/sqlc_gen_ruby/drivers/pg.rb index 1cc9eb3..aa8a314 100644 --- a/lib/sqlc_gen_ruby/drivers/pg.rb +++ b/lib/sqlc_gen_ruby/drivers/pg.rb @@ -20,20 +20,26 @@ def module_members ] end - def class_methods + def class_methods(ruby_version) + transaction = legacy_transaction_template( + TRANSACTION, ruby_version, + "rescued || Thread.current.status == 'aborting' ? rollback : commit", + "rescued || Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit" + ) [ [NOT_FOUND, "class NotFound < StandardError\nend"], [CONNECT, 'def self.connect: (Hash[untyped, untyped] pg_params, ?logger: untyped, **untyped pool_options) -> instance'], [INITIALIZE, 'def initialize: (untyped db, ?logger: untyped) -> void'], - [TRANSACTION, TRANSACTION_RBS] + [transaction, TRANSACTION_RBS] ] end - def private_methods + def private_methods(ruby_version) [ [INSTRUMENT, INSTRUMENT_RBS], [WITH_CLIENT, 'def with_client: [T] (String name) { (untyped) -> T } -> T'], - [CONFIGURE, "def configure: (untyped client) -> void\n\ndef string_arrays: (untyped client) -> untyped"] + [CONFIGURE, "def configure: (untyped client) -> void\n\ndef string_arrays: (untyped client) -> untyped"], + *timeout_exit_methods(ruby_version) ] end @@ -109,9 +115,13 @@ def encode_bytea(value) end RUBY 'encode_json' => [<<~'RUBY'.chomp, 'def encode_json: (untyped value) -> untyped'], - # json / jsonb parameters: an Array is a JSON array, not a PG array (Hashes and JSON text pass through). + # json / jsonb parameters: booleans and Arrays need JSON encoding, not PostgreSQL's boolean / array encoding. + # Hashes and JSON text pass through; nil stays nil. def encode_json(value) - value.is_a?(Array) ? PG::TextEncoder::JSON.new.encode(value) : value + case value + when Array, TrueClass, FalseClass then PG::TextEncoder::JSON.new.encode(value) + else value + end end RUBY 'encode_float' => [<<~'RUBY'.chomp, 'def encode_float: (untyped value) -> untyped'], diff --git a/lib/sqlc_gen_ruby/drivers/sqlite3.rb b/lib/sqlc_gen_ruby/drivers/sqlite3.rb index 9a963b3..24037ee 100644 --- a/lib/sqlc_gen_ruby/drivers/sqlite3.rb +++ b/lib/sqlc_gen_ruby/drivers/sqlite3.rb @@ -22,19 +22,25 @@ def helper_methods(queries) [[ATOMICALLY, 'def atomically: (untyped client) { () -> void } -> void']] end - def class_methods + def class_methods(ruby_version) + transaction = legacy_transaction_template( + TRANSACTION, ruby_version, + "Thread.current.status == 'aborting' ? rollback : commit", + "Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit" + ) [ [NOT_FOUND, "class NotFound < StandardError\nend"], [INITIALIZE, 'def initialize: (untyped db, ?logger: untyped) -> void'], - [TRANSACTION, "def transaction: [T] (?mode: (:deferred | :immediate | :exclusive)?) { (self) -> T } -> T"] + [transaction, "def transaction: [T] (?mode: (:deferred | :immediate | :exclusive)?) { (self) -> T } -> T"] ] end - def private_methods + def private_methods(ruby_version) [ [INSTRUMENT, INSTRUMENT_RBS], ["def with_client(name, &block)\n instrument(name) { @lock.synchronize { block.call(@db) } }\nend", - 'def with_client: [T] (String name) { (untyped) -> T } -> T'] + 'def with_client: [T] (String name) { (untyped) -> T } -> T'], + *timeout_exit_methods(ruby_version) ] end @@ -50,6 +56,85 @@ def closes_statements? = true def supports_slice? = true + def query_text(query) + return query.text unless query.params.any? { |param| param.column.is_sqlc_slice } + + replacements = placeholder_occurrences(query.text).filter_map do |kind, number, first, last| + [first, last] if kind == :numbered + end + replacements.reverse_each.reduce(query.text.dup) { |sql, (first, last)| sql[0...first] + '?' + sql[last..] } + end + + def query_params(query, params) + return super unless query.params.any? { |param| param.column.is_sqlc_slice } + + by_number = query.params.zip(params).to_h { |source, param| [source.number, param] } + occurrences = placeholder_occurrences(query.text) + referenced = occurrences.filter_map { |kind, number,| number if kind == :numbered } + slices = occurrences.filter_map { |kind, name,| name if kind == :slice } + positional = query.params.each_with_index.filter_map do |source, index| + by_number[source.number] unless referenced.include?(source.number) || + (source.column.is_sqlc_slice && slices.include?(source.column.name)) + end + positional_index = 0 + ordered = occurrences.map do |kind, value,| + case kind + when :slice + query.params.each_with_index.find { |source, _| source.column.is_sqlc_slice && source.column.name == value } + .then { |_, index| params[index] } + when :numbered then by_number.fetch(value) + when :positional + param = positional.fetch(positional_index) + positional_index += 1 + param + end + end + expressions = ordered.map { |param| param.slice ? "*#{param.name}" : encode_param(param.name, param.column) } + "query_params = [#{Code.comma_join(expressions)}]" + end + + def placeholder_occurrences(sql) + occurrences = [] + index = 0 + while index < sql.length + if (slice = /\A\/\*SLICE:([A-Za-z_]\w*)\*\/\?/.match(sql[index..])) + occurrences << [:slice, slice[1], index, index + slice[0].length] + index += slice[0].length + elsif sql[index..].start_with?('--') + index = (sql.index("\n", index) || sql.length) + elsif sql[index..].start_with?('/*') + index = (sql.index('*/', index + 2) || sql.length - 2) + 2 + elsif %w[' " `].include?(sql[index]) + quote = sql[index] + index += 1 + while index < sql.length + if sql[index] == quote + if sql[index + 1] == quote + index += 2 + else + index += 1 + break + end + else + index += 1 + end + end + elsif sql[index] == '[' + index = (sql.index(']', index + 1) || sql.length - 1) + 1 + elsif sql[index] == '?' + finish = index + 1 + finish += 1 while finish < sql.length && sql[finish].match?(/\d/) + kind = finish == index + 1 ? :positional : :numbered + number = kind == :numbered ? sql[(index + 1)...finish].to_i : nil + occurrences << [kind, number, index, finish] + index = finish + else + index += 1 + end + end + occurrences + end + # sqlite3 only runs a statement eagerly when it has no result columns: one with RETURNING is stepped to the end. def exec_lines(exec, query) = [query.columns.empty? ? exec : "#{exec}.to_a", 'nil'] diff --git a/lib/sqlc_gen_ruby/generator.rb b/lib/sqlc_gen_ruby/generator.rb index 09e574c..e8b7b2b 100644 --- a/lib/sqlc_gen_ruby/generator.rb +++ b/lib/sqlc_gen_ruby/generator.rb @@ -158,12 +158,12 @@ def row_pairs(query) = @layout.entries(query).map { |e| [e.attr, e.type] } # constructor / connect / transaction, one method per query, then the private helpers def class_members(queries, method_gen) - members = @driver.class_methods.dup + members = @driver.class_methods(@options.ruby_version).dup if queries.any? { |q| q.cmd == ':execresult' } # last_insert_id is nil on PostgreSQL (use RETURNING) members << dataclass('ExecResult', [%w[rows_affected Integer], ['last_insert_id', 'Integer?']]) end - helpers = @driver.private_methods + method_gen.helper_methods(queries) + helpers = @driver.private_methods(@options.ruby_version) + method_gen.helper_methods(queries) check_method_names(queries, (members + helpers).flat_map { |code, _| code.scan(/^def (?:self\.)?([\w?!]+)/) }.flatten + Code::OBJECT_METHODS) members.concat(queries.map { |q| [method_gen.code(q), method_gen.type(q)] }) @@ -175,6 +175,7 @@ def class_members(queries, method_gen) def check_method_names(queries, generated) taken = generated.to_h { |name| [name, 'a method of the generated class'] } + Code::KERNEL_METHODS.each { |name| taken[name] = 'a Kernel method used by the generated class' } queries.each do |q| func = Code.snake_case(q.name) [func, *("#{func}!" if q.cmd == ':one')].each do |name| @@ -290,7 +291,10 @@ def unique_constants(values) end def gemfile(queries) - gems = (@driver.required_gems + @driver.extra_gems(queries)).uniq.sort.map { |g| "gem '#{g}'" }.join("\n") + gems = (@driver.required_gems + @driver.extra_gems(queries)).uniq.sort.map { |g| "gem '#{g}'" } + gems << "gem 'timeout', '>= 0.4'" if @options.ruby_version < RubyVersion.new(3, 3) + gems.sort! + gems = gems.join("\n") Plugin::File.new(name: 'Gemfile', contents: "# frozen_string_literal: true\n\nsource 'https://rubygems.org'\n\n#{gems}\n") end end diff --git a/lib/sqlc_gen_ruby/method_gen.rb b/lib/sqlc_gen_ruby/method_gen.rb index 968447d..cc82d02 100644 --- a/lib/sqlc_gen_ruby/method_gen.rb +++ b/lib/sqlc_gen_ruby/method_gen.rb @@ -64,7 +64,9 @@ def code(query) signature = '(rows)' else statements = query_statements(query, func, params) - keywords = params.reject(&:reused).map { |p| p.optional ? "#{p.name}: nil" : "#{p.name}:" } + keywords = params.reject(&:reused).partition(&:optional).then do |optional, required| + (required + optional).map { |p| p.optional ? "#{p.name}: nil" : "#{p.name}:" } + end signature = keywords.empty? ? '' : "(#{keywords.join(', ')})" end doc = query.comments.map { |c| "# #{c.strip}".rstrip }.join("\n") @@ -87,7 +89,8 @@ def type(query) if ROWS_COMMANDS.include?(query.cmd) signature = "(Array[#{query.name}Args] rows)" else - list = params(query).reject(&:reused).map { |p| "#{p.optional ? '?' : ''}#{p.name}: #{p.type}" } + optional, required = params(query).reject(&:reused).partition(&:optional) + list = (required + optional).map { |p| "#{p.optional ? '?' : ''}#{p.name}: #{p.type}" } signature = list.empty? ? '' : "(#{list.join(', ')})" end rbs = "def #{func}: #{signature}#{signature.empty? ? '' : ' '}-> #{return_type(query)}" @@ -195,7 +198,7 @@ def query_statements(query, func, params) end statements = [] - statements << query_params(params) unless params.empty? + statements << query_params(params, query) unless params.empty? const = Code.sql_constant(query.name) if slice?(query) statements << slice_sql(const, params) @@ -245,9 +248,8 @@ def batch_statements(query, func, params) statements end - def query_params(params) - list = params.map { |p| p.slice ? "*#{p.name}" : @driver.encode_param(p.name, p.column) } - "query_params = [#{Code.comma_join(list)}]" + def query_params(params, query) + @driver.query_params(query, params) end # sql = FOO_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) - once per slice diff --git a/scripts/capture_request.rb b/scripts/capture_request.rb index a82bbce..082616d 100755 --- a/scripts/capture_request.rb +++ b/scripts/capture_request.rb @@ -16,6 +16,6 @@ request = SqlcGenRuby::Plugin::GenerateRequest.decode($stdin.binmode.read) request.catalog.schemas.reject! { |s| SqlcGenRuby::Catalog::INTERNAL_SCHEMAS.include?(s.name) } -File.binwrite(ARGV.fetch(0), request.encode) +File.binwrite(ENV.fetch('FIXTURE_PATH') { ARGV.fetch(0) }, request.encode) # sqlc expects a (possibly empty) GenerateResponse back $stdout.binmode.write('') diff --git a/scripts/tests/capture_fixtures.rb b/scripts/tests/capture_fixtures.rb new file mode 100644 index 0000000..f260ef5 --- /dev/null +++ b/scripts/tests/capture_fixtures.rb @@ -0,0 +1,39 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require 'fileutils' +require 'rbconfig' +require 'yaml' + +root = File.expand_path('../..', __dir__) +config_path = File.expand_path(ARGV.fetch(0, 'sqlc.local.yaml'), root) +config = YAML.safe_load_file(config_path) +capture = File.join(root, 'scripts/capture_request.rb') +drivers = config.fetch('sql').map { |sql| sql.fetch('codegen').first.fetch('options').fetch('driver') }.uniq +wrappers = {} + +config['plugins'] = drivers.map do |driver| + fixture = File.join(root, "test/fixtures/#{driver}.bin") + wrapper = File.join(root, ".capture-request-#{driver}-#{Process.pid}") + File.write(wrapper, "#!/usr/bin/env ruby\nexec #{[RbConfig.ruby, capture, fixture].map(&:dump).join(', ')}\n") + FileUtils.chmod(0o755, wrapper) + wrappers[driver] = wrapper + { + 'name' => "capture-#{driver}", + 'process' => { 'cmd' => wrapper }, + 'env' => ['PATH'] + } +end +config.fetch('sql').each do |sql| + driver = sql.fetch('codegen').first.fetch('options').fetch('driver') + sql.fetch('codegen').first['plugin'] = "capture-#{driver}" +end + +tmp_config = File.join(File.dirname(config_path), ".capture-fixtures.#{Process.pid}.yaml") +begin + File.write(tmp_config, config.to_yaml) + system('sqlc', '-f', tmp_config, 'generate') || abort('sqlc fixture capture failed') +ensure + File.delete(tmp_config) if File.exist?(tmp_config) + wrappers.each_value { |wrapper| FileUtils.rm_f(wrapper) } +end diff --git a/scripts/tests/generate_examples.rb b/scripts/tests/generate_examples.rb new file mode 100644 index 0000000..bc10696 --- /dev/null +++ b/scripts/tests/generate_examples.rb @@ -0,0 +1,35 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require 'fileutils' +require 'yaml' + +ruby_version, config_path = ARGV +abort "usage: #{$PROGRAM_NAME} [sqlc config]" unless ruby_version + +root = File.expand_path('../..', __dir__) +config_path = File.expand_path(config_path || 'sqlc.local.yaml', root) +config_dir = File.dirname(config_path) +scratch = File.expand_path(".examples-ruby-#{ruby_version}", root) +unless scratch.start_with?("#{root}/.examples-ruby-") && File.basename(scratch).match?(/\A\.examples-ruby-[\d.]+\z/) + abort "refusing to generate outside an examples scratch directory: #{scratch}" +end + +FileUtils.rm_rf(scratch) +FileUtils.mkdir_p(scratch) +config = YAML.safe_load_file(config_path) +config.fetch('sql').each do |sql| + codegen = sql.fetch('codegen').first + codegen['out'] = File.join(File.basename(scratch), codegen.fetch('options').fetch('driver')) + codegen.fetch('options')['rubyVersion'] = ruby_version +end + +tmp_config = File.join(config_dir, ".generate-examples.#{Process.pid}.yaml") +begin + File.write(tmp_config, config.to_yaml) + system('sqlc', '-f', tmp_config, 'generate') || abort('sqlc generate failed') +ensure + FileUtils.rm_f(tmp_config) +end + +puts "EXAMPLES_DIR=#{scratch}" diff --git a/steep_expectations.yml b/steep_expectations.yml index 3aca857..9763999 100644 --- a/steep_expectations.yml +++ b/steep_expectations.yml @@ -3,50 +3,50 @@ diagnostics: - range: start: - line: 310 + line: 334 character: 10 end: - line: 310 + line: 334 character: 24 severity: WARNING message: 'Cannot find the declaration of constant: `ConnectionPool`' code: Ruby::UnknownConstant - range: start: - line: 310 + line: 334 character: 47 end: - line: 310 + line: 334 character: 53 severity: WARNING message: 'Cannot find the declaration of constant: `Mysql2`' code: Ruby::UnknownConstant - range: start: - line: 310 + line: 334 character: 66 end: - line: 310 + line: 334 character: 81 severity: WARNING message: Empty kwargs doesn't have type annotation code: Ruby::UnannotatedEmptyCollection - range: start: - line: 989 + line: 1055 character: 17 end: - line: 989 + line: 1055 character: 23 severity: WARNING message: 'Cannot find the declaration of constant: `Mysql2`' code: Ruby::UnknownConstant - range: start: - line: 1022 + line: 1088 character: 16 end: - line: 1022 + line: 1088 character: 22 severity: WARNING message: 'Cannot find the declaration of constant: `Mysql2`' @@ -55,260 +55,260 @@ diagnostics: - range: start: - line: 408 + line: 454 character: 10 end: - line: 408 + line: 454 character: 24 severity: WARNING message: 'Cannot find the declaration of constant: `ConnectionPool`' code: Ruby::UnknownConstant - range: start: - line: 408 + line: 454 character: 47 end: - line: 408 + line: 454 character: 49 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 408 + line: 454 character: 58 end: - line: 408 + line: 454 character: 69 severity: WARNING message: Empty kwargs doesn't have type annotation code: Ruby::UnannotatedEmptyCollection - range: start: - line: 423 + line: 469 character: 49 end: - line: 423 + line: 469 character: 51 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 443 + line: 489 character: 51 end: - line: 443 + line: 489 character: 53 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 447 + line: 493 character: 18 end: - line: 447 + line: 493 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 639 + line: 764 character: 18 end: - line: 639 + line: 764 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1045 + line: 1193 character: 18 end: - line: 1045 + line: 1193 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1126 + line: 1274 character: 18 end: - line: 1126 + line: 1274 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1204 + line: 1352 character: 43 end: - line: 1204 + line: 1352 character: 45 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1213 + line: 1361 character: 17 end: - line: 1213 + line: 1361 character: 19 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1216 + line: 1364 character: 16 end: - line: 1216 + line: 1364 character: 18 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1218 + line: 1366 character: 49 end: - line: 1218 + line: 1366 character: 51 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1225 + line: 1373 character: 16 end: - line: 1225 + line: 1373 character: 18 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1226 + line: 1374 character: 15 end: - line: 1226 + line: 1374 character: 17 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1227 + line: 1375 character: 79 end: - line: 1227 + line: 1375 character: 81 severity: WARNING message: Empty array doesn't have type annotation code: Ruby::UnannotatedEmptyCollection - range: start: - line: 1228 + line: 1376 character: 25 end: - line: 1228 + line: 1376 character: 27 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1229 + line: 1377 character: 67 end: - line: 1229 + line: 1377 character: 69 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1237 + line: 1385 character: 15 end: - line: 1237 + line: 1385 character: 17 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1242 - character: 27 + line: 1392 + character: 45 end: - line: 1242 - character: 29 + line: 1392 + character: 47 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1255 + line: 1407 character: 6 end: - line: 1255 + line: 1407 character: 8 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1255 + line: 1407 character: 48 end: - line: 1255 + line: 1407 character: 50 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1262 + line: 1414 character: 40 end: - line: 1262 + line: 1414 character: 42 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1263 + line: 1415 character: 23 end: - line: 1263 + line: 1415 character: 25 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1264 + line: 1416 character: 42 end: - line: 1264 + line: 1416 character: 44 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1265 + line: 1417 character: 23 end: - line: 1265 + line: 1417 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 6806a70..131b7b8 100644 --- a/test/features_test.rb +++ b/test/features_test.rb @@ -425,10 +425,12 @@ def test_pg_encodes_bytea_json_and_string_arrays rb = generate(load_request('pg')).fetch('query_sql.rb') assert_includes rb, 'encode_bytea(payload),' assert_includes rb, 'encode_json(doc),' + assert_includes rb, 'when Array, TrueClass, FalseClass then PG::TextEncoder::JSON.new.encode(value)' assert_includes rb, 'encode_array(bio_types)' assert_includes rb, 'def encode_array(value, kind = :value)' noprep = generate(load_request('pg', preparedStatements: false)).fetch('query_sql.rb') assert_includes noprep, 'encode_array(bio_types)' + assert_includes noprep, 'encode_json(doc)' end def test_transactions_finish_on_non_local_exits @@ -437,6 +439,32 @@ def test_transactions_finish_on_non_local_exits end end + def test_legacy_timeout_guard_is_emitted_only_for_pre_33_targets + %w[pg mysql2 sqlite3].each do |driver| + legacy = generate(load_request(driver, rubyVersion: '3.1', generateGemfile: true)) + rb = legacy.fetch('query_sql.rb') + assert_includes rb, 'completed = false' + assert_includes rb, '!completed && timeout_exits_with_throw?' + assert_includes rb, 'def timeout_exits_with_throw?' + assert_includes legacy.fetch('query_sql.rbs'), 'def timeout_exits_with_throw?: () -> bool' + assert_includes legacy.fetch('Gemfile'), "gem 'timeout', '>= 0.4'" + + modern = generate(load_request(driver, rubyVersion: '3.3')).fetch('query_sql.rb') + refute_includes modern, 'completed = false' + refute_includes modern, 'timeout_exits_with_throw?' + refute_includes modern, 'On Ruby <3.3 with timeout <0.4' + end + end + + def test_legacy_timeout_helper_name_is_reserved + request = load_request('pg', rubyVersion: '3.1') + request.queries << SqlcGenRuby::Plugin::Query.new( + name: 'TimeoutExitsWithThrow?', cmd: ':exec', text: 'SELECT 1', filename: 'query.sql' + ) + error = assert_raises(RuntimeError) { generate(request) } + assert_match(/method timeout_exits_with_throw\? is already/, error.message) + end + def test_parameters_named_like_generated_locals_are_renamed request = load_request('pg') query(request, 'GetAuthor').params.first.column.name = 'client' @@ -545,6 +573,51 @@ def test_query_method_colliding_with_a_generated_method_is_an_error assert_raises(RuntimeError) { generate(request) } end + def test_required_keywords_precede_optional_keywords + %w[pg mysql2 sqlite3].each do |driver| + request = load_request(driver) + optional = query(request, 'GetAuthorByNamePattern').params.first.dup + required = query(request, 'GetAuthor').params.first.dup + optional.number = 1 + required.number = 2 + request.queries << SqlcGenRuby::Plugin::Query.new( + name: 'MixedKeywordOrder', cmd: ':exec', text: 'SELECT 1 WHERE ? AND ?', + filename: 'query.sql', params: [optional, required] + ) + + files = generate(request) + assert_includes files.fetch('query_sql.rb'), 'def mixed_keyword_order(id:, name_pattern: nil)' + assert_match(/def mixed_keyword_order: \(id: Integer, \?name_pattern: String\??\) -> void/, + files.fetch('query_sql.rbs')) + end + end + + def test_kernel_methods_used_by_generated_code_are_reserved + %w[pg mysql2 sqlite3].each do |driver| + %w[Raise Rand Format].each do |name| + request = load_request(driver) + request.queries << SqlcGenRuby::Plugin::Query.new( + name: name, cmd: ':exec', text: 'SELECT 1', filename: 'query.sql' + ) + error = assert_raises(RuntimeError) { generate(request) } + assert_match(/query #{name}: method #{name.downcase} is already a Kernel method/, error.message) + end + end + end + + def test_bare_kernel_calls_in_generated_code_are_reserved + paths = Dir.glob(File.join(PluginHelper::ROOT, 'examples/*/query_sql.rb')) + + Dir.glob(File.join(PluginHelper::ROOT, 'test/golden/**/query_sql.rb')) + calls = paths.flat_map do |path| + source = File.read(path).gsub(/<<~'SQL'\n.*?^[ \t]*SQL[ \t]*$/m, '') + source = source.lines.reject { |line| line.lstrip.start_with?('#') }.join + methods = source.scan(/^\s*def (?:self\.)?([\w?!]+)/).flatten + source.scan(/(?SELECT id, `order`, note, payload FROM regressions ORDER BY idListRegressionRows:many". +id0ÿÿÿÿÿÿÿÿÿR  regressionsbbigintzid"- +order0ÿR  regressionsb varcharzorder". +note0ÿÿÿÿÿÿÿÿÿR  regressionsbtextznote"0 payload0R  regressionsb  varbinaryzpayload: query.sqlC TRUNCATE TABLE regressionsTruncateRegressions:exec: query.sql  LINSERT INTO copy_types (id, code, doc, flags, amount) VALUES (?, ?, ?, ?, ?) CopyCopyTypes :copyfrom*95 diff --git a/test/fixtures/pg.bin b/test/fixtures/pg.bin index 762b6bc..6998b15 100644 --- a/test/fixtures/pg.bin +++ b/test/fixtures/pg.bin @@ -1,7 +1,7 @@  2 -postgresqlËpublic"‰publicÍ +postgresql�!public"ÎpublicÍ authors) id0ÿÿÿÿÿÿÿÿÿR authorsb  bigserial& name0ÿÿÿÿÿÿÿÿÿR authorsbtextH @@ -44,7 +44,11 @@ pg_catalogvarchar4 name0ÿÿÿÿÿÿÿÿÿRbooksbtext5 author_id0ÿÿÿÿÿÿÿÿÿRbooksb pg_catalogint8) - description0ÿÿÿÿÿÿÿÿÿRbooksbtext‰ + description0ÿÿÿÿÿÿÿÿÿRbooksbtext„ +deferred_books0 +id0ÿÿÿÿÿÿÿÿÿRdeferred_booksb  bigserial> + author_id0ÿÿÿÿÿÿÿÿÿRdeferred_booksb +pg_catalogint8‰ postgres_extra_types3 c_uuid0ÿÿÿÿÿÿÿÿÿRpostgres_extra_typesbuuid5 c_jsonb0ÿÿÿÿÿÿÿÿÿRpostgres_extra_typesbjsonb> @@ -56,7 +60,13 @@ pg_catalog timestamptz3 c_inet0ÿÿÿÿÿÿÿÿÿRpostgres_extra_typesbinet5 c_money0ÿÿÿÿÿÿÿÿÿRpostgres_extra_typesbmoney9 -c_not_null0ÿÿÿÿÿÿÿÿÿRpostgres_extra_typesbtextú +c_not_null0ÿÿÿÿÿÿÿÿÿRpostgres_extra_typesbtext» +json_round_trips9 +id0ÿÿÿÿÿÿÿÿÿRjson_round_tripsb +pg_catalogint43 + +json_value0ÿÿÿÿÿÿÿÿÿRjson_round_tripsbjson5 + jsonb_value0ÿÿÿÿÿÿÿÿÿRjson_round_tripsbjsonbú postgres_array_types> c_uuid_array 0ÿÿÿÿÿÿÿÿÿRpostgres_array_typesbuuidˆP c_bio_type_array 0ÿÿÿÿÿÿÿÿÿRpostgres_array_typesbextendedbio_typeˆN @@ -163,7 +173,29 @@ pg_catalogbpcharzc_char"O pg_catalogvarcharz c_varchar"= c_bytea0ÿÿÿÿÿÿÿÿÿRnode_postgres_typesbbyteazc_bytea": c_text0ÿÿÿÿÿÿÿÿÿRnode_postgres_typesbtextzc_text": -c_json0ÿÿÿÿÿÿÿÿÿRnode_postgres_typesbjsonzc_json: query.sql¾ +c_json0ÿÿÿÿÿÿÿÿÿRnode_postgres_typesbjsonzc_json: query.sqlï +NINSERT INTO json_round_trips (id, json_value, jsonb_value) VALUES ($1, $2, $3)SetJSONRoundTrip:exec*HD +id0ÿÿÿÿÿÿÿÿÿRpublicjson_round_tripsbpg_catalog.int4zid*KG + +json_value0ÿÿÿÿÿÿÿÿÿRpublicjson_round_tripsbjsonz +json_value*NJ + jsonb_value0ÿÿÿÿÿÿÿÿÿRpublicjson_round_tripsbjsonbz jsonb_value: query.sqlBjson_round_trips® +BSELECT json_value, jsonb_value FROM json_round_trips WHERE id = $1GetJSONRoundTrip:one"? + +json_value0ÿÿÿÿÿÿÿÿÿRjson_round_tripsbjsonz +json_value"B + jsonb_value0ÿÿÿÿÿÿÿÿÿRjson_round_tripsbjsonbz jsonb_value*@< +id0ÿÿÿÿÿÿÿÿÿRjson_round_tripsbpg_catalog.int4zid: query.sqlÑ +eUPDATE json_round_trips +SET jsonb_value = to_jsonb(NOT (jsonb_value #>> '{}')::boolean) +WHERE id = $1ToggleJSONBRoundTrip:exec*@< +id0ÿÿÿÿÿÿÿÿÿRjson_round_tripsbpg_catalog.int4zid: query.sqlK +TRUNCATE TABLE json_round_tripsTruncateJSONRoundTrips:exec: query.sqlš +%SELECT pg_sleep($1::double precision) +PauseQuery:exec"! +pg_sleep0ÿÿÿÿÿÿÿÿÿ@bvoid*0, +seconds0ÿÿÿÿÿÿÿÿÿb +pg_catalogfloat8: query.sql¾ BSELECT id, name, bio FROM authors ORDER BY name LIMIT $2 @@ -178,7 +210,16 @@ WHERE name LIKE COALESCE($1, '%')GetAuthorByNamePattern:many"- id0ÿÿÿÿÿÿÿÿÿR authorsb  bigserialzid", name0ÿÿÿÿÿÿÿÿÿR authorsbtextzname"( bio0ÿÿÿÿÿÿÿÿÿR authorsbtextzbio*84 - name_pattern0ÿÿÿÿÿÿÿÿÿ8R authorsbtextzname: query.sql– + name_pattern0ÿÿÿÿÿÿÿÿÿ8R authorsbtextzname: query.sqlâ +bSELECT id, name, bio FROM authors +WHERE ($1::text IS NULL OR name = $1::text) +ORDER BY id +LIMIT $2GetAuthorsByOptionalName:many"- +id0ÿÿÿÿÿÿÿÿÿR authorsb  bigserialzid", +name0ÿÿÿÿÿÿÿÿÿR authorsbtextzname"( +bio0ÿÿÿÿÿÿÿÿÿR authorsbtextzbio*$ + name_filter0ÿÿÿÿÿÿÿÿÿbtext*# +lim0ÿÿÿÿÿÿÿÿÿ8b integer: query.sql– B=EPij%jH+If{h+K7q@t<$QOaz2)N$Jz$M| z3W($*?_WsBX{1z9X;oEGxq_Nn1EX0ar<0&uStwRe{w^|fpejpB4apf8TEuRy?QUAO zlkb#Uu3blyCwswDy?x1ZNd9+a z!?JN-X?$CbBXJhR<(u*TX5siE7S5YoGPbe4Y-)A9X_-2 z1u{M%jE>gqcC)Uz_#)U=WO$_khI(oo6XRAwPC~G(wH>^2f+2XbcpKMaVc`88?sMh* VnJc4oR;LLehWw00y6Y3)*KCq2eBFE0s*Cx1EvfbRSE_g m1ax6%a}pzy@dXr<-z^BU`vVRJvtbL71hXX)@f5S4D)$5CwHiDC diff --git a/test/golden/mysql2/default/query_sql.rb b/test/golden/mysql2/default/query_sql.rb index d49bcfa..690c3a0 100644 --- a/test/golden/mysql2/default/query_sql.rb +++ b/test/golden/mysql2/default/query_sql.rb @@ -114,6 +114,23 @@ class ListAuthorsPagedRow < Data.define(:id, :name, :bio) class GetAuthorByNamePatternRow < Data.define(:id, :name, :bio) end + GET_AUTHORS_BY_OPTIONAL_NAME_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE (? IS NULL OR name = ?) + ORDER BY id + LIMIT ? + SQL + + class GetAuthorsByOptionalNameRow < Data.define(:id, :name, :bio) + end + + PAUSE_QUERY_SQL = <<~'SQL' + SELECT SLEEP(?) + SQL + + class PauseQueryRow < Data.define(:sleep) + end + UPDATE_AUTHOR_BIOS_SQL = <<~'SQL' UPDATE authors SET bio = ? @@ -245,6 +262,13 @@ class CopyRegressionRowsArgs < Data.define(:id, :order, :note, :payload) class ListRegressionPayloadsRow < Data.define(:id, :payload) end + LIST_REGRESSION_ROWS_SQL = <<~'SQL' + SELECT id, `order`, note, payload FROM regressions ORDER BY id + SQL + + class ListRegressionRowsRow < Data.define(:id, :order, :note, :payload) + end + TRUNCATE_REGRESSIONS_SQL = <<~'SQL' TRUNCATE TABLE regressions SQL @@ -559,6 +583,34 @@ def get_author_by_name_pattern(name_pattern: nil) end end + def get_authors_by_optional_name(limit:, name_filter: nil) + with_client('get_authors_by_optional_name') do |client| + query_params = [name_filter, name_filter, limit] + stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) + begin + result = stmt.execute(*query_params, as: :array) + result.map do |row| + GetAuthorsByOptionalNameRow.new(row[0], row[1], row[2]) + end + ensure + stmt.close + end + end + end + + def pause_query(seconds:) + with_client('pause_query') do |client| + query_params = [seconds] + stmt = client.prepare(PAUSE_QUERY_SQL) + begin + stmt.execute(*query_params, as: :array) + nil + ensure + stmt.close + end + end + end + # Sets the bio of every author that has one and returns how many were changed. def update_author_bios(bio: nil) with_client('update_author_bios') do |client| @@ -876,6 +928,20 @@ def list_regression_payloads end end + def list_regression_rows + with_client('list_regression_rows') do |client| + stmt = client.prepare(LIST_REGRESSION_ROWS_SQL) + begin + result = stmt.execute(as: :array) + result.map do |row| + ListRegressionRowsRow.new(row[0], row[1], row[2], row[3]) + end + ensure + stmt.close + end + end + end + def truncate_regressions with_client('truncate_regressions') do |client| stmt = client.prepare(TRUNCATE_REGRESSIONS_SQL) diff --git a/test/golden/mysql2/default/query_sql.rbs b/test/golden/mysql2/default/query_sql.rbs index b734145..3064f05 100644 --- a/test/golden/mysql2/default/query_sql.rbs +++ b/test/golden/mysql2/default/query_sql.rbs @@ -125,6 +125,22 @@ module Mysql2Codegen def initialize: ( Integer id, String name, String? bio) -> void end + GET_AUTHORS_BY_OPTIONAL_NAME_SQL: String + + class GetAuthorsByOptionalNameRow + attr_reader id: Integer + attr_reader name: String + attr_reader bio: String? + def initialize: ( Integer id, String name, String? bio) -> void + end + + PAUSE_QUERY_SQL: String + + class PauseQueryRow + attr_reader sleep: Integer + def initialize: ( Integer sleep) -> void + end + UPDATE_AUTHOR_BIOS_SQL: String GET_AUTHORS_BY_IDS_SQL: String @@ -271,6 +287,16 @@ module Mysql2Codegen def initialize: ( Integer id, String? payload) -> void end + LIST_REGRESSION_ROWS_SQL: String + + class ListRegressionRowsRow + attr_reader id: Integer + attr_reader order: String + attr_reader note: String? + attr_reader payload: String? + def initialize: ( Integer id, String order, String? note, String? payload) -> void + end + TRUNCATE_REGRESSIONS_SQL: String class CopyCopyTypesArgs @@ -415,6 +441,10 @@ module Mysql2Codegen def get_author_by_name_pattern: (?name_pattern: String?) -> Array[GetAuthorByNamePatternRow] + def get_authors_by_optional_name: (limit: Integer, ?name_filter: untyped) -> Array[GetAuthorsByOptionalNameRow] + + def pause_query: (seconds: Integer) -> void + def update_author_bios: (?bio: String?) -> Integer def get_authors_by_ids: (ids: Array[Integer]) -> Array[GetAuthorsByIdsRow] @@ -461,6 +491,8 @@ module Mysql2Codegen def list_regression_payloads: -> Array[ListRegressionPayloadsRow] + def list_regression_rows: -> Array[ListRegressionRowsRow] + def truncate_regressions: -> void def copy_copy_types: (Array[CopyCopyTypesArgs] rows) -> void diff --git a/test/golden/mysql2/ruby31_gemfile/Gemfile b/test/golden/mysql2/ruby31_gemfile/Gemfile index ba8c9e9..6e1d56d 100644 --- a/test/golden/mysql2/ruby31_gemfile/Gemfile +++ b/test/golden/mysql2/ruby31_gemfile/Gemfile @@ -4,3 +4,4 @@ source 'https://rubygems.org' gem 'connection_pool' gem 'mysql2' +gem 'timeout', '>= 0.4' diff --git a/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rb b/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rb index 7e3b3a8..dbb6bac 100644 --- a/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rb +++ b/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rb @@ -47,8 +47,11 @@ def transaction(isolation: nil) end rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false + completed = false begin - yield self + result = yield self + completed = true + result rescue Exception # rubocop:disable Lint/RescueException failed = true client.query(rollback) @@ -57,7 +60,7 @@ def transaction(isolation: nil) begin unless failed commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" - client.query(Thread.current.status == 'aborting' ? rollback : commit) + client.query(Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit) end ensure client.in_transaction = false if outermost @@ -107,5 +110,10 @@ def with_client(name) end end end + + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end end end diff --git a/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rbs b/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rbs index 31a9283..3d6482f 100644 --- a/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rbs +++ b/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rbs @@ -19,5 +19,7 @@ module Mysql2Codegen def instrument: [T] (String name) { () -> T } -> T def with_client: [T] (String name) { (untyped) -> T } -> T + + def timeout_exits_with_throw?: () -> bool end end diff --git a/test/golden/mysql2/ruby31_gemfile/other_sql.rb b/test/golden/mysql2/ruby31_gemfile/other_sql.rb index c55138e..b784a0b 100644 --- a/test/golden/mysql2/ruby31_gemfile/other_sql.rb +++ b/test/golden/mysql2/ruby31_gemfile/other_sql.rb @@ -59,8 +59,11 @@ def transaction(isolation: nil) end rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false + completed = false begin - yield self + result = yield self + completed = true + result rescue Exception # rubocop:disable Lint/RescueException failed = true client.query(rollback) @@ -69,7 +72,7 @@ def transaction(isolation: nil) begin unless failed commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" - client.query(Thread.current.status == 'aborting' ? rollback : commit) + client.query(Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit) end ensure client.in_transaction = false if outermost @@ -156,5 +159,10 @@ def with_client(name) end end end + + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end end end diff --git a/test/golden/mysql2/ruby31_gemfile/other_sql.rbs b/test/golden/mysql2/ruby31_gemfile/other_sql.rbs index 118f7bf..0b0621e 100644 --- a/test/golden/mysql2/ruby31_gemfile/other_sql.rbs +++ b/test/golden/mysql2/ruby31_gemfile/other_sql.rbs @@ -35,5 +35,7 @@ module Mysql2Codegen def instrument: [T] (String name) { () -> T } -> T def with_client: [T] (String name) { (untyped) -> T } -> T + + def timeout_exits_with_throw?: () -> bool end end diff --git a/test/golden/mysql2/ruby31_gemfile/query_sql.rb b/test/golden/mysql2/ruby31_gemfile/query_sql.rb index da336be..7f13d00 100644 --- a/test/golden/mysql2/ruby31_gemfile/query_sql.rb +++ b/test/golden/mysql2/ruby31_gemfile/query_sql.rb @@ -110,6 +110,21 @@ module Mysql2Codegen GetAuthorByNamePatternRow = Struct.new(:id, :name, :bio, keyword_init: true) + GET_AUTHORS_BY_OPTIONAL_NAME_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE (? IS NULL OR name = ?) + ORDER BY id + LIMIT ? + SQL + + GetAuthorsByOptionalNameRow = Struct.new(:id, :name, :bio, keyword_init: true) + + PAUSE_QUERY_SQL = <<~'SQL' + SELECT SLEEP(?) + SQL + + PauseQueryRow = Struct.new(:sleep, keyword_init: true) + UPDATE_AUTHOR_BIOS_SQL = <<~'SQL' UPDATE authors SET bio = ? @@ -242,6 +257,18 @@ module Mysql2Codegen ListRegressionPayloadsRow = Struct.new(:id, :payload, keyword_init: true) + LIST_REGRESSION_ROWS_SQL = <<~'SQL' + SELECT id, `order`, note, payload FROM regressions ORDER BY id + SQL + + ListRegressionRowsRow = Struct.new( + :id, + :order, + :note, + :payload, + keyword_init: true + ) + TRUNCATE_REGRESSIONS_SQL = <<~'SQL' TRUNCATE TABLE regressions SQL @@ -367,8 +394,11 @@ def transaction(isolation: nil) end rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false + completed = false begin - yield self + result = yield self + completed = true + result rescue Exception # rubocop:disable Lint/RescueException failed = true client.query(rollback) @@ -377,7 +407,7 @@ def transaction(isolation: nil) begin unless failed commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" - client.query(Thread.current.status == 'aborting' ? rollback : commit) + client.query(Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit) end ensure client.in_transaction = false if outermost @@ -560,6 +590,34 @@ def get_author_by_name_pattern(name_pattern: nil) end end + def get_authors_by_optional_name(limit:, name_filter: nil) + with_client('get_authors_by_optional_name') do |client| + query_params = [name_filter, name_filter, limit] + stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) + begin + result = stmt.execute(*query_params, as: :array) + result.map do |row| + GetAuthorsByOptionalNameRow.new(id: row[0], name: row[1], bio: row[2]) + end + ensure + stmt.close + end + end + end + + def pause_query(seconds:) + with_client('pause_query') do |client| + query_params = [seconds] + stmt = client.prepare(PAUSE_QUERY_SQL) + begin + stmt.execute(*query_params, as: :array) + nil + ensure + stmt.close + end + end + end + # Sets the bio of every author that has one and returns how many were changed. def update_author_bios(bio: nil) with_client('update_author_bios') do |client| @@ -890,6 +948,20 @@ def list_regression_payloads end end + def list_regression_rows + with_client('list_regression_rows') do |client| + stmt = client.prepare(LIST_REGRESSION_ROWS_SQL) + begin + result = stmt.execute(as: :array) + result.map do |row| + ListRegressionRowsRow.new(id: row[0], order: row[1], note: row[2], payload: row[3]) + end + ensure + stmt.close + end + end + end + def truncate_regressions with_client('truncate_regressions') do |client| stmt = client.prepare(TRUNCATE_REGRESSIONS_SQL) @@ -1099,6 +1171,11 @@ def with_client(name) end end + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end + # Formats one row for LOAD DATA ... FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\'. def csv_line(values) fields = values.map do |value| diff --git a/test/golden/mysql2/ruby31_gemfile/query_sql.rbs b/test/golden/mysql2/ruby31_gemfile/query_sql.rbs index e5c05c5..35e5757 100644 --- a/test/golden/mysql2/ruby31_gemfile/query_sql.rbs +++ b/test/golden/mysql2/ruby31_gemfile/query_sql.rbs @@ -168,6 +168,24 @@ module Mysql2Codegen def self.new: ( id: Integer, name: String, bio: String?) -> GetAuthorByNamePatternRow end + GET_AUTHORS_BY_OPTIONAL_NAME_SQL: String + + class GetAuthorsByOptionalNameRow < ::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?) -> GetAuthorsByOptionalNameRow + end + + PAUSE_QUERY_SQL: String + + class PauseQueryRow < ::Struct[untyped] + attr_accessor sleep: Integer + def initialize: ( sleep: Integer) -> void + def self.new: ( sleep: Integer) -> PauseQueryRow + end + UPDATE_AUTHOR_BIOS_SQL: String GET_AUTHORS_BY_IDS_SQL: String @@ -331,6 +349,17 @@ module Mysql2Codegen def self.new: ( id: Integer, payload: String?) -> ListRegressionPayloadsRow end + LIST_REGRESSION_ROWS_SQL: String + + class ListRegressionRowsRow < ::Struct[untyped] + attr_accessor id: Integer + attr_accessor order: String + attr_accessor note: String? + attr_accessor payload: String? + def initialize: ( id: Integer, order: String, note: String?, payload: String?) -> void + def self.new: ( id: Integer, order: String, note: String?, payload: String?) -> ListRegressionRowsRow + end + TRUNCATE_REGRESSIONS_SQL: String class CopyCopyTypesArgs < ::Struct[untyped] @@ -504,6 +533,10 @@ module Mysql2Codegen def get_author_by_name_pattern: (?name_pattern: String?) -> Array[GetAuthorByNamePatternRow] + def get_authors_by_optional_name: (limit: Integer, ?name_filter: untyped) -> Array[GetAuthorsByOptionalNameRow] + + def pause_query: (seconds: Integer) -> void + def update_author_bios: (?bio: String?) -> Integer def get_authors_by_ids: (ids: Array[Integer]) -> Array[GetAuthorsByIdsRow] @@ -550,6 +583,8 @@ module Mysql2Codegen def list_regression_payloads: -> Array[ListRegressionPayloadsRow] + def list_regression_rows: -> Array[ListRegressionRowsRow] + def truncate_regressions: -> void def copy_copy_types: (Array[CopyCopyTypesArgs] rows) -> void @@ -580,6 +615,8 @@ module Mysql2Codegen def with_client: [T] (String name) { (untyped) -> T } -> T + def timeout_exits_with_throw?: () -> bool + def csv_line: (Array[untyped] values) -> String def load_data: (untyped client, String sql, ?strict: bool) -> void diff --git a/test/golden/mysql2/ruby32_notypes/my_queries_sql.rb b/test/golden/mysql2/ruby32_notypes/my_queries_sql.rb index 7e3b3a8..dbb6bac 100644 --- a/test/golden/mysql2/ruby32_notypes/my_queries_sql.rb +++ b/test/golden/mysql2/ruby32_notypes/my_queries_sql.rb @@ -47,8 +47,11 @@ def transaction(isolation: nil) end rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false + completed = false begin - yield self + result = yield self + completed = true + result rescue Exception # rubocop:disable Lint/RescueException failed = true client.query(rollback) @@ -57,7 +60,7 @@ def transaction(isolation: nil) begin unless failed commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" - client.query(Thread.current.status == 'aborting' ? rollback : commit) + client.query(Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit) end ensure client.in_transaction = false if outermost @@ -107,5 +110,10 @@ def with_client(name) end end end + + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end end end diff --git a/test/golden/mysql2/ruby32_notypes/other_sql.rb b/test/golden/mysql2/ruby32_notypes/other_sql.rb index c067da1..30684c1 100644 --- a/test/golden/mysql2/ruby32_notypes/other_sql.rb +++ b/test/golden/mysql2/ruby32_notypes/other_sql.rb @@ -60,8 +60,11 @@ def transaction(isolation: nil) end rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false + completed = false begin - yield self + result = yield self + completed = true + result rescue Exception # rubocop:disable Lint/RescueException failed = true client.query(rollback) @@ -70,7 +73,7 @@ def transaction(isolation: nil) begin unless failed commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" - client.query(Thread.current.status == 'aborting' ? rollback : commit) + client.query(Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit) end ensure client.in_transaction = false if outermost @@ -157,5 +160,10 @@ def with_client(name) end end end + + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end end end diff --git a/test/golden/mysql2/ruby32_notypes/query_sql.rb b/test/golden/mysql2/ruby32_notypes/query_sql.rb index d49bcfa..1bd62eb 100644 --- a/test/golden/mysql2/ruby32_notypes/query_sql.rb +++ b/test/golden/mysql2/ruby32_notypes/query_sql.rb @@ -114,6 +114,23 @@ class ListAuthorsPagedRow < Data.define(:id, :name, :bio) class GetAuthorByNamePatternRow < Data.define(:id, :name, :bio) end + GET_AUTHORS_BY_OPTIONAL_NAME_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE (? IS NULL OR name = ?) + ORDER BY id + LIMIT ? + SQL + + class GetAuthorsByOptionalNameRow < Data.define(:id, :name, :bio) + end + + PAUSE_QUERY_SQL = <<~'SQL' + SELECT SLEEP(?) + SQL + + class PauseQueryRow < Data.define(:sleep) + end + UPDATE_AUTHOR_BIOS_SQL = <<~'SQL' UPDATE authors SET bio = ? @@ -245,6 +262,13 @@ class CopyRegressionRowsArgs < Data.define(:id, :order, :note, :payload) class ListRegressionPayloadsRow < Data.define(:id, :payload) end + LIST_REGRESSION_ROWS_SQL = <<~'SQL' + SELECT id, `order`, note, payload FROM regressions ORDER BY id + SQL + + class ListRegressionRowsRow < Data.define(:id, :order, :note, :payload) + end + TRUNCATE_REGRESSIONS_SQL = <<~'SQL' TRUNCATE TABLE regressions SQL @@ -365,8 +389,11 @@ def transaction(isolation: nil) end rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false + completed = false begin - yield self + result = yield self + completed = true + result rescue Exception # rubocop:disable Lint/RescueException failed = true client.query(rollback) @@ -375,7 +402,7 @@ def transaction(isolation: nil) begin unless failed commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" - client.query(Thread.current.status == 'aborting' ? rollback : commit) + client.query(Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit) end ensure client.in_transaction = false if outermost @@ -559,6 +586,34 @@ def get_author_by_name_pattern(name_pattern: nil) end end + def get_authors_by_optional_name(limit:, name_filter: nil) + with_client('get_authors_by_optional_name') do |client| + query_params = [name_filter, name_filter, limit] + stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) + begin + result = stmt.execute(*query_params, as: :array) + result.map do |row| + GetAuthorsByOptionalNameRow.new(row[0], row[1], row[2]) + end + ensure + stmt.close + end + end + end + + def pause_query(seconds:) + with_client('pause_query') do |client| + query_params = [seconds] + stmt = client.prepare(PAUSE_QUERY_SQL) + begin + stmt.execute(*query_params, as: :array) + nil + ensure + stmt.close + end + end + end + # Sets the bio of every author that has one and returns how many were changed. def update_author_bios(bio: nil) with_client('update_author_bios') do |client| @@ -876,6 +931,20 @@ def list_regression_payloads end end + def list_regression_rows + with_client('list_regression_rows') do |client| + stmt = client.prepare(LIST_REGRESSION_ROWS_SQL) + begin + result = stmt.execute(as: :array) + result.map do |row| + ListRegressionRowsRow.new(row[0], row[1], row[2], row[3]) + end + ensure + stmt.close + end + end + end + def truncate_regressions with_client('truncate_regressions') do |client| stmt = client.prepare(TRUNCATE_REGRESSIONS_SQL) @@ -1085,6 +1154,11 @@ def with_client(name) end end + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end + # Formats one row for LOAD DATA ... FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\'. def csv_line(values) fields = values.map do |value| diff --git a/test/golden/pg/default/models.rb b/test/golden/pg/default/models.rb index b9a3073..6b0fdb0 100644 --- a/test/golden/pg/default/models.rb +++ b/test/golden/pg/default/models.rb @@ -40,6 +40,9 @@ class NodePostgresType < Data.define( class Book < Data.define(:id, :name, :author_id, :description) end + class DeferredBook < Data.define(:id, :author_id) + end + class PostgresExtraType < Data.define( :c_uuid, :c_jsonb, @@ -52,6 +55,9 @@ class PostgresExtraType < Data.define( ) end + class JsonRoundTrip < Data.define(:id, :json_value, :jsonb_value) + end + class PostgresArrayType < Data.define(:c_uuid_array, :c_bio_type_array, :c_integer_matrix) end diff --git a/test/golden/pg/default/models.rbs b/test/golden/pg/default/models.rbs index edc0520..e5685fd 100644 --- a/test/golden/pg/default/models.rbs +++ b/test/golden/pg/default/models.rbs @@ -66,6 +66,12 @@ module PgCodegen def initialize: ( String id, String name, Integer author_id, String? description) -> void end + class DeferredBook + attr_reader id: Integer + attr_reader author_id: Integer + def initialize: ( Integer id, Integer author_id) -> void + end + class PostgresExtraType attr_reader c_uuid: String? attr_reader c_jsonb: untyped @@ -87,6 +93,13 @@ module PgCodegen ) -> void end + class JsonRoundTrip + attr_reader id: Integer + attr_reader json_value: untyped + attr_reader jsonb_value: untyped + def initialize: ( Integer id, untyped json_value, untyped jsonb_value) -> void + end + class PostgresArrayType attr_reader c_uuid_array: Array[String]? attr_reader c_bio_type_array: Array[String]? diff --git a/test/golden/pg/default/query_sql.rb b/test/golden/pg/default/query_sql.rb index 462945e..6dec461 100644 --- a/test/golden/pg/default/query_sql.rb +++ b/test/golden/pg/default/query_sql.rb @@ -67,6 +67,34 @@ class TestRow < Data.define( ) end + SET_JSON_ROUND_TRIP_SQL = <<~'SQL' + INSERT INTO json_round_trips (id, json_value, jsonb_value) VALUES ($1, $2, $3) + SQL + + GET_JSON_ROUND_TRIP_SQL = <<~'SQL' + SELECT json_value, jsonb_value FROM json_round_trips WHERE id = $1 + SQL + + class GetJSONRoundTripRow < Data.define(:json_value, :jsonb_value) + end + + TOGGLE_JSONB_ROUND_TRIP_SQL = <<~'SQL' + UPDATE json_round_trips + SET jsonb_value = to_jsonb(NOT (jsonb_value #>> '{}')::boolean) + WHERE id = $1 + SQL + + TRUNCATE_JSON_ROUND_TRIPS_SQL = <<~'SQL' + TRUNCATE TABLE json_round_trips + SQL + + PAUSE_QUERY_SQL = <<~'SQL' + SELECT pg_sleep($1::double precision) + SQL + + class PauseQueryRow < Data.define(:pg_sleep) + end + LIST_AUTHORS_PAGED_SQL = <<~'SQL' SELECT id, name, bio FROM authors ORDER BY name @@ -85,6 +113,16 @@ class ListAuthorsPagedRow < Data.define(:id, :name, :bio) class GetAuthorByNamePatternRow < Data.define(:id, :name, :bio) end + GET_AUTHORS_BY_OPTIONAL_NAME_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE ($1::text IS NULL OR name = $1::text) + ORDER BY id + LIMIT $2 + SQL + + class GetAuthorsByOptionalNameRow < Data.define(:id, :name, :bio) + end + CREATE_AUTHOR_RETURN_ID_SQL = <<~'SQL' INSERT INTO authors (name, bio) VALUES ($1, $2) RETURNING id SQL @@ -128,6 +166,14 @@ class CopyAuthorsArgs < Data.define(:name, :bio) class CreateBookRow < Data.define(:id) end + CREATE_DEFERRED_BOOK_SQL = <<~'SQL' + INSERT INTO deferred_books (author_id) VALUES ($1) + SQL + + TRUNCATE_DEFERRED_BOOKS_SQL = <<~'SQL' + TRUNCATE TABLE deferred_books + SQL + LIST_ALL_AUTHORS_BOOKS_SQL = <<~'SQL' SELECT authors.id, authors.name, authors.bio, @@ -581,6 +627,71 @@ def test!(...) test(...) || raise(NotFound, 'Test returned no row') end + 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') + end + client.exec_prepared('query_sql_set_json_round_trip', query_params) + nil + end + end + + 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') + end + result = client.exec_prepared('query_sql_get_json_round_trip', query_params) + row = result.first + row && GetJSONRoundTripRow.new(row['json_value'], row['jsonb_value']) + end + end + + # Like #get_json_round_trip, but raises NotFound when there is no row. + def get_json_round_trip!(...) + get_json_round_trip(...) || raise(NotFound, 'GetJSONRoundTrip returned no row') + end + + 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') + end + client.exec_prepared('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') + end + client.exec_prepared('query_sql_truncate_json_round_trips') + nil + end + end + + 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') + end + client.exec_prepared('query_sql_pause_query', query_params) + nil + end + end + def list_authors_paged(offset:, limit:) with_client('list_authors_paged') do |client| query_params = [offset, limit] @@ -609,6 +720,20 @@ def get_author_by_name_pattern(name_pattern: nil) end end + 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') + end + result = client.exec_prepared('query_sql_get_authors_by_optional_name', query_params) + result.map do |row| + GetAuthorsByOptionalNameRow.new(row['id'], row['name'], row['bio']) + end + end + end + def create_author_return_id(name:, bio:) with_client('create_author_return_id') do |client| query_params = [name, bio] @@ -700,6 +825,29 @@ def create_book(name:, author_id:) end end + 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') + end + client.exec_prepared('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') + end + client.exec_prepared('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') @@ -1324,9 +1472,13 @@ def encode_bytea(value) value && PG::TextEncoder::Bytea.new.encode(value) end - # json / jsonb parameters: an Array is a JSON array, not a PG array (Hashes and JSON text pass through). + # json / jsonb parameters: booleans and Arrays need JSON encoding, not PostgreSQL's boolean / array encoding. + # Hashes and JSON text pass through; nil stays nil. def encode_json(value) - value.is_a?(Array) ? PG::TextEncoder::JSON.new.encode(value) : value + case value + when Array, TrueClass, FalseClass then PG::TextEncoder::JSON.new.encode(value) + else value + end end # Float#to_s is the shortest text that reads back as the same Float (the pg gem's encoder drops digits). diff --git a/test/golden/pg/default/query_sql.rbs b/test/golden/pg/default/query_sql.rbs index dd1cc04..3f045f5 100644 --- a/test/golden/pg/default/query_sql.rbs +++ b/test/golden/pg/default/query_sql.rbs @@ -72,6 +72,27 @@ module PgCodegen ) -> void end + SET_JSON_ROUND_TRIP_SQL: String + + GET_JSON_ROUND_TRIP_SQL: String + + class GetJSONRoundTripRow + attr_reader json_value: untyped + attr_reader jsonb_value: untyped + def initialize: ( untyped json_value, untyped jsonb_value) -> void + end + + TOGGLE_JSONB_ROUND_TRIP_SQL: String + + TRUNCATE_JSON_ROUND_TRIPS_SQL: String + + PAUSE_QUERY_SQL: String + + class PauseQueryRow + attr_reader pg_sleep: untyped + def initialize: ( untyped pg_sleep) -> void + end + LIST_AUTHORS_PAGED_SQL: String class ListAuthorsPagedRow @@ -90,6 +111,15 @@ module PgCodegen def initialize: ( Integer id, String name, String? bio) -> void end + GET_AUTHORS_BY_OPTIONAL_NAME_SQL: String + + class GetAuthorsByOptionalNameRow + attr_reader id: Integer + attr_reader name: String + attr_reader bio: String? + def initialize: ( Integer id, String name, String? bio) -> void + end + CREATE_AUTHOR_RETURN_ID_SQL: String class CreateAuthorReturnIdRow @@ -132,6 +162,10 @@ module PgCodegen def initialize: ( String id) -> void end + CREATE_DEFERRED_BOOK_SQL: String + + TRUNCATE_DEFERRED_BOOKS_SQL: String + LIST_ALL_AUTHORS_BOOKS_SQL: String class ListAllAuthorsBooksRow @@ -499,10 +533,24 @@ module PgCodegen def test!: -> TestRow + def set_json_round_trip: (id: Integer, json_value: untyped, jsonb_value: untyped) -> void + + def get_json_round_trip: (id: Integer) -> GetJSONRoundTripRow? + + def get_json_round_trip!: (id: Integer) -> GetJSONRoundTripRow + + def toggle_jsonb_round_trip: (id: Integer) -> void + + def truncate_json_round_trips: -> void + + def pause_query: (seconds: Float) -> void + def list_authors_paged: (offset: Integer, limit: Integer) -> Array[ListAuthorsPagedRow] def get_author_by_name_pattern: (?name_pattern: String?) -> Array[GetAuthorByNamePatternRow] + def get_authors_by_optional_name: (lim: Integer, ?name_filter: String?) -> Array[GetAuthorsByOptionalNameRow] + def create_author_return_id: (name: String, bio: String?) -> Integer def update_author_bios: (bio: String?) -> Integer @@ -517,6 +565,10 @@ module PgCodegen def create_book: (name: String, author_id: Integer) -> String + def create_deferred_book: (author_id: Integer) -> void + + def truncate_deferred_books: -> void + def list_all_authors_books: -> Array[ListAllAuthorsBooksRow] def get_duplicate_authors: -> Array[GetDuplicateAuthorsRow] diff --git a/test/golden/pg/ruby31_gemfile/Gemfile b/test/golden/pg/ruby31_gemfile/Gemfile index 10a2344..8caad84 100644 --- a/test/golden/pg/ruby31_gemfile/Gemfile +++ b/test/golden/pg/ruby31_gemfile/Gemfile @@ -4,3 +4,4 @@ source 'https://rubygems.org' gem 'connection_pool' gem 'pg' +gem 'timeout', '>= 0.4' diff --git a/test/golden/pg/ruby31_gemfile/models.rb b/test/golden/pg/ruby31_gemfile/models.rb index 3912a47..487f6a5 100644 --- a/test/golden/pg/ruby31_gemfile/models.rb +++ b/test/golden/pg/ruby31_gemfile/models.rb @@ -44,6 +44,8 @@ module ExtendedBioType keyword_init: true ) + DeferredBook = Struct.new(:id, :author_id, keyword_init: true) + PostgresExtraType = Struct.new( :c_uuid, :c_jsonb, @@ -56,6 +58,8 @@ module ExtendedBioType keyword_init: true ) + JsonRoundTrip = Struct.new(:id, :json_value, :jsonb_value, keyword_init: true) + PostgresArrayType = Struct.new(:c_uuid_array, :c_bio_type_array, :c_integer_matrix, keyword_init: true) PostgresSpecialType = Struct.new( diff --git a/test/golden/pg/ruby31_gemfile/models.rbs b/test/golden/pg/ruby31_gemfile/models.rbs index e9b3bc8..f1e41a1 100644 --- a/test/golden/pg/ruby31_gemfile/models.rbs +++ b/test/golden/pg/ruby31_gemfile/models.rbs @@ -88,6 +88,13 @@ module PgCodegen def self.new: ( id: String, name: String, author_id: Integer, description: String?) -> Book end + class DeferredBook < ::Struct[untyped] + attr_accessor id: Integer + attr_accessor author_id: Integer + def initialize: ( id: Integer, author_id: Integer) -> void + def self.new: ( id: Integer, author_id: Integer) -> DeferredBook + end + class PostgresExtraType < ::Struct[untyped] attr_accessor c_uuid: String? attr_accessor c_jsonb: untyped @@ -119,6 +126,14 @@ module PgCodegen ) -> PostgresExtraType end + class JsonRoundTrip < ::Struct[untyped] + attr_accessor id: Integer + attr_accessor json_value: untyped + attr_accessor jsonb_value: untyped + def initialize: ( id: Integer, json_value: untyped, jsonb_value: untyped) -> void + def self.new: ( id: Integer, json_value: untyped, jsonb_value: untyped) -> JsonRoundTrip + end + class PostgresArrayType < ::Struct[untyped] attr_accessor c_uuid_array: Array[String]? attr_accessor c_bio_type_array: Array[String]? diff --git a/test/golden/pg/ruby31_gemfile/my_queries_sql.rb b/test/golden/pg/ruby31_gemfile/my_queries_sql.rb index 289d380..903216a 100644 --- a/test/golden/pg/ruby31_gemfile/my_queries_sql.rb +++ b/test/golden/pg/ruby31_gemfile/my_queries_sql.rb @@ -46,8 +46,11 @@ def transaction(isolation: nil) end rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false + completed = false begin - yield self + result = yield self + completed = true + result rescue Exception # rubocop:disable Lint/RescueException failed = true client.exec(rollback) @@ -57,7 +60,7 @@ def transaction(isolation: nil) # a statement failed and the block rescued the error: PostgreSQL can only roll back rescued = client.transaction_status == PG::PQTRANS_INERROR commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" - client.exec(rescued || Thread.current.status == 'aborting' ? rollback : commit) + client.exec(rescued || Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit) client.exec("RELEASE SAVEPOINT #{savepoint}") if rescued && savepoint raise PG::InFailedSqlTransaction, 'transaction rolled back: a statement in it failed' if rescued && Thread.current.status != 'aborting' end @@ -124,5 +127,10 @@ def string_arrays(client) arrays.default_type_map = strings arrays end + + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end end end diff --git a/test/golden/pg/ruby31_gemfile/my_queries_sql.rbs b/test/golden/pg/ruby31_gemfile/my_queries_sql.rbs index d9facc1..0db7220 100644 --- a/test/golden/pg/ruby31_gemfile/my_queries_sql.rbs +++ b/test/golden/pg/ruby31_gemfile/my_queries_sql.rbs @@ -23,5 +23,7 @@ module PgCodegen def configure: (untyped client) -> void def string_arrays: (untyped client) -> untyped + + def timeout_exits_with_throw?: () -> bool end end diff --git a/test/golden/pg/ruby31_gemfile/other_sql.rb b/test/golden/pg/ruby31_gemfile/other_sql.rb index 4e2cd5d..533d650 100644 --- a/test/golden/pg/ruby31_gemfile/other_sql.rb +++ b/test/golden/pg/ruby31_gemfile/other_sql.rb @@ -58,8 +58,11 @@ def transaction(isolation: nil) end rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false + completed = false begin - yield self + result = yield self + completed = true + result rescue Exception # rubocop:disable Lint/RescueException failed = true client.exec(rollback) @@ -69,7 +72,7 @@ def transaction(isolation: nil) # a statement failed and the block rescued the error: PostgreSQL can only roll back rescued = client.transaction_status == PG::PQTRANS_INERROR commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" - client.exec(rescued || Thread.current.status == 'aborting' ? rollback : commit) + client.exec(rescued || Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit) client.exec("RELEASE SAVEPOINT #{savepoint}") if rescued && savepoint raise PG::InFailedSqlTransaction, 'transaction rolled back: a statement in it failed' if rescued && Thread.current.status != 'aborting' end @@ -172,6 +175,11 @@ def string_arrays(client) arrays end + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end + # bytea parameters are sent escaped: binary Strings (NUL bytes, invalid UTF-8) are not valid text. def encode_bytea(value) value && PG::TextEncoder::Bytea.new.encode(value) diff --git a/test/golden/pg/ruby31_gemfile/other_sql.rbs b/test/golden/pg/ruby31_gemfile/other_sql.rbs index 0324878..4ebce04 100644 --- a/test/golden/pg/ruby31_gemfile/other_sql.rbs +++ b/test/golden/pg/ruby31_gemfile/other_sql.rbs @@ -40,6 +40,8 @@ module PgCodegen def string_arrays: (untyped client) -> untyped + def timeout_exits_with_throw?: () -> bool + def encode_bytea: (String? value) -> String? def encode_float: (untyped value) -> untyped diff --git a/test/golden/pg/ruby31_gemfile/query_sql.rb b/test/golden/pg/ruby31_gemfile/query_sql.rb index 8cd07dd..08c5bae 100644 --- a/test/golden/pg/ruby31_gemfile/query_sql.rb +++ b/test/golden/pg/ruby31_gemfile/query_sql.rb @@ -65,6 +65,32 @@ module PgCodegen keyword_init: true ) + SET_JSON_ROUND_TRIP_SQL = <<~'SQL' + INSERT INTO json_round_trips (id, json_value, jsonb_value) VALUES ($1, $2, $3) + SQL + + GET_JSON_ROUND_TRIP_SQL = <<~'SQL' + SELECT json_value, jsonb_value FROM json_round_trips WHERE id = $1 + SQL + + GetJSONRoundTripRow = Struct.new(:json_value, :jsonb_value, keyword_init: true) + + TOGGLE_JSONB_ROUND_TRIP_SQL = <<~'SQL' + UPDATE json_round_trips + SET jsonb_value = to_jsonb(NOT (jsonb_value #>> '{}')::boolean) + WHERE id = $1 + SQL + + TRUNCATE_JSON_ROUND_TRIPS_SQL = <<~'SQL' + TRUNCATE TABLE json_round_trips + SQL + + PAUSE_QUERY_SQL = <<~'SQL' + SELECT pg_sleep($1::double precision) + SQL + + PauseQueryRow = Struct.new(:pg_sleep, keyword_init: true) + LIST_AUTHORS_PAGED_SQL = <<~'SQL' SELECT id, name, bio FROM authors ORDER BY name @@ -81,6 +107,15 @@ module PgCodegen GetAuthorByNamePatternRow = Struct.new(:id, :name, :bio, keyword_init: true) + GET_AUTHORS_BY_OPTIONAL_NAME_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE ($1::text IS NULL OR name = $1::text) + ORDER BY id + LIMIT $2 + SQL + + GetAuthorsByOptionalNameRow = Struct.new(:id, :name, :bio, keyword_init: true) + CREATE_AUTHOR_RETURN_ID_SQL = <<~'SQL' INSERT INTO authors (name, bio) VALUES ($1, $2) RETURNING id SQL @@ -119,6 +154,14 @@ module PgCodegen CreateBookRow = Struct.new(:id, keyword_init: true) + CREATE_DEFERRED_BOOK_SQL = <<~'SQL' + INSERT INTO deferred_books (author_id) VALUES ($1) + SQL + + TRUNCATE_DEFERRED_BOOKS_SQL = <<~'SQL' + TRUNCATE TABLE deferred_books + SQL + LIST_ALL_AUTHORS_BOOKS_SQL = <<~'SQL' SELECT authors.id, authors.name, authors.bio, @@ -458,8 +501,11 @@ def transaction(isolation: nil) end rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false + completed = false begin - yield self + result = yield self + completed = true + result rescue Exception # rubocop:disable Lint/RescueException failed = true client.exec(rollback) @@ -469,7 +515,7 @@ def transaction(isolation: nil) # a statement failed and the block rescued the error: PostgreSQL can only roll back rescued = client.transaction_status == PG::PQTRANS_INERROR commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" - client.exec(rescued || Thread.current.status == 'aborting' ? rollback : commit) + client.exec(rescued || Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit) client.exec("RELEASE SAVEPOINT #{savepoint}") if rescued && savepoint raise PG::InFailedSqlTransaction, 'transaction rolled back: a statement in it failed' if rescued && Thread.current.status != 'aborting' end @@ -577,6 +623,71 @@ def test!(...) test(...) || raise(NotFound, 'Test returned no row') end + 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') + end + client.exec_prepared('query_sql_set_json_round_trip', query_params) + nil + end + end + + 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') + end + result = client.exec_prepared('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 + end + + # Like #get_json_round_trip, but raises NotFound when there is no row. + def get_json_round_trip!(...) + get_json_round_trip(...) || raise(NotFound, 'GetJSONRoundTrip returned no row') + end + + 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') + end + client.exec_prepared('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') + end + client.exec_prepared('query_sql_truncate_json_round_trips') + nil + end + end + + 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') + end + client.exec_prepared('query_sql_pause_query', query_params) + nil + end + end + def list_authors_paged(offset:, limit:) with_client('list_authors_paged') do |client| query_params = [offset, limit] @@ -605,6 +716,20 @@ def get_author_by_name_pattern(name_pattern: nil) end end + 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') + end + result = client.exec_prepared('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 + end + end + def create_author_return_id(name:, bio:) with_client('create_author_return_id') do |client| query_params = [name, bio] @@ -696,6 +821,29 @@ def create_book(name:, author_id:) end end + 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') + end + client.exec_prepared('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') + end + client.exec_prepared('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') @@ -1331,14 +1479,23 @@ def string_arrays(client) arrays end + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end + # bytea parameters are sent escaped: binary Strings (NUL bytes, invalid UTF-8) are not valid text. def encode_bytea(value) value && PG::TextEncoder::Bytea.new.encode(value) end - # json / jsonb parameters: an Array is a JSON array, not a PG array (Hashes and JSON text pass through). + # json / jsonb parameters: booleans and Arrays need JSON encoding, not PostgreSQL's boolean / array encoding. + # Hashes and JSON text pass through; nil stays nil. def encode_json(value) - value.is_a?(Array) ? PG::TextEncoder::JSON.new.encode(value) : value + case value + when Array, TrueClass, FalseClass then PG::TextEncoder::JSON.new.encode(value) + else value + end end # Float#to_s is the shortest text that reads back as the same Float (the pg gem's encoder drops digits). diff --git a/test/golden/pg/ruby31_gemfile/query_sql.rbs b/test/golden/pg/ruby31_gemfile/query_sql.rbs index c1aa9c6..f8cb392 100644 --- a/test/golden/pg/ruby31_gemfile/query_sql.rbs +++ b/test/golden/pg/ruby31_gemfile/query_sql.rbs @@ -95,6 +95,29 @@ module PgCodegen ) -> TestRow end + SET_JSON_ROUND_TRIP_SQL: String + + GET_JSON_ROUND_TRIP_SQL: String + + class GetJSONRoundTripRow < ::Struct[untyped] + attr_accessor json_value: untyped + attr_accessor jsonb_value: untyped + def initialize: ( json_value: untyped, jsonb_value: untyped) -> void + def self.new: ( json_value: untyped, jsonb_value: untyped) -> GetJSONRoundTripRow + end + + TOGGLE_JSONB_ROUND_TRIP_SQL: String + + TRUNCATE_JSON_ROUND_TRIPS_SQL: String + + PAUSE_QUERY_SQL: String + + class PauseQueryRow < ::Struct[untyped] + attr_accessor pg_sleep: untyped + def initialize: ( pg_sleep: untyped) -> void + def self.new: ( pg_sleep: untyped) -> PauseQueryRow + end + LIST_AUTHORS_PAGED_SQL: String class ListAuthorsPagedRow < ::Struct[untyped] @@ -115,6 +138,16 @@ module PgCodegen def self.new: ( id: Integer, name: String, bio: String?) -> GetAuthorByNamePatternRow end + GET_AUTHORS_BY_OPTIONAL_NAME_SQL: String + + class GetAuthorsByOptionalNameRow < ::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?) -> GetAuthorsByOptionalNameRow + end + CREATE_AUTHOR_RETURN_ID_SQL: String class CreateAuthorReturnIdRow < ::Struct[untyped] @@ -162,6 +195,10 @@ module PgCodegen def self.new: ( id: String) -> CreateBookRow end + CREATE_DEFERRED_BOOK_SQL: String + + TRUNCATE_DEFERRED_BOOKS_SQL: String + LIST_ALL_AUTHORS_BOOKS_SQL: String class ListAllAuthorsBooksRow < ::Struct[untyped] @@ -620,10 +657,24 @@ module PgCodegen def test!: -> TestRow + def set_json_round_trip: (id: Integer, json_value: untyped, jsonb_value: untyped) -> void + + def get_json_round_trip: (id: Integer) -> GetJSONRoundTripRow? + + def get_json_round_trip!: (id: Integer) -> GetJSONRoundTripRow + + def toggle_jsonb_round_trip: (id: Integer) -> void + + def truncate_json_round_trips: -> void + + def pause_query: (seconds: Float) -> void + def list_authors_paged: (offset: Integer, limit: Integer) -> Array[ListAuthorsPagedRow] def get_author_by_name_pattern: (?name_pattern: String?) -> Array[GetAuthorByNamePatternRow] + def get_authors_by_optional_name: (lim: Integer, ?name_filter: String?) -> Array[GetAuthorsByOptionalNameRow] + def create_author_return_id: (name: String, bio: String?) -> Integer def update_author_bios: (bio: String?) -> Integer @@ -638,6 +689,10 @@ module PgCodegen def create_book: (name: String, author_id: Integer) -> String + def create_deferred_book: (author_id: Integer) -> void + + def truncate_deferred_books: -> void + def list_all_authors_books: -> Array[ListAllAuthorsBooksRow] def get_duplicate_authors: -> Array[GetDuplicateAuthorsRow] @@ -732,6 +787,8 @@ module PgCodegen def string_arrays: (untyped client) -> untyped + def timeout_exits_with_throw?: () -> bool + def encode_bytea: (String? value) -> String? def encode_json: (untyped value) -> untyped diff --git a/test/golden/pg/ruby32_notypes/models.rb b/test/golden/pg/ruby32_notypes/models.rb index b9a3073..6b0fdb0 100644 --- a/test/golden/pg/ruby32_notypes/models.rb +++ b/test/golden/pg/ruby32_notypes/models.rb @@ -40,6 +40,9 @@ class NodePostgresType < Data.define( class Book < Data.define(:id, :name, :author_id, :description) end + class DeferredBook < Data.define(:id, :author_id) + end + class PostgresExtraType < Data.define( :c_uuid, :c_jsonb, @@ -52,6 +55,9 @@ class PostgresExtraType < Data.define( ) end + class JsonRoundTrip < Data.define(:id, :json_value, :jsonb_value) + end + class PostgresArrayType < Data.define(:c_uuid_array, :c_bio_type_array, :c_integer_matrix) end diff --git a/test/golden/pg/ruby32_notypes/my_queries_sql.rb b/test/golden/pg/ruby32_notypes/my_queries_sql.rb index 9c3f794..c60c07d 100644 --- a/test/golden/pg/ruby32_notypes/my_queries_sql.rb +++ b/test/golden/pg/ruby32_notypes/my_queries_sql.rb @@ -45,8 +45,11 @@ def transaction(isolation: nil) end rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false + completed = false begin - yield self + result = yield self + completed = true + result rescue Exception # rubocop:disable Lint/RescueException failed = true client.exec(rollback) @@ -56,7 +59,7 @@ def transaction(isolation: nil) # a statement failed and the block rescued the error: PostgreSQL can only roll back rescued = client.transaction_status == PG::PQTRANS_INERROR commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" - client.exec(rescued || Thread.current.status == 'aborting' ? rollback : commit) + client.exec(rescued || Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit) client.exec("RELEASE SAVEPOINT #{savepoint}") if rescued && savepoint raise PG::InFailedSqlTransaction, 'transaction rolled back: a statement in it failed' if rescued && Thread.current.status != 'aborting' end @@ -123,5 +126,10 @@ def string_arrays(client) arrays.default_type_map = strings arrays end + + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end end end diff --git a/test/golden/pg/ruby32_notypes/other_sql.rb b/test/golden/pg/ruby32_notypes/other_sql.rb index 21eb5b6..e415ab8 100644 --- a/test/golden/pg/ruby32_notypes/other_sql.rb +++ b/test/golden/pg/ruby32_notypes/other_sql.rb @@ -58,8 +58,11 @@ def transaction(isolation: nil) end rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false + completed = false begin - yield self + result = yield self + completed = true + result rescue Exception # rubocop:disable Lint/RescueException failed = true client.exec(rollback) @@ -69,7 +72,7 @@ def transaction(isolation: nil) # a statement failed and the block rescued the error: PostgreSQL can only roll back rescued = client.transaction_status == PG::PQTRANS_INERROR commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" - client.exec(rescued || Thread.current.status == 'aborting' ? rollback : commit) + client.exec(rescued || Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit) client.exec("RELEASE SAVEPOINT #{savepoint}") if rescued && savepoint raise PG::InFailedSqlTransaction, 'transaction rolled back: a statement in it failed' if rescued && Thread.current.status != 'aborting' end @@ -172,6 +175,11 @@ def string_arrays(client) arrays end + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end + # bytea parameters are sent escaped: binary Strings (NUL bytes, invalid UTF-8) are not valid text. def encode_bytea(value) value && PG::TextEncoder::Bytea.new.encode(value) diff --git a/test/golden/pg/ruby32_notypes/query_sql.rb b/test/golden/pg/ruby32_notypes/query_sql.rb index 462945e..5f3e150 100644 --- a/test/golden/pg/ruby32_notypes/query_sql.rb +++ b/test/golden/pg/ruby32_notypes/query_sql.rb @@ -67,6 +67,34 @@ class TestRow < Data.define( ) end + SET_JSON_ROUND_TRIP_SQL = <<~'SQL' + INSERT INTO json_round_trips (id, json_value, jsonb_value) VALUES ($1, $2, $3) + SQL + + GET_JSON_ROUND_TRIP_SQL = <<~'SQL' + SELECT json_value, jsonb_value FROM json_round_trips WHERE id = $1 + SQL + + class GetJSONRoundTripRow < Data.define(:json_value, :jsonb_value) + end + + TOGGLE_JSONB_ROUND_TRIP_SQL = <<~'SQL' + UPDATE json_round_trips + SET jsonb_value = to_jsonb(NOT (jsonb_value #>> '{}')::boolean) + WHERE id = $1 + SQL + + TRUNCATE_JSON_ROUND_TRIPS_SQL = <<~'SQL' + TRUNCATE TABLE json_round_trips + SQL + + PAUSE_QUERY_SQL = <<~'SQL' + SELECT pg_sleep($1::double precision) + SQL + + class PauseQueryRow < Data.define(:pg_sleep) + end + LIST_AUTHORS_PAGED_SQL = <<~'SQL' SELECT id, name, bio FROM authors ORDER BY name @@ -85,6 +113,16 @@ class ListAuthorsPagedRow < Data.define(:id, :name, :bio) class GetAuthorByNamePatternRow < Data.define(:id, :name, :bio) end + GET_AUTHORS_BY_OPTIONAL_NAME_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE ($1::text IS NULL OR name = $1::text) + ORDER BY id + LIMIT $2 + SQL + + class GetAuthorsByOptionalNameRow < Data.define(:id, :name, :bio) + end + CREATE_AUTHOR_RETURN_ID_SQL = <<~'SQL' INSERT INTO authors (name, bio) VALUES ($1, $2) RETURNING id SQL @@ -128,6 +166,14 @@ class CopyAuthorsArgs < Data.define(:name, :bio) class CreateBookRow < Data.define(:id) end + CREATE_DEFERRED_BOOK_SQL = <<~'SQL' + INSERT INTO deferred_books (author_id) VALUES ($1) + SQL + + TRUNCATE_DEFERRED_BOOKS_SQL = <<~'SQL' + TRUNCATE TABLE deferred_books + SQL + LIST_ALL_AUTHORS_BOOKS_SQL = <<~'SQL' SELECT authors.id, authors.name, authors.bio, @@ -461,8 +507,11 @@ def transaction(isolation: nil) end rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false + completed = false begin - yield self + result = yield self + completed = true + result rescue Exception # rubocop:disable Lint/RescueException failed = true client.exec(rollback) @@ -472,7 +521,7 @@ def transaction(isolation: nil) # a statement failed and the block rescued the error: PostgreSQL can only roll back rescued = client.transaction_status == PG::PQTRANS_INERROR commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" - client.exec(rescued || Thread.current.status == 'aborting' ? rollback : commit) + client.exec(rescued || Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit) client.exec("RELEASE SAVEPOINT #{savepoint}") if rescued && savepoint raise PG::InFailedSqlTransaction, 'transaction rolled back: a statement in it failed' if rescued && Thread.current.status != 'aborting' end @@ -581,6 +630,71 @@ def test!(...) test(...) || raise(NotFound, 'Test returned no row') end + 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') + end + client.exec_prepared('query_sql_set_json_round_trip', query_params) + nil + end + end + + 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') + end + result = client.exec_prepared('query_sql_get_json_round_trip', query_params) + row = result.first + row && GetJSONRoundTripRow.new(row['json_value'], row['jsonb_value']) + end + end + + # Like #get_json_round_trip, but raises NotFound when there is no row. + def get_json_round_trip!(...) + get_json_round_trip(...) || raise(NotFound, 'GetJSONRoundTrip returned no row') + end + + 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') + end + client.exec_prepared('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') + end + client.exec_prepared('query_sql_truncate_json_round_trips') + nil + end + end + + 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') + end + client.exec_prepared('query_sql_pause_query', query_params) + nil + end + end + def list_authors_paged(offset:, limit:) with_client('list_authors_paged') do |client| query_params = [offset, limit] @@ -609,6 +723,20 @@ def get_author_by_name_pattern(name_pattern: nil) end end + 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') + end + result = client.exec_prepared('query_sql_get_authors_by_optional_name', query_params) + result.map do |row| + GetAuthorsByOptionalNameRow.new(row['id'], row['name'], row['bio']) + end + end + end + def create_author_return_id(name:, bio:) with_client('create_author_return_id') do |client| query_params = [name, bio] @@ -700,6 +828,29 @@ def create_book(name:, author_id:) end end + 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') + end + client.exec_prepared('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') + end + client.exec_prepared('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') @@ -1319,14 +1470,23 @@ def string_arrays(client) arrays end + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end + # bytea parameters are sent escaped: binary Strings (NUL bytes, invalid UTF-8) are not valid text. def encode_bytea(value) value && PG::TextEncoder::Bytea.new.encode(value) end - # json / jsonb parameters: an Array is a JSON array, not a PG array (Hashes and JSON text pass through). + # json / jsonb parameters: booleans and Arrays need JSON encoding, not PostgreSQL's boolean / array encoding. + # Hashes and JSON text pass through; nil stays nil. def encode_json(value) - value.is_a?(Array) ? PG::TextEncoder::JSON.new.encode(value) : value + case value + when Array, TrueClass, FalseClass then PG::TextEncoder::JSON.new.encode(value) + else value + end end # Float#to_s is the shortest text that reads back as the same Float (the pg gem's encoder drops digits). diff --git a/test/golden/sqlite3/default/models.rb b/test/golden/sqlite3/default/models.rb index 498e262..365bef7 100644 --- a/test/golden/sqlite3/default/models.rb +++ b/test/golden/sqlite3/default/models.rb @@ -20,7 +20,7 @@ class DeclaredType < Data.define( ) end - class Regression < Data.define(:id, :order, :price) + class Regression < Data.define(:id, :order, :price, :payload) end class Tag < Data.define(:name, :uses) diff --git a/test/golden/sqlite3/default/models.rbs b/test/golden/sqlite3/default/models.rbs index 60532b3..271946e 100644 --- a/test/golden/sqlite3/default/models.rbs +++ b/test/golden/sqlite3/default/models.rbs @@ -38,7 +38,8 @@ module Sqlite3Codegen attr_reader id: Integer attr_reader order: String attr_reader price: Float? - def initialize: ( Integer id, String order, Float? price) -> void + attr_reader payload: String? + def initialize: ( Integer id, String order, Float? price, String? payload) -> void end class Tag diff --git a/test/golden/sqlite3/default/query_sql.rb b/test/golden/sqlite3/default/query_sql.rb index 5fe3381..4ac63f5 100644 --- a/test/golden/sqlite3/default/query_sql.rb +++ b/test/golden/sqlite3/default/query_sql.rb @@ -54,6 +54,16 @@ class ListAuthorsPagedRow < Data.define(:id, :name, :bio) class GetAuthorByNamePatternRow < Data.define(:id, :name, :bio) end + GET_AUTHORS_BY_OPTIONAL_NAME_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE (?1 IS NULL OR name = ?1) + ORDER BY id + LIMIT ?2 + SQL + + class GetAuthorsByOptionalNameRow < Data.define(:id, :name, :bio) + end + CREATE_AUTHOR_RETURN_ID_SQL = <<~'SQL' INSERT INTO authors (name, bio) VALUES (?, ?) RETURNING id SQL @@ -81,6 +91,20 @@ class GetAuthorsByIdsRow < Data.define(:id, :name, :bio) class GetAuthorsByIdsAndNamesRow < Data.define(:id, :name, :bio) end + GET_AUTHORS_BY_MIXED_PLACEHOLDERS_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE name = ? + AND id IN (/*SLICE:ids*/?) + AND name = ? + AND id > ? + AND id IN (/*SLICE:more*/?) + AND 'x?' != '?3' + ORDER BY id + SQL + + class GetAuthorsByMixedPlaceholdersRow < Data.define(:id, :name, :bio) + end + class CopyAuthorsArgs < Data.define(:name, :bio) end @@ -218,6 +242,27 @@ class GetMaxRegressionPriceRow < Data.define(:max_price) DELETE FROM regressions SQL + class CopyRegressionRowsArgs < Data.define(:id, :order, :price, :payload) + end + + LIST_REGRESSION_ROWS_SQL = <<~'SQL' + SELECT id, "order", price, payload FROM regressions ORDER BY id + SQL + + class ListRegressionRowsRow < Data.define(:id, :order, :price, :payload) + end + + TRUNCATE_REGRESSION_ROWS_SQL = <<~'SQL' + DELETE FROM regressions + SQL + + PAUSE_QUERY_SQL = <<~'SQL' + SELECT sleep(?) + SQL + + class PauseQueryRow < Data.define(:sleep) + end + class CopyTagsOrReplaceArgs < Data.define(:name, :uses) end @@ -405,6 +450,21 @@ def get_author_by_name_pattern(name_pattern: nil) end end + def get_authors_by_optional_name(lim:, name_filter: nil) + with_client('get_authors_by_optional_name') do |client| + query_params = [name_filter, lim] + stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) + begin + result = stmt.execute(*query_params) + result.map do |row| + GetAuthorsByOptionalNameRow.new(row[0], row[1], row[2]) + end + ensure + stmt.close + end + end + end + def create_author_return_id(name:, bio:) with_client('create_author_return_id') do |client| query_params = [name, bio] @@ -466,6 +526,31 @@ def get_authors_by_ids_and_names(ids:, names:) end end + # rubocop:disable Naming/MethodParameterName + def get_authors_by_mixed_placeholders(n:, ids:, id:, more:) + with_client('get_authors_by_mixed_placeholders') do |client| + query_params = [ + n, + *ids, + n, + id, + *more + ] + sql = GET_AUTHORS_BY_MIXED_PLACEHOLDERS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) + sql = sql.gsub('/*SLICE:more*/?', slice_placeholders(more)) + stmt = client.prepare(sql) + begin + result = stmt.execute(*query_params) + result.map do |row| + GetAuthorsByMixedPlaceholdersRow.new(row[0], row[1], row[2]) + end + ensure + stmt.close + end + end + end + # rubocop:enable Naming/MethodParameterName + # Bulk insert: pass an Array of CopyAuthorsArgs. def copy_authors(rows) with_client('copy_authors') do |client| @@ -753,6 +838,57 @@ def truncate_regressions end end + def copy_regression_rows(rows) + with_client('copy_regression_rows') do |client| + atomically(client) do + rows.each_slice(249) do |batch| + sql = %(INSERT INTO "regressions" ("id", "order", "price", "payload") VALUES #{Array.new(batch.size, '(?, ?, ?, ?)').join(', ')}) + client.execute(sql, batch.flat_map { |row| [row.id, row.order, row.price, row.payload] }) + end + end + nil + end + end + + def list_regression_rows + with_client('list_regression_rows') do |client| + stmt = client.prepare(LIST_REGRESSION_ROWS_SQL) + begin + result = stmt.execute + result.map do |row| + ListRegressionRowsRow.new(row[0], row[1], row[2], row[3]) + end + ensure + stmt.close + end + end + end + + def truncate_regression_rows + with_client('truncate_regression_rows') do |client| + stmt = client.prepare(TRUNCATE_REGRESSION_ROWS_SQL) + begin + stmt.execute + nil + ensure + stmt.close + end + end + end + + def pause_query(sleep:) + with_client('pause_query') do |client| + query_params = [sleep] + stmt = client.prepare(PAUSE_QUERY_SQL) + begin + stmt.execute(*query_params).to_a + nil + ensure + stmt.close + end + end + end + def copy_tags_or_replace(rows) with_client('copy_tags_or_replace') do |client| atomically(client) do diff --git a/test/golden/sqlite3/default/query_sql.rbs b/test/golden/sqlite3/default/query_sql.rbs index 8d2029f..e324931 100644 --- a/test/golden/sqlite3/default/query_sql.rbs +++ b/test/golden/sqlite3/default/query_sql.rbs @@ -40,6 +40,15 @@ module Sqlite3Codegen def initialize: ( Integer id, String name, String? bio) -> void end + GET_AUTHORS_BY_OPTIONAL_NAME_SQL: String + + class GetAuthorsByOptionalNameRow + attr_reader id: Integer + attr_reader name: String + attr_reader bio: String? + def initialize: ( Integer id, String name, String? bio) -> void + end + CREATE_AUTHOR_RETURN_ID_SQL: String class CreateAuthorReturnIdRow @@ -67,6 +76,15 @@ module Sqlite3Codegen def initialize: ( Integer id, String name, String? bio) -> void end + GET_AUTHORS_BY_MIXED_PLACEHOLDERS_SQL: String + + class GetAuthorsByMixedPlaceholdersRow + attr_reader id: Integer + attr_reader name: String + attr_reader bio: String? + def initialize: ( Integer id, String name, String? bio) -> void + end + class CopyAuthorsArgs attr_reader name: String attr_reader bio: String? @@ -209,6 +227,33 @@ module Sqlite3Codegen TRUNCATE_REGRESSIONS_SQL: String + class CopyRegressionRowsArgs + attr_reader id: Integer + attr_reader order: String + attr_reader price: Float? + attr_reader payload: String? + def initialize: ( Integer id, String order, Float? price, String? payload) -> void + end + + LIST_REGRESSION_ROWS_SQL: String + + class ListRegressionRowsRow + attr_reader id: Integer + attr_reader order: String + attr_reader price: Float? + attr_reader payload: String? + def initialize: ( Integer id, String order, Float? price, String? payload) -> void + end + + TRUNCATE_REGRESSION_ROWS_SQL: String + + PAUSE_QUERY_SQL: String + + class PauseQueryRow + attr_reader sleep: untyped + def initialize: ( untyped sleep) -> void + end + class CopyTagsOrReplaceArgs attr_reader name: String attr_reader uses: Integer @@ -300,6 +345,8 @@ module Sqlite3Codegen def get_author_by_name_pattern: (?name_pattern: String?) -> Array[GetAuthorByNamePatternRow] + def get_authors_by_optional_name: (lim: Integer, ?name_filter: untyped) -> Array[GetAuthorsByOptionalNameRow] + def create_author_return_id: (name: String, bio: String?) -> Integer def update_author_bios: (bio: String?) -> Integer @@ -308,6 +355,8 @@ module Sqlite3Codegen def get_authors_by_ids_and_names: (ids: Array[Integer], names: Array[String]) -> Array[GetAuthorsByIdsAndNamesRow] + def get_authors_by_mixed_placeholders: (n: String, ids: Array[Integer], id: Integer, more: Array[Integer]) -> Array[GetAuthorsByMixedPlaceholdersRow] + def copy_authors: (Array[CopyAuthorsArgs] rows) -> void def truncate_authors: -> void @@ -350,6 +399,14 @@ module Sqlite3Codegen def truncate_regressions: -> void + def copy_regression_rows: (Array[CopyRegressionRowsArgs] rows) -> void + + def list_regression_rows: -> Array[ListRegressionRowsRow] + + def truncate_regression_rows: -> void + + def pause_query: (sleep: untyped) -> void + def copy_tags_or_replace: (Array[CopyTagsOrReplaceArgs] rows) -> void def copy_tags_or_ignore: (Array[CopyTagsOrIgnoreArgs] rows) -> void diff --git a/test/golden/sqlite3/ruby31_gemfile/Gemfile b/test/golden/sqlite3/ruby31_gemfile/Gemfile index 80d2d85..1714d2e 100644 --- a/test/golden/sqlite3/ruby31_gemfile/Gemfile +++ b/test/golden/sqlite3/ruby31_gemfile/Gemfile @@ -4,3 +4,4 @@ source 'https://rubygems.org' gem 'connection_pool' gem 'sqlite3' +gem 'timeout', '>= 0.4' diff --git a/test/golden/sqlite3/ruby31_gemfile/models.rb b/test/golden/sqlite3/ruby31_gemfile/models.rb index 622dde5..3710727 100644 --- a/test/golden/sqlite3/ruby31_gemfile/models.rb +++ b/test/golden/sqlite3/ruby31_gemfile/models.rb @@ -24,7 +24,13 @@ module Sqlite3Codegen keyword_init: true ) - Regression = Struct.new(:id, :order, :price, keyword_init: true) + Regression = Struct.new( + :id, + :order, + :price, + :payload, + keyword_init: true + ) Tag = Struct.new(:name, :uses, keyword_init: true) end diff --git a/test/golden/sqlite3/ruby31_gemfile/models.rbs b/test/golden/sqlite3/ruby31_gemfile/models.rbs index 27900f5..2aa5c09 100644 --- a/test/golden/sqlite3/ruby31_gemfile/models.rbs +++ b/test/golden/sqlite3/ruby31_gemfile/models.rbs @@ -49,8 +49,9 @@ module Sqlite3Codegen attr_accessor id: Integer attr_accessor order: String attr_accessor price: Float? - def initialize: ( id: Integer, order: String, price: Float?) -> void - def self.new: ( id: Integer, order: String, price: Float?) -> Regression + attr_accessor payload: String? + def initialize: ( id: Integer, order: String, price: Float?, payload: String?) -> void + def self.new: ( id: Integer, order: String, price: Float?, payload: String?) -> Regression end class Tag < ::Struct[untyped] diff --git a/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rb b/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rb index 5298177..7b7f5eb 100644 --- a/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rb +++ b/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rb @@ -41,8 +41,11 @@ def transaction(mode: nil) @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}") rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false + completed = false begin - yield self + result = yield self + completed = true + result rescue Exception # rubocop:disable Lint/RescueException failed = true @db.execute(rollback) @@ -50,7 +53,7 @@ def transaction(mode: nil) ensure unless failed commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" - @db.execute(Thread.current.status == 'aborting' ? rollback : commit) + @db.execute(Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit) end end end @@ -87,5 +90,10 @@ def instrument(name) def with_client(name, &block) instrument(name) { @lock.synchronize { block.call(@db) } } end + + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end end end diff --git a/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rbs b/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rbs index c5a960b..9d5d645 100644 --- a/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rbs +++ b/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rbs @@ -17,5 +17,7 @@ module Sqlite3Codegen def instrument: [T] (String name) { () -> T } -> T def with_client: [T] (String name) { (untyped) -> T } -> T + + def timeout_exits_with_throw?: () -> bool end end diff --git a/test/golden/sqlite3/ruby31_gemfile/other_sql.rb b/test/golden/sqlite3/ruby31_gemfile/other_sql.rb index ce4c088..46c7b7c 100644 --- a/test/golden/sqlite3/ruby31_gemfile/other_sql.rb +++ b/test/golden/sqlite3/ruby31_gemfile/other_sql.rb @@ -53,8 +53,11 @@ def transaction(mode: nil) @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}") rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false + completed = false begin - yield self + result = yield self + completed = true + result rescue Exception # rubocop:disable Lint/RescueException failed = true @db.execute(rollback) @@ -62,7 +65,7 @@ def transaction(mode: nil) ensure unless failed commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" - @db.execute(Thread.current.status == 'aborting' ? rollback : commit) + @db.execute(Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit) end end end @@ -136,5 +139,10 @@ def instrument(name) def with_client(name, &block) instrument(name) { @lock.synchronize { block.call(@db) } } end + + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end end end diff --git a/test/golden/sqlite3/ruby31_gemfile/other_sql.rbs b/test/golden/sqlite3/ruby31_gemfile/other_sql.rbs index 6cd3045..a1df20a 100644 --- a/test/golden/sqlite3/ruby31_gemfile/other_sql.rbs +++ b/test/golden/sqlite3/ruby31_gemfile/other_sql.rbs @@ -33,5 +33,7 @@ module Sqlite3Codegen def instrument: [T] (String name) { () -> T } -> T def with_client: [T] (String name) { (untyped) -> T } -> T + + def timeout_exits_with_throw?: () -> bool end end diff --git a/test/golden/sqlite3/ruby31_gemfile/query_sql.rb b/test/golden/sqlite3/ruby31_gemfile/query_sql.rb index 088a2e4..b6c3f5b 100644 --- a/test/golden/sqlite3/ruby31_gemfile/query_sql.rb +++ b/test/golden/sqlite3/ruby31_gemfile/query_sql.rb @@ -50,6 +50,15 @@ module Sqlite3Codegen GetAuthorByNamePatternRow = Struct.new(:id, :name, :bio, keyword_init: true) + GET_AUTHORS_BY_OPTIONAL_NAME_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE (?1 IS NULL OR name = ?1) + ORDER BY id + LIMIT ?2 + SQL + + GetAuthorsByOptionalNameRow = Struct.new(:id, :name, :bio, keyword_init: true) + CREATE_AUTHOR_RETURN_ID_SQL = <<~'SQL' INSERT INTO authors (name, bio) VALUES (?, ?) RETURNING id SQL @@ -74,6 +83,19 @@ module Sqlite3Codegen GetAuthorsByIdsAndNamesRow = Struct.new(:id, :name, :bio, keyword_init: true) + GET_AUTHORS_BY_MIXED_PLACEHOLDERS_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE name = ? + AND id IN (/*SLICE:ids*/?) + AND name = ? + AND id > ? + AND id IN (/*SLICE:more*/?) + AND 'x?' != '?3' + ORDER BY id + SQL + + GetAuthorsByMixedPlaceholdersRow = Struct.new(:id, :name, :bio, keyword_init: true) + CopyAuthorsArgs = Struct.new(:name, :bio, keyword_init: true) TRUNCATE_AUTHORS_SQL = <<~'SQL' @@ -202,6 +224,36 @@ module Sqlite3Codegen DELETE FROM regressions SQL + CopyRegressionRowsArgs = Struct.new( + :id, + :order, + :price, + :payload, + keyword_init: true + ) + + LIST_REGRESSION_ROWS_SQL = <<~'SQL' + SELECT id, "order", price, payload FROM regressions ORDER BY id + SQL + + ListRegressionRowsRow = Struct.new( + :id, + :order, + :price, + :payload, + keyword_init: true + ) + + TRUNCATE_REGRESSION_ROWS_SQL = <<~'SQL' + DELETE FROM regressions + SQL + + PAUSE_QUERY_SQL = <<~'SQL' + SELECT sleep(?) + SQL + + PauseQueryRow = Struct.new(:sleep, keyword_init: true) + CopyTagsOrReplaceArgs = Struct.new(:name, :uses, keyword_init: true) CopyTagsOrIgnoreArgs = Struct.new(:name, :uses, keyword_init: true) @@ -280,8 +332,11 @@ def transaction(mode: nil) @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}") rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false + completed = false begin - yield self + result = yield self + completed = true + result rescue Exception # rubocop:disable Lint/RescueException failed = true @db.execute(rollback) @@ -289,7 +344,7 @@ def transaction(mode: nil) ensure unless failed commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" - @db.execute(Thread.current.status == 'aborting' ? rollback : commit) + @db.execute(Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit) end end end @@ -388,6 +443,21 @@ def get_author_by_name_pattern(name_pattern: nil) end end + def get_authors_by_optional_name(lim:, name_filter: nil) + with_client('get_authors_by_optional_name') do |client| + query_params = [name_filter, lim] + stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) + begin + result = stmt.execute(*query_params) + result.map do |row| + GetAuthorsByOptionalNameRow.new(id: row[0], name: row[1], bio: row[2]) + end + ensure + stmt.close + end + end + end + def create_author_return_id(name:, bio:) with_client('create_author_return_id') do |client| query_params = [name, bio] @@ -449,6 +519,31 @@ def get_authors_by_ids_and_names(ids:, names:) end end + # rubocop:disable Naming/MethodParameterName + def get_authors_by_mixed_placeholders(n:, ids:, id:, more:) + with_client('get_authors_by_mixed_placeholders') do |client| + query_params = [ + n, + *ids, + n, + id, + *more + ] + sql = GET_AUTHORS_BY_MIXED_PLACEHOLDERS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) + sql = sql.gsub('/*SLICE:more*/?', slice_placeholders(more)) + stmt = client.prepare(sql) + begin + result = stmt.execute(*query_params) + result.map do |row| + GetAuthorsByMixedPlaceholdersRow.new(id: row[0], name: row[1], bio: row[2]) + end + ensure + stmt.close + end + end + end + # rubocop:enable Naming/MethodParameterName + # Bulk insert: pass an Array of CopyAuthorsArgs. def copy_authors(rows) with_client('copy_authors') do |client| @@ -744,6 +839,57 @@ def truncate_regressions end end + def copy_regression_rows(rows) + with_client('copy_regression_rows') do |client| + atomically(client) do + rows.each_slice(249) do |batch| + sql = %(INSERT INTO "regressions" ("id", "order", "price", "payload") VALUES #{Array.new(batch.size, '(?, ?, ?, ?)').join(', ')}) + client.execute(sql, batch.flat_map { |row| [row.id, row.order, row.price, row.payload] }) + end + end + nil + end + end + + def list_regression_rows + with_client('list_regression_rows') do |client| + stmt = client.prepare(LIST_REGRESSION_ROWS_SQL) + begin + result = stmt.execute + result.map do |row| + ListRegressionRowsRow.new(id: row[0], order: row[1], price: row[2], payload: row[3]) + end + ensure + stmt.close + end + end + end + + def truncate_regression_rows + with_client('truncate_regression_rows') do |client| + stmt = client.prepare(TRUNCATE_REGRESSION_ROWS_SQL) + begin + stmt.execute + nil + ensure + stmt.close + end + end + end + + def pause_query(sleep:) + with_client('pause_query') do |client| + query_params = [sleep] + stmt = client.prepare(PAUSE_QUERY_SQL) + begin + stmt.execute(*query_params).to_a + nil + ensure + stmt.close + end + end + end + def copy_tags_or_replace(rows) with_client('copy_tags_or_replace') do |client| atomically(client) do @@ -884,6 +1030,11 @@ def with_client(name, &block) instrument(name) { @lock.synchronize { block.call(@db) } } end + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end + def atomically(client) client.execute('SAVEPOINT sqlc_copy') done = false diff --git a/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs b/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs index e939847..797904b 100644 --- a/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs +++ b/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs @@ -44,6 +44,16 @@ module Sqlite3Codegen def self.new: ( id: Integer, name: String, bio: String?) -> GetAuthorByNamePatternRow end + GET_AUTHORS_BY_OPTIONAL_NAME_SQL: String + + class GetAuthorsByOptionalNameRow < ::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?) -> GetAuthorsByOptionalNameRow + end + CREATE_AUTHOR_RETURN_ID_SQL: String class CreateAuthorReturnIdRow < ::Struct[untyped] @@ -74,6 +84,16 @@ module Sqlite3Codegen def self.new: ( id: Integer, name: String, bio: String?) -> GetAuthorsByIdsAndNamesRow end + GET_AUTHORS_BY_MIXED_PLACEHOLDERS_SQL: String + + class GetAuthorsByMixedPlaceholdersRow < ::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?) -> GetAuthorsByMixedPlaceholdersRow + end + class CopyAuthorsArgs < ::Struct[untyped] attr_accessor name: String attr_accessor bio: String? @@ -240,6 +260,36 @@ module Sqlite3Codegen TRUNCATE_REGRESSIONS_SQL: String + class CopyRegressionRowsArgs < ::Struct[untyped] + attr_accessor id: Integer + attr_accessor order: String + attr_accessor price: Float? + attr_accessor payload: String? + def initialize: ( id: Integer, order: String, price: Float?, payload: String?) -> void + def self.new: ( id: Integer, order: String, price: Float?, payload: String?) -> CopyRegressionRowsArgs + end + + LIST_REGRESSION_ROWS_SQL: String + + class ListRegressionRowsRow < ::Struct[untyped] + attr_accessor id: Integer + attr_accessor order: String + attr_accessor price: Float? + attr_accessor payload: String? + def initialize: ( id: Integer, order: String, price: Float?, payload: String?) -> void + def self.new: ( id: Integer, order: String, price: Float?, payload: String?) -> ListRegressionRowsRow + end + + TRUNCATE_REGRESSION_ROWS_SQL: String + + PAUSE_QUERY_SQL: String + + class PauseQueryRow < ::Struct[untyped] + attr_accessor sleep: untyped + def initialize: ( sleep: untyped) -> void + def self.new: ( sleep: untyped) -> PauseQueryRow + end + class CopyTagsOrReplaceArgs < ::Struct[untyped] attr_accessor name: String attr_accessor uses: Integer @@ -345,6 +395,8 @@ module Sqlite3Codegen def get_author_by_name_pattern: (?name_pattern: String?) -> Array[GetAuthorByNamePatternRow] + def get_authors_by_optional_name: (lim: Integer, ?name_filter: untyped) -> Array[GetAuthorsByOptionalNameRow] + def create_author_return_id: (name: String, bio: String?) -> Integer def update_author_bios: (bio: String?) -> Integer @@ -353,6 +405,8 @@ module Sqlite3Codegen def get_authors_by_ids_and_names: (ids: Array[Integer], names: Array[String]) -> Array[GetAuthorsByIdsAndNamesRow] + def get_authors_by_mixed_placeholders: (n: String, ids: Array[Integer], id: Integer, more: Array[Integer]) -> Array[GetAuthorsByMixedPlaceholdersRow] + def copy_authors: (Array[CopyAuthorsArgs] rows) -> void def truncate_authors: -> void @@ -395,6 +449,14 @@ module Sqlite3Codegen def truncate_regressions: -> void + def copy_regression_rows: (Array[CopyRegressionRowsArgs] rows) -> void + + def list_regression_rows: -> Array[ListRegressionRowsRow] + + def truncate_regression_rows: -> void + + def pause_query: (sleep: untyped) -> void + def copy_tags_or_replace: (Array[CopyTagsOrReplaceArgs] rows) -> void def copy_tags_or_ignore: (Array[CopyTagsOrIgnoreArgs] rows) -> void @@ -419,6 +481,8 @@ module Sqlite3Codegen def with_client: [T] (String name) { (untyped) -> T } -> T + def timeout_exits_with_throw?: () -> bool + def atomically: (untyped client) { () -> void } -> void def slice_placeholders: (Array[untyped] values) -> String diff --git a/test/golden/sqlite3/ruby32_notypes/models.rb b/test/golden/sqlite3/ruby32_notypes/models.rb index 498e262..365bef7 100644 --- a/test/golden/sqlite3/ruby32_notypes/models.rb +++ b/test/golden/sqlite3/ruby32_notypes/models.rb @@ -20,7 +20,7 @@ class DeclaredType < Data.define( ) end - class Regression < Data.define(:id, :order, :price) + class Regression < Data.define(:id, :order, :price, :payload) end class Tag < Data.define(:name, :uses) diff --git a/test/golden/sqlite3/ruby32_notypes/my_queries_sql.rb b/test/golden/sqlite3/ruby32_notypes/my_queries_sql.rb index 5298177..7b7f5eb 100644 --- a/test/golden/sqlite3/ruby32_notypes/my_queries_sql.rb +++ b/test/golden/sqlite3/ruby32_notypes/my_queries_sql.rb @@ -41,8 +41,11 @@ def transaction(mode: nil) @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}") rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false + completed = false begin - yield self + result = yield self + completed = true + result rescue Exception # rubocop:disable Lint/RescueException failed = true @db.execute(rollback) @@ -50,7 +53,7 @@ def transaction(mode: nil) ensure unless failed commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" - @db.execute(Thread.current.status == 'aborting' ? rollback : commit) + @db.execute(Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit) end end end @@ -87,5 +90,10 @@ def instrument(name) def with_client(name, &block) instrument(name) { @lock.synchronize { block.call(@db) } } end + + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end end end diff --git a/test/golden/sqlite3/ruby32_notypes/other_sql.rb b/test/golden/sqlite3/ruby32_notypes/other_sql.rb index 5d31abc..bed518c 100644 --- a/test/golden/sqlite3/ruby32_notypes/other_sql.rb +++ b/test/golden/sqlite3/ruby32_notypes/other_sql.rb @@ -54,8 +54,11 @@ def transaction(mode: nil) @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}") rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false + completed = false begin - yield self + result = yield self + completed = true + result rescue Exception # rubocop:disable Lint/RescueException failed = true @db.execute(rollback) @@ -63,7 +66,7 @@ def transaction(mode: nil) ensure unless failed commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" - @db.execute(Thread.current.status == 'aborting' ? rollback : commit) + @db.execute(Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit) end end end @@ -137,5 +140,10 @@ def instrument(name) def with_client(name, &block) instrument(name) { @lock.synchronize { block.call(@db) } } end + + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end end end diff --git a/test/golden/sqlite3/ruby32_notypes/query_sql.rb b/test/golden/sqlite3/ruby32_notypes/query_sql.rb index 5fe3381..1e1c294 100644 --- a/test/golden/sqlite3/ruby32_notypes/query_sql.rb +++ b/test/golden/sqlite3/ruby32_notypes/query_sql.rb @@ -54,6 +54,16 @@ class ListAuthorsPagedRow < Data.define(:id, :name, :bio) class GetAuthorByNamePatternRow < Data.define(:id, :name, :bio) end + GET_AUTHORS_BY_OPTIONAL_NAME_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE (?1 IS NULL OR name = ?1) + ORDER BY id + LIMIT ?2 + SQL + + class GetAuthorsByOptionalNameRow < Data.define(:id, :name, :bio) + end + CREATE_AUTHOR_RETURN_ID_SQL = <<~'SQL' INSERT INTO authors (name, bio) VALUES (?, ?) RETURNING id SQL @@ -81,6 +91,20 @@ class GetAuthorsByIdsRow < Data.define(:id, :name, :bio) class GetAuthorsByIdsAndNamesRow < Data.define(:id, :name, :bio) end + GET_AUTHORS_BY_MIXED_PLACEHOLDERS_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE name = ? + AND id IN (/*SLICE:ids*/?) + AND name = ? + AND id > ? + AND id IN (/*SLICE:more*/?) + AND 'x?' != '?3' + ORDER BY id + SQL + + class GetAuthorsByMixedPlaceholdersRow < Data.define(:id, :name, :bio) + end + class CopyAuthorsArgs < Data.define(:name, :bio) end @@ -218,6 +242,27 @@ class GetMaxRegressionPriceRow < Data.define(:max_price) DELETE FROM regressions SQL + class CopyRegressionRowsArgs < Data.define(:id, :order, :price, :payload) + end + + LIST_REGRESSION_ROWS_SQL = <<~'SQL' + SELECT id, "order", price, payload FROM regressions ORDER BY id + SQL + + class ListRegressionRowsRow < Data.define(:id, :order, :price, :payload) + end + + TRUNCATE_REGRESSION_ROWS_SQL = <<~'SQL' + DELETE FROM regressions + SQL + + PAUSE_QUERY_SQL = <<~'SQL' + SELECT sleep(?) + SQL + + class PauseQueryRow < Data.define(:sleep) + end + class CopyTagsOrReplaceArgs < Data.define(:name, :uses) end @@ -296,8 +341,11 @@ def transaction(mode: nil) @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}") rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}" failed = false + completed = false begin - yield self + result = yield self + completed = true + result rescue Exception # rubocop:disable Lint/RescueException failed = true @db.execute(rollback) @@ -305,7 +353,7 @@ def transaction(mode: nil) ensure unless failed commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}" - @db.execute(Thread.current.status == 'aborting' ? rollback : commit) + @db.execute(Thread.current.status == 'aborting' || (!completed && timeout_exits_with_throw?) ? rollback : commit) end end end @@ -405,6 +453,21 @@ def get_author_by_name_pattern(name_pattern: nil) end end + def get_authors_by_optional_name(lim:, name_filter: nil) + with_client('get_authors_by_optional_name') do |client| + query_params = [name_filter, lim] + stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) + begin + result = stmt.execute(*query_params) + result.map do |row| + GetAuthorsByOptionalNameRow.new(row[0], row[1], row[2]) + end + ensure + stmt.close + end + end + end + def create_author_return_id(name:, bio:) with_client('create_author_return_id') do |client| query_params = [name, bio] @@ -466,6 +529,31 @@ def get_authors_by_ids_and_names(ids:, names:) end end + # rubocop:disable Naming/MethodParameterName + def get_authors_by_mixed_placeholders(n:, ids:, id:, more:) + with_client('get_authors_by_mixed_placeholders') do |client| + query_params = [ + n, + *ids, + n, + id, + *more + ] + sql = GET_AUTHORS_BY_MIXED_PLACEHOLDERS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) + sql = sql.gsub('/*SLICE:more*/?', slice_placeholders(more)) + stmt = client.prepare(sql) + begin + result = stmt.execute(*query_params) + result.map do |row| + GetAuthorsByMixedPlaceholdersRow.new(row[0], row[1], row[2]) + end + ensure + stmt.close + end + end + end + # rubocop:enable Naming/MethodParameterName + # Bulk insert: pass an Array of CopyAuthorsArgs. def copy_authors(rows) with_client('copy_authors') do |client| @@ -753,6 +841,57 @@ def truncate_regressions end end + def copy_regression_rows(rows) + with_client('copy_regression_rows') do |client| + atomically(client) do + rows.each_slice(249) do |batch| + sql = %(INSERT INTO "regressions" ("id", "order", "price", "payload") VALUES #{Array.new(batch.size, '(?, ?, ?, ?)').join(', ')}) + client.execute(sql, batch.flat_map { |row| [row.id, row.order, row.price, row.payload] }) + end + end + nil + end + end + + def list_regression_rows + with_client('list_regression_rows') do |client| + stmt = client.prepare(LIST_REGRESSION_ROWS_SQL) + begin + result = stmt.execute + result.map do |row| + ListRegressionRowsRow.new(row[0], row[1], row[2], row[3]) + end + ensure + stmt.close + end + end + end + + def truncate_regression_rows + with_client('truncate_regression_rows') do |client| + stmt = client.prepare(TRUNCATE_REGRESSION_ROWS_SQL) + begin + stmt.execute + nil + ensure + stmt.close + end + end + end + + def pause_query(sleep:) + with_client('pause_query') do |client| + query_params = [sleep] + stmt = client.prepare(PAUSE_QUERY_SQL) + begin + stmt.execute(*query_params).to_a + nil + ensure + stmt.close + end + end + end + def copy_tags_or_replace(rows) with_client('copy_tags_or_replace') do |client| atomically(client) do @@ -893,6 +1032,11 @@ def with_client(name, &block) instrument(name) { @lock.synchronize { block.call(@db) } } end + def timeout_exits_with_throw? + !!(defined?(::Timeout::VERSION) && + (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) + end + def atomically(client) client.execute('SAVEPOINT sqlc_copy') done = false diff --git a/test/sqlite_placeholders_test.rb b/test/sqlite_placeholders_test.rb new file mode 100644 index 0000000..9150e30 --- /dev/null +++ b/test/sqlite_placeholders_test.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require_relative 'test_helper' +require 'sqlc_gen_ruby/drivers/sqlite3' + +class SqlitePlaceholdersTest < Minitest::Test + Column = Struct.new(:name, :is_sqlc_slice) + SourceParam = Struct.new(:number, :column) + Param = Struct.new(:name, :column, :slice) + Query = Struct.new(:text, :params) + + def setup + @driver = SqlcGenRuby::Drivers::Sqlite3.new + end + + def test_scanner_ignores_question_marks_in_literals_and_comments + sql = "SELECT '?' , \"?4\", `?5`, [?6], ? /* ?7 */ -- ?8\n, /*SLICE:ids*/?, ?2, 'it''s ?9'" + + occurrences = @driver.placeholder_occurrences(sql).map { |kind, value,| [kind, value] } + assert_equal [[:positional, nil], [:slice, 'ids'], [:numbered, 2]], occurrences + assert_equal "SELECT '?' , \"?4\", `?5`, [?6], ? /* ?7 */ -- ?8\n, /*SLICE:ids*/?, ?, 'it''s ?9'", + @driver.query_text(Query.new(sql, [SourceParam.new(3, Column.new('ids', true))])) + end + + def test_query_params_follow_sql_occurrence_order_with_repeated_names_and_slices + sql = "name = ?2 AND id IN (/*SLICE:ids*/?) AND name = ?2 AND id > ? " \ + "AND id IN (/*SLICE:more*/?) AND '?3' != '?' /* ?4 */" + sources = [ + SourceParam.new(2, Column.new('n', false)), + SourceParam.new(3, Column.new('ids', true)), + SourceParam.new(1, Column.new('id', false)), + SourceParam.new(4, Column.new('more', true)) + ] + params = [ + Param.new('n', sources[0].column, false), + Param.new('ids', sources[1].column, true), + Param.new('id', sources[2].column, false), + Param.new('more', sources[3].column, true) + ] + query = Query.new(sql, sources) + + assert_equal <<~RUBY.chomp, @driver.query_params(query, params) + query_params = [ + n, + *ids, + n, + id, + *more + ] + RUBY + end + + def test_numbered_placeholders_are_rewritten_only_for_queries_with_slices + sliced = Query.new('a = ?2 AND id IN (/*SLICE:ids*/?) AND b = "?3"', + [SourceParam.new(3, Column.new('ids', true))]) + plain = Query.new('a = ?2 AND b = "?3"', []) + + assert_equal 'a = ? AND id IN (/*SLICE:ids*/?) AND b = "?3"', @driver.query_text(sliced) + assert_equal plain.text, @driver.query_text(plain) + end +end diff --git a/tests/end2end_mysql2.rb b/tests/end2end_mysql2.rb index fe81bea..e3c0a20 100755 --- a/tests/end2end_mysql2.rb +++ b/tests/end2end_mysql2.rb @@ -6,14 +6,48 @@ require 'json' require 'async' require 'async/barrier' +require 'timeout' +EXAMPLES_DIR = File.expand_path(ENV.fetch('EXAMPLES_DIR', File.expand_path('../examples', __dir__))).freeze require_relative 'consts' require_relative 'rbs_types' -require_relative '../examples/mysql2/query_sql' +require File.join(EXAMPLES_DIR, 'mysql2/query_sql') # End-to-end tests of the code generated for examples/authors/mysql, against a real MySQL / MariaDB server. class TestMysql2 < Minitest::Test include Mysql2Codegen + TEXT_PARTS = ["é", '🙂', "\t", "\n", "\r", '\\', "'", '"', ',', '\N', 'NULL'].freeze + + def row_for(klass, values) + klass.new(**klass.members.zip(values).to_h) + end + + def row_value(row, member) + actual_member = row.class.members.find { |name| name == member.to_sym || name == :"#{member}_" } + row.public_send(actual_member) + end + + def seeded_random + seed = Integer(ENV.fetch('SEED', '1793729686')) + warn "SEED=#{seed} #{self.class}" + Random.new(seed) + end + + def random_text(random) + Array.new(random.rand(1..8)) { TEXT_PARTS.fetch(random.rand(TEXT_PARTS.length)) }.join + end + + def old_timeout_version? + defined?(::Timeout::VERSION) && (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative? + end + + def return_from_transaction(name) + @query_sql.transaction do |tx| + tx.create_author_return_id(name: name, bio: nil) + return :returned + end + end + def mysql2_params { 'database' => ENV['DB_NAME'], @@ -24,6 +58,13 @@ def mysql2_params }.compact end + def prepared_statement_count + client = Mysql2::Client.new(mysql2_params) + client.query("SHOW GLOBAL STATUS LIKE 'Prepared_stmt_count'").first.fetch('Value').to_i + ensure + client&.close + end + def setup @query_sql = QuerySql.connect(mysql2_params) @query_sql.truncate_authors # cascades to books @@ -42,7 +83,7 @@ def test_flow first = create_author(Consts::BOJACK_AUTHOR, Consts::BOJACK_THEME) @query_sql.create_author(name: Consts::DR_SEUSS_AUTHOR, bio: Consts::DR_SEUSS_QUOTE) - assert_equal GetAuthorRow.new(first, Consts::BOJACK_AUTHOR, Consts::BOJACK_THEME), + assert_equal row_for(GetAuthorRow, [first, Consts::BOJACK_AUTHOR, Consts::BOJACK_THEME]), @query_sql.get_author(id: first) assert_nil @query_sql.get_author(id: -1) assert_equal [Consts::BOJACK_AUTHOR, Consts::DR_SEUSS_AUTHOR].sort, @query_sql.list_authors.map(&:name).sort @@ -64,10 +105,10 @@ def test_one_on_empty_table_returns_nil end def test_prepared_statements_are_closed + before = prepared_statement_count id = create_author('a') 300.times { @query_sql.get_author(id: id) } - status = Mysql2::Client.new(mysql2_params).query("SHOW GLOBAL STATUS LIKE 'Prepared_stmt_count'").first - assert_equal '0', status['Value'].to_s, 'prepared statements leak on the server' + assert_operator prepared_statement_count, :<=, before, 'prepared statements leak on the server' end # ---- :execlastid / :execrows ----------------------------------------------------------------------------------- @@ -87,12 +128,12 @@ def test_execresult_returns_rows_affected_and_last_id end def test_batch_commands_run_once_per_args - @query_sql.create_authors_batch([CreateAuthorsBatchArgs.new('a', nil), CreateAuthorsBatchArgs.new('b', 'x'), - CreateAuthorsBatchArgs.new('b', 'y')]) + @query_sql.create_authors_batch([CreateAuthorsBatchArgs.new(name: 'a', bio: nil), CreateAuthorsBatchArgs.new(name: 'b', bio: 'x'), + CreateAuthorsBatchArgs.new(name: 'b', bio: 'y')]) ids = @query_sql.list_authors.to_h { |a| [a.bio, a.id] } - found = @query_sql.get_author_batch([GetAuthorBatchArgs.new(ids['x']), GetAuthorBatchArgs.new(-1)]) + found = @query_sql.get_author_batch([GetAuthorBatchArgs.new(id: ids['x']), GetAuthorBatchArgs.new(id: -1)]) assert_equal ['b', nil], found.map { |r| r&.name } - many = @query_sql.list_authors_by_name_batch(%w[b a c].map { |n| ListAuthorsByNameBatchArgs.new(n) }) + many = @query_sql.list_authors_by_name_batch(%w[b a c].map { |n| ListAuthorsByNameBatchArgs.new(name: n) }) assert_equal [%w[x y], [nil], []], many.map { |rows| rows.map(&:bio) } assert_nil @query_sql.create_authors_batch([]) end @@ -119,6 +160,12 @@ def test_nullable_named_arg assert_equal %w[abc abd], ab.map(&:name).sort end + def test_required_argument_after_optional_named_argument + %w[alpha beta gamma].each { |name| create_author(name) } + assert_equal %w[alpha beta gamma], @query_sql.get_authors_by_optional_name(limit: 10).map(&:name).sort + assert_equal ['beta'], @query_sql.get_authors_by_optional_name(limit: 10, name_filter: 'beta').map(&:name) + end + # ---- sqlc.slice ------------------------------------------------------------------------------------------------ def test_slice @@ -172,7 +219,7 @@ def test_embed rows = @query_sql.list_all_authors_books assert_equal %w[a b], rows.map { |r| r.author.name } assert_equal ['book 1', 'book 2'], rows.map { |r| r.book.name } - assert_equal Author.new(first, 'a', 'bio a'), rows.first.author + assert_equal row_for(Author, [first, 'a', 'bio a']), rows.first.author assert_instance_of Book, rows.first.book assert_equal first, rows.first.book.author_id end @@ -201,7 +248,7 @@ def test_enum_and_set_columns author_type: 'Author,Editor') @query_sql.create_extended_bio(author_name: 'a', name: 'n2', bio_type: 'Biography', author_type: nil) row = @query_sql.get_first_extended_bio_by_type(bio_type: 'Memoir') - assert_equal GetFirstExtendedBioByTypeRow.new('a', 'n1', 'Memoir', 'Author,Editor'), row + assert_equal row_for(GetFirstExtendedBioByTypeRow, ['a', 'n1', 'Memoir', 'Author,Editor']), row assert_equal %w[Autobiography Biography Memoir], ExtendedBiosBioType::ALL assert_equal %w[Author Editor Translator], ExtendedBiosAuthorType::ALL end @@ -246,7 +293,7 @@ def test_declared_types_match_runtime_values 'tb', 'tt', 'bl', 'te', 'mb', 'mt', 'lb', 'lt', '{"a": 1}') SQL row = @query_sql.test - assert_empty RbsTypes.mismatches(row, File.expand_path('../examples/mysql2/query_sql.rbs', __dir__), 'TestRow') + assert_empty RbsTypes.mismatches(row, File.join(EXAMPLES_DIR, 'mysql2/query_sql.rbs'), 'TestRow') assert_equal Date.new(2024, 1, 2), row.c_date assert_equal BigDecimal('1.5'), row.c_decimal assert_equal 2024, row.c_year @@ -259,7 +306,7 @@ def test_declared_types_match_runtime_values_for_nulls client.query('DELETE FROM node_mysql_types') client.query('INSERT INTO node_mysql_types (c_int) VALUES (NULL)') row = @query_sql.test - assert_empty RbsTypes.mismatches(row, File.expand_path('../examples/mysql2/query_sql.rbs', __dir__), 'TestRow') + assert_empty RbsTypes.mismatches(row, File.join(EXAMPLES_DIR, 'mysql2/query_sql.rbs'), 'TestRow') ensure client&.query('DELETE FROM node_mysql_types') end @@ -315,17 +362,12 @@ def test_queries_can_share_an_injected_pool assert_equal 'shared', second.get_author(id: id).name end - # The generated classes are plain synchronous Ruby; under the Fiber scheduler (the `async` gem) the pooled - # queries overlap because mysql2 and connection_pool are fiber aware. - def test_queries_run_concurrently_under_the_fiber_scheduler + # mysql2 prepared-statement execution blocks the Fiber scheduler; use threads for concurrent queries. + def test_generated_queries_overlap_across_threads queries = QuerySql.connect(mysql2_params, size: 5) - pool = queries.instance_variable_get(:@db) started = Process.clock_gettime(Process::CLOCK_MONOTONIC) - Sync do - barrier = Async::Barrier.new - 5.times { barrier.async { pool.with { |client| client.query('select sleep(0.4)') } } } - barrier.wait - end + threads = Array.new(5) { Thread.new { queries.pause_query(seconds: 0.4) } } + threads.each(&:value) elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started assert_operator elapsed, :<, 1.5, "5 x 0.4s queries took #{elapsed}s: they ran one after the other" @@ -373,7 +415,7 @@ def test_reused_named_parameter_and_unaliased_columns @query_sql.copy_regressions([CopyRegressionsArgs.new(order: 'x', note: 'y'), CopyRegressionsArgs.new(order: 'z', note: 'x'), CopyRegressionsArgs.new(order: 'z', note: 'z')]) - assert_equal 2, @query_sql.count_regressions_by_text!(term: 'x').count + assert_equal 2, row_value(@query_sql.count_regressions_by_text!(term: 'x'), :count) end def test_repeated_positional_parameters_get_distinct_names @@ -428,7 +470,84 @@ def test_transaction_left_with_break_or_throw_commits_and_frees_the_connection end end pooled.instance_variable_get(:@db).with { |client| refute client.in_transaction } - assert_equal ['break 0', 'break 1', 'throw'], @query_sql.list_authors.map(&:name).sort + expected = old_timeout_version? ? [] : ['break 0', 'break 1', 'throw'] + assert_equal expected, @query_sql.list_authors.map(&:name).sort + end + + def test_transaction_timeout_rolls_back_and_keeps_connection_usable + [:database, :ruby_sleep].each do |kind| + assert_raises(Timeout::Error) do + Timeout.timeout(0.05) do + @query_sql.transaction do |tx| + tx.create_author_return_id(name: kind.to_s, bio: nil) + kind == :database ? tx.pause_query(seconds: 0.2) : sleep(0.2) + end + end + end + assert_empty @query_sql.list_authors + @query_sql.create_author_return_id(name: 'usable', bio: nil) + assert_equal ['usable'], @query_sql.list_authors.map(&:name) + @query_sql.truncate_authors + end + end + + def test_transaction_nonlocal_exit_semantics_follow_timeout_version + break_value = @query_sql.transaction do |tx| + tx.create_author_return_id(name: 'break', bio: nil) + break :break_value + end + assert_equal :break_value, break_value + assert_equal :returned, return_from_transaction('return') + thrown = catch(:transaction_exit) do + @query_sql.transaction do |tx| + tx.create_author_return_id(name: 'throw', bio: nil) + throw :transaction_exit, :throw_value + end + end + assert_equal :throw_value, thrown + expected = old_timeout_version? ? [] : %w[break return throw] + assert_equal expected, @query_sql.list_authors.map(&:name).sort + end + + def test_thread_kill_rolls_back_and_leaves_connection_outside_transaction + started = Queue.new + thread = Thread.new do + @query_sql.transaction do |tx| + tx.create_author_return_id(name: 'killed', bio: nil) + started << true + sleep(5) + end + end + started.pop + thread.kill + thread.join + assert_empty @query_sql.list_authors + @query_sql.instance_variable_get(:@db).with { |client| refute client.in_transaction } + end + + def test_nested_savepoint_rolls_back_while_outer_transaction_commits + @query_sql.transaction do |tx| + tx.create_author_return_id(name: 'outer before', bio: nil) + assert_raises(RuntimeError) do + tx.transaction do |inner| + inner.create_author_return_id(name: 'nested', bio: nil) + raise 'rollback savepoint' + end + end + tx.create_author_return_id(name: 'outer after', bio: nil) + end + assert_equal ['outer after', 'outer before'], @query_sql.list_authors.map(&:name).sort + end + + def test_parent_query_object_calls_are_part_of_same_thread_transaction + assert_raises(RuntimeError) do + @query_sql.transaction do |tx| + tx.create_author_return_id(name: 'tx', bio: nil) + @query_sql.create_author_return_id(name: 'parent', bio: nil) + raise 'rollback outer transaction' + end + end + assert_empty @query_sql.list_authors end # ---- final sweep fixes: :copyfrom into JSON, IGNORE / REPLACE, reused statements, dead connections ------------ @@ -471,4 +590,96 @@ def test_a_connection_closed_by_the_server_is_replaced assert_raises(Mysql2::Error) { pooled.list_authors } assert_equal [], pooled.list_authors end + + def test_killed_backend_inside_transaction_is_replaced + pooled = QuerySql.connect(mysql2_params, size: 1) + assert_raises(Mysql2::Error) do + pooled.transaction do |tx| + tx.create_author_return_id(name: 'rolled back', bio: nil) + id = pooled.instance_variable_get(:@db).with(&:thread_id) + admin = Mysql2::Client.new(**mysql2_params.transform_keys(&:to_sym)) + admin.query("KILL #{id}") + admin.close + tx.list_authors + end + end + assert_empty pooled.list_authors + end + + def test_seeded_random_copyfrom_round_trip_and_duplicate_is_atomic + random = seeded_random + rows = Array.new(300) do |i| + CopyRegressionRowsArgs.new(id: 5_000_000_000 + i, order: "#{i}#{random_text(random)}", + note: random.rand(3).zero? ? nil : random_text(random), + payload: random.bytes(random.rand(0..16)).b) + end + @query_sql.copy_regression_rows(rows) + expected = rows.map { |r| [r.id, r.order, r.note, r.payload] }.sort_by(&:first) + actual = @query_sql.list_regression_rows.map { |r| [r.id, r.order, r.note, r.payload] } + assert_equal expected, actual + + duplicate = [CopyRegressionRowsArgs.new(id: 6_000_000_000, order: 'new', note: nil, payload: nil), rows.first] + assert_raises(Mysql2::Error) { @query_sql.copy_regression_rows(duplicate) } + assert_equal expected, @query_sql.list_regression_rows.map { |r| [r.id, r.order, r.note, r.payload] } + end + + def test_seeded_random_batch_round_trip + random = seeded_random + rows = Array.new(300) do |i| + { name: "#{i}-#{random_text(random)}", bio: random.rand(3).zero? ? nil : random_text(random) } + end + @query_sql.create_authors_batch(rows.map { |row| CreateAuthorsBatchArgs.new(**row) }) + stored = @query_sql.list_authors + assert_equal rows.sort_by { |row| row[:name] }, + stored.map { |row| { name: row.name, bio: row.bio } }.sort_by { |row| row[:name] } + one = @query_sql.get_author_batch(stored.map { |row| GetAuthorBatchArgs.new(id: row.id) }) + assert_equal stored.map(&:name).sort, one.map(&:name).sort + many = @query_sql.list_authors_by_name_batch(stored.map { |row| ListAuthorsByNameBatchArgs.new(name: row.name) }) + assert_equal stored.map(&:name).sort, many.flatten.map(&:name).sort + end + + def test_seeded_concurrent_transactions_commit_the_exact_planned_set + random = seeded_random + plan = Array.new(4) { Array.new(8) { random.rand(4) } } + names = plan.each_with_index.map do |actions, thread_index| + actions.each_index.map { |index| "thread-#{thread_index}-#{index}" } + end + expected = plan.each_with_index.flat_map do |actions, thread_index| + actions.each_with_index.flat_map do |action, index| + name = names[thread_index][index] + action == 1 ? [] : (action == 3 ? [name, "#{name}-batch"] : [name]) + end + end.sort + threads = plan.each_with_index.map do |actions, thread_index| + Thread.new do + actions.each_with_index do |action, index| + name = names[thread_index][index] + begin + @query_sql.transaction do |tx| + tx.create_author_return_id(name: name, bio: nil) + case action + when 1 + raise "rollback #{name}" + when 2 + begin + tx.transaction do |inner| + inner.create_author_return_id(name: "#{name}-nested", bio: nil) + raise "savepoint #{name}" + end + rescue RuntimeError + nil + end + when 3 + tx.create_authors_batch([CreateAuthorsBatchArgs.new(name: "#{name}-batch", bio: "batch #{name}")]) + end + end + rescue RuntimeError => error + raise unless action == 1 && error.message == "rollback #{name}" + end + end + end + end + threads.each(&:value) + assert_equal expected, @query_sql.list_authors.map(&:name).sort + end end diff --git a/tests/end2end_pg.rb b/tests/end2end_pg.rb index 04e99a6..8febb28 100755 --- a/tests/end2end_pg.rb +++ b/tests/end2end_pg.rb @@ -8,14 +8,48 @@ require 'bigdecimal' require 'ipaddr' require 'securerandom' +require 'timeout' +EXAMPLES_DIR = File.expand_path(ENV.fetch('EXAMPLES_DIR', File.expand_path('../examples', __dir__))).freeze require_relative 'consts' require_relative 'rbs_types' -require_relative '../examples/pg/query_sql' +require File.join(EXAMPLES_DIR, 'pg/query_sql') # End-to-end tests of the code generated for examples/authors/postgresql, against a real PostgreSQL server. class TestPg < Minitest::Test include PgCodegen + TEXT_PARTS = ["é", '🙂', "\t", "\n", "\r", '\\', "'", '"', ',', '\N', 'NULL'].freeze + + def row_for(klass, values) + klass.new(**klass.members.zip(values).to_h) + end + + def row_value(row, member) + actual_member = row.class.members.find { |name| name == member.to_sym || name == :"#{member}_" } + row.public_send(actual_member) + end + + def seeded_random + seed = Integer(ENV.fetch('SEED', '1793729686')) + warn "SEED=#{seed} #{self.class}" + Random.new(seed) + end + + def random_text(random) + Array.new(random.rand(1..8)) { TEXT_PARTS.fetch(random.rand(TEXT_PARTS.length)) }.join + end + + def old_timeout_version? + defined?(::Timeout::VERSION) && (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative? + end + + def return_from_transaction(name) + @query_sql.transaction do |tx| + tx.create_author_return_id(name: name, bio: nil) + return :returned + end + end + def pg_params { 'dbname' => ENV['DB_NAME'], @@ -35,6 +69,8 @@ def setup @query_sql.truncate_special_types @query_sql.truncate_regressions @query_sql.truncate_batch_types + @query_sql.truncate_json_round_trips + @query_sql.truncate_deferred_books end def create_author(name, bio = nil) @@ -45,16 +81,16 @@ def create_author(name, bio = nil) def test_flow first = @query_sql.create_author(name: Consts::BOJACK_AUTHOR, bio: Consts::BOJACK_THEME) - assert_equal CreateAuthorRow.new(first.id, Consts::BOJACK_AUTHOR, Consts::BOJACK_THEME), first + assert_equal row_for(CreateAuthorRow, [first.id, Consts::BOJACK_AUTHOR, Consts::BOJACK_THEME]), first second = @query_sql.create_author(name: Consts::DR_SEUSS_AUTHOR, bio: Consts::DR_SEUSS_QUOTE) - assert_equal GetAuthorRow.new(first.id, Consts::BOJACK_AUTHOR, Consts::BOJACK_THEME), + assert_equal row_for(GetAuthorRow, [first.id, Consts::BOJACK_AUTHOR, Consts::BOJACK_THEME]), @query_sql.get_author(id: first.id) assert_nil @query_sql.get_author(id: -1) assert_equal [second.name, first.name].sort, @query_sql.list_authors.map(&:name).sort @query_sql.delete_author(id: first.id) - assert_equal [ListAuthorsRow.new(second.id, Consts::DR_SEUSS_AUTHOR, Consts::DR_SEUSS_QUOTE)], + assert_equal [row_for(ListAuthorsRow, [second.id, Consts::DR_SEUSS_AUTHOR, Consts::DR_SEUSS_QUOTE])], @query_sql.list_authors end @@ -89,12 +125,12 @@ def test_execresult_returns_rows_affected end def test_batch_commands_run_once_per_args - @query_sql.create_authors_batch([CreateAuthorsBatchArgs.new('a', nil), CreateAuthorsBatchArgs.new('b', 'x'), - CreateAuthorsBatchArgs.new('b', 'y')]) + @query_sql.create_authors_batch([CreateAuthorsBatchArgs.new(name: 'a', bio: nil), CreateAuthorsBatchArgs.new(name: 'b', bio: 'x'), + CreateAuthorsBatchArgs.new(name: 'b', bio: 'y')]) ids = @query_sql.list_authors.to_h { |a| [a.bio, a.id] } - found = @query_sql.get_author_batch([GetAuthorBatchArgs.new(ids['x']), GetAuthorBatchArgs.new(-1)]) + found = @query_sql.get_author_batch([GetAuthorBatchArgs.new(id: ids['x']), GetAuthorBatchArgs.new(id: -1)]) assert_equal ['b', nil], found.map { |r| r&.name } - many = @query_sql.list_authors_by_name_batch(%w[b a c].map { |n| ListAuthorsByNameBatchArgs.new(n) }) + many = @query_sql.list_authors_by_name_batch(%w[b a c].map { |n| ListAuthorsByNameBatchArgs.new(name: n) }) assert_equal [%w[x y], [nil], []], many.map { |rows| rows.map(&:bio) } assert_nil @query_sql.create_authors_batch([]) end @@ -123,6 +159,12 @@ def test_nullable_named_arg assert_equal %w[abc abd], ab.map(&:name).sort end + def test_required_argument_after_optional_named_argument + %w[alpha beta gamma].each { |name| create_author(name) } + assert_equal %w[alpha beta gamma], @query_sql.get_authors_by_optional_name(lim: 10).map(&:name).sort + assert_equal ['beta'], @query_sql.get_authors_by_optional_name(lim: 10, name_filter: 'beta').map(&:name) + end + # ---- array parameters (PostgreSQL has no sqlc.slice) ----------------------------------------------------------- def test_array_parameters @@ -167,7 +209,7 @@ def test_embed assert_equal ['book 1', 'book 2'], rows.map { |r| r.book.name } assert_instance_of Author, rows.first.author assert_instance_of Book, rows.first.book - assert_equal Author.new(first, 'a', 'bio a'), rows.first.author + assert_equal row_for(Author, [first, 'a', 'bio a']), rows.first.author assert_equal first, rows.first.book.author_id end @@ -209,7 +251,7 @@ def test_enum_column @query_sql.create_extended_bio(author_name: 'a', name: 'n1', bio_type: ExtendedBioType::MEMOIR) @query_sql.create_extended_bio(author_name: 'a', name: 'n2', bio_type: ExtendedBioType::BIOGRAPHY) row = @query_sql.get_first_extended_bio_by_type(bio_type: 'Biography') - assert_equal GetFirstExtendedBioByTypeRow.new('a', 'n2', 'Biography'), row + assert_equal row_for(GetFirstExtendedBioByTypeRow, %w[a n2 Biography]), row assert_equal %w[Autobiography Biography Memoir], ExtendedBioType::ALL end @@ -295,6 +337,29 @@ def test_extra_types_round_trip assert_equal 'nn', row.c_not_null end + def test_json_and_jsonb_parameters_round_trip_all_json_value_types + cases = [ + [true, true], [false, false], [7, 7], [1.25, 1.25], + [{ 'nested' => [1, { 'nil' => nil }] }, { 'nested' => [1, { 'nil' => nil }] }], + [[1, false, { 'a' => 'b' }], [1, false, { 'a' => 'b' }]], + ['{"from":"json text"}', { 'from' => 'json text' }], [nil, nil] + ] + cases.each_with_index do |(value, expected), id| + @query_sql.set_json_round_trip(id: id, json_value: value, jsonb_value: value) + row = @query_sql.get_json_round_trip(id: id) + if expected.nil? + assert_nil row.json_value + assert_nil row.jsonb_value + else + assert_equal expected, row.json_value + assert_equal expected, row.jsonb_value + end + end + @query_sql.set_json_round_trip(id: 10, json_value: true, jsonb_value: true) + @query_sql.toggle_jsonb_round_trip(id: 10) + assert_equal false, @query_sql.get_json_round_trip(id: 10).jsonb_value + end + def test_arrays_of_types_without_a_decoder_are_arrays uuids = [SecureRandom.uuid, SecureRandom.uuid] @query_sql.insert_array_types(c_uuid_array: uuids, c_bio_type_array: %w[Memoir Biography], @@ -303,7 +368,7 @@ def test_arrays_of_types_without_a_decoder_are_arrays assert_equal uuids, row.c_uuid_array assert_equal %w[Memoir Biography], row.c_bio_type_array assert_equal [[1, 2], [3, 4]], row.c_integer_matrix - assert_empty RbsTypes.mismatches(row, File.expand_path('../examples/pg/query_sql.rbs', __dir__), 'GetArrayTypesRow') + assert_empty RbsTypes.mismatches(row, File.join(EXAMPLES_DIR, 'pg/query_sql.rbs'), 'GetArrayTypesRow') end def test_special_types_composites_and_domains @@ -312,7 +377,7 @@ def test_special_types_composites_and_domains row = @query_sql.get_special_types assert_equal ['12:00:00+02', "'a':1 'b':2", '(1,2)', '[1,4)', 42, '(3,4)', 5], [row.c_timetz, row.c_tsvector, row.c_point, row.c_int4range, row.c_oid, row.c_dimensions, row.c_positive] - assert_empty RbsTypes.mismatches(row, File.expand_path('../examples/pg/query_sql.rbs', __dir__), 'GetSpecialTypesRow') + assert_empty RbsTypes.mismatches(row, File.join(EXAMPLES_DIR, 'pg/query_sql.rbs'), 'GetSpecialTypesRow') end def test_extra_types_nulls @@ -337,7 +402,7 @@ def test_declared_types_match_runtime_values '2024-01-02 03:04:05', 'a', 'b', '\\x0102', 'c', '{"a": 1}') SQL row = @query_sql.test - assert_empty RbsTypes.mismatches(row, File.expand_path('../examples/pg/query_sql.rbs', __dir__), 'TestRow') + assert_empty RbsTypes.mismatches(row, File.join(EXAMPLES_DIR, 'pg/query_sql.rbs'), 'TestRow') assert_equal Date.new(2024, 1, 2), row.c_date assert_equal BigDecimal('1.5'), row.c_decimal assert_equal({ 'a' => 1 }, row.c_json) @@ -351,7 +416,7 @@ def test_declared_types_match_runtime_values_for_nulls client.exec('DELETE FROM node_postgres_types') client.exec('INSERT INTO node_postgres_types (c_integer) VALUES (NULL)') row = @query_sql.test - assert_empty RbsTypes.mismatches(row, File.expand_path('../examples/pg/query_sql.rbs', __dir__), 'TestRow') + assert_empty RbsTypes.mismatches(row, File.join(EXAMPLES_DIR, 'pg/query_sql.rbs'), 'TestRow') assert_equal [nil], row.to_h.except(:c_serial).values.uniq # c_serial is filled by its default ensure client&.exec('DELETE FROM node_postgres_types') @@ -362,7 +427,7 @@ def test_extra_types_match_declared_types c_uuid: SecureRandom.uuid, c_jsonb: '[1]', c_text_array: %w[a], c_integer_array: [1], c_timestamptz: Time.now, c_inet: '::1', c_money: '1', c_not_null: 'x' ) - assert_empty RbsTypes.mismatches(@query_sql.get_extra_types, File.expand_path('../examples/pg/query_sql.rbs', __dir__), + assert_empty RbsTypes.mismatches(@query_sql.get_extra_types, File.join(EXAMPLES_DIR, 'pg/query_sql.rbs'), 'GetExtraTypesRow') end @@ -426,11 +491,10 @@ def test_queries_can_share_an_injected_pool # queries overlap because pg and connection_pool are fiber aware. def test_queries_run_concurrently_under_the_fiber_scheduler queries = QuerySql.connect(pg_params, size: 5) - pool = queries.instance_variable_get(:@db) started = Process.clock_gettime(Process::CLOCK_MONOTONIC) Sync do barrier = Async::Barrier.new - 5.times { barrier.async { pool.with { |client| client.exec('select pg_sleep(0.4)') } } } + 5.times { barrier.async { queries.pause_query(seconds: 0.4) } } barrier.wait end elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started @@ -493,8 +557,10 @@ def test_execlastid_is_nil_when_no_row_is_returned def test_copyfrom_quotes_identifiers_and_encodes_bytea_and_json binary = "\x00\x01\xFE".b @query_sql.copy_regressions([CopyRegressionsArgs.new(id: 1, order: 'a', payload: binary, doc: [1, 2]), - CopyRegressionsArgs.new(id: 2, order: 'b', payload: nil, doc: nil)]) - assert_equal [[1, 'a', binary, [1, 2]], [2, 'b', nil, nil]], + CopyRegressionsArgs.new(id: 2, order: 'b', payload: nil, doc: nil), + CopyRegressionsArgs.new(id: 3, order: 'true', payload: nil, doc: true), + CopyRegressionsArgs.new(id: 4, order: 'false', payload: nil, doc: false)]) + assert_equal [[1, 'a', binary, [1, 2]], [2, 'b', nil, nil], [3, 'true', nil, true], [4, 'false', nil, false]], @query_sql.list_regressions.map { |r| [r.id, r.order, r.payload, r.doc] } end @@ -518,8 +584,11 @@ def test_prepared_statements_survive_a_connection_reset def test_batch_parameters_encode_bytea_and_json binary = "\x00\xFFb".b @query_sql.create_regressions_batch([CreateRegressionsBatchArgs.new(id: 1, order: 'a', payload: binary, doc: [1, { 'k' => 'é' }]), - CreateRegressionsBatchArgs.new(id: 2, order: 'b', payload: nil, doc: nil)]) - assert_equal [[1, binary, [1, { 'k' => 'é' }]], [2, nil, nil]], @query_sql.list_regressions.map { |r| [r.id, r.payload, r.doc] } + CreateRegressionsBatchArgs.new(id: 2, order: 'b', payload: nil, doc: nil), + CreateRegressionsBatchArgs.new(id: 3, order: 'c', payload: nil, doc: true), + CreateRegressionsBatchArgs.new(id: 4, order: 'd', payload: nil, doc: false)]) + assert_equal [[1, binary, [1, { 'k' => 'é' }]], [2, nil, nil], [3, nil, true], [4, nil, false]], + @query_sql.list_regressions.map { |r| [r.id, r.payload, r.doc] } end def test_transaction_whose_failed_statement_was_rescued_raises_instead_of_committing @@ -553,7 +622,86 @@ def test_transaction_left_with_break_or_throw_commits_and_frees_the_connection end end pooled.instance_variable_get(:@db).with { |client| assert_equal PG::PQTRANS_IDLE, client.transaction_status } - assert_equal ['break 0', 'break 1', 'throw'], @query_sql.list_authors.map(&:name).sort + expected = old_timeout_version? ? [] : ['break 0', 'break 1', 'throw'] + assert_equal expected, @query_sql.list_authors.map(&:name).sort + end + + def test_transaction_timeout_rolls_back_and_keeps_connection_usable + [:database, :ruby_sleep].each do |kind| + assert_raises(Timeout::Error) do + Timeout.timeout(0.05) do + @query_sql.transaction do |tx| + tx.create_author_return_id(name: kind.to_s, bio: nil) + kind == :database ? tx.pause_query(seconds: 0.2) : sleep(0.2) + end + end + end + assert_empty @query_sql.list_authors + @query_sql.create_author_return_id(name: 'usable', bio: nil) + assert_equal ['usable'], @query_sql.list_authors.map(&:name) + @query_sql.truncate_authors + end + end + + def test_transaction_nonlocal_exit_semantics_follow_timeout_version + break_value = @query_sql.transaction do |tx| + tx.create_author_return_id(name: 'break', bio: nil) + break :break_value + end + assert_equal :break_value, break_value + assert_equal :returned, return_from_transaction('return') + thrown = catch(:transaction_exit) do + @query_sql.transaction do |tx| + tx.create_author_return_id(name: 'throw', bio: nil) + throw :transaction_exit, :throw_value + end + end + assert_equal :throw_value, thrown + expected = old_timeout_version? ? [] : %w[break return throw] + assert_equal expected, @query_sql.list_authors.map(&:name).sort + end + + def test_thread_kill_rolls_back_and_leaves_connection_idle + started = Queue.new + thread = Thread.new do + @query_sql.transaction do |tx| + tx.create_author_return_id(name: 'killed', bio: nil) + started << true + sleep(5) + end + end + started.pop + thread.kill + thread.join + assert_empty @query_sql.list_authors + @query_sql.instance_variable_get(:@db).with do |client| + assert_equal PG::PQTRANS_IDLE, client.transaction_status + end + end + + def test_nested_savepoint_rolls_back_while_outer_transaction_commits + @query_sql.transaction do |tx| + tx.create_author_return_id(name: 'outer before', bio: nil) + assert_raises(RuntimeError) do + tx.transaction do |inner| + inner.create_author_return_id(name: 'nested', bio: nil) + raise 'rollback savepoint' + end + end + tx.create_author_return_id(name: 'outer after', bio: nil) + end + assert_equal ['outer after', 'outer before'], @query_sql.list_authors.map(&:name).sort + end + + def test_parent_query_object_calls_are_part_of_same_thread_transaction + assert_raises(RuntimeError) do + @query_sql.transaction do |tx| + tx.create_author_return_id(name: 'tx', bio: nil) + @query_sql.create_author_return_id(name: 'parent', bio: nil) + raise 'rollback outer transaction' + end + end + assert_empty @query_sql.list_authors end # ---- final sweep fixes: :batch* / :copyfrom parameter encoding, nested rescued errors, dead connections --------- @@ -602,7 +750,7 @@ def test_array_parameters_need_no_server_side_parameter_types end def test_all_caps_parameter_names_are_snake_cased - assert_equal 0, @query_sql.count_batch_types_above!(min_id: 0).count + assert_equal 0, row_value(@query_sql.count_batch_types_above!(min_id: 0), :count) end def test_nested_transaction_rescuing_a_failed_statement_rolls_back_only_itself @@ -633,4 +781,115 @@ def test_a_connection_closed_by_the_server_is_reconnected assert_raises(PG::Error) { pooled.list_authors } assert_equal [], pooled.list_authors end + + def test_deferred_foreign_key_failure_at_commit_leaves_connection_idle + assert_raises(PG::ForeignKeyViolation) do + @query_sql.transaction do |tx| + tx.create_deferred_book(author_id: -1) + end + end + @query_sql.instance_variable_get(:@db).with do |client| + assert_equal PG::PQTRANS_IDLE, client.transaction_status + end + assert_kind_of Integer, @query_sql.create_author_return_id(name: 'usable', bio: nil) + end + + def test_terminated_backend_inside_transaction_is_replaced + pooled = QuerySql.connect(pg_params, size: 1) + assert_raises(PG::Error) do + pooled.transaction do |tx| + tx.create_author_return_id(name: 'rolled back', bio: nil) + pid = pooled.instance_variable_get(:@db).with(&:backend_pid) + PG.connect(**pg_params.transform_keys(&:to_sym)) do |admin| + admin.exec_params('SELECT pg_terminate_backend($1)', [pid]) + end + tx.list_authors + end + end + assert_empty pooled.list_authors + end + + def test_seeded_random_copyfrom_round_trip_and_duplicate_is_atomic + random = seeded_random + rows = Array.new(300) do |i| + doc = case random.rand(6) + when 0 then nil + when 1 then true + when 2 then false + when 3 then random.rand(-2**60..2**60) + when 4 then random.rand(1..100) / 4.0 + else { 'text' => random_text(random), 'items' => [random.rand(100), nil] } + end + CopyRegressionsArgs.new(id: 5_000_000_000 + i, order: "#{i}#{random_text(random)}", + payload: random.bytes(random.rand(0..16)).b, doc: doc) + end + @query_sql.copy_regressions(rows) + expected = rows.map { |r| [r.id, r.order, r.payload, r.doc] }.sort_by(&:first) + actual = @query_sql.list_regressions.map { |r| [r.id, r.order, r.payload, r.doc] } + assert_equal expected, actual + + duplicate = [CopyRegressionsArgs.new(id: 6_000_000_000, order: 'new', payload: nil, doc: nil), rows.first] + assert_raises(PG::UniqueViolation) { @query_sql.copy_regressions(duplicate) } + assert_equal expected, @query_sql.list_regressions.map { |r| [r.id, r.order, r.payload, r.doc] } + end + + def test_seeded_random_batch_round_trip + random = seeded_random + rows = Array.new(300) do |i| + { name: "#{i}-#{random_text(random)}", bio: random.rand(3).zero? ? nil : random_text(random) } + end + @query_sql.create_authors_batch(rows.map { |row| CreateAuthorsBatchArgs.new(**row) }) + stored = @query_sql.list_authors + assert_equal rows.sort_by { |row| row[:name] }, + stored.map { |row| { name: row.name, bio: row.bio } }.sort_by { |row| row[:name] } + one = @query_sql.get_author_batch(stored.map { |row| GetAuthorBatchArgs.new(id: row.id) }) + assert_equal stored.map(&:name).sort, one.map(&:name).sort + many = @query_sql.list_authors_by_name_batch(stored.map { |row| ListAuthorsByNameBatchArgs.new(name: row.name) }) + assert_equal stored.map(&:name).sort, many.flatten.map(&:name).sort + end + + def test_seeded_concurrent_transactions_commit_the_exact_planned_set + random = seeded_random + plan = Array.new(4) { Array.new(8) { random.rand(4) } } + names = plan.each_with_index.map do |actions, thread_index| + actions.each_index.map { |index| "thread-#{thread_index}-#{index}" } + end + expected = plan.each_with_index.flat_map do |actions, thread_index| + actions.each_with_index.flat_map do |action, index| + name = names[thread_index][index] + action == 1 ? [] : (action == 3 ? [name, "#{name}-batch"] : [name]) + end + end.sort + threads = plan.each_with_index.map do |actions, thread_index| + Thread.new do + actions.each_with_index do |action, index| + name = names[thread_index][index] + begin + @query_sql.transaction do |tx| + tx.create_author_return_id(name: name, bio: nil) + case action + when 1 + raise "rollback #{name}" + when 2 + begin + tx.transaction do |inner| + inner.create_author_return_id(name: "#{name}-nested", bio: nil) + raise "savepoint #{name}" + end + rescue RuntimeError + nil + end + when 3 + tx.create_authors_batch([CreateAuthorsBatchArgs.new(name: "#{name}-batch", bio: random_text(random))]) + end + end + rescue RuntimeError => error + raise unless action == 1 && error.message == "rollback #{name}" + end + end + end + end + threads.each(&:value) + assert_equal expected, @query_sql.list_authors.map(&:name).sort + end end diff --git a/tests/end2end_sqlite3.rb b/tests/end2end_sqlite3.rb index 2961ee5..e300dfd 100644 --- a/tests/end2end_sqlite3.rb +++ b/tests/end2end_sqlite3.rb @@ -4,19 +4,49 @@ require 'logger' require 'stringio' require 'sqlite3' +require 'timeout' +EXAMPLES_DIR = File.expand_path(ENV.fetch('EXAMPLES_DIR', File.expand_path('../examples', __dir__))).freeze require_relative 'consts' require_relative 'rbs_types' -require_relative '../examples/sqlite3/query_sql' +require File.join(EXAMPLES_DIR, 'sqlite3/query_sql') # End-to-end tests of the code generated for examples/authors/sqlite, against a real (in-memory) SQLite database. class TestSqlite3 < Minitest::Test include Sqlite3Codegen + TEXT_PARTS = ["é", '🙂', "\t", "\n", "\r", '\\', "'", '"', ',', '\N', 'NULL'].freeze + + def row_for(klass, values) + klass.new(**klass.members.zip(values).to_h) + end + + def seeded_random + seed = Integer(ENV.fetch('SEED', '1793729686')) + warn "SEED=#{seed} #{self.class}" + Random.new(seed) + end + + def random_text(random) + Array.new(random.rand(1..8)) { TEXT_PARTS.fetch(random.rand(TEXT_PARTS.length)) }.join + end + + def old_timeout_version? + defined?(::Timeout::VERSION) && (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative? + end + + def return_from_transaction(name) + @query_sql.transaction do |tx| + tx.create_author_return_id(name: name, bio: nil) + return :returned + end + end + SCHEMA = File.read(File.expand_path('../examples/authors/sqlite/schema.sql', __dir__)) def setup @db = SQLite3::Database.new(':memory:') @db.execute_batch(SCHEMA) + @db.create_function('sleep', 1) { |_func, seconds| Kernel.sleep(seconds) } @query_sql = QuerySql.new(@db) end @@ -30,7 +60,7 @@ def test_flow first = create_author(Consts::BOJACK_AUTHOR, Consts::BOJACK_THEME) @query_sql.create_author(name: Consts::DR_SEUSS_AUTHOR, bio: Consts::DR_SEUSS_QUOTE) - assert_equal GetAuthorRow.new(first, Consts::BOJACK_AUTHOR, Consts::BOJACK_THEME), + assert_equal row_for(GetAuthorRow, [first, Consts::BOJACK_AUTHOR, Consts::BOJACK_THEME]), @query_sql.get_author(id: first) assert_nil @query_sql.get_author(id: -1) assert_equal [Consts::BOJACK_AUTHOR, Consts::DR_SEUSS_AUTHOR].sort, @query_sql.list_authors.map(&:name).sort @@ -61,12 +91,12 @@ def test_execresult_returns_rows_affected_and_last_id end def test_batch_commands_run_once_per_args - @query_sql.create_authors_batch([CreateAuthorsBatchArgs.new('a', nil), CreateAuthorsBatchArgs.new('b', 'x'), - CreateAuthorsBatchArgs.new('b', 'y')]) + @query_sql.create_authors_batch([CreateAuthorsBatchArgs.new(name: 'a', bio: nil), CreateAuthorsBatchArgs.new(name: 'b', bio: 'x'), + CreateAuthorsBatchArgs.new(name: 'b', bio: 'y')]) ids = @query_sql.list_authors.to_h { |a| [a.bio, a.id] } - found = @query_sql.get_author_batch([GetAuthorBatchArgs.new(ids['x']), GetAuthorBatchArgs.new(-1)]) + found = @query_sql.get_author_batch([GetAuthorBatchArgs.new(id: ids['x']), GetAuthorBatchArgs.new(id: -1)]) assert_equal ['b', nil], found.map { |r| r&.name } - many = @query_sql.list_authors_by_name_batch(%w[b a c].map { |n| ListAuthorsByNameBatchArgs.new(n) }) + many = @query_sql.list_authors_by_name_batch(%w[b a c].map { |n| ListAuthorsByNameBatchArgs.new(name: n) }) assert_equal [%w[x y], [nil], []], many.map { |rows| rows.map(&:bio) } assert_nil @query_sql.create_authors_batch([]) end @@ -77,7 +107,7 @@ def test_declared_types_with_parameters c_json: '{"a":[1]}', c_uuid: uid) row = @query_sql.get_declared_types assert_equal ['v', 1.5, 7, 'n', 2.5, '{"a":[1]}', uid], row.to_h.values - assert_empty RbsTypes.mismatches(row, File.expand_path('../examples/sqlite3/query_sql.rbs', __dir__), 'GetDeclaredTypesRow') + assert_empty RbsTypes.mismatches(row, File.join(EXAMPLES_DIR, 'sqlite3/query_sql.rbs'), 'GetDeclaredTypesRow') end def test_execrows_returns_affected_row_count @@ -102,6 +132,12 @@ def test_nullable_named_arg assert_equal %w[abc abd], ab.map(&:name).sort end + def test_required_argument_after_optional_named_argument + %w[alpha beta gamma].each { |name| create_author(name) } + assert_equal %w[alpha beta gamma], @query_sql.get_authors_by_optional_name(lim: 10).map(&:name).sort + assert_equal ['beta'], @query_sql.get_authors_by_optional_name(lim: 10, name_filter: 'beta').map(&:name) + end + # ---- sqlc.slice ------------------------------------------------------------------------------------------------ def test_slice @@ -121,6 +157,23 @@ def test_several_slices_in_one_query assert_equal %w[b c], got.map(&:name).sort end + def test_numbered_placeholders_keep_their_bindings_after_slice_expansion + ids = %w[slice-a slice-b slice-c slice-d slice-e].map { |name| create_author(name) } + ids.each do |count| + selected = ids.first(count) + more = ids.reverse + query = @query_sql.get_authors_by_mixed_placeholders(n: 'slice-a', ids: selected, id: 0, more: more) + sql_ids = selected.empty? ? 'NULL' : Array.new(selected.size, '?').join(', ') + more_sql_ids = Array.new(more.size, '?').join(', ') + expected = @db.execute( + "SELECT id, name, bio FROM authors WHERE name = ? AND id IN (#{sql_ids}) AND name = ? " \ + "AND id > ? AND id IN (#{more_sql_ids}) AND 'x?' != '?3' ORDER BY id", + ['slice-a', *selected, 'slice-a', 0, *more] + ).map { |row| row_for(GetAuthorsByMixedPlaceholdersRow, row) } + assert_equal expected, query, "slice count #{count}" + end + end + # ---- :copyfrom ------------------------------------------------------------------------------------------------- def test_copyfrom @@ -151,7 +204,7 @@ def test_embed rows = @query_sql.list_all_authors_books assert_equal %w[a b], rows.map { |r| r.author.name } assert_equal ['book 1', 'book 2'], rows.map { |r| r.book.name } - assert_equal Author.new(first, 'a', 'bio a'), rows.first.author + assert_equal row_for(Author, [first, 'a', 'bio a']), rows.first.author assert_instance_of Book, rows.first.book end @@ -197,7 +250,7 @@ def test_transaction_rolls_back_on_error def test_declared_types_match_runtime_values @db.execute('INSERT INTO authors (id, name, bio) VALUES (1, ?, ?)', ['a', 'b']) @db.execute('INSERT INTO authors (id, name, bio) VALUES (2, ?, NULL)', ['c']) - rbs = File.expand_path('../examples/sqlite3/query_sql.rbs', __dir__) + rbs = File.join(EXAMPLES_DIR, 'sqlite3/query_sql.rbs') @query_sql.list_authors.each { |row| assert_empty RbsTypes.mismatches(row, rbs, 'ListAuthorsRow') } end @@ -336,17 +389,80 @@ def test_transaction_left_with_break_or_throw_commits end end refute @db.transaction_active? - assert_equal ['break 0', 'break 1', 'throw'], @query_sql.list_authors.map(&:name).sort + expected = old_timeout_version? ? [] : ['break 0', 'break 1', 'throw'] + assert_equal expected, @query_sql.list_authors.map(&:name).sort + end + + def test_transaction_timeout_rolls_back_and_keeps_connection_usable + [:database, :ruby_sleep].each do |kind| + assert_raises(Timeout::Error) do + Timeout.timeout(0.05) do + @query_sql.transaction do |tx| + tx.create_author_return_id(name: kind.to_s, bio: nil) + kind == :database ? tx.pause_query(sleep: 0.2) : sleep(0.2) + end + end + end + assert_empty @query_sql.list_authors + @query_sql.create_author_return_id(name: 'usable', bio: nil) + assert_equal ['usable'], @query_sql.list_authors.map(&:name) + @query_sql.truncate_authors + end + end + + def test_transaction_nonlocal_exit_semantics_follow_timeout_version + break_value = @query_sql.transaction do |tx| + tx.create_author_return_id(name: 'break', bio: nil) + break :break_value + end + assert_equal :break_value, break_value + assert_equal :returned, return_from_transaction('return') + thrown = catch(:transaction_exit) do + @query_sql.transaction do |tx| + tx.create_author_return_id(name: 'throw', bio: nil) + throw :transaction_exit, :throw_value + end + end + assert_equal :throw_value, thrown + expected = old_timeout_version? ? [] : %w[break return throw] + assert_equal expected, @query_sql.list_authors.map(&:name).sort + end + + def test_thread_kill_rolls_back_and_leaves_connection_inactive + started = Queue.new + thread = Thread.new do + @query_sql.transaction do |tx| + tx.create_author_return_id(name: 'killed', bio: nil) + started << true + sleep(5) + end + end + started.pop + thread.kill + thread.join + assert_empty @query_sql.list_authors + refute @db.transaction_active? + end + + def test_parent_query_object_calls_are_part_of_same_thread_transaction + assert_raises(RuntimeError) do + @query_sql.transaction do |tx| + tx.create_author_return_id(name: 'tx', bio: nil) + @query_sql.create_author_return_id(name: 'parent', bio: nil) + raise 'rollback outer transaction' + end + end + assert_empty @query_sql.list_authors end # ---- final sweep fixes: :copyfrom conflict clauses, nested modes ------------------------------------------------ def test_copyfrom_keeps_conflict_clauses - @query_sql.copy_tags_or_ignore([CopyTagsOrIgnoreArgs.new('a', 1), CopyTagsOrIgnoreArgs.new('a', 2)]) + @query_sql.copy_tags_or_ignore([CopyTagsOrIgnoreArgs.new(name: 'a', uses: 1), CopyTagsOrIgnoreArgs.new(name: 'a', uses: 2)]) assert_equal [['a', 1]], @query_sql.list_tags.map { |t| [t.name, t.uses] } - @query_sql.copy_tags_or_replace([CopyTagsOrReplaceArgs.new('a', 5), CopyTagsOrReplaceArgs.new('b', 1)]) + @query_sql.copy_tags_or_replace([CopyTagsOrReplaceArgs.new(name: 'a', uses: 5), CopyTagsOrReplaceArgs.new(name: 'b', uses: 1)]) assert_equal [['a', 5], ['b', 1]], @query_sql.list_tags.map { |t| [t.name, t.uses] } - @query_sql.copy_tags_adding_uses([CopyTagsAddingUsesArgs.new('a', 1), CopyTagsAddingUsesArgs.new('c', 1)]) + @query_sql.copy_tags_adding_uses([CopyTagsAddingUsesArgs.new(name: 'a', uses: 1), CopyTagsAddingUsesArgs.new(name: 'c', uses: 1)]) assert_equal [['a', 6], ['b', 1], ['c', 1]], @query_sql.list_tags.map { |t| [t.name, t.uses] } end @@ -355,4 +471,81 @@ def test_nested_transaction_rejects_a_mode assert_raises(ArgumentError) { tx.transaction(mode: :immediate) { nil } } end end + + def test_seeded_random_copyfrom_round_trip_and_duplicate_is_atomic + random = seeded_random + rows = Array.new(300) do |i| + CopyRegressionRowsArgs.new(id: 5_000_000_000 + i, order: "#{i}#{random_text(random)}", + price: random.rand(3).zero? ? nil : random.rand(1..100), + payload: random.bytes(random.rand(0..64)).b) + end + @query_sql.copy_regression_rows(rows) + expected = rows.map { |r| [r.id, r.order, r.price, r.payload] }.sort_by(&:first) + actual = @query_sql.list_regression_rows.map { |r| [r.id, r.order, r.price, r.payload] } + assert_equal expected, actual + + duplicate = [CopyRegressionRowsArgs.new(id: 6_000_000_000, order: 'new', price: nil, payload: nil), rows.first] + assert_raises(SQLite3::ConstraintException) { @query_sql.copy_regression_rows(duplicate) } + assert_equal expected, @query_sql.list_regression_rows.map { |r| [r.id, r.order, r.price, r.payload] } + end + + def test_seeded_random_batch_round_trip + random = seeded_random + rows = Array.new(300) do |i| + { name: "#{i}-#{random_text(random)}", bio: random.rand(3).zero? ? nil : random_text(random) } + end + @query_sql.create_authors_batch(rows.map { |row| CreateAuthorsBatchArgs.new(**row) }) + stored = @query_sql.list_authors + assert_equal rows.sort_by { |row| row[:name] }, + stored.map { |row| { name: row.name, bio: row.bio } }.sort_by { |row| row[:name] } + one = @query_sql.get_author_batch(stored.map { |row| GetAuthorBatchArgs.new(id: row.id) }) + assert_equal stored.map(&:name).sort, one.map(&:name).sort + many = @query_sql.list_authors_by_name_batch(stored.map { |row| ListAuthorsByNameBatchArgs.new(name: row.name) }) + assert_equal stored.map(&:name).sort, many.flatten.map(&:name).sort + end + + def test_seeded_concurrent_transactions_commit_the_exact_planned_set + random = seeded_random + plan = Array.new(4) { Array.new(8) { random.rand(4) } } + names = plan.each_with_index.map do |actions, thread_index| + actions.each_index.map { |index| "thread-#{thread_index}-#{index}" } + end + expected = plan.each_with_index.flat_map do |actions, thread_index| + actions.each_with_index.flat_map do |action, index| + name = names[thread_index][index] + action == 1 ? [] : (action == 3 ? [name, "#{name}-batch"] : [name]) + end + end.sort + threads = plan.each_with_index.map do |actions, thread_index| + Thread.new do + actions.each_with_index do |action, index| + name = names[thread_index][index] + begin + @query_sql.transaction do |tx| + tx.create_author_return_id(name: name, bio: nil) + case action + when 1 + raise "rollback #{name}" + when 2 + begin + tx.transaction do |inner| + inner.create_author_return_id(name: "#{name}-nested", bio: nil) + raise "savepoint #{name}" + end + rescue RuntimeError + nil + end + when 3 + tx.create_authors_batch([CreateAuthorsBatchArgs.new(name: "#{name}-batch", bio: "batch #{name}")]) + end + end + rescue RuntimeError => error + raise unless action == 1 && error.message == "rollback #{name}" + end + end + end + end + threads.each(&:value) + assert_equal expected, @query_sql.list_authors.map(&:name).sort + end end diff --git a/tests/rbs_types.rb b/tests/rbs_types.rb index 8fb14e3..0d1b710 100644 --- a/tests/rbs_types.rb +++ b/tests/rbs_types.rb @@ -10,7 +10,8 @@ module RbsTypes # { 'c_date' => 'Date?', ... } for `class ` in an .rbs file def attributes(rbs_path, class_name) - body = File.read(rbs_path)[/^( *)class #{class_name}\n(.*?)^\1end$/m, 2] or raise "#{class_name} not in #{rbs_path}" + body = File.read(rbs_path)[/^( *)class #{Regexp.escape(class_name)}(?: < [^\n]+)?\n(.*?)^\1end$/m, 2] or + raise "#{class_name} not in #{rbs_path}" body.scan(/attr_(?:reader|accessor) (\w+): (.+)$/).to_h end From 611384a496d8c94c449715deb2e97d19e9bcf405 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 19:09:01 +0000 Subject: [PATCH 2/9] Fix SQLite numbered parameter binding Co-Authored-By: Doron Eli Rachman --- .github/workflows/tests.yml | 6 +- README.md | 5 +- examples/authors/sqlite/query.sql | 20 ++++ examples/sqlite3/query_sql.rb | 88 ++++++++++++++++-- examples/sqlite3/query_sql.rbs | 37 ++++++++ lib/sqlc_gen_ruby/drivers/base.rb | 7 +- lib/sqlc_gen_ruby/drivers/sqlite3.rb | 17 ++-- lib/sqlc_gen_ruby/method_gen.rb | 2 +- test/fixtures/sqlite3.bin | Bin 11926 -> 13066 bytes test/golden/sqlite3/default/query_sql.rb | 88 ++++++++++++++++-- test/golden/sqlite3/default/query_sql.rbs | 37 ++++++++ .../sqlite3/ruby31_gemfile/query_sql.rb | 84 +++++++++++++++-- .../sqlite3/ruby31_gemfile/query_sql.rbs | 41 ++++++++ .../sqlite3/ruby32_notypes/query_sql.rb | 88 ++++++++++++++++-- test/sqlite_placeholders_test.rb | 49 +++++++++- tests/end2end_sqlite3.rb | 44 +++++++++ 16 files changed, 573 insertions(+), 40 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 4928920..a3637f4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -69,7 +69,7 @@ jobs: strategy: fail-fast: false matrix: - ruby-version: ['3.1', '3.2', '3.4', '4.0'] + ruby-version: ['3.1', '3.2', '3.3', '3.4', '4.0'] services: postgres: @@ -128,7 +128,7 @@ jobs: strategy: fail-fast: false matrix: - ruby-version: ['3.1', '3.2', '3.4', '4.0'] + ruby-version: ['3.1', '3.2', '3.3', '3.4', '4.0'] services: mysql: @@ -186,7 +186,7 @@ jobs: strategy: fail-fast: false matrix: - ruby-version: ['3.1', '3.2', '3.4', '4.0'] + ruby-version: ['3.1', '3.2', '3.3', '3.4', '4.0'] env: BUNDLE_GEMFILE: ${{ (matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2') && 'Gemfile.e2e' || 'Gemfile' }} diff --git a/README.md b/README.md index e5e2baf..153c105 100644 --- a/README.md +++ b/README.md @@ -118,8 +118,9 @@ end * Everything in the block runs on one connection. It commits when the block ends (also on `break`, `return`, `throw`) and rolls back when it raises or the thread is killed. On Ruby <3.3 with timeout <0.4, `break`, `return`, and `throw` also roll back because Timeout cannot be told apart; add `gem 'timeout', '>= 0.4'` to the generated Gemfile - to keep those exits committing. A nested `transaction` rolls back only its own part. Query methods called on the - parent query object from the same thread also use the transaction's connection. + to keep those exits committing. A nested `transaction` rolls back only its own part. With `connection_pool` or + SQLite, parent-object calls from the same thread reuse the transaction connection; injected `#with` objects must do + the same. * `isolation:` (`pg`, `mysql2`) takes `:read_uncommitted`, `:read_committed`, `:repeatable_read`, `:serializable`; `mode:` (`sqlite3`) takes `:deferred`, `:immediate`, `:exclusive`. Other values raise before anything runs, and so do `isolation:` / `mode:` on a nested `transaction` (a savepoint cannot change them). diff --git a/examples/authors/sqlite/query.sql b/examples/authors/sqlite/query.sql index a6fab79..4f093d7 100644 --- a/examples/authors/sqlite/query.sql +++ b/examples/authors/sqlite/query.sql @@ -58,10 +58,30 @@ WHERE name = sqlc.arg(n) AND 'x?' != '?3' ORDER BY id; +-- name: GetAuthorsByRepeatedNamedAndPositional :many +SELECT * FROM authors +WHERE name = sqlc.arg(n) + AND name != sqlc.arg(other) + AND id > ? + AND name = sqlc.arg(n) +ORDER BY id; + +-- name: GetAuthorsByRepeatedNamedAndPositionalBatch :batchone +SELECT * FROM authors +WHERE name = sqlc.arg(n) + AND name != sqlc.arg(other) + AND id > ? + AND name = sqlc.arg(n) +ORDER BY id +LIMIT 1; + -- name: CopyAuthors :copyfrom -- Bulk insert: pass an Array of CopyAuthorsArgs. INSERT INTO authors (name, bio) VALUES (?, ?); +-- name: CopyAuthorsNamed :copyfrom +INSERT INTO authors (name, bio) VALUES (sqlc.arg(name), sqlc.arg(bio)); + -- name: TruncateAuthors :exec DELETE FROM authors; diff --git a/examples/sqlite3/query_sql.rb b/examples/sqlite3/query_sql.rb index 9e3c27b..29a54c0 100644 --- a/examples/sqlite3/query_sql.rb +++ b/examples/sqlite3/query_sql.rb @@ -40,7 +40,7 @@ class ListAuthorsRow < Data.define(:id, :name, :bio) LIST_AUTHORS_PAGED_SQL = <<~'SQL' SELECT id, name, bio FROM authors ORDER BY name - LIMIT ?2 OFFSET ?1 + LIMIT ? OFFSET ? SQL class ListAuthorsPagedRow < Data.define(:id, :name, :bio) @@ -48,7 +48,7 @@ class ListAuthorsPagedRow < Data.define(:id, :name, :bio) GET_AUTHOR_BY_NAME_PATTERN_SQL = <<~'SQL' SELECT id, name, bio FROM authors - WHERE name LIKE COALESCE(?1, '%') + WHERE name LIKE COALESCE(?, '%') SQL class GetAuthorByNamePatternRow < Data.define(:id, :name, :bio) @@ -56,9 +56,9 @@ class GetAuthorByNamePatternRow < Data.define(:id, :name, :bio) GET_AUTHORS_BY_OPTIONAL_NAME_SQL = <<~'SQL' SELECT id, name, bio FROM authors - WHERE (?1 IS NULL OR name = ?1) + WHERE (? IS NULL OR name = ?) ORDER BY id - LIMIT ?2 + LIMIT ? SQL class GetAuthorsByOptionalNameRow < Data.define(:id, :name, :bio) @@ -105,9 +105,40 @@ class GetAuthorsByIdsAndNamesRow < Data.define(:id, :name, :bio) class GetAuthorsByMixedPlaceholdersRow < Data.define(:id, :name, :bio) end + GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE name = ? + AND name != ? + AND id > ? + AND name = ? + ORDER BY id + SQL + + class GetAuthorsByRepeatedNamedAndPositionalRow < Data.define(:id, :name, :bio) + end + + GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_BATCH_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE name = ? + AND name != ? + AND id > ? + AND name = ? + ORDER BY id + LIMIT 1 + SQL + + class GetAuthorsByRepeatedNamedAndPositionalBatchRow < Data.define(:id, :name, :bio) + end + + class GetAuthorsByRepeatedNamedAndPositionalBatchArgs < Data.define(:n, :other, :id) + end + class CopyAuthorsArgs < Data.define(:name, :bio) end + class CopyAuthorsNamedArgs < Data.define(:name, :bio) + end + TRUNCATE_AUTHORS_SQL = <<~'SQL' DELETE FROM authors SQL @@ -392,7 +423,7 @@ def delete_author(id:) def list_authors_paged(offset:, limit:) with_client('list_authors_paged') do |client| - query_params = [offset, limit] + query_params = [limit, offset] stmt = client.prepare(LIST_AUTHORS_PAGED_SQL) begin result = stmt.execute(*query_params) @@ -422,7 +453,7 @@ 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] + query_params = [name_filter, name_filter, lim] stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) begin result = stmt.execute(*query_params) @@ -521,6 +552,39 @@ def get_authors_by_mixed_placeholders(n:, ids:, id:, more:) end # rubocop:enable Naming/MethodParameterName + # rubocop:disable Naming/MethodParameterName + def get_authors_by_repeated_named_and_positional(n:, other:, id:) + with_client('get_authors_by_repeated_named_and_positional') do |client| + query_params = [n, other, id, n] + stmt = client.prepare(GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_SQL) + begin + result = stmt.execute(*query_params) + result.map do |row| + GetAuthorsByRepeatedNamedAndPositionalRow.new(row[0], row[1], row[2]) + end + ensure + stmt.close + end + end + end + # rubocop:enable Naming/MethodParameterName + + def get_authors_by_repeated_named_and_positional_batch(rows) + with_client('get_authors_by_repeated_named_and_positional_batch') do |client| + stmt = client.prepare(GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_BATCH_SQL) + begin + rows.map do |args| + query_params = [args.n, args.other, args.id, args.n] + result = stmt.execute(*query_params) + row = result.first + row && GetAuthorsByRepeatedNamedAndPositionalBatchRow.new(row[0], row[1], row[2]) + end + ensure + stmt.close + end + end + end + # Bulk insert: pass an Array of CopyAuthorsArgs. def copy_authors(rows) with_client('copy_authors') do |client| @@ -534,6 +598,18 @@ def copy_authors(rows) end end + def copy_authors_named(rows) + with_client('copy_authors_named') do |client| + atomically(client) do + rows.each_slice(499) do |batch| + sql = %(INSERT INTO "authors" ("name", "bio") VALUES #{Array.new(batch.size, '(?, ?)').join(', ')}) + client.execute(sql, batch.flat_map { |row| [row.name, row.bio] }) + end + end + nil + end + end + def truncate_authors with_client('truncate_authors') do |client| stmt = client.prepare(TRUNCATE_AUTHORS_SQL) diff --git a/examples/sqlite3/query_sql.rbs b/examples/sqlite3/query_sql.rbs index 17d95ff..649eba3 100644 --- a/examples/sqlite3/query_sql.rbs +++ b/examples/sqlite3/query_sql.rbs @@ -85,12 +85,43 @@ module Sqlite3Codegen def initialize: ( Integer id, String name, String? bio) -> void end + GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_SQL: String + + class GetAuthorsByRepeatedNamedAndPositionalRow + attr_reader id: Integer + attr_reader name: String + attr_reader bio: String? + def initialize: ( Integer id, String name, String? bio) -> void + end + + GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_BATCH_SQL: String + + class GetAuthorsByRepeatedNamedAndPositionalBatchRow + attr_reader id: Integer + attr_reader name: String + attr_reader bio: String? + def initialize: ( Integer id, String name, String? bio) -> void + end + + class GetAuthorsByRepeatedNamedAndPositionalBatchArgs + attr_reader n: String + attr_reader other: String + attr_reader id: Integer + def initialize: ( String n, String other, Integer id) -> void + end + class CopyAuthorsArgs attr_reader name: String attr_reader bio: String? def initialize: ( String name, String? bio) -> void end + class CopyAuthorsNamedArgs + attr_reader name: String + attr_reader bio: String? + def initialize: ( String name, String? bio) -> void + end + TRUNCATE_AUTHORS_SQL: String CREATE_BOOK_SQL: String @@ -322,8 +353,14 @@ module Sqlite3Codegen def get_authors_by_mixed_placeholders: (n: String, ids: Array[Integer], id: Integer, more: Array[Integer]) -> Array[GetAuthorsByMixedPlaceholdersRow] + def get_authors_by_repeated_named_and_positional: (n: String, other: String, id: Integer) -> Array[GetAuthorsByRepeatedNamedAndPositionalRow] + + def get_authors_by_repeated_named_and_positional_batch: (Array[GetAuthorsByRepeatedNamedAndPositionalBatchArgs] rows) -> Array[GetAuthorsByRepeatedNamedAndPositionalBatchRow?] + def copy_authors: (Array[CopyAuthorsArgs] rows) -> void + def copy_authors_named: (Array[CopyAuthorsNamedArgs] rows) -> void + def truncate_authors: -> void def create_book: (name: String, author_id: Integer) -> Integer diff --git a/lib/sqlc_gen_ruby/drivers/base.rb b/lib/sqlc_gen_ruby/drivers/base.rb index 5f774ef..c321c61 100644 --- a/lib/sqlc_gen_ruby/drivers/base.rb +++ b/lib/sqlc_gen_ruby/drivers/base.rb @@ -46,8 +46,11 @@ def client_block(func) = "with_client('#{func}') do |client|" def query_text(query) = query.text # Ruby expressions for the parameters passed to one generated query method. - def query_params(_query, params) - list = params.map { |p| p.slice ? "*#{p.name}" : encode_param(p.name, p.column) } + def query_params(_query, params, value_prefix: '') + list = params.map do |p| + value = "#{value_prefix}#{p.name}" + p.slice ? "*#{value}" : encode_param(value, p.column) + end "query_params = [#{Code.comma_join(list)}]" end diff --git a/lib/sqlc_gen_ruby/drivers/sqlite3.rb b/lib/sqlc_gen_ruby/drivers/sqlite3.rb index 24037ee..1e89273 100644 --- a/lib/sqlc_gen_ruby/drivers/sqlite3.rb +++ b/lib/sqlc_gen_ruby/drivers/sqlite3.rb @@ -57,19 +57,21 @@ def closes_statements? = true def supports_slice? = true def query_text(query) - return query.text unless query.params.any? { |param| param.column.is_sqlc_slice } - replacements = placeholder_occurrences(query.text).filter_map do |kind, number, first, last| [first, last] if kind == :numbered end + return query.text if replacements.empty? + replacements.reverse_each.reduce(query.text.dup) { |sql, (first, last)| sql[0...first] + '?' + sql[last..] } end - def query_params(query, params) - return super unless query.params.any? { |param| param.column.is_sqlc_slice } + def query_params(query, params, value_prefix: '') + occurrences = placeholder_occurrences(query.text) + unless occurrences.any? { |kind,| kind == :numbered || kind == :slice } + return super(query, params, value_prefix: value_prefix) + end by_number = query.params.zip(params).to_h { |source, param| [source.number, param] } - occurrences = placeholder_occurrences(query.text) referenced = occurrences.filter_map { |kind, number,| number if kind == :numbered } slices = occurrences.filter_map { |kind, name,| name if kind == :slice } positional = query.params.each_with_index.filter_map do |source, index| @@ -89,7 +91,10 @@ def query_params(query, params) param end end - expressions = ordered.map { |param| param.slice ? "*#{param.name}" : encode_param(param.name, param.column) } + expressions = ordered.map do |param| + value = "#{value_prefix}#{param.name}" + param.slice ? "*#{value}" : encode_param(value, param.column) + end "query_params = [#{Code.comma_join(expressions)}]" end diff --git a/lib/sqlc_gen_ruby/method_gen.rb b/lib/sqlc_gen_ruby/method_gen.rb index cc82d02..bb9ff6b 100644 --- a/lib/sqlc_gen_ruby/method_gen.rb +++ b/lib/sqlc_gen_ruby/method_gen.rb @@ -225,7 +225,7 @@ def batch_statements(query, func, params) prepare = @driver.prepare_stmt(stmt_name, const) cmd = BATCH_COMMANDS.fetch(query.cmd) body = [] - body << "query_params = [#{Code.comma_join(params.map { |p| @driver.encode_param("args.#{p.name}", p.column) })}]" unless params.empty? + body << @driver.query_params(query, params, value_prefix: 'args.') unless params.empty? commands = command_statements(query, stmt_name, const, !params.empty?, cmd: cmd) commands.pop if cmd == ':exec' # the trailing nil if @driver.prepares_batch_rows? diff --git a/test/fixtures/sqlite3.bin b/test/fixtures/sqlite3.bin index 4080fffb5d5173bc590a14db47dd5d5a3b5cb48b..a68e0e34a44b574b5ada5c9afb27c2edbc9cac99 100644 GIT binary patch delta 401 zcmbOh+m*IKRfy#A$=~^{F02+qRHHHWHR|9t1zR* ? + AND name = ? + ORDER BY id + SQL + + class GetAuthorsByRepeatedNamedAndPositionalRow < Data.define(:id, :name, :bio) + end + + GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_BATCH_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE name = ? + AND name != ? + AND id > ? + AND name = ? + ORDER BY id + LIMIT 1 + SQL + + class GetAuthorsByRepeatedNamedAndPositionalBatchRow < Data.define(:id, :name, :bio) + end + + class GetAuthorsByRepeatedNamedAndPositionalBatchArgs < Data.define(:n, :other, :id) + end + class CopyAuthorsArgs < Data.define(:name, :bio) end + class CopyAuthorsNamedArgs < Data.define(:name, :bio) + end + TRUNCATE_AUTHORS_SQL = <<~'SQL' DELETE FROM authors SQL @@ -422,7 +453,7 @@ def delete_author(id:) def list_authors_paged(offset:, limit:) with_client('list_authors_paged') do |client| - query_params = [offset, limit] + query_params = [limit, offset] stmt = client.prepare(LIST_AUTHORS_PAGED_SQL) begin result = stmt.execute(*query_params) @@ -452,7 +483,7 @@ 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] + query_params = [name_filter, name_filter, lim] stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) begin result = stmt.execute(*query_params) @@ -551,6 +582,39 @@ def get_authors_by_mixed_placeholders(n:, ids:, id:, more:) end # rubocop:enable Naming/MethodParameterName + # rubocop:disable Naming/MethodParameterName + def get_authors_by_repeated_named_and_positional(n:, other:, id:) + with_client('get_authors_by_repeated_named_and_positional') do |client| + query_params = [n, other, id, n] + stmt = client.prepare(GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_SQL) + begin + result = stmt.execute(*query_params) + result.map do |row| + GetAuthorsByRepeatedNamedAndPositionalRow.new(row[0], row[1], row[2]) + end + ensure + stmt.close + end + end + end + # rubocop:enable Naming/MethodParameterName + + def get_authors_by_repeated_named_and_positional_batch(rows) + with_client('get_authors_by_repeated_named_and_positional_batch') do |client| + stmt = client.prepare(GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_BATCH_SQL) + begin + rows.map do |args| + query_params = [args.n, args.other, args.id, args.n] + result = stmt.execute(*query_params) + row = result.first + row && GetAuthorsByRepeatedNamedAndPositionalBatchRow.new(row[0], row[1], row[2]) + end + ensure + stmt.close + end + end + end + # Bulk insert: pass an Array of CopyAuthorsArgs. def copy_authors(rows) with_client('copy_authors') do |client| @@ -564,6 +628,18 @@ def copy_authors(rows) end end + def copy_authors_named(rows) + with_client('copy_authors_named') do |client| + atomically(client) do + rows.each_slice(499) do |batch| + sql = %(INSERT INTO "authors" ("name", "bio") VALUES #{Array.new(batch.size, '(?, ?)').join(', ')}) + client.execute(sql, batch.flat_map { |row| [row.name, row.bio] }) + end + end + nil + end + end + def truncate_authors with_client('truncate_authors') do |client| stmt = client.prepare(TRUNCATE_AUTHORS_SQL) diff --git a/test/golden/sqlite3/default/query_sql.rbs b/test/golden/sqlite3/default/query_sql.rbs index e324931..e15166b 100644 --- a/test/golden/sqlite3/default/query_sql.rbs +++ b/test/golden/sqlite3/default/query_sql.rbs @@ -85,12 +85,43 @@ module Sqlite3Codegen def initialize: ( Integer id, String name, String? bio) -> void end + GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_SQL: String + + class GetAuthorsByRepeatedNamedAndPositionalRow + attr_reader id: Integer + attr_reader name: String + attr_reader bio: String? + def initialize: ( Integer id, String name, String? bio) -> void + end + + GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_BATCH_SQL: String + + class GetAuthorsByRepeatedNamedAndPositionalBatchRow + attr_reader id: Integer + attr_reader name: String + attr_reader bio: String? + def initialize: ( Integer id, String name, String? bio) -> void + end + + class GetAuthorsByRepeatedNamedAndPositionalBatchArgs + attr_reader n: String + attr_reader other: String + attr_reader id: Integer + def initialize: ( String n, String other, Integer id) -> void + end + class CopyAuthorsArgs attr_reader name: String attr_reader bio: String? def initialize: ( String name, String? bio) -> void end + class CopyAuthorsNamedArgs + attr_reader name: String + attr_reader bio: String? + def initialize: ( String name, String? bio) -> void + end + TRUNCATE_AUTHORS_SQL: String CREATE_BOOK_SQL: String @@ -357,8 +388,14 @@ module Sqlite3Codegen def get_authors_by_mixed_placeholders: (n: String, ids: Array[Integer], id: Integer, more: Array[Integer]) -> Array[GetAuthorsByMixedPlaceholdersRow] + def get_authors_by_repeated_named_and_positional: (n: String, other: String, id: Integer) -> Array[GetAuthorsByRepeatedNamedAndPositionalRow] + + def get_authors_by_repeated_named_and_positional_batch: (Array[GetAuthorsByRepeatedNamedAndPositionalBatchArgs] rows) -> Array[GetAuthorsByRepeatedNamedAndPositionalBatchRow?] + def copy_authors: (Array[CopyAuthorsArgs] rows) -> void + def copy_authors_named: (Array[CopyAuthorsNamedArgs] rows) -> void + def truncate_authors: -> void def create_book: (name: String, author_id: Integer) -> Integer diff --git a/test/golden/sqlite3/ruby31_gemfile/query_sql.rb b/test/golden/sqlite3/ruby31_gemfile/query_sql.rb index b6c3f5b..a65a317 100644 --- a/test/golden/sqlite3/ruby31_gemfile/query_sql.rb +++ b/test/golden/sqlite3/ruby31_gemfile/query_sql.rb @@ -38,23 +38,23 @@ module Sqlite3Codegen LIST_AUTHORS_PAGED_SQL = <<~'SQL' SELECT id, name, bio FROM authors ORDER BY name - LIMIT ?2 OFFSET ?1 + LIMIT ? OFFSET ? SQL ListAuthorsPagedRow = Struct.new(:id, :name, :bio, keyword_init: true) GET_AUTHOR_BY_NAME_PATTERN_SQL = <<~'SQL' SELECT id, name, bio FROM authors - WHERE name LIKE COALESCE(?1, '%') + WHERE name LIKE COALESCE(?, '%') SQL GetAuthorByNamePatternRow = Struct.new(:id, :name, :bio, keyword_init: true) GET_AUTHORS_BY_OPTIONAL_NAME_SQL = <<~'SQL' SELECT id, name, bio FROM authors - WHERE (?1 IS NULL OR name = ?1) + WHERE (? IS NULL OR name = ?) ORDER BY id - LIMIT ?2 + LIMIT ? SQL GetAuthorsByOptionalNameRow = Struct.new(:id, :name, :bio, keyword_init: true) @@ -96,8 +96,35 @@ module Sqlite3Codegen GetAuthorsByMixedPlaceholdersRow = Struct.new(:id, :name, :bio, keyword_init: true) + GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE name = ? + AND name != ? + AND id > ? + AND name = ? + ORDER BY id + SQL + + GetAuthorsByRepeatedNamedAndPositionalRow = Struct.new(:id, :name, :bio, keyword_init: true) + + GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_BATCH_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE name = ? + AND name != ? + AND id > ? + AND name = ? + ORDER BY id + LIMIT 1 + SQL + + GetAuthorsByRepeatedNamedAndPositionalBatchRow = Struct.new(:id, :name, :bio, keyword_init: true) + + GetAuthorsByRepeatedNamedAndPositionalBatchArgs = Struct.new(:n, :other, :id, keyword_init: true) + CopyAuthorsArgs = Struct.new(:name, :bio, keyword_init: true) + CopyAuthorsNamedArgs = Struct.new(:name, :bio, keyword_init: true) + TRUNCATE_AUTHORS_SQL = <<~'SQL' DELETE FROM authors SQL @@ -415,7 +442,7 @@ def delete_author(id:) def list_authors_paged(offset:, limit:) with_client('list_authors_paged') do |client| - query_params = [offset, limit] + query_params = [limit, offset] stmt = client.prepare(LIST_AUTHORS_PAGED_SQL) begin result = stmt.execute(*query_params) @@ -445,7 +472,7 @@ 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] + query_params = [name_filter, name_filter, lim] stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) begin result = stmt.execute(*query_params) @@ -544,6 +571,39 @@ def get_authors_by_mixed_placeholders(n:, ids:, id:, more:) end # rubocop:enable Naming/MethodParameterName + # rubocop:disable Naming/MethodParameterName + def get_authors_by_repeated_named_and_positional(n:, other:, id:) + with_client('get_authors_by_repeated_named_and_positional') do |client| + query_params = [n, other, id, n] + stmt = client.prepare(GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_SQL) + begin + result = stmt.execute(*query_params) + result.map do |row| + GetAuthorsByRepeatedNamedAndPositionalRow.new(id: row[0], name: row[1], bio: row[2]) + end + ensure + stmt.close + end + end + end + # rubocop:enable Naming/MethodParameterName + + def get_authors_by_repeated_named_and_positional_batch(rows) + with_client('get_authors_by_repeated_named_and_positional_batch') do |client| + stmt = client.prepare(GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_BATCH_SQL) + begin + rows.map do |args| + query_params = [args.n, args.other, args.id, args.n] + result = stmt.execute(*query_params) + row = result.first + row && GetAuthorsByRepeatedNamedAndPositionalBatchRow.new(id: row[0], name: row[1], bio: row[2]) + end + ensure + stmt.close + end + end + end + # Bulk insert: pass an Array of CopyAuthorsArgs. def copy_authors(rows) with_client('copy_authors') do |client| @@ -557,6 +617,18 @@ def copy_authors(rows) end end + def copy_authors_named(rows) + with_client('copy_authors_named') do |client| + atomically(client) do + rows.each_slice(499) do |batch| + sql = %(INSERT INTO "authors" ("name", "bio") VALUES #{Array.new(batch.size, '(?, ?)').join(', ')}) + client.execute(sql, batch.flat_map { |row| [row.name, row.bio] }) + end + end + nil + end + end + def truncate_authors with_client('truncate_authors') do |client| stmt = client.prepare(TRUNCATE_AUTHORS_SQL) diff --git a/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs b/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs index 797904b..cd033b0 100644 --- a/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs +++ b/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs @@ -94,6 +94,34 @@ module Sqlite3Codegen def self.new: ( id: Integer, name: String, bio: String?) -> GetAuthorsByMixedPlaceholdersRow end + GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_SQL: String + + class GetAuthorsByRepeatedNamedAndPositionalRow < ::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?) -> GetAuthorsByRepeatedNamedAndPositionalRow + end + + GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_BATCH_SQL: String + + class GetAuthorsByRepeatedNamedAndPositionalBatchRow < ::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?) -> GetAuthorsByRepeatedNamedAndPositionalBatchRow + end + + class GetAuthorsByRepeatedNamedAndPositionalBatchArgs < ::Struct[untyped] + attr_accessor n: String + attr_accessor other: String + attr_accessor id: Integer + def initialize: ( n: String, other: String, id: Integer) -> void + def self.new: ( n: String, other: String, id: Integer) -> GetAuthorsByRepeatedNamedAndPositionalBatchArgs + end + class CopyAuthorsArgs < ::Struct[untyped] attr_accessor name: String attr_accessor bio: String? @@ -101,6 +129,13 @@ module Sqlite3Codegen def self.new: ( name: String, bio: String?) -> CopyAuthorsArgs end + class CopyAuthorsNamedArgs < ::Struct[untyped] + attr_accessor name: String + attr_accessor bio: String? + def initialize: ( name: String, bio: String?) -> void + def self.new: ( name: String, bio: String?) -> CopyAuthorsNamedArgs + end + TRUNCATE_AUTHORS_SQL: String CREATE_BOOK_SQL: String @@ -407,8 +442,14 @@ module Sqlite3Codegen def get_authors_by_mixed_placeholders: (n: String, ids: Array[Integer], id: Integer, more: Array[Integer]) -> Array[GetAuthorsByMixedPlaceholdersRow] + def get_authors_by_repeated_named_and_positional: (n: String, other: String, id: Integer) -> Array[GetAuthorsByRepeatedNamedAndPositionalRow] + + def get_authors_by_repeated_named_and_positional_batch: (Array[GetAuthorsByRepeatedNamedAndPositionalBatchArgs] rows) -> Array[GetAuthorsByRepeatedNamedAndPositionalBatchRow?] + def copy_authors: (Array[CopyAuthorsArgs] rows) -> void + def copy_authors_named: (Array[CopyAuthorsNamedArgs] rows) -> void + def truncate_authors: -> void def create_book: (name: String, author_id: Integer) -> Integer diff --git a/test/golden/sqlite3/ruby32_notypes/query_sql.rb b/test/golden/sqlite3/ruby32_notypes/query_sql.rb index 1e1c294..5c2671e 100644 --- a/test/golden/sqlite3/ruby32_notypes/query_sql.rb +++ b/test/golden/sqlite3/ruby32_notypes/query_sql.rb @@ -40,7 +40,7 @@ class ListAuthorsRow < Data.define(:id, :name, :bio) LIST_AUTHORS_PAGED_SQL = <<~'SQL' SELECT id, name, bio FROM authors ORDER BY name - LIMIT ?2 OFFSET ?1 + LIMIT ? OFFSET ? SQL class ListAuthorsPagedRow < Data.define(:id, :name, :bio) @@ -48,7 +48,7 @@ class ListAuthorsPagedRow < Data.define(:id, :name, :bio) GET_AUTHOR_BY_NAME_PATTERN_SQL = <<~'SQL' SELECT id, name, bio FROM authors - WHERE name LIKE COALESCE(?1, '%') + WHERE name LIKE COALESCE(?, '%') SQL class GetAuthorByNamePatternRow < Data.define(:id, :name, :bio) @@ -56,9 +56,9 @@ class GetAuthorByNamePatternRow < Data.define(:id, :name, :bio) GET_AUTHORS_BY_OPTIONAL_NAME_SQL = <<~'SQL' SELECT id, name, bio FROM authors - WHERE (?1 IS NULL OR name = ?1) + WHERE (? IS NULL OR name = ?) ORDER BY id - LIMIT ?2 + LIMIT ? SQL class GetAuthorsByOptionalNameRow < Data.define(:id, :name, :bio) @@ -105,9 +105,40 @@ class GetAuthorsByIdsAndNamesRow < Data.define(:id, :name, :bio) class GetAuthorsByMixedPlaceholdersRow < Data.define(:id, :name, :bio) end + GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE name = ? + AND name != ? + AND id > ? + AND name = ? + ORDER BY id + SQL + + class GetAuthorsByRepeatedNamedAndPositionalRow < Data.define(:id, :name, :bio) + end + + GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_BATCH_SQL = <<~'SQL' + SELECT id, name, bio FROM authors + WHERE name = ? + AND name != ? + AND id > ? + AND name = ? + ORDER BY id + LIMIT 1 + SQL + + class GetAuthorsByRepeatedNamedAndPositionalBatchRow < Data.define(:id, :name, :bio) + end + + class GetAuthorsByRepeatedNamedAndPositionalBatchArgs < Data.define(:n, :other, :id) + end + class CopyAuthorsArgs < Data.define(:name, :bio) end + class CopyAuthorsNamedArgs < Data.define(:name, :bio) + end + TRUNCATE_AUTHORS_SQL = <<~'SQL' DELETE FROM authors SQL @@ -425,7 +456,7 @@ def delete_author(id:) def list_authors_paged(offset:, limit:) with_client('list_authors_paged') do |client| - query_params = [offset, limit] + query_params = [limit, offset] stmt = client.prepare(LIST_AUTHORS_PAGED_SQL) begin result = stmt.execute(*query_params) @@ -455,7 +486,7 @@ 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] + query_params = [name_filter, name_filter, lim] stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) begin result = stmt.execute(*query_params) @@ -554,6 +585,39 @@ def get_authors_by_mixed_placeholders(n:, ids:, id:, more:) end # rubocop:enable Naming/MethodParameterName + # rubocop:disable Naming/MethodParameterName + def get_authors_by_repeated_named_and_positional(n:, other:, id:) + with_client('get_authors_by_repeated_named_and_positional') do |client| + query_params = [n, other, id, n] + stmt = client.prepare(GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_SQL) + begin + result = stmt.execute(*query_params) + result.map do |row| + GetAuthorsByRepeatedNamedAndPositionalRow.new(row[0], row[1], row[2]) + end + ensure + stmt.close + end + end + end + # rubocop:enable Naming/MethodParameterName + + def get_authors_by_repeated_named_and_positional_batch(rows) + with_client('get_authors_by_repeated_named_and_positional_batch') do |client| + stmt = client.prepare(GET_AUTHORS_BY_REPEATED_NAMED_AND_POSITIONAL_BATCH_SQL) + begin + rows.map do |args| + query_params = [args.n, args.other, args.id, args.n] + result = stmt.execute(*query_params) + row = result.first + row && GetAuthorsByRepeatedNamedAndPositionalBatchRow.new(row[0], row[1], row[2]) + end + ensure + stmt.close + end + end + end + # Bulk insert: pass an Array of CopyAuthorsArgs. def copy_authors(rows) with_client('copy_authors') do |client| @@ -567,6 +631,18 @@ def copy_authors(rows) end end + def copy_authors_named(rows) + with_client('copy_authors_named') do |client| + atomically(client) do + rows.each_slice(499) do |batch| + sql = %(INSERT INTO "authors" ("name", "bio") VALUES #{Array.new(batch.size, '(?, ?)').join(', ')}) + client.execute(sql, batch.flat_map { |row| [row.name, row.bio] }) + end + end + nil + end + end + def truncate_authors with_client('truncate_authors') do |client| stmt = client.prepare(TRUNCATE_AUTHORS_SQL) diff --git a/test/sqlite_placeholders_test.rb b/test/sqlite_placeholders_test.rb index 9150e30..280c92c 100644 --- a/test/sqlite_placeholders_test.rb +++ b/test/sqlite_placeholders_test.rb @@ -50,12 +50,57 @@ def test_query_params_follow_sql_occurrence_order_with_repeated_names_and_slices RUBY end - def test_numbered_placeholders_are_rewritten_only_for_queries_with_slices + def test_query_params_follow_sql_occurrence_order_for_numbered_parameters_without_slices + sql = "name = ?2 AND name != ?3 AND id > ? AND name = ?2 AND '?4' != '?' -- ?5\n" + sources = [ + SourceParam.new(2, Column.new('n', false)), + SourceParam.new(3, Column.new('other', false)), + SourceParam.new(1, Column.new('id', false)) + ] + params = [ + Param.new('n', sources[0].column, false), + Param.new('other', sources[1].column, false), + Param.new('id', sources[2].column, false) + ] + query = Query.new(sql, sources) + + assert_equal "name = ? AND name != ? AND id > ? AND name = ? AND '?4' != '?' -- ?5\n", + @driver.query_text(query) + assert_equal 'query_params = [n, other, id, n]', @driver.query_params(query, params) + end + + def test_batch_query_params_use_occurrence_order_with_args_prefix + sql = 'name = ?2 AND name != ?3 AND id > ? AND name = ?2' + sources = [ + SourceParam.new(2, Column.new('n', false)), + SourceParam.new(3, Column.new('other', false)), + SourceParam.new(1, Column.new('id', false)) + ] + params = [ + Param.new('n', sources[0].column, false), + Param.new('other', sources[1].column, false), + Param.new('id', sources[2].column, false) + ] + + assert_equal 'query_params = [args.n, args.other, args.id, args.n]', + @driver.query_params(Query.new(sql, sources), params, value_prefix: 'args.') + end + + def test_numbered_placeholders_are_rewritten_but_plain_placeholders_are_unchanged sliced = Query.new('a = ?2 AND id IN (/*SLICE:ids*/?) AND b = "?3"', [SourceParam.new(3, Column.new('ids', true))]) - plain = Query.new('a = ?2 AND b = "?3"', []) + numbered = Query.new('a = ?2 AND b = "?3"', []) + plain = Query.new('a = ? AND b = "?3"', []) assert_equal 'a = ? AND id IN (/*SLICE:ids*/?) AND b = "?3"', @driver.query_text(sliced) + assert_equal 'a = ? AND b = "?3"', @driver.query_text(numbered) assert_equal plain.text, @driver.query_text(plain) end + + def test_scanner_orders_numbered_and_positional_parameters_without_slices + sql = "name = ?2 AND name != ?3 AND id > ? AND name = ?2 AND '?4' != '?' /* ?5 */" + occurrences = @driver.placeholder_occurrences(sql).map { |kind, value,| [kind, value] } + + assert_equal [[:numbered, 2], [:numbered, 3], [:positional, nil], [:numbered, 2]], occurrences + end end diff --git a/tests/end2end_sqlite3.rb b/tests/end2end_sqlite3.rb index e300dfd..e84b131 100644 --- a/tests/end2end_sqlite3.rb +++ b/tests/end2end_sqlite3.rb @@ -174,6 +174,39 @@ def test_numbered_placeholders_keep_their_bindings_after_slice_expansion end end + def test_non_slice_numbered_placeholders_keep_sql_order + create_author('bob') + create_author('filler') + create_author('alice') + + sql = 'SELECT id, name, bio FROM authors WHERE name = ? AND name != ? AND id > ? AND name = ? ORDER BY id' + expected = @db.execute(sql, ['alice', 'bob', 1, 'alice']) + .map { |row| row_for(GetAuthorsByRepeatedNamedAndPositionalRow, row) } + + assert_equal [3], expected.map(&:id) + assert_equal expected, @query_sql.get_authors_by_repeated_named_and_positional(n: 'alice', other: 'bob', id: 1) + end + + def test_batch_numbered_placeholders_keep_sql_order + create_author('bob') + create_author('filler') + create_author('alice') + create_author('carol') + + args = [ + GetAuthorsByRepeatedNamedAndPositionalBatchArgs.new(n: 'alice', other: 'bob', id: 1), + GetAuthorsByRepeatedNamedAndPositionalBatchArgs.new(n: 'carol', other: 'alice', id: 1), + GetAuthorsByRepeatedNamedAndPositionalBatchArgs.new(n: 'alice', other: 'alice', id: 0) + ] + sql = 'SELECT id, name, bio FROM authors WHERE name = ? AND name != ? AND id > ? AND name = ? ORDER BY id LIMIT 1' + expected = args.map do |row| + result = @db.execute(sql, [row.n, row.other, row.id, row.n]).first + result && row_for(GetAuthorsByRepeatedNamedAndPositionalBatchRow, result) + end + + assert_equal expected, @query_sql.get_authors_by_repeated_named_and_positional_batch(args) + end + # ---- :copyfrom ------------------------------------------------------------------------------------------------- def test_copyfrom @@ -186,6 +219,17 @@ def test_copyfrom assert_equal rows.to_h { |r| [r.name, r.bio] }, @query_sql.list_authors.to_h { |a| [a.name, a.bio] } end + def test_copyfrom_accepts_sqlc_named_values + rows = [ + CopyAuthorsNamedArgs.new(name: 'named one', bio: 'first bio'), + CopyAuthorsNamedArgs.new(name: 'named two', bio: nil) + ] + + @query_sql.copy_authors_named(rows) + + assert_equal rows.to_h { |r| [r.name, r.bio] }, @query_sql.list_authors.to_h { |a| [a.name, a.bio] } + end + def test_copyfrom_is_batched_below_the_bound_parameter_limit @query_sql.copy_authors([]) assert_empty @query_sql.list_authors From 254bed732bb06b9e605649dc02da623595f06572 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 20:26:18 +0000 Subject: [PATCH 3/9] Use client-side mysql2 query binding Co-Authored-By: Doron Eli Rachman --- README.md | 13 +- docs/Architecture.md | 10 +- docs/Design.md | 5 +- examples/authors/mysql/query.sql | 25 + examples/authors/mysql/schema.sql | 15 + examples/mysql2/models.rb | 18 + examples/mysql2/models.rbs | 29 + examples/mysql2/query_sql.rb | 657 ++++++++-------- examples/mysql2/query_sql.rbs | 65 ++ lib/sqlc_gen_ruby/drivers/mysql2.rb | 122 ++- lib/sqlc_gen_ruby/method_gen.rb | 3 +- steep_expectations.yml | 110 ++- test/features_test.rb | 28 +- test/fixtures/mysql2.bin | 101 ++- test/golden/mysql2/default/models.rb | 18 + test/golden/mysql2/default/models.rbs | 29 + test/golden/mysql2/default/my_queries_sql.rb | 94 ++- test/golden/mysql2/default/my_queries_sql.rbs | 4 + test/golden/mysql2/default/other_sql.rb | 114 ++- test/golden/mysql2/default/other_sql.rbs | 4 + test/golden/mysql2/default/query_sql.rb | 702 +++++++++-------- test/golden/mysql2/default/query_sql.rbs | 65 ++ test/golden/mysql2/ruby31_gemfile/Gemfile | 2 + test/golden/mysql2/ruby31_gemfile/models.rb | 18 + test/golden/mysql2/ruby31_gemfile/models.rbs | 43 ++ .../mysql2/ruby31_gemfile/my_queries_sql.rb | 94 ++- .../mysql2/ruby31_gemfile/my_queries_sql.rbs | 4 + .../golden/mysql2/ruby31_gemfile/other_sql.rb | 114 ++- .../mysql2/ruby31_gemfile/other_sql.rbs | 4 + .../golden/mysql2/ruby31_gemfile/query_sql.rb | 726 +++++++++--------- .../mysql2/ruby31_gemfile/query_sql.rbs | 81 ++ test/golden/mysql2/ruby32_notypes/models.rb | 18 + .../mysql2/ruby32_notypes/my_queries_sql.rb | 94 ++- .../golden/mysql2/ruby32_notypes/other_sql.rb | 114 ++- .../golden/mysql2/ruby32_notypes/query_sql.rb | 702 +++++++++-------- test/golden/pg/default/query_sql.rbs | 6 +- test/golden/pg/ruby31_gemfile/query_sql.rbs | 6 +- test/golden/sqlite3/default/query_sql.rbs | 4 +- .../sqlite3/ruby31_gemfile/query_sql.rbs | 4 +- tests/end2end_mysql2.rb | 166 +++- tests/rbs_types.rb | 77 ++ 41 files changed, 3012 insertions(+), 1496 deletions(-) diff --git a/README.md b/README.md index 153c105..373f459 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,8 @@ per table, one module of constants per enum), `connection.rb` (shared helpers) a * **Arguments** are keywords named after the parameters (`get_author(id:)`); `sqlc.arg(name)` names them yourself. Missing or unknown ones raise `ArgumentError`. Nullable parameters named with `sqlc.arg` / `sqlc.narg` default to `nil`; other nullable parameters (`VALUES ($1, $2)`) must be passed, even as `nil`. -* **Rows** are frozen `Data` objects (keyword `Struct`s below Ruby 3.2), so pattern matching works: `case row in { id:, name: }`. +* **Rows** are frozen `Data` objects from Ruby 3.2 (with `with` and `to_h`) or mutable keyword `Struct`s below Ruby 3.2; + both support pattern matching: `case row in { id:, name: }`. * **Bang finders:** every `:one` query also gets a `get_author!` that raises `QuerySql::NotFound` instead of returning `nil`. * **Connections:** `QuerySql.connect(params, **pool_options)` builds a [`connection_pool`](https://github.com/mperham/connection_pool). To use a pool you already have (or a fake in tests) pass it to `QuerySql.new(pool)`; anything with `#with` works. @@ -101,8 +102,9 @@ per table, one module of constants per enum), `connection.rb` (shared helpers) a * **Logging:** `connect(params, logger: Rails.logger)` / `new(pool, logger:)` logs `QuerySql#get_author (0.4ms)` at debug level for each query and transaction. Parameter values are never logged. * **Async / fibers:** it is plain synchronous Ruby and works with the Fiber scheduler ([`async`](https://github.com/socketry/async)): - inside `Sync { ... }`, pooled `pg` queries run concurrently. `mysql2` prepared statements block the scheduler; use - threads for concurrent `mysql2` queries. SQLite queries run one at a time. + inside `Sync { ... }`, pooled `pg` and `mysql2` queries run concurrently. MySQL parameters are quoted client-side + with `Mysql2::Client#escape` and sent with `Client#query`, not server-side prepared statements. SQLite queries run + one at a time. * **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`. @@ -175,7 +177,10 @@ server; duplicate keys and invalid values raise `Mysql2::Error` instead of being The generated SQLite code uses `Monitor`: if you run Steep, add `library "monitor"` to your `Steepfile`. ### Names -* Names are snake_cased, acronyms kept together (`MIN_ID` → `min_id`, `HTTPServer` → `http_server`). Ruby keywords and names a row or method already uses get a trailing `_` (`class_`, `hash_`). +* Query method names and keyword arguments are snake_cased, with acronyms kept together (`MIN_ID` → `min_id`, + `HTTPServer` → `http_server`). Ruby keywords and names a method already uses get a trailing `_`. +* Row members retain the column spelling unless they conflict with a generated row method: `"camelCase"` is `camelCase` + on a row but `camel_case:` as an argument; a column named `end` is row member `:end` and argument `end_:`. * A parameter used twice is one keyword when named with `sqlc.arg`; repeated `?` / `$n` of one column become `id:`, `id_2:`. * A table `authors` next to a table `author` keeps its plural model name (`Authors`). Query names that would replace a generated method (`Transaction`, `Initialize`, ...) are errors. diff --git a/docs/Architecture.md b/docs/Architecture.md index 99225f5..721f829 100644 --- a/docs/Architecture.md +++ b/docs/Architecture.md @@ -27,7 +27,7 @@ stdout ◄─ Proto (encode) ◄─ GenerateResponse (query_sql.rb, query_sql.rb | `type_resolver.rb` | RBS type of a column / parameter: driver type table, arrays, nullability (`T?`), `overrides:` (a `db_type` override types array elements too) | | `catalog.rb` | the schema's tables and enums, and their Ruby names | | `drivers/base.rb` | the driver interface and what all drivers share: `instrument` logging, `NotFound`, transaction RBS | -| `drivers/{pg,mysql2,sqlite3}.rb` | the per-library parts: type mapping, connecting / pooling, preparing and executing statements, transactions, `:copyfrom` | +| `drivers/{pg,mysql2,sqlite3}.rb` | the per-library parts: type mapping, connecting / pooling, executing statements, transactions, `:copyfrom` | | `code.rb` | text helpers: indentation, `snake_case`, `camelize`, Ruby-keyword-safe parameter names, `rubocop:disable` pairs for schema names that break a naming cop | **Rule of thumb:** `MethodGen` / `Generator` decide *what* is generated and are the same for every database; a driver only @@ -62,11 +62,13 @@ Look at [`examples/pg/query_sql.rb`](../examples/pg/query_sql.rb) for a complete Design decisions worth knowing (the *why* is in the comments of `drivers/base.rb` and `drivers/pg.rb`): -* **Prepared statements are per connection.** A pooled query object is shared by many connections, and a prepared - statement exists only on the connection that prepared it, so each connection remembers its own (`ConnectionState`). +* **PostgreSQL prepared statements are per connection.** A pooled query object is shared by many connections, and a + prepared statement exists only on the connection that prepared it, so each connection remembers its own + (`ConnectionState`). MySQL quotes values client-side and uses `Mysql2::Client#query` so queries cooperate with the + Fiber scheduler. * **Pool injection.** `QuerySql.new(pool)` accepts anything that responds to `#with`, so an app can share its pool and tests can pass a fake. `connect(...)` is only a convenience that builds a `connection_pool`. -* **Statements are closed.** SQLite/MySQL statements are closed in `ensure` (an open SQLite statement blocks `COMMIT`). +* **SQLite statements are closed.** They are closed in `ensure` because an open statement blocks `COMMIT`. * **Parameters are encoded by the driver.** `Driver#encode_param` (e.g. `bytea`, `json` and arrays on `pg`) is used by every command, batches included; add a type's encoding there, not in `MethodGen`. * **`:copyfrom` is all or nothing** on every driver (a savepoint / transaction around the load), and MySQL's diff --git a/docs/Design.md b/docs/Design.md index 2a8783f..585a1e5 100644 --- a/docs/Design.md +++ b/docs/Design.md @@ -33,8 +33,9 @@ against real databases) is described in [Testing.md](Testing.md); how it works i Generated code follows what Ruby code usually looks like rather than mirroring the C# plugin: keyword arguments instead of parameter objects, `Data` rows, implicit returns, `result.map { ... }`, 2-space indentation, a frozen-string-literal header, SQL in raw heredocs, `connection_pool` for pooling with an injectable pool, nested transactions via savepoints. -Prepared statements are named per query class and remembered per *connection* (a module mixed into each connection), -so several query objects can share a pool. +PostgreSQL prepared statements are named per query class and remembered per *connection* (a module mixed into each +connection), so several query objects can share a pool. MySQL quotes parameters client-side and sends queries through +`Mysql2::Client#query` rather than server-side prepared statements. ## History The plugin started as a port of the C# `sqlc-gen-csharp` plugin and reproduced its output byte for byte (verified diff --git a/examples/authors/mysql/query.sql b/examples/authors/mysql/query.sql index 35b68ca..0687f92 100644 --- a/examples/authors/mysql/query.sql +++ b/examples/authors/mysql/query.sql @@ -151,3 +151,28 @@ SELECT id, code, doc, flags, amount FROM copy_types ORDER BY id; -- name: TruncateCopyTypes :exec TRUNCATE TABLE copy_types; + +-- name: FindAuthorsByExactName :many +SELECT id FROM authors WHERE name = sqlc.arg(name); + +-- name: GetAuthorWithQuestionMarks :many +SELECT id FROM authors AS `?` +WHERE name = ? AND '?' = '?' AND "?" = "?" /* ? */ +# ? +ORDER BY id; + +-- name: InsertMysqlBindingValues :exec +INSERT INTO mysql_binding_values ( + id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, + datetime_value, datetime_2, bool_value, nullable_value +) VALUES ( + sqlc.arg(id), sqlc.arg(text_value), sqlc.arg(blob_value), sqlc.arg(signed_value), sqlc.arg(unsigned_value), + sqlc.arg(decimal_value), sqlc.arg(float_value), sqlc.arg(date_value), sqlc.arg(datetime_value), + sqlc.arg(datetime_2), sqlc.arg(bool_value), sqlc.arg(nullable_value) +); + +-- name: GetMysqlBindingValues :one +SELECT * FROM mysql_binding_values WHERE id = sqlc.arg(id); + +-- name: TruncateMysqlBindingValues :exec +TRUNCATE TABLE mysql_binding_values; diff --git a/examples/authors/mysql/schema.sql b/examples/authors/mysql/schema.sql index e9985c9..d4a1a10 100644 --- a/examples/authors/mysql/schema.sql +++ b/examples/authors/mysql/schema.sql @@ -78,3 +78,18 @@ CREATE TABLE copy_types ( flags bit(8), amount int unsigned ); + +CREATE TABLE mysql_binding_values ( + id BIGINT NOT NULL PRIMARY KEY, + text_value LONGTEXT, + blob_value LONGBLOB, + signed_value BIGINT, + unsigned_value BIGINT UNSIGNED, + decimal_value DECIMAL(65, 30), + float_value DOUBLE, + date_value DATE, + datetime_value DATETIME(6), + datetime_2 DATETIME(6), + bool_value BOOLEAN, + nullable_value TEXT +); diff --git a/examples/mysql2/models.rb b/examples/mysql2/models.rb index 07b421a..f1181dd 100644 --- a/examples/mysql2/models.rb +++ b/examples/mysql2/models.rb @@ -78,4 +78,22 @@ class CopyType < Data.define( :amount ) end + + # rubocop:disable Naming/VariableNumber + class MysqlBindingValu < Data.define( + :id, + :text_value, + :blob_value, + :signed_value, + :unsigned_value, + :decimal_value, + :float_value, + :date_value, + :datetime_value, + :datetime_2, + :bool_value, + :nullable_value + ) + end + # rubocop:enable Naming/VariableNumber end diff --git a/examples/mysql2/models.rbs b/examples/mysql2/models.rbs index 15dee64..1888981 100644 --- a/examples/mysql2/models.rbs +++ b/examples/mysql2/models.rbs @@ -138,4 +138,33 @@ module Mysql2Codegen Integer? amount ) -> void end + + class MysqlBindingValu + attr_reader id: Integer + attr_reader text_value: String? + attr_reader blob_value: String? + attr_reader signed_value: Integer? + attr_reader unsigned_value: Integer? + attr_reader decimal_value: BigDecimal? + attr_reader float_value: Float? + attr_reader date_value: Date? + attr_reader datetime_value: Time? + attr_reader datetime_2: Time? + attr_reader bool_value: Integer? + attr_reader nullable_value: String? + def initialize: ( + Integer id, + String? text_value, + String? blob_value, + Integer? signed_value, + Integer? unsigned_value, + BigDecimal? decimal_value, + Float? float_value, + Date? date_value, + Time? datetime_value, + Time? datetime_2, + Integer? bool_value, + String? nullable_value + ) -> void + end end diff --git a/examples/mysql2/query_sql.rb b/examples/mysql2/query_sql.rb index 076b1eb..bed3059 100644 --- a/examples/mysql2/query_sql.rb +++ b/examples/mysql2/query_sql.rb @@ -3,6 +3,8 @@ # auto-generated by sqlc - do not edit require 'connection_pool' require 'mysql2' +require 'bigdecimal' +require 'date' require 'tempfile' require_relative 'connection' require_relative 'models' @@ -324,6 +326,59 @@ class ListCopyTypesRow < Data.define( TRUNCATE TABLE copy_types SQL + FIND_AUTHORS_BY_EXACT_NAME_SQL = <<~'SQL' + SELECT id FROM authors WHERE name = ? + SQL + + class FindAuthorsByExactNameRow < Data.define(:id) + end + + GET_AUTHOR_WITH_QUESTION_MARKS_SQL = <<~'SQL' + SELECT id FROM authors AS `?` + WHERE name = ? AND '?' = '?' AND "?" = "?" /* ? */ + ORDER BY id + SQL + + class GetAuthorWithQuestionMarksRow < Data.define(:id) + end + + INSERT_MYSQL_BINDING_VALUES_SQL = <<~'SQL' + INSERT INTO mysql_binding_values ( + id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, + datetime_value, datetime_2, bool_value, nullable_value + ) VALUES ( + ?, ?, ?, ?, ?, + ?, ?, ?, ?, + ?, ?, ? + ) + SQL + + GET_MYSQL_BINDING_VALUES_SQL = <<~'SQL' + SELECT id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, datetime_value, datetime_2, bool_value, nullable_value FROM mysql_binding_values WHERE id = ? + SQL + + # rubocop:disable Naming/VariableNumber + class GetMysqlBindingValuesRow < Data.define( + :id, + :text_value, + :blob_value, + :signed_value, + :unsigned_value, + :decimal_value, + :float_value, + :date_value, + :datetime_value, + :datetime_2, + :bool_value, + :nullable_value + ) + end + # rubocop:enable Naming/VariableNumber + + TRUNCATE_MYSQL_BINDING_VALUES_SQL = <<~'SQL' + TRUNCATE TABLE mysql_binding_values + SQL + class QuerySql # Raised by the bang variants (get_author!) when the query returns no row. class NotFound < StandardError; end @@ -385,14 +440,9 @@ class ExecResult < Data.define(:rows_affected, :last_insert_id) def get_author(id:) with_client('get_author') do |client| query_params = [id] - stmt = client.prepare(GET_AUTHOR_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && GetAuthorRow.new(row[0], row[1], row[2]) - ensure - stmt.close - end + result = client.query(bind_params(client, GET_AUTHOR_SQL, query_params), as: :array) + row = result.first + row && GetAuthorRow.new(row[0], row[1], row[2]) end end @@ -403,14 +453,9 @@ def get_author!(...) def list_authors with_client('list_authors') do |client| - stmt = client.prepare(LIST_AUTHORS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListAuthorsRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_AUTHORS_SQL, []), as: :array) + result.map do |row| + ListAuthorsRow.new(row[0], row[1], row[2]) end end end @@ -418,103 +463,78 @@ def list_authors def create_author(name:, bio:) with_client('create_author') do |client| query_params = [name, bio] - stmt = client.prepare(CREATE_AUTHOR_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, CREATE_AUTHOR_SQL, query_params)) + nil end end def update_author(bio:, id:) with_client('update_author') do |client| query_params = [bio, id] - stmt = client.prepare(UPDATE_AUTHOR_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, UPDATE_AUTHOR_SQL, query_params)) + nil end end def create_author_return_id(name:, bio:) with_client('create_author_return_id') do |client| query_params = [name, bio] - stmt = client.prepare(CREATE_AUTHOR_RETURN_ID_SQL) - begin - stmt.execute(*query_params) - client.last_id - ensure - stmt.close - end + client.query(bind_params(client, CREATE_AUTHOR_RETURN_ID_SQL, query_params)) + client.last_id end end def delete_author(id:) with_client('delete_author') do |client| query_params = [id] - stmt = client.prepare(DELETE_AUTHOR_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, DELETE_AUTHOR_SQL, query_params)) + nil end end def test with_client('test') do |client| - stmt = client.prepare(TEST_SQL) - begin - result = stmt.execute(as: :array) - row = result.first - row && TestRow.new( - row[0], - row[1], - row[2], - row[3], - row[4], - row[5], - row[6], - row[7], - row[8], - row[9], - row[10], - row[11], - row[12], - row[13], - row[14], - row[15], - row[16], - row[17], - row[18], - row[19], - row[20], - row[21], - row[22], - row[23], - row[24], - row[25], - row[26], - row[27], - row[28], - row[29], - row[30], - row[31], - row[32], - row[33], - row[34], - row[35], - row[36] - ) - ensure - stmt.close - end + result = client.query(bind_params(client, TEST_SQL, []), as: :array) + row = result.first + row && TestRow.new( + row[0], + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7], + row[8], + row[9], + row[10], + row[11], + row[12], + row[13], + row[14], + row[15], + row[16], + row[17], + row[18], + row[19], + row[20], + row[21], + row[22], + row[23], + row[24], + row[25], + row[26], + row[27], + row[28], + row[29], + row[30], + row[31], + row[32], + row[33], + row[34], + row[35], + row[36] + ) end end @@ -526,14 +546,9 @@ def test!(...) def list_authors_paged(limit:, offset:) with_client('list_authors_paged') do |client| query_params = [limit, offset] - stmt = client.prepare(LIST_AUTHORS_PAGED_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - ListAuthorsPagedRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_AUTHORS_PAGED_SQL, query_params), as: :array) + result.map do |row| + ListAuthorsPagedRow.new(row[0], row[1], row[2]) end end end @@ -541,14 +556,9 @@ def list_authors_paged(limit:, offset:) def get_author_by_name_pattern(name_pattern: nil) with_client('get_author_by_name_pattern') do |client| query_params = [name_pattern] - stmt = client.prepare(GET_AUTHOR_BY_NAME_PATTERN_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorByNamePatternRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, GET_AUTHOR_BY_NAME_PATTERN_SQL, query_params), as: :array) + result.map do |row| + GetAuthorByNamePatternRow.new(row[0], row[1], row[2]) end end end @@ -556,14 +566,9 @@ def get_author_by_name_pattern(name_pattern: nil) def get_authors_by_optional_name(limit:, name_filter: nil) with_client('get_authors_by_optional_name') do |client| query_params = [name_filter, name_filter, limit] - stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorsByOptionalNameRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, GET_AUTHORS_BY_OPTIONAL_NAME_SQL, query_params), as: :array) + result.map do |row| + GetAuthorsByOptionalNameRow.new(row[0], row[1], row[2]) end end end @@ -571,13 +576,8 @@ def get_authors_by_optional_name(limit:, name_filter: nil) def pause_query(seconds:) with_client('pause_query') do |client| query_params = [seconds] - stmt = client.prepare(PAUSE_QUERY_SQL) - begin - stmt.execute(*query_params, as: :array) - nil - ensure - stmt.close - end + client.query(bind_params(client, PAUSE_QUERY_SQL, query_params), as: :array) + nil end end @@ -585,13 +585,8 @@ def pause_query(seconds:) def update_author_bios(bio: nil) with_client('update_author_bios') do |client| query_params = [bio] - stmt = client.prepare(UPDATE_AUTHOR_BIOS_SQL) - begin - stmt.execute(*query_params) - stmt.affected_rows - ensure - stmt.close - end + client.query(bind_params(client, UPDATE_AUTHOR_BIOS_SQL, query_params)) + client.affected_rows end end @@ -599,14 +594,9 @@ def get_authors_by_ids(ids:) with_client('get_authors_by_ids') do |client| query_params = [*ids] sql = GET_AUTHORS_BY_IDS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) - stmt = client.prepare(sql) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorsByIdsRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, sql, query_params), as: :array) + result.map do |row| + GetAuthorsByIdsRow.new(row[0], row[1], row[2]) end end end @@ -616,14 +606,9 @@ def get_authors_by_ids_and_names(ids:, names:) query_params = [*ids, *names] sql = GET_AUTHORS_BY_IDS_AND_NAMES_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) sql = sql.gsub('/*SLICE:names*/?', slice_placeholders(names)) - stmt = client.prepare(sql) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorsByIdsAndNamesRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, sql, query_params), as: :array) + result.map do |row| + GetAuthorsByIdsAndNamesRow.new(row[0], row[1], row[2]) end end end @@ -646,56 +631,36 @@ def copy_authors(rows) def truncate_authors with_client('truncate_authors') do |client| - stmt = client.prepare(TRUNCATE_AUTHORS_SQL) - begin - stmt.execute - nil - ensure - stmt.close - end + client.query(bind_params(client, TRUNCATE_AUTHORS_SQL, [])) + nil end end def create_book(name:, author_id:) with_client('create_book') do |client| query_params = [name, author_id] - stmt = client.prepare(CREATE_BOOK_SQL) - begin - stmt.execute(*query_params) - client.last_id - ensure - stmt.close - end + client.query(bind_params(client, CREATE_BOOK_SQL, query_params)) + client.last_id end end def list_all_authors_books with_client('list_all_authors_books') do |client| - stmt = client.prepare(LIST_ALL_AUTHORS_BOOKS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListAllAuthorsBooksRow.new( - Author.new(row[0], row[1], row[2]), - Book.new(row[3], row[4], row[5], row[6]) - ) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_ALL_AUTHORS_BOOKS_SQL, []), as: :array) + result.map do |row| + ListAllAuthorsBooksRow.new( + Author.new(row[0], row[1], row[2]), + Book.new(row[3], row[4], row[5], row[6]) + ) end end end def get_duplicate_authors with_client('get_duplicate_authors') do |client| - stmt = client.prepare(GET_DUPLICATE_AUTHORS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - GetDuplicateAuthorsRow.new(Author.new(row[0], row[1], row[2]), Author.new(row[3], row[4], row[5])) - end - ensure - stmt.close + result = client.query(bind_params(client, GET_DUPLICATE_AUTHORS_SQL, []), as: :array) + result.map do |row| + GetDuplicateAuthorsRow.new(Author.new(row[0], row[1], row[2]), Author.new(row[3], row[4], row[5])) end end end @@ -703,14 +668,9 @@ def get_duplicate_authors def get_authors_by_book_name(name:) with_client('get_authors_by_book_name') do |client| query_params = [name] - stmt = client.prepare(GET_AUTHORS_BY_BOOK_NAME_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorsByBookNameRow.new(row[0], row[1], row[2], Book.new(row[3], row[4], row[5], row[6])) - end - ensure - stmt.close + result = client.query(bind_params(client, GET_AUTHORS_BY_BOOK_NAME_SQL, query_params), as: :array) + result.map do |row| + GetAuthorsByBookNameRow.new(row[0], row[1], row[2], Book.new(row[3], row[4], row[5], row[6])) end end end @@ -718,27 +678,17 @@ def get_authors_by_book_name(name:) def create_extended_bio(author_name:, name:, bio_type:, author_type:) with_client('create_extended_bio') do |client| query_params = [author_name, name, bio_type, author_type] - stmt = client.prepare(CREATE_EXTENDED_BIO_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, CREATE_EXTENDED_BIO_SQL, query_params)) + nil end end def get_first_extended_bio_by_type(bio_type:) with_client('get_first_extended_bio_by_type') do |client| query_params = [bio_type] - stmt = client.prepare(GET_FIRST_EXTENDED_BIO_BY_TYPE_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && GetFirstExtendedBioByTypeRow.new(row[0], row[1], row[2], row[3]) - ensure - stmt.close - end + result = client.query(bind_params(client, GET_FIRST_EXTENDED_BIO_BY_TYPE_SQL, query_params), as: :array) + row = result.first + row && GetFirstExtendedBioByTypeRow.new(row[0], row[1], row[2], row[3]) end end @@ -749,26 +699,16 @@ def get_first_extended_bio_by_type!(...) def truncate_extended_bios with_client('truncate_extended_bios') do |client| - stmt = client.prepare(TRUNCATE_EXTENDED_BIOS_SQL) - begin - stmt.execute - nil - ensure - stmt.close - end + client.query(bind_params(client, TRUNCATE_EXTENDED_BIOS_SQL, [])) + nil end end def create_author_result(name:, bio:) with_client('create_author_result') do |client| query_params = [name, bio] - stmt = client.prepare(CREATE_AUTHOR_RESULT_SQL) - begin - stmt.execute(*query_params) - ExecResult.new(stmt.affected_rows, client.last_id) - ensure - stmt.close - end + client.query(bind_params(client, CREATE_AUTHOR_RESULT_SQL, query_params)) + ExecResult.new(client.affected_rows, client.last_id) end end @@ -776,12 +716,7 @@ def create_authors_batch(rows) with_client('create_authors_batch') do |client| rows.each do |args| query_params = [args.name, args.bio] - stmt = client.prepare(CREATE_AUTHORS_BATCH_SQL) - begin - stmt.execute(*query_params) - ensure - stmt.close - end + client.query(bind_params(client, CREATE_AUTHORS_BATCH_SQL, query_params)) end nil end @@ -791,14 +726,9 @@ def get_author_batch(rows) with_client('get_author_batch') do |client| rows.map do |args| query_params = [args.id] - stmt = client.prepare(GET_AUTHOR_BATCH_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && GetAuthorBatchRow.new(row[0], row[1], row[2]) - ensure - stmt.close - end + result = client.query(bind_params(client, GET_AUTHOR_BATCH_SQL, query_params), as: :array) + row = result.first + row && GetAuthorBatchRow.new(row[0], row[1], row[2]) end end end @@ -807,14 +737,9 @@ def list_authors_by_name_batch(rows) with_client('list_authors_by_name_batch') do |client| rows.map do |args| query_params = [args.name] - stmt = client.prepare(LIST_AUTHORS_BY_NAME_BATCH_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - ListAuthorsByNameBatchRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_AUTHORS_BY_NAME_BATCH_SQL, query_params), as: :array) + result.map do |row| + ListAuthorsByNameBatchRow.new(row[0], row[1], row[2]) end end end @@ -823,14 +748,9 @@ def list_authors_by_name_batch(rows) def count_regressions_by_text(term:) with_client('count_regressions_by_text') do |client| query_params = [term, term] - stmt = client.prepare(COUNT_REGRESSIONS_BY_TEXT_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && CountRegressionsByTextRow.new(row[0]) - ensure - stmt.close - end + result = client.query(bind_params(client, COUNT_REGRESSIONS_BY_TEXT_SQL, query_params), as: :array) + row = result.first + row && CountRegressionsByTextRow.new(row[0]) end end @@ -842,14 +762,9 @@ def count_regressions_by_text!(...) def list_regressions_by_ids(id:, id_2:, id_3:) with_client('list_regressions_by_ids') do |client| query_params = [id, id_2, id_3] - stmt = client.prepare(LIST_REGRESSIONS_BY_IDS_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - ListRegressionsByIdsRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_REGRESSIONS_BY_IDS_SQL, query_params), as: :array) + result.map do |row| + ListRegressionsByIdsRow.new(row[0], row[1], row[2]) end end end @@ -886,41 +801,26 @@ def copy_regression_rows(rows) def list_regression_payloads with_client('list_regression_payloads') do |client| - stmt = client.prepare(LIST_REGRESSION_PAYLOADS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListRegressionPayloadsRow.new(row[0], row[1]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_REGRESSION_PAYLOADS_SQL, []), as: :array) + result.map do |row| + ListRegressionPayloadsRow.new(row[0], row[1]) end end end def list_regression_rows with_client('list_regression_rows') do |client| - stmt = client.prepare(LIST_REGRESSION_ROWS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListRegressionRowsRow.new(row[0], row[1], row[2], row[3]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_REGRESSION_ROWS_SQL, []), as: :array) + result.map do |row| + ListRegressionRowsRow.new(row[0], row[1], row[2], row[3]) end end end def truncate_regressions with_client('truncate_regressions') do |client| - stmt = client.prepare(TRUNCATE_REGRESSIONS_SQL) - begin - stmt.execute - nil - ensure - stmt.close - end + client.query(bind_params(client, TRUNCATE_REGRESSIONS_SQL, [])) + nil end end @@ -973,12 +873,7 @@ def create_copy_types_batch(rows) with_client('create_copy_types_batch') do |client| rows.each do |args| query_params = [args.id, args.code, args.flags, args.amount] - stmt = client.prepare(CREATE_COPY_TYPES_BATCH_SQL) - begin - stmt.execute(*query_params) - ensure - stmt.close - end + client.query(bind_params(client, CREATE_COPY_TYPES_BATCH_SQL, query_params)) end nil end @@ -988,50 +883,111 @@ def count_copy_types_by_codes(rows) with_client('count_copy_types_by_codes') do |client| rows.map do |args| query_params = [args.code, args.code_2] - stmt = client.prepare(COUNT_COPY_TYPES_BY_CODES_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && CountCopyTypesByCodesRow.new(row[0]) - ensure - stmt.close - end + result = client.query(bind_params(client, COUNT_COPY_TYPES_BY_CODES_SQL, query_params), as: :array) + row = result.first + row && CountCopyTypesByCodesRow.new(row[0]) end end end def list_copy_types with_client('list_copy_types') do |client| - stmt = client.prepare(LIST_COPY_TYPES_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListCopyTypesRow.new( - row[0], - row[1], - row[2], - row[3], - row[4] - ) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_COPY_TYPES_SQL, []), as: :array) + result.map do |row| + ListCopyTypesRow.new( + row[0], + row[1], + row[2], + row[3], + row[4] + ) end end end def truncate_copy_types with_client('truncate_copy_types') do |client| - stmt = client.prepare(TRUNCATE_COPY_TYPES_SQL) - begin - stmt.execute - nil - ensure - stmt.close + client.query(bind_params(client, TRUNCATE_COPY_TYPES_SQL, [])) + nil + end + end + + def find_authors_by_exact_name(name:) + with_client('find_authors_by_exact_name') do |client| + query_params = [name] + result = client.query(bind_params(client, FIND_AUTHORS_BY_EXACT_NAME_SQL, query_params), as: :array) + result.map do |row| + FindAuthorsByExactNameRow.new(row[0]) + end + end + end + + # ? + def get_author_with_question_marks(name:) + with_client('get_author_with_question_marks') do |client| + query_params = [name] + result = client.query(bind_params(client, GET_AUTHOR_WITH_QUESTION_MARKS_SQL, query_params), as: :array) + result.map do |row| + GetAuthorWithQuestionMarksRow.new(row[0]) end end end + def insert_mysql_binding_values(id:, text_value: nil, blob_value: nil, signed_value: nil, unsigned_value: nil, decimal_value: nil, float_value: nil, date_value: nil, datetime_value: nil, datetime_2: nil, bool_value: nil, nullable_value: nil) + with_client('insert_mysql_binding_values') do |client| + query_params = [ + id, + text_value, + blob_value, + signed_value, + unsigned_value, + decimal_value, + float_value, + date_value, + datetime_value, + datetime_2, + bool_value, + nullable_value + ] + client.query(bind_params(client, INSERT_MYSQL_BINDING_VALUES_SQL, query_params)) + nil + end + end + + def get_mysql_binding_values(id:) + with_client('get_mysql_binding_values') do |client| + query_params = [id] + result = client.query(bind_params(client, GET_MYSQL_BINDING_VALUES_SQL, query_params), as: :array) + row = result.first + row && GetMysqlBindingValuesRow.new( + row[0], + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7], + row[8], + row[9], + row[10], + row[11] + ) + end + end + + # Like #get_mysql_binding_values, but raises NotFound when there is no row. + def get_mysql_binding_values!(...) + get_mysql_binding_values(...) || raise(NotFound, 'GetMysqlBindingValues returned no row') + end + + def truncate_mysql_binding_values + with_client('truncate_mysql_binding_values') do |client| + client.query(bind_params(client, TRUNCATE_MYSQL_BINDING_VALUES_SQL, [])) + nil + end + end + private def instrument(name) @@ -1061,6 +1017,89 @@ def with_client(name) end end + def quote(client, value) + case value + when NilClass then 'NULL' + when TrueClass then 'TRUE' + when FalseClass then 'FALSE' + when Integer then value.to_s + when Float + raise ArgumentError, 'cannot bind a non-finite Float to MySQL' unless value.finite? + + value.to_s + when BigDecimal + raise ArgumentError, 'cannot bind a non-finite BigDecimal to MySQL' unless value.finite? + + value.to_s('F') + when DateTime, Time + "'#{value.strftime('%Y-%m-%d %H:%M:%S.%6N')}'" + when Date + "'#{value.strftime('%Y-%m-%d')}'" + when String + if value.encoding == Encoding::BINARY || !value.valid_encoding? + "X'#{value.unpack1('H*')}'" + else + "'#{client.escape(value)}'" + end + else + raise TypeError, "cannot bind #{value.class} to MySQL" + end + end + + def bind_params(client, sql, params) + output = +'' + index = 0 + parameter = 0 + while index < sql.length + char = sql[index] + if ["'", '"', '`'].include?(char) + quote_char = char + start = index + index += 1 + while index < sql.length + if sql[index] == '\\' + index += 2 + elsif sql[index] == quote_char + if sql[index + 1] == quote_char + index += 2 + else + index += 1 + break + end + else + index += 1 + end + end + output << sql[start...index] + elsif sql[index, 2] == '--' && (sql[index + 2].nil? || sql[index + 2].match?(/\s/)) + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif char == '#' + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif sql[index, 2] == '/*' + finish = sql.index('*/', index + 2) + finish = finish ? finish + 2 : sql.length + output << sql[index...finish] + index = finish + elsif char == '?' + raise ArgumentError, 'more MySQL placeholders than parameters' if parameter >= params.length + + output << quote(client, params[parameter]) + parameter += 1 + index += 1 + else + output << char + index += 1 + end + end + raise ArgumentError, 'fewer MySQL placeholders than parameters' unless parameter == params.length + + output + end + # Formats one row for LOAD DATA ... FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\'. def csv_line(values) fields = values.map do |value| diff --git a/examples/mysql2/query_sql.rbs b/examples/mysql2/query_sql.rbs index 764e843..550d280 100644 --- a/examples/mysql2/query_sql.rbs +++ b/examples/mysql2/query_sql.rbs @@ -368,6 +368,55 @@ module Mysql2Codegen TRUNCATE_COPY_TYPES_SQL: String + FIND_AUTHORS_BY_EXACT_NAME_SQL: String + + class FindAuthorsByExactNameRow + attr_reader id: Integer + def initialize: ( Integer id) -> void + end + + GET_AUTHOR_WITH_QUESTION_MARKS_SQL: String + + class GetAuthorWithQuestionMarksRow + attr_reader id: Integer + def initialize: ( Integer id) -> void + end + + INSERT_MYSQL_BINDING_VALUES_SQL: String + + GET_MYSQL_BINDING_VALUES_SQL: String + + class GetMysqlBindingValuesRow + attr_reader id: Integer + attr_reader text_value: String? + attr_reader blob_value: String? + attr_reader signed_value: Integer? + attr_reader unsigned_value: Integer? + attr_reader decimal_value: BigDecimal? + attr_reader float_value: Float? + attr_reader date_value: Date? + attr_reader datetime_value: Time? + attr_reader datetime_2: Time? + attr_reader bool_value: Integer? + attr_reader nullable_value: String? + def initialize: ( + Integer id, + String? text_value, + String? blob_value, + Integer? signed_value, + Integer? unsigned_value, + BigDecimal? decimal_value, + Float? float_value, + Date? date_value, + Time? datetime_value, + Time? datetime_2, + Integer? bool_value, + String? nullable_value + ) -> void + end + + TRUNCATE_MYSQL_BINDING_VALUES_SQL: String + class QuerySql class NotFound < StandardError end @@ -474,12 +523,28 @@ module Mysql2Codegen def truncate_copy_types: -> void + def find_authors_by_exact_name: (name: String) -> Array[FindAuthorsByExactNameRow] + + def get_author_with_question_marks: (name: String) -> Array[GetAuthorWithQuestionMarksRow] + + def insert_mysql_binding_values: (id: Integer, ?text_value: String?, ?blob_value: String?, ?signed_value: Integer?, ?unsigned_value: Integer?, ?decimal_value: BigDecimal?, ?float_value: Float?, ?date_value: Date?, ?datetime_value: Time?, ?datetime_2: Time?, ?bool_value: Integer?, ?nullable_value: String?) -> void + + def get_mysql_binding_values: (id: Integer) -> GetMysqlBindingValuesRow? + + def get_mysql_binding_values!: (id: Integer) -> GetMysqlBindingValuesRow + + def truncate_mysql_binding_values: -> void + private def instrument: [T] (String name) { () -> T } -> T def with_client: [T] (String name) { (untyped) -> T } -> T + def quote: (untyped client, untyped value) -> String + + def bind_params: (untyped client, String sql, Array[untyped] params) -> String + def csv_line: (Array[untyped] values) -> String def load_data: (untyped client, String sql, ?strict: bool) -> void diff --git a/lib/sqlc_gen_ruby/drivers/mysql2.rb b/lib/sqlc_gen_ruby/drivers/mysql2.rb index 29cc5e3..8e5f0d1 100644 --- a/lib/sqlc_gen_ruby/drivers/mysql2.rb +++ b/lib/sqlc_gen_ruby/drivers/mysql2.rb @@ -4,7 +4,7 @@ module SqlcGenRuby module Drivers - # MySQL / MariaDB via the `mysql2` gem (prepared statements, closed after use). + # MySQL / MariaDB via the `mysql2` gem. class Mysql2 < Base def name = 'Mysql2' @@ -37,16 +37,16 @@ def private_methods(ruby_version) ] end - def prepare_stmt(_stmt_name, const) = "stmt = client.prepare(#{const})" + def prepare_stmt(_stmt_name, _const) = nil - def execute_stmt(_stmt_name, _const, has_params, array_rows: false) - args = [] - args << '*query_params' if has_params - args << 'as: :array' if array_rows - args.empty? ? 'stmt.execute' : "stmt.execute(#{args.join(', ')})" + def execute_stmt(_stmt_name, const, has_params, array_rows: false) + sql = "bind_params(client, #{const}, #{has_params ? 'query_params' : '[]'})" + sql = "client.query(#{sql}" + sql += ', as: :array' if array_rows + "#{sql})" end - def closes_statements? = true + def closes_statements? = false def supports_slice? = true @@ -55,11 +55,11 @@ def row_data_type = :array def quote_identifier(name) = "`#{name.gsub('`', '``')}`" - def exec_rows_lines(exec, _query) = [exec, 'stmt.affected_rows'] + def exec_rows_lines(exec, _query) = [exec, 'client.affected_rows'] def exec_last_id_lines(exec, _query) = [exec, 'client.last_id'] - def exec_result_lines(exec) = [exec, 'stmt.affected_rows', 'client.last_id'] + def exec_result_lines(exec) = [exec, 'client.affected_rows', 'client.last_id'] # LOAD DATA LOCAL INFILE from a temporary CSV (the client needs `local_infile: true`, the server # `local_infile=1`). @@ -97,15 +97,18 @@ def load_columns(columns, sources) { columns: list.join(', '), set: set.empty? ? '' : " SET #{set.join(', ')}" } end - def prepares_batch_rows? = true + def prepares_batch_rows? = false - def extra_requires(queries, _ruby_version) = copy_from?(queries) ? %w[tempfile] : [] + def extra_requires(queries, _ruby_version) = %w[bigdecimal date] + (copy_from?(queries) ? %w[tempfile] : []) - def helper_methods(queries) - return [] unless copy_from?(queries) + def extra_gems(_queries) = %w[bigdecimal date] - [[CSV_LINE, 'def csv_line: (Array[untyped] values) -> String'], - [LOAD_DATA_HELPER, 'def load_data: (untyped client, String sql, ?strict: bool) -> void']] + def helper_methods(queries) + helpers = [[QUOTE, 'def quote: (untyped client, untyped value) -> String'], + [BIND_PARAMS, 'def bind_params: (untyped client, String sql, Array[untyped] params) -> String']] + helpers.concat([[CSV_LINE, 'def csv_line: (Array[untyped] values) -> String'], + [LOAD_DATA_HELPER, 'def load_data: (untyped client, String sql, ?strict: bool) -> void']]) if copy_from?(queries) + helpers end CONNECTION_STATE = <<~'RUBY'.chomp @@ -236,6 +239,93 @@ def csv_line(values) end RUBY + QUOTE = <<~'RUBY'.chomp + def quote(client, value) + case value + when NilClass then 'NULL' + when TrueClass then 'TRUE' + when FalseClass then 'FALSE' + when Integer then value.to_s + when Float + raise ArgumentError, 'cannot bind a non-finite Float to MySQL' unless value.finite? + + value.to_s + when BigDecimal + raise ArgumentError, 'cannot bind a non-finite BigDecimal to MySQL' unless value.finite? + + value.to_s('F') + when DateTime, Time + "'#{value.strftime('%Y-%m-%d %H:%M:%S.%6N')}'" + when Date + "'#{value.strftime('%Y-%m-%d')}'" + when String + if value.encoding == Encoding::BINARY || !value.valid_encoding? + "X'#{value.unpack1('H*')}'" + else + "'#{client.escape(value)}'" + end + else + raise TypeError, "cannot bind #{value.class} to MySQL" + end + end + RUBY + + BIND_PARAMS = <<~'RUBY'.chomp + def bind_params(client, sql, params) + output = +'' + index = 0 + parameter = 0 + while index < sql.length + char = sql[index] + if ["'", '"', '`'].include?(char) + quote_char = char + start = index + index += 1 + while index < sql.length + if sql[index] == '\\' + index += 2 + elsif sql[index] == quote_char + if sql[index + 1] == quote_char + index += 2 + else + index += 1 + break + end + else + index += 1 + end + end + output << sql[start...index] + elsif sql[index, 2] == '--' && (sql[index + 2].nil? || sql[index + 2].match?(/\s/)) + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif char == '#' + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif sql[index, 2] == '/*' + finish = sql.index('*/', index + 2) + finish = finish ? finish + 2 : sql.length + output << sql[index...finish] + index = finish + elsif char == '?' + raise ArgumentError, 'more MySQL placeholders than parameters' if parameter >= params.length + + output << quote(client, params[parameter]) + parameter += 1 + index += 1 + else + output << char + index += 1 + end + end + raise ArgumentError, 'fewer MySQL placeholders than parameters' unless parameter == params.length + + output + end + RUBY + private def copy_from?(queries) = queries.any? { |q| q.cmd == ':copyfrom' } diff --git a/lib/sqlc_gen_ruby/method_gen.rb b/lib/sqlc_gen_ruby/method_gen.rb index bb9ff6b..8e35e1b 100644 --- a/lib/sqlc_gen_ruby/method_gen.rb +++ b/lib/sqlc_gen_ruby/method_gen.rb @@ -148,7 +148,8 @@ def return_type(query) def last_id_type(query) return 'Integer' if query.columns.empty? - @types.type(query.columns.first, query_name: query.name) + type = @types.type(query.columns.first, query_name: query.name) + type.end_with?('?') ? type : "#{type}?" end def copy_statements(query, params) diff --git a/steep_expectations.yml b/steep_expectations.yml index 9763999..88890f1 100644 --- a/steep_expectations.yml +++ b/steep_expectations.yml @@ -3,50 +3,140 @@ diagnostics: - range: start: - line: 334 + line: 389 character: 10 end: - line: 334 + line: 389 character: 24 severity: WARNING message: 'Cannot find the declaration of constant: `ConnectionPool`' code: Ruby::UnknownConstant - range: start: - line: 334 + line: 389 character: 47 end: - line: 334 + line: 389 character: 53 severity: WARNING message: 'Cannot find the declaration of constant: `Mysql2`' code: Ruby::UnknownConstant - range: start: - line: 334 + line: 389 character: 66 end: - line: 334 + line: 389 character: 81 severity: WARNING message: Empty kwargs doesn't have type annotation code: Ruby::UnannotatedEmptyCollection - range: start: - line: 1055 + line: 1011 character: 17 end: - line: 1055 + line: 1011 character: 23 severity: WARNING message: 'Cannot find the declaration of constant: `Mysql2`' code: Ruby::UnknownConstant - range: start: - line: 1088 + line: 1073 + character: 20 + end: + line: 1073 + character: 38 + severity: ERROR + message: |- + Cannot pass a value of type `(::String | nil)` as an argument of type `(::string | ::Integer)` + (::String | nil) <: (::string | ::Integer) + nil <: (::string | ::Integer) + nil <: ::string + nil <: (::String | ::_ToStr) + nil <: ::String + code: Ruby::ArgumentTypeMismatch + - range: + start: + line: 1074 + character: 78 + end: + line: 1074 + character: 84 + severity: ERROR + message: Type `(::String | nil)` does not have method `match?` + code: Ruby::NoMethod + - range: + start: + line: 1076 + character: 20 + end: + line: 1076 + character: 39 + severity: ERROR + message: |- + Cannot pass a value of type `(::String | nil)` as an argument of type `(::string | ::Integer)` + (::String | nil) <: (::string | ::Integer) + nil <: (::string | ::Integer) + nil <: ::string + nil <: (::String | ::_ToStr) + nil <: ::String + code: Ruby::ArgumentTypeMismatch + - range: + start: + line: 1080 + character: 20 + end: + line: 1080 + character: 39 + severity: ERROR + message: |- + Cannot pass a value of type `(::String | nil)` as an argument of type `(::string | ::Integer)` + (::String | nil) <: (::string | ::Integer) + nil <: (::string | ::Integer) + nil <: ::string + nil <: (::String | ::_ToStr) + nil <: ::String + code: Ruby::ArgumentTypeMismatch + - range: + start: + line: 1085 + character: 20 + end: + line: 1085 + character: 39 + severity: ERROR + message: |- + Cannot pass a value of type `(::String | nil)` as an argument of type `(::string | ::Integer)` + (::String | nil) <: (::string | ::Integer) + nil <: (::string | ::Integer) + nil <: ::string + nil <: (::String | ::_ToStr) + nil <: ::String + code: Ruby::ArgumentTypeMismatch + - range: + start: + line: 1094 + character: 20 + end: + line: 1094 + character: 24 + severity: ERROR + message: |- + Cannot pass a value of type `(::String | nil)` as an argument of type `(::string | ::Integer)` + (::String | nil) <: (::string | ::Integer) + nil <: (::string | ::Integer) + nil <: ::string + nil <: (::String | ::_ToStr) + nil <: ::String + code: Ruby::ArgumentTypeMismatch + - range: + start: + line: 1127 character: 16 end: - line: 1088 + line: 1127 character: 22 severity: WARNING message: 'Cannot find the declaration of constant: `Mysql2`' diff --git a/test/features_test.rb b/test/features_test.rb index 131b7b8..1377237 100644 --- a/test/features_test.rb +++ b/test/features_test.rb @@ -82,7 +82,7 @@ def test_sqlite_declared_types_follow_type_affinity end def test_execresult_returns_rows_affected_and_last_insert_id - { 'pg' => 'ExecResult.new(result.cmd_tuples, nil)', 'mysql2' => 'ExecResult.new(stmt.affected_rows, client.last_id)', + { 'pg' => 'ExecResult.new(result.cmd_tuples, nil)', 'mysql2' => 'ExecResult.new(client.affected_rows, client.last_id)', 'sqlite3' => 'ExecResult.new(client.changes, client.last_insert_row_id)' }.each do |driver, line| files = generate(load_request(driver)) assert_includes files.fetch('query_sql.rb'), line @@ -164,19 +164,35 @@ def test_pg_and_sqlite_execlastid_need_returning end end + def test_returning_last_id_is_optional_when_no_row_is_returned + %w[pg sqlite3].each do |driver| + rbs = generate(load_request(driver)).fetch('query_sql.rbs') + assert_match(/def create_author_return_id: .*-> Integer\?/, rbs) + end + end + def test_mysql_copyfrom_writes_its_own_csv files = generate(load_request('mysql2', generateGemfile: true)) refute_includes files.fetch('query_sql.rb'), "require 'csv'" assert_includes files.fetch('query_sql.rb'), "require 'tempfile'" + assert_includes files.fetch('query_sql.rb'), "require 'bigdecimal'" + assert_includes files.fetch('query_sql.rb'), "require 'date'" assert_includes files.fetch('query_sql.rb'), 'def csv_line(values)' - assert_equal "# frozen_string_literal: true\n\nsource 'https://rubygems.org'\n\ngem 'connection_pool'\ngem 'mysql2'\n", + assert_equal "# frozen_string_literal: true\n\nsource 'https://rubygems.org'\n\ngem 'bigdecimal'\ngem 'connection_pool'\ngem 'date'\ngem 'mysql2'\n", files.fetch('Gemfile') end - def test_sqlite_and_mysql_close_prepared_statements - %w[mysql2 sqlite3].each do |driver| - assert_match(/ensure\n\s+stmt\.close\n/, generate(load_request(driver)).fetch('query_sql.rb'), driver) - end + def test_mysql_uses_client_side_binding_and_sqlite_closes_prepared_statements + mysql = generate(load_request('mysql2')).fetch('query_sql.rb') + assert_includes mysql, 'client.query(bind_params(client, GET_AUTHOR_SQL, query_params), as: :array)' + assert_includes mysql, 'def quote(client, value)' + assert_includes mysql, 'def bind_params(client, sql, params)' + refute_match(/\bstmt(?:\.| =)/, mysql) + refute_includes mysql, 'client.prepare(' + refute_includes mysql, 'stmt.close' + + sqlite = generate(load_request('sqlite3')).fetch('query_sql.rb') + assert_match(/ensure\n\s+stmt\.close\n/, sqlite) end # ---- Ruby style of the generated code ------------------------------------------------------------------------ diff --git a/test/fixtures/mysql2.bin b/test/fixtures/mysql2.bin index 052e10a..07156bc 100644 --- a/test/fixtures/mysql2.bin +++ b/test/fixtures/mysql2.bin @@ -1,7 +1,7 @@ -2mysql£public"˜public€ +2mysqlópublic"èpublic€ authors& id0ÿÿÿÿÿÿÿÿÿR authorsbbigint& name0ÿÿÿÿÿÿÿÿÿR authorsbtext# @@ -87,7 +87,29 @@ copy_typesbjson flags0R  copy_typesbbit+ amount0ÿÿÿÿÿÿÿÿÿR  -copy_typesbint€": +copy_typesbint€Í +mysql_binding_values3 +id0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbbigint; + +text_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesb +longtext; + +blob_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesb +longblob; + signed_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbbigint@ +unsigned_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbbigint€4 + decimal_value0ARmysql_binding_valuesb decimal: + float_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbdouble7 + +date_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbdate6 +datetime_value0Rmysql_binding_valuesb +datetime2 + +datetime_20Rmysql_binding_valuesb +datetime1 + +bool_value0Rmysql_binding_valuesb tinyint; +nullable_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbtext": extended_bios_bio_type Autobiography BiographyMemoir"7 extended_bios_author_typeAuthorEditor TranslatorË @@ -365,4 +387,77 @@ copy_typesbjsonzdoc"% copy_typesbbitzflags"3 amount0ÿÿÿÿÿÿÿÿÿR  copy_typesbintzamount€: query.sql@ -TRUNCATE TABLE copy_typesTruncateCopyTypes:exec: query.sql"v1.27.0*T{"driver":"mysql2","generateGemfile":false,"generateTypes":true,"rubyVersion":"3.3"} \ No newline at end of file +TRUNCATE TABLE copy_typesTruncateCopyTypes:exec: query.sql± +%SELECT id FROM authors WHERE name = ?FindAuthorsByExactName:many"* +id0ÿÿÿÿÿÿÿÿÿR authorsbbigintzid*2. +name0ÿÿÿÿÿÿÿÿÿ8R authorsbtextzname: query.sqlî +\SELECT id FROM authors AS `?` +WHERE name = ? AND '?' = '?' AND "?" = "?" /* ? */ +ORDER BY idGetAuthorWithQuestionMarks:many"* +id0ÿÿÿÿÿÿÿÿÿR authorsbbigintzid*0, +name0ÿÿÿÿÿÿÿÿÿR authorsbtextzname2 ?: query.sql³ + +õINSERT INTO mysql_binding_values ( + id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, + datetime_value, datetime_2, bool_value, nullable_value +) VALUES ( + ?, ?, ?, ?, ?, + ?, ?, ?, ?, + ?, ?, ? +)InsertMysqlBindingValues:exec*EA +id0ÿÿÿÿÿÿÿÿÿ8Rpublicmysql_binding_valuesbbigintzid*UQ + +text_value0ÿÿÿÿÿÿÿÿÿ8Rpublicmysql_binding_valuesb +longtextz +text_value*UQ + +blob_value0ÿÿÿÿÿÿÿÿÿ8Rpublicmysql_binding_valuesb +longblobz +blob_value*WS + signed_value0ÿÿÿÿÿÿÿÿÿ8Rpublicmysql_binding_valuesbbigintz signed_value*^Z +unsigned_value0ÿÿÿÿÿÿÿÿÿ8Rpublicmysql_binding_valuesbbigintzunsigned_value€*QM + decimal_value0A8Rpublicmysql_binding_valuesb decimalz decimal_value*UQ + float_value0ÿÿÿÿÿÿÿÿÿ8Rpublicmysql_binding_valuesbdoublez float_value*QM + +date_value0ÿÿÿÿÿÿÿÿÿ8Rpublicmysql_binding_valuesbdatez +date_value*T P +datetime_value08Rpublicmysql_binding_valuesb +datetimezdatetime_value*L +H + +datetime_208Rpublicmysql_binding_valuesb +datetimez +datetime_2*K G + +bool_value08Rpublicmysql_binding_valuesb tinyintz +bool_value*Y U +nullable_value0ÿÿÿÿÿÿÿÿÿ8Rpublicmysql_binding_valuesbtextznullable_value: query.sqlBmysql_binding_valuesÿ +ÆSELECT id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, datetime_value, datetime_2, bool_value, nullable_value FROM mysql_binding_values WHERE id = ?GetMysqlBindingValues:one"7 +id0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbbigintzid"G + +text_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesb +longtextz +text_value"G + +blob_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesb +longblobz +blob_value"I + signed_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbbigintz signed_value"P +unsigned_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbbigintzunsigned_value€"C + decimal_value0ARmysql_binding_valuesb decimalz decimal_value"G + float_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbdoublez float_value"C + +date_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbdatez +date_value"F +datetime_value0Rmysql_binding_valuesb +datetimezdatetime_value"> + +datetime_20Rmysql_binding_valuesb +datetimez +datetime_2"= + +bool_value0Rmysql_binding_valuesb tinyintz +bool_value"K +nullable_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbtextznullable_value*=9 +id0ÿÿÿÿÿÿÿÿÿ8Rmysql_binding_valuesbbigintzid: query.sqlS +#TRUNCATE TABLE mysql_binding_valuesTruncateMysqlBindingValues:exec: 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/golden/mysql2/default/models.rb b/test/golden/mysql2/default/models.rb index 07b421a..f1181dd 100644 --- a/test/golden/mysql2/default/models.rb +++ b/test/golden/mysql2/default/models.rb @@ -78,4 +78,22 @@ class CopyType < Data.define( :amount ) end + + # rubocop:disable Naming/VariableNumber + class MysqlBindingValu < Data.define( + :id, + :text_value, + :blob_value, + :signed_value, + :unsigned_value, + :decimal_value, + :float_value, + :date_value, + :datetime_value, + :datetime_2, + :bool_value, + :nullable_value + ) + end + # rubocop:enable Naming/VariableNumber end diff --git a/test/golden/mysql2/default/models.rbs b/test/golden/mysql2/default/models.rbs index 15dee64..1888981 100644 --- a/test/golden/mysql2/default/models.rbs +++ b/test/golden/mysql2/default/models.rbs @@ -138,4 +138,33 @@ module Mysql2Codegen Integer? amount ) -> void end + + class MysqlBindingValu + attr_reader id: Integer + attr_reader text_value: String? + attr_reader blob_value: String? + attr_reader signed_value: Integer? + attr_reader unsigned_value: Integer? + attr_reader decimal_value: BigDecimal? + attr_reader float_value: Float? + attr_reader date_value: Date? + attr_reader datetime_value: Time? + attr_reader datetime_2: Time? + attr_reader bool_value: Integer? + attr_reader nullable_value: String? + def initialize: ( + Integer id, + String? text_value, + String? blob_value, + Integer? signed_value, + Integer? unsigned_value, + BigDecimal? decimal_value, + Float? float_value, + Date? date_value, + Time? datetime_value, + Time? datetime_2, + Integer? bool_value, + String? nullable_value + ) -> void + end end diff --git a/test/golden/mysql2/default/my_queries_sql.rb b/test/golden/mysql2/default/my_queries_sql.rb index 7e3b3a8..8cae2fb 100644 --- a/test/golden/mysql2/default/my_queries_sql.rb +++ b/test/golden/mysql2/default/my_queries_sql.rb @@ -3,6 +3,8 @@ # auto-generated by sqlc - do not edit require 'connection_pool' require 'mysql2' +require 'bigdecimal' +require 'date' require_relative 'connection' require_relative 'models' @@ -69,13 +71,8 @@ def transaction(isolation: nil) def with_numbers2_go(c0:) with_client('with_numbers2_go') do |client| query_params = [c0] - stmt = client.prepare(WITH_NUMBERS2_GO_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, WITH_NUMBERS2_GO_SQL, query_params)) + nil end end @@ -107,5 +104,88 @@ def with_client(name) end end end + + def quote(client, value) + case value + when NilClass then 'NULL' + when TrueClass then 'TRUE' + when FalseClass then 'FALSE' + when Integer then value.to_s + when Float + raise ArgumentError, 'cannot bind a non-finite Float to MySQL' unless value.finite? + + value.to_s + when BigDecimal + raise ArgumentError, 'cannot bind a non-finite BigDecimal to MySQL' unless value.finite? + + value.to_s('F') + when DateTime, Time + "'#{value.strftime('%Y-%m-%d %H:%M:%S.%6N')}'" + when Date + "'#{value.strftime('%Y-%m-%d')}'" + when String + if value.encoding == Encoding::BINARY || !value.valid_encoding? + "X'#{value.unpack1('H*')}'" + else + "'#{client.escape(value)}'" + end + else + raise TypeError, "cannot bind #{value.class} to MySQL" + end + end + + def bind_params(client, sql, params) + output = +'' + index = 0 + parameter = 0 + while index < sql.length + char = sql[index] + if ["'", '"', '`'].include?(char) + quote_char = char + start = index + index += 1 + while index < sql.length + if sql[index] == '\\' + index += 2 + elsif sql[index] == quote_char + if sql[index + 1] == quote_char + index += 2 + else + index += 1 + break + end + else + index += 1 + end + end + output << sql[start...index] + elsif sql[index, 2] == '--' && (sql[index + 2].nil? || sql[index + 2].match?(/\s/)) + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif char == '#' + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif sql[index, 2] == '/*' + finish = sql.index('*/', index + 2) + finish = finish ? finish + 2 : sql.length + output << sql[index...finish] + index = finish + elsif char == '?' + raise ArgumentError, 'more MySQL placeholders than parameters' if parameter >= params.length + + output << quote(client, params[parameter]) + parameter += 1 + index += 1 + else + output << char + index += 1 + end + end + raise ArgumentError, 'fewer MySQL placeholders than parameters' unless parameter == params.length + + output + end end end diff --git a/test/golden/mysql2/default/my_queries_sql.rbs b/test/golden/mysql2/default/my_queries_sql.rbs index 31a9283..4b90848 100644 --- a/test/golden/mysql2/default/my_queries_sql.rbs +++ b/test/golden/mysql2/default/my_queries_sql.rbs @@ -19,5 +19,9 @@ module Mysql2Codegen def instrument: [T] (String name) { () -> T } -> T def with_client: [T] (String name) { (untyped) -> T } -> T + + def quote: (untyped client, untyped value) -> String + + def bind_params: (untyped client, String sql, Array[untyped] params) -> String end end diff --git a/test/golden/mysql2/default/other_sql.rb b/test/golden/mysql2/default/other_sql.rb index c067da1..3428538 100644 --- a/test/golden/mysql2/default/other_sql.rb +++ b/test/golden/mysql2/default/other_sql.rb @@ -3,6 +3,8 @@ # auto-generated by sqlc - do not edit require 'connection_pool' require 'mysql2' +require 'bigdecimal' +require 'date' require_relative 'connection' require_relative 'models' @@ -88,26 +90,16 @@ def no_cols(c0:, c1:, c2:, c3:, c4:) c3, c4 ] - stmt = client.prepare(NO_COLS_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, NO_COLS_SQL, query_params)) + nil end end def other_one with_client('other_one') do |client| - stmt = client.prepare(OTHER_ONE_SQL) - begin - result = stmt.execute(as: :array) - row = result.first - row && OtherOneRow.new(row[0]) - ensure - stmt.close - end + result = client.query(bind_params(client, OTHER_ONE_SQL, []), as: :array) + row = result.first + row && OtherOneRow.new(row[0]) end end @@ -119,13 +111,8 @@ def other_one!(...) def count_rows(c0:) with_client('count_rows') do |client| query_params = [c0] - stmt = client.prepare(COUNT_ROWS_SQL) - begin - stmt.execute(*query_params) - stmt.affected_rows - ensure - stmt.close - end + client.query(bind_params(client, COUNT_ROWS_SQL, query_params)) + client.affected_rows end end @@ -157,5 +144,88 @@ def with_client(name) end end end + + def quote(client, value) + case value + when NilClass then 'NULL' + when TrueClass then 'TRUE' + when FalseClass then 'FALSE' + when Integer then value.to_s + when Float + raise ArgumentError, 'cannot bind a non-finite Float to MySQL' unless value.finite? + + value.to_s + when BigDecimal + raise ArgumentError, 'cannot bind a non-finite BigDecimal to MySQL' unless value.finite? + + value.to_s('F') + when DateTime, Time + "'#{value.strftime('%Y-%m-%d %H:%M:%S.%6N')}'" + when Date + "'#{value.strftime('%Y-%m-%d')}'" + when String + if value.encoding == Encoding::BINARY || !value.valid_encoding? + "X'#{value.unpack1('H*')}'" + else + "'#{client.escape(value)}'" + end + else + raise TypeError, "cannot bind #{value.class} to MySQL" + end + end + + def bind_params(client, sql, params) + output = +'' + index = 0 + parameter = 0 + while index < sql.length + char = sql[index] + if ["'", '"', '`'].include?(char) + quote_char = char + start = index + index += 1 + while index < sql.length + if sql[index] == '\\' + index += 2 + elsif sql[index] == quote_char + if sql[index + 1] == quote_char + index += 2 + else + index += 1 + break + end + else + index += 1 + end + end + output << sql[start...index] + elsif sql[index, 2] == '--' && (sql[index + 2].nil? || sql[index + 2].match?(/\s/)) + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif char == '#' + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif sql[index, 2] == '/*' + finish = sql.index('*/', index + 2) + finish = finish ? finish + 2 : sql.length + output << sql[index...finish] + index = finish + elsif char == '?' + raise ArgumentError, 'more MySQL placeholders than parameters' if parameter >= params.length + + output << quote(client, params[parameter]) + parameter += 1 + index += 1 + else + output << char + index += 1 + end + end + raise ArgumentError, 'fewer MySQL placeholders than parameters' unless parameter == params.length + + output + end end end diff --git a/test/golden/mysql2/default/other_sql.rbs b/test/golden/mysql2/default/other_sql.rbs index 5a73803..43983b9 100644 --- a/test/golden/mysql2/default/other_sql.rbs +++ b/test/golden/mysql2/default/other_sql.rbs @@ -34,5 +34,9 @@ module Mysql2Codegen def instrument: [T] (String name) { () -> T } -> T def with_client: [T] (String name) { (untyped) -> T } -> T + + def quote: (untyped client, untyped value) -> String + + def bind_params: (untyped client, String sql, Array[untyped] params) -> String end end diff --git a/test/golden/mysql2/default/query_sql.rb b/test/golden/mysql2/default/query_sql.rb index 690c3a0..3985144 100644 --- a/test/golden/mysql2/default/query_sql.rb +++ b/test/golden/mysql2/default/query_sql.rb @@ -3,6 +3,8 @@ # auto-generated by sqlc - do not edit require 'connection_pool' require 'mysql2' +require 'bigdecimal' +require 'date' require 'tempfile' require_relative 'connection' require_relative 'models' @@ -324,6 +326,59 @@ class ListCopyTypesRow < Data.define( TRUNCATE TABLE copy_types SQL + FIND_AUTHORS_BY_EXACT_NAME_SQL = <<~'SQL' + SELECT id FROM authors WHERE name = ? + SQL + + class FindAuthorsByExactNameRow < Data.define(:id) + end + + GET_AUTHOR_WITH_QUESTION_MARKS_SQL = <<~'SQL' + SELECT id FROM authors AS `?` + WHERE name = ? AND '?' = '?' AND "?" = "?" /* ? */ + ORDER BY id + SQL + + class GetAuthorWithQuestionMarksRow < Data.define(:id) + end + + INSERT_MYSQL_BINDING_VALUES_SQL = <<~'SQL' + INSERT INTO mysql_binding_values ( + id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, + datetime_value, datetime_2, bool_value, nullable_value + ) VALUES ( + ?, ?, ?, ?, ?, + ?, ?, ?, ?, + ?, ?, ? + ) + SQL + + GET_MYSQL_BINDING_VALUES_SQL = <<~'SQL' + SELECT id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, datetime_value, datetime_2, bool_value, nullable_value FROM mysql_binding_values WHERE id = ? + SQL + + # rubocop:disable Naming/VariableNumber + class GetMysqlBindingValuesRow < Data.define( + :id, + :text_value, + :blob_value, + :signed_value, + :unsigned_value, + :decimal_value, + :float_value, + :date_value, + :datetime_value, + :datetime_2, + :bool_value, + :nullable_value + ) + end + # rubocop:enable Naming/VariableNumber + + TRUNCATE_MYSQL_BINDING_VALUES_SQL = <<~'SQL' + TRUNCATE TABLE mysql_binding_values + SQL + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -415,14 +470,9 @@ class ExecResult < Data.define(:rows_affected, :last_insert_id) def get_author(id:) with_client('get_author') do |client| query_params = [id] - stmt = client.prepare(GET_AUTHOR_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && GetAuthorRow.new(row[0], row[1], row[2]) - ensure - stmt.close - end + result = client.query(bind_params(client, GET_AUTHOR_SQL, query_params), as: :array) + row = result.first + row && GetAuthorRow.new(row[0], row[1], row[2]) end end @@ -433,14 +483,9 @@ def get_author!(...) def list_authors with_client('list_authors') do |client| - stmt = client.prepare(LIST_AUTHORS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListAuthorsRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_AUTHORS_SQL, []), as: :array) + result.map do |row| + ListAuthorsRow.new(row[0], row[1], row[2]) end end end @@ -448,103 +493,78 @@ def list_authors def create_author(name:, bio:) with_client('create_author') do |client| query_params = [name, bio] - stmt = client.prepare(CREATE_AUTHOR_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, CREATE_AUTHOR_SQL, query_params)) + nil end end def update_author(bio:, id:) with_client('update_author') do |client| query_params = [bio, id] - stmt = client.prepare(UPDATE_AUTHOR_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, UPDATE_AUTHOR_SQL, query_params)) + nil end end def create_author_return_id(name:, bio:) with_client('create_author_return_id') do |client| query_params = [name, bio] - stmt = client.prepare(CREATE_AUTHOR_RETURN_ID_SQL) - begin - stmt.execute(*query_params) - client.last_id - ensure - stmt.close - end + client.query(bind_params(client, CREATE_AUTHOR_RETURN_ID_SQL, query_params)) + client.last_id end end def delete_author(id:) with_client('delete_author') do |client| query_params = [id] - stmt = client.prepare(DELETE_AUTHOR_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, DELETE_AUTHOR_SQL, query_params)) + nil end end def test with_client('test') do |client| - stmt = client.prepare(TEST_SQL) - begin - result = stmt.execute(as: :array) - row = result.first - row && TestRow.new( - row[0], - row[1], - row[2], - row[3], - row[4], - row[5], - row[6], - row[7], - row[8], - row[9], - row[10], - row[11], - row[12], - row[13], - row[14], - row[15], - row[16], - row[17], - row[18], - row[19], - row[20], - row[21], - row[22], - row[23], - row[24], - row[25], - row[26], - row[27], - row[28], - row[29], - row[30], - row[31], - row[32], - row[33], - row[34], - row[35], - row[36] - ) - ensure - stmt.close - end + result = client.query(bind_params(client, TEST_SQL, []), as: :array) + row = result.first + row && TestRow.new( + row[0], + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7], + row[8], + row[9], + row[10], + row[11], + row[12], + row[13], + row[14], + row[15], + row[16], + row[17], + row[18], + row[19], + row[20], + row[21], + row[22], + row[23], + row[24], + row[25], + row[26], + row[27], + row[28], + row[29], + row[30], + row[31], + row[32], + row[33], + row[34], + row[35], + row[36] + ) end end @@ -556,14 +576,9 @@ def test!(...) def list_authors_paged(limit:, offset:) with_client('list_authors_paged') do |client| query_params = [limit, offset] - stmt = client.prepare(LIST_AUTHORS_PAGED_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - ListAuthorsPagedRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_AUTHORS_PAGED_SQL, query_params), as: :array) + result.map do |row| + ListAuthorsPagedRow.new(row[0], row[1], row[2]) end end end @@ -571,14 +586,9 @@ def list_authors_paged(limit:, offset:) def get_author_by_name_pattern(name_pattern: nil) with_client('get_author_by_name_pattern') do |client| query_params = [name_pattern] - stmt = client.prepare(GET_AUTHOR_BY_NAME_PATTERN_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorByNamePatternRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, GET_AUTHOR_BY_NAME_PATTERN_SQL, query_params), as: :array) + result.map do |row| + GetAuthorByNamePatternRow.new(row[0], row[1], row[2]) end end end @@ -586,14 +596,9 @@ def get_author_by_name_pattern(name_pattern: nil) def get_authors_by_optional_name(limit:, name_filter: nil) with_client('get_authors_by_optional_name') do |client| query_params = [name_filter, name_filter, limit] - stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorsByOptionalNameRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, GET_AUTHORS_BY_OPTIONAL_NAME_SQL, query_params), as: :array) + result.map do |row| + GetAuthorsByOptionalNameRow.new(row[0], row[1], row[2]) end end end @@ -601,13 +606,8 @@ def get_authors_by_optional_name(limit:, name_filter: nil) def pause_query(seconds:) with_client('pause_query') do |client| query_params = [seconds] - stmt = client.prepare(PAUSE_QUERY_SQL) - begin - stmt.execute(*query_params, as: :array) - nil - ensure - stmt.close - end + client.query(bind_params(client, PAUSE_QUERY_SQL, query_params), as: :array) + nil end end @@ -615,13 +615,8 @@ def pause_query(seconds:) def update_author_bios(bio: nil) with_client('update_author_bios') do |client| query_params = [bio] - stmt = client.prepare(UPDATE_AUTHOR_BIOS_SQL) - begin - stmt.execute(*query_params) - stmt.affected_rows - ensure - stmt.close - end + client.query(bind_params(client, UPDATE_AUTHOR_BIOS_SQL, query_params)) + client.affected_rows end end @@ -629,14 +624,9 @@ def get_authors_by_ids(ids:) with_client('get_authors_by_ids') do |client| query_params = [*ids] sql = GET_AUTHORS_BY_IDS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) - stmt = client.prepare(sql) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorsByIdsRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, sql, query_params), as: :array) + result.map do |row| + GetAuthorsByIdsRow.new(row[0], row[1], row[2]) end end end @@ -646,14 +636,9 @@ def get_authors_by_ids_and_names(ids:, names:) query_params = [*ids, *names] sql = GET_AUTHORS_BY_IDS_AND_NAMES_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) sql = sql.gsub('/*SLICE:names*/?', slice_placeholders(names)) - stmt = client.prepare(sql) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorsByIdsAndNamesRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, sql, query_params), as: :array) + result.map do |row| + GetAuthorsByIdsAndNamesRow.new(row[0], row[1], row[2]) end end end @@ -676,56 +661,36 @@ def copy_authors(rows) def truncate_authors with_client('truncate_authors') do |client| - stmt = client.prepare(TRUNCATE_AUTHORS_SQL) - begin - stmt.execute - nil - ensure - stmt.close - end + client.query(bind_params(client, TRUNCATE_AUTHORS_SQL, [])) + nil end end def create_book(name:, author_id:) with_client('create_book') do |client| query_params = [name, author_id] - stmt = client.prepare(CREATE_BOOK_SQL) - begin - stmt.execute(*query_params) - client.last_id - ensure - stmt.close - end + client.query(bind_params(client, CREATE_BOOK_SQL, query_params)) + client.last_id end end def list_all_authors_books with_client('list_all_authors_books') do |client| - stmt = client.prepare(LIST_ALL_AUTHORS_BOOKS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListAllAuthorsBooksRow.new( - Author.new(row[0], row[1], row[2]), - Book.new(row[3], row[4], row[5], row[6]) - ) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_ALL_AUTHORS_BOOKS_SQL, []), as: :array) + result.map do |row| + ListAllAuthorsBooksRow.new( + Author.new(row[0], row[1], row[2]), + Book.new(row[3], row[4], row[5], row[6]) + ) end end end def get_duplicate_authors with_client('get_duplicate_authors') do |client| - stmt = client.prepare(GET_DUPLICATE_AUTHORS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - GetDuplicateAuthorsRow.new(Author.new(row[0], row[1], row[2]), Author.new(row[3], row[4], row[5])) - end - ensure - stmt.close + result = client.query(bind_params(client, GET_DUPLICATE_AUTHORS_SQL, []), as: :array) + result.map do |row| + GetDuplicateAuthorsRow.new(Author.new(row[0], row[1], row[2]), Author.new(row[3], row[4], row[5])) end end end @@ -733,14 +698,9 @@ def get_duplicate_authors def get_authors_by_book_name(name:) with_client('get_authors_by_book_name') do |client| query_params = [name] - stmt = client.prepare(GET_AUTHORS_BY_BOOK_NAME_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorsByBookNameRow.new(row[0], row[1], row[2], Book.new(row[3], row[4], row[5], row[6])) - end - ensure - stmt.close + result = client.query(bind_params(client, GET_AUTHORS_BY_BOOK_NAME_SQL, query_params), as: :array) + result.map do |row| + GetAuthorsByBookNameRow.new(row[0], row[1], row[2], Book.new(row[3], row[4], row[5], row[6])) end end end @@ -748,27 +708,17 @@ def get_authors_by_book_name(name:) def create_extended_bio(author_name:, name:, bio_type:, author_type:) with_client('create_extended_bio') do |client| query_params = [author_name, name, bio_type, author_type] - stmt = client.prepare(CREATE_EXTENDED_BIO_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, CREATE_EXTENDED_BIO_SQL, query_params)) + nil end end def get_first_extended_bio_by_type(bio_type:) with_client('get_first_extended_bio_by_type') do |client| query_params = [bio_type] - stmt = client.prepare(GET_FIRST_EXTENDED_BIO_BY_TYPE_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && GetFirstExtendedBioByTypeRow.new(row[0], row[1], row[2], row[3]) - ensure - stmt.close - end + result = client.query(bind_params(client, GET_FIRST_EXTENDED_BIO_BY_TYPE_SQL, query_params), as: :array) + row = result.first + row && GetFirstExtendedBioByTypeRow.new(row[0], row[1], row[2], row[3]) end end @@ -779,26 +729,16 @@ def get_first_extended_bio_by_type!(...) def truncate_extended_bios with_client('truncate_extended_bios') do |client| - stmt = client.prepare(TRUNCATE_EXTENDED_BIOS_SQL) - begin - stmt.execute - nil - ensure - stmt.close - end + client.query(bind_params(client, TRUNCATE_EXTENDED_BIOS_SQL, [])) + nil end end def create_author_result(name:, bio:) with_client('create_author_result') do |client| query_params = [name, bio] - stmt = client.prepare(CREATE_AUTHOR_RESULT_SQL) - begin - stmt.execute(*query_params) - ExecResult.new(stmt.affected_rows, client.last_id) - ensure - stmt.close - end + client.query(bind_params(client, CREATE_AUTHOR_RESULT_SQL, query_params)) + ExecResult.new(client.affected_rows, client.last_id) end end @@ -806,12 +746,7 @@ def create_authors_batch(rows) with_client('create_authors_batch') do |client| rows.each do |args| query_params = [args.name, args.bio] - stmt = client.prepare(CREATE_AUTHORS_BATCH_SQL) - begin - stmt.execute(*query_params) - ensure - stmt.close - end + client.query(bind_params(client, CREATE_AUTHORS_BATCH_SQL, query_params)) end nil end @@ -821,14 +756,9 @@ def get_author_batch(rows) with_client('get_author_batch') do |client| rows.map do |args| query_params = [args.id] - stmt = client.prepare(GET_AUTHOR_BATCH_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && GetAuthorBatchRow.new(row[0], row[1], row[2]) - ensure - stmt.close - end + result = client.query(bind_params(client, GET_AUTHOR_BATCH_SQL, query_params), as: :array) + row = result.first + row && GetAuthorBatchRow.new(row[0], row[1], row[2]) end end end @@ -837,14 +767,9 @@ def list_authors_by_name_batch(rows) with_client('list_authors_by_name_batch') do |client| rows.map do |args| query_params = [args.name] - stmt = client.prepare(LIST_AUTHORS_BY_NAME_BATCH_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - ListAuthorsByNameBatchRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_AUTHORS_BY_NAME_BATCH_SQL, query_params), as: :array) + result.map do |row| + ListAuthorsByNameBatchRow.new(row[0], row[1], row[2]) end end end @@ -853,14 +778,9 @@ def list_authors_by_name_batch(rows) def count_regressions_by_text(term:) with_client('count_regressions_by_text') do |client| query_params = [term, term] - stmt = client.prepare(COUNT_REGRESSIONS_BY_TEXT_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && CountRegressionsByTextRow.new(row[0]) - ensure - stmt.close - end + result = client.query(bind_params(client, COUNT_REGRESSIONS_BY_TEXT_SQL, query_params), as: :array) + row = result.first + row && CountRegressionsByTextRow.new(row[0]) end end @@ -872,14 +792,9 @@ def count_regressions_by_text!(...) def list_regressions_by_ids(id:, id_2:, id_3:) with_client('list_regressions_by_ids') do |client| query_params = [id, id_2, id_3] - stmt = client.prepare(LIST_REGRESSIONS_BY_IDS_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - ListRegressionsByIdsRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_REGRESSIONS_BY_IDS_SQL, query_params), as: :array) + result.map do |row| + ListRegressionsByIdsRow.new(row[0], row[1], row[2]) end end end @@ -916,41 +831,26 @@ def copy_regression_rows(rows) def list_regression_payloads with_client('list_regression_payloads') do |client| - stmt = client.prepare(LIST_REGRESSION_PAYLOADS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListRegressionPayloadsRow.new(row[0], row[1]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_REGRESSION_PAYLOADS_SQL, []), as: :array) + result.map do |row| + ListRegressionPayloadsRow.new(row[0], row[1]) end end end def list_regression_rows with_client('list_regression_rows') do |client| - stmt = client.prepare(LIST_REGRESSION_ROWS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListRegressionRowsRow.new(row[0], row[1], row[2], row[3]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_REGRESSION_ROWS_SQL, []), as: :array) + result.map do |row| + ListRegressionRowsRow.new(row[0], row[1], row[2], row[3]) end end end def truncate_regressions with_client('truncate_regressions') do |client| - stmt = client.prepare(TRUNCATE_REGRESSIONS_SQL) - begin - stmt.execute - nil - ensure - stmt.close - end + client.query(bind_params(client, TRUNCATE_REGRESSIONS_SQL, [])) + nil end end @@ -1003,12 +903,7 @@ def create_copy_types_batch(rows) with_client('create_copy_types_batch') do |client| rows.each do |args| query_params = [args.id, args.code, args.flags, args.amount] - stmt = client.prepare(CREATE_COPY_TYPES_BATCH_SQL) - begin - stmt.execute(*query_params) - ensure - stmt.close - end + client.query(bind_params(client, CREATE_COPY_TYPES_BATCH_SQL, query_params)) end nil end @@ -1018,50 +913,111 @@ def count_copy_types_by_codes(rows) with_client('count_copy_types_by_codes') do |client| rows.map do |args| query_params = [args.code, args.code_2] - stmt = client.prepare(COUNT_COPY_TYPES_BY_CODES_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && CountCopyTypesByCodesRow.new(row[0]) - ensure - stmt.close - end + result = client.query(bind_params(client, COUNT_COPY_TYPES_BY_CODES_SQL, query_params), as: :array) + row = result.first + row && CountCopyTypesByCodesRow.new(row[0]) end end end def list_copy_types with_client('list_copy_types') do |client| - stmt = client.prepare(LIST_COPY_TYPES_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListCopyTypesRow.new( - row[0], - row[1], - row[2], - row[3], - row[4] - ) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_COPY_TYPES_SQL, []), as: :array) + result.map do |row| + ListCopyTypesRow.new( + row[0], + row[1], + row[2], + row[3], + row[4] + ) end end end def truncate_copy_types with_client('truncate_copy_types') do |client| - stmt = client.prepare(TRUNCATE_COPY_TYPES_SQL) - begin - stmt.execute - nil - ensure - stmt.close + client.query(bind_params(client, TRUNCATE_COPY_TYPES_SQL, [])) + nil + end + end + + def find_authors_by_exact_name(name:) + with_client('find_authors_by_exact_name') do |client| + query_params = [name] + result = client.query(bind_params(client, FIND_AUTHORS_BY_EXACT_NAME_SQL, query_params), as: :array) + result.map do |row| + FindAuthorsByExactNameRow.new(row[0]) + end + end + end + + # ? + def get_author_with_question_marks(name:) + with_client('get_author_with_question_marks') do |client| + query_params = [name] + result = client.query(bind_params(client, GET_AUTHOR_WITH_QUESTION_MARKS_SQL, query_params), as: :array) + result.map do |row| + GetAuthorWithQuestionMarksRow.new(row[0]) end end end + def insert_mysql_binding_values(id:, text_value: nil, blob_value: nil, signed_value: nil, unsigned_value: nil, decimal_value: nil, float_value: nil, date_value: nil, datetime_value: nil, datetime_2: nil, bool_value: nil, nullable_value: nil) + with_client('insert_mysql_binding_values') do |client| + query_params = [ + id, + text_value, + blob_value, + signed_value, + unsigned_value, + decimal_value, + float_value, + date_value, + datetime_value, + datetime_2, + bool_value, + nullable_value + ] + client.query(bind_params(client, INSERT_MYSQL_BINDING_VALUES_SQL, query_params)) + nil + end + end + + def get_mysql_binding_values(id:) + with_client('get_mysql_binding_values') do |client| + query_params = [id] + result = client.query(bind_params(client, GET_MYSQL_BINDING_VALUES_SQL, query_params), as: :array) + row = result.first + row && GetMysqlBindingValuesRow.new( + row[0], + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7], + row[8], + row[9], + row[10], + row[11] + ) + end + end + + # Like #get_mysql_binding_values, but raises NotFound when there is no row. + def get_mysql_binding_values!(...) + get_mysql_binding_values(...) || raise(NotFound, 'GetMysqlBindingValues returned no row') + end + + def truncate_mysql_binding_values + with_client('truncate_mysql_binding_values') do |client| + client.query(bind_params(client, TRUNCATE_MYSQL_BINDING_VALUES_SQL, [])) + nil + end + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ @@ -1071,14 +1027,9 @@ def many_params(c0:, c1:, c2:, c3:, c4:) c3, c4 ] - stmt = client.prepare(MANY_PARAMS_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && ManyParamsRow.new(row[0], row[1]) - ensure - stmt.close - end + result = client.query(bind_params(client, MANY_PARAMS_SQL, query_params), as: :array) + row = result.first + row && ManyParamsRow.new(row[0], row[1]) end end @@ -1090,34 +1041,24 @@ def many_params!(...) def many_rows(c0:) with_client('many_rows') do |client| query_params = [c0] - stmt = client.prepare(MANY_ROWS_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - ManyRowsRow.new( - row[0], - row[1], - row[2], - row[3], - row[4] - ) - end - ensure - stmt.close + result = client.query(bind_params(client, MANY_ROWS_SQL, query_params), as: :array) + result.map do |row| + ManyRowsRow.new( + row[0], + row[1], + row[2], + row[3], + row[4] + ) end end end def four_cols with_client('four_cols') do |client| - stmt = client.prepare(FOUR_COLS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - FourColsRow.new(row[0], row[1], row[2], row[3]) - end - ensure - stmt.close + result = client.query(bind_params(client, FOUR_COLS_SQL, []), as: :array) + result.map do |row| + FourColsRow.new(row[0], row[1], row[2], row[3]) end end end @@ -1151,6 +1092,89 @@ def with_client(name) end end + def quote(client, value) + case value + when NilClass then 'NULL' + when TrueClass then 'TRUE' + when FalseClass then 'FALSE' + when Integer then value.to_s + when Float + raise ArgumentError, 'cannot bind a non-finite Float to MySQL' unless value.finite? + + value.to_s + when BigDecimal + raise ArgumentError, 'cannot bind a non-finite BigDecimal to MySQL' unless value.finite? + + value.to_s('F') + when DateTime, Time + "'#{value.strftime('%Y-%m-%d %H:%M:%S.%6N')}'" + when Date + "'#{value.strftime('%Y-%m-%d')}'" + when String + if value.encoding == Encoding::BINARY || !value.valid_encoding? + "X'#{value.unpack1('H*')}'" + else + "'#{client.escape(value)}'" + end + else + raise TypeError, "cannot bind #{value.class} to MySQL" + end + end + + def bind_params(client, sql, params) + output = +'' + index = 0 + parameter = 0 + while index < sql.length + char = sql[index] + if ["'", '"', '`'].include?(char) + quote_char = char + start = index + index += 1 + while index < sql.length + if sql[index] == '\\' + index += 2 + elsif sql[index] == quote_char + if sql[index + 1] == quote_char + index += 2 + else + index += 1 + break + end + else + index += 1 + end + end + output << sql[start...index] + elsif sql[index, 2] == '--' && (sql[index + 2].nil? || sql[index + 2].match?(/\s/)) + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif char == '#' + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif sql[index, 2] == '/*' + finish = sql.index('*/', index + 2) + finish = finish ? finish + 2 : sql.length + output << sql[index...finish] + index = finish + elsif char == '?' + raise ArgumentError, 'more MySQL placeholders than parameters' if parameter >= params.length + + output << quote(client, params[parameter]) + parameter += 1 + index += 1 + else + output << char + index += 1 + end + end + raise ArgumentError, 'fewer MySQL placeholders than parameters' unless parameter == params.length + + output + end + # Formats one row for LOAD DATA ... FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\'. def csv_line(values) fields = values.map do |value| diff --git a/test/golden/mysql2/default/query_sql.rbs b/test/golden/mysql2/default/query_sql.rbs index 3064f05..6bccdc0 100644 --- a/test/golden/mysql2/default/query_sql.rbs +++ b/test/golden/mysql2/default/query_sql.rbs @@ -368,6 +368,55 @@ module Mysql2Codegen TRUNCATE_COPY_TYPES_SQL: String + FIND_AUTHORS_BY_EXACT_NAME_SQL: String + + class FindAuthorsByExactNameRow + attr_reader id: Integer + def initialize: ( Integer id) -> void + end + + GET_AUTHOR_WITH_QUESTION_MARKS_SQL: String + + class GetAuthorWithQuestionMarksRow + attr_reader id: Integer + def initialize: ( Integer id) -> void + end + + INSERT_MYSQL_BINDING_VALUES_SQL: String + + GET_MYSQL_BINDING_VALUES_SQL: String + + class GetMysqlBindingValuesRow + attr_reader id: Integer + attr_reader text_value: String? + attr_reader blob_value: String? + attr_reader signed_value: Integer? + attr_reader unsigned_value: Integer? + attr_reader decimal_value: BigDecimal? + attr_reader float_value: Float? + attr_reader date_value: Date? + attr_reader datetime_value: Time? + attr_reader datetime_2: Time? + attr_reader bool_value: Integer? + attr_reader nullable_value: String? + def initialize: ( + Integer id, + String? text_value, + String? blob_value, + Integer? signed_value, + Integer? unsigned_value, + BigDecimal? decimal_value, + Float? float_value, + Date? date_value, + Time? datetime_value, + Time? datetime_2, + Integer? bool_value, + String? nullable_value + ) -> void + end + + TRUNCATE_MYSQL_BINDING_VALUES_SQL: String + MANY_PARAMS_SQL: String class ManyParamsRow @@ -509,6 +558,18 @@ module Mysql2Codegen def truncate_copy_types: -> void + def find_authors_by_exact_name: (name: String) -> Array[FindAuthorsByExactNameRow] + + def get_author_with_question_marks: (name: String) -> Array[GetAuthorWithQuestionMarksRow] + + def insert_mysql_binding_values: (id: Integer, ?text_value: String?, ?blob_value: String?, ?signed_value: Integer?, ?unsigned_value: Integer?, ?decimal_value: BigDecimal?, ?float_value: Float?, ?date_value: Date?, ?datetime_value: Time?, ?datetime_2: Time?, ?bool_value: Integer?, ?nullable_value: String?) -> void + + def get_mysql_binding_values: (id: Integer) -> GetMysqlBindingValuesRow? + + def get_mysql_binding_values!: (id: Integer) -> GetMysqlBindingValuesRow + + def truncate_mysql_binding_values: -> 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 @@ -523,6 +584,10 @@ module Mysql2Codegen def with_client: [T] (String name) { (untyped) -> T } -> T + def quote: (untyped client, untyped value) -> String + + def bind_params: (untyped client, String sql, Array[untyped] params) -> String + def csv_line: (Array[untyped] values) -> String def load_data: (untyped client, String sql, ?strict: bool) -> void diff --git a/test/golden/mysql2/ruby31_gemfile/Gemfile b/test/golden/mysql2/ruby31_gemfile/Gemfile index 6e1d56d..2c8274f 100644 --- a/test/golden/mysql2/ruby31_gemfile/Gemfile +++ b/test/golden/mysql2/ruby31_gemfile/Gemfile @@ -2,6 +2,8 @@ source 'https://rubygems.org' +gem 'bigdecimal' gem 'connection_pool' +gem 'date' gem 'mysql2' gem 'timeout', '>= 0.4' diff --git a/test/golden/mysql2/ruby31_gemfile/models.rb b/test/golden/mysql2/ruby31_gemfile/models.rb index 8da87bb..772053a 100644 --- a/test/golden/mysql2/ruby31_gemfile/models.rb +++ b/test/golden/mysql2/ruby31_gemfile/models.rb @@ -92,4 +92,22 @@ module ExtendedBiosAuthorType :amount, keyword_init: true ) + + # rubocop:disable Naming/VariableNumber + MysqlBindingValu = Struct.new( + :id, + :text_value, + :blob_value, + :signed_value, + :unsigned_value, + :decimal_value, + :float_value, + :date_value, + :datetime_value, + :datetime_2, + :bool_value, + :nullable_value, + keyword_init: true + ) + # rubocop:enable Naming/VariableNumber end diff --git a/test/golden/mysql2/ruby31_gemfile/models.rbs b/test/golden/mysql2/ruby31_gemfile/models.rbs index e5769a0..77b4ead 100644 --- a/test/golden/mysql2/ruby31_gemfile/models.rbs +++ b/test/golden/mysql2/ruby31_gemfile/models.rbs @@ -188,4 +188,47 @@ module Mysql2Codegen amount: Integer? ) -> CopyType end + + class MysqlBindingValu < ::Struct[untyped] + attr_accessor id: Integer + attr_accessor text_value: String? + attr_accessor blob_value: String? + attr_accessor signed_value: Integer? + attr_accessor unsigned_value: Integer? + attr_accessor decimal_value: BigDecimal? + attr_accessor float_value: Float? + attr_accessor date_value: Date? + attr_accessor datetime_value: Time? + attr_accessor datetime_2: Time? + attr_accessor bool_value: Integer? + attr_accessor nullable_value: String? + def initialize: ( + id: Integer, + text_value: String?, + blob_value: String?, + signed_value: Integer?, + unsigned_value: Integer?, + decimal_value: BigDecimal?, + float_value: Float?, + date_value: Date?, + datetime_value: Time?, + datetime_2: Time?, + bool_value: Integer?, + nullable_value: String? + ) -> void + def self.new: ( + id: Integer, + text_value: String?, + blob_value: String?, + signed_value: Integer?, + unsigned_value: Integer?, + decimal_value: BigDecimal?, + float_value: Float?, + date_value: Date?, + datetime_value: Time?, + datetime_2: Time?, + bool_value: Integer?, + nullable_value: String? + ) -> MysqlBindingValu + end end diff --git a/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rb b/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rb index dbb6bac..13d5741 100644 --- a/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rb +++ b/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rb @@ -3,6 +3,8 @@ # auto-generated by sqlc - do not edit require 'connection_pool' require 'mysql2' +require 'bigdecimal' +require 'date' require_relative 'connection' require_relative 'models' @@ -72,13 +74,8 @@ def transaction(isolation: nil) def with_numbers2_go(c0:) with_client('with_numbers2_go') do |client| query_params = [c0] - stmt = client.prepare(WITH_NUMBERS2_GO_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, WITH_NUMBERS2_GO_SQL, query_params)) + nil end end @@ -115,5 +112,88 @@ def timeout_exits_with_throw? !!(defined?(::Timeout::VERSION) && (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) end + + def quote(client, value) + case value + when NilClass then 'NULL' + when TrueClass then 'TRUE' + when FalseClass then 'FALSE' + when Integer then value.to_s + when Float + raise ArgumentError, 'cannot bind a non-finite Float to MySQL' unless value.finite? + + value.to_s + when BigDecimal + raise ArgumentError, 'cannot bind a non-finite BigDecimal to MySQL' unless value.finite? + + value.to_s('F') + when DateTime, Time + "'#{value.strftime('%Y-%m-%d %H:%M:%S.%6N')}'" + when Date + "'#{value.strftime('%Y-%m-%d')}'" + when String + if value.encoding == Encoding::BINARY || !value.valid_encoding? + "X'#{value.unpack1('H*')}'" + else + "'#{client.escape(value)}'" + end + else + raise TypeError, "cannot bind #{value.class} to MySQL" + end + end + + def bind_params(client, sql, params) + output = +'' + index = 0 + parameter = 0 + while index < sql.length + char = sql[index] + if ["'", '"', '`'].include?(char) + quote_char = char + start = index + index += 1 + while index < sql.length + if sql[index] == '\\' + index += 2 + elsif sql[index] == quote_char + if sql[index + 1] == quote_char + index += 2 + else + index += 1 + break + end + else + index += 1 + end + end + output << sql[start...index] + elsif sql[index, 2] == '--' && (sql[index + 2].nil? || sql[index + 2].match?(/\s/)) + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif char == '#' + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif sql[index, 2] == '/*' + finish = sql.index('*/', index + 2) + finish = finish ? finish + 2 : sql.length + output << sql[index...finish] + index = finish + elsif char == '?' + raise ArgumentError, 'more MySQL placeholders than parameters' if parameter >= params.length + + output << quote(client, params[parameter]) + parameter += 1 + index += 1 + else + output << char + index += 1 + end + end + raise ArgumentError, 'fewer MySQL placeholders than parameters' unless parameter == params.length + + output + end end end diff --git a/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rbs b/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rbs index 3d6482f..23d610a 100644 --- a/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rbs +++ b/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rbs @@ -21,5 +21,9 @@ module Mysql2Codegen def with_client: [T] (String name) { (untyped) -> T } -> T def timeout_exits_with_throw?: () -> bool + + def quote: (untyped client, untyped value) -> String + + def bind_params: (untyped client, String sql, Array[untyped] params) -> String end end diff --git a/test/golden/mysql2/ruby31_gemfile/other_sql.rb b/test/golden/mysql2/ruby31_gemfile/other_sql.rb index b784a0b..3216306 100644 --- a/test/golden/mysql2/ruby31_gemfile/other_sql.rb +++ b/test/golden/mysql2/ruby31_gemfile/other_sql.rb @@ -3,6 +3,8 @@ # auto-generated by sqlc - do not edit require 'connection_pool' require 'mysql2' +require 'bigdecimal' +require 'date' require_relative 'connection' require_relative 'models' @@ -90,26 +92,16 @@ def no_cols(c0:, c1:, c2:, c3:, c4:) c3, c4 ] - stmt = client.prepare(NO_COLS_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, NO_COLS_SQL, query_params)) + nil end end def other_one with_client('other_one') do |client| - stmt = client.prepare(OTHER_ONE_SQL) - begin - result = stmt.execute(as: :array) - row = result.first - row && OtherOneRow.new(c0: row[0]) - ensure - stmt.close - end + result = client.query(bind_params(client, OTHER_ONE_SQL, []), as: :array) + row = result.first + row && OtherOneRow.new(c0: row[0]) end end @@ -121,13 +113,8 @@ def other_one!(...) def count_rows(c0:) with_client('count_rows') do |client| query_params = [c0] - stmt = client.prepare(COUNT_ROWS_SQL) - begin - stmt.execute(*query_params) - stmt.affected_rows - ensure - stmt.close - end + client.query(bind_params(client, COUNT_ROWS_SQL, query_params)) + client.affected_rows end end @@ -164,5 +151,88 @@ def timeout_exits_with_throw? !!(defined?(::Timeout::VERSION) && (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) end + + def quote(client, value) + case value + when NilClass then 'NULL' + when TrueClass then 'TRUE' + when FalseClass then 'FALSE' + when Integer then value.to_s + when Float + raise ArgumentError, 'cannot bind a non-finite Float to MySQL' unless value.finite? + + value.to_s + when BigDecimal + raise ArgumentError, 'cannot bind a non-finite BigDecimal to MySQL' unless value.finite? + + value.to_s('F') + when DateTime, Time + "'#{value.strftime('%Y-%m-%d %H:%M:%S.%6N')}'" + when Date + "'#{value.strftime('%Y-%m-%d')}'" + when String + if value.encoding == Encoding::BINARY || !value.valid_encoding? + "X'#{value.unpack1('H*')}'" + else + "'#{client.escape(value)}'" + end + else + raise TypeError, "cannot bind #{value.class} to MySQL" + end + end + + def bind_params(client, sql, params) + output = +'' + index = 0 + parameter = 0 + while index < sql.length + char = sql[index] + if ["'", '"', '`'].include?(char) + quote_char = char + start = index + index += 1 + while index < sql.length + if sql[index] == '\\' + index += 2 + elsif sql[index] == quote_char + if sql[index + 1] == quote_char + index += 2 + else + index += 1 + break + end + else + index += 1 + end + end + output << sql[start...index] + elsif sql[index, 2] == '--' && (sql[index + 2].nil? || sql[index + 2].match?(/\s/)) + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif char == '#' + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif sql[index, 2] == '/*' + finish = sql.index('*/', index + 2) + finish = finish ? finish + 2 : sql.length + output << sql[index...finish] + index = finish + elsif char == '?' + raise ArgumentError, 'more MySQL placeholders than parameters' if parameter >= params.length + + output << quote(client, params[parameter]) + parameter += 1 + index += 1 + else + output << char + index += 1 + end + end + raise ArgumentError, 'fewer MySQL placeholders than parameters' unless parameter == params.length + + output + end end end diff --git a/test/golden/mysql2/ruby31_gemfile/other_sql.rbs b/test/golden/mysql2/ruby31_gemfile/other_sql.rbs index 0b0621e..ad35cec 100644 --- a/test/golden/mysql2/ruby31_gemfile/other_sql.rbs +++ b/test/golden/mysql2/ruby31_gemfile/other_sql.rbs @@ -37,5 +37,9 @@ module Mysql2Codegen def with_client: [T] (String name) { (untyped) -> T } -> T def timeout_exits_with_throw?: () -> bool + + def quote: (untyped client, untyped value) -> String + + def bind_params: (untyped client, String sql, Array[untyped] params) -> String end end diff --git a/test/golden/mysql2/ruby31_gemfile/query_sql.rb b/test/golden/mysql2/ruby31_gemfile/query_sql.rb index 7f13d00..f57a12c 100644 --- a/test/golden/mysql2/ruby31_gemfile/query_sql.rb +++ b/test/golden/mysql2/ruby31_gemfile/query_sql.rb @@ -3,6 +3,8 @@ # auto-generated by sqlc - do not edit require 'connection_pool' require 'mysql2' +require 'bigdecimal' +require 'date' require 'tempfile' require_relative 'connection' require_relative 'models' @@ -325,6 +327,57 @@ module Mysql2Codegen TRUNCATE TABLE copy_types SQL + FIND_AUTHORS_BY_EXACT_NAME_SQL = <<~'SQL' + SELECT id FROM authors WHERE name = ? + SQL + + FindAuthorsByExactNameRow = Struct.new(:id, keyword_init: true) + + GET_AUTHOR_WITH_QUESTION_MARKS_SQL = <<~'SQL' + SELECT id FROM authors AS `?` + WHERE name = ? AND '?' = '?' AND "?" = "?" /* ? */ + ORDER BY id + SQL + + GetAuthorWithQuestionMarksRow = Struct.new(:id, keyword_init: true) + + INSERT_MYSQL_BINDING_VALUES_SQL = <<~'SQL' + INSERT INTO mysql_binding_values ( + id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, + datetime_value, datetime_2, bool_value, nullable_value + ) VALUES ( + ?, ?, ?, ?, ?, + ?, ?, ?, ?, + ?, ?, ? + ) + SQL + + GET_MYSQL_BINDING_VALUES_SQL = <<~'SQL' + SELECT id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, datetime_value, datetime_2, bool_value, nullable_value FROM mysql_binding_values WHERE id = ? + SQL + + # rubocop:disable Naming/VariableNumber + GetMysqlBindingValuesRow = Struct.new( + :id, + :text_value, + :blob_value, + :signed_value, + :unsigned_value, + :decimal_value, + :float_value, + :date_value, + :datetime_value, + :datetime_2, + :bool_value, + :nullable_value, + keyword_init: true + ) + # rubocop:enable Naming/VariableNumber + + TRUNCATE_MYSQL_BINDING_VALUES_SQL = <<~'SQL' + TRUNCATE TABLE mysql_binding_values + SQL + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -422,14 +475,9 @@ def transaction(isolation: nil) def get_author(id:) with_client('get_author') do |client| query_params = [id] - stmt = client.prepare(GET_AUTHOR_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && GetAuthorRow.new(id: row[0], name: row[1], bio: row[2]) - ensure - stmt.close - end + result = client.query(bind_params(client, GET_AUTHOR_SQL, query_params), as: :array) + row = result.first + row && GetAuthorRow.new(id: row[0], name: row[1], bio: row[2]) end end @@ -440,14 +488,9 @@ def get_author!(...) def list_authors with_client('list_authors') do |client| - stmt = client.prepare(LIST_AUTHORS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListAuthorsRow.new(id: row[0], name: row[1], bio: row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_AUTHORS_SQL, []), as: :array) + result.map do |row| + ListAuthorsRow.new(id: row[0], name: row[1], bio: row[2]) end end end @@ -455,103 +498,78 @@ def list_authors def create_author(name:, bio:) with_client('create_author') do |client| query_params = [name, bio] - stmt = client.prepare(CREATE_AUTHOR_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, CREATE_AUTHOR_SQL, query_params)) + nil end end def update_author(bio:, id:) with_client('update_author') do |client| query_params = [bio, id] - stmt = client.prepare(UPDATE_AUTHOR_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, UPDATE_AUTHOR_SQL, query_params)) + nil end end def create_author_return_id(name:, bio:) with_client('create_author_return_id') do |client| query_params = [name, bio] - stmt = client.prepare(CREATE_AUTHOR_RETURN_ID_SQL) - begin - stmt.execute(*query_params) - client.last_id - ensure - stmt.close - end + client.query(bind_params(client, CREATE_AUTHOR_RETURN_ID_SQL, query_params)) + client.last_id end end def delete_author(id:) with_client('delete_author') do |client| query_params = [id] - stmt = client.prepare(DELETE_AUTHOR_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, DELETE_AUTHOR_SQL, query_params)) + nil end end def test with_client('test') do |client| - stmt = client.prepare(TEST_SQL) - begin - result = stmt.execute(as: :array) - row = result.first - row && TestRow.new( - c_bit: row[0], - c_tinyint: row[1], - c_bool: row[2], - c_boolean: row[3], - c_smallint: row[4], - c_mediumint: row[5], - c_int: row[6], - c_integer: row[7], - c_bigint: row[8], - c_serial: row[9], - c_decimal: row[10], - c_dec: row[11], - c_numeric: row[12], - c_fixed: row[13], - c_float: row[14], - c_double: row[15], - c_double_precision: row[16], - c_date: row[17], - c_time: row[18], - c_datetime: row[19], - c_timestamp: row[20], - c_year: row[21], - c_char: row[22], - c_nchar: row[23], - c_national_char: row[24], - c_varchar: row[25], - c_binary: row[26], - c_varbinary: row[27], - c_tinyblob: row[28], - c_tinytext: row[29], - c_blob: row[30], - c_text: row[31], - c_mediumblob: row[32], - c_mediumtext: row[33], - c_longblob: row[34], - c_longtext: row[35], - c_json: row[36] - ) - ensure - stmt.close - end + result = client.query(bind_params(client, TEST_SQL, []), as: :array) + row = result.first + row && TestRow.new( + c_bit: row[0], + c_tinyint: row[1], + c_bool: row[2], + c_boolean: row[3], + c_smallint: row[4], + c_mediumint: row[5], + c_int: row[6], + c_integer: row[7], + c_bigint: row[8], + c_serial: row[9], + c_decimal: row[10], + c_dec: row[11], + c_numeric: row[12], + c_fixed: row[13], + c_float: row[14], + c_double: row[15], + c_double_precision: row[16], + c_date: row[17], + c_time: row[18], + c_datetime: row[19], + c_timestamp: row[20], + c_year: row[21], + c_char: row[22], + c_nchar: row[23], + c_national_char: row[24], + c_varchar: row[25], + c_binary: row[26], + c_varbinary: row[27], + c_tinyblob: row[28], + c_tinytext: row[29], + c_blob: row[30], + c_text: row[31], + c_mediumblob: row[32], + c_mediumtext: row[33], + c_longblob: row[34], + c_longtext: row[35], + c_json: row[36] + ) end end @@ -563,14 +581,9 @@ def test!(...) def list_authors_paged(limit:, offset:) with_client('list_authors_paged') do |client| query_params = [limit, offset] - stmt = client.prepare(LIST_AUTHORS_PAGED_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - ListAuthorsPagedRow.new(id: row[0], name: row[1], bio: row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_AUTHORS_PAGED_SQL, query_params), as: :array) + result.map do |row| + ListAuthorsPagedRow.new(id: row[0], name: row[1], bio: row[2]) end end end @@ -578,14 +591,9 @@ def list_authors_paged(limit:, offset:) def get_author_by_name_pattern(name_pattern: nil) with_client('get_author_by_name_pattern') do |client| query_params = [name_pattern] - stmt = client.prepare(GET_AUTHOR_BY_NAME_PATTERN_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorByNamePatternRow.new(id: row[0], name: row[1], bio: row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, GET_AUTHOR_BY_NAME_PATTERN_SQL, query_params), as: :array) + result.map do |row| + GetAuthorByNamePatternRow.new(id: row[0], name: row[1], bio: row[2]) end end end @@ -593,14 +601,9 @@ def get_author_by_name_pattern(name_pattern: nil) def get_authors_by_optional_name(limit:, name_filter: nil) with_client('get_authors_by_optional_name') do |client| query_params = [name_filter, name_filter, limit] - stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorsByOptionalNameRow.new(id: row[0], name: row[1], bio: row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, GET_AUTHORS_BY_OPTIONAL_NAME_SQL, query_params), as: :array) + result.map do |row| + GetAuthorsByOptionalNameRow.new(id: row[0], name: row[1], bio: row[2]) end end end @@ -608,13 +611,8 @@ def get_authors_by_optional_name(limit:, name_filter: nil) def pause_query(seconds:) with_client('pause_query') do |client| query_params = [seconds] - stmt = client.prepare(PAUSE_QUERY_SQL) - begin - stmt.execute(*query_params, as: :array) - nil - ensure - stmt.close - end + client.query(bind_params(client, PAUSE_QUERY_SQL, query_params), as: :array) + nil end end @@ -622,13 +620,8 @@ def pause_query(seconds:) def update_author_bios(bio: nil) with_client('update_author_bios') do |client| query_params = [bio] - stmt = client.prepare(UPDATE_AUTHOR_BIOS_SQL) - begin - stmt.execute(*query_params) - stmt.affected_rows - ensure - stmt.close - end + client.query(bind_params(client, UPDATE_AUTHOR_BIOS_SQL, query_params)) + client.affected_rows end end @@ -636,14 +629,9 @@ def get_authors_by_ids(ids:) with_client('get_authors_by_ids') do |client| query_params = [*ids] sql = GET_AUTHORS_BY_IDS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) - stmt = client.prepare(sql) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorsByIdsRow.new(id: row[0], name: row[1], bio: row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, sql, query_params), as: :array) + result.map do |row| + GetAuthorsByIdsRow.new(id: row[0], name: row[1], bio: row[2]) end end end @@ -653,14 +641,9 @@ def get_authors_by_ids_and_names(ids:, names:) query_params = [*ids, *names] sql = GET_AUTHORS_BY_IDS_AND_NAMES_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) sql = sql.gsub('/*SLICE:names*/?', slice_placeholders(names)) - stmt = client.prepare(sql) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorsByIdsAndNamesRow.new(id: row[0], name: row[1], bio: row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, sql, query_params), as: :array) + result.map do |row| + GetAuthorsByIdsAndNamesRow.new(id: row[0], name: row[1], bio: row[2]) end end end @@ -683,59 +666,39 @@ def copy_authors(rows) def truncate_authors with_client('truncate_authors') do |client| - stmt = client.prepare(TRUNCATE_AUTHORS_SQL) - begin - stmt.execute - nil - ensure - stmt.close - end + client.query(bind_params(client, TRUNCATE_AUTHORS_SQL, [])) + nil end end def create_book(name:, author_id:) with_client('create_book') do |client| query_params = [name, author_id] - stmt = client.prepare(CREATE_BOOK_SQL) - begin - stmt.execute(*query_params) - client.last_id - ensure - stmt.close - end + client.query(bind_params(client, CREATE_BOOK_SQL, query_params)) + client.last_id end end def list_all_authors_books with_client('list_all_authors_books') do |client| - stmt = client.prepare(LIST_ALL_AUTHORS_BOOKS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListAllAuthorsBooksRow.new( - author: Author.new(id: row[0], name: row[1], bio: row[2]), - book: Book.new(id: row[3], name: row[4], author_id: row[5], description: row[6]) - ) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_ALL_AUTHORS_BOOKS_SQL, []), as: :array) + result.map do |row| + ListAllAuthorsBooksRow.new( + author: Author.new(id: row[0], name: row[1], bio: row[2]), + book: Book.new(id: row[3], name: row[4], author_id: row[5], description: row[6]) + ) end end end def get_duplicate_authors with_client('get_duplicate_authors') do |client| - stmt = client.prepare(GET_DUPLICATE_AUTHORS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - GetDuplicateAuthorsRow.new( - author: Author.new(id: row[0], name: row[1], bio: row[2]), - author2: Author.new(id: row[3], name: row[4], bio: row[5]) - ) - end - ensure - stmt.close + result = client.query(bind_params(client, GET_DUPLICATE_AUTHORS_SQL, []), as: :array) + result.map do |row| + GetDuplicateAuthorsRow.new( + author: Author.new(id: row[0], name: row[1], bio: row[2]), + author2: Author.new(id: row[3], name: row[4], bio: row[5]) + ) end end end @@ -743,19 +706,14 @@ def get_duplicate_authors def get_authors_by_book_name(name:) with_client('get_authors_by_book_name') do |client| query_params = [name] - stmt = client.prepare(GET_AUTHORS_BY_BOOK_NAME_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorsByBookNameRow.new( - id: row[0], - name: row[1], - bio: row[2], - book: Book.new(id: row[3], name: row[4], author_id: row[5], description: row[6]) - ) - end - ensure - stmt.close + result = client.query(bind_params(client, GET_AUTHORS_BY_BOOK_NAME_SQL, query_params), as: :array) + result.map do |row| + GetAuthorsByBookNameRow.new( + id: row[0], + name: row[1], + bio: row[2], + book: Book.new(id: row[3], name: row[4], author_id: row[5], description: row[6]) + ) end end end @@ -763,32 +721,22 @@ def get_authors_by_book_name(name:) def create_extended_bio(author_name:, name:, bio_type:, author_type:) with_client('create_extended_bio') do |client| query_params = [author_name, name, bio_type, author_type] - stmt = client.prepare(CREATE_EXTENDED_BIO_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, CREATE_EXTENDED_BIO_SQL, query_params)) + nil end end def get_first_extended_bio_by_type(bio_type:) with_client('get_first_extended_bio_by_type') do |client| query_params = [bio_type] - stmt = client.prepare(GET_FIRST_EXTENDED_BIO_BY_TYPE_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && GetFirstExtendedBioByTypeRow.new( - author_name: row[0], - name: row[1], - bio_type: row[2], - author_type: row[3] - ) - ensure - stmt.close - end + result = client.query(bind_params(client, GET_FIRST_EXTENDED_BIO_BY_TYPE_SQL, query_params), as: :array) + row = result.first + row && GetFirstExtendedBioByTypeRow.new( + author_name: row[0], + name: row[1], + bio_type: row[2], + author_type: row[3] + ) end end @@ -799,26 +747,16 @@ def get_first_extended_bio_by_type!(...) def truncate_extended_bios with_client('truncate_extended_bios') do |client| - stmt = client.prepare(TRUNCATE_EXTENDED_BIOS_SQL) - begin - stmt.execute - nil - ensure - stmt.close - end + client.query(bind_params(client, TRUNCATE_EXTENDED_BIOS_SQL, [])) + nil end end def create_author_result(name:, bio:) with_client('create_author_result') do |client| query_params = [name, bio] - stmt = client.prepare(CREATE_AUTHOR_RESULT_SQL) - begin - stmt.execute(*query_params) - ExecResult.new(rows_affected: stmt.affected_rows, last_insert_id: client.last_id) - ensure - stmt.close - end + client.query(bind_params(client, CREATE_AUTHOR_RESULT_SQL, query_params)) + ExecResult.new(rows_affected: client.affected_rows, last_insert_id: client.last_id) end end @@ -826,12 +764,7 @@ def create_authors_batch(rows) with_client('create_authors_batch') do |client| rows.each do |args| query_params = [args.name, args.bio] - stmt = client.prepare(CREATE_AUTHORS_BATCH_SQL) - begin - stmt.execute(*query_params) - ensure - stmt.close - end + client.query(bind_params(client, CREATE_AUTHORS_BATCH_SQL, query_params)) end nil end @@ -841,14 +774,9 @@ def get_author_batch(rows) with_client('get_author_batch') do |client| rows.map do |args| query_params = [args.id] - stmt = client.prepare(GET_AUTHOR_BATCH_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && GetAuthorBatchRow.new(id: row[0], name: row[1], bio: row[2]) - ensure - stmt.close - end + result = client.query(bind_params(client, GET_AUTHOR_BATCH_SQL, query_params), as: :array) + row = result.first + row && GetAuthorBatchRow.new(id: row[0], name: row[1], bio: row[2]) end end end @@ -857,14 +785,9 @@ def list_authors_by_name_batch(rows) with_client('list_authors_by_name_batch') do |client| rows.map do |args| query_params = [args.name] - stmt = client.prepare(LIST_AUTHORS_BY_NAME_BATCH_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - ListAuthorsByNameBatchRow.new(id: row[0], name: row[1], bio: row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_AUTHORS_BY_NAME_BATCH_SQL, query_params), as: :array) + result.map do |row| + ListAuthorsByNameBatchRow.new(id: row[0], name: row[1], bio: row[2]) end end end @@ -873,14 +796,9 @@ def list_authors_by_name_batch(rows) def count_regressions_by_text(term:) with_client('count_regressions_by_text') do |client| query_params = [term, term] - stmt = client.prepare(COUNT_REGRESSIONS_BY_TEXT_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && CountRegressionsByTextRow.new(count_: row[0]) - ensure - stmt.close - end + result = client.query(bind_params(client, COUNT_REGRESSIONS_BY_TEXT_SQL, query_params), as: :array) + row = result.first + row && CountRegressionsByTextRow.new(count_: row[0]) end end @@ -892,14 +810,9 @@ def count_regressions_by_text!(...) def list_regressions_by_ids(id:, id_2:, id_3:) with_client('list_regressions_by_ids') do |client| query_params = [id, id_2, id_3] - stmt = client.prepare(LIST_REGRESSIONS_BY_IDS_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - ListRegressionsByIdsRow.new(id: row[0], order: row[1], note: row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_REGRESSIONS_BY_IDS_SQL, query_params), as: :array) + result.map do |row| + ListRegressionsByIdsRow.new(id: row[0], order: row[1], note: row[2]) end end end @@ -936,41 +849,26 @@ def copy_regression_rows(rows) def list_regression_payloads with_client('list_regression_payloads') do |client| - stmt = client.prepare(LIST_REGRESSION_PAYLOADS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListRegressionPayloadsRow.new(id: row[0], payload: row[1]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_REGRESSION_PAYLOADS_SQL, []), as: :array) + result.map do |row| + ListRegressionPayloadsRow.new(id: row[0], payload: row[1]) end end end def list_regression_rows with_client('list_regression_rows') do |client| - stmt = client.prepare(LIST_REGRESSION_ROWS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListRegressionRowsRow.new(id: row[0], order: row[1], note: row[2], payload: row[3]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_REGRESSION_ROWS_SQL, []), as: :array) + result.map do |row| + ListRegressionRowsRow.new(id: row[0], order: row[1], note: row[2], payload: row[3]) end end end def truncate_regressions with_client('truncate_regressions') do |client| - stmt = client.prepare(TRUNCATE_REGRESSIONS_SQL) - begin - stmt.execute - nil - ensure - stmt.close - end + client.query(bind_params(client, TRUNCATE_REGRESSIONS_SQL, [])) + nil end end @@ -1023,12 +921,7 @@ def create_copy_types_batch(rows) with_client('create_copy_types_batch') do |client| rows.each do |args| query_params = [args.id, args.code, args.flags, args.amount] - stmt = client.prepare(CREATE_COPY_TYPES_BATCH_SQL) - begin - stmt.execute(*query_params) - ensure - stmt.close - end + client.query(bind_params(client, CREATE_COPY_TYPES_BATCH_SQL, query_params)) end nil end @@ -1038,50 +931,111 @@ def count_copy_types_by_codes(rows) with_client('count_copy_types_by_codes') do |client| rows.map do |args| query_params = [args.code, args.code_2] - stmt = client.prepare(COUNT_COPY_TYPES_BY_CODES_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && CountCopyTypesByCodesRow.new(matches: row[0]) - ensure - stmt.close - end + result = client.query(bind_params(client, COUNT_COPY_TYPES_BY_CODES_SQL, query_params), as: :array) + row = result.first + row && CountCopyTypesByCodesRow.new(matches: row[0]) end end end def list_copy_types with_client('list_copy_types') do |client| - stmt = client.prepare(LIST_COPY_TYPES_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListCopyTypesRow.new( - id: row[0], - code: row[1], - doc: row[2], - flags: row[3], - amount: row[4] - ) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_COPY_TYPES_SQL, []), as: :array) + result.map do |row| + ListCopyTypesRow.new( + id: row[0], + code: row[1], + doc: row[2], + flags: row[3], + amount: row[4] + ) end end end def truncate_copy_types with_client('truncate_copy_types') do |client| - stmt = client.prepare(TRUNCATE_COPY_TYPES_SQL) - begin - stmt.execute - nil - ensure - stmt.close + client.query(bind_params(client, TRUNCATE_COPY_TYPES_SQL, [])) + nil + end + end + + def find_authors_by_exact_name(name:) + with_client('find_authors_by_exact_name') do |client| + query_params = [name] + result = client.query(bind_params(client, FIND_AUTHORS_BY_EXACT_NAME_SQL, query_params), as: :array) + result.map do |row| + FindAuthorsByExactNameRow.new(id: row[0]) + end + end + end + + # ? + def get_author_with_question_marks(name:) + with_client('get_author_with_question_marks') do |client| + query_params = [name] + result = client.query(bind_params(client, GET_AUTHOR_WITH_QUESTION_MARKS_SQL, query_params), as: :array) + result.map do |row| + GetAuthorWithQuestionMarksRow.new(id: row[0]) end end end + def insert_mysql_binding_values(id:, text_value: nil, blob_value: nil, signed_value: nil, unsigned_value: nil, decimal_value: nil, float_value: nil, date_value: nil, datetime_value: nil, datetime_2: nil, bool_value: nil, nullable_value: nil) + with_client('insert_mysql_binding_values') do |client| + query_params = [ + id, + text_value, + blob_value, + signed_value, + unsigned_value, + decimal_value, + float_value, + date_value, + datetime_value, + datetime_2, + bool_value, + nullable_value + ] + client.query(bind_params(client, INSERT_MYSQL_BINDING_VALUES_SQL, query_params)) + nil + end + end + + def get_mysql_binding_values(id:) + with_client('get_mysql_binding_values') do |client| + query_params = [id] + result = client.query(bind_params(client, GET_MYSQL_BINDING_VALUES_SQL, query_params), as: :array) + row = result.first + row && GetMysqlBindingValuesRow.new( + id: row[0], + text_value: row[1], + blob_value: row[2], + signed_value: row[3], + unsigned_value: row[4], + decimal_value: row[5], + float_value: row[6], + date_value: row[7], + datetime_value: row[8], + datetime_2: row[9], + bool_value: row[10], + nullable_value: row[11] + ) + end + end + + # Like #get_mysql_binding_values, but raises NotFound when there is no row. + def get_mysql_binding_values!(...) + get_mysql_binding_values(...) || raise(NotFound, 'GetMysqlBindingValues returned no row') + end + + def truncate_mysql_binding_values + with_client('truncate_mysql_binding_values') do |client| + client.query(bind_params(client, TRUNCATE_MYSQL_BINDING_VALUES_SQL, [])) + nil + end + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ @@ -1091,14 +1045,9 @@ def many_params(c0:, c1:, c2:, c3:, c4:) c3, c4 ] - stmt = client.prepare(MANY_PARAMS_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && ManyParamsRow.new(c0: row[0], c1: row[1]) - ensure - stmt.close - end + result = client.query(bind_params(client, MANY_PARAMS_SQL, query_params), as: :array) + row = result.first + row && ManyParamsRow.new(c0: row[0], c1: row[1]) end end @@ -1110,34 +1059,24 @@ def many_params!(...) def many_rows(c0:) with_client('many_rows') do |client| query_params = [c0] - stmt = client.prepare(MANY_ROWS_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - ManyRowsRow.new( - c0: row[0], - c1: row[1], - c2: row[2], - c3: row[3], - c4: row[4] - ) - end - ensure - stmt.close + result = client.query(bind_params(client, MANY_ROWS_SQL, query_params), as: :array) + result.map do |row| + ManyRowsRow.new( + c0: row[0], + c1: row[1], + c2: row[2], + c3: row[3], + c4: row[4] + ) end end end def four_cols with_client('four_cols') do |client| - stmt = client.prepare(FOUR_COLS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - FourColsRow.new(c0: row[0], c1: row[1], c2: row[2], c3: row[3]) - end - ensure - stmt.close + result = client.query(bind_params(client, FOUR_COLS_SQL, []), as: :array) + result.map do |row| + FourColsRow.new(c0: row[0], c1: row[1], c2: row[2], c3: row[3]) end end end @@ -1176,6 +1115,89 @@ def timeout_exits_with_throw? (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) end + def quote(client, value) + case value + when NilClass then 'NULL' + when TrueClass then 'TRUE' + when FalseClass then 'FALSE' + when Integer then value.to_s + when Float + raise ArgumentError, 'cannot bind a non-finite Float to MySQL' unless value.finite? + + value.to_s + when BigDecimal + raise ArgumentError, 'cannot bind a non-finite BigDecimal to MySQL' unless value.finite? + + value.to_s('F') + when DateTime, Time + "'#{value.strftime('%Y-%m-%d %H:%M:%S.%6N')}'" + when Date + "'#{value.strftime('%Y-%m-%d')}'" + when String + if value.encoding == Encoding::BINARY || !value.valid_encoding? + "X'#{value.unpack1('H*')}'" + else + "'#{client.escape(value)}'" + end + else + raise TypeError, "cannot bind #{value.class} to MySQL" + end + end + + def bind_params(client, sql, params) + output = +'' + index = 0 + parameter = 0 + while index < sql.length + char = sql[index] + if ["'", '"', '`'].include?(char) + quote_char = char + start = index + index += 1 + while index < sql.length + if sql[index] == '\\' + index += 2 + elsif sql[index] == quote_char + if sql[index + 1] == quote_char + index += 2 + else + index += 1 + break + end + else + index += 1 + end + end + output << sql[start...index] + elsif sql[index, 2] == '--' && (sql[index + 2].nil? || sql[index + 2].match?(/\s/)) + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif char == '#' + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif sql[index, 2] == '/*' + finish = sql.index('*/', index + 2) + finish = finish ? finish + 2 : sql.length + output << sql[index...finish] + index = finish + elsif char == '?' + raise ArgumentError, 'more MySQL placeholders than parameters' if parameter >= params.length + + output << quote(client, params[parameter]) + parameter += 1 + index += 1 + else + output << char + index += 1 + end + end + raise ArgumentError, 'fewer MySQL placeholders than parameters' unless parameter == params.length + + output + end + # Formats one row for LOAD DATA ... FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\'. def csv_line(values) fields = values.map do |value| diff --git a/test/golden/mysql2/ruby31_gemfile/query_sql.rbs b/test/golden/mysql2/ruby31_gemfile/query_sql.rbs index 35e5757..f60e8dc 100644 --- a/test/golden/mysql2/ruby31_gemfile/query_sql.rbs +++ b/test/golden/mysql2/ruby31_gemfile/query_sql.rbs @@ -450,6 +450,71 @@ module Mysql2Codegen TRUNCATE_COPY_TYPES_SQL: String + FIND_AUTHORS_BY_EXACT_NAME_SQL: String + + class FindAuthorsByExactNameRow < ::Struct[untyped] + attr_accessor id: Integer + def initialize: ( id: Integer) -> void + def self.new: ( id: Integer) -> FindAuthorsByExactNameRow + end + + GET_AUTHOR_WITH_QUESTION_MARKS_SQL: String + + class GetAuthorWithQuestionMarksRow < ::Struct[untyped] + attr_accessor id: Integer + def initialize: ( id: Integer) -> void + def self.new: ( id: Integer) -> GetAuthorWithQuestionMarksRow + end + + INSERT_MYSQL_BINDING_VALUES_SQL: String + + GET_MYSQL_BINDING_VALUES_SQL: String + + class GetMysqlBindingValuesRow < ::Struct[untyped] + attr_accessor id: Integer + attr_accessor text_value: String? + attr_accessor blob_value: String? + attr_accessor signed_value: Integer? + attr_accessor unsigned_value: Integer? + attr_accessor decimal_value: BigDecimal? + attr_accessor float_value: Float? + attr_accessor date_value: Date? + attr_accessor datetime_value: Time? + attr_accessor datetime_2: Time? + attr_accessor bool_value: Integer? + attr_accessor nullable_value: String? + def initialize: ( + id: Integer, + text_value: String?, + blob_value: String?, + signed_value: Integer?, + unsigned_value: Integer?, + decimal_value: BigDecimal?, + float_value: Float?, + date_value: Date?, + datetime_value: Time?, + datetime_2: Time?, + bool_value: Integer?, + nullable_value: String? + ) -> void + def self.new: ( + id: Integer, + text_value: String?, + blob_value: String?, + signed_value: Integer?, + unsigned_value: Integer?, + decimal_value: BigDecimal?, + float_value: Float?, + date_value: Date?, + datetime_value: Time?, + datetime_2: Time?, + bool_value: Integer?, + nullable_value: String? + ) -> GetMysqlBindingValuesRow + end + + TRUNCATE_MYSQL_BINDING_VALUES_SQL: String + MANY_PARAMS_SQL: String class ManyParamsRow < ::Struct[untyped] @@ -601,6 +666,18 @@ module Mysql2Codegen def truncate_copy_types: -> void + def find_authors_by_exact_name: (name: String) -> Array[FindAuthorsByExactNameRow] + + def get_author_with_question_marks: (name: String) -> Array[GetAuthorWithQuestionMarksRow] + + def insert_mysql_binding_values: (id: Integer, ?text_value: String?, ?blob_value: String?, ?signed_value: Integer?, ?unsigned_value: Integer?, ?decimal_value: BigDecimal?, ?float_value: Float?, ?date_value: Date?, ?datetime_value: Time?, ?datetime_2: Time?, ?bool_value: Integer?, ?nullable_value: String?) -> void + + def get_mysql_binding_values: (id: Integer) -> GetMysqlBindingValuesRow? + + def get_mysql_binding_values!: (id: Integer) -> GetMysqlBindingValuesRow + + def truncate_mysql_binding_values: -> 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 @@ -617,6 +694,10 @@ module Mysql2Codegen def timeout_exits_with_throw?: () -> bool + def quote: (untyped client, untyped value) -> String + + def bind_params: (untyped client, String sql, Array[untyped] params) -> String + def csv_line: (Array[untyped] values) -> String def load_data: (untyped client, String sql, ?strict: bool) -> void diff --git a/test/golden/mysql2/ruby32_notypes/models.rb b/test/golden/mysql2/ruby32_notypes/models.rb index 07b421a..f1181dd 100644 --- a/test/golden/mysql2/ruby32_notypes/models.rb +++ b/test/golden/mysql2/ruby32_notypes/models.rb @@ -78,4 +78,22 @@ class CopyType < Data.define( :amount ) end + + # rubocop:disable Naming/VariableNumber + class MysqlBindingValu < Data.define( + :id, + :text_value, + :blob_value, + :signed_value, + :unsigned_value, + :decimal_value, + :float_value, + :date_value, + :datetime_value, + :datetime_2, + :bool_value, + :nullable_value + ) + end + # rubocop:enable Naming/VariableNumber end diff --git a/test/golden/mysql2/ruby32_notypes/my_queries_sql.rb b/test/golden/mysql2/ruby32_notypes/my_queries_sql.rb index dbb6bac..13d5741 100644 --- a/test/golden/mysql2/ruby32_notypes/my_queries_sql.rb +++ b/test/golden/mysql2/ruby32_notypes/my_queries_sql.rb @@ -3,6 +3,8 @@ # auto-generated by sqlc - do not edit require 'connection_pool' require 'mysql2' +require 'bigdecimal' +require 'date' require_relative 'connection' require_relative 'models' @@ -72,13 +74,8 @@ def transaction(isolation: nil) def with_numbers2_go(c0:) with_client('with_numbers2_go') do |client| query_params = [c0] - stmt = client.prepare(WITH_NUMBERS2_GO_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, WITH_NUMBERS2_GO_SQL, query_params)) + nil end end @@ -115,5 +112,88 @@ def timeout_exits_with_throw? !!(defined?(::Timeout::VERSION) && (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) end + + def quote(client, value) + case value + when NilClass then 'NULL' + when TrueClass then 'TRUE' + when FalseClass then 'FALSE' + when Integer then value.to_s + when Float + raise ArgumentError, 'cannot bind a non-finite Float to MySQL' unless value.finite? + + value.to_s + when BigDecimal + raise ArgumentError, 'cannot bind a non-finite BigDecimal to MySQL' unless value.finite? + + value.to_s('F') + when DateTime, Time + "'#{value.strftime('%Y-%m-%d %H:%M:%S.%6N')}'" + when Date + "'#{value.strftime('%Y-%m-%d')}'" + when String + if value.encoding == Encoding::BINARY || !value.valid_encoding? + "X'#{value.unpack1('H*')}'" + else + "'#{client.escape(value)}'" + end + else + raise TypeError, "cannot bind #{value.class} to MySQL" + end + end + + def bind_params(client, sql, params) + output = +'' + index = 0 + parameter = 0 + while index < sql.length + char = sql[index] + if ["'", '"', '`'].include?(char) + quote_char = char + start = index + index += 1 + while index < sql.length + if sql[index] == '\\' + index += 2 + elsif sql[index] == quote_char + if sql[index + 1] == quote_char + index += 2 + else + index += 1 + break + end + else + index += 1 + end + end + output << sql[start...index] + elsif sql[index, 2] == '--' && (sql[index + 2].nil? || sql[index + 2].match?(/\s/)) + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif char == '#' + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif sql[index, 2] == '/*' + finish = sql.index('*/', index + 2) + finish = finish ? finish + 2 : sql.length + output << sql[index...finish] + index = finish + elsif char == '?' + raise ArgumentError, 'more MySQL placeholders than parameters' if parameter >= params.length + + output << quote(client, params[parameter]) + parameter += 1 + index += 1 + else + output << char + index += 1 + end + end + raise ArgumentError, 'fewer MySQL placeholders than parameters' unless parameter == params.length + + output + end end end diff --git a/test/golden/mysql2/ruby32_notypes/other_sql.rb b/test/golden/mysql2/ruby32_notypes/other_sql.rb index 30684c1..66c30fd 100644 --- a/test/golden/mysql2/ruby32_notypes/other_sql.rb +++ b/test/golden/mysql2/ruby32_notypes/other_sql.rb @@ -3,6 +3,8 @@ # auto-generated by sqlc - do not edit require 'connection_pool' require 'mysql2' +require 'bigdecimal' +require 'date' require_relative 'connection' require_relative 'models' @@ -91,26 +93,16 @@ def no_cols(c0:, c1:, c2:, c3:, c4:) c3, c4 ] - stmt = client.prepare(NO_COLS_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, NO_COLS_SQL, query_params)) + nil end end def other_one with_client('other_one') do |client| - stmt = client.prepare(OTHER_ONE_SQL) - begin - result = stmt.execute(as: :array) - row = result.first - row && OtherOneRow.new(row[0]) - ensure - stmt.close - end + result = client.query(bind_params(client, OTHER_ONE_SQL, []), as: :array) + row = result.first + row && OtherOneRow.new(row[0]) end end @@ -122,13 +114,8 @@ def other_one!(...) def count_rows(c0:) with_client('count_rows') do |client| query_params = [c0] - stmt = client.prepare(COUNT_ROWS_SQL) - begin - stmt.execute(*query_params) - stmt.affected_rows - ensure - stmt.close - end + client.query(bind_params(client, COUNT_ROWS_SQL, query_params)) + client.affected_rows end end @@ -165,5 +152,88 @@ def timeout_exits_with_throw? !!(defined?(::Timeout::VERSION) && (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) end + + def quote(client, value) + case value + when NilClass then 'NULL' + when TrueClass then 'TRUE' + when FalseClass then 'FALSE' + when Integer then value.to_s + when Float + raise ArgumentError, 'cannot bind a non-finite Float to MySQL' unless value.finite? + + value.to_s + when BigDecimal + raise ArgumentError, 'cannot bind a non-finite BigDecimal to MySQL' unless value.finite? + + value.to_s('F') + when DateTime, Time + "'#{value.strftime('%Y-%m-%d %H:%M:%S.%6N')}'" + when Date + "'#{value.strftime('%Y-%m-%d')}'" + when String + if value.encoding == Encoding::BINARY || !value.valid_encoding? + "X'#{value.unpack1('H*')}'" + else + "'#{client.escape(value)}'" + end + else + raise TypeError, "cannot bind #{value.class} to MySQL" + end + end + + def bind_params(client, sql, params) + output = +'' + index = 0 + parameter = 0 + while index < sql.length + char = sql[index] + if ["'", '"', '`'].include?(char) + quote_char = char + start = index + index += 1 + while index < sql.length + if sql[index] == '\\' + index += 2 + elsif sql[index] == quote_char + if sql[index + 1] == quote_char + index += 2 + else + index += 1 + break + end + else + index += 1 + end + end + output << sql[start...index] + elsif sql[index, 2] == '--' && (sql[index + 2].nil? || sql[index + 2].match?(/\s/)) + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif char == '#' + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif sql[index, 2] == '/*' + finish = sql.index('*/', index + 2) + finish = finish ? finish + 2 : sql.length + output << sql[index...finish] + index = finish + elsif char == '?' + raise ArgumentError, 'more MySQL placeholders than parameters' if parameter >= params.length + + output << quote(client, params[parameter]) + parameter += 1 + index += 1 + else + output << char + index += 1 + end + end + raise ArgumentError, 'fewer MySQL placeholders than parameters' unless parameter == params.length + + output + end end end diff --git a/test/golden/mysql2/ruby32_notypes/query_sql.rb b/test/golden/mysql2/ruby32_notypes/query_sql.rb index 1bd62eb..fcb3578 100644 --- a/test/golden/mysql2/ruby32_notypes/query_sql.rb +++ b/test/golden/mysql2/ruby32_notypes/query_sql.rb @@ -3,6 +3,8 @@ # auto-generated by sqlc - do not edit require 'connection_pool' require 'mysql2' +require 'bigdecimal' +require 'date' require 'tempfile' require_relative 'connection' require_relative 'models' @@ -324,6 +326,59 @@ class ListCopyTypesRow < Data.define( TRUNCATE TABLE copy_types SQL + FIND_AUTHORS_BY_EXACT_NAME_SQL = <<~'SQL' + SELECT id FROM authors WHERE name = ? + SQL + + class FindAuthorsByExactNameRow < Data.define(:id) + end + + GET_AUTHOR_WITH_QUESTION_MARKS_SQL = <<~'SQL' + SELECT id FROM authors AS `?` + WHERE name = ? AND '?' = '?' AND "?" = "?" /* ? */ + ORDER BY id + SQL + + class GetAuthorWithQuestionMarksRow < Data.define(:id) + end + + INSERT_MYSQL_BINDING_VALUES_SQL = <<~'SQL' + INSERT INTO mysql_binding_values ( + id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, + datetime_value, datetime_2, bool_value, nullable_value + ) VALUES ( + ?, ?, ?, ?, ?, + ?, ?, ?, ?, + ?, ?, ? + ) + SQL + + GET_MYSQL_BINDING_VALUES_SQL = <<~'SQL' + SELECT id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, datetime_value, datetime_2, bool_value, nullable_value FROM mysql_binding_values WHERE id = ? + SQL + + # rubocop:disable Naming/VariableNumber + class GetMysqlBindingValuesRow < Data.define( + :id, + :text_value, + :blob_value, + :signed_value, + :unsigned_value, + :decimal_value, + :float_value, + :date_value, + :datetime_value, + :datetime_2, + :bool_value, + :nullable_value + ) + end + # rubocop:enable Naming/VariableNumber + + TRUNCATE_MYSQL_BINDING_VALUES_SQL = <<~'SQL' + TRUNCATE TABLE mysql_binding_values + SQL + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -418,14 +473,9 @@ class ExecResult < Data.define(:rows_affected, :last_insert_id) def get_author(id:) with_client('get_author') do |client| query_params = [id] - stmt = client.prepare(GET_AUTHOR_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && GetAuthorRow.new(row[0], row[1], row[2]) - ensure - stmt.close - end + result = client.query(bind_params(client, GET_AUTHOR_SQL, query_params), as: :array) + row = result.first + row && GetAuthorRow.new(row[0], row[1], row[2]) end end @@ -436,14 +486,9 @@ def get_author!(...) def list_authors with_client('list_authors') do |client| - stmt = client.prepare(LIST_AUTHORS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListAuthorsRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_AUTHORS_SQL, []), as: :array) + result.map do |row| + ListAuthorsRow.new(row[0], row[1], row[2]) end end end @@ -451,103 +496,78 @@ def list_authors def create_author(name:, bio:) with_client('create_author') do |client| query_params = [name, bio] - stmt = client.prepare(CREATE_AUTHOR_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, CREATE_AUTHOR_SQL, query_params)) + nil end end def update_author(bio:, id:) with_client('update_author') do |client| query_params = [bio, id] - stmt = client.prepare(UPDATE_AUTHOR_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, UPDATE_AUTHOR_SQL, query_params)) + nil end end def create_author_return_id(name:, bio:) with_client('create_author_return_id') do |client| query_params = [name, bio] - stmt = client.prepare(CREATE_AUTHOR_RETURN_ID_SQL) - begin - stmt.execute(*query_params) - client.last_id - ensure - stmt.close - end + client.query(bind_params(client, CREATE_AUTHOR_RETURN_ID_SQL, query_params)) + client.last_id end end def delete_author(id:) with_client('delete_author') do |client| query_params = [id] - stmt = client.prepare(DELETE_AUTHOR_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, DELETE_AUTHOR_SQL, query_params)) + nil end end def test with_client('test') do |client| - stmt = client.prepare(TEST_SQL) - begin - result = stmt.execute(as: :array) - row = result.first - row && TestRow.new( - row[0], - row[1], - row[2], - row[3], - row[4], - row[5], - row[6], - row[7], - row[8], - row[9], - row[10], - row[11], - row[12], - row[13], - row[14], - row[15], - row[16], - row[17], - row[18], - row[19], - row[20], - row[21], - row[22], - row[23], - row[24], - row[25], - row[26], - row[27], - row[28], - row[29], - row[30], - row[31], - row[32], - row[33], - row[34], - row[35], - row[36] - ) - ensure - stmt.close - end + result = client.query(bind_params(client, TEST_SQL, []), as: :array) + row = result.first + row && TestRow.new( + row[0], + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7], + row[8], + row[9], + row[10], + row[11], + row[12], + row[13], + row[14], + row[15], + row[16], + row[17], + row[18], + row[19], + row[20], + row[21], + row[22], + row[23], + row[24], + row[25], + row[26], + row[27], + row[28], + row[29], + row[30], + row[31], + row[32], + row[33], + row[34], + row[35], + row[36] + ) end end @@ -559,14 +579,9 @@ def test!(...) def list_authors_paged(limit:, offset:) with_client('list_authors_paged') do |client| query_params = [limit, offset] - stmt = client.prepare(LIST_AUTHORS_PAGED_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - ListAuthorsPagedRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_AUTHORS_PAGED_SQL, query_params), as: :array) + result.map do |row| + ListAuthorsPagedRow.new(row[0], row[1], row[2]) end end end @@ -574,14 +589,9 @@ def list_authors_paged(limit:, offset:) def get_author_by_name_pattern(name_pattern: nil) with_client('get_author_by_name_pattern') do |client| query_params = [name_pattern] - stmt = client.prepare(GET_AUTHOR_BY_NAME_PATTERN_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorByNamePatternRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, GET_AUTHOR_BY_NAME_PATTERN_SQL, query_params), as: :array) + result.map do |row| + GetAuthorByNamePatternRow.new(row[0], row[1], row[2]) end end end @@ -589,14 +599,9 @@ def get_author_by_name_pattern(name_pattern: nil) def get_authors_by_optional_name(limit:, name_filter: nil) with_client('get_authors_by_optional_name') do |client| query_params = [name_filter, name_filter, limit] - stmt = client.prepare(GET_AUTHORS_BY_OPTIONAL_NAME_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorsByOptionalNameRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, GET_AUTHORS_BY_OPTIONAL_NAME_SQL, query_params), as: :array) + result.map do |row| + GetAuthorsByOptionalNameRow.new(row[0], row[1], row[2]) end end end @@ -604,13 +609,8 @@ def get_authors_by_optional_name(limit:, name_filter: nil) def pause_query(seconds:) with_client('pause_query') do |client| query_params = [seconds] - stmt = client.prepare(PAUSE_QUERY_SQL) - begin - stmt.execute(*query_params, as: :array) - nil - ensure - stmt.close - end + client.query(bind_params(client, PAUSE_QUERY_SQL, query_params), as: :array) + nil end end @@ -618,13 +618,8 @@ def pause_query(seconds:) def update_author_bios(bio: nil) with_client('update_author_bios') do |client| query_params = [bio] - stmt = client.prepare(UPDATE_AUTHOR_BIOS_SQL) - begin - stmt.execute(*query_params) - stmt.affected_rows - ensure - stmt.close - end + client.query(bind_params(client, UPDATE_AUTHOR_BIOS_SQL, query_params)) + client.affected_rows end end @@ -632,14 +627,9 @@ def get_authors_by_ids(ids:) with_client('get_authors_by_ids') do |client| query_params = [*ids] sql = GET_AUTHORS_BY_IDS_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) - stmt = client.prepare(sql) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorsByIdsRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, sql, query_params), as: :array) + result.map do |row| + GetAuthorsByIdsRow.new(row[0], row[1], row[2]) end end end @@ -649,14 +639,9 @@ def get_authors_by_ids_and_names(ids:, names:) query_params = [*ids, *names] sql = GET_AUTHORS_BY_IDS_AND_NAMES_SQL.gsub('/*SLICE:ids*/?', slice_placeholders(ids)) sql = sql.gsub('/*SLICE:names*/?', slice_placeholders(names)) - stmt = client.prepare(sql) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorsByIdsAndNamesRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, sql, query_params), as: :array) + result.map do |row| + GetAuthorsByIdsAndNamesRow.new(row[0], row[1], row[2]) end end end @@ -679,56 +664,36 @@ def copy_authors(rows) def truncate_authors with_client('truncate_authors') do |client| - stmt = client.prepare(TRUNCATE_AUTHORS_SQL) - begin - stmt.execute - nil - ensure - stmt.close - end + client.query(bind_params(client, TRUNCATE_AUTHORS_SQL, [])) + nil end end def create_book(name:, author_id:) with_client('create_book') do |client| query_params = [name, author_id] - stmt = client.prepare(CREATE_BOOK_SQL) - begin - stmt.execute(*query_params) - client.last_id - ensure - stmt.close - end + client.query(bind_params(client, CREATE_BOOK_SQL, query_params)) + client.last_id end end def list_all_authors_books with_client('list_all_authors_books') do |client| - stmt = client.prepare(LIST_ALL_AUTHORS_BOOKS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListAllAuthorsBooksRow.new( - Author.new(row[0], row[1], row[2]), - Book.new(row[3], row[4], row[5], row[6]) - ) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_ALL_AUTHORS_BOOKS_SQL, []), as: :array) + result.map do |row| + ListAllAuthorsBooksRow.new( + Author.new(row[0], row[1], row[2]), + Book.new(row[3], row[4], row[5], row[6]) + ) end end end def get_duplicate_authors with_client('get_duplicate_authors') do |client| - stmt = client.prepare(GET_DUPLICATE_AUTHORS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - GetDuplicateAuthorsRow.new(Author.new(row[0], row[1], row[2]), Author.new(row[3], row[4], row[5])) - end - ensure - stmt.close + result = client.query(bind_params(client, GET_DUPLICATE_AUTHORS_SQL, []), as: :array) + result.map do |row| + GetDuplicateAuthorsRow.new(Author.new(row[0], row[1], row[2]), Author.new(row[3], row[4], row[5])) end end end @@ -736,14 +701,9 @@ def get_duplicate_authors def get_authors_by_book_name(name:) with_client('get_authors_by_book_name') do |client| query_params = [name] - stmt = client.prepare(GET_AUTHORS_BY_BOOK_NAME_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - GetAuthorsByBookNameRow.new(row[0], row[1], row[2], Book.new(row[3], row[4], row[5], row[6])) - end - ensure - stmt.close + result = client.query(bind_params(client, GET_AUTHORS_BY_BOOK_NAME_SQL, query_params), as: :array) + result.map do |row| + GetAuthorsByBookNameRow.new(row[0], row[1], row[2], Book.new(row[3], row[4], row[5], row[6])) end end end @@ -751,27 +711,17 @@ def get_authors_by_book_name(name:) def create_extended_bio(author_name:, name:, bio_type:, author_type:) with_client('create_extended_bio') do |client| query_params = [author_name, name, bio_type, author_type] - stmt = client.prepare(CREATE_EXTENDED_BIO_SQL) - begin - stmt.execute(*query_params) - nil - ensure - stmt.close - end + client.query(bind_params(client, CREATE_EXTENDED_BIO_SQL, query_params)) + nil end end def get_first_extended_bio_by_type(bio_type:) with_client('get_first_extended_bio_by_type') do |client| query_params = [bio_type] - stmt = client.prepare(GET_FIRST_EXTENDED_BIO_BY_TYPE_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && GetFirstExtendedBioByTypeRow.new(row[0], row[1], row[2], row[3]) - ensure - stmt.close - end + result = client.query(bind_params(client, GET_FIRST_EXTENDED_BIO_BY_TYPE_SQL, query_params), as: :array) + row = result.first + row && GetFirstExtendedBioByTypeRow.new(row[0], row[1], row[2], row[3]) end end @@ -782,26 +732,16 @@ def get_first_extended_bio_by_type!(...) def truncate_extended_bios with_client('truncate_extended_bios') do |client| - stmt = client.prepare(TRUNCATE_EXTENDED_BIOS_SQL) - begin - stmt.execute - nil - ensure - stmt.close - end + client.query(bind_params(client, TRUNCATE_EXTENDED_BIOS_SQL, [])) + nil end end def create_author_result(name:, bio:) with_client('create_author_result') do |client| query_params = [name, bio] - stmt = client.prepare(CREATE_AUTHOR_RESULT_SQL) - begin - stmt.execute(*query_params) - ExecResult.new(stmt.affected_rows, client.last_id) - ensure - stmt.close - end + client.query(bind_params(client, CREATE_AUTHOR_RESULT_SQL, query_params)) + ExecResult.new(client.affected_rows, client.last_id) end end @@ -809,12 +749,7 @@ def create_authors_batch(rows) with_client('create_authors_batch') do |client| rows.each do |args| query_params = [args.name, args.bio] - stmt = client.prepare(CREATE_AUTHORS_BATCH_SQL) - begin - stmt.execute(*query_params) - ensure - stmt.close - end + client.query(bind_params(client, CREATE_AUTHORS_BATCH_SQL, query_params)) end nil end @@ -824,14 +759,9 @@ def get_author_batch(rows) with_client('get_author_batch') do |client| rows.map do |args| query_params = [args.id] - stmt = client.prepare(GET_AUTHOR_BATCH_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && GetAuthorBatchRow.new(row[0], row[1], row[2]) - ensure - stmt.close - end + result = client.query(bind_params(client, GET_AUTHOR_BATCH_SQL, query_params), as: :array) + row = result.first + row && GetAuthorBatchRow.new(row[0], row[1], row[2]) end end end @@ -840,14 +770,9 @@ def list_authors_by_name_batch(rows) with_client('list_authors_by_name_batch') do |client| rows.map do |args| query_params = [args.name] - stmt = client.prepare(LIST_AUTHORS_BY_NAME_BATCH_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - ListAuthorsByNameBatchRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_AUTHORS_BY_NAME_BATCH_SQL, query_params), as: :array) + result.map do |row| + ListAuthorsByNameBatchRow.new(row[0], row[1], row[2]) end end end @@ -856,14 +781,9 @@ def list_authors_by_name_batch(rows) def count_regressions_by_text(term:) with_client('count_regressions_by_text') do |client| query_params = [term, term] - stmt = client.prepare(COUNT_REGRESSIONS_BY_TEXT_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && CountRegressionsByTextRow.new(row[0]) - ensure - stmt.close - end + result = client.query(bind_params(client, COUNT_REGRESSIONS_BY_TEXT_SQL, query_params), as: :array) + row = result.first + row && CountRegressionsByTextRow.new(row[0]) end end @@ -875,14 +795,9 @@ def count_regressions_by_text!(...) def list_regressions_by_ids(id:, id_2:, id_3:) with_client('list_regressions_by_ids') do |client| query_params = [id, id_2, id_3] - stmt = client.prepare(LIST_REGRESSIONS_BY_IDS_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - ListRegressionsByIdsRow.new(row[0], row[1], row[2]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_REGRESSIONS_BY_IDS_SQL, query_params), as: :array) + result.map do |row| + ListRegressionsByIdsRow.new(row[0], row[1], row[2]) end end end @@ -919,41 +834,26 @@ def copy_regression_rows(rows) def list_regression_payloads with_client('list_regression_payloads') do |client| - stmt = client.prepare(LIST_REGRESSION_PAYLOADS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListRegressionPayloadsRow.new(row[0], row[1]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_REGRESSION_PAYLOADS_SQL, []), as: :array) + result.map do |row| + ListRegressionPayloadsRow.new(row[0], row[1]) end end end def list_regression_rows with_client('list_regression_rows') do |client| - stmt = client.prepare(LIST_REGRESSION_ROWS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListRegressionRowsRow.new(row[0], row[1], row[2], row[3]) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_REGRESSION_ROWS_SQL, []), as: :array) + result.map do |row| + ListRegressionRowsRow.new(row[0], row[1], row[2], row[3]) end end end def truncate_regressions with_client('truncate_regressions') do |client| - stmt = client.prepare(TRUNCATE_REGRESSIONS_SQL) - begin - stmt.execute - nil - ensure - stmt.close - end + client.query(bind_params(client, TRUNCATE_REGRESSIONS_SQL, [])) + nil end end @@ -1006,12 +906,7 @@ def create_copy_types_batch(rows) with_client('create_copy_types_batch') do |client| rows.each do |args| query_params = [args.id, args.code, args.flags, args.amount] - stmt = client.prepare(CREATE_COPY_TYPES_BATCH_SQL) - begin - stmt.execute(*query_params) - ensure - stmt.close - end + client.query(bind_params(client, CREATE_COPY_TYPES_BATCH_SQL, query_params)) end nil end @@ -1021,50 +916,111 @@ def count_copy_types_by_codes(rows) with_client('count_copy_types_by_codes') do |client| rows.map do |args| query_params = [args.code, args.code_2] - stmt = client.prepare(COUNT_COPY_TYPES_BY_CODES_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && CountCopyTypesByCodesRow.new(row[0]) - ensure - stmt.close - end + result = client.query(bind_params(client, COUNT_COPY_TYPES_BY_CODES_SQL, query_params), as: :array) + row = result.first + row && CountCopyTypesByCodesRow.new(row[0]) end end end def list_copy_types with_client('list_copy_types') do |client| - stmt = client.prepare(LIST_COPY_TYPES_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - ListCopyTypesRow.new( - row[0], - row[1], - row[2], - row[3], - row[4] - ) - end - ensure - stmt.close + result = client.query(bind_params(client, LIST_COPY_TYPES_SQL, []), as: :array) + result.map do |row| + ListCopyTypesRow.new( + row[0], + row[1], + row[2], + row[3], + row[4] + ) end end end def truncate_copy_types with_client('truncate_copy_types') do |client| - stmt = client.prepare(TRUNCATE_COPY_TYPES_SQL) - begin - stmt.execute - nil - ensure - stmt.close + client.query(bind_params(client, TRUNCATE_COPY_TYPES_SQL, [])) + nil + end + end + + def find_authors_by_exact_name(name:) + with_client('find_authors_by_exact_name') do |client| + query_params = [name] + result = client.query(bind_params(client, FIND_AUTHORS_BY_EXACT_NAME_SQL, query_params), as: :array) + result.map do |row| + FindAuthorsByExactNameRow.new(row[0]) + end + end + end + + # ? + def get_author_with_question_marks(name:) + with_client('get_author_with_question_marks') do |client| + query_params = [name] + result = client.query(bind_params(client, GET_AUTHOR_WITH_QUESTION_MARKS_SQL, query_params), as: :array) + result.map do |row| + GetAuthorWithQuestionMarksRow.new(row[0]) end end end + def insert_mysql_binding_values(id:, text_value: nil, blob_value: nil, signed_value: nil, unsigned_value: nil, decimal_value: nil, float_value: nil, date_value: nil, datetime_value: nil, datetime_2: nil, bool_value: nil, nullable_value: nil) + with_client('insert_mysql_binding_values') do |client| + query_params = [ + id, + text_value, + blob_value, + signed_value, + unsigned_value, + decimal_value, + float_value, + date_value, + datetime_value, + datetime_2, + bool_value, + nullable_value + ] + client.query(bind_params(client, INSERT_MYSQL_BINDING_VALUES_SQL, query_params)) + nil + end + end + + def get_mysql_binding_values(id:) + with_client('get_mysql_binding_values') do |client| + query_params = [id] + result = client.query(bind_params(client, GET_MYSQL_BINDING_VALUES_SQL, query_params), as: :array) + row = result.first + row && GetMysqlBindingValuesRow.new( + row[0], + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7], + row[8], + row[9], + row[10], + row[11] + ) + end + end + + # Like #get_mysql_binding_values, but raises NotFound when there is no row. + def get_mysql_binding_values!(...) + get_mysql_binding_values(...) || raise(NotFound, 'GetMysqlBindingValues returned no row') + end + + def truncate_mysql_binding_values + with_client('truncate_mysql_binding_values') do |client| + client.query(bind_params(client, TRUNCATE_MYSQL_BINDING_VALUES_SQL, [])) + nil + end + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ @@ -1074,14 +1030,9 @@ def many_params(c0:, c1:, c2:, c3:, c4:) c3, c4 ] - stmt = client.prepare(MANY_PARAMS_SQL) - begin - result = stmt.execute(*query_params, as: :array) - row = result.first - row && ManyParamsRow.new(row[0], row[1]) - ensure - stmt.close - end + result = client.query(bind_params(client, MANY_PARAMS_SQL, query_params), as: :array) + row = result.first + row && ManyParamsRow.new(row[0], row[1]) end end @@ -1093,34 +1044,24 @@ def many_params!(...) def many_rows(c0:) with_client('many_rows') do |client| query_params = [c0] - stmt = client.prepare(MANY_ROWS_SQL) - begin - result = stmt.execute(*query_params, as: :array) - result.map do |row| - ManyRowsRow.new( - row[0], - row[1], - row[2], - row[3], - row[4] - ) - end - ensure - stmt.close + result = client.query(bind_params(client, MANY_ROWS_SQL, query_params), as: :array) + result.map do |row| + ManyRowsRow.new( + row[0], + row[1], + row[2], + row[3], + row[4] + ) end end end def four_cols with_client('four_cols') do |client| - stmt = client.prepare(FOUR_COLS_SQL) - begin - result = stmt.execute(as: :array) - result.map do |row| - FourColsRow.new(row[0], row[1], row[2], row[3]) - end - ensure - stmt.close + result = client.query(bind_params(client, FOUR_COLS_SQL, []), as: :array) + result.map do |row| + FourColsRow.new(row[0], row[1], row[2], row[3]) end end end @@ -1159,6 +1100,89 @@ def timeout_exits_with_throw? (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?) end + def quote(client, value) + case value + when NilClass then 'NULL' + when TrueClass then 'TRUE' + when FalseClass then 'FALSE' + when Integer then value.to_s + when Float + raise ArgumentError, 'cannot bind a non-finite Float to MySQL' unless value.finite? + + value.to_s + when BigDecimal + raise ArgumentError, 'cannot bind a non-finite BigDecimal to MySQL' unless value.finite? + + value.to_s('F') + when DateTime, Time + "'#{value.strftime('%Y-%m-%d %H:%M:%S.%6N')}'" + when Date + "'#{value.strftime('%Y-%m-%d')}'" + when String + if value.encoding == Encoding::BINARY || !value.valid_encoding? + "X'#{value.unpack1('H*')}'" + else + "'#{client.escape(value)}'" + end + else + raise TypeError, "cannot bind #{value.class} to MySQL" + end + end + + def bind_params(client, sql, params) + output = +'' + index = 0 + parameter = 0 + while index < sql.length + char = sql[index] + if ["'", '"', '`'].include?(char) + quote_char = char + start = index + index += 1 + while index < sql.length + if sql[index] == '\\' + index += 2 + elsif sql[index] == quote_char + if sql[index + 1] == quote_char + index += 2 + else + index += 1 + break + end + else + index += 1 + end + end + output << sql[start...index] + elsif sql[index, 2] == '--' && (sql[index + 2].nil? || sql[index + 2].match?(/\s/)) + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif char == '#' + finish = sql.index("\n", index) || sql.length + output << sql[index...finish] + index = finish + elsif sql[index, 2] == '/*' + finish = sql.index('*/', index + 2) + finish = finish ? finish + 2 : sql.length + output << sql[index...finish] + index = finish + elsif char == '?' + raise ArgumentError, 'more MySQL placeholders than parameters' if parameter >= params.length + + output << quote(client, params[parameter]) + parameter += 1 + index += 1 + else + output << char + index += 1 + end + end + raise ArgumentError, 'fewer MySQL placeholders than parameters' unless parameter == params.length + + output + end + # Formats one row for LOAD DATA ... FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\'. def csv_line(values) fields = values.map do |value| diff --git a/test/golden/pg/default/query_sql.rbs b/test/golden/pg/default/query_sql.rbs index 3f045f5..e550e96 100644 --- a/test/golden/pg/default/query_sql.rbs +++ b/test/golden/pg/default/query_sql.rbs @@ -551,7 +551,7 @@ module PgCodegen def get_authors_by_optional_name: (lim: Integer, ?name_filter: String?) -> Array[GetAuthorsByOptionalNameRow] - def create_author_return_id: (name: String, bio: String?) -> Integer + def create_author_return_id: (name: String, bio: String?) -> Integer? def update_author_bios: (bio: String?) -> Integer @@ -563,7 +563,7 @@ module PgCodegen def truncate_authors: -> void - def create_book: (name: String, author_id: Integer) -> String + def create_book: (name: String, author_id: Integer) -> String? def create_deferred_book: (author_id: Integer) -> void @@ -623,7 +623,7 @@ module PgCodegen def create_regression!: (id: Integer, order: String, payload: String?, doc: untyped, bio_types: Array[String]?, uids: Array[String]?) -> CreateRegressionRow - def create_regression_if_new: (id: Integer, order: String) -> Integer + def create_regression_if_new: (id: Integer, order: String) -> Integer? def copy_regressions: (Array[CopyRegressionsArgs] rows) -> void diff --git a/test/golden/pg/ruby31_gemfile/query_sql.rbs b/test/golden/pg/ruby31_gemfile/query_sql.rbs index f8cb392..3e6959e 100644 --- a/test/golden/pg/ruby31_gemfile/query_sql.rbs +++ b/test/golden/pg/ruby31_gemfile/query_sql.rbs @@ -675,7 +675,7 @@ module PgCodegen def get_authors_by_optional_name: (lim: Integer, ?name_filter: String?) -> Array[GetAuthorsByOptionalNameRow] - def create_author_return_id: (name: String, bio: String?) -> Integer + def create_author_return_id: (name: String, bio: String?) -> Integer? def update_author_bios: (bio: String?) -> Integer @@ -687,7 +687,7 @@ module PgCodegen def truncate_authors: -> void - def create_book: (name: String, author_id: Integer) -> String + def create_book: (name: String, author_id: Integer) -> String? def create_deferred_book: (author_id: Integer) -> void @@ -747,7 +747,7 @@ module PgCodegen def create_regression!: (id: Integer, order: String, payload: String?, doc: untyped, bio_types: Array[String]?, uids: Array[String]?) -> CreateRegressionRow - def create_regression_if_new: (id: Integer, order: String) -> Integer + def create_regression_if_new: (id: Integer, order: String) -> Integer? def copy_regressions: (Array[CopyRegressionsArgs] rows) -> void diff --git a/test/golden/sqlite3/default/query_sql.rbs b/test/golden/sqlite3/default/query_sql.rbs index e15166b..9dbfd5b 100644 --- a/test/golden/sqlite3/default/query_sql.rbs +++ b/test/golden/sqlite3/default/query_sql.rbs @@ -378,7 +378,7 @@ module Sqlite3Codegen def get_authors_by_optional_name: (lim: Integer, ?name_filter: untyped) -> Array[GetAuthorsByOptionalNameRow] - def create_author_return_id: (name: String, bio: String?) -> Integer + def create_author_return_id: (name: String, bio: String?) -> Integer? def update_author_bios: (bio: String?) -> Integer @@ -398,7 +398,7 @@ module Sqlite3Codegen def truncate_authors: -> void - def create_book: (name: String, author_id: Integer) -> Integer + def create_book: (name: String, author_id: Integer) -> Integer? def list_all_authors_books: -> Array[ListAllAuthorsBooksRow] diff --git a/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs b/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs index cd033b0..5efee66 100644 --- a/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs +++ b/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs @@ -432,7 +432,7 @@ module Sqlite3Codegen def get_authors_by_optional_name: (lim: Integer, ?name_filter: untyped) -> Array[GetAuthorsByOptionalNameRow] - def create_author_return_id: (name: String, bio: String?) -> Integer + def create_author_return_id: (name: String, bio: String?) -> Integer? def update_author_bios: (bio: String?) -> Integer @@ -452,7 +452,7 @@ module Sqlite3Codegen def truncate_authors: -> void - def create_book: (name: String, author_id: Integer) -> Integer + def create_book: (name: String, author_id: Integer) -> Integer? def list_all_authors_books: -> Array[ListAllAuthorsBooksRow] diff --git a/tests/end2end_mysql2.rb b/tests/end2end_mysql2.rb index e3c0a20..2d1933c 100755 --- a/tests/end2end_mysql2.rb +++ b/tests/end2end_mysql2.rb @@ -4,6 +4,8 @@ require 'logger' require 'stringio' require 'json' +require 'bigdecimal' +require 'date' require 'async' require 'async/barrier' require 'timeout' @@ -11,10 +13,12 @@ require_relative 'consts' require_relative 'rbs_types' require File.join(EXAMPLES_DIR, 'mysql2/query_sql') +RbsTypes.install(Object.const_get("#{ENV.fetch('EXAMPLES_MODULE', 'Mysql2Codegen')}::QuerySql"), + File.join(EXAMPLES_DIR, 'mysql2/query_sql.rbs')) # End-to-end tests of the code generated for examples/authors/mysql, against a real MySQL / MariaDB server. class TestMysql2 < Minitest::Test - include Mysql2Codegen + include Object.const_get(ENV.fetch('EXAMPLES_MODULE', 'Mysql2Codegen')) TEXT_PARTS = ["é", '🙂', "\t", "\n", "\r", '\\', "'", '"', ',', '\N', 'NULL'].freeze @@ -22,6 +26,24 @@ def row_for(klass, values) klass.new(**klass.members.zip(values).to_h) end + def assert_row_layout(row) + if defined?(Data) && row.is_a?(Data) + assert_predicate row, :frozen? + assert_equal row.to_h, row.with.to_h + else + assert_kind_of Struct, row + assert row.class.keyword_init? + refute_predicate row, :frozen? + end + case row + in { id:, name: } + assert_kind_of Integer, id + assert_equal row.name, name + else + flunk 'row did not support keyword pattern matching' + end + end + def row_value(row, member) actual_member = row.class.members.find { |name| name == member.to_sym || name == :"#{member}_" } row.public_send(actual_member) @@ -58,25 +80,27 @@ def mysql2_params }.compact end - def prepared_statement_count - client = Mysql2::Client.new(mysql2_params) - client.query("SHOW GLOBAL STATUS LIKE 'Prepared_stmt_count'").first.fetch('Value').to_i - ensure - client&.close - end - def setup @query_sql = QuerySql.connect(mysql2_params) @query_sql.truncate_authors # cascades to books @query_sql.truncate_extended_bios @query_sql.truncate_regressions @query_sql.truncate_copy_types + @query_sql.truncate_mysql_binding_values end def create_author(name, bio = nil) @query_sql.create_author_return_id(name: name, bio: bio) end + def binding_values(id, float_value = nil) + { + id: id, text_value: nil, blob_value: nil, signed_value: nil, unsigned_value: nil, decimal_value: nil, + float_value: float_value, date_value: nil, datetime_value: nil, datetime_2: nil, bool_value: nil, + nullable_value: nil + } + end + # ---- :one / :many / :exec ------------------------------------------------------------------------------------- def test_flow @@ -95,6 +119,14 @@ def test_flow assert_equal [Consts::DR_SEUSS_AUTHOR], @query_sql.list_authors.map(&:name) end + def test_rows_and_embedded_models_have_supported_layout_and_pattern_matching + first = create_author('pattern') + create_author('pattern') + assert_row_layout(@query_sql.get_author(id: first)) + embedded = @query_sql.get_duplicate_authors.first.author + assert_row_layout(embedded) + end + def test_nullable_columns_round_trip_nil id = create_author('no bio', nil) assert_nil @query_sql.get_author(id: id).bio @@ -104,11 +136,18 @@ def test_one_on_empty_table_returns_nil assert_nil @query_sql.test end - def test_prepared_statements_are_closed - before = prepared_statement_count - id = create_author('a') - 300.times { @query_sql.get_author(id: id) } - assert_operator prepared_statement_count, :<=, before, 'prepared statements leak on the server' + def test_generated_queries_do_not_create_server_side_prepared_statements + client = Mysql2::Client.new(mysql2_params) + database = Object.new + database.define_singleton_method(:with) { |&block| block.call(client) } + queries = QuerySql.new(database) + before = client.query("SHOW SESSION STATUS LIKE 'Com_stmt_prepare'").first.fetch('Value').to_i + id = queries.create_author_return_id(name: 'plain query', bio: nil) + 20.times { queries.get_author(id: id) } + after = client.query("SHOW SESSION STATUS LIKE 'Com_stmt_prepare'").first.fetch('Value').to_i + assert_equal before, after + ensure + client&.close end # ---- :execlastid / :execrows ----------------------------------------------------------------------------------- @@ -362,7 +401,7 @@ def test_queries_can_share_an_injected_pool assert_equal 'shared', second.get_author(id: id).name end - # mysql2 prepared-statement execution blocks the Fiber scheduler; use threads for concurrent queries. + # Both query and transaction paths must use Mysql2::Client#query for Fiber scheduler cooperation. def test_generated_queries_overlap_across_threads queries = QuerySql.connect(mysql2_params, size: 5) started = Process.clock_gettime(Process::CLOCK_MONOTONIC) @@ -381,6 +420,105 @@ def test_generated_queries_overlap_across_threads assert_equal 10, @query_sql.list_authors.size end + def test_generated_queries_overlap_under_sync_including_transactions + queries = QuerySql.connect(mysql2_params, size: 5) + count = 5 + seconds = 0.3 + started = Process.clock_gettime(Process::CLOCK_MONOTONIC) + count.times { queries.pause_query(seconds: seconds) } + serial = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started + started = Process.clock_gettime(Process::CLOCK_MONOTONIC) + Sync do + barrier = Async::Barrier.new + tasks = Array.new(count) { barrier.async { queries.pause_query(seconds: seconds) } } + barrier.wait + tasks.each(&:wait) + end + concurrent = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started + assert_operator concurrent, :<, serial * 0.6, + "#{count} generated queries took #{concurrent}s vs #{serial}s serially" + + started = Process.clock_gettime(Process::CLOCK_MONOTONIC) + count.times { queries.transaction { |tx| tx.pause_query(seconds: seconds) } } + serial = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started + started = Process.clock_gettime(Process::CLOCK_MONOTONIC) + Sync do + barrier = Async::Barrier.new + tasks = Array.new(count) do + barrier.async { queries.transaction { |tx| tx.pause_query(seconds: seconds) } } + end + barrier.wait + tasks.each(&:wait) + end + concurrent = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started + assert_operator concurrent, :<, serial * 0.6, + "#{count} generated transactions took #{concurrent}s vs #{serial}s serially" + end + + def test_sql_placeholders_inside_literals_identifiers_and_comments_are_ignored + id = create_author('quoted ? -- # /* value') + rows = @query_sql.get_author_with_question_marks(name: 'quoted ? -- # /* value') + assert_equal [id], rows.map(&:id) + end + + def test_client_side_quoting_round_trips_supported_values + text = "quotes ' \" slash \\ question ? -- # /* NUL \0 emoji 🙂" + binary = "\x00\xFF\x80\x01".b + decimal = BigDecimal('123456789012345678901234567890.123456789012345678901234567890') + float = 1.0 / 3 + time = Time.new(2024, 2, 3, 4, 5, Rational(6_123_456, 1_000_000), '+05:30') + date_time = DateTime.new(2024, 3, 4, 5, 6, Rational(7_654_321, 1_000_000), '-07:00') + @query_sql.insert_mysql_binding_values( + id: 42, text_value: text, blob_value: binary, signed_value: -9_223_372_036_854_775_808, + unsigned_value: 18_446_744_073_709_551_615, decimal_value: decimal, float_value: float, + date_value: Date.new(2024, 2, 3), datetime_value: time, datetime_2: date_time, + bool_value: true, nullable_value: nil + ) + row = @query_sql.get_mysql_binding_values(id: 42) + assert_equal text, row.text_value + assert_equal binary, row.blob_value + assert_equal(-9_223_372_036_854_775_808, row.signed_value) + assert_equal 18_446_744_073_709_551_615, row.unsigned_value + assert_equal decimal, row.decimal_value + assert_equal float, row.float_value + assert_equal Date.new(2024, 2, 3), row.date_value + assert_equal time.strftime('%Y-%m-%d %H:%M:%S.%6N'), row.datetime_value.strftime('%Y-%m-%d %H:%M:%S.%6N') + assert_equal date_time.strftime('%Y-%m-%d %H:%M:%S.%6N'), row.datetime_2.strftime('%Y-%m-%d %H:%M:%S.%6N') + assert_equal 1, row.bool_value + assert_nil row.nullable_value + + @query_sql.insert_mysql_binding_values(**binding_values(43).merge(bool_value: false)) + assert_equal 0, @query_sql.get_mysql_binding_values(id: 43).bool_value + assert_raises(ArgumentError) do + @query_sql.insert_mysql_binding_values(**binding_values(44, Float::INFINITY)) + end + end + + def test_time_values_match_mysql2_prepared_statement_storage + client = Mysql2::Client.new(mysql2_params) + time = Time.new(2024, 2, 3, 4, 5, Rational(6_123_456, 1_000_000), '+05:30') + date_time = DateTime.new(2024, 3, 4, 5, 6, Rational(7_654_321, 1_000_000), '-07:00') + statement = client.prepare('INSERT INTO mysql_binding_values (id, datetime_value, datetime_2) VALUES (?, ?, ?)') + statement.execute(1, time, date_time) + statement.close + @query_sql.insert_mysql_binding_values(**binding_values(2).merge(datetime_value: time, datetime_2: date_time)) + prepared = @query_sql.get_mysql_binding_values(id: 1) + quoted = @query_sql.get_mysql_binding_values(id: 2) + assert_equal prepared.datetime_value, quoted.datetime_value + assert_equal prepared.datetime_2, quoted.datetime_2 + ensure + client&.close + end + + 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| + assert_raises(TypeError) { @query_sql.find_authors_by_exact_name(name: value) } + end + assert_equal ['ordinary'], @query_sql.list_authors.map(&:name) + end + # ---- Ruby conventions: bang finders, logger ---------------------------------------------------------------- def test_bang_finder_raises_not_found diff --git a/tests/rbs_types.rb b/tests/rbs_types.rb index 0d1b710..2afc583 100644 --- a/tests/rbs_types.rb +++ b/tests/rbs_types.rb @@ -25,6 +25,83 @@ def mismatches(row, rbs_path, class_name) end end + def install(query_class, rbs_path) + return unless File.file?(rbs_path) + + module_name = query_class.name.sub(/::QuerySql\z/, '') + signatures = File.read(rbs_path).scan(/^\s+def ([\w!?]+):.*?-> (.+)$/).to_h + methods = query_class.public_instance_methods(false).filter_map do |name| + type = signatures[name.to_s] + [name, type] if type && type != 'T' && name != :transaction + end + checker = Module.new do + methods.each do |name, type| + define_method(name) do |*args, **kwargs, &block| + value = super(*args, **kwargs, &block) + errors = RbsTypes.return_mismatches(value, type, rbs_path, module_name) + raise TypeError, "#{self.class}##{name}: #{errors.join('; ')}" unless errors.empty? + + value + end + end + end + query_class.prepend(checker) + end + + def return_mismatches(value, type, rbs_path, module_name) + return [] if %w[untyped untyped?].include?(type) + return ['expected nil'] if type == 'void' && !value.nil? + return [] if type == 'void' + + optional = type.end_with?('?') + type = type.delete_suffix('?') + return optional ? [] : ["expected #{type}, got nil"] if value.nil? + + if (element_type = type[/\AArray\[(.*)\]\z/, 1]) + return ["expected Array, got #{value.class}"] unless value.is_a?(Array) + element_type += '?' unless element_type.end_with?('?') + + return value.each_with_index.flat_map do |element, index| + return_mismatches(element, element_type, rbs_path, module_name).map { |error| "[#{index}] #{error}" } + end + end + + if type == 'bool' + return [true, false].include?(value) ? [] : ["expected bool, got #{value.class}"] + end + klass = resolve_type(type, module_name) + return ["expected #{type}, got #{value.class}"] unless klass && value.is_a?(klass) + + class_name = type.split('::').last + class_rbs_path = [rbs_path, File.join(File.dirname(rbs_path), 'models.rbs')].find do |path| + File.file?(path) && rbs_has_class?(path, class_name) + end + return [] unless class_rbs_path + + attributes(class_rbs_path, class_name).flat_map do |name, attribute_type| + nested = return_mismatches(value.public_send(name), attribute_type, rbs_path, module_name) + nested.map { |error| "#{class_name}##{name} #{error}" } + end + end + + def resolve_type(type, module_name) + return { 'String' => String, 'Integer' => Integer, 'Float' => Float, 'BigDecimal' => BigDecimal, + 'Date' => Date, 'Time' => Time, 'IPAddr' => IPAddr, 'bool' => [TrueClass, FalseClass], + 'nil' => NilClass }[type] if + %w[String Integer Float BigDecimal Date Time IPAddr bool nil].include?(type) + + ["#{module_name}::#{type}", "#{module_name}::QuerySql::#{type}"].each do |name| + return Object.const_get(name) + rescue NameError + next + end + nil + end + + def rbs_has_class?(rbs_path, class_name) + File.read(rbs_path).match?(/^\s*class #{Regexp.escape(class_name)}(?: < [^\n]+)?$/) + end + def matches?(value, type) optional = type.end_with?('?') type = type.chomp('?') From 795eb5f23c95b2aaf2c0776da7f413045243df50 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 20:26:51 +0000 Subject: [PATCH 4/9] Regenerate nullable returning ID signatures Co-Authored-By: Doron Eli Rachman --- examples/pg/query_sql.rbs | 6 +++--- examples/sqlite3/query_sql.rbs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/pg/query_sql.rbs b/examples/pg/query_sql.rbs index 844a775..ac69f99 100644 --- a/examples/pg/query_sql.rbs +++ b/examples/pg/query_sql.rbs @@ -516,7 +516,7 @@ module PgCodegen def get_authors_by_optional_name: (lim: Integer, ?name_filter: String?) -> Array[GetAuthorsByOptionalNameRow] - def create_author_return_id: (name: String, bio: String?) -> Integer + def create_author_return_id: (name: String, bio: String?) -> Integer? def update_author_bios: (bio: String?) -> Integer @@ -528,7 +528,7 @@ module PgCodegen def truncate_authors: -> void - def create_book: (name: String, author_id: Integer) -> String + def create_book: (name: String, author_id: Integer) -> String? def create_deferred_book: (author_id: Integer) -> void @@ -588,7 +588,7 @@ module PgCodegen def create_regression!: (id: Integer, order: String, payload: String?, doc: untyped, bio_types: Array[String]?, uids: Array[String]?) -> CreateRegressionRow - def create_regression_if_new: (id: Integer, order: String) -> Integer + def create_regression_if_new: (id: Integer, order: String) -> Integer? def copy_regressions: (Array[CopyRegressionsArgs] rows) -> void diff --git a/examples/sqlite3/query_sql.rbs b/examples/sqlite3/query_sql.rbs index 649eba3..1c252ae 100644 --- a/examples/sqlite3/query_sql.rbs +++ b/examples/sqlite3/query_sql.rbs @@ -343,7 +343,7 @@ module Sqlite3Codegen def get_authors_by_optional_name: (lim: Integer, ?name_filter: untyped) -> Array[GetAuthorsByOptionalNameRow] - def create_author_return_id: (name: String, bio: String?) -> Integer + def create_author_return_id: (name: String, bio: String?) -> Integer? def update_author_bios: (bio: String?) -> Integer @@ -363,7 +363,7 @@ module Sqlite3Codegen def truncate_authors: -> void - def create_book: (name: String, author_id: Integer) -> Integer + def create_book: (name: String, author_id: Integer) -> Integer? def list_all_authors_books: -> Array[ListAllAuthorsBooksRow] From 92d134e548b2f2e3b28dcdc374ccbea7365cf50c Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 20:32:09 +0000 Subject: [PATCH 5/9] Add E2E option variants and runtime checks Co-Authored-By: Doron Eli Rachman --- .github/workflows/tests.yml | 79 ++++++++++++++++++++++++------ Gemfile.e2e | 11 ++--- scripts/tests/generate_examples.rb | 33 +++++++++++-- tests/end2end_pg.rb | 35 ++++++++++++- tests/end2end_sqlite3.rb | 32 +++++++++++- 5 files changed, 161 insertions(+), 29 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a3637f4..86d043e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -63,13 +63,17 @@ jobs: run: ruby -Ilib -Itest -e 'Dir["test/*_test.rb"].sort.each { |file| require File.expand_path(file) }' end2end-pg: - name: End-to-End (PostgreSQL, Ruby ${{ matrix.ruby-version }}) + name: End-to-End (PostgreSQL, Ruby ${{ matrix.ruby-version }}, ${{ matrix.variant }}) runs-on: ubuntu-latest strategy: fail-fast: false matrix: ruby-version: ['3.1', '3.2', '3.3', '3.4', '4.0'] + variant: ['default'] + include: + - ruby-version: '4.0' + variant: 'module-no-prepared' services: postgres: @@ -88,6 +92,9 @@ jobs: env: POSTGRES_HOST: 127.0.0.1 + EXAMPLES_DRIVER: pg + EXAMPLES_MODULE: ${{ matrix.variant == 'module-no-prepared' && 'MyApp::Db' || 'PgCodegen' }} + PREPARED_STATEMENTS: ${{ matrix.variant == 'module-no-prepared' && 'false' || 'true' }} BUNDLE_GEMFILE: ${{ (matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2') && 'Gemfile.e2e' || 'Gemfile' }} steps: @@ -96,23 +103,33 @@ jobs: - uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby-version }} - bundler-cache: true + bundler-cache: ${{ matrix.ruby-version != '3.1' && matrix.ruby-version != '3.2' }} - name: Load tool versions - if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' + if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' || matrix.variant != 'default' run: grep -E '^[A-Z_]+=' scripts/tool-versions.env >> "$GITHUB_ENV" - uses: sqlc-dev/setup-sqlc@v5 - if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' + if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' || matrix.variant != 'default' with: sqlc-version: '${{ env.SQLC_VERSION }}' - name: Generate version-specific examples if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' run: | - ruby scripts/tests/generate_examples.rb "${{ matrix.ruby-version }}" sqlc.local.yaml + ruby scripts/tests/generate_examples.rb "${{ matrix.ruby-version }}" sqlc.local.yaml --generate-gemfile echo "EXAMPLES_DIR=$PWD/.examples-ruby-${{ matrix.ruby-version }}" >> "$GITHUB_ENV" + - name: Generate option variant + if: matrix.variant == 'module-no-prepared' + run: | + ruby scripts/tests/generate_examples.rb 4.0 sqlc.local.yaml --driver pg --options '{"moduleName":"MyApp::Db","preparedStatements":false}' + echo "EXAMPLES_DIR=$PWD/.examples-ruby-4.0" >> "$GITHUB_ENV" + + - name: Install E2E dependencies + if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' + run: bundle install + - name: Create the schema run: psql -v ON_ERROR_STOP=1 -h 127.0.0.1 -U "${DB_USER}" -d "${DB_NAME}" -f examples/authors/postgresql/schema.sql env: @@ -122,13 +139,17 @@ jobs: run: bundle exec rake end2end_pg end2end-mysql: - name: End-to-End (MySQL, Ruby ${{ matrix.ruby-version }}) + name: End-to-End (MySQL, Ruby ${{ matrix.ruby-version }}, ${{ matrix.variant }}) runs-on: ubuntu-latest strategy: fail-fast: false matrix: ruby-version: ['3.1', '3.2', '3.3', '3.4', '4.0'] + variant: ['default'] + include: + - ruby-version: '4.0' + variant: 'module-name' services: mysql: @@ -146,6 +167,8 @@ jobs: env: MYSQL_HOST: 127.0.0.1 + EXAMPLES_DRIVER: mysql2 + EXAMPLES_MODULE: ${{ matrix.variant == 'module-name' && 'MyApp::Db' || 'Mysql2Codegen' }} BUNDLE_GEMFILE: ${{ (matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2') && 'Gemfile.e2e' || 'Gemfile' }} steps: @@ -154,23 +177,33 @@ jobs: - uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby-version }} - bundler-cache: true + bundler-cache: ${{ matrix.ruby-version != '3.1' && matrix.ruby-version != '3.2' }} - name: Load tool versions - if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' + if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' || matrix.variant != 'default' run: grep -E '^[A-Z_]+=' scripts/tool-versions.env >> "$GITHUB_ENV" - uses: sqlc-dev/setup-sqlc@v5 - if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' + if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' || matrix.variant != 'default' with: sqlc-version: '${{ env.SQLC_VERSION }}' - name: Generate version-specific examples if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' run: | - ruby scripts/tests/generate_examples.rb "${{ matrix.ruby-version }}" sqlc.local.yaml + ruby scripts/tests/generate_examples.rb "${{ matrix.ruby-version }}" sqlc.local.yaml --generate-gemfile echo "EXAMPLES_DIR=$PWD/.examples-ruby-${{ matrix.ruby-version }}" >> "$GITHUB_ENV" + - name: Generate option variant + if: matrix.variant == 'module-name' + run: | + ruby scripts/tests/generate_examples.rb 4.0 sqlc.local.yaml --driver mysql2 --options '{"moduleName":"MyApp::Db"}' + echo "EXAMPLES_DIR=$PWD/.examples-ruby-4.0" >> "$GITHUB_ENV" + + - name: Install E2E dependencies + if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' + run: bundle install + - name: Create the schema (and allow LOAD DATA LOCAL INFILE for :copyfrom) run: | mysql -h 127.0.0.1 -u "${DB_USER}" -e 'SET GLOBAL local_infile = 1' @@ -180,15 +213,21 @@ jobs: run: bundle exec rake end2end_mysql2 end2end-sqlite: - name: End-to-End (SQLite, Ruby ${{ matrix.ruby-version }}) + name: End-to-End (SQLite, Ruby ${{ matrix.ruby-version }}, ${{ matrix.variant }}) runs-on: ubuntu-latest strategy: fail-fast: false matrix: ruby-version: ['3.1', '3.2', '3.3', '3.4', '4.0'] + variant: ['default'] + include: + - ruby-version: '4.0' + variant: 'module-name' env: + EXAMPLES_DRIVER: sqlite3 + EXAMPLES_MODULE: ${{ matrix.variant == 'module-name' && 'MyApp::Db' || 'Sqlite3Codegen' }} BUNDLE_GEMFILE: ${{ (matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2') && 'Gemfile.e2e' || 'Gemfile' }} steps: @@ -197,22 +236,32 @@ jobs: - uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby-version }} - bundler-cache: true + bundler-cache: ${{ matrix.ruby-version != '3.1' && matrix.ruby-version != '3.2' }} - name: Load tool versions - if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' + if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' || matrix.variant != 'default' run: grep -E '^[A-Z_]+=' scripts/tool-versions.env >> "$GITHUB_ENV" - uses: sqlc-dev/setup-sqlc@v5 - if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' + if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' || matrix.variant != 'default' with: sqlc-version: '${{ env.SQLC_VERSION }}' - name: Generate version-specific examples if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' run: | - ruby scripts/tests/generate_examples.rb "${{ matrix.ruby-version }}" sqlc.local.yaml + ruby scripts/tests/generate_examples.rb "${{ matrix.ruby-version }}" sqlc.local.yaml --generate-gemfile echo "EXAMPLES_DIR=$PWD/.examples-ruby-${{ matrix.ruby-version }}" >> "$GITHUB_ENV" + - name: Generate option variant + if: matrix.variant == 'module-name' + run: | + ruby scripts/tests/generate_examples.rb 4.0 sqlc.local.yaml --driver sqlite3 --options '{"moduleName":"MyApp::Db"}' + echo "EXAMPLES_DIR=$PWD/.examples-ruby-4.0" >> "$GITHUB_ENV" + + - name: Install E2E dependencies + if: matrix.ruby-version == '3.1' || matrix.ruby-version == '3.2' + run: bundle install + - name: End-to-End tests run: bundle exec rake end2end_sqlite3 diff --git a/Gemfile.e2e b/Gemfile.e2e index d525ab6..0b924e0 100644 --- a/Gemfile.e2e +++ b/Gemfile.e2e @@ -2,13 +2,10 @@ source 'https://rubygems.org' +examples_dir = ENV.fetch('EXAMPLES_DIR') { raise 'EXAMPLES_DIR must point at generated scratch examples' } +driver = ENV.fetch('EXAMPLES_DRIVER') { raise 'EXAMPLES_DRIVER must be set for E2E bundling' } +eval_gemfile File.join(examples_dir, driver, 'Gemfile') + gem 'async', RUBY_VERSION.start_with?('3.1') ? '< 2.7' : '>= 2.7' -gem 'bigdecimal' -gem 'connection_pool' -gem 'csv' -gem 'logger' gem 'minitest' -gem 'mysql2' -gem 'pg', '1.7.0' gem 'rake' -gem 'sqlite3' diff --git a/scripts/tests/generate_examples.rb b/scripts/tests/generate_examples.rb index bc10696..fce5914 100644 --- a/scripts/tests/generate_examples.rb +++ b/scripts/tests/generate_examples.rb @@ -2,13 +2,33 @@ # frozen_string_literal: true require 'fileutils' +require 'json' require 'yaml' -ruby_version, config_path = ARGV -abort "usage: #{$PROGRAM_NAME} [sqlc config]" unless ruby_version +ruby_version = ARGV.shift +abort "usage: #{$PROGRAM_NAME} [sqlc config] [--driver DRIVER] [--options JSON] [--generate-gemfile]" unless ruby_version + +config_path = ARGV.first&.start_with?('--') ? nil : ARGV.shift +config_path ||= 'sqlc.local.yaml' +driver = nil +plugin_options = {} +generate_gemfile = false +until ARGV.empty? + case ARGV.shift + when '--driver' + driver = ARGV.shift || abort('--driver requires a value') + when '--options' + plugin_options = JSON.parse(ARGV.shift || abort('--options requires a JSON object')) + abort('--options must be a JSON object') unless plugin_options.is_a?(Hash) + when '--generate-gemfile' + generate_gemfile = true + else + abort "usage: #{$PROGRAM_NAME} [sqlc config] [--driver DRIVER] [--options JSON] [--generate-gemfile]" + end +end root = File.expand_path('../..', __dir__) -config_path = File.expand_path(config_path || 'sqlc.local.yaml', root) +config_path = File.expand_path(config_path, root) config_dir = File.dirname(config_path) scratch = File.expand_path(".examples-ruby-#{ruby_version}", root) unless scratch.start_with?("#{root}/.examples-ruby-") && File.basename(scratch).match?(/\A\.examples-ruby-[\d.]+\z/) @@ -18,10 +38,15 @@ FileUtils.rm_rf(scratch) FileUtils.mkdir_p(scratch) config = YAML.safe_load_file(config_path) +config['sql'] = config.fetch('sql').select do |sql| + !driver || sql.fetch('codegen').first.fetch('options').fetch('driver') == driver +end +abort "no sql block found for driver #{driver}" if config['sql'].empty? config.fetch('sql').each do |sql| codegen = sql.fetch('codegen').first codegen['out'] = File.join(File.basename(scratch), codegen.fetch('options').fetch('driver')) - codegen.fetch('options')['rubyVersion'] = ruby_version + codegen.fetch('options').merge!(plugin_options) + codegen.fetch('options').merge!('rubyVersion' => ruby_version, 'generateGemfile' => generate_gemfile) end tmp_config = File.join(config_dir, ".generate-examples.#{Process.pid}.yaml") diff --git a/tests/end2end_pg.rb b/tests/end2end_pg.rb index 8febb28..51d0215 100755 --- a/tests/end2end_pg.rb +++ b/tests/end2end_pg.rb @@ -13,10 +13,12 @@ require_relative 'consts' require_relative 'rbs_types' require File.join(EXAMPLES_DIR, 'pg/query_sql') +RbsTypes.install(Object.const_get("#{ENV.fetch('EXAMPLES_MODULE', 'PgCodegen')}::QuerySql"), + File.join(EXAMPLES_DIR, 'pg/query_sql.rbs')) # End-to-end tests of the code generated for examples/authors/postgresql, against a real PostgreSQL server. class TestPg < Minitest::Test - include PgCodegen + include Object.const_get(ENV.fetch('EXAMPLES_MODULE', 'PgCodegen')) TEXT_PARTS = ["é", '🙂', "\t", "\n", "\r", '\\', "'", '"', ',', '\N', 'NULL'].freeze @@ -24,6 +26,24 @@ def row_for(klass, values) klass.new(**klass.members.zip(values).to_h) end + def assert_row_layout(row) + if defined?(Data) && row.is_a?(Data) + assert_predicate row, :frozen? + assert_equal row.to_h, row.with.to_h + else + assert_kind_of Struct, row + assert row.class.keyword_init? + refute_predicate row, :frozen? + end + case row + in { id:, name: } + assert_kind_of Integer, id + assert_equal row.name, name + else + flunk 'row did not support keyword pattern matching' + end + end + def row_value(row, member) actual_member = row.class.members.find { |name| name == member.to_sym || name == :"#{member}_" } row.public_send(actual_member) @@ -94,6 +114,14 @@ def test_flow @query_sql.list_authors end + def test_rows_and_embedded_models_have_supported_layout_and_pattern_matching + first = create_author('pattern') + create_author('pattern') + assert_row_layout(@query_sql.get_author(id: first)) + embedded = @query_sql.get_duplicate_authors.first.author + assert_row_layout(embedded) + end + def test_nullable_columns_round_trip_nil id = create_author('no bio', nil) assert_nil @query_sql.get_author(id: id).bio @@ -295,6 +323,11 @@ def test_transaction_is_isolated_until_commit # ---- connection pool ------------------------------------------------------------------------------------------- def test_every_pooled_connection_prepares_its_own_statements + if ENV['PREPARED_STATEMENTS'] == 'false' + assert_empty @query_sql.list_authors + return + end + pooled = QuerySql.connect(pg_params, size: 3) pool = pooled.instance_variable_get(:@db) held = Queue.new diff --git a/tests/end2end_sqlite3.rb b/tests/end2end_sqlite3.rb index e84b131..87ecb44 100644 --- a/tests/end2end_sqlite3.rb +++ b/tests/end2end_sqlite3.rb @@ -9,10 +9,12 @@ require_relative 'consts' require_relative 'rbs_types' require File.join(EXAMPLES_DIR, 'sqlite3/query_sql') +RbsTypes.install(Object.const_get("#{ENV.fetch('EXAMPLES_MODULE', 'Sqlite3Codegen')}::QuerySql"), + File.join(EXAMPLES_DIR, 'sqlite3/query_sql.rbs')) # End-to-end tests of the code generated for examples/authors/sqlite, against a real (in-memory) SQLite database. class TestSqlite3 < Minitest::Test - include Sqlite3Codegen + include Object.const_get(ENV.fetch('EXAMPLES_MODULE', 'Sqlite3Codegen')) TEXT_PARTS = ["é", '🙂', "\t", "\n", "\r", '\\', "'", '"', ',', '\N', 'NULL'].freeze @@ -20,6 +22,24 @@ def row_for(klass, values) klass.new(**klass.members.zip(values).to_h) end + def assert_row_layout(row) + if defined?(Data) && row.is_a?(Data) + assert_predicate row, :frozen? + assert_equal row.to_h, row.with.to_h + else + assert_kind_of Struct, row + assert row.class.keyword_init? + refute_predicate row, :frozen? + end + case row + in { id:, name: } + assert_kind_of Integer, id + assert_equal row.name, name + else + flunk 'row did not support keyword pattern matching' + end + end + def seeded_random seed = Integer(ENV.fetch('SEED', '1793729686')) warn "SEED=#{seed} #{self.class}" @@ -69,6 +89,14 @@ def test_flow assert_equal [Consts::DR_SEUSS_AUTHOR], @query_sql.list_authors.map(&:name) end + def test_rows_and_embedded_models_have_supported_layout_and_pattern_matching + first = create_author('pattern') + create_author('pattern') + assert_row_layout(@query_sql.get_author(id: first)) + embedded = @query_sql.get_duplicate_authors.first.author + assert_row_layout(embedded) + end + def test_nullable_columns_round_trip_nil id = create_author('no bio', nil) assert_nil @query_sql.get_author(id: id).bio @@ -520,7 +548,7 @@ def test_seeded_random_copyfrom_round_trip_and_duplicate_is_atomic random = seeded_random rows = Array.new(300) do |i| CopyRegressionRowsArgs.new(id: 5_000_000_000 + i, order: "#{i}#{random_text(random)}", - price: random.rand(3).zero? ? nil : random.rand(1..100), + price: random.rand(3).zero? ? nil : random.rand(1..100) / 101.0, payload: random.bytes(random.rand(0..64)).b) end @query_sql.copy_regression_rows(rows) From a9ba415ef32370ccf33015b5d7319b597f324a62 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 20:56:50 +0000 Subject: [PATCH 6/9] Add option and engine coverage E2E Co-Authored-By: Doron Eli Rachman --- .github/workflows/tests.yml | 45 +++++ examples/authors/mysql/query.sql | 23 +++ examples/authors/mysql/schema.sql | 17 +- examples/authors/postgresql/query.sql | 31 +++ examples/authors/postgresql/schema.sql | 30 +++ examples/authors/sqlite/query.sql | 23 +++ examples/authors/sqlite/schema.sql | 11 ++ examples/mysql2/models.rb | 18 +- examples/mysql2/models.rbs | 31 ++- examples/mysql2/query_sql.rb | 160 ++++++++++++++- examples/mysql2/query_sql.rbs | 82 +++++++- examples/pg/models.rb | 31 +++ examples/pg/models.rbs | 52 +++++ examples/pg/query_sql.rb | 246 ++++++++++++++++++++++++ examples/pg/query_sql.rbs | 107 +++++++++++ examples/sqlite3/models.rb | 14 ++ examples/sqlite3/models.rbs | 21 ++ examples/sqlite3/query_sql.rb | 175 +++++++++++++++++ examples/sqlite3/query_sql.rbs | 72 +++++++ tests/end2end_mysql2.rb | 80 +++++++- tests/end2end_options.rb | 113 +++++++++++ tests/end2end_pg.rb | 72 ++++++- tests/end2end_sqlite3.rb | 77 +++++++- tests/options/pg_queries/first.sql | 4 + tests/options/pg_schema.sql | 19 ++ tests/options/sqlc.yaml | 72 +++++++ tests/options/sqlite_queries/first.sql | 13 ++ tests/options/sqlite_queries/second.sql | 4 + tests/options/sqlite_schema.sql | 21 ++ 29 files changed, 1646 insertions(+), 18 deletions(-) create mode 100644 tests/end2end_options.rb create mode 100644 tests/options/pg_queries/first.sql create mode 100644 tests/options/pg_schema.sql create mode 100644 tests/options/sqlc.yaml create mode 100644 tests/options/sqlite_queries/first.sql create mode 100644 tests/options/sqlite_queries/second.sql create mode 100644 tests/options/sqlite_schema.sql diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 86d043e..2652924 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -265,3 +265,48 @@ jobs: - name: End-to-End tests run: bundle exec rake end2end_sqlite3 + + end2end-options: + name: Generator options E2E (Ruby ${{ matrix.ruby-version }}) + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + ruby-version: ['3.1', '4.0'] + + services: + postgres: + image: postgres:16.2 + env: + POSTGRES_DB: tests + POSTGRES_USER: root + POSTGRES_PASSWORD: pass + ports: + - 5432:5432 + options: >- + --health-cmd "pg_isready -U root -d tests" + --health-interval 5s + --health-timeout 5s + --health-retries 10 + + env: + POSTGRES_HOST: 127.0.0.1 + + steps: + - uses: actions/checkout@v7 + + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby-version }} + bundler-cache: true + + - name: Load tool versions + run: grep -E '^[A-Z_]+=' scripts/tool-versions.env >> "$GITHUB_ENV" + + - uses: sqlc-dev/setup-sqlc@v5 + with: + sqlc-version: '${{ env.SQLC_VERSION }}' + + - name: End-to-end option tests + run: bundle exec ruby tests/end2end_options.rb diff --git a/examples/authors/mysql/query.sql b/examples/authors/mysql/query.sql index 0687f92..e8a4d61 100644 --- a/examples/authors/mysql/query.sql +++ b/examples/authors/mysql/query.sql @@ -128,6 +128,12 @@ SELECT id, payload FROM regressions ORDER BY id; -- name: ListRegressionRows :many SELECT id, `order`, note, payload FROM regressions ORDER BY id; +-- name: ListAuthorsWithOptionalBook :many +SELECT sqlc.embed(authors), sqlc.embed(books) +FROM authors +LEFT JOIN books ON authors.id = books.author_id +ORDER BY authors.id; + -- name: TruncateRegressions :exec TRUNCATE TABLE regressions; @@ -176,3 +182,20 @@ SELECT * FROM mysql_binding_values WHERE id = sqlc.arg(id); -- name: TruncateMysqlBindingValues :exec TRUNCATE TABLE mysql_binding_values; + +-- name: InsertNameCase :exec +INSERT INTO name_cases (id, `class`, `end`, `hash`, `method`, `count`, `camelCase`, `HTTPServer`) +VALUES (sqlc.arg(id), sqlc.arg(class), sqlc.arg(end_), sqlc.arg(hash), sqlc.arg(method), sqlc.arg(count), + sqlc.arg(camel_case), sqlc.arg(http_server)); + +-- name: GetNameCase :one +SELECT * FROM name_cases +WHERE id = sqlc.arg(id) AND `class` = sqlc.arg(class) AND `end` = sqlc.arg(end_) AND `hash` = sqlc.arg(hash) + AND `method` = sqlc.arg(method) AND `count` = sqlc.arg(count) + AND `HTTPServer` = sqlc.arg(http_server) AND `camelCase` = sqlc.arg(camel_case); + +-- name: GetHTTPServer :one +SELECT * FROM name_cases WHERE id = sqlc.arg(id); + +-- name: TruncateNameCases :exec +TRUNCATE TABLE name_cases; diff --git a/examples/authors/mysql/schema.sql b/examples/authors/mysql/schema.sql index d4a1a10..ac8b339 100644 --- a/examples/authors/mysql/schema.sql +++ b/examples/authors/mysql/schema.sql @@ -91,5 +91,20 @@ CREATE TABLE mysql_binding_values ( datetime_value DATETIME(6), datetime_2 DATETIME(6), bool_value BOOLEAN, - nullable_value TEXT + nullable_value TEXT, + bit_value BIT(16), + year_value YEAR, + time_value TIME, + json_unicode JSON +); + +CREATE TABLE name_cases ( + id BIGINT NOT NULL PRIMARY KEY, + `class` TEXT NOT NULL, + `end` TEXT NOT NULL, + `hash` TEXT NOT NULL, + `method` TEXT NOT NULL, + `count` INT NOT NULL, + `camelCase` TEXT NOT NULL, + `HTTPServer` TEXT NOT NULL ); diff --git a/examples/authors/postgresql/query.sql b/examples/authors/postgresql/query.sql index 39452aa..89a04d9 100644 --- a/examples/authors/postgresql/query.sql +++ b/examples/authors/postgresql/query.sql @@ -131,6 +131,20 @@ SELECT * FROM postgres_extra_types LIMIT 1; -- name: TruncateExtraTypes :exec TRUNCATE TABLE postgres_extra_types; +-- name: InsertRemainingTypes :exec +INSERT INTO postgres_remaining_types ( + c_hstore, c_ltree, c_regclass, c_cidr, c_macaddr, c_interval, c_varbit, c_xml, + c_numeric, c_bigint, c_timestamp, c_timestamptz, c_text_matrix +) VALUES ( + $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13 +); + +-- name: GetRemainingTypes :one +SELECT * FROM postgres_remaining_types LIMIT 1; + +-- name: TruncateRemainingTypes :exec +TRUNCATE TABLE postgres_remaining_types; + -- name: InsertArrayTypes :exec INSERT INTO postgres_array_types (c_uuid_array, c_bio_type_array, c_integer_matrix) VALUES ($1, $2, $3); @@ -207,3 +221,20 @@ SELECT count(*) FROM batch_types WHERE id > sqlc.arg(MIN_ID); -- name: TruncateBatchTypes :exec TRUNCATE TABLE batch_types; + +-- name: InsertNameCase :exec +INSERT INTO name_cases (id, "class", "end", "hash", "method", "count", "camelCase", "HTTPServer") +VALUES (sqlc.arg(id), sqlc.arg(class), sqlc.arg(end_), sqlc.arg(hash), sqlc.arg(method), sqlc.arg(count), + sqlc.arg(camel_case), sqlc.arg(http_server)); + +-- name: GetNameCase :one +SELECT * FROM name_cases +WHERE id = sqlc.arg(id) AND "class" = sqlc.arg(class) AND "end" = sqlc.arg(end_) AND "hash" = sqlc.arg(hash) + AND "method" = sqlc.arg(method) AND "count" = sqlc.arg(count) + AND "HTTPServer" = sqlc.arg(http_server) AND "camelCase" = sqlc.arg(camel_case); + +-- name: GetHTTPServer :one +SELECT * FROM name_cases WHERE id = sqlc.arg(id); + +-- name: TruncateNameCases :exec +TRUNCATE TABLE name_cases; diff --git a/examples/authors/postgresql/schema.sql b/examples/authors/postgresql/schema.sql index f694cca..5123fd8 100644 --- a/examples/authors/postgresql/schema.sql +++ b/examples/authors/postgresql/schema.sql @@ -44,6 +44,9 @@ CREATE TABLE deferred_books ( CREATE SCHEMA extended; +CREATE EXTENSION IF NOT EXISTS hstore; +CREATE EXTENSION IF NOT EXISTS ltree; + CREATE TYPE extended.bio_type AS ENUM ('Autobiography', 'Biography', 'Memoir'); CREATE TABLE extended.bios ( @@ -64,6 +67,22 @@ CREATE TABLE postgres_extra_types ( c_not_null TEXT NOT NULL DEFAULT 'x' ); +CREATE TABLE postgres_remaining_types ( + c_hstore HSTORE, + c_ltree LTREE, + c_regclass REGCLASS, + c_cidr CIDR, + c_macaddr MACADDR, + c_interval INTERVAL, + c_varbit VARBIT, + c_xml XML, + c_numeric NUMERIC(100, 50), + c_bigint BIGINT, + c_timestamp TIMESTAMP, + c_timestamptz TIMESTAMPTZ, + c_text_matrix TEXT[][] +); + CREATE TABLE json_round_trips ( id INTEGER PRIMARY KEY, json_value JSON, @@ -113,3 +132,14 @@ CREATE TABLE batch_types ( amounts numeric[], stamps timestamptz[] ); + +CREATE TABLE name_cases ( + id BIGINT PRIMARY KEY, + "class" TEXT NOT NULL, + "end" TEXT NOT NULL, + "hash" TEXT NOT NULL, + "method" TEXT NOT NULL, + "count" INTEGER NOT NULL, + "camelCase" TEXT NOT NULL, + "HTTPServer" TEXT NOT NULL +); diff --git a/examples/authors/sqlite/query.sql b/examples/authors/sqlite/query.sql index 4f093d7..9100a64 100644 --- a/examples/authors/sqlite/query.sql +++ b/examples/authors/sqlite/query.sql @@ -175,3 +175,26 @@ SELECT name, uses FROM tags ORDER BY name; -- name: TruncateTags :exec DELETE FROM tags; + +-- name: ListAuthorsWithOptionalBook :many +SELECT sqlc.embed(authors), sqlc.embed(books) +FROM authors +LEFT JOIN books ON authors.id = books.author_id +ORDER BY authors.id; + +-- name: InsertNameCase :exec +INSERT INTO name_cases (id, "class", "end", "hash", "method", "count", "camelCase", "HTTPServer") +VALUES (sqlc.arg(id), sqlc.arg(class), sqlc.arg(end_), sqlc.arg(hash), sqlc.arg(method), sqlc.arg(count), + sqlc.arg(camel_case), sqlc.arg(http_server)); + +-- name: GetNameCase :one +SELECT * FROM name_cases +WHERE id = sqlc.arg(id) AND "class" = sqlc.arg(class) AND "end" = sqlc.arg(end_) AND "hash" = sqlc.arg(hash) + AND "method" = sqlc.arg(method) AND "count" = sqlc.arg(count) + AND "HTTPServer" = sqlc.arg(http_server) AND "camelCase" = sqlc.arg(camel_case); + +-- name: GetHTTPServer :one +SELECT * FROM name_cases WHERE id = sqlc.arg(id); + +-- name: TruncateNameCases :exec +DELETE FROM name_cases; diff --git a/examples/authors/sqlite/schema.sql b/examples/authors/sqlite/schema.sql index 6ec86cc..ebec882 100644 --- a/examples/authors/sqlite/schema.sql +++ b/examples/authors/sqlite/schema.sql @@ -34,3 +34,14 @@ CREATE TABLE tags ( name TEXT NOT NULL PRIMARY KEY, uses INTEGER NOT NULL DEFAULT 0 ); + +CREATE TABLE name_cases ( + id INTEGER PRIMARY KEY, + "class" TEXT NOT NULL, + "end" TEXT NOT NULL, + "hash" TEXT NOT NULL, + "method" TEXT NOT NULL, + "count" INTEGER NOT NULL, + "camelCase" TEXT NOT NULL, + "HTTPServer" TEXT NOT NULL +); diff --git a/examples/mysql2/models.rb b/examples/mysql2/models.rb index f1181dd..444d3df 100644 --- a/examples/mysql2/models.rb +++ b/examples/mysql2/models.rb @@ -92,8 +92,24 @@ class MysqlBindingValu < Data.define( :datetime_value, :datetime_2, :bool_value, - :nullable_value + :nullable_value, + :bit_value, + :year_value, + :time_value, + :json_unicode ) end # rubocop:enable Naming/VariableNumber + + class NameCase < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelcase, + :httpserver + ) + end end diff --git a/examples/mysql2/models.rbs b/examples/mysql2/models.rbs index 1888981..224d903 100644 --- a/examples/mysql2/models.rbs +++ b/examples/mysql2/models.rbs @@ -152,6 +152,10 @@ module Mysql2Codegen attr_reader datetime_2: Time? attr_reader bool_value: Integer? attr_reader nullable_value: String? + attr_reader bit_value: String? + attr_reader year_value: Integer? + attr_reader time_value: Time? + attr_reader json_unicode: String? def initialize: ( Integer id, String? text_value, @@ -164,7 +168,32 @@ module Mysql2Codegen Time? datetime_value, Time? datetime_2, Integer? bool_value, - String? nullable_value + String? nullable_value, + String? bit_value, + Integer? year_value, + Time? time_value, + String? json_unicode + ) -> void + end + + class NameCase + attr_reader id: Integer + attr_reader class_: String + attr_reader end: String + attr_reader hash_: String + attr_reader method_: String + attr_reader count: Integer + attr_reader camelcase: String + attr_reader httpserver: String + def initialize: ( + Integer id, + String class_, + String end, + String hash_, + String method_, + Integer count, + String camelcase, + String httpserver ) -> void end end diff --git a/examples/mysql2/query_sql.rb b/examples/mysql2/query_sql.rb index bed3059..18c82ad 100644 --- a/examples/mysql2/query_sql.rb +++ b/examples/mysql2/query_sql.rb @@ -271,6 +271,16 @@ class ListRegressionPayloadsRow < Data.define(:id, :payload) class ListRegressionRowsRow < Data.define(:id, :order, :note, :payload) end + LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL = <<~'SQL' + SELECT authors.id, authors.name, authors.bio, books.id, books.name, books.author_id, books.description + FROM authors + LEFT JOIN books ON authors.id = books.author_id + ORDER BY authors.id + SQL + + class ListAuthorsWithOptionalBookRow < Data.define(:author, :book) + end + TRUNCATE_REGRESSIONS_SQL = <<~'SQL' TRUNCATE TABLE regressions SQL @@ -354,7 +364,7 @@ class GetAuthorWithQuestionMarksRow < Data.define(:id) SQL GET_MYSQL_BINDING_VALUES_SQL = <<~'SQL' - SELECT id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, datetime_value, datetime_2, bool_value, nullable_value FROM mysql_binding_values WHERE id = ? + SELECT id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, datetime_value, datetime_2, bool_value, nullable_value, bit_value, year_value, time_value, json_unicode FROM mysql_binding_values WHERE id = ? SQL # rubocop:disable Naming/VariableNumber @@ -370,7 +380,11 @@ class GetMysqlBindingValuesRow < Data.define( :datetime_value, :datetime_2, :bool_value, - :nullable_value + :nullable_value, + :bit_value, + :year_value, + :time_value, + :json_unicode ) end # rubocop:enable Naming/VariableNumber @@ -379,6 +393,51 @@ class GetMysqlBindingValuesRow < Data.define( TRUNCATE TABLE mysql_binding_values SQL + INSERT_NAME_CASE_SQL = <<~'SQL' + INSERT INTO name_cases (id, `class`, `end`, `hash`, `method`, `count`, `camelCase`, `HTTPServer`) + VALUES (?, ?, ?, ?, ?, ?, + ?, ?) + SQL + + GET_NAME_CASE_SQL = <<~'SQL' + SELECT id, class, end, hash, method, count, camelcase, httpserver FROM name_cases + WHERE id = ? AND `class` = ? AND `end` = ? AND `hash` = ? + AND `method` = ? AND `count` = ? + AND `HTTPServer` = ? AND `camelCase` = ? + SQL + + class GetNameCaseRow < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelcase, + :httpserver + ) + end + + GET_HTTP_SERVER_SQL = <<~'SQL' + SELECT id, class, end, hash, method, count, camelcase, httpserver FROM name_cases WHERE id = ? + SQL + + class GetHTTPServerRow < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelcase, + :httpserver + ) + end + + TRUNCATE_NAME_CASES_SQL = <<~'SQL' + TRUNCATE TABLE name_cases + SQL + class QuerySql # Raised by the bang variants (get_author!) when the query returns no row. class NotFound < StandardError; end @@ -817,6 +876,18 @@ def list_regression_rows end end + def list_authors_with_optional_book + with_client('list_authors_with_optional_book') do |client| + result = client.query(bind_params(client, LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL, []), as: :array) + result.map do |row| + ListAuthorsWithOptionalBookRow.new( + row[0].nil? && row[1].nil? ? nil : Author.new(row[0], row[1], row[2]), + row[3].nil? && row[4].nil? && row[5].nil? ? nil : Book.new(row[3], row[4], row[5], row[6]) + ) + end + end + end + def truncate_regressions with_client('truncate_regressions') do |client| client.query(bind_params(client, TRUNCATE_REGRESSIONS_SQL, [])) @@ -971,7 +1042,11 @@ def get_mysql_binding_values(id:) row[8], row[9], row[10], - row[11] + row[11], + row[12], + row[13], + row[14], + row[15] ) end end @@ -988,6 +1063,85 @@ def truncate_mysql_binding_values end end + def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, http_server:) + with_client('insert_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + camel_case, + http_server + ] + client.query(bind_params(client, INSERT_NAME_CASE_SQL, query_params)) + nil + end + end + + def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, camel_case:) + with_client('get_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + http_server, + camel_case + ] + result = client.query(bind_params(client, GET_NAME_CASE_SQL, query_params), as: :array) + row = result.first + row && GetNameCaseRow.new( + row[0], + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7] + ) + end + end + + # Like #get_name_case, but raises NotFound when there is no row. + def get_name_case!(...) + get_name_case(...) || raise(NotFound, 'GetNameCase returned no row') + end + + def get_http_server(id:) + with_client('get_http_server') do |client| + query_params = [id] + result = client.query(bind_params(client, GET_HTTP_SERVER_SQL, query_params), as: :array) + row = result.first + row && GetHTTPServerRow.new( + row[0], + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7] + ) + end + end + + # Like #get_http_server, but raises NotFound when there is no row. + def get_http_server!(...) + get_http_server(...) || raise(NotFound, 'GetHTTPServer returned no row') + end + + def truncate_name_cases + with_client('truncate_name_cases') do |client| + client.query(bind_params(client, TRUNCATE_NAME_CASES_SQL, [])) + nil + end + end + private def instrument(name) diff --git a/examples/mysql2/query_sql.rbs b/examples/mysql2/query_sql.rbs index 550d280..6e6234a 100644 --- a/examples/mysql2/query_sql.rbs +++ b/examples/mysql2/query_sql.rbs @@ -297,6 +297,14 @@ module Mysql2Codegen def initialize: ( Integer id, String order, String? note, String? payload) -> void end + LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL: String + + class ListAuthorsWithOptionalBookRow + attr_reader author: Author? + attr_reader book: Book? + def initialize: ( Author? author, Book? book) -> void + end + TRUNCATE_REGRESSIONS_SQL: String class CopyCopyTypesArgs @@ -399,6 +407,10 @@ module Mysql2Codegen attr_reader datetime_2: Time? attr_reader bool_value: Integer? attr_reader nullable_value: String? + attr_reader bit_value: String? + attr_reader year_value: Integer? + attr_reader time_value: Time? + attr_reader json_unicode: String? def initialize: ( Integer id, String? text_value, @@ -411,12 +423,66 @@ module Mysql2Codegen Time? datetime_value, Time? datetime_2, Integer? bool_value, - String? nullable_value + String? nullable_value, + String? bit_value, + Integer? year_value, + Time? time_value, + String? json_unicode ) -> void end TRUNCATE_MYSQL_BINDING_VALUES_SQL: String + INSERT_NAME_CASE_SQL: String + + GET_NAME_CASE_SQL: String + + class GetNameCaseRow + attr_reader id: Integer + attr_reader class_: String + attr_reader end: String + attr_reader hash_: String + attr_reader method_: String + attr_reader count: Integer + attr_reader camelcase: String + attr_reader httpserver: String + def initialize: ( + Integer id, + String class_, + String end, + String hash_, + String method_, + Integer count, + String camelcase, + String httpserver + ) -> void + end + + GET_HTTP_SERVER_SQL: String + + class GetHTTPServerRow + attr_reader id: Integer + attr_reader class_: String + attr_reader end: String + attr_reader hash_: String + attr_reader method_: String + attr_reader count: Integer + attr_reader camelcase: String + attr_reader httpserver: String + def initialize: ( + Integer id, + String class_, + String end, + String hash_, + String method_, + Integer count, + String camelcase, + String httpserver + ) -> void + end + + TRUNCATE_NAME_CASES_SQL: String + class QuerySql class NotFound < StandardError end @@ -507,6 +573,8 @@ module Mysql2Codegen def list_regression_rows: -> Array[ListRegressionRowsRow] + def list_authors_with_optional_book: -> Array[ListAuthorsWithOptionalBookRow] + def truncate_regressions: -> void def copy_copy_types: (Array[CopyCopyTypesArgs] rows) -> void @@ -535,6 +603,18 @@ module Mysql2Codegen def truncate_mysql_binding_values: -> void + def insert_name_case: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, camel_case: String, http_server: String) -> void + + def get_name_case: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, http_server: String, camel_case: String) -> GetNameCaseRow? + + def get_name_case!: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, http_server: String, camel_case: String) -> GetNameCaseRow + + def get_http_server: (id: Integer) -> GetHTTPServerRow? + + def get_http_server!: (id: Integer) -> GetHTTPServerRow + + def truncate_name_cases: -> void + private def instrument: [T] (String name) { () -> T } -> T diff --git a/examples/pg/models.rb b/examples/pg/models.rb index 6b0fdb0..c690651 100644 --- a/examples/pg/models.rb +++ b/examples/pg/models.rb @@ -55,6 +55,23 @@ class PostgresExtraType < Data.define( ) end + class PostgresRemainingType < Data.define( + :c_hstore, + :c_ltree, + :c_regclass, + :c_cidr, + :c_macaddr, + :c_interval, + :c_varbit, + :c_xml, + :c_numeric, + :c_bigint, + :c_timestamp, + :c_timestamptz, + :c_text_matrix + ) + end + class JsonRoundTrip < Data.define(:id, :json_value, :jsonb_value) end @@ -94,6 +111,20 @@ class BatchType < Data.define( ) end + # rubocop:disable Naming/MethodName + class NameCase < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelCase, + :HTTPServer + ) + end + # rubocop:enable Naming/MethodName + class ExtendedBio < Data.define(:author_name, :name, :bio_type) end end diff --git a/examples/pg/models.rbs b/examples/pg/models.rbs index e5685fd..a9f570d 100644 --- a/examples/pg/models.rbs +++ b/examples/pg/models.rbs @@ -93,6 +93,37 @@ module PgCodegen ) -> void end + class PostgresRemainingType + attr_reader c_hstore: String? + attr_reader c_ltree: String? + attr_reader c_regclass: String? + attr_reader c_cidr: IPAddr? + attr_reader c_macaddr: String? + attr_reader c_interval: String? + attr_reader c_varbit: String? + attr_reader c_xml: String? + attr_reader c_numeric: BigDecimal? + attr_reader c_bigint: Integer? + attr_reader c_timestamp: Time? + attr_reader c_timestamptz: Time? + attr_reader c_text_matrix: Array[Array[String]]? + def initialize: ( + String? c_hstore, + String? c_ltree, + String? c_regclass, + IPAddr? c_cidr, + String? c_macaddr, + String? c_interval, + String? c_varbit, + String? c_xml, + BigDecimal? c_numeric, + Integer? c_bigint, + Time? c_timestamp, + Time? c_timestamptz, + Array[Array[String]]? c_text_matrix + ) -> void + end + class JsonRoundTrip attr_reader id: Integer attr_reader json_value: untyped @@ -164,6 +195,27 @@ module PgCodegen ) -> void end + class NameCase + attr_reader id: Integer + attr_reader class_: String + attr_reader end: String + attr_reader hash_: String + attr_reader method_: String + attr_reader count: Integer + attr_reader camelCase: String + attr_reader HTTPServer: String + def initialize: ( + Integer id, + String class_, + String end, + String hash_, + String method_, + Integer count, + String camelCase, + String HTTPServer + ) -> void + end + class ExtendedBio attr_reader author_name: String attr_reader name: String diff --git a/examples/pg/query_sql.rb b/examples/pg/query_sql.rb index 6f705bf..6b52d98 100644 --- a/examples/pg/query_sql.rb +++ b/examples/pg/query_sql.rb @@ -249,6 +249,40 @@ class GetExtraTypesRow < Data.define( TRUNCATE TABLE postgres_extra_types SQL + INSERT_REMAINING_TYPES_SQL = <<~'SQL' + INSERT INTO postgres_remaining_types ( + c_hstore, c_ltree, c_regclass, c_cidr, c_macaddr, c_interval, c_varbit, c_xml, + c_numeric, c_bigint, c_timestamp, c_timestamptz, c_text_matrix + ) VALUES ( + $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13 + ) + SQL + + GET_REMAINING_TYPES_SQL = <<~'SQL' + SELECT c_hstore, c_ltree, c_regclass, c_cidr, c_macaddr, c_interval, c_varbit, c_xml, c_numeric, c_bigint, c_timestamp, c_timestamptz, c_text_matrix FROM postgres_remaining_types LIMIT 1 + SQL + + class GetRemainingTypesRow < Data.define( + :c_hstore, + :c_ltree, + :c_regclass, + :c_cidr, + :c_macaddr, + :c_interval, + :c_varbit, + :c_xml, + :c_numeric, + :c_bigint, + :c_timestamp, + :c_timestamptz, + :c_text_matrix + ) + end + + TRUNCATE_REMAINING_TYPES_SQL = <<~'SQL' + TRUNCATE TABLE postgres_remaining_types + SQL + INSERT_ARRAY_TYPES_SQL = <<~'SQL' INSERT INTO postgres_array_types (c_uuid_array, c_bio_type_array, c_integer_matrix) VALUES ($1, $2, $3) SQL @@ -444,6 +478,55 @@ class CountBatchTypesAboveRow < Data.define(:count) TRUNCATE TABLE batch_types SQL + INSERT_NAME_CASE_SQL = <<~'SQL' + INSERT INTO name_cases (id, "class", "end", "hash", "method", "count", "camelCase", "HTTPServer") + VALUES ($1, $2, $3, $4, $5, $6, + $7, $8) + SQL + + GET_NAME_CASE_SQL = <<~'SQL' + SELECT id, class, "end", hash, method, count, "camelCase", "HTTPServer" FROM name_cases + WHERE id = $1 AND "class" = $2 AND "end" = $3 AND "hash" = $4 + AND "method" = $5 AND "count" = $6 + AND "HTTPServer" = $7 AND "camelCase" = $8 + SQL + + # rubocop:disable Naming/MethodName + class GetNameCaseRow < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelCase, + :HTTPServer + ) + end + # rubocop:enable Naming/MethodName + + GET_HTTP_SERVER_SQL = <<~'SQL' + SELECT id, class, "end", hash, method, count, "camelCase", "HTTPServer" FROM name_cases WHERE id = $1 + SQL + + # rubocop:disable Naming/MethodName + class GetHTTPServerRow < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelCase, + :HTTPServer + ) + end + # rubocop:enable Naming/MethodName + + TRUNCATE_NAME_CASES_SQL = <<~'SQL' + TRUNCATE TABLE name_cases + SQL + class QuerySql # Raised by the bang variants (get_author!) when the query returns no row. class NotFound < StandardError; end @@ -960,6 +1043,74 @@ def truncate_extra_types end end + def insert_remaining_types(c_hstore:, c_ltree:, c_regclass:, c_cidr:, c_macaddr:, c_interval:, c_varbit:, c_xml:, c_numeric:, c_bigint:, c_timestamp:, c_timestamptz:, c_text_matrix:) + with_client('insert_remaining_types') do |client| + query_params = [ + c_hstore, + c_ltree, + c_regclass, + c_cidr, + c_macaddr, + c_interval, + c_varbit, + c_xml, + c_numeric, + c_bigint, + c_timestamp, + 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') + end + client.exec_prepared('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') + end + result = client.exec_prepared('query_sql_get_remaining_types') + row = result.first + row && GetRemainingTypesRow.new( + row['c_hstore'], + row['c_ltree'], + row['c_regclass'], + row['c_cidr'], + row['c_macaddr'], + row['c_interval'], + row['c_varbit'], + row['c_xml'], + row['c_numeric'], + row['c_bigint'], + row['c_timestamp'], + row['c_timestamptz'], + row['c_text_matrix'] + ) + end + end + + # Like #get_remaining_types, but raises NotFound when there is no row. + def get_remaining_types!(...) + get_remaining_types(...) || raise(NotFound, 'GetRemainingTypes returned no row') + end + + 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') + end + client.exec_prepared('query_sql_truncate_remaining_types') + nil + end + end + 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)] @@ -1332,6 +1483,101 @@ def truncate_batch_types end end + def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, http_server:) + with_client('insert_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + 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') + end + client.exec_prepared('query_sql_insert_name_case', query_params) + nil + end + end + + def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, camel_case:) + with_client('get_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + 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') + end + result = client.exec_prepared('query_sql_get_name_case', query_params) + row = result.first + row && GetNameCaseRow.new( + row['id'], + row['class'], + row['end'], + row['hash'], + row['method'], + row['count'], + row['camelCase'], + row['HTTPServer'] + ) + end + end + + # Like #get_name_case, but raises NotFound when there is no row. + def get_name_case!(...) + get_name_case(...) || raise(NotFound, 'GetNameCase returned no row') + end + + 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') + end + result = client.exec_prepared('query_sql_get_http_server', query_params) + row = result.first + row && GetHTTPServerRow.new( + row['id'], + row['class'], + row['end'], + row['hash'], + row['method'], + row['count'], + row['camelCase'], + row['HTTPServer'] + ) + end + end + + # Like #get_http_server, but raises NotFound when there is no row. + def get_http_server!(...) + get_http_server(...) || raise(NotFound, 'GetHTTPServer returned no row') + end + + 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') + end + client.exec_prepared('query_sql_truncate_name_cases') + nil + end + end + private def instrument(name) diff --git a/examples/pg/query_sql.rbs b/examples/pg/query_sql.rbs index ac69f99..3809e46 100644 --- a/examples/pg/query_sql.rbs +++ b/examples/pg/query_sql.rbs @@ -232,6 +232,43 @@ module PgCodegen TRUNCATE_EXTRA_TYPES_SQL: String + INSERT_REMAINING_TYPES_SQL: String + + GET_REMAINING_TYPES_SQL: String + + class GetRemainingTypesRow + attr_reader c_hstore: String? + attr_reader c_ltree: String? + attr_reader c_regclass: String? + attr_reader c_cidr: IPAddr? + attr_reader c_macaddr: String? + attr_reader c_interval: String? + attr_reader c_varbit: String? + attr_reader c_xml: String? + attr_reader c_numeric: BigDecimal? + attr_reader c_bigint: Integer? + attr_reader c_timestamp: Time? + attr_reader c_timestamptz: Time? + attr_reader c_text_matrix: Array[Array[String]]? + def initialize: ( + String? c_hstore, + String? c_ltree, + String? c_regclass, + IPAddr? c_cidr, + String? c_macaddr, + String? c_interval, + String? c_varbit, + String? c_xml, + BigDecimal? c_numeric, + Integer? c_bigint, + Time? c_timestamp, + Time? c_timestamptz, + Array[Array[String]]? c_text_matrix + ) -> void + end + + TRUNCATE_REMAINING_TYPES_SQL: String + INSERT_ARRAY_TYPES_SQL: String GET_ARRAY_TYPES_SQL: String @@ -466,6 +503,56 @@ module PgCodegen TRUNCATE_BATCH_TYPES_SQL: String + INSERT_NAME_CASE_SQL: String + + GET_NAME_CASE_SQL: String + + class GetNameCaseRow + attr_reader id: Integer + attr_reader class_: String + attr_reader end: String + attr_reader hash_: String + attr_reader method_: String + attr_reader count: Integer + attr_reader camelCase: String + attr_reader HTTPServer: String + def initialize: ( + Integer id, + String class_, + String end, + String hash_, + String method_, + Integer count, + String camelCase, + String HTTPServer + ) -> void + end + + GET_HTTP_SERVER_SQL: String + + class GetHTTPServerRow + attr_reader id: Integer + attr_reader class_: String + attr_reader end: String + attr_reader hash_: String + attr_reader method_: String + attr_reader count: Integer + attr_reader camelCase: String + attr_reader HTTPServer: String + def initialize: ( + Integer id, + String class_, + String end, + String hash_, + String method_, + Integer count, + String camelCase, + String HTTPServer + ) -> void + end + + TRUNCATE_NAME_CASES_SQL: String + class QuerySql class NotFound < StandardError end @@ -556,6 +643,14 @@ module PgCodegen def truncate_extra_types: -> void + def insert_remaining_types: (c_hstore: String?, c_ltree: String?, c_regclass: String?, c_cidr: IPAddr?, c_macaddr: String?, c_interval: String?, c_varbit: String?, c_xml: String?, c_numeric: BigDecimal?, c_bigint: Integer?, c_timestamp: Time?, c_timestamptz: Time?, c_text_matrix: Array[Array[String]]?) -> void + + def get_remaining_types: -> GetRemainingTypesRow? + + def get_remaining_types!: -> GetRemainingTypesRow + + def truncate_remaining_types: -> void + def insert_array_types: (c_uuid_array: Array[String]?, c_bio_type_array: Array[String]?, c_integer_matrix: Array[Array[Integer]]?) -> void def get_array_types: -> GetArrayTypesRow? @@ -610,6 +705,18 @@ module PgCodegen def truncate_batch_types: -> void + def insert_name_case: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, camel_case: String, http_server: String) -> void + + def get_name_case: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, http_server: String, camel_case: String) -> GetNameCaseRow? + + def get_name_case!: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, http_server: String, camel_case: String) -> GetNameCaseRow + + def get_http_server: (id: Integer) -> GetHTTPServerRow? + + def get_http_server!: (id: Integer) -> GetHTTPServerRow + + def truncate_name_cases: -> void + private def instrument: [T] (String name) { () -> T } -> T diff --git a/examples/sqlite3/models.rb b/examples/sqlite3/models.rb index 365bef7..5a78ef5 100644 --- a/examples/sqlite3/models.rb +++ b/examples/sqlite3/models.rb @@ -25,4 +25,18 @@ class Regression < Data.define(:id, :order, :price, :payload) class Tag < Data.define(:name, :uses) end + + # rubocop:disable Naming/MethodName + class NameCase < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelCase, + :HTTPServer + ) + end + # rubocop:enable Naming/MethodName end diff --git a/examples/sqlite3/models.rbs b/examples/sqlite3/models.rbs index 271946e..4ea1aac 100644 --- a/examples/sqlite3/models.rbs +++ b/examples/sqlite3/models.rbs @@ -47,4 +47,25 @@ module Sqlite3Codegen attr_reader uses: Integer def initialize: ( String name, Integer uses) -> void end + + class NameCase + attr_reader id: Integer + attr_reader class_: String + attr_reader end: String + attr_reader hash_: String + attr_reader method_: String + attr_reader count: Integer + attr_reader camelCase: String + attr_reader HTTPServer: String + def initialize: ( + Integer id, + String class_, + String end, + String hash_, + String method_, + Integer count, + String camelCase, + String HTTPServer + ) -> void + end end diff --git a/examples/sqlite3/query_sql.rb b/examples/sqlite3/query_sql.rb index 29a54c0..8ee07ff 100644 --- a/examples/sqlite3/query_sql.rb +++ b/examples/sqlite3/query_sql.rb @@ -314,6 +314,65 @@ class ListTagsRow < Data.define(:name, :uses) DELETE FROM tags SQL + LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL = <<~'SQL' + SELECT authors.id, authors.name, authors.bio, books.id, books.name, books.author_id, books.description + FROM authors + LEFT JOIN books ON authors.id = books.author_id + ORDER BY authors.id + SQL + + class ListAuthorsWithOptionalBookRow < Data.define(:author, :book) + end + + INSERT_NAME_CASE_SQL = <<~'SQL' + INSERT INTO name_cases (id, "class", "end", "hash", "method", "count", "camelCase", "HTTPServer") + VALUES (?, ?, ?, ?, ?, ?, + ?, ?) + SQL + + GET_NAME_CASE_SQL = <<~'SQL' + SELECT id, class, "end", hash, method, count, camelCase, HTTPServer FROM name_cases + WHERE id = ? AND "class" = ? AND "end" = ? AND "hash" = ? + AND "method" = ? AND "count" = ? + AND "HTTPServer" = ? AND "camelCase" = ? + SQL + + # rubocop:disable Naming/MethodName + class GetNameCaseRow < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelCase, + :HTTPServer + ) + end + # rubocop:enable Naming/MethodName + + GET_HTTP_SERVER_SQL = <<~'SQL' + SELECT id, class, "end", hash, method, count, camelCase, HTTPServer FROM name_cases WHERE id = ? + SQL + + # rubocop:disable Naming/MethodName + class GetHTTPServerRow < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelCase, + :HTTPServer + ) + end + # rubocop:enable Naming/MethodName + + TRUNCATE_NAME_CASES_SQL = <<~'SQL' + DELETE FROM name_cases + SQL + class QuerySql # Raised by the bang variants (get_author!) when the query returns no row. class NotFound < StandardError; end @@ -997,6 +1056,122 @@ def truncate_tags end end + def list_authors_with_optional_book + with_client('list_authors_with_optional_book') do |client| + stmt = client.prepare(LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL) + begin + result = stmt.execute + result.map do |row| + ListAuthorsWithOptionalBookRow.new( + row[0].nil? && row[1].nil? ? nil : Author.new(row[0], row[1], row[2]), + row[3].nil? && row[4].nil? && row[5].nil? ? nil : Book.new(row[3], row[4], row[5], row[6]) + ) + end + ensure + stmt.close + end + end + end + + def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, http_server:) + with_client('insert_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + camel_case, + http_server + ] + stmt = client.prepare(INSERT_NAME_CASE_SQL) + begin + stmt.execute(*query_params) + nil + ensure + stmt.close + end + end + end + + def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, camel_case:) + with_client('get_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + http_server, + camel_case + ] + stmt = client.prepare(GET_NAME_CASE_SQL) + begin + result = stmt.execute(*query_params) + row = result.first + row && GetNameCaseRow.new( + row[0], + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7] + ) + ensure + stmt.close + end + end + end + + # Like #get_name_case, but raises NotFound when there is no row. + def get_name_case!(...) + get_name_case(...) || raise(NotFound, 'GetNameCase returned no row') + end + + def get_http_server(id:) + with_client('get_http_server') do |client| + query_params = [id] + stmt = client.prepare(GET_HTTP_SERVER_SQL) + begin + result = stmt.execute(*query_params) + row = result.first + row && GetHTTPServerRow.new( + row[0], + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7] + ) + ensure + stmt.close + end + end + end + + # Like #get_http_server, but raises NotFound when there is no row. + def get_http_server!(...) + get_http_server(...) || raise(NotFound, 'GetHTTPServer returned no row') + end + + def truncate_name_cases + with_client('truncate_name_cases') do |client| + stmt = client.prepare(TRUNCATE_NAME_CASES_SQL) + begin + stmt.execute + nil + ensure + stmt.close + end + end + end + private def instrument(name) diff --git a/examples/sqlite3/query_sql.rbs b/examples/sqlite3/query_sql.rbs index 1c252ae..581c04e 100644 --- a/examples/sqlite3/query_sql.rbs +++ b/examples/sqlite3/query_sql.rbs @@ -313,6 +313,64 @@ module Sqlite3Codegen TRUNCATE_TAGS_SQL: String + LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL: String + + class ListAuthorsWithOptionalBookRow + attr_reader author: Author? + attr_reader book: Book? + def initialize: ( Author? author, Book? book) -> void + end + + INSERT_NAME_CASE_SQL: String + + GET_NAME_CASE_SQL: String + + class GetNameCaseRow + attr_reader id: Integer + attr_reader class_: String + attr_reader end: String + attr_reader hash_: String + attr_reader method_: String + attr_reader count: Integer + attr_reader camelCase: String + attr_reader HTTPServer: String + def initialize: ( + Integer id, + String class_, + String end, + String hash_, + String method_, + Integer count, + String camelCase, + String HTTPServer + ) -> void + end + + GET_HTTP_SERVER_SQL: String + + class GetHTTPServerRow + attr_reader id: Integer + attr_reader class_: String + attr_reader end: String + attr_reader hash_: String + attr_reader method_: String + attr_reader count: Integer + attr_reader camelCase: String + attr_reader HTTPServer: String + def initialize: ( + Integer id, + String class_, + String end, + String hash_, + String method_, + Integer count, + String camelCase, + String HTTPServer + ) -> void + end + + TRUNCATE_NAME_CASES_SQL: String + class QuerySql class NotFound < StandardError end @@ -419,6 +477,20 @@ module Sqlite3Codegen def truncate_tags: -> void + def list_authors_with_optional_book: -> Array[ListAuthorsWithOptionalBookRow] + + def insert_name_case: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, camel_case: String, http_server: String) -> void + + def get_name_case: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, http_server: String, camel_case: String) -> GetNameCaseRow? + + def get_name_case!: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, http_server: String, camel_case: String) -> GetNameCaseRow + + def get_http_server: (id: Integer) -> GetHTTPServerRow? + + def get_http_server!: (id: Integer) -> GetHTTPServerRow + + def truncate_name_cases: -> void + private def instrument: [T] (String name) { () -> T } -> T diff --git a/tests/end2end_mysql2.rb b/tests/end2end_mysql2.rb index 2d1933c..f8216f3 100755 --- a/tests/end2end_mysql2.rb +++ b/tests/end2end_mysql2.rb @@ -87,6 +87,7 @@ def setup @query_sql.truncate_regressions @query_sql.truncate_copy_types @query_sql.truncate_mysql_binding_values + @query_sql.truncate_name_cases end def create_author(name, bio = nil) @@ -280,6 +281,16 @@ def test_same_table_embedded_twice assert_equal [low, high], [rows.first.author.id, rows.first.author2.id] end + def test_outer_join_embed_is_nil_without_a_match + with_book = create_author('a') + create_author('b') + @query_sql.create_book(name: 'book', author_id: with_book) + rows = @query_sql.list_authors_with_optional_book + assert_equal 'book', rows.first.book.name + assert_equal 'b', rows.last.author.name + assert_nil rows.last.book + end + # ---- enums / sets ---------------------------------------------------------------------------------------------- def test_enum_and_set_columns @@ -290,6 +301,15 @@ def test_enum_and_set_columns assert_equal row_for(GetFirstExtendedBioByTypeRow, ['a', 'n1', 'Memoir', 'Author,Editor']), row assert_equal %w[Autobiography Biography Memoir], ExtendedBiosBioType::ALL assert_equal %w[Author Editor Translator], ExtendedBiosAuthorType::ALL + @query_sql.instance_variable_get(:@db).with do |client| + %w[bio_type author_type].zip([ExtendedBiosBioType::ALL, ExtendedBiosAuthorType::ALL]).each do |column, constants| + column_type = client.query(<<~SQL).first.values.first + SELECT COLUMN_TYPE FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'extended_bios' AND COLUMN_NAME = '#{column}' + SQL + assert_equal constants, column_type.scan(/'((?:''|[^'])*)'/).flatten + end + end end # ---- transactions ---------------------------------------------------------------------------------------------- @@ -388,9 +408,26 @@ def test_named_nullable_arguments_are_optional assert_equal ['abc'], @query_sql.get_author_by_name_pattern.map(&:name) end - def test_transaction_isolation_level_is_accepted - assert_equal 1, @query_sql.transaction(isolation: :serializable) { 1 } + def test_transaction_isolation_level_is_applied + @query_sql.transaction(isolation: :serializable) do |tx| + row = tx.instance_variable_get(:@db).with do |client| + client.query(<<~SQL).first + SELECT ISOLATION_LEVEL, STATE + FROM performance_schema.events_transactions_current + WHERE THREAD_ID = PS_CURRENT_THREAD_ID() + SQL + end + assert_equal ['SERIALIZABLE', 'ACTIVE'], row.values + end assert_raises(KeyError) { @query_sql.transaction(isolation: :bogus) { nil } } + @query_sql.instance_variable_get(:@db).with do |client| + rows = client.query(<<~SQL) + SELECT STATE + FROM performance_schema.events_transactions_current + WHERE THREAD_ID = PS_CURRENT_THREAD_ID() + SQL + assert_equal 'COMMITTED', rows.first.fetch('STATE') + end end def test_queries_can_share_an_injected_pool @@ -474,6 +511,14 @@ def test_client_side_quoting_round_trips_supported_values date_value: Date.new(2024, 2, 3), datetime_value: time, datetime_2: date_time, bool_value: true, nullable_value: nil ) + @query_sql.instance_variable_get(:@db).with do |client| + client.query(<<~SQL) + UPDATE mysql_binding_values + SET bit_value = b'1010101010101010', year_value = 2024, time_value = '12:34:56', + json_unicode = '{"greeting":"Grüße 🙂"}' + WHERE id = 42 + SQL + end row = @query_sql.get_mysql_binding_values(id: 42) assert_equal text, row.text_value assert_equal binary, row.blob_value @@ -486,6 +531,10 @@ def test_client_side_quoting_round_trips_supported_values assert_equal date_time.strftime('%Y-%m-%d %H:%M:%S.%6N'), row.datetime_2.strftime('%Y-%m-%d %H:%M:%S.%6N') assert_equal 1, row.bool_value assert_nil row.nullable_value + assert_equal [0xAA, 0xAA].pack('C*'), row.bit_value + assert_equal 2024, row.year_value + assert_equal Time.utc(2000, 1, 1, 12, 34, 56), row.time_value + assert_equal 'Grüße 🙂', JSON.parse(row.json_unicode).fetch('greeting') @query_sql.insert_mysql_binding_values(**binding_values(43).merge(bool_value: false)) assert_equal 0, @query_sql.get_mysql_binding_values(id: 43).bool_value @@ -529,13 +578,20 @@ def test_bang_finder_raises_not_found assert_nil @query_sql.get_author(id: -1) # the plain finder still returns nil end + def test_every_one_query_has_a_bang_method + methods = File.read(File.join(EXAMPLES_DIR, 'mysql2/query_sql.rbs')).scan(/^\s+def (\w+):.* -> \w+Row\?$/).flatten + refute_empty methods + methods.each { |name| assert_respond_to @query_sql, "#{name}!" } + end + def test_logger_receives_query_names_and_durations io = StringIO.new logged = QuerySql.connect(mysql2_params, logger: Logger.new(io, level: Logger::DEBUG)) - logged.list_authors + logged.get_author(id: -987654321) logged.transaction { |tx| tx.list_authors } - assert_match(/QuerySql#list_authors \(\d+\.\dms\)/, io.string) - assert_match(/QuerySql#transaction \(\d+\.\dms\)/, io.string) + assert_match(/DEBUG -- .*QuerySql#get_author \(\d+\.\dms\)/, io.string) + assert_match(/DEBUG -- .*QuerySql#transaction \(\d+\.\dms\)/, io.string) + refute_includes io.string, '-987654321' @query_sql.list_authors # no logger: nothing is logged, nothing breaks end @@ -561,6 +617,20 @@ def test_repeated_positional_parameters_get_distinct_names assert_equal [[1, 'a'], [3, 'c']], @query_sql.list_regressions_by_ids(id: 1, id_2: 3, id_3: 99).map { |r| [r.id, r.order] } end + def test_keyword_and_method_names_keep_distinct_row_and_argument_names + args = { id: 1, class_: 'class', end_: 'end', hash: 'hash', method: 'method', count: 7, + camel_case: 'camel', http_server: 'server' } + @query_sql.insert_name_case(**args) + row = @query_sql.get_name_case(**args) + count_member = row.is_a?(Struct) ? :count_ : :count + assert_equal %i[id class_ end hash_ method_].concat([count_member, :camelcase, :httpserver]), row.class.members + assert_equal %w[class end hash method], %i[class_ end hash_ method_].map { |member| row.public_send(member) } + assert_equal 7, row.public_send(count_member) + assert_equal 'camel', row.public_send(:camelcase) + assert_equal 'server', row.public_send(:httpserver) + assert_equal 1, @query_sql.get_http_server(id: 1).id + end + def test_copyfrom_keeps_strings_that_look_like_null_and_special_characters notes = ['NULL', '\N', 'a "quoted", value', 'back\\slash', "new\nline", nil] @query_sql.copy_regressions(notes.map { |n| CopyRegressionsArgs.new(order: 'o', note: n) }) diff --git a/tests/end2end_options.rb b/tests/end2end_options.rb new file mode 100644 index 0000000..70f6692 --- /dev/null +++ b/tests/end2end_options.rb @@ -0,0 +1,113 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +gem 'minitest' +require 'minitest/autorun' +require 'fileutils' +require 'connection_pool' +require 'open3' +require 'pg' +require 'rbconfig' +require 'sqlite3' +require 'yaml' + +class TestOptions < Minitest::Test + ROOT = File.expand_path('..', __dir__) + OPTIONS_DIR = File.join(ROOT, 'tests/options') + OUTPUT_DIR = File.join(OPTIONS_DIR, '.generated') + + def test_generator_options_preserve_runtime_values_and_generate_expected_layouts + FileUtils.rm_rf(OUTPUT_DIR) + config_path = File.join(OPTIONS_DIR, ".sqlc-#{Process.pid}.yaml") + begin + config = YAML.safe_load_file(File.join(OPTIONS_DIR, 'sqlc.yaml')) + config.fetch('options').fetch('ruby')['rubyVersion'] = RUBY_VERSION.split('.')[0, 2].join('.') + File.write(config_path, config.to_yaml) + assert system('sqlc', 'generate', '-f', config_path, chdir: ROOT) + sqlite_dir = File.join(OUTPUT_DIR, 'sqlite3') + pg_dir = File.join(OUTPUT_DIR, 'pg') + assert_equal %w[first_sql.rb first_sql.rbs second_sql.rb second_sql.rbs models.rb models.rbs connection.rb connection.rbs + Gemfile].sort, + Dir.children(sqlite_dir).sort + refute Dir.children(pg_dir).any? { |file| file.end_with?('.rbs') } + assert File.file?(File.join(pg_dir, 'Gemfile')) + + models = File.read(File.join(sqlite_dir, 'models.rb')) + %w[Author Articles Authors SQLiteItems].each { |name| assert_model_defined(models, name) } + pg_models = File.read(File.join(pg_dir, 'models.rb')) + %w[GlobalAuthors PgItems].each { |name| assert_model_defined(pg_models, name) } + + rbs = File.read(File.join(sqlite_dir, 'first_sql.rbs')) + assert_match(/memo: Pathname\b/, rbs) + assert_match(/tag: Symbol\b/, rbs) + assert_match(/extra: Date\b/, rbs) + assert_match(/id: String\b/, rbs) + assert_match(/class GetBothModelsRow\b/, rbs) + assert File.file?(File.join(sqlite_dir, 'second_sql.rbs')) + + assert_sqlite_runtime(sqlite_dir) + assert_pg_runtime(pg_dir) + ensure + FileUtils.rm_rf(OUTPUT_DIR) + FileUtils.rm_f(config_path) + end + end + + private + + def assert_model_defined(source, name) + assert_match(/(?:^|\n)\s*(?:#{Regexp.escape(name)}\s*=\s*Struct\.new|class\s+#{Regexp.escape(name)}\s*<\s*Data\.define)/, + source) + end + + def assert_sqlite_runtime(generated_dir) + gemfile = File.join(generated_dir, 'Gemfile') + stdout, stderr, status = Open3.capture3( + { 'BUNDLE_GEMFILE' => gemfile, 'OPTIONS_SQLITE_DIR' => generated_dir, + 'OPTIONS_SCHEMA' => File.join(OPTIONS_DIR, 'sqlite_schema.sql') }, + 'bundle', 'install' + ) + assert status.success?, "generated-Gemfile bundle install failed:\n#{stdout}\n#{stderr}" + stdout, stderr, status = Open3.capture3( + { 'BUNDLE_GEMFILE' => gemfile, 'OPTIONS_SQLITE_DIR' => generated_dir, + 'OPTIONS_SCHEMA' => File.join(OPTIONS_DIR, 'sqlite_schema.sql') }, + 'bundle', 'exec', RbConfig.ruby, '-e', <<~'RUBY' + require File.join(ENV.fetch('OPTIONS_SQLITE_DIR'), 'first_sql') + require File.join(ENV.fetch('OPTIONS_SQLITE_DIR'), 'second_sql') + db = SQLite3::Database.new(':memory:') + db.execute_batch(File.read(ENV.fetch('OPTIONS_SCHEMA'))) + db.execute("INSERT INTO option_items VALUES (1, 'note', 'tag', 'extra')") + db.execute("INSERT INTO author VALUES (1, 'singular')") + db.execute("INSERT INTO authors VALUES (1, 'plural')") + db.execute("INSERT INTO articles VALUES (1, 'article')") + options = Options::SqliteDb + row = options::FirstSql.new(db).get_option_author(id: 1) + raise 'override changed the runtime value' unless [row.id, row.memo, row.tag, row.extra] == [1, 'note', 'tag', 'extra'] + raise 'models were not shared by query files' unless options::SecondSql.new(db).get_option_extra(id: 1).extra == 'extra' + joined = options::FirstSql.new(db).get_both_models(id: 1) + raise 'embedded models were not renamed as expected' unless [joined.author.name, joined.author2.name] == %w[singular plural] + raise 'exactTableNames did not preserve the plural table model' unless options::FirstSql.new(db).list_option_articles.first.title == 'article' + RUBY + ) + assert status.success?, "generated-Gemfile SQLite run failed:\n#{stdout}\n#{stderr}" + end + + def assert_pg_runtime(generated_dir) + params = { + dbname: ENV.fetch('DB_NAME'), host: ENV.fetch('POSTGRES_HOST'), + user: ENV.fetch('DB_USER'), password: ENV.fetch('DB_PASS'), port: ENV['POSTGRES_PORT'] + }.compact + conn = PG.connect(**params) + conn.exec(File.read(File.join(OPTIONS_DIR, 'pg_schema.sql'))) + conn.exec("INSERT INTO options_audit.option_items VALUES (1, 'note', 'tag', 'extra') ON CONFLICT (id) DO UPDATE SET note = EXCLUDED.note") + queries_path = File.join(generated_dir, 'first_sql.rb') + require queries_path + 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] + ensure + pool&.shutdown(&:close) + conn&.exec('DROP SCHEMA IF EXISTS options_audit CASCADE') + conn&.close + end +end diff --git a/tests/end2end_pg.rb b/tests/end2end_pg.rb index 51d0215..e74233f 100755 --- a/tests/end2end_pg.rb +++ b/tests/end2end_pg.rb @@ -85,10 +85,12 @@ def setup @query_sql.truncate_authors # cascades to books @query_sql.truncate_extended_bios @query_sql.truncate_extra_types + @query_sql.truncate_remaining_types @query_sql.truncate_array_types @query_sql.truncate_special_types @query_sql.truncate_regressions @query_sql.truncate_batch_types + @query_sql.truncate_name_cases @query_sql.truncate_json_round_trips @query_sql.truncate_deferred_books end @@ -273,6 +275,20 @@ def test_repeated_column_parameters_get_distinct_names assert_equal [ids[1]], @query_sql.list_authors_between_ids(id: ids[0], id_2: ids[2]).map(&:id) end + def test_keyword_and_method_names_keep_distinct_row_and_argument_names + args = { id: 1, class_: 'class', end_: 'end', hash: 'hash', method: 'method', count: 7, + camel_case: 'camel', http_server: 'server' } + @query_sql.insert_name_case(**args) + row = @query_sql.get_name_case(**args) + count_member = row.is_a?(Struct) ? :count_ : :count + assert_equal %i[id class_ end hash_ method_].concat([count_member, :camelCase, :HTTPServer]), row.class.members + assert_equal %w[class end hash method], %i[class_ end hash_ method_].map { |member| row.public_send(member) } + assert_equal 7, row.public_send(count_member) + assert_equal 'camel', row.public_send(:camelCase) + assert_equal 'server', row.public_send(:HTTPServer) + assert_equal 1, @query_sql.get_http_server(id: 1).id + end + # ---- enums ----------------------------------------------------------------------------------------------------- def test_enum_column @@ -281,6 +297,11 @@ def test_enum_column row = @query_sql.get_first_extended_bio_by_type(bio_type: 'Biography') assert_equal row_for(GetFirstExtendedBioByTypeRow, %w[a n2 Biography]), row assert_equal %w[Autobiography Biography Memoir], ExtendedBioType::ALL + labels = @query_sql.instance_variable_get(:@db).with do |c| + c.exec("SELECT unnest(enum_range(NULL::extended.bio_type))::text").column_values(0) + end + assert_equal labels, ExtendedBioType::ALL + assert_equal 'Memoir', ExtendedBioType::MEMOIR end def test_enum_rejects_unknown_values @@ -423,6 +444,43 @@ def test_extra_types_nulls :c_inet, :c_money) end + def test_remaining_postgresql_types_round_trip + timestamp = Time.new(2024, 2, 3, 4, 5, Rational(6_123_456, 1_000_000), '+05:30') + timestamptz = Time.new(2024, 3, 4, 5, 6, Rational(7_654_321, 1_000_000), '-07:00') + matrix = [['', "a,'b", nil], ["back\\slash", "line\nbreak", '🙂']] + @query_sql.insert_remaining_types( + c_hstore: '"key"=>"value", "quoted"=>"two"', + c_ltree: 'Top.Science.Astronomy', + c_regclass: 'authors', + c_cidr: '192.168.1.0/24', + c_macaddr: '08:00:2b:01:02:03', + c_interval: '1 year 2 mons 3 days 04:05:06.789', + c_varbit: '1010010110', + c_xml: 'Grüße🙂', + c_numeric: BigDecimal('12345678901234567890.1234567890123456789012345678901234567890'), + c_bigint: -9_223_372_036_854_775_808, + c_timestamp: timestamp, + c_timestamptz: timestamptz, + c_text_matrix: matrix + ) + row = @query_sql.get_remaining_types + assert_equal '"key"=>"value", "quoted"=>"two"', row.c_hstore + assert_equal 'Top.Science.Astronomy', row.c_ltree + assert_equal 'authors', row.c_regclass + assert_equal '192.168.1.0', row.c_cidr.to_s + assert_equal 24, row.c_cidr.prefix + assert_equal '08:00:2b:01:02:03', row.c_macaddr + assert_equal '1 year 2 mons 3 days 04:05:06.789', row.c_interval + assert_equal '1010010110', row.c_varbit + assert_equal 'Grüße🙂', row.c_xml + assert_equal BigDecimal('12345678901234567890.1234567890123456789012345678901234567890'), row.c_numeric + assert_equal(-9_223_372_036_854_775_808, row.c_bigint) + assert_equal timestamp.strftime('%Y-%m-%d %H:%M:%S.%6N'), row.c_timestamp.strftime('%Y-%m-%d %H:%M:%S.%6N') + assert_equal timestamptz, row.c_timestamptz + assert_equal matrix, row.c_text_matrix + assert_empty RbsTypes.mismatches(row, File.join(EXAMPLES_DIR, 'pg/query_sql.rbs'), 'GetRemainingTypesRow') + end + def raw_client PG.connect(**pg_params.transform_keys(&:to_sym)) end @@ -508,6 +566,7 @@ def test_transaction_isolation_level assert_equal 'serializable', level end assert_raises(KeyError) { @query_sql.transaction(isolation: :bogus) { nil } } + @query_sql.instance_variable_get(:@db).with { |c| assert_equal PG::PQTRANS_IDLE, c.transaction_status } end def test_queries_can_share_an_injected_pool @@ -553,13 +612,20 @@ def test_bang_finder_raises_not_found assert_nil @query_sql.get_author(id: -1) # the plain finder still returns nil end + def test_every_one_query_has_a_bang_method + methods = File.read(File.join(EXAMPLES_DIR, 'pg/query_sql.rbs')).scan(/^\s+def (\w+):.* -> \w+Row\?$/).flatten + refute_empty methods + methods.each { |name| assert_respond_to @query_sql, "#{name}!" } + end + def test_logger_receives_query_names_and_durations io = StringIO.new logged = QuerySql.connect(pg_params, logger: Logger.new(io, level: Logger::DEBUG)) - logged.list_authors + logged.get_author(id: -987654321) logged.transaction { |tx| tx.list_authors } - assert_match(/QuerySql#list_authors \(\d+\.\dms\)/, io.string) - assert_match(/QuerySql#transaction \(\d+\.\dms\)/, io.string) + assert_match(/DEBUG -- .*QuerySql#get_author \(\d+\.\dms\)/, io.string) + assert_match(/DEBUG -- .*QuerySql#transaction \(\d+\.\dms\)/, io.string) + refute_includes io.string, '-987654321' @query_sql.list_authors # no logger: nothing is logged, nothing breaks end diff --git a/tests/end2end_sqlite3.rb b/tests/end2end_sqlite3.rb index 87ecb44..a9a9f3b 100644 --- a/tests/end2end_sqlite3.rb +++ b/tests/end2end_sqlite3.rb @@ -5,6 +5,9 @@ require 'stringio' require 'sqlite3' require 'timeout' +require 'async' +require 'async/barrier' +require 'tempfile' EXAMPLES_DIR = File.expand_path(ENV.fetch('EXAMPLES_DIR', File.expand_path('../examples', __dir__))).freeze require_relative 'consts' require_relative 'rbs_types' @@ -166,6 +169,20 @@ def test_required_argument_after_optional_named_argument assert_equal ['beta'], @query_sql.get_authors_by_optional_name(lim: 10, name_filter: 'beta').map(&:name) end + def test_keyword_and_method_names_keep_distinct_row_and_argument_names + args = { id: 1, class_: 'class', end_: 'end', hash: 'hash', method: 'method', count: 7, + camel_case: 'camel', http_server: 'server' } + @query_sql.insert_name_case(**args) + row = @query_sql.get_name_case(**args) + count_member = row.is_a?(Struct) ? :count_ : :count + assert_equal %i[id class_ end hash_ method_].concat([count_member, :camelCase, :HTTPServer]), row.class.members + assert_equal %w[class end hash method], %i[class_ end hash_ method_].map { |member| row.public_send(member) } + assert_equal 7, row.public_send(count_member) + assert_equal 'camel', row.public_send(:camelCase) + assert_equal 'server', row.public_send(:HTTPServer) + assert_equal 1, @query_sql.get_http_server(id: 1).id + end + # ---- sqlc.slice ------------------------------------------------------------------------------------------------ def test_slice @@ -297,6 +314,16 @@ def test_same_table_embedded_twice assert_equal [low, high], [rows.first.author.id, rows.first.author2.id] end + def test_outer_join_embed_is_nil_without_a_match + with_book = create_author('a') + create_author('b') + @query_sql.create_book(name: 'book', author_id: with_book) + rows = @query_sql.list_authors_with_optional_book + assert_equal 'book', rows.first.book.name + assert_equal 'b', rows.last.author.name + assert_nil rows.last.book + end + # ---- transactions ---------------------------------------------------------------------------------------------- def test_transaction_commits_and_returns_the_block_value @@ -368,6 +395,43 @@ def test_transaction_mode assert_equal 1, @query_sql.transaction(mode: :immediate) { 1 } end + def test_immediate_and_exclusive_transactions_block_a_second_connection + Tempfile.create('sqlite-lock-test') do |file| + first = SQLite3::Database.new(file.path) + first.execute_batch(SCHEMA) + second = SQLite3::Database.new(file.path) + second.busy_timeout = 0 + query = QuerySql.new(first) + %i[immediate exclusive].each do |mode| + assert_raises(SQLite3::BusyException) do + query.transaction(mode: mode) { second.execute('BEGIN IMMEDIATE') } + end + second.execute('ROLLBACK') if second.transaction_active? + end + ensure + second&.close + first&.close + end + end + + def test_fibers_sharing_a_database_take_turns + results = Sync do + barrier = Async::Barrier.new + tasks = Array.new(8) do |i| + barrier.async do + @query_sql.transaction do |tx| + id = tx.create_author_return_id(name: "fiber #{i}", bio: nil) + tx.get_author(id: id).name + end + end + end + barrier.wait + tasks.map(&:wait) + end + assert_equal (0...8).map { |i| "fiber #{i}" }.sort, results.sort + assert_equal 8, @query_sql.list_authors.size + end + # ---- Ruby conventions: bang finders, logger ---------------------------------------------------------------- def test_bang_finder_raises_not_found @@ -378,13 +442,20 @@ def test_bang_finder_raises_not_found assert_nil @query_sql.get_author(id: -1) # the plain finder still returns nil end + def test_every_one_query_has_a_bang_method + methods = File.read(File.join(EXAMPLES_DIR, 'sqlite3/query_sql.rbs')).scan(/^\s+def (\w+):.* -> \w+Row\?$/).flatten + refute_empty methods + methods.each { |name| assert_respond_to @query_sql, "#{name}!" } + end + def test_logger_receives_query_names_and_durations io = StringIO.new logged = QuerySql.new(@db, logger: Logger.new(io, level: Logger::DEBUG)) - logged.list_authors + logged.get_author(id: -987654321) logged.transaction { |tx| tx.list_authors } - assert_match(/QuerySql#list_authors \(\d+\.\dms\)/, io.string) - assert_match(/QuerySql#transaction \(\d+\.\dms\)/, io.string) + assert_match(/DEBUG -- .*QuerySql#get_author \(\d+\.\dms\)/, io.string) + assert_match(/DEBUG -- .*QuerySql#transaction \(\d+\.\dms\)/, io.string) + refute_includes io.string, '-987654321' @query_sql.list_authors # no logger: nothing is logged, nothing breaks end diff --git a/tests/options/pg_queries/first.sql b/tests/options/pg_queries/first.sql new file mode 100644 index 0000000..a96a540 --- /dev/null +++ b/tests/options/pg_queries/first.sql @@ -0,0 +1,4 @@ +-- name: GetPgOptionAuthor :one +SELECT id, note, tag, extra +FROM options_audit.option_items +WHERE id = sqlc.arg(id); diff --git a/tests/options/pg_schema.sql b/tests/options/pg_schema.sql new file mode 100644 index 0000000..2d18348 --- /dev/null +++ b/tests/options/pg_schema.sql @@ -0,0 +1,19 @@ +DROP SCHEMA IF EXISTS options_audit CASCADE; +CREATE SCHEMA options_audit; + +CREATE TABLE options_audit.author ( + id BIGINT PRIMARY KEY, + name TEXT NOT NULL +); + +CREATE TABLE options_audit.authors ( + id BIGINT PRIMARY KEY, + name TEXT NOT NULL +); + +CREATE TABLE options_audit.option_items ( + id BIGINT PRIMARY KEY, + note TEXT NOT NULL, + tag TEXT NOT NULL, + extra TEXT NOT NULL +); diff --git a/tests/options/sqlc.yaml b/tests/options/sqlc.yaml new file mode 100644 index 0000000..017aea5 --- /dev/null +++ b/tests/options/sqlc.yaml @@ -0,0 +1,72 @@ +version: "2" +plugins: +- name: ruby + process: + cmd: ./exe/plugin.rb + env: + - PATH +options: + ruby: + moduleName: Shared::Defaults + rubyVersion: "3.3" + generateTypes: true + generateGemfile: true + exactTableNames: false + rename: + "options_audit.authors": GlobalAuthors + note: memo + overrides: + - column: "GetOptionAuthor:note" + ruby_type: { type: "Date" } + - column: "option_items.note" + ruby_type: { type: "Float" } + - column: "*:note" + ruby_type: { type: "Time" } + - column: "option_items.tag" + ruby_type: { type: "Symbol" } + - column: "*:tag" + ruby_type: { type: "Integer" } + - column: "*:extra" + ruby_type: { type: "Date" } + - db_type: "text" + ruby_type: { type: "String" } + - db_type: "integer" + ruby_type: { type: "String" } +sql: +- schema: "sqlite_schema.sql" + queries: + - "sqlite_queries/first.sql" + - "sqlite_queries/second.sql" + engine: "sqlite" + codegen: + - plugin: ruby + out: ".generated/sqlite3" + options: + driver: sqlite3 + moduleName: Options::SqliteDb + exactTableNames: true + rename: + authors: Authors + option_items: SQLiteItems + overrides: + - column: "GetOptionAuthor:note" + ruby_type: { type: "Pathname" } + - column: "option_items.note" + ruby_type: { type: "Float" } + - column: "*:note" + ruby_type: { type: "Time" } +- schema: "pg_schema.sql" + queries: + - "pg_queries/first.sql" + engine: "postgresql" + codegen: + - plugin: ruby + out: ".generated/pg" + options: + driver: pg + moduleName: Options::PgDb + exactTableNames: true + preparedStatements: false + generateTypes: false + rename: + "options_audit.option_items": PgItems diff --git a/tests/options/sqlite_queries/first.sql b/tests/options/sqlite_queries/first.sql new file mode 100644 index 0000000..d69951b --- /dev/null +++ b/tests/options/sqlite_queries/first.sql @@ -0,0 +1,13 @@ +-- name: GetOptionAuthor :one +SELECT id, note, tag, extra +FROM option_items +WHERE id = sqlc.arg(id); + +-- name: GetBothModels :one +SELECT sqlc.embed(author), sqlc.embed(authors) +FROM author +JOIN authors USING (id) +WHERE author.id = sqlc.arg(id); + +-- name: ListOptionArticles :many +SELECT * FROM articles ORDER BY id; diff --git a/tests/options/sqlite_queries/second.sql b/tests/options/sqlite_queries/second.sql new file mode 100644 index 0000000..a108224 --- /dev/null +++ b/tests/options/sqlite_queries/second.sql @@ -0,0 +1,4 @@ +-- name: GetOptionExtra :one +SELECT id, extra +FROM option_items +WHERE id = sqlc.arg(id); diff --git a/tests/options/sqlite_schema.sql b/tests/options/sqlite_schema.sql new file mode 100644 index 0000000..a401c13 --- /dev/null +++ b/tests/options/sqlite_schema.sql @@ -0,0 +1,21 @@ +CREATE TABLE author ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL +); + +CREATE TABLE authors ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL +); + +CREATE TABLE option_items ( + id INTEGER PRIMARY KEY, + note TEXT NOT NULL, + tag TEXT NOT NULL, + extra TEXT NOT NULL +); + +CREATE TABLE articles ( + id INTEGER PRIMARY KEY, + title TEXT NOT NULL +); From f0772a6e749575421a792d9950682f013a026a65 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 21:05:06 +0000 Subject: [PATCH 7/9] Document feature coverage and finalize Phase 4 Co-Authored-By: Doron Eli Rachman --- CLAUDE.md | 13 +- README.md | 13 +- docs/Coverage.md | 32 +++ docs/Testing.md | 15 +- steep_expectations.yml | 148 +++++------ test/coverage_map_test.rb | 17 ++ test/features_test.rb | 8 + test/fixtures/mysql2.bin | 141 +++++++++- test/fixtures/pg.bin | 189 +++++++++++++- test/fixtures/sqlite3.bin | Bin 13066 -> 16298 bytes test/golden/mysql2/default/models.rb | 18 +- test/golden/mysql2/default/models.rbs | 31 ++- test/golden/mysql2/default/query_sql.rb | 160 +++++++++++- test/golden/mysql2/default/query_sql.rbs | 82 +++++- test/golden/mysql2/ruby31_gemfile/models.rb | 16 ++ test/golden/mysql2/ruby31_gemfile/models.rbs | 47 +++- .../golden/mysql2/ruby31_gemfile/query_sql.rb | 157 ++++++++++- .../mysql2/ruby31_gemfile/query_sql.rbs | 109 +++++++- test/golden/mysql2/ruby32_notypes/models.rb | 18 +- .../golden/mysql2/ruby32_notypes/query_sql.rb | 160 +++++++++++- test/golden/pg/default/models.rb | 31 +++ test/golden/pg/default/models.rbs | 52 ++++ test/golden/pg/default/query_sql.rb | 246 ++++++++++++++++++ test/golden/pg/default/query_sql.rbs | 107 ++++++++ test/golden/pg/ruby31_gemfile/models.rb | 31 +++ test/golden/pg/ruby31_gemfile/models.rbs | 77 ++++++ test/golden/pg/ruby31_gemfile/query_sql.rb | 246 ++++++++++++++++++ test/golden/pg/ruby31_gemfile/query_sql.rbs | 142 ++++++++++ test/golden/pg/ruby32_notypes/models.rb | 31 +++ test/golden/pg/ruby32_notypes/query_sql.rb | 246 ++++++++++++++++++ test/golden/sqlite3/default/models.rb | 14 + test/golden/sqlite3/default/models.rbs | 21 ++ test/golden/sqlite3/default/query_sql.rb | 175 +++++++++++++ test/golden/sqlite3/default/query_sql.rbs | 72 +++++ test/golden/sqlite3/ruby31_gemfile/models.rb | 14 + test/golden/sqlite3/ruby31_gemfile/models.rbs | 31 +++ .../sqlite3/ruby31_gemfile/query_sql.rb | 174 +++++++++++++ .../sqlite3/ruby31_gemfile/query_sql.rbs | 93 +++++++ test/golden/sqlite3/ruby32_notypes/models.rb | 14 + .../sqlite3/ruby32_notypes/query_sql.rb | 175 +++++++++++++ tests/end2end_mysql2.rb | 6 + tests/end2end_pg.rb | 6 + tests/end2end_sqlite3.rb | 6 + 43 files changed, 3269 insertions(+), 115 deletions(-) create mode 100644 docs/Coverage.md create mode 100644 test/coverage_map_test.rb diff --git a/CLAUDE.md b/CLAUDE.md index 2e9686e..28e2b33 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -31,14 +31,21 @@ UPDATE_GOLDEN=1 bundle exec ruby -Itest test/extended_golden_test.rb # rewrite * **Generated code must stay valid for Ruby 3.1–4.0** (`Struct` below 3.2, `Data` from 3.2) and RuboCop/Steep-clean. * Generated code is plain, idiomatic Ruby (keyword args, `Data` rows, implicit returns, 2-space indent); do not mirror the C# sibling plugin's style. Parameter *values* are never logged. -* Prepared statements are per connection (`ConnectionState`) because the query object is shared by pooled connections. +* PostgreSQL prepared statements are per connection (`ConnectionState`) because the query object is shared by pooled + connections. MySQL uses client-side quoting and `Mysql2::Client#query` instead of server-side prepared statements. +* Every README claim or supported feature needs a real-database E2E test for each applicable engine, listed in + `docs/Coverage.md`. A bug fix starts with an E2E test that fails before the fix; use unit tests only where E2E cannot + reach the behavior (for example option validation or generation errors). +* E2E coverage runs on every supported Ruby and includes full-suite option variants. Update `docs/Coverage.md` and + `docs/Testing.md` with new supported behavior and its test names. * sqlc runs process plugins with an empty env (`env: [PATH]` is needed in `sqlc.local.yaml`) and resolves `out:` relative to the config file. ## Git, CI and releases * Develop on a feature branch, small commits; PRs target `main`. Label PRs (`bug`, `enhancement`, `documentation`; `breaking` for incompatible changes, see `.github/release.yml`). -* CI: `build.yml` (WASM, lint, `sqlc diff`, Steep, option matrix) and `tests.yml` (unit, Ruby 3.1/3.2/3.4/4.0, E2E on real - PostgreSQL/MySQL/SQLite). Both must be green. +* CI: `build.yml` (WASM, lint, `sqlc diff`, Steep, option matrix) and `tests.yml` (unit, compatibility Ruby + 3.1/3.2/3.4/4.0, full E2E Ruby 3.1–4.0, Ruby 4.0 module-name variants and PostgreSQL without prepared statements, + plus generator-options E2E on Ruby 3.1/4.0 against PostgreSQL 16, MySQL 8.3 and SQLite). Both must be green. * **Never create tags or releases by hand and never add release markers to commit messages.** Releases are started by a maintainer with the *Release* workflow (`docs/Releasing.md`); do not trigger it unless explicitly asked. * Tool versions (sqlc, ruby.wasm) are pinned in `scripts/tool-versions.env`; bump them there only. diff --git a/README.md b/README.md index 373f459..9f09c74 100644 --- a/README.md +++ b/README.md @@ -177,10 +177,11 @@ server; duplicate keys and invalid values raise `Mysql2::Error` instead of being The generated SQLite code uses `Monitor`: if you run Steep, add `library "monitor"` to your `Steepfile`. ### Names -* Query method names and keyword arguments are snake_cased, with acronyms kept together (`MIN_ID` → `min_id`, - `HTTPServer` → `http_server`). Ruby keywords and names a method already uses get a trailing `_`. -* Row members retain the column spelling unless they conflict with a generated row method: `"camelCase"` is `camelCase` - on a row but `camel_case:` as an argument; a column named `end` is row member `:end` and argument `end_:`. +* Query methods and keyword arguments are snake_cased, with acronyms kept together (`MIN_ID` → `min_id`, + `HTTPServer` → `http_server`); Ruby keywords get a trailing `_` (`end_:`). Row members use the column spelling + reported by sqlc for the engine, except names that conflict with generated row methods (`class_`, `hash_`): + PostgreSQL and SQLite keep `"camelCase"` as `:camelCase`, while MySQL reports it as `:camelcase`. A column named + `end` is the row member `:end` but the argument `end_:`. * A parameter used twice is one keyword when named with `sqlc.arg`; repeated `?` / `$n` of one column become `id:`, `id_2:`. * A table `authors` next to a table `author` keeps its plural model name (`Authors`). Query names that would replace a generated method (`Transaction`, `Initialize`, ...) are errors. @@ -189,8 +190,8 @@ server; duplicate keys and invalid values raise `Mysql2::Error` instead of being * Two result columns with the same name (e.g. `a.id, b.id`) are an error: alias one (`b.id AS book_id`) or use `sqlc.embed`. * SQLite's `NUMERIC` affinity can return an `Integer` where `Float` is declared. * `:batchexec` / `:batchone` / `:batchmany` are not atomic on their own: wrap them in `transaction` for all-or-nothing. -* SQLite: a transaction belongs to the thread that opened it; a Fiber / `Enumerator#next` inside it that uses the same - database waits for it (with `async`, run the work in the transaction's own fiber). +* SQLite transactions belong to the thread that opened them; fibers sharing the same database take turns, and a + transaction holds it until it ends. * Not applicable to Ruby (they exist in the C# plugin): `withAsyncSuffix` / `withCancellationToken` (use the Fiber scheduler), `useDapper`, target framework / csproj options (`moduleName` replaces the namespace). diff --git a/docs/Coverage.md b/docs/Coverage.md new file mode 100644 index 0000000..20a08d7 --- /dev/null +++ b/docs/Coverage.md @@ -0,0 +1,32 @@ +# Supported feature coverage + +Every README behavior is linked to the real-database test that exercises it for each applicable engine. `unit:` is used +only when the behavior is rejected or decided during generation and generated code cannot reach it. + +| README claim / feature | PostgreSQL | MySQL | SQLite | Unit-only checks | +|---|---|---|---|---| +| Options: driver, Ruby target, types, Gemfile, module name, renames, exact names, overrides and global/local merge | [`test_generator_options_preserve_runtime_values_and_generate_expected_layouts`](../tests/end2end_options.rb#test_generator_options_preserve_runtime_values_and_generate_expected_layouts) | unit: MySQL-specific `preparedStatements` is rejected | [`test_generator_options_preserve_runtime_values_and_generate_expected_layouts`](../tests/end2end_options.rb#test_generator_options_preserve_runtime_values_and_generate_expected_layouts) | [`test_unknown_options_are_rejected_with_a_hint`](../test/options_test.rb#test_unknown_options_are_rejected_with_a_hint), [`test_override_forms`](../test/options_test.rb#test_override_forms) | +| `preparedStatements` disabled and configured `moduleName` | [`test_flow`](../tests/end2end_pg.rb#test_flow) (Ruby 4.0 variant) | [`test_flow`](../tests/end2end_mysql2.rb#test_flow) (Ruby 4.0 variant) | [`test_flow`](../tests/end2end_sqlite3.rb#test_flow) (Ruby 4.0 variant) | [`test_pg_can_skip_prepared_statements`](../test/features_test.rb#test_pg_can_skip_prepared_statements) | +| Generated query/model/connection files and generated `.rbs`; runtime return/attribute checks | [`test_models_are_generated_for_every_table`](../tests/end2end_pg.rb#test_models_are_generated_for_every_table), [`test_declared_types_match_runtime_values`](../tests/end2end_pg.rb#test_declared_types_match_runtime_values) | [`test_models_are_generated_for_every_table`](../tests/end2end_mysql2.rb#test_models_are_generated_for_every_table), [`test_declared_types_match_runtime_values`](../tests/end2end_mysql2.rb#test_declared_types_match_runtime_values) | [`test_models_are_generated_for_every_table`](../tests/end2end_sqlite3.rb#test_models_are_generated_for_every_table), [`test_declared_types_match_runtime_values`](../tests/end2end_sqlite3.rb#test_declared_types_match_runtime_values) | [`test_unknown_types_in_queries_fall_back_to_untyped`](../test/features_test.rb#test_unknown_types_in_queries_fall_back_to_untyped), [`test_models_fall_back_to_untyped_for_unknown_types`](../test/features_test.rb#test_models_fall_back_to_untyped_for_unknown_types) | +| `:one`, `:many`, `:exec`, nil for no row, bang finder methods and `NotFound` error | [`test_flow`](../tests/end2end_pg.rb#test_flow), [`test_one_on_empty_table_returns_nil`](../tests/end2end_pg.rb#test_one_on_empty_table_returns_nil), [`test_every_one_query_has_a_bang_method`](../tests/end2end_pg.rb#test_every_one_query_has_a_bang_method), [`test_bang_finder_raises_not_found`](../tests/end2end_pg.rb#test_bang_finder_raises_not_found) | [`test_flow`](../tests/end2end_mysql2.rb#test_flow), [`test_one_on_empty_table_returns_nil`](../tests/end2end_mysql2.rb#test_one_on_empty_table_returns_nil), [`test_every_one_query_has_a_bang_method`](../tests/end2end_mysql2.rb#test_every_one_query_has_a_bang_method), [`test_bang_finder_raises_not_found`](../tests/end2end_mysql2.rb#test_bang_finder_raises_not_found) | [`test_flow`](../tests/end2end_sqlite3.rb#test_flow), [`test_every_one_query_has_a_bang_method`](../tests/end2end_sqlite3.rb#test_every_one_query_has_a_bang_method), [`test_bang_finder_raises_not_found`](../tests/end2end_sqlite3.rb#test_bang_finder_raises_not_found) | [`test_command_signatures`](../test/features_test.rb#test_command_signatures) | +| `:execrows`, `:execlastid`, `:execresult` semantics | [`test_execrows_returns_affected_row_count`](../tests/end2end_pg.rb#test_execrows_returns_affected_row_count), [`test_execlastid_returns_the_returning_column`](../tests/end2end_pg.rb#test_execlastid_returns_the_returning_column), [`test_execresult_returns_rows_affected`](../tests/end2end_pg.rb#test_execresult_returns_rows_affected) | [`test_execrows_returns_affected_row_count`](../tests/end2end_mysql2.rb#test_execrows_returns_affected_row_count), [`test_execlastid_returns_the_new_id`](../tests/end2end_mysql2.rb#test_execlastid_returns_the_new_id), [`test_execresult_returns_rows_affected_and_last_id`](../tests/end2end_mysql2.rb#test_execresult_returns_rows_affected_and_last_id) | [`test_execrows_returns_affected_row_count`](../tests/end2end_sqlite3.rb#test_execrows_returns_affected_row_count), [`test_execlastid_returns_the_returning_column`](../tests/end2end_sqlite3.rb#test_execlastid_returns_the_returning_column), [`test_execresult_returns_rows_affected_and_last_id`](../tests/end2end_sqlite3.rb#test_execresult_returns_rows_affected_and_last_id) | [`test_pg_and_sqlite_execlastid_need_returning`](../test/features_test.rb#test_pg_and_sqlite_execlastid_need_returning) | +| `:batchexec`, `:batchone`, `:batchmany`; batches use one connection and are not atomic unless wrapped in a transaction | [`test_batch_commands_run_once_per_args`](../tests/end2end_pg.rb#test_batch_commands_run_once_per_args), [`test_batches_are_not_atomic_without_a_transaction`](../tests/end2end_pg.rb#test_batches_are_not_atomic_without_a_transaction) | [`test_batch_commands_run_once_per_args`](../tests/end2end_mysql2.rb#test_batch_commands_run_once_per_args), [`test_batches_are_not_atomic_without_a_transaction`](../tests/end2end_mysql2.rb#test_batches_are_not_atomic_without_a_transaction) | [`test_batch_commands_run_once_per_args`](../tests/end2end_sqlite3.rb#test_batch_commands_run_once_per_args), [`test_batches_are_not_atomic_without_a_transaction`](../tests/end2end_sqlite3.rb#test_batches_are_not_atomic_without_a_transaction) | [`test_batch_commands_take_an_array_of_args`](../test/features_test.rb#test_batch_commands_take_an_array_of_args) | +| `:copyfrom` round trips, all-or-none behavior and conflict clauses | [`test_copyfrom_many_rows_and_none`](../tests/end2end_pg.rb#test_copyfrom_many_rows_and_none), [`test_seeded_random_copyfrom_round_trip_and_duplicate_is_atomic`](../tests/end2end_pg.rb#test_seeded_random_copyfrom_round_trip_and_duplicate_is_atomic) | [`test_copyfrom_loads_binary_values`](../tests/end2end_mysql2.rb#test_copyfrom_loads_binary_values), [`test_seeded_random_copyfrom_round_trip_and_duplicate_is_atomic`](../tests/end2end_mysql2.rb#test_seeded_random_copyfrom_round_trip_and_duplicate_is_atomic) | [`test_copyfrom_is_all_or_nothing`](../tests/end2end_sqlite3.rb#test_copyfrom_is_all_or_nothing), [`test_copyfrom_keeps_conflict_clauses`](../tests/end2end_sqlite3.rb#test_copyfrom_keeps_conflict_clauses) | [`test_copyfrom_keeps_or_rejects_conflict_clauses`](../test/features_test.rb#test_copyfrom_keeps_or_rejects_conflict_clauses) | +| `sqlc.arg`, `sqlc.narg`, positional/named keyword args, optional nils and argument errors | [`test_named_nullable_arguments_are_optional`](../tests/end2end_pg.rb#test_named_nullable_arguments_are_optional), [`test_required_argument_after_optional_named_argument`](../tests/end2end_pg.rb#test_required_argument_after_optional_named_argument), [`test_missing_keyword_arguments_are_errors`](../tests/end2end_pg.rb#test_missing_keyword_arguments_are_errors) | [`test_nullable_named_arg`](../tests/end2end_mysql2.rb#test_nullable_named_arg), [`test_required_argument_after_optional_named_argument`](../tests/end2end_mysql2.rb#test_required_argument_after_optional_named_argument), [`test_missing_keyword_arguments_are_errors`](../tests/end2end_mysql2.rb#test_missing_keyword_arguments_are_errors) | [`test_nullable_named_arg`](../tests/end2end_sqlite3.rb#test_nullable_named_arg), [`test_required_argument_after_optional_named_argument`](../tests/end2end_sqlite3.rb#test_required_argument_after_optional_named_argument), [`test_missing_keyword_arguments_are_errors`](../tests/end2end_sqlite3.rb#test_missing_keyword_arguments_are_errors) | [`test_named_nullable_parameters_are_optional_keywords`](../test/features_test.rb#test_named_nullable_parameters_are_optional_keywords) | +| `sqlc.slice`, placeholder order and repeated parameters | unit: PostgreSQL uses arrays instead of slices | [`test_slice`](../tests/end2end_mysql2.rb#test_slice) | [`test_slice`](../tests/end2end_sqlite3.rb#test_slice), [`test_numbered_placeholders_keep_their_bindings_after_slice_expansion`](../tests/end2end_sqlite3.rb#test_numbered_placeholders_keep_their_bindings_after_slice_expansion), [`test_non_slice_numbered_placeholders_keep_sql_order`](../tests/end2end_sqlite3.rb#test_non_slice_numbered_placeholders_keep_sql_order), [`test_batch_numbered_placeholders_keep_sql_order`](../tests/end2end_sqlite3.rb#test_batch_numbered_placeholders_keep_sql_order) | [`test_slices_are_rejected_for_postgres`](../test/features_test.rb#test_slices_are_rejected_for_postgres) | +| `sqlc.embed`, outer-join nil, same table embedded twice | [`test_embed`](../tests/end2end_pg.rb#test_embed), [`test_embed_on_the_outer_side_of_a_join_is_nil_without_a_match`](../tests/end2end_pg.rb#test_embed_on_the_outer_side_of_a_join_is_nil_without_a_match), [`test_same_table_embedded_twice`](../tests/end2end_pg.rb#test_same_table_embedded_twice) | [`test_embed`](../tests/end2end_mysql2.rb#test_embed), [`test_outer_join_embed_is_nil_without_a_match`](../tests/end2end_mysql2.rb#test_outer_join_embed_is_nil_without_a_match), [`test_same_table_embedded_twice`](../tests/end2end_mysql2.rb#test_same_table_embedded_twice) | [`test_embed`](../tests/end2end_sqlite3.rb#test_embed), [`test_outer_join_embed_is_nil_without_a_match`](../tests/end2end_sqlite3.rb#test_outer_join_embed_is_nil_without_a_match), [`test_same_table_embedded_twice`](../tests/end2end_sqlite3.rb#test_same_table_embedded_twice) | [`test_embedded_tables_become_model_attributes`](../test/features_test.rb#test_embedded_tables_become_model_attributes) | +| Frozen Ruby 3.2+ `Data`, mutable older `Struct`, `with`, `to_h`, pattern matching | [`test_rows_and_embedded_models_have_supported_layout_and_pattern_matching`](../tests/end2end_pg.rb#test_rows_and_embedded_models_have_supported_layout_and_pattern_matching) | [`test_rows_and_embedded_models_have_supported_layout_and_pattern_matching`](../tests/end2end_mysql2.rb#test_rows_and_embedded_models_have_supported_layout_and_pattern_matching) | [`test_rows_and_embedded_models_have_supported_layout_and_pattern_matching`](../tests/end2end_sqlite3.rb#test_rows_and_embedded_models_have_supported_layout_and_pattern_matching) | [`test_struct_classes_are_valid_and_usable`](../test/features_test.rb#test_struct_classes_are_valid_and_usable), [`test_data_classes_are_usable`](../test/features_test.rb#test_data_classes_are_usable) | +| Enums, constants, database labels and MySQL `SET` | [`test_enum_column`](../tests/end2end_pg.rb#test_enum_column) | [`test_enum_and_set_columns`](../tests/end2end_mysql2.rb#test_enum_and_set_columns) | not supported by the SQLite schema | [`test_models_cover_tables_and_enums`](../test/features_test.rb#test_models_cover_tables_and_enums) | +| PostgreSQL arrays, JSON/JSONB, type round trips and RBS | [`test_array_parameters`](../tests/end2end_pg.rb#test_array_parameters), [`test_remaining_postgresql_types_round_trip`](../tests/end2end_pg.rb#test_remaining_postgresql_types_round_trip), [`test_json_and_jsonb_parameters_round_trip_all_json_value_types`](../tests/end2end_pg.rb#test_json_and_jsonb_parameters_round_trip_all_json_value_types) | not PostgreSQL types | not PostgreSQL types | [`test_multi_dimensional_arrays`](../test/features_test.rb#test_multi_dimensional_arrays), [`test_more_postgres_types_composites_and_domains`](../test/features_test.rb#test_more_postgres_types_composites_and_domains) | +| MySQL unsigned integers, BIT, YEAR, DATETIME/TIME, JSON, enums/SET and client quoting | not MySQL | [`test_client_side_quoting_round_trips_supported_values`](../tests/end2end_mysql2.rb#test_client_side_quoting_round_trips_supported_values), [`test_time_values_match_mysql2_prepared_statement_storage`](../tests/end2end_mysql2.rb#test_time_values_match_mysql2_prepared_statement_storage), [`test_declared_types_match_runtime_values`](../tests/end2end_mysql2.rb#test_declared_types_match_runtime_values) | not MySQL | [`test_mysql_uses_client_side_binding_and_sqlite_closes_prepared_statements`](../test/features_test.rb#test_mysql_uses_client_side_binding_and_sqlite_closes_prepared_statements) | +| SQLite affinity, JSON/UUID, and NUMERIC integer/float results | not SQLite | not SQLite | [`test_declared_types_match_runtime_values`](../tests/end2end_sqlite3.rb#test_declared_types_match_runtime_values), [`test_declared_types_with_parameters`](../tests/end2end_sqlite3.rb#test_declared_types_with_parameters) | [`test_sqlite_declared_types_follow_type_affinity`](../test/features_test.rb#test_sqlite_declared_types_follow_type_affinity), [`test_sqlite_json_and_uuid_declarations_are_strings`](../test/features_test.rb#test_sqlite_json_and_uuid_declarations_are_strings) | +| Names: keywords, method collisions, acronyms, quoted mixed case, repeated `$n`, plural models | [`test_keyword_and_method_names_keep_distinct_row_and_argument_names`](../tests/end2end_pg.rb#test_keyword_and_method_names_keep_distinct_row_and_argument_names), [`test_repeated_column_parameters_get_distinct_names`](../tests/end2end_pg.rb#test_repeated_column_parameters_get_distinct_names) | [`test_keyword_and_method_names_keep_distinct_row_and_argument_names`](../tests/end2end_mysql2.rb#test_keyword_and_method_names_keep_distinct_row_and_argument_names), [`test_repeated_positional_parameters_get_distinct_names`](../tests/end2end_mysql2.rb#test_repeated_positional_parameters_get_distinct_names) | [`test_keyword_and_method_names_keep_distinct_row_and_argument_names`](../tests/end2end_sqlite3.rb#test_keyword_and_method_names_keep_distinct_row_and_argument_names) | [`test_snake_case_keeps_acronyms_together`](../test/features_test.rb#test_snake_case_keeps_acronyms_together), [`test_query_names_cannot_replace_generated_methods`](../test/features_test.rb#test_query_names_cannot_replace_generated_methods), [`test_tables_with_the_same_singular_get_distinct_models`](../test/features_test.rb#test_tables_with_the_same_singular_get_distinct_models) | +| Connections, injected pools, reconnect behavior and SQLite serialization | [`test_queries_can_share_an_injected_pool`](../tests/end2end_pg.rb#test_queries_can_share_an_injected_pool), [`test_a_connection_closed_by_the_server_is_reconnected`](../tests/end2end_pg.rb#test_a_connection_closed_by_the_server_is_reconnected) | [`test_queries_can_share_an_injected_pool`](../tests/end2end_mysql2.rb#test_queries_can_share_an_injected_pool), [`test_a_connection_closed_by_the_server_is_replaced`](../tests/end2end_mysql2.rb#test_a_connection_closed_by_the_server_is_replaced) | [`test_fibers_sharing_a_database_take_turns`](../tests/end2end_sqlite3.rb#test_fibers_sharing_a_database_take_turns), [`test_threads_sharing_a_database_take_turns_in_transactions`](../tests/end2end_sqlite3.rb#test_threads_sharing_a_database_take_turns_in_transactions) | [`test_engine_must_match_the_driver`](../test/features_test.rb#test_engine_must_match_the_driver) | +| Transaction result, commit/rollback, nested savepoint, exit semantics and rescued PostgreSQL error | [`test_nested_savepoint_rolls_back_while_outer_transaction_commits`](../tests/end2end_pg.rb#test_nested_savepoint_rolls_back_while_outer_transaction_commits), [`test_transaction_nonlocal_exit_semantics_follow_timeout_version`](../tests/end2end_pg.rb#test_transaction_nonlocal_exit_semantics_follow_timeout_version), [`test_transaction_whose_failed_statement_was_rescued_raises_instead_of_committing`](../tests/end2end_pg.rb#test_transaction_whose_failed_statement_was_rescued_raises_instead_of_committing) | [`test_nested_savepoint_rolls_back_while_outer_transaction_commits`](../tests/end2end_mysql2.rb#test_nested_savepoint_rolls_back_while_outer_transaction_commits), [`test_transaction_nonlocal_exit_semantics_follow_timeout_version`](../tests/end2end_mysql2.rb#test_transaction_nonlocal_exit_semantics_follow_timeout_version) | [`test_nested_transaction_rolls_back_to_a_savepoint`](../tests/end2end_sqlite3.rb#test_nested_transaction_rolls_back_to_a_savepoint), [`test_transaction_nonlocal_exit_semantics_follow_timeout_version`](../tests/end2end_sqlite3.rb#test_transaction_nonlocal_exit_semantics_follow_timeout_version) | [`test_nested_transactions_reject_isolation_and_mode`](../test/features_test.rb#test_nested_transactions_reject_isolation_and_mode) | +| Isolation applied; invalid isolation/mode rejected before begin; SQLite locks | [`test_transaction_isolation_level`](../tests/end2end_pg.rb#test_transaction_isolation_level) | [`test_transaction_isolation_level_is_applied`](../tests/end2end_mysql2.rb#test_transaction_isolation_level_is_applied) | [`test_transaction_mode`](../tests/end2end_sqlite3.rb#test_transaction_mode), [`test_immediate_and_exclusive_transactions_block_a_second_connection`](../tests/end2end_sqlite3.rb#test_immediate_and_exclusive_transactions_block_a_second_connection), [`test_unknown_transaction_mode_is_rejected_before_beginning`](../tests/end2end_sqlite3.rb#test_unknown_transaction_mode_is_rejected_before_beginning) | [`test_nested_transactions_reject_isolation_and_mode`](../test/features_test.rb#test_nested_transactions_reject_isolation_and_mode) | +| Logger debug format, transaction logs, secret-free parameters | [`test_logger_receives_query_names_and_durations`](../tests/end2end_pg.rb#test_logger_receives_query_names_and_durations) | [`test_logger_receives_query_names_and_durations`](../tests/end2end_mysql2.rb#test_logger_receives_query_names_and_durations) | [`test_logger_receives_query_names_and_durations`](../tests/end2end_sqlite3.rb#test_logger_receives_query_names_and_durations) | [`test_logger_support`](../test/features_test.rb#test_logger_support) | +| 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) | + +CI runs `test/coverage_map_test.rb`, which checks every linked test method exists in its referenced `tests/` or `test/` file. diff --git a/docs/Testing.md b/docs/Testing.md index cbe6614..f2b8cbe 100644 --- a/docs/Testing.md +++ b/docs/Testing.md @@ -1,5 +1,10 @@ # Testing and CI +Every README claim and supported feature needs a real-database E2E test for each applicable engine, listed in +[`Coverage.md`](Coverage.md). A bug fix starts with a real-database E2E regression that fails before the fix. Use unit +tests only where E2E cannot reach the behavior, such as option validation or generation errors. CI runs the full E2E +suites on every supported Ruby version plus full-suite option variants; generator-options E2E runs on Ruby 3.1 and 4.0. + There are four layers. Each one answers a different question, and each runs in CI. | Layer | Command | Needs | Question it answers | @@ -40,7 +45,7 @@ They `require` the generated code from `examples/` and run it against a real dat | Workflow | When | What | |---|---|---| | `build.yml` | push to `main`, pull requests (not for Markdown-only changes), release | build `plugin.wasm`; lint; `sqlc diff` with the WASM and the process plugin; Steep; generate with option combinations (Ruby 3.1/3.2/3.3/4.0 × types/Gemfile) and check the output with the matching Ruby | -| `tests.yml` | same | unit tests; the plugin on Ruby 3.1 / 3.2 / 3.4 / 4.0; end-to-end against PostgreSQL 16 and MySQL 8.3 service containers and SQLite (Ruby 3.3 and 4.0) | +| `tests.yml` | same | unit tests; plugin compatibility on Ruby 3.1 / 3.2 / 3.4 / 4.0; full E2E matrix on Ruby 3.1 / 3.2 / 3.3 / 3.4 / 4.0; Ruby 4.0 module-name variants for MySQL and SQLite and module-name + no-prepared-statements variant for PostgreSQL; PostgreSQL 16.2, MySQL 8.3.0, and SQLite; generator-options E2E on Ruby 3.1 / 4.0 | | `docs.yml` | `examples/` change on `main`, manual | regenerates `docs/Examples.md` and opens a PR | | `release.yml` | manual only | see [Releasing](Releasing.md) | @@ -49,6 +54,10 @@ Tool versions (sqlc, ruby.wasm) live in `scripts/tool-versions.env`; dependabot ## Before you push ``` bundle exec rake test rubocop steep -rake sqlc_generate_process && git status --short examples # must show no changes (or commit them) -rake end2end_sqlite3 # plus pg / mysql2 when the generated code changed +sqlc -f sqlc.local.yaml diff # committed examples must be current +scripts/tests/run_codegen.rb sqlc.local.yaml 3.1 true true +rake end2end_sqlite3 end2end_pg end2end_mysql2 # full Ruby 3.1–4.0 matrix, plus Ruby 4.0 variants +bundle exec ruby tests/end2end_options.rb # Ruby 3.1 and 4.0 ``` +CI also runs the unit suite without Bundler on Ruby 3.1 and 4.0. For Ruby 3.1/3.2 E2E, generate scratch examples +with their matching `rubyVersion` and install the generated Gemfile before running the suite. diff --git a/steep_expectations.yml b/steep_expectations.yml index 88890f1..8f0a535 100644 --- a/steep_expectations.yml +++ b/steep_expectations.yml @@ -3,50 +3,50 @@ diagnostics: - range: start: - line: 389 + line: 448 character: 10 end: - line: 389 + line: 448 character: 24 severity: WARNING message: 'Cannot find the declaration of constant: `ConnectionPool`' code: Ruby::UnknownConstant - range: start: - line: 389 + line: 448 character: 47 end: - line: 389 + line: 448 character: 53 severity: WARNING message: 'Cannot find the declaration of constant: `Mysql2`' code: Ruby::UnknownConstant - range: start: - line: 389 + line: 448 character: 66 end: - line: 389 + line: 448 character: 81 severity: WARNING message: Empty kwargs doesn't have type annotation code: Ruby::UnannotatedEmptyCollection - range: start: - line: 1011 + line: 1165 character: 17 end: - line: 1011 + line: 1165 character: 23 severity: WARNING message: 'Cannot find the declaration of constant: `Mysql2`' code: Ruby::UnknownConstant - range: start: - line: 1073 + line: 1227 character: 20 end: - line: 1073 + line: 1227 character: 38 severity: ERROR message: |- @@ -59,20 +59,20 @@ code: Ruby::ArgumentTypeMismatch - range: start: - line: 1074 + line: 1228 character: 78 end: - line: 1074 + line: 1228 character: 84 severity: ERROR message: Type `(::String | nil)` does not have method `match?` code: Ruby::NoMethod - range: start: - line: 1076 + line: 1230 character: 20 end: - line: 1076 + line: 1230 character: 39 severity: ERROR message: |- @@ -85,10 +85,10 @@ code: Ruby::ArgumentTypeMismatch - range: start: - line: 1080 + line: 1234 character: 20 end: - line: 1080 + line: 1234 character: 39 severity: ERROR message: |- @@ -101,10 +101,10 @@ code: Ruby::ArgumentTypeMismatch - range: start: - line: 1085 + line: 1239 character: 20 end: - line: 1085 + line: 1239 character: 39 severity: ERROR message: |- @@ -117,10 +117,10 @@ code: Ruby::ArgumentTypeMismatch - range: start: - line: 1094 + line: 1248 character: 20 end: - line: 1094 + line: 1248 character: 24 severity: ERROR message: |- @@ -133,10 +133,10 @@ code: Ruby::ArgumentTypeMismatch - range: start: - line: 1127 + line: 1281 character: 16 end: - line: 1127 + line: 1281 character: 22 severity: WARNING message: 'Cannot find the declaration of constant: `Mysql2`' @@ -145,260 +145,260 @@ diagnostics: - range: start: - line: 454 + line: 537 character: 10 end: - line: 454 + line: 537 character: 24 severity: WARNING message: 'Cannot find the declaration of constant: `ConnectionPool`' code: Ruby::UnknownConstant - range: start: - line: 454 + line: 537 character: 47 end: - line: 454 + line: 537 character: 49 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 454 + line: 537 character: 58 end: - line: 454 + line: 537 character: 69 severity: WARNING message: Empty kwargs doesn't have type annotation code: Ruby::UnannotatedEmptyCollection - range: start: - line: 469 + line: 552 character: 49 end: - line: 469 + line: 552 character: 51 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 489 + line: 572 character: 51 end: - line: 489 + line: 572 character: 53 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 493 + line: 576 character: 18 end: - line: 493 + line: 576 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 764 + line: 847 character: 18 end: - line: 764 + line: 847 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1193 + line: 1344 character: 18 end: - line: 1193 + line: 1344 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1274 + line: 1425 character: 18 end: - line: 1274 + line: 1425 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1352 + line: 1598 character: 43 end: - line: 1352 + line: 1598 character: 45 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1361 + line: 1607 character: 17 end: - line: 1361 + line: 1607 character: 19 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1364 + line: 1610 character: 16 end: - line: 1364 + line: 1610 character: 18 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1366 + line: 1612 character: 49 end: - line: 1366 + line: 1612 character: 51 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1373 + line: 1619 character: 16 end: - line: 1373 + line: 1619 character: 18 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1374 + line: 1620 character: 15 end: - line: 1374 + line: 1620 character: 17 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1375 + line: 1621 character: 79 end: - line: 1375 + line: 1621 character: 81 severity: WARNING message: Empty array doesn't have type annotation code: Ruby::UnannotatedEmptyCollection - range: start: - line: 1376 + line: 1622 character: 25 end: - line: 1376 + line: 1622 character: 27 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1377 + line: 1623 character: 67 end: - line: 1377 + line: 1623 character: 69 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1385 + line: 1631 character: 15 end: - line: 1385 + line: 1631 character: 17 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1392 + line: 1638 character: 45 end: - line: 1392 + line: 1638 character: 47 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1407 + line: 1653 character: 6 end: - line: 1407 + line: 1653 character: 8 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1407 + line: 1653 character: 48 end: - line: 1407 + line: 1653 character: 50 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1414 + line: 1660 character: 40 end: - line: 1414 + line: 1660 character: 42 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1415 + line: 1661 character: 23 end: - line: 1415 + line: 1661 character: 25 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1416 + line: 1662 character: 42 end: - line: 1416 + line: 1662 character: 44 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1417 + line: 1663 character: 23 end: - line: 1417 + line: 1663 character: 25 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' diff --git a/test/coverage_map_test.rb b/test/coverage_map_test.rb new file mode 100644 index 0000000..745ca5b --- /dev/null +++ b/test/coverage_map_test.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require_relative 'test_helper' + +class CoverageMapTest < Minitest::Test + def test_every_linked_test_method_exists + root = File.expand_path('..', __dir__) + coverage = File.read(File.join(root, 'docs/Coverage.md')) + references = coverage.scan(/\]\(\.\.\/((?:tests|test)\/[^)#]+\.rb)#(test_[a-z0-9_]+)\)/) + refute_empty references + missing = references.filter_map do |file, method| + source = File.read(File.join(root, file)) + "#{file}##{method}" unless source.match?(/\bdef #{Regexp.escape(method)}(?:\s|\()/) + end + assert_empty missing, "missing coverage methods: #{missing.join(', ')}" + end +end diff --git a/test/features_test.rb b/test/features_test.rb index 1377237..5340a3a 100644 --- a/test/features_test.rb +++ b/test/features_test.rb @@ -583,6 +583,14 @@ def test_columns_become_valid_non_overriding_attributes assert_includes generate(request).fetch('query_sql.rb'), 'class GetAuthorRow < Data.define(:id, :weird_name, :hash_)' end + def test_duplicate_result_columns_are_rejected + request = load_request('pg') + columns = query(request, 'GetAuthor').columns + columns << columns.first.dup + error = assert_raises(RuntimeError) { generate(request) } + assert_match(/duplicate column names in GetAuthorRow: id/, error.message) + end + def test_query_method_colliding_with_a_generated_method_is_an_error request = load_request('pg') query(request, 'GetAuthor').name = 'Transaction' diff --git a/test/fixtures/mysql2.bin b/test/fixtures/mysql2.bin index 07156bc..8547fca 100644 --- a/test/fixtures/mysql2.bin +++ b/test/fixtures/mysql2.bin @@ -1,7 +1,7 @@ -2mysqlópublic"èpublic€ +2mysql»!public"°!public€ authors& id0ÿÿÿÿÿÿÿÿÿR authorsbbigint& name0ÿÿÿÿÿÿÿÿÿR authorsbtext# @@ -87,7 +87,7 @@ copy_typesbjson flags0R  copy_typesbbit+ amount0ÿÿÿÿÿÿÿÿÿR  -copy_typesbint€Í +copy_typesbint€Ÿ mysql_binding_values3 id0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbbigint; @@ -109,7 +109,33 @@ datetime_20Rmysql_binding_valuesb datetime1 bool_value0Rmysql_binding_valuesb tinyint; -nullable_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbtext": +nullable_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbtext, + bit_value0Rmysql_binding_valuesbbit7 + +year_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbyear. + +time_value0 +Rmysql_binding_valuesbtime9 + json_unicode0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbjsonó +  +name_cases) +id0ÿÿÿÿÿÿÿÿÿR  +name_casesbbigint* +class0ÿÿÿÿÿÿÿÿÿR  +name_casesbtext( +end0ÿÿÿÿÿÿÿÿÿR  +name_casesbtext) +hash0ÿÿÿÿÿÿÿÿÿR  +name_casesbtext+ +method0ÿÿÿÿÿÿÿÿÿR  +name_casesbtext) +count0ÿÿÿÿÿÿÿÿÿR  +name_casesbint. + camelcase0ÿÿÿÿÿÿÿÿÿR  +name_casesbtext/ + +httpserver0ÿÿÿÿÿÿÿÿÿR  +name_casesbtext": extended_bios_bio_type Autobiography BiographyMemoir"7 extended_bios_author_typeAuthorEditor TranslatorË @@ -333,7 +359,13 @@ HINSERT INTO regressions (id, `order`, note, payload) VALUES (?, ?, ?, ?)CopyR id0ÿÿÿÿÿÿÿÿÿR  regressionsbbigintzid"- order0ÿR  regressionsb varcharzorder". note0ÿÿÿÿÿÿÿÿÿR  regressionsbtextznote"0 -payload0R  regressionsb  varbinaryzpayload: query.sqlC +payload0R  regressionsb  varbinaryzpayload: query.sql« +·SELECT authors.id, authors.name, authors.bio, books.id, books.name, books.author_id, books.description +FROM authors +LEFT JOIN books ON authors.id = books.author_id +ORDER BY authors.idListAuthorsWithOptionalBook:many"! +authors0ÿÿÿÿÿÿÿÿÿbr authors" +books0ÿÿÿÿÿÿÿÿÿbrbooks: query.sqlC TRUNCATE TABLE regressionsTruncateRegressions:exec: query.sql  LINSERT INTO copy_types (id, code, doc, flags, amount) VALUES (?, ?, ?, ?, ?) CopyCopyTypes :copyfrom*95 id0ÿÿÿÿÿÿÿÿÿRpublic @@ -431,8 +463,8 @@ datetime_2*K G bool_value08Rpublicmysql_binding_valuesb tinyintz bool_value*Y U -nullable_value0ÿÿÿÿÿÿÿÿÿ8Rpublicmysql_binding_valuesbtextznullable_value: query.sqlBmysql_binding_valuesÿ -ÆSELECT id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, datetime_value, datetime_2, bool_value, nullable_value FROM mysql_binding_values WHERE id = ?GetMysqlBindingValues:one"7 +nullable_value0ÿÿÿÿÿÿÿÿÿ8Rpublicmysql_binding_valuesbtextznullable_value: query.sqlBmysql_binding_values³ +÷SELECT id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, datetime_value, datetime_2, bool_value, nullable_value, bit_value, year_value, time_value, json_unicode FROM mysql_binding_values WHERE id = ?GetMysqlBindingValues:one"7 id0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbbigintzid"G text_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesb @@ -458,6 +490,99 @@ datetime_2"= bool_value0Rmysql_binding_valuesb tinyintz bool_value"K -nullable_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbtextznullable_value*=9 +nullable_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbtextznullable_value"7 + bit_value0Rmysql_binding_valuesbbitz bit_value"C + +year_value0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbyearz +year_value": + +time_value0 +Rmysql_binding_valuesbtimez +time_value"G + json_unicode0ÿÿÿÿÿÿÿÿÿRmysql_binding_valuesbjsonz json_unicode*=9 id0ÿÿÿÿÿÿÿÿÿ8Rmysql_binding_valuesbbigintzid: query.sqlS -#TRUNCATE TABLE mysql_binding_valuesTruncateMysqlBindingValues:exec: query.sql"v1.27.0*T{"driver":"mysql2","generateGemfile":false,"generateTypes":true,"rubyVersion":"3.3"} \ No newline at end of file +#TRUNCATE TABLE mysql_binding_valuesTruncateMysqlBindingValues:exec: query.sqlÐ +‰INSERT INTO name_cases (id, `class`, `end`, `hash`, `method`, `count`, `camelCase`, `HTTPServer`) +VALUES (?, ?, ?, ?, ?, ?, + ?, ?)InsertNameCase:exec*;7 +id0ÿÿÿÿÿÿÿÿÿ8Rpublic +name_casesbbigintzid*?; +class0ÿÿÿÿÿÿÿÿÿ8Rpublic +name_casesbtextzclass*<8 +end_0ÿÿÿÿÿÿÿÿÿ8Rpublic +name_casesbtextzend*=9 +hash0ÿÿÿÿÿÿÿÿÿ8Rpublic +name_casesbtextzhash*A= +method0ÿÿÿÿÿÿÿÿÿ8Rpublic +name_casesbtextzmethod*>: +count0ÿÿÿÿÿÿÿÿÿ8Rpublic +name_casesbintzcount*HD + +camel_case0ÿÿÿÿÿÿÿÿÿ8Rpublic +name_casesbtextz camelcase*JF + http_server0ÿÿÿÿÿÿÿÿÿ8Rpublic +name_casesbtextz +httpserver: query.sqlB  +name_casesï +ÙSELECT id, class, end, hash, method, count, camelcase, httpserver FROM name_cases +WHERE id = ? AND `class` = ? AND `end` = ? AND `hash` = ? + AND `method` = ? AND `count` = ? + AND `HTTPServer` = ? AND `camelCase` = ? GetNameCase:one"- +id0ÿÿÿÿÿÿÿÿÿR  +name_casesbbigintzid"1 +class0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextzclass"- +end0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextzend"/ +hash0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextzhash"3 +method0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextzmethod"0 +count0ÿÿÿÿÿÿÿÿÿR  +name_casesbintzcount"9 + camelcase0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextz camelcase"; + +httpserver0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextz +httpserver*3/ +id0ÿÿÿÿÿÿÿÿÿ8R  +name_casesbbigintzid*73 +class0ÿÿÿÿÿÿÿÿÿ8R  +name_casesbtextzclass*40 +end_0ÿÿÿÿÿÿÿÿÿ8R  +name_casesbtextzend*51 +hash0ÿÿÿÿÿÿÿÿÿ8R  +name_casesbtextzhash*95 +method0ÿÿÿÿÿÿÿÿÿ8R  +name_casesbtextzmethod*62 +count0ÿÿÿÿÿÿÿÿÿ8R  +name_casesbintzcount*B> + http_server0ÿÿÿÿÿÿÿÿÿ8R  +name_casesbtextz +httpserver*@< + +camel_case0ÿÿÿÿÿÿÿÿÿ8R  +name_casesbtextz camelcase: query.sqlÖ +^SELECT id, class, end, hash, method, count, camelcase, httpserver FROM name_cases WHERE id = ? GetHTTPServer:one"- +id0ÿÿÿÿÿÿÿÿÿR  +name_casesbbigintzid"1 +class0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextzclass"- +end0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextzend"/ +hash0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextzhash"3 +method0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextzmethod"0 +count0ÿÿÿÿÿÿÿÿÿR  +name_casesbintzcount"9 + camelcase0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextz camelcase"; + +httpserver0ÿÿÿÿÿÿÿÿÿR  +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 diff --git a/test/fixtures/pg.bin b/test/fixtures/pg.bin index 6998b15..4300157 100644 --- a/test/fixtures/pg.bin +++ b/test/fixtures/pg.bin @@ -1,7 +1,7 @@  2 -postgresql�!public"ÎpublicÍ +postgresql›+public"Ù(publicÍ authors) id0ÿÿÿÿÿÿÿÿÿR authorsb  bigserial& name0ÿÿÿÿÿÿÿÿÿR authorsbtextH @@ -60,7 +60,28 @@ pg_catalog timestamptz3 c_inet0ÿÿÿÿÿÿÿÿÿRpostgres_extra_typesbinet5 c_money0ÿÿÿÿÿÿÿÿÿRpostgres_extra_typesbmoney9 -c_not_null0ÿÿÿÿÿÿÿÿÿRpostgres_extra_typesbtext» +c_not_null0ÿÿÿÿÿÿÿÿÿRpostgres_extra_typesbtextû +postgres_remaining_types; +c_hstore0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesbhstore9 +c_ltree0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesbltree? + +c_regclass0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesb +regclass7 +c_cidr0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesbcidr= + c_macaddr0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesb macaddrK + +c_interval0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesb +pg_cataloginterval; +c_varbit0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesbvarbit5 +c_xml0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesbxmlI + c_numeric0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesb +pg_catalognumericE +c_bigint0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesb +pg_catalogint8M + c_timestamp0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesb +pg_catalog timestampE + c_timestamptz0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesb  timestamptzC + c_text_matrix 0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesbtextˆ» json_round_trips9 id0ÿÿÿÿÿÿÿÿÿRjson_round_tripsb pg_catalogint43 @@ -104,7 +125,28 @@ pg_catalogbool+ pg_catalogint4ˆ? amounts 0ÿÿÿÿÿÿÿÿÿR  batch_typesb pg_catalognumericˆ6 -stamps 0ÿÿÿÿÿÿÿÿÿR  batch_typesb  timestamptzˆ"´extendedÔ +stamps 0ÿÿÿÿÿÿÿÿÿR  batch_typesb  timestamptzˆŠ +  +name_cases3 +id0ÿÿÿÿÿÿÿÿÿR  +name_casesb +pg_catalogint8* +class0ÿÿÿÿÿÿÿÿÿR  +name_casesbtext( +end0ÿÿÿÿÿÿÿÿÿR  +name_casesbtext) +hash0ÿÿÿÿÿÿÿÿÿR  +name_casesbtext+ +method0ÿÿÿÿÿÿÿÿÿR  +name_casesbtext6 +count0ÿÿÿÿÿÿÿÿÿR  +name_casesb +pg_catalogint4. + camelCase0ÿÿÿÿÿÿÿÿÿR  +name_casesbtext/ + +HTTPServer0ÿÿÿÿÿÿÿÿÿR  +name_casesbtext"´extendedÔ extendedbiosC author_name0ÿÿÿÿÿÿÿÿÿRextendedbiosb pg_catalogvarchar< @@ -315,7 +357,56 @@ pg_catalog timestamptzz c_timestamptz"; c_not_null0ÿÿÿÿÿÿÿÿÿRpostgres_extra_typesbtextz c_not_null: query.sqlK -#TRUNCATE TABLE postgres_extra_typesTruncateExtraTypes:exec: query.sqlÛ +#TRUNCATE TABLE postgres_extra_typesTruncateExtraTypes:exec: query.sqlË +þINSERT INTO postgres_remaining_types ( + c_hstore, c_ltree, c_regclass, c_cidr, c_macaddr, c_interval, c_varbit, c_xml, + c_numeric, c_bigint, c_timestamp, c_timestamptz, c_text_matrix +) VALUES ( + $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13 +)InsertRemainingTypes:exec*QM +c_hstore0ÿÿÿÿÿÿÿÿÿR"publicpostgres_remaining_typesbhstorezc_hstore*NJ +c_ltree0ÿÿÿÿÿÿÿÿÿR"publicpostgres_remaining_typesbltreezc_ltree*WS + +c_regclass0ÿÿÿÿÿÿÿÿÿR"publicpostgres_remaining_typesb +regclassz +c_regclass*KG +c_cidr0ÿÿÿÿÿÿÿÿÿR"publicpostgres_remaining_typesbcidrzc_cidr*TP + c_macaddr0ÿÿÿÿÿÿÿÿÿR"publicpostgres_remaining_typesb macaddrz c_macaddr*b^ + +c_interval0ÿÿÿÿÿÿÿÿÿR"publicpostgres_remaining_typesbpg_catalog.intervalz +c_interval*QM +c_varbit0ÿÿÿÿÿÿÿÿÿR"publicpostgres_remaining_typesbvarbitzc_varbit*HD +c_xml0ÿÿÿÿÿÿÿÿÿR"publicpostgres_remaining_typesbxmlzc_xml*_ [ + c_numeric0ÿÿÿÿÿÿÿÿÿR"publicpostgres_remaining_typesbpg_catalog.numericz c_numeric*Z +V +c_bigint0ÿÿÿÿÿÿÿÿÿR"publicpostgres_remaining_typesbpg_catalog.int8zc_bigint*e a + c_timestamp0ÿÿÿÿÿÿÿÿÿR"publicpostgres_remaining_typesbpg_catalog.timestampz c_timestamp*` \ + c_timestamptz0ÿÿÿÿÿÿÿÿÿR"publicpostgres_remaining_typesb  timestamptzz c_timestamptz*^ Z + c_text_matrix 0ÿÿÿÿÿÿÿÿÿR"publicpostgres_remaining_typesbtextz c_text_matrixˆ: query.sqlBpostgres_remaining_typesÏ +ºSELECT c_hstore, c_ltree, c_regclass, c_cidr, c_macaddr, c_interval, c_varbit, c_xml, c_numeric, c_bigint, c_timestamp, c_timestamptz, c_text_matrix FROM postgres_remaining_types LIMIT 1GetRemainingTypes:one"E +c_hstore0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesbhstorezc_hstore"B +c_ltree0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesbltreezc_ltree"K + +c_regclass0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesb +regclassz +c_regclass"? +c_cidr0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesbcidrzc_cidr"H + c_macaddr0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesb macaddrz c_macaddr"W + +c_interval0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesb +pg_catalogintervalz +c_interval"E +c_varbit0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesbvarbitzc_varbit"< +c_xml0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesbxmlzc_xml"T + c_numeric0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesb +pg_catalognumericz c_numeric"O +c_bigint0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesb +pg_catalogint8zc_bigint"Z + c_timestamp0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesb +pg_catalog timestampz c_timestamp"T + c_timestamptz0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesb  timestamptzz c_timestamptz"R + c_text_matrix 0ÿÿÿÿÿÿÿÿÿRpostgres_remaining_typesbtextz c_text_matrixˆ: query.sqlS +'TRUNCATE TABLE postgres_remaining_typesTruncateRemainingTypes:exec: query.sqlÛ gINSERT INTO postgres_array_types (c_uuid_array, c_bio_type_array, c_integer_matrix) VALUES ($1, $2, $3)InsertArrayTypes:exec*XT c_uuid_array 0ÿÿÿÿÿÿÿÿÿRpublicpostgres_array_typesbuuidz c_uuid_arrayˆ*mi c_bio_type_array 0ÿÿÿÿÿÿÿÿÿRpublicpostgres_array_typesbextended.bio_typezc_bio_type_arrayˆ*kg @@ -465,4 +556,92 @@ pg_catalognumericzamounts .SELECT count(*) FROM batch_types WHERE id > $1CountBatchTypesAbove:one" count0ÿÿÿÿÿÿÿÿÿ@bbigint*A= min_id0ÿÿÿÿÿÿÿÿÿ8R  batch_typesbpg_catalog.int8zid: query.sqlB -TRUNCATE TABLE batch_typesTruncateBatchTypes:exec: query.sql"v1.27.0*P{"driver":"pg","generateGemfile":false,"generateTypes":true,"rubyVersion":"3.3"} \ No newline at end of file +TRUNCATE TABLE batch_typesTruncateBatchTypes:exec: query.sqlí +‘INSERT INTO name_cases (id, "class", "end", "hash", "method", "count", "camelCase", "HTTPServer") +VALUES ($1, $2, $3, $4, $5, $6, + $7, $8)InsertNameCase:exec*D@ +id0ÿÿÿÿÿÿÿÿÿ8Rpublic +name_casesbpg_catalog.int8zid*?; +class0ÿÿÿÿÿÿÿÿÿ8Rpublic +name_casesbtextzclass*<8 +end_0ÿÿÿÿÿÿÿÿÿ8Rpublic +name_casesbtextzend*=9 +hash0ÿÿÿÿÿÿÿÿÿ8Rpublic +name_casesbtextzhash*A= +method0ÿÿÿÿÿÿÿÿÿ8Rpublic +name_casesbtextzmethod*JF +count0ÿÿÿÿÿÿÿÿÿ8Rpublic +name_casesbpg_catalog.int4zcount*HD + +camel_case0ÿÿÿÿÿÿÿÿÿ8Rpublic +name_casesbtextz camelCase*JF + http_server0ÿÿÿÿÿÿÿÿÿ8Rpublic +name_casesbtextz +HTTPServer: query.sqlB  +name_cases© +çSELECT id, class, "end", hash, method, count, "camelCase", "HTTPServer" FROM name_cases +WHERE id = $1 AND "class" = $2 AND "end" = $3 AND "hash" = $4 + AND "method" = $5 AND "count" = $6 + AND "HTTPServer" = $7 AND "camelCase" = $8 GetNameCase:one"7 +id0ÿÿÿÿÿÿÿÿÿR  +name_casesb +pg_catalogint8zid"1 +class0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextzclass"- +end0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextzend"/ +hash0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextzhash"3 +method0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextzmethod"= +count0ÿÿÿÿÿÿÿÿÿR  +name_casesb +pg_catalogint4zcount"9 + camelCase0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextz camelCase"; + +HTTPServer0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextz +HTTPServer*<8 +id0ÿÿÿÿÿÿÿÿÿ8R  +name_casesbpg_catalog.int8zid*73 +class0ÿÿÿÿÿÿÿÿÿ8R  +name_casesbtextzclass*40 +end_0ÿÿÿÿÿÿÿÿÿ8R  +name_casesbtextzend*51 +hash0ÿÿÿÿÿÿÿÿÿ8R  +name_casesbtextzhash*95 +method0ÿÿÿÿÿÿÿÿÿ8R  +name_casesbtextzmethod*B> +count0ÿÿÿÿÿÿÿÿÿ8R  +name_casesbpg_catalog.int4zcount*B> + http_server0ÿÿÿÿÿÿÿÿÿ8R  +name_casesbtextz +HTTPServer*@< + +camel_case0ÿÿÿÿÿÿÿÿÿ8R  +name_casesbtextz camelCase: query.sqlý +eSELECT id, class, "end", hash, method, count, "camelCase", "HTTPServer" FROM name_cases WHERE id = $1 GetHTTPServer:one"7 +id0ÿÿÿÿÿÿÿÿÿR  +name_casesb +pg_catalogint8zid"1 +class0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextzclass"- +end0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextzend"/ +hash0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextzhash"3 +method0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextzmethod"= +count0ÿÿÿÿÿÿÿÿÿR  +name_casesb +pg_catalogint4zcount"9 + camelCase0ÿÿÿÿÿÿÿÿÿR  +name_casesbtextz camelCase"; + +HTTPServer0ÿÿÿÿÿÿÿÿÿR  +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 diff --git a/test/fixtures/sqlite3.bin b/test/fixtures/sqlite3.bin index a68e0e34a44b574b5ada5c9afb27c2edbc9cac99..3251dc72b14030fa8e41c14edfb5e8309b8c619e 100644 GIT binary patch delta 3029 zcmchZ&r{Pt6vrne#jr(Z+=F9-BQX~tlpifnpn&C9_%VaPlpu~5rj$6XLJLiBw3j`2 zbG$em$Nxc3jt8&$2kIl-FMGFlQ@bYE`>Xd zlf#m!>#(p;b-W+SaSD^y(!H#Ir~QrR%?YAAH;5 zpAB`3Un1x`x2>+Li(37p=VASRd^w8N)n%>zB0k$&I>0^aCBvLQg#}lQ-I7_{JT^;} za{h3kQh8bb5#J$aQS^aZ+t^lfngm(el-$fpeWinhB(sFc@B!JNFS^t8ZEYj{Vtd3s zoW;|IyE3^SJ(*wMQMaYOY%(EbQ}B$!GnSA@b{sy^2_)IS>;$wY`|+){vY}Va4e&^J zQ;cTx*Lp$82pmoafScrU*rL=+rU$$lAgas?5j@odAfeE5E$rVOI|ABjdIcItgZ#c1F3gVEq&JKxdk)a}vi^bhk+OOr@;LLxK8e zDabVfg&`-Kih|Wdla0R?bTE*Tzi5`Xb1ZynMBH3I$d%F)%-Nb;!> zz6ny`iiUV8F(5`0UDqrIj6Zq=HzDr;_`k(~H14MMId{!sFxdw8X{wZbr0}Pi?UjP6A delta 52 zcmZ2g-<2lG#m&WNB*a!+n3GwODm05jh$S~MGf!zE$3{sZ7M8P2TxOFm@+odU%^IaQ I*~>lw0EP+=W&i*H diff --git a/test/golden/mysql2/default/models.rb b/test/golden/mysql2/default/models.rb index f1181dd..444d3df 100644 --- a/test/golden/mysql2/default/models.rb +++ b/test/golden/mysql2/default/models.rb @@ -92,8 +92,24 @@ class MysqlBindingValu < Data.define( :datetime_value, :datetime_2, :bool_value, - :nullable_value + :nullable_value, + :bit_value, + :year_value, + :time_value, + :json_unicode ) end # rubocop:enable Naming/VariableNumber + + class NameCase < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelcase, + :httpserver + ) + end end diff --git a/test/golden/mysql2/default/models.rbs b/test/golden/mysql2/default/models.rbs index 1888981..224d903 100644 --- a/test/golden/mysql2/default/models.rbs +++ b/test/golden/mysql2/default/models.rbs @@ -152,6 +152,10 @@ module Mysql2Codegen attr_reader datetime_2: Time? attr_reader bool_value: Integer? attr_reader nullable_value: String? + attr_reader bit_value: String? + attr_reader year_value: Integer? + attr_reader time_value: Time? + attr_reader json_unicode: String? def initialize: ( Integer id, String? text_value, @@ -164,7 +168,32 @@ module Mysql2Codegen Time? datetime_value, Time? datetime_2, Integer? bool_value, - String? nullable_value + String? nullable_value, + String? bit_value, + Integer? year_value, + Time? time_value, + String? json_unicode + ) -> void + end + + class NameCase + attr_reader id: Integer + attr_reader class_: String + attr_reader end: String + attr_reader hash_: String + attr_reader method_: String + attr_reader count: Integer + attr_reader camelcase: String + attr_reader httpserver: String + def initialize: ( + Integer id, + String class_, + String end, + String hash_, + String method_, + Integer count, + String camelcase, + String httpserver ) -> void end end diff --git a/test/golden/mysql2/default/query_sql.rb b/test/golden/mysql2/default/query_sql.rb index 3985144..39d167b 100644 --- a/test/golden/mysql2/default/query_sql.rb +++ b/test/golden/mysql2/default/query_sql.rb @@ -271,6 +271,16 @@ class ListRegressionPayloadsRow < Data.define(:id, :payload) class ListRegressionRowsRow < Data.define(:id, :order, :note, :payload) end + LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL = <<~'SQL' + SELECT authors.id, authors.name, authors.bio, books.id, books.name, books.author_id, books.description + FROM authors + LEFT JOIN books ON authors.id = books.author_id + ORDER BY authors.id + SQL + + class ListAuthorsWithOptionalBookRow < Data.define(:author, :book) + end + TRUNCATE_REGRESSIONS_SQL = <<~'SQL' TRUNCATE TABLE regressions SQL @@ -354,7 +364,7 @@ class GetAuthorWithQuestionMarksRow < Data.define(:id) SQL GET_MYSQL_BINDING_VALUES_SQL = <<~'SQL' - SELECT id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, datetime_value, datetime_2, bool_value, nullable_value FROM mysql_binding_values WHERE id = ? + SELECT id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, datetime_value, datetime_2, bool_value, nullable_value, bit_value, year_value, time_value, json_unicode FROM mysql_binding_values WHERE id = ? SQL # rubocop:disable Naming/VariableNumber @@ -370,7 +380,11 @@ class GetMysqlBindingValuesRow < Data.define( :datetime_value, :datetime_2, :bool_value, - :nullable_value + :nullable_value, + :bit_value, + :year_value, + :time_value, + :json_unicode ) end # rubocop:enable Naming/VariableNumber @@ -379,6 +393,51 @@ class GetMysqlBindingValuesRow < Data.define( TRUNCATE TABLE mysql_binding_values SQL + INSERT_NAME_CASE_SQL = <<~'SQL' + INSERT INTO name_cases (id, `class`, `end`, `hash`, `method`, `count`, `camelCase`, `HTTPServer`) + VALUES (?, ?, ?, ?, ?, ?, + ?, ?) + SQL + + GET_NAME_CASE_SQL = <<~'SQL' + SELECT id, class, end, hash, method, count, camelcase, httpserver FROM name_cases + WHERE id = ? AND `class` = ? AND `end` = ? AND `hash` = ? + AND `method` = ? AND `count` = ? + AND `HTTPServer` = ? AND `camelCase` = ? + SQL + + class GetNameCaseRow < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelcase, + :httpserver + ) + end + + GET_HTTP_SERVER_SQL = <<~'SQL' + SELECT id, class, end, hash, method, count, camelcase, httpserver FROM name_cases WHERE id = ? + SQL + + class GetHTTPServerRow < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelcase, + :httpserver + ) + end + + TRUNCATE_NAME_CASES_SQL = <<~'SQL' + TRUNCATE TABLE name_cases + SQL + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -847,6 +906,18 @@ def list_regression_rows end end + def list_authors_with_optional_book + with_client('list_authors_with_optional_book') do |client| + result = client.query(bind_params(client, LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL, []), as: :array) + result.map do |row| + ListAuthorsWithOptionalBookRow.new( + row[0].nil? && row[1].nil? ? nil : Author.new(row[0], row[1], row[2]), + row[3].nil? && row[4].nil? && row[5].nil? ? nil : Book.new(row[3], row[4], row[5], row[6]) + ) + end + end + end + def truncate_regressions with_client('truncate_regressions') do |client| client.query(bind_params(client, TRUNCATE_REGRESSIONS_SQL, [])) @@ -1001,7 +1072,11 @@ def get_mysql_binding_values(id:) row[8], row[9], row[10], - row[11] + row[11], + row[12], + row[13], + row[14], + row[15] ) end end @@ -1018,6 +1093,85 @@ def truncate_mysql_binding_values end end + def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, http_server:) + with_client('insert_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + camel_case, + http_server + ] + client.query(bind_params(client, INSERT_NAME_CASE_SQL, query_params)) + nil + end + end + + def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, camel_case:) + with_client('get_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + http_server, + camel_case + ] + result = client.query(bind_params(client, GET_NAME_CASE_SQL, query_params), as: :array) + row = result.first + row && GetNameCaseRow.new( + row[0], + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7] + ) + end + end + + # Like #get_name_case, but raises NotFound when there is no row. + def get_name_case!(...) + get_name_case(...) || raise(NotFound, 'GetNameCase returned no row') + end + + def get_http_server(id:) + with_client('get_http_server') do |client| + query_params = [id] + result = client.query(bind_params(client, GET_HTTP_SERVER_SQL, query_params), as: :array) + row = result.first + row && GetHTTPServerRow.new( + row[0], + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7] + ) + end + end + + # Like #get_http_server, but raises NotFound when there is no row. + def get_http_server!(...) + get_http_server(...) || raise(NotFound, 'GetHTTPServer returned no row') + end + + def truncate_name_cases + with_client('truncate_name_cases') do |client| + client.query(bind_params(client, TRUNCATE_NAME_CASES_SQL, [])) + nil + end + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ diff --git a/test/golden/mysql2/default/query_sql.rbs b/test/golden/mysql2/default/query_sql.rbs index 6bccdc0..615543b 100644 --- a/test/golden/mysql2/default/query_sql.rbs +++ b/test/golden/mysql2/default/query_sql.rbs @@ -297,6 +297,14 @@ module Mysql2Codegen def initialize: ( Integer id, String order, String? note, String? payload) -> void end + LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL: String + + class ListAuthorsWithOptionalBookRow + attr_reader author: Author? + attr_reader book: Book? + def initialize: ( Author? author, Book? book) -> void + end + TRUNCATE_REGRESSIONS_SQL: String class CopyCopyTypesArgs @@ -399,6 +407,10 @@ module Mysql2Codegen attr_reader datetime_2: Time? attr_reader bool_value: Integer? attr_reader nullable_value: String? + attr_reader bit_value: String? + attr_reader year_value: Integer? + attr_reader time_value: Time? + attr_reader json_unicode: String? def initialize: ( Integer id, String? text_value, @@ -411,12 +423,66 @@ module Mysql2Codegen Time? datetime_value, Time? datetime_2, Integer? bool_value, - String? nullable_value + String? nullable_value, + String? bit_value, + Integer? year_value, + Time? time_value, + String? json_unicode ) -> void end TRUNCATE_MYSQL_BINDING_VALUES_SQL: String + INSERT_NAME_CASE_SQL: String + + GET_NAME_CASE_SQL: String + + class GetNameCaseRow + attr_reader id: Integer + attr_reader class_: String + attr_reader end: String + attr_reader hash_: String + attr_reader method_: String + attr_reader count: Integer + attr_reader camelcase: String + attr_reader httpserver: String + def initialize: ( + Integer id, + String class_, + String end, + String hash_, + String method_, + Integer count, + String camelcase, + String httpserver + ) -> void + end + + GET_HTTP_SERVER_SQL: String + + class GetHTTPServerRow + attr_reader id: Integer + attr_reader class_: String + attr_reader end: String + attr_reader hash_: String + attr_reader method_: String + attr_reader count: Integer + attr_reader camelcase: String + attr_reader httpserver: String + def initialize: ( + Integer id, + String class_, + String end, + String hash_, + String method_, + Integer count, + String camelcase, + String httpserver + ) -> void + end + + TRUNCATE_NAME_CASES_SQL: String + MANY_PARAMS_SQL: String class ManyParamsRow @@ -542,6 +608,8 @@ module Mysql2Codegen def list_regression_rows: -> Array[ListRegressionRowsRow] + def list_authors_with_optional_book: -> Array[ListAuthorsWithOptionalBookRow] + def truncate_regressions: -> void def copy_copy_types: (Array[CopyCopyTypesArgs] rows) -> void @@ -570,6 +638,18 @@ module Mysql2Codegen def truncate_mysql_binding_values: -> void + def insert_name_case: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, camel_case: String, http_server: String) -> void + + def get_name_case: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, http_server: String, camel_case: String) -> GetNameCaseRow? + + def get_name_case!: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, http_server: String, camel_case: String) -> GetNameCaseRow + + def get_http_server: (id: Integer) -> GetHTTPServerRow? + + def get_http_server!: (id: Integer) -> GetHTTPServerRow + + def truncate_name_cases: -> 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/mysql2/ruby31_gemfile/models.rb b/test/golden/mysql2/ruby31_gemfile/models.rb index 772053a..d1899e5 100644 --- a/test/golden/mysql2/ruby31_gemfile/models.rb +++ b/test/golden/mysql2/ruby31_gemfile/models.rb @@ -107,7 +107,23 @@ module ExtendedBiosAuthorType :datetime_2, :bool_value, :nullable_value, + :bit_value, + :year_value, + :time_value, + :json_unicode, keyword_init: true ) # rubocop:enable Naming/VariableNumber + + NameCase = Struct.new( + :id, + :class_, + :end, + :hash_, + :method_, + :count_, + :camelcase, + :httpserver, + keyword_init: true + ) end diff --git a/test/golden/mysql2/ruby31_gemfile/models.rbs b/test/golden/mysql2/ruby31_gemfile/models.rbs index 77b4ead..d782060 100644 --- a/test/golden/mysql2/ruby31_gemfile/models.rbs +++ b/test/golden/mysql2/ruby31_gemfile/models.rbs @@ -202,6 +202,10 @@ module Mysql2Codegen attr_accessor datetime_2: Time? attr_accessor bool_value: Integer? attr_accessor nullable_value: String? + attr_accessor bit_value: String? + attr_accessor year_value: Integer? + attr_accessor time_value: Time? + attr_accessor json_unicode: String? def initialize: ( id: Integer, text_value: String?, @@ -214,7 +218,11 @@ module Mysql2Codegen datetime_value: Time?, datetime_2: Time?, bool_value: Integer?, - nullable_value: String? + nullable_value: String?, + bit_value: String?, + year_value: Integer?, + time_value: Time?, + json_unicode: String? ) -> void def self.new: ( id: Integer, @@ -228,7 +236,42 @@ module Mysql2Codegen datetime_value: Time?, datetime_2: Time?, bool_value: Integer?, - nullable_value: String? + nullable_value: String?, + bit_value: String?, + year_value: Integer?, + time_value: Time?, + json_unicode: String? ) -> MysqlBindingValu end + + class NameCase < ::Struct[untyped] + attr_accessor id: Integer + attr_accessor class_: String + attr_accessor end: String + attr_accessor hash_: String + attr_accessor method_: String + attr_accessor count_: Integer + attr_accessor camelcase: String + attr_accessor httpserver: String + def initialize: ( + id: Integer, + class_: String, + end: String, + hash_: String, + method_: String, + count_: Integer, + camelcase: String, + httpserver: String + ) -> void + def self.new: ( + id: Integer, + class_: String, + end: String, + hash_: String, + method_: String, + count_: Integer, + camelcase: String, + httpserver: String + ) -> NameCase + end end diff --git a/test/golden/mysql2/ruby31_gemfile/query_sql.rb b/test/golden/mysql2/ruby31_gemfile/query_sql.rb index f57a12c..20394f5 100644 --- a/test/golden/mysql2/ruby31_gemfile/query_sql.rb +++ b/test/golden/mysql2/ruby31_gemfile/query_sql.rb @@ -271,6 +271,15 @@ module Mysql2Codegen keyword_init: true ) + LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL = <<~'SQL' + SELECT authors.id, authors.name, authors.bio, books.id, books.name, books.author_id, books.description + FROM authors + LEFT JOIN books ON authors.id = books.author_id + ORDER BY authors.id + SQL + + ListAuthorsWithOptionalBookRow = Struct.new(:author, :book, keyword_init: true) + TRUNCATE_REGRESSIONS_SQL = <<~'SQL' TRUNCATE TABLE regressions SQL @@ -353,7 +362,7 @@ module Mysql2Codegen SQL GET_MYSQL_BINDING_VALUES_SQL = <<~'SQL' - SELECT id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, datetime_value, datetime_2, bool_value, nullable_value FROM mysql_binding_values WHERE id = ? + SELECT id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, datetime_value, datetime_2, bool_value, nullable_value, bit_value, year_value, time_value, json_unicode FROM mysql_binding_values WHERE id = ? SQL # rubocop:disable Naming/VariableNumber @@ -370,6 +379,10 @@ module Mysql2Codegen :datetime_2, :bool_value, :nullable_value, + :bit_value, + :year_value, + :time_value, + :json_unicode, keyword_init: true ) # rubocop:enable Naming/VariableNumber @@ -378,6 +391,51 @@ module Mysql2Codegen TRUNCATE TABLE mysql_binding_values SQL + INSERT_NAME_CASE_SQL = <<~'SQL' + INSERT INTO name_cases (id, `class`, `end`, `hash`, `method`, `count`, `camelCase`, `HTTPServer`) + VALUES (?, ?, ?, ?, ?, ?, + ?, ?) + SQL + + GET_NAME_CASE_SQL = <<~'SQL' + SELECT id, class, end, hash, method, count, camelcase, httpserver FROM name_cases + WHERE id = ? AND `class` = ? AND `end` = ? AND `hash` = ? + AND `method` = ? AND `count` = ? + AND `HTTPServer` = ? AND `camelCase` = ? + SQL + + GetNameCaseRow = Struct.new( + :id, + :class_, + :end, + :hash_, + :method_, + :count_, + :camelcase, + :httpserver, + keyword_init: true + ) + + GET_HTTP_SERVER_SQL = <<~'SQL' + SELECT id, class, end, hash, method, count, camelcase, httpserver FROM name_cases WHERE id = ? + SQL + + GetHTTPServerRow = Struct.new( + :id, + :class_, + :end, + :hash_, + :method_, + :count_, + :camelcase, + :httpserver, + keyword_init: true + ) + + TRUNCATE_NAME_CASES_SQL = <<~'SQL' + TRUNCATE TABLE name_cases + SQL + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -865,6 +923,18 @@ def list_regression_rows end end + def list_authors_with_optional_book + with_client('list_authors_with_optional_book') do |client| + result = client.query(bind_params(client, LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL, []), as: :array) + 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]), + book: row[3].nil? && row[4].nil? && row[5].nil? ? nil : Book.new(id: row[3], name: row[4], author_id: row[5], description: row[6]) + ) + end + end + end + def truncate_regressions with_client('truncate_regressions') do |client| client.query(bind_params(client, TRUNCATE_REGRESSIONS_SQL, [])) @@ -1019,7 +1089,11 @@ def get_mysql_binding_values(id:) datetime_value: row[8], datetime_2: row[9], bool_value: row[10], - nullable_value: row[11] + nullable_value: row[11], + bit_value: row[12], + year_value: row[13], + time_value: row[14], + json_unicode: row[15] ) end end @@ -1036,6 +1110,85 @@ def truncate_mysql_binding_values end end + def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, http_server:) + with_client('insert_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + camel_case, + http_server + ] + client.query(bind_params(client, INSERT_NAME_CASE_SQL, query_params)) + nil + end + end + + def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, camel_case:) + with_client('get_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + http_server, + camel_case + ] + result = client.query(bind_params(client, GET_NAME_CASE_SQL, query_params), as: :array) + row = result.first + row && GetNameCaseRow.new( + id: row[0], + class_: row[1], + end: row[2], + hash_: row[3], + method_: row[4], + count_: row[5], + camelcase: row[6], + httpserver: row[7] + ) + end + end + + # Like #get_name_case, but raises NotFound when there is no row. + def get_name_case!(...) + get_name_case(...) || raise(NotFound, 'GetNameCase returned no row') + end + + def get_http_server(id:) + with_client('get_http_server') do |client| + query_params = [id] + result = client.query(bind_params(client, GET_HTTP_SERVER_SQL, query_params), as: :array) + row = result.first + row && GetHTTPServerRow.new( + id: row[0], + class_: row[1], + end: row[2], + hash_: row[3], + method_: row[4], + count_: row[5], + camelcase: row[6], + httpserver: row[7] + ) + end + end + + # Like #get_http_server, but raises NotFound when there is no row. + def get_http_server!(...) + get_http_server(...) || raise(NotFound, 'GetHTTPServer returned no row') + end + + def truncate_name_cases + with_client('truncate_name_cases') do |client| + client.query(bind_params(client, TRUNCATE_NAME_CASES_SQL, [])) + nil + end + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ diff --git a/test/golden/mysql2/ruby31_gemfile/query_sql.rbs b/test/golden/mysql2/ruby31_gemfile/query_sql.rbs index f60e8dc..dcfc87f 100644 --- a/test/golden/mysql2/ruby31_gemfile/query_sql.rbs +++ b/test/golden/mysql2/ruby31_gemfile/query_sql.rbs @@ -360,6 +360,15 @@ module Mysql2Codegen def self.new: ( id: Integer, order: String, note: String?, payload: String?) -> ListRegressionRowsRow end + LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL: String + + class ListAuthorsWithOptionalBookRow < ::Struct[untyped] + attr_accessor author: Author? + attr_accessor book: Book? + def initialize: ( author: Author?, book: Book?) -> void + def self.new: ( author: Author?, book: Book?) -> ListAuthorsWithOptionalBookRow + end + TRUNCATE_REGRESSIONS_SQL: String class CopyCopyTypesArgs < ::Struct[untyped] @@ -483,6 +492,10 @@ module Mysql2Codegen attr_accessor datetime_2: Time? attr_accessor bool_value: Integer? attr_accessor nullable_value: String? + attr_accessor bit_value: String? + attr_accessor year_value: Integer? + attr_accessor time_value: Time? + attr_accessor json_unicode: String? def initialize: ( id: Integer, text_value: String?, @@ -495,7 +508,11 @@ module Mysql2Codegen datetime_value: Time?, datetime_2: Time?, bool_value: Integer?, - nullable_value: String? + nullable_value: String?, + bit_value: String?, + year_value: Integer?, + time_value: Time?, + json_unicode: String? ) -> void def self.new: ( id: Integer, @@ -509,12 +526,86 @@ module Mysql2Codegen datetime_value: Time?, datetime_2: Time?, bool_value: Integer?, - nullable_value: String? + nullable_value: String?, + bit_value: String?, + year_value: Integer?, + time_value: Time?, + json_unicode: String? ) -> GetMysqlBindingValuesRow end TRUNCATE_MYSQL_BINDING_VALUES_SQL: String + INSERT_NAME_CASE_SQL: String + + GET_NAME_CASE_SQL: String + + class GetNameCaseRow < ::Struct[untyped] + attr_accessor id: Integer + attr_accessor class_: String + attr_accessor end: String + attr_accessor hash_: String + attr_accessor method_: String + attr_accessor count_: Integer + attr_accessor camelcase: String + attr_accessor httpserver: String + def initialize: ( + id: Integer, + class_: String, + end: String, + hash_: String, + method_: String, + count_: Integer, + camelcase: String, + httpserver: String + ) -> void + def self.new: ( + id: Integer, + class_: String, + end: String, + hash_: String, + method_: String, + count_: Integer, + camelcase: String, + httpserver: String + ) -> GetNameCaseRow + end + + GET_HTTP_SERVER_SQL: String + + class GetHTTPServerRow < ::Struct[untyped] + attr_accessor id: Integer + attr_accessor class_: String + attr_accessor end: String + attr_accessor hash_: String + attr_accessor method_: String + attr_accessor count_: Integer + attr_accessor camelcase: String + attr_accessor httpserver: String + def initialize: ( + id: Integer, + class_: String, + end: String, + hash_: String, + method_: String, + count_: Integer, + camelcase: String, + httpserver: String + ) -> void + def self.new: ( + id: Integer, + class_: String, + end: String, + hash_: String, + method_: String, + count_: Integer, + camelcase: String, + httpserver: String + ) -> GetHTTPServerRow + end + + TRUNCATE_NAME_CASES_SQL: String + MANY_PARAMS_SQL: String class ManyParamsRow < ::Struct[untyped] @@ -650,6 +741,8 @@ module Mysql2Codegen def list_regression_rows: -> Array[ListRegressionRowsRow] + def list_authors_with_optional_book: -> Array[ListAuthorsWithOptionalBookRow] + def truncate_regressions: -> void def copy_copy_types: (Array[CopyCopyTypesArgs] rows) -> void @@ -678,6 +771,18 @@ module Mysql2Codegen def truncate_mysql_binding_values: -> void + def insert_name_case: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, camel_case: String, http_server: String) -> void + + def get_name_case: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, http_server: String, camel_case: String) -> GetNameCaseRow? + + def get_name_case!: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, http_server: String, camel_case: String) -> GetNameCaseRow + + def get_http_server: (id: Integer) -> GetHTTPServerRow? + + def get_http_server!: (id: Integer) -> GetHTTPServerRow + + def truncate_name_cases: -> 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/mysql2/ruby32_notypes/models.rb b/test/golden/mysql2/ruby32_notypes/models.rb index f1181dd..444d3df 100644 --- a/test/golden/mysql2/ruby32_notypes/models.rb +++ b/test/golden/mysql2/ruby32_notypes/models.rb @@ -92,8 +92,24 @@ class MysqlBindingValu < Data.define( :datetime_value, :datetime_2, :bool_value, - :nullable_value + :nullable_value, + :bit_value, + :year_value, + :time_value, + :json_unicode ) end # rubocop:enable Naming/VariableNumber + + class NameCase < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelcase, + :httpserver + ) + end end diff --git a/test/golden/mysql2/ruby32_notypes/query_sql.rb b/test/golden/mysql2/ruby32_notypes/query_sql.rb index fcb3578..47e9599 100644 --- a/test/golden/mysql2/ruby32_notypes/query_sql.rb +++ b/test/golden/mysql2/ruby32_notypes/query_sql.rb @@ -271,6 +271,16 @@ class ListRegressionPayloadsRow < Data.define(:id, :payload) class ListRegressionRowsRow < Data.define(:id, :order, :note, :payload) end + LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL = <<~'SQL' + SELECT authors.id, authors.name, authors.bio, books.id, books.name, books.author_id, books.description + FROM authors + LEFT JOIN books ON authors.id = books.author_id + ORDER BY authors.id + SQL + + class ListAuthorsWithOptionalBookRow < Data.define(:author, :book) + end + TRUNCATE_REGRESSIONS_SQL = <<~'SQL' TRUNCATE TABLE regressions SQL @@ -354,7 +364,7 @@ class GetAuthorWithQuestionMarksRow < Data.define(:id) SQL GET_MYSQL_BINDING_VALUES_SQL = <<~'SQL' - SELECT id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, datetime_value, datetime_2, bool_value, nullable_value FROM mysql_binding_values WHERE id = ? + SELECT id, text_value, blob_value, signed_value, unsigned_value, decimal_value, float_value, date_value, datetime_value, datetime_2, bool_value, nullable_value, bit_value, year_value, time_value, json_unicode FROM mysql_binding_values WHERE id = ? SQL # rubocop:disable Naming/VariableNumber @@ -370,7 +380,11 @@ class GetMysqlBindingValuesRow < Data.define( :datetime_value, :datetime_2, :bool_value, - :nullable_value + :nullable_value, + :bit_value, + :year_value, + :time_value, + :json_unicode ) end # rubocop:enable Naming/VariableNumber @@ -379,6 +393,51 @@ class GetMysqlBindingValuesRow < Data.define( TRUNCATE TABLE mysql_binding_values SQL + INSERT_NAME_CASE_SQL = <<~'SQL' + INSERT INTO name_cases (id, `class`, `end`, `hash`, `method`, `count`, `camelCase`, `HTTPServer`) + VALUES (?, ?, ?, ?, ?, ?, + ?, ?) + SQL + + GET_NAME_CASE_SQL = <<~'SQL' + SELECT id, class, end, hash, method, count, camelcase, httpserver FROM name_cases + WHERE id = ? AND `class` = ? AND `end` = ? AND `hash` = ? + AND `method` = ? AND `count` = ? + AND `HTTPServer` = ? AND `camelCase` = ? + SQL + + class GetNameCaseRow < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelcase, + :httpserver + ) + end + + GET_HTTP_SERVER_SQL = <<~'SQL' + SELECT id, class, end, hash, method, count, camelcase, httpserver FROM name_cases WHERE id = ? + SQL + + class GetHTTPServerRow < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelcase, + :httpserver + ) + end + + TRUNCATE_NAME_CASES_SQL = <<~'SQL' + TRUNCATE TABLE name_cases + SQL + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -850,6 +909,18 @@ def list_regression_rows end end + def list_authors_with_optional_book + with_client('list_authors_with_optional_book') do |client| + result = client.query(bind_params(client, LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL, []), as: :array) + result.map do |row| + ListAuthorsWithOptionalBookRow.new( + row[0].nil? && row[1].nil? ? nil : Author.new(row[0], row[1], row[2]), + row[3].nil? && row[4].nil? && row[5].nil? ? nil : Book.new(row[3], row[4], row[5], row[6]) + ) + end + end + end + def truncate_regressions with_client('truncate_regressions') do |client| client.query(bind_params(client, TRUNCATE_REGRESSIONS_SQL, [])) @@ -1004,7 +1075,11 @@ def get_mysql_binding_values(id:) row[8], row[9], row[10], - row[11] + row[11], + row[12], + row[13], + row[14], + row[15] ) end end @@ -1021,6 +1096,85 @@ def truncate_mysql_binding_values end end + def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, http_server:) + with_client('insert_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + camel_case, + http_server + ] + client.query(bind_params(client, INSERT_NAME_CASE_SQL, query_params)) + nil + end + end + + def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, camel_case:) + with_client('get_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + http_server, + camel_case + ] + result = client.query(bind_params(client, GET_NAME_CASE_SQL, query_params), as: :array) + row = result.first + row && GetNameCaseRow.new( + row[0], + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7] + ) + end + end + + # Like #get_name_case, but raises NotFound when there is no row. + def get_name_case!(...) + get_name_case(...) || raise(NotFound, 'GetNameCase returned no row') + end + + def get_http_server(id:) + with_client('get_http_server') do |client| + query_params = [id] + result = client.query(bind_params(client, GET_HTTP_SERVER_SQL, query_params), as: :array) + row = result.first + row && GetHTTPServerRow.new( + row[0], + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7] + ) + end + end + + # Like #get_http_server, but raises NotFound when there is no row. + def get_http_server!(...) + get_http_server(...) || raise(NotFound, 'GetHTTPServer returned no row') + end + + def truncate_name_cases + with_client('truncate_name_cases') do |client| + client.query(bind_params(client, TRUNCATE_NAME_CASES_SQL, [])) + nil + end + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ diff --git a/test/golden/pg/default/models.rb b/test/golden/pg/default/models.rb index 6b0fdb0..c690651 100644 --- a/test/golden/pg/default/models.rb +++ b/test/golden/pg/default/models.rb @@ -55,6 +55,23 @@ class PostgresExtraType < Data.define( ) end + class PostgresRemainingType < Data.define( + :c_hstore, + :c_ltree, + :c_regclass, + :c_cidr, + :c_macaddr, + :c_interval, + :c_varbit, + :c_xml, + :c_numeric, + :c_bigint, + :c_timestamp, + :c_timestamptz, + :c_text_matrix + ) + end + class JsonRoundTrip < Data.define(:id, :json_value, :jsonb_value) end @@ -94,6 +111,20 @@ class BatchType < Data.define( ) end + # rubocop:disable Naming/MethodName + class NameCase < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelCase, + :HTTPServer + ) + end + # rubocop:enable Naming/MethodName + class ExtendedBio < Data.define(:author_name, :name, :bio_type) end end diff --git a/test/golden/pg/default/models.rbs b/test/golden/pg/default/models.rbs index e5685fd..a9f570d 100644 --- a/test/golden/pg/default/models.rbs +++ b/test/golden/pg/default/models.rbs @@ -93,6 +93,37 @@ module PgCodegen ) -> void end + class PostgresRemainingType + attr_reader c_hstore: String? + attr_reader c_ltree: String? + attr_reader c_regclass: String? + attr_reader c_cidr: IPAddr? + attr_reader c_macaddr: String? + attr_reader c_interval: String? + attr_reader c_varbit: String? + attr_reader c_xml: String? + attr_reader c_numeric: BigDecimal? + attr_reader c_bigint: Integer? + attr_reader c_timestamp: Time? + attr_reader c_timestamptz: Time? + attr_reader c_text_matrix: Array[Array[String]]? + def initialize: ( + String? c_hstore, + String? c_ltree, + String? c_regclass, + IPAddr? c_cidr, + String? c_macaddr, + String? c_interval, + String? c_varbit, + String? c_xml, + BigDecimal? c_numeric, + Integer? c_bigint, + Time? c_timestamp, + Time? c_timestamptz, + Array[Array[String]]? c_text_matrix + ) -> void + end + class JsonRoundTrip attr_reader id: Integer attr_reader json_value: untyped @@ -164,6 +195,27 @@ module PgCodegen ) -> void end + class NameCase + attr_reader id: Integer + attr_reader class_: String + attr_reader end: String + attr_reader hash_: String + attr_reader method_: String + attr_reader count: Integer + attr_reader camelCase: String + attr_reader HTTPServer: String + def initialize: ( + Integer id, + String class_, + String end, + String hash_, + String method_, + Integer count, + String camelCase, + String HTTPServer + ) -> void + end + class ExtendedBio attr_reader author_name: String attr_reader name: String diff --git a/test/golden/pg/default/query_sql.rb b/test/golden/pg/default/query_sql.rb index 6dec461..d761b02 100644 --- a/test/golden/pg/default/query_sql.rb +++ b/test/golden/pg/default/query_sql.rb @@ -249,6 +249,40 @@ class GetExtraTypesRow < Data.define( TRUNCATE TABLE postgres_extra_types SQL + INSERT_REMAINING_TYPES_SQL = <<~'SQL' + INSERT INTO postgres_remaining_types ( + c_hstore, c_ltree, c_regclass, c_cidr, c_macaddr, c_interval, c_varbit, c_xml, + c_numeric, c_bigint, c_timestamp, c_timestamptz, c_text_matrix + ) VALUES ( + $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13 + ) + SQL + + GET_REMAINING_TYPES_SQL = <<~'SQL' + SELECT c_hstore, c_ltree, c_regclass, c_cidr, c_macaddr, c_interval, c_varbit, c_xml, c_numeric, c_bigint, c_timestamp, c_timestamptz, c_text_matrix FROM postgres_remaining_types LIMIT 1 + SQL + + class GetRemainingTypesRow < Data.define( + :c_hstore, + :c_ltree, + :c_regclass, + :c_cidr, + :c_macaddr, + :c_interval, + :c_varbit, + :c_xml, + :c_numeric, + :c_bigint, + :c_timestamp, + :c_timestamptz, + :c_text_matrix + ) + end + + TRUNCATE_REMAINING_TYPES_SQL = <<~'SQL' + TRUNCATE TABLE postgres_remaining_types + SQL + INSERT_ARRAY_TYPES_SQL = <<~'SQL' INSERT INTO postgres_array_types (c_uuid_array, c_bio_type_array, c_integer_matrix) VALUES ($1, $2, $3) SQL @@ -444,6 +478,55 @@ class CountBatchTypesAboveRow < Data.define(:count) TRUNCATE TABLE batch_types SQL + INSERT_NAME_CASE_SQL = <<~'SQL' + INSERT INTO name_cases (id, "class", "end", "hash", "method", "count", "camelCase", "HTTPServer") + VALUES ($1, $2, $3, $4, $5, $6, + $7, $8) + SQL + + GET_NAME_CASE_SQL = <<~'SQL' + SELECT id, class, "end", hash, method, count, "camelCase", "HTTPServer" FROM name_cases + WHERE id = $1 AND "class" = $2 AND "end" = $3 AND "hash" = $4 + AND "method" = $5 AND "count" = $6 + AND "HTTPServer" = $7 AND "camelCase" = $8 + SQL + + # rubocop:disable Naming/MethodName + class GetNameCaseRow < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelCase, + :HTTPServer + ) + end + # rubocop:enable Naming/MethodName + + GET_HTTP_SERVER_SQL = <<~'SQL' + SELECT id, class, "end", hash, method, count, "camelCase", "HTTPServer" FROM name_cases WHERE id = $1 + SQL + + # rubocop:disable Naming/MethodName + class GetHTTPServerRow < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelCase, + :HTTPServer + ) + end + # rubocop:enable Naming/MethodName + + TRUNCATE_NAME_CASES_SQL = <<~'SQL' + TRUNCATE TABLE name_cases + SQL + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -990,6 +1073,74 @@ def truncate_extra_types end end + def insert_remaining_types(c_hstore:, c_ltree:, c_regclass:, c_cidr:, c_macaddr:, c_interval:, c_varbit:, c_xml:, c_numeric:, c_bigint:, c_timestamp:, c_timestamptz:, c_text_matrix:) + with_client('insert_remaining_types') do |client| + query_params = [ + c_hstore, + c_ltree, + c_regclass, + c_cidr, + c_macaddr, + c_interval, + c_varbit, + c_xml, + c_numeric, + c_bigint, + c_timestamp, + 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') + end + client.exec_prepared('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') + end + result = client.exec_prepared('query_sql_get_remaining_types') + row = result.first + row && GetRemainingTypesRow.new( + row['c_hstore'], + row['c_ltree'], + row['c_regclass'], + row['c_cidr'], + row['c_macaddr'], + row['c_interval'], + row['c_varbit'], + row['c_xml'], + row['c_numeric'], + row['c_bigint'], + row['c_timestamp'], + row['c_timestamptz'], + row['c_text_matrix'] + ) + end + end + + # Like #get_remaining_types, but raises NotFound when there is no row. + def get_remaining_types!(...) + get_remaining_types(...) || raise(NotFound, 'GetRemainingTypes returned no row') + end + + 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') + end + client.exec_prepared('query_sql_truncate_remaining_types') + nil + end + end + 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)] @@ -1362,6 +1513,101 @@ def truncate_batch_types end end + def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, http_server:) + with_client('insert_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + 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') + end + client.exec_prepared('query_sql_insert_name_case', query_params) + nil + end + end + + def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, camel_case:) + with_client('get_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + 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') + end + result = client.exec_prepared('query_sql_get_name_case', query_params) + row = result.first + row && GetNameCaseRow.new( + row['id'], + row['class'], + row['end'], + row['hash'], + row['method'], + row['count'], + row['camelCase'], + row['HTTPServer'] + ) + end + end + + # Like #get_name_case, but raises NotFound when there is no row. + def get_name_case!(...) + get_name_case(...) || raise(NotFound, 'GetNameCase returned no row') + end + + 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') + end + result = client.exec_prepared('query_sql_get_http_server', query_params) + row = result.first + row && GetHTTPServerRow.new( + row['id'], + row['class'], + row['end'], + row['hash'], + row['method'], + row['count'], + row['camelCase'], + row['HTTPServer'] + ) + end + end + + # Like #get_http_server, but raises NotFound when there is no row. + def get_http_server!(...) + get_http_server(...) || raise(NotFound, 'GetHTTPServer returned no row') + end + + 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') + end + client.exec_prepared('query_sql_truncate_name_cases') + nil + end + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ diff --git a/test/golden/pg/default/query_sql.rbs b/test/golden/pg/default/query_sql.rbs index e550e96..c6bd118 100644 --- a/test/golden/pg/default/query_sql.rbs +++ b/test/golden/pg/default/query_sql.rbs @@ -232,6 +232,43 @@ module PgCodegen TRUNCATE_EXTRA_TYPES_SQL: String + INSERT_REMAINING_TYPES_SQL: String + + GET_REMAINING_TYPES_SQL: String + + class GetRemainingTypesRow + attr_reader c_hstore: String? + attr_reader c_ltree: String? + attr_reader c_regclass: String? + attr_reader c_cidr: IPAddr? + attr_reader c_macaddr: String? + attr_reader c_interval: String? + attr_reader c_varbit: String? + attr_reader c_xml: String? + attr_reader c_numeric: BigDecimal? + attr_reader c_bigint: Integer? + attr_reader c_timestamp: Time? + attr_reader c_timestamptz: Time? + attr_reader c_text_matrix: Array[Array[String]]? + def initialize: ( + String? c_hstore, + String? c_ltree, + String? c_regclass, + IPAddr? c_cidr, + String? c_macaddr, + String? c_interval, + String? c_varbit, + String? c_xml, + BigDecimal? c_numeric, + Integer? c_bigint, + Time? c_timestamp, + Time? c_timestamptz, + Array[Array[String]]? c_text_matrix + ) -> void + end + + TRUNCATE_REMAINING_TYPES_SQL: String + INSERT_ARRAY_TYPES_SQL: String GET_ARRAY_TYPES_SQL: String @@ -466,6 +503,56 @@ module PgCodegen TRUNCATE_BATCH_TYPES_SQL: String + INSERT_NAME_CASE_SQL: String + + GET_NAME_CASE_SQL: String + + class GetNameCaseRow + attr_reader id: Integer + attr_reader class_: String + attr_reader end: String + attr_reader hash_: String + attr_reader method_: String + attr_reader count: Integer + attr_reader camelCase: String + attr_reader HTTPServer: String + def initialize: ( + Integer id, + String class_, + String end, + String hash_, + String method_, + Integer count, + String camelCase, + String HTTPServer + ) -> void + end + + GET_HTTP_SERVER_SQL: String + + class GetHTTPServerRow + attr_reader id: Integer + attr_reader class_: String + attr_reader end: String + attr_reader hash_: String + attr_reader method_: String + attr_reader count: Integer + attr_reader camelCase: String + attr_reader HTTPServer: String + def initialize: ( + Integer id, + String class_, + String end, + String hash_, + String method_, + Integer count, + String camelCase, + String HTTPServer + ) -> void + end + + TRUNCATE_NAME_CASES_SQL: String + MANY_PARAMS_SQL: String class ManyParamsRow @@ -591,6 +678,14 @@ module PgCodegen def truncate_extra_types: -> void + def insert_remaining_types: (c_hstore: String?, c_ltree: String?, c_regclass: String?, c_cidr: IPAddr?, c_macaddr: String?, c_interval: String?, c_varbit: String?, c_xml: String?, c_numeric: BigDecimal?, c_bigint: Integer?, c_timestamp: Time?, c_timestamptz: Time?, c_text_matrix: Array[Array[String]]?) -> void + + def get_remaining_types: -> GetRemainingTypesRow? + + def get_remaining_types!: -> GetRemainingTypesRow + + def truncate_remaining_types: -> void + def insert_array_types: (c_uuid_array: Array[String]?, c_bio_type_array: Array[String]?, c_integer_matrix: Array[Array[Integer]]?) -> void def get_array_types: -> GetArrayTypesRow? @@ -645,6 +740,18 @@ module PgCodegen def truncate_batch_types: -> void + def insert_name_case: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, camel_case: String, http_server: String) -> void + + def get_name_case: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, http_server: String, camel_case: String) -> GetNameCaseRow? + + def get_name_case!: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, http_server: String, camel_case: String) -> GetNameCaseRow + + def get_http_server: (id: Integer) -> GetHTTPServerRow? + + def get_http_server!: (id: Integer) -> GetHTTPServerRow + + def truncate_name_cases: -> void + 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/models.rb b/test/golden/pg/ruby31_gemfile/models.rb index 487f6a5..bbf8948 100644 --- a/test/golden/pg/ruby31_gemfile/models.rb +++ b/test/golden/pg/ruby31_gemfile/models.rb @@ -58,6 +58,23 @@ module ExtendedBioType keyword_init: true ) + PostgresRemainingType = Struct.new( + :c_hstore, + :c_ltree, + :c_regclass, + :c_cidr, + :c_macaddr, + :c_interval, + :c_varbit, + :c_xml, + :c_numeric, + :c_bigint, + :c_timestamp, + :c_timestamptz, + :c_text_matrix, + keyword_init: true + ) + JsonRoundTrip = Struct.new(:id, :json_value, :jsonb_value, keyword_init: true) PostgresArrayType = Struct.new(:c_uuid_array, :c_bio_type_array, :c_integer_matrix, keyword_init: true) @@ -95,5 +112,19 @@ module ExtendedBioType keyword_init: true ) + # rubocop:disable Naming/MethodName + NameCase = Struct.new( + :id, + :class_, + :end, + :hash_, + :method_, + :count_, + :camelCase, + :HTTPServer, + keyword_init: true + ) + # rubocop:enable Naming/MethodName + ExtendedBio = Struct.new(:author_name, :name, :bio_type, keyword_init: true) end diff --git a/test/golden/pg/ruby31_gemfile/models.rbs b/test/golden/pg/ruby31_gemfile/models.rbs index f1e41a1..4874095 100644 --- a/test/golden/pg/ruby31_gemfile/models.rbs +++ b/test/golden/pg/ruby31_gemfile/models.rbs @@ -126,6 +126,52 @@ module PgCodegen ) -> PostgresExtraType end + class PostgresRemainingType < ::Struct[untyped] + attr_accessor c_hstore: String? + attr_accessor c_ltree: String? + attr_accessor c_regclass: String? + attr_accessor c_cidr: IPAddr? + attr_accessor c_macaddr: String? + attr_accessor c_interval: String? + attr_accessor c_varbit: String? + attr_accessor c_xml: String? + attr_accessor c_numeric: BigDecimal? + attr_accessor c_bigint: Integer? + attr_accessor c_timestamp: Time? + attr_accessor c_timestamptz: Time? + attr_accessor c_text_matrix: Array[Array[String]]? + def initialize: ( + c_hstore: String?, + c_ltree: String?, + c_regclass: String?, + c_cidr: IPAddr?, + c_macaddr: String?, + c_interval: String?, + c_varbit: String?, + c_xml: String?, + c_numeric: BigDecimal?, + c_bigint: Integer?, + c_timestamp: Time?, + c_timestamptz: Time?, + c_text_matrix: Array[Array[String]]? + ) -> void + def self.new: ( + c_hstore: String?, + c_ltree: String?, + c_regclass: String?, + c_cidr: IPAddr?, + c_macaddr: String?, + c_interval: String?, + c_varbit: String?, + c_xml: String?, + c_numeric: BigDecimal?, + c_bigint: Integer?, + c_timestamp: Time?, + c_timestamptz: Time?, + c_text_matrix: Array[Array[String]]? + ) -> PostgresRemainingType + end + class JsonRoundTrip < ::Struct[untyped] attr_accessor id: Integer attr_accessor json_value: untyped @@ -226,6 +272,37 @@ module PgCodegen ) -> BatchType end + class NameCase < ::Struct[untyped] + attr_accessor id: Integer + attr_accessor class_: String + attr_accessor end: String + attr_accessor hash_: String + attr_accessor method_: String + attr_accessor count_: Integer + attr_accessor camelCase: String + attr_accessor HTTPServer: String + def initialize: ( + id: Integer, + class_: String, + end: String, + hash_: String, + method_: String, + count_: Integer, + camelCase: String, + HTTPServer: String + ) -> void + def self.new: ( + id: Integer, + class_: String, + end: String, + hash_: String, + method_: String, + count_: Integer, + camelCase: String, + HTTPServer: String + ) -> NameCase + end + class ExtendedBio < ::Struct[untyped] attr_accessor author_name: String attr_accessor name: String diff --git a/test/golden/pg/ruby31_gemfile/query_sql.rb b/test/golden/pg/ruby31_gemfile/query_sql.rb index 08c5bae..4e27bf6 100644 --- a/test/golden/pg/ruby31_gemfile/query_sql.rb +++ b/test/golden/pg/ruby31_gemfile/query_sql.rb @@ -239,6 +239,40 @@ module PgCodegen TRUNCATE TABLE postgres_extra_types SQL + INSERT_REMAINING_TYPES_SQL = <<~'SQL' + INSERT INTO postgres_remaining_types ( + c_hstore, c_ltree, c_regclass, c_cidr, c_macaddr, c_interval, c_varbit, c_xml, + c_numeric, c_bigint, c_timestamp, c_timestamptz, c_text_matrix + ) VALUES ( + $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13 + ) + SQL + + GET_REMAINING_TYPES_SQL = <<~'SQL' + SELECT c_hstore, c_ltree, c_regclass, c_cidr, c_macaddr, c_interval, c_varbit, c_xml, c_numeric, c_bigint, c_timestamp, c_timestamptz, c_text_matrix FROM postgres_remaining_types LIMIT 1 + SQL + + GetRemainingTypesRow = Struct.new( + :c_hstore, + :c_ltree, + :c_regclass, + :c_cidr, + :c_macaddr, + :c_interval, + :c_varbit, + :c_xml, + :c_numeric, + :c_bigint, + :c_timestamp, + :c_timestamptz, + :c_text_matrix, + keyword_init: true + ) + + TRUNCATE_REMAINING_TYPES_SQL = <<~'SQL' + TRUNCATE TABLE postgres_remaining_types + SQL + INSERT_ARRAY_TYPES_SQL = <<~'SQL' INSERT INTO postgres_array_types (c_uuid_array, c_bio_type_array, c_integer_matrix) VALUES ($1, $2, $3) SQL @@ -434,6 +468,55 @@ module PgCodegen TRUNCATE TABLE batch_types SQL + INSERT_NAME_CASE_SQL = <<~'SQL' + INSERT INTO name_cases (id, "class", "end", "hash", "method", "count", "camelCase", "HTTPServer") + VALUES ($1, $2, $3, $4, $5, $6, + $7, $8) + SQL + + GET_NAME_CASE_SQL = <<~'SQL' + SELECT id, class, "end", hash, method, count, "camelCase", "HTTPServer" FROM name_cases + WHERE id = $1 AND "class" = $2 AND "end" = $3 AND "hash" = $4 + AND "method" = $5 AND "count" = $6 + AND "HTTPServer" = $7 AND "camelCase" = $8 + SQL + + # rubocop:disable Naming/MethodName + GetNameCaseRow = Struct.new( + :id, + :class_, + :end, + :hash_, + :method_, + :count_, + :camelCase, + :HTTPServer, + keyword_init: true + ) + # rubocop:enable Naming/MethodName + + GET_HTTP_SERVER_SQL = <<~'SQL' + SELECT id, class, "end", hash, method, count, "camelCase", "HTTPServer" FROM name_cases WHERE id = $1 + SQL + + # rubocop:disable Naming/MethodName + GetHTTPServerRow = Struct.new( + :id, + :class_, + :end, + :hash_, + :method_, + :count_, + :camelCase, + :HTTPServer, + keyword_init: true + ) + # rubocop:enable Naming/MethodName + + TRUNCATE_NAME_CASES_SQL = <<~'SQL' + TRUNCATE TABLE name_cases + SQL + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -998,6 +1081,74 @@ def truncate_extra_types end end + def insert_remaining_types(c_hstore:, c_ltree:, c_regclass:, c_cidr:, c_macaddr:, c_interval:, c_varbit:, c_xml:, c_numeric:, c_bigint:, c_timestamp:, c_timestamptz:, c_text_matrix:) + with_client('insert_remaining_types') do |client| + query_params = [ + c_hstore, + c_ltree, + c_regclass, + c_cidr, + c_macaddr, + c_interval, + c_varbit, + c_xml, + c_numeric, + c_bigint, + c_timestamp, + 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') + end + client.exec_prepared('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') + end + result = client.exec_prepared('query_sql_get_remaining_types') + row = result.first + row && GetRemainingTypesRow.new( + c_hstore: row['c_hstore'], + c_ltree: row['c_ltree'], + c_regclass: row['c_regclass'], + c_cidr: row['c_cidr'], + c_macaddr: row['c_macaddr'], + c_interval: row['c_interval'], + c_varbit: row['c_varbit'], + c_xml: row['c_xml'], + c_numeric: row['c_numeric'], + c_bigint: row['c_bigint'], + c_timestamp: row['c_timestamp'], + c_timestamptz: row['c_timestamptz'], + c_text_matrix: row['c_text_matrix'] + ) + end + end + + # Like #get_remaining_types, but raises NotFound when there is no row. + def get_remaining_types!(...) + get_remaining_types(...) || raise(NotFound, 'GetRemainingTypes returned no row') + end + + 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') + end + client.exec_prepared('query_sql_truncate_remaining_types') + nil + end + end + 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)] @@ -1374,6 +1525,101 @@ def truncate_batch_types end end + def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, http_server:) + with_client('insert_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + 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') + end + client.exec_prepared('query_sql_insert_name_case', query_params) + nil + end + end + + def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, camel_case:) + with_client('get_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + 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') + end + result = client.exec_prepared('query_sql_get_name_case', query_params) + row = result.first + row && GetNameCaseRow.new( + id: row['id'], + class_: row['class'], + end: row['end'], + hash_: row['hash'], + method_: row['method'], + count_: row['count'], + camelCase: row['camelCase'], + HTTPServer: row['HTTPServer'] + ) + end + end + + # Like #get_name_case, but raises NotFound when there is no row. + def get_name_case!(...) + get_name_case(...) || raise(NotFound, 'GetNameCase returned no row') + end + + 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') + end + result = client.exec_prepared('query_sql_get_http_server', query_params) + row = result.first + row && GetHTTPServerRow.new( + id: row['id'], + class_: row['class'], + end: row['end'], + hash_: row['hash'], + method_: row['method'], + count_: row['count'], + camelCase: row['camelCase'], + HTTPServer: row['HTTPServer'] + ) + end + end + + # Like #get_http_server, but raises NotFound when there is no row. + def get_http_server!(...) + get_http_server(...) || raise(NotFound, 'GetHTTPServer returned no row') + end + + 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') + end + client.exec_prepared('query_sql_truncate_name_cases') + nil + end + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ diff --git a/test/golden/pg/ruby31_gemfile/query_sql.rbs b/test/golden/pg/ruby31_gemfile/query_sql.rbs index 3e6959e..0e50262 100644 --- a/test/golden/pg/ruby31_gemfile/query_sql.rbs +++ b/test/golden/pg/ruby31_gemfile/query_sql.rbs @@ -279,6 +279,58 @@ module PgCodegen TRUNCATE_EXTRA_TYPES_SQL: String + INSERT_REMAINING_TYPES_SQL: String + + GET_REMAINING_TYPES_SQL: String + + class GetRemainingTypesRow < ::Struct[untyped] + attr_accessor c_hstore: String? + attr_accessor c_ltree: String? + attr_accessor c_regclass: String? + attr_accessor c_cidr: IPAddr? + attr_accessor c_macaddr: String? + attr_accessor c_interval: String? + attr_accessor c_varbit: String? + attr_accessor c_xml: String? + attr_accessor c_numeric: BigDecimal? + attr_accessor c_bigint: Integer? + attr_accessor c_timestamp: Time? + attr_accessor c_timestamptz: Time? + attr_accessor c_text_matrix: Array[Array[String]]? + def initialize: ( + c_hstore: String?, + c_ltree: String?, + c_regclass: String?, + c_cidr: IPAddr?, + c_macaddr: String?, + c_interval: String?, + c_varbit: String?, + c_xml: String?, + c_numeric: BigDecimal?, + c_bigint: Integer?, + c_timestamp: Time?, + c_timestamptz: Time?, + c_text_matrix: Array[Array[String]]? + ) -> void + def self.new: ( + c_hstore: String?, + c_ltree: String?, + c_regclass: String?, + c_cidr: IPAddr?, + c_macaddr: String?, + c_interval: String?, + c_varbit: String?, + c_xml: String?, + c_numeric: BigDecimal?, + c_bigint: Integer?, + c_timestamp: Time?, + c_timestamptz: Time?, + c_text_matrix: Array[Array[String]]? + ) -> GetRemainingTypesRow + end + + TRUNCATE_REMAINING_TYPES_SQL: String + INSERT_ARRAY_TYPES_SQL: String GET_ARRAY_TYPES_SQL: String @@ -580,6 +632,76 @@ module PgCodegen TRUNCATE_BATCH_TYPES_SQL: String + INSERT_NAME_CASE_SQL: String + + GET_NAME_CASE_SQL: String + + class GetNameCaseRow < ::Struct[untyped] + attr_accessor id: Integer + attr_accessor class_: String + attr_accessor end: String + attr_accessor hash_: String + attr_accessor method_: String + attr_accessor count_: Integer + attr_accessor camelCase: String + attr_accessor HTTPServer: String + def initialize: ( + id: Integer, + class_: String, + end: String, + hash_: String, + method_: String, + count_: Integer, + camelCase: String, + HTTPServer: String + ) -> void + def self.new: ( + id: Integer, + class_: String, + end: String, + hash_: String, + method_: String, + count_: Integer, + camelCase: String, + HTTPServer: String + ) -> GetNameCaseRow + end + + GET_HTTP_SERVER_SQL: String + + class GetHTTPServerRow < ::Struct[untyped] + attr_accessor id: Integer + attr_accessor class_: String + attr_accessor end: String + attr_accessor hash_: String + attr_accessor method_: String + attr_accessor count_: Integer + attr_accessor camelCase: String + attr_accessor HTTPServer: String + def initialize: ( + id: Integer, + class_: String, + end: String, + hash_: String, + method_: String, + count_: Integer, + camelCase: String, + HTTPServer: String + ) -> void + def self.new: ( + id: Integer, + class_: String, + end: String, + hash_: String, + method_: String, + count_: Integer, + camelCase: String, + HTTPServer: String + ) -> GetHTTPServerRow + end + + TRUNCATE_NAME_CASES_SQL: String + MANY_PARAMS_SQL: String class ManyParamsRow < ::Struct[untyped] @@ -715,6 +837,14 @@ module PgCodegen def truncate_extra_types: -> void + def insert_remaining_types: (c_hstore: String?, c_ltree: String?, c_regclass: String?, c_cidr: IPAddr?, c_macaddr: String?, c_interval: String?, c_varbit: String?, c_xml: String?, c_numeric: BigDecimal?, c_bigint: Integer?, c_timestamp: Time?, c_timestamptz: Time?, c_text_matrix: Array[Array[String]]?) -> void + + def get_remaining_types: -> GetRemainingTypesRow? + + def get_remaining_types!: -> GetRemainingTypesRow + + def truncate_remaining_types: -> void + def insert_array_types: (c_uuid_array: Array[String]?, c_bio_type_array: Array[String]?, c_integer_matrix: Array[Array[Integer]]?) -> void def get_array_types: -> GetArrayTypesRow? @@ -769,6 +899,18 @@ module PgCodegen def truncate_batch_types: -> void + def insert_name_case: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, camel_case: String, http_server: String) -> void + + def get_name_case: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, http_server: String, camel_case: String) -> GetNameCaseRow? + + def get_name_case!: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, http_server: String, camel_case: String) -> GetNameCaseRow + + def get_http_server: (id: Integer) -> GetHTTPServerRow? + + def get_http_server!: (id: Integer) -> GetHTTPServerRow + + def truncate_name_cases: -> void + 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/models.rb b/test/golden/pg/ruby32_notypes/models.rb index 6b0fdb0..c690651 100644 --- a/test/golden/pg/ruby32_notypes/models.rb +++ b/test/golden/pg/ruby32_notypes/models.rb @@ -55,6 +55,23 @@ class PostgresExtraType < Data.define( ) end + class PostgresRemainingType < Data.define( + :c_hstore, + :c_ltree, + :c_regclass, + :c_cidr, + :c_macaddr, + :c_interval, + :c_varbit, + :c_xml, + :c_numeric, + :c_bigint, + :c_timestamp, + :c_timestamptz, + :c_text_matrix + ) + end + class JsonRoundTrip < Data.define(:id, :json_value, :jsonb_value) end @@ -94,6 +111,20 @@ class BatchType < Data.define( ) end + # rubocop:disable Naming/MethodName + class NameCase < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelCase, + :HTTPServer + ) + end + # rubocop:enable Naming/MethodName + class ExtendedBio < Data.define(:author_name, :name, :bio_type) end end diff --git a/test/golden/pg/ruby32_notypes/query_sql.rb b/test/golden/pg/ruby32_notypes/query_sql.rb index 5f3e150..7c91085 100644 --- a/test/golden/pg/ruby32_notypes/query_sql.rb +++ b/test/golden/pg/ruby32_notypes/query_sql.rb @@ -249,6 +249,40 @@ class GetExtraTypesRow < Data.define( TRUNCATE TABLE postgres_extra_types SQL + INSERT_REMAINING_TYPES_SQL = <<~'SQL' + INSERT INTO postgres_remaining_types ( + c_hstore, c_ltree, c_regclass, c_cidr, c_macaddr, c_interval, c_varbit, c_xml, + c_numeric, c_bigint, c_timestamp, c_timestamptz, c_text_matrix + ) VALUES ( + $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13 + ) + SQL + + GET_REMAINING_TYPES_SQL = <<~'SQL' + SELECT c_hstore, c_ltree, c_regclass, c_cidr, c_macaddr, c_interval, c_varbit, c_xml, c_numeric, c_bigint, c_timestamp, c_timestamptz, c_text_matrix FROM postgres_remaining_types LIMIT 1 + SQL + + class GetRemainingTypesRow < Data.define( + :c_hstore, + :c_ltree, + :c_regclass, + :c_cidr, + :c_macaddr, + :c_interval, + :c_varbit, + :c_xml, + :c_numeric, + :c_bigint, + :c_timestamp, + :c_timestamptz, + :c_text_matrix + ) + end + + TRUNCATE_REMAINING_TYPES_SQL = <<~'SQL' + TRUNCATE TABLE postgres_remaining_types + SQL + INSERT_ARRAY_TYPES_SQL = <<~'SQL' INSERT INTO postgres_array_types (c_uuid_array, c_bio_type_array, c_integer_matrix) VALUES ($1, $2, $3) SQL @@ -444,6 +478,55 @@ class CountBatchTypesAboveRow < Data.define(:count) TRUNCATE TABLE batch_types SQL + INSERT_NAME_CASE_SQL = <<~'SQL' + INSERT INTO name_cases (id, "class", "end", "hash", "method", "count", "camelCase", "HTTPServer") + VALUES ($1, $2, $3, $4, $5, $6, + $7, $8) + SQL + + GET_NAME_CASE_SQL = <<~'SQL' + SELECT id, class, "end", hash, method, count, "camelCase", "HTTPServer" FROM name_cases + WHERE id = $1 AND "class" = $2 AND "end" = $3 AND "hash" = $4 + AND "method" = $5 AND "count" = $6 + AND "HTTPServer" = $7 AND "camelCase" = $8 + SQL + + # rubocop:disable Naming/MethodName + class GetNameCaseRow < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelCase, + :HTTPServer + ) + end + # rubocop:enable Naming/MethodName + + GET_HTTP_SERVER_SQL = <<~'SQL' + SELECT id, class, "end", hash, method, count, "camelCase", "HTTPServer" FROM name_cases WHERE id = $1 + SQL + + # rubocop:disable Naming/MethodName + class GetHTTPServerRow < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelCase, + :HTTPServer + ) + end + # rubocop:enable Naming/MethodName + + TRUNCATE_NAME_CASES_SQL = <<~'SQL' + TRUNCATE TABLE name_cases + SQL + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -993,6 +1076,74 @@ def truncate_extra_types end end + def insert_remaining_types(c_hstore:, c_ltree:, c_regclass:, c_cidr:, c_macaddr:, c_interval:, c_varbit:, c_xml:, c_numeric:, c_bigint:, c_timestamp:, c_timestamptz:, c_text_matrix:) + with_client('insert_remaining_types') do |client| + query_params = [ + c_hstore, + c_ltree, + c_regclass, + c_cidr, + c_macaddr, + c_interval, + c_varbit, + c_xml, + c_numeric, + c_bigint, + c_timestamp, + 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') + end + client.exec_prepared('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') + end + result = client.exec_prepared('query_sql_get_remaining_types') + row = result.first + row && GetRemainingTypesRow.new( + row['c_hstore'], + row['c_ltree'], + row['c_regclass'], + row['c_cidr'], + row['c_macaddr'], + row['c_interval'], + row['c_varbit'], + row['c_xml'], + row['c_numeric'], + row['c_bigint'], + row['c_timestamp'], + row['c_timestamptz'], + row['c_text_matrix'] + ) + end + end + + # Like #get_remaining_types, but raises NotFound when there is no row. + def get_remaining_types!(...) + get_remaining_types(...) || raise(NotFound, 'GetRemainingTypes returned no row') + end + + 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') + end + client.exec_prepared('query_sql_truncate_remaining_types') + nil + end + end + 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)] @@ -1365,6 +1516,101 @@ def truncate_batch_types end end + def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, http_server:) + with_client('insert_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + 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') + end + client.exec_prepared('query_sql_insert_name_case', query_params) + nil + end + end + + def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, camel_case:) + with_client('get_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + 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') + end + result = client.exec_prepared('query_sql_get_name_case', query_params) + row = result.first + row && GetNameCaseRow.new( + row['id'], + row['class'], + row['end'], + row['hash'], + row['method'], + row['count'], + row['camelCase'], + row['HTTPServer'] + ) + end + end + + # Like #get_name_case, but raises NotFound when there is no row. + def get_name_case!(...) + get_name_case(...) || raise(NotFound, 'GetNameCase returned no row') + end + + 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') + end + result = client.exec_prepared('query_sql_get_http_server', query_params) + row = result.first + row && GetHTTPServerRow.new( + row['id'], + row['class'], + row['end'], + row['hash'], + row['method'], + row['count'], + row['camelCase'], + row['HTTPServer'] + ) + end + end + + # Like #get_http_server, but raises NotFound when there is no row. + def get_http_server!(...) + get_http_server(...) || raise(NotFound, 'GetHTTPServer returned no row') + end + + 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') + end + client.exec_prepared('query_sql_truncate_name_cases') + nil + end + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ diff --git a/test/golden/sqlite3/default/models.rb b/test/golden/sqlite3/default/models.rb index 365bef7..5a78ef5 100644 --- a/test/golden/sqlite3/default/models.rb +++ b/test/golden/sqlite3/default/models.rb @@ -25,4 +25,18 @@ class Regression < Data.define(:id, :order, :price, :payload) class Tag < Data.define(:name, :uses) end + + # rubocop:disable Naming/MethodName + class NameCase < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelCase, + :HTTPServer + ) + end + # rubocop:enable Naming/MethodName end diff --git a/test/golden/sqlite3/default/models.rbs b/test/golden/sqlite3/default/models.rbs index 271946e..4ea1aac 100644 --- a/test/golden/sqlite3/default/models.rbs +++ b/test/golden/sqlite3/default/models.rbs @@ -47,4 +47,25 @@ module Sqlite3Codegen attr_reader uses: Integer def initialize: ( String name, Integer uses) -> void end + + class NameCase + attr_reader id: Integer + attr_reader class_: String + attr_reader end: String + attr_reader hash_: String + attr_reader method_: String + attr_reader count: Integer + attr_reader camelCase: String + attr_reader HTTPServer: String + def initialize: ( + Integer id, + String class_, + String end, + String hash_, + String method_, + Integer count, + String camelCase, + String HTTPServer + ) -> void + end end diff --git a/test/golden/sqlite3/default/query_sql.rb b/test/golden/sqlite3/default/query_sql.rb index 859e736..e55cd17 100644 --- a/test/golden/sqlite3/default/query_sql.rb +++ b/test/golden/sqlite3/default/query_sql.rb @@ -314,6 +314,65 @@ class ListTagsRow < Data.define(:name, :uses) DELETE FROM tags SQL + LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL = <<~'SQL' + SELECT authors.id, authors.name, authors.bio, books.id, books.name, books.author_id, books.description + FROM authors + LEFT JOIN books ON authors.id = books.author_id + ORDER BY authors.id + SQL + + class ListAuthorsWithOptionalBookRow < Data.define(:author, :book) + end + + INSERT_NAME_CASE_SQL = <<~'SQL' + INSERT INTO name_cases (id, "class", "end", "hash", "method", "count", "camelCase", "HTTPServer") + VALUES (?, ?, ?, ?, ?, ?, + ?, ?) + SQL + + GET_NAME_CASE_SQL = <<~'SQL' + SELECT id, class, "end", hash, method, count, camelCase, HTTPServer FROM name_cases + WHERE id = ? AND "class" = ? AND "end" = ? AND "hash" = ? + AND "method" = ? AND "count" = ? + AND "HTTPServer" = ? AND "camelCase" = ? + SQL + + # rubocop:disable Naming/MethodName + class GetNameCaseRow < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelCase, + :HTTPServer + ) + end + # rubocop:enable Naming/MethodName + + GET_HTTP_SERVER_SQL = <<~'SQL' + SELECT id, class, "end", hash, method, count, camelCase, HTTPServer FROM name_cases WHERE id = ? + SQL + + # rubocop:disable Naming/MethodName + class GetHTTPServerRow < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelCase, + :HTTPServer + ) + end + # rubocop:enable Naming/MethodName + + TRUNCATE_NAME_CASES_SQL = <<~'SQL' + DELETE FROM name_cases + SQL + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -1027,6 +1086,122 @@ def truncate_tags end end + def list_authors_with_optional_book + with_client('list_authors_with_optional_book') do |client| + stmt = client.prepare(LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL) + begin + result = stmt.execute + result.map do |row| + ListAuthorsWithOptionalBookRow.new( + row[0].nil? && row[1].nil? ? nil : Author.new(row[0], row[1], row[2]), + row[3].nil? && row[4].nil? && row[5].nil? ? nil : Book.new(row[3], row[4], row[5], row[6]) + ) + end + ensure + stmt.close + end + end + end + + def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, http_server:) + with_client('insert_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + camel_case, + http_server + ] + stmt = client.prepare(INSERT_NAME_CASE_SQL) + begin + stmt.execute(*query_params) + nil + ensure + stmt.close + end + end + end + + def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, camel_case:) + with_client('get_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + http_server, + camel_case + ] + stmt = client.prepare(GET_NAME_CASE_SQL) + begin + result = stmt.execute(*query_params) + row = result.first + row && GetNameCaseRow.new( + row[0], + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7] + ) + ensure + stmt.close + end + end + end + + # Like #get_name_case, but raises NotFound when there is no row. + def get_name_case!(...) + get_name_case(...) || raise(NotFound, 'GetNameCase returned no row') + end + + def get_http_server(id:) + with_client('get_http_server') do |client| + query_params = [id] + stmt = client.prepare(GET_HTTP_SERVER_SQL) + begin + result = stmt.execute(*query_params) + row = result.first + row && GetHTTPServerRow.new( + row[0], + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7] + ) + ensure + stmt.close + end + end + end + + # Like #get_http_server, but raises NotFound when there is no row. + def get_http_server!(...) + get_http_server(...) || raise(NotFound, 'GetHTTPServer returned no row') + end + + def truncate_name_cases + with_client('truncate_name_cases') do |client| + stmt = client.prepare(TRUNCATE_NAME_CASES_SQL) + begin + stmt.execute + nil + ensure + stmt.close + end + end + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ diff --git a/test/golden/sqlite3/default/query_sql.rbs b/test/golden/sqlite3/default/query_sql.rbs index 9dbfd5b..0da7229 100644 --- a/test/golden/sqlite3/default/query_sql.rbs +++ b/test/golden/sqlite3/default/query_sql.rbs @@ -313,6 +313,64 @@ module Sqlite3Codegen TRUNCATE_TAGS_SQL: String + LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL: String + + class ListAuthorsWithOptionalBookRow + attr_reader author: Author? + attr_reader book: Book? + def initialize: ( Author? author, Book? book) -> void + end + + INSERT_NAME_CASE_SQL: String + + GET_NAME_CASE_SQL: String + + class GetNameCaseRow + attr_reader id: Integer + attr_reader class_: String + attr_reader end: String + attr_reader hash_: String + attr_reader method_: String + attr_reader count: Integer + attr_reader camelCase: String + attr_reader HTTPServer: String + def initialize: ( + Integer id, + String class_, + String end, + String hash_, + String method_, + Integer count, + String camelCase, + String HTTPServer + ) -> void + end + + GET_HTTP_SERVER_SQL: String + + class GetHTTPServerRow + attr_reader id: Integer + attr_reader class_: String + attr_reader end: String + attr_reader hash_: String + attr_reader method_: String + attr_reader count: Integer + attr_reader camelCase: String + attr_reader HTTPServer: String + def initialize: ( + Integer id, + String class_, + String end, + String hash_, + String method_, + Integer count, + String camelCase, + String HTTPServer + ) -> void + end + + TRUNCATE_NAME_CASES_SQL: String + MANY_PARAMS_SQL: String class ManyParamsRow @@ -454,6 +512,20 @@ module Sqlite3Codegen def truncate_tags: -> void + def list_authors_with_optional_book: -> Array[ListAuthorsWithOptionalBookRow] + + def insert_name_case: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, camel_case: String, http_server: String) -> void + + def get_name_case: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, http_server: String, camel_case: String) -> GetNameCaseRow? + + def get_name_case!: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, http_server: String, camel_case: String) -> GetNameCaseRow + + def get_http_server: (id: Integer) -> GetHTTPServerRow? + + def get_http_server!: (id: Integer) -> GetHTTPServerRow + + def truncate_name_cases: -> 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/models.rb b/test/golden/sqlite3/ruby31_gemfile/models.rb index 3710727..85f97a1 100644 --- a/test/golden/sqlite3/ruby31_gemfile/models.rb +++ b/test/golden/sqlite3/ruby31_gemfile/models.rb @@ -33,4 +33,18 @@ module Sqlite3Codegen ) Tag = Struct.new(:name, :uses, keyword_init: true) + + # rubocop:disable Naming/MethodName + NameCase = Struct.new( + :id, + :class_, + :end, + :hash_, + :method_, + :count_, + :camelCase, + :HTTPServer, + keyword_init: true + ) + # rubocop:enable Naming/MethodName end diff --git a/test/golden/sqlite3/ruby31_gemfile/models.rbs b/test/golden/sqlite3/ruby31_gemfile/models.rbs index 2aa5c09..bda472f 100644 --- a/test/golden/sqlite3/ruby31_gemfile/models.rbs +++ b/test/golden/sqlite3/ruby31_gemfile/models.rbs @@ -60,4 +60,35 @@ module Sqlite3Codegen def initialize: ( name: String, uses: Integer) -> void def self.new: ( name: String, uses: Integer) -> Tag end + + class NameCase < ::Struct[untyped] + attr_accessor id: Integer + attr_accessor class_: String + attr_accessor end: String + attr_accessor hash_: String + attr_accessor method_: String + attr_accessor count_: Integer + attr_accessor camelCase: String + attr_accessor HTTPServer: String + def initialize: ( + id: Integer, + class_: String, + end: String, + hash_: String, + method_: String, + count_: Integer, + camelCase: String, + HTTPServer: String + ) -> void + def self.new: ( + id: Integer, + class_: String, + end: String, + hash_: String, + method_: String, + count_: Integer, + camelCase: String, + HTTPServer: String + ) -> NameCase + end end diff --git a/test/golden/sqlite3/ruby31_gemfile/query_sql.rb b/test/golden/sqlite3/ruby31_gemfile/query_sql.rb index a65a317..7a8a70e 100644 --- a/test/golden/sqlite3/ruby31_gemfile/query_sql.rb +++ b/test/golden/sqlite3/ruby31_gemfile/query_sql.rb @@ -297,6 +297,64 @@ module Sqlite3Codegen DELETE FROM tags SQL + LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL = <<~'SQL' + SELECT authors.id, authors.name, authors.bio, books.id, books.name, books.author_id, books.description + FROM authors + LEFT JOIN books ON authors.id = books.author_id + ORDER BY authors.id + SQL + + ListAuthorsWithOptionalBookRow = Struct.new(:author, :book, keyword_init: true) + + INSERT_NAME_CASE_SQL = <<~'SQL' + INSERT INTO name_cases (id, "class", "end", "hash", "method", "count", "camelCase", "HTTPServer") + VALUES (?, ?, ?, ?, ?, ?, + ?, ?) + SQL + + GET_NAME_CASE_SQL = <<~'SQL' + SELECT id, class, "end", hash, method, count, camelCase, HTTPServer FROM name_cases + WHERE id = ? AND "class" = ? AND "end" = ? AND "hash" = ? + AND "method" = ? AND "count" = ? + AND "HTTPServer" = ? AND "camelCase" = ? + SQL + + # rubocop:disable Naming/MethodName + GetNameCaseRow = Struct.new( + :id, + :class_, + :end, + :hash_, + :method_, + :count_, + :camelCase, + :HTTPServer, + keyword_init: true + ) + # rubocop:enable Naming/MethodName + + GET_HTTP_SERVER_SQL = <<~'SQL' + SELECT id, class, "end", hash, method, count, camelCase, HTTPServer FROM name_cases WHERE id = ? + SQL + + # rubocop:disable Naming/MethodName + GetHTTPServerRow = Struct.new( + :id, + :class_, + :end, + :hash_, + :method_, + :count_, + :camelCase, + :HTTPServer, + keyword_init: true + ) + # rubocop:enable Naming/MethodName + + TRUNCATE_NAME_CASES_SQL = <<~'SQL' + DELETE FROM name_cases + SQL + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -1024,6 +1082,122 @@ def truncate_tags end end + def list_authors_with_optional_book + with_client('list_authors_with_optional_book') do |client| + stmt = client.prepare(LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL) + begin + result = stmt.execute + result.map do |row| + ListAuthorsWithOptionalBookRow.new( + author: row[0].nil? && row[1].nil? ? nil : Author.new(id: row[0], name: row[1], bio: row[2]), + book: row[3].nil? && row[4].nil? && row[5].nil? ? nil : Book.new(id: row[3], name: row[4], author_id: row[5], description: row[6]) + ) + end + ensure + stmt.close + end + end + end + + def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, http_server:) + with_client('insert_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + camel_case, + http_server + ] + stmt = client.prepare(INSERT_NAME_CASE_SQL) + begin + stmt.execute(*query_params) + nil + ensure + stmt.close + end + end + end + + def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, camel_case:) + with_client('get_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + http_server, + camel_case + ] + stmt = client.prepare(GET_NAME_CASE_SQL) + begin + result = stmt.execute(*query_params) + row = result.first + row && GetNameCaseRow.new( + id: row[0], + class_: row[1], + end: row[2], + hash_: row[3], + method_: row[4], + count_: row[5], + camelCase: row[6], + HTTPServer: row[7] + ) + ensure + stmt.close + end + end + end + + # Like #get_name_case, but raises NotFound when there is no row. + def get_name_case!(...) + get_name_case(...) || raise(NotFound, 'GetNameCase returned no row') + end + + def get_http_server(id:) + with_client('get_http_server') do |client| + query_params = [id] + stmt = client.prepare(GET_HTTP_SERVER_SQL) + begin + result = stmt.execute(*query_params) + row = result.first + row && GetHTTPServerRow.new( + id: row[0], + class_: row[1], + end: row[2], + hash_: row[3], + method_: row[4], + count_: row[5], + camelCase: row[6], + HTTPServer: row[7] + ) + ensure + stmt.close + end + end + end + + # Like #get_http_server, but raises NotFound when there is no row. + def get_http_server!(...) + get_http_server(...) || raise(NotFound, 'GetHTTPServer returned no row') + end + + def truncate_name_cases + with_client('truncate_name_cases') do |client| + stmt = client.prepare(TRUNCATE_NAME_CASES_SQL) + begin + stmt.execute + nil + ensure + stmt.close + end + end + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ diff --git a/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs b/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs index 5efee66..e43ff5a 100644 --- a/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs +++ b/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs @@ -357,6 +357,85 @@ module Sqlite3Codegen TRUNCATE_TAGS_SQL: String + LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL: String + + class ListAuthorsWithOptionalBookRow < ::Struct[untyped] + attr_accessor author: Author? + attr_accessor book: Book? + def initialize: ( author: Author?, book: Book?) -> void + def self.new: ( author: Author?, book: Book?) -> ListAuthorsWithOptionalBookRow + end + + INSERT_NAME_CASE_SQL: String + + GET_NAME_CASE_SQL: String + + class GetNameCaseRow < ::Struct[untyped] + attr_accessor id: Integer + attr_accessor class_: String + attr_accessor end: String + attr_accessor hash_: String + attr_accessor method_: String + attr_accessor count_: Integer + attr_accessor camelCase: String + attr_accessor HTTPServer: String + def initialize: ( + id: Integer, + class_: String, + end: String, + hash_: String, + method_: String, + count_: Integer, + camelCase: String, + HTTPServer: String + ) -> void + def self.new: ( + id: Integer, + class_: String, + end: String, + hash_: String, + method_: String, + count_: Integer, + camelCase: String, + HTTPServer: String + ) -> GetNameCaseRow + end + + GET_HTTP_SERVER_SQL: String + + class GetHTTPServerRow < ::Struct[untyped] + attr_accessor id: Integer + attr_accessor class_: String + attr_accessor end: String + attr_accessor hash_: String + attr_accessor method_: String + attr_accessor count_: Integer + attr_accessor camelCase: String + attr_accessor HTTPServer: String + def initialize: ( + id: Integer, + class_: String, + end: String, + hash_: String, + method_: String, + count_: Integer, + camelCase: String, + HTTPServer: String + ) -> void + def self.new: ( + id: Integer, + class_: String, + end: String, + hash_: String, + method_: String, + count_: Integer, + camelCase: String, + HTTPServer: String + ) -> GetHTTPServerRow + end + + TRUNCATE_NAME_CASES_SQL: String + MANY_PARAMS_SQL: String class ManyParamsRow < ::Struct[untyped] @@ -508,6 +587,20 @@ module Sqlite3Codegen def truncate_tags: -> void + def list_authors_with_optional_book: -> Array[ListAuthorsWithOptionalBookRow] + + def insert_name_case: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, camel_case: String, http_server: String) -> void + + def get_name_case: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, http_server: String, camel_case: String) -> GetNameCaseRow? + + def get_name_case!: (id: Integer, class_: String, end_: String, hash: String, method: String, count: Integer, http_server: String, camel_case: String) -> GetNameCaseRow + + def get_http_server: (id: Integer) -> GetHTTPServerRow? + + def get_http_server!: (id: Integer) -> GetHTTPServerRow + + def truncate_name_cases: -> 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/models.rb b/test/golden/sqlite3/ruby32_notypes/models.rb index 365bef7..5a78ef5 100644 --- a/test/golden/sqlite3/ruby32_notypes/models.rb +++ b/test/golden/sqlite3/ruby32_notypes/models.rb @@ -25,4 +25,18 @@ class Regression < Data.define(:id, :order, :price, :payload) class Tag < Data.define(:name, :uses) end + + # rubocop:disable Naming/MethodName + class NameCase < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelCase, + :HTTPServer + ) + end + # rubocop:enable Naming/MethodName end diff --git a/test/golden/sqlite3/ruby32_notypes/query_sql.rb b/test/golden/sqlite3/ruby32_notypes/query_sql.rb index 5c2671e..7ca312c 100644 --- a/test/golden/sqlite3/ruby32_notypes/query_sql.rb +++ b/test/golden/sqlite3/ruby32_notypes/query_sql.rb @@ -314,6 +314,65 @@ class ListTagsRow < Data.define(:name, :uses) DELETE FROM tags SQL + LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL = <<~'SQL' + SELECT authors.id, authors.name, authors.bio, books.id, books.name, books.author_id, books.description + FROM authors + LEFT JOIN books ON authors.id = books.author_id + ORDER BY authors.id + SQL + + class ListAuthorsWithOptionalBookRow < Data.define(:author, :book) + end + + INSERT_NAME_CASE_SQL = <<~'SQL' + INSERT INTO name_cases (id, "class", "end", "hash", "method", "count", "camelCase", "HTTPServer") + VALUES (?, ?, ?, ?, ?, ?, + ?, ?) + SQL + + GET_NAME_CASE_SQL = <<~'SQL' + SELECT id, class, "end", hash, method, count, camelCase, HTTPServer FROM name_cases + WHERE id = ? AND "class" = ? AND "end" = ? AND "hash" = ? + AND "method" = ? AND "count" = ? + AND "HTTPServer" = ? AND "camelCase" = ? + SQL + + # rubocop:disable Naming/MethodName + class GetNameCaseRow < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelCase, + :HTTPServer + ) + end + # rubocop:enable Naming/MethodName + + GET_HTTP_SERVER_SQL = <<~'SQL' + SELECT id, class, "end", hash, method, count, camelCase, HTTPServer FROM name_cases WHERE id = ? + SQL + + # rubocop:disable Naming/MethodName + class GetHTTPServerRow < Data.define( + :id, + :class_, + :end, + :hash_, + :method_, + :count, + :camelCase, + :HTTPServer + ) + end + # rubocop:enable Naming/MethodName + + TRUNCATE_NAME_CASES_SQL = <<~'SQL' + DELETE FROM name_cases + SQL + MANY_PARAMS_SQL = <<~'SQL' SELECT 1 -- ? ? WHERE a = ? @@ -1030,6 +1089,122 @@ def truncate_tags end end + def list_authors_with_optional_book + with_client('list_authors_with_optional_book') do |client| + stmt = client.prepare(LIST_AUTHORS_WITH_OPTIONAL_BOOK_SQL) + begin + result = stmt.execute + result.map do |row| + ListAuthorsWithOptionalBookRow.new( + row[0].nil? && row[1].nil? ? nil : Author.new(row[0], row[1], row[2]), + row[3].nil? && row[4].nil? && row[5].nil? ? nil : Book.new(row[3], row[4], row[5], row[6]) + ) + end + ensure + stmt.close + end + end + end + + def insert_name_case(id:, class_:, end_:, hash:, method:, count:, camel_case:, http_server:) + with_client('insert_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + camel_case, + http_server + ] + stmt = client.prepare(INSERT_NAME_CASE_SQL) + begin + stmt.execute(*query_params) + nil + ensure + stmt.close + end + end + end + + def get_name_case(id:, class_:, end_:, hash:, method:, count:, http_server:, camel_case:) + with_client('get_name_case') do |client| + query_params = [ + id, + class_, + end_, + hash, + method, + count, + http_server, + camel_case + ] + stmt = client.prepare(GET_NAME_CASE_SQL) + begin + result = stmt.execute(*query_params) + row = result.first + row && GetNameCaseRow.new( + row[0], + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7] + ) + ensure + stmt.close + end + end + end + + # Like #get_name_case, but raises NotFound when there is no row. + def get_name_case!(...) + get_name_case(...) || raise(NotFound, 'GetNameCase returned no row') + end + + def get_http_server(id:) + with_client('get_http_server') do |client| + query_params = [id] + stmt = client.prepare(GET_HTTP_SERVER_SQL) + begin + result = stmt.execute(*query_params) + row = result.first + row && GetHTTPServerRow.new( + row[0], + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7] + ) + ensure + stmt.close + end + end + end + + # Like #get_http_server, but raises NotFound when there is no row. + def get_http_server!(...) + get_http_server(...) || raise(NotFound, 'GetHTTPServer returned no row') + end + + def truncate_name_cases + with_client('truncate_name_cases') do |client| + stmt = client.prepare(TRUNCATE_NAME_CASES_SQL) + begin + stmt.execute + nil + ensure + stmt.close + end + end + end + def many_params(c0:, c1:, c2:, c3:, c4:) with_client('many_params') do |client| query_params = [ diff --git a/tests/end2end_mysql2.rb b/tests/end2end_mysql2.rb index f8216f3..db30b1b 100755 --- a/tests/end2end_mysql2.rb +++ b/tests/end2end_mysql2.rb @@ -178,6 +178,12 @@ def test_batch_commands_run_once_per_args assert_nil @query_sql.create_authors_batch([]) end + def test_batches_are_not_atomic_without_a_transaction + rows = [CreateAuthorsBatchArgs.new(name: 'committed', bio: nil), CreateAuthorsBatchArgs.new(name: nil, bio: nil)] + assert_raises(Mysql2::Error) { @query_sql.create_authors_batch(rows) } + assert_equal ['committed'], @query_sql.list_authors.map(&:name) + end + def test_execrows_returns_affected_row_count create_author('with bio', 'bio') create_author('with bio 2', 'bio') diff --git a/tests/end2end_pg.rb b/tests/end2end_pg.rb index e74233f..1eb0451 100755 --- a/tests/end2end_pg.rb +++ b/tests/end2end_pg.rb @@ -165,6 +165,12 @@ def test_batch_commands_run_once_per_args assert_nil @query_sql.create_authors_batch([]) end + def test_batches_are_not_atomic_without_a_transaction + rows = [CreateAuthorsBatchArgs.new(name: 'committed', bio: nil), CreateAuthorsBatchArgs.new(name: nil, bio: nil)] + assert_raises(PG::NotNullViolation) { @query_sql.create_authors_batch(rows) } + assert_equal ['committed'], @query_sql.list_authors.map(&:name) + end + def test_execrows_returns_affected_row_count create_author('with bio', 'bio') create_author('with bio 2', 'bio') diff --git a/tests/end2end_sqlite3.rb b/tests/end2end_sqlite3.rb index a9a9f3b..b46a313 100644 --- a/tests/end2end_sqlite3.rb +++ b/tests/end2end_sqlite3.rb @@ -132,6 +132,12 @@ def test_batch_commands_run_once_per_args assert_nil @query_sql.create_authors_batch([]) end + def test_batches_are_not_atomic_without_a_transaction + rows = [CreateAuthorsBatchArgs.new(name: 'committed', bio: nil), CreateAuthorsBatchArgs.new(name: nil, bio: nil)] + assert_raises(SQLite3::ConstraintException) { @query_sql.create_authors_batch(rows) } + assert_equal ['committed'], @query_sql.list_authors.map(&:name) + end + def test_declared_types_with_parameters uid = '6f1c4a52-1d2b-4c3e-9f00-0a1b2c3d4e5f' @query_sql.insert_declared_types(c_varchar: 'v', c_decimal: 1.5, c_unsigned_big_int: 7, c_native: 'n', c_double: 2.5, From 1d5bd03870b4fffd38ff7023e423c663d0e144cf Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 21:09:14 +0000 Subject: [PATCH 8/9] Fix generator options E2E bundling Co-Authored-By: Doron Eli Rachman --- .github/workflows/tests.yml | 6 +++++- Gemfile.options.e2e | 6 ++++++ docs/Testing.md | 2 +- tests/end2end_options.rb | 13 +++++++------ 4 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 Gemfile.options.e2e diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 2652924..12e4ca4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -292,6 +292,7 @@ jobs: env: POSTGRES_HOST: 127.0.0.1 + BUNDLE_GEMFILE: Gemfile.options.e2e steps: - uses: actions/checkout@v7 @@ -299,7 +300,10 @@ jobs: - uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby-version }} - bundler-cache: true + bundler-cache: false + + - name: Install options test dependencies + run: bundle install - name: Load tool versions run: grep -E '^[A-Z_]+=' scripts/tool-versions.env >> "$GITHUB_ENV" diff --git a/Gemfile.options.e2e b/Gemfile.options.e2e new file mode 100644 index 0000000..da44f62 --- /dev/null +++ b/Gemfile.options.e2e @@ -0,0 +1,6 @@ +source 'https://rubygems.org' + +gem 'connection_pool', '~> 2.5' +gem 'minitest', RUBY_VERSION.start_with?('3.1') ? '< 6' : '>= 5' +gem 'pg', '1.7.0' +gem 'sqlite3' diff --git a/docs/Testing.md b/docs/Testing.md index f2b8cbe..47344ce 100644 --- a/docs/Testing.md +++ b/docs/Testing.md @@ -57,7 +57,7 @@ bundle exec rake test rubocop steep sqlc -f sqlc.local.yaml diff # committed examples must be current scripts/tests/run_codegen.rb sqlc.local.yaml 3.1 true true rake end2end_sqlite3 end2end_pg end2end_mysql2 # full Ruby 3.1–4.0 matrix, plus Ruby 4.0 variants -bundle exec ruby tests/end2end_options.rb # Ruby 3.1 and 4.0 +BUNDLE_GEMFILE=Gemfile.options.e2e bundle exec ruby tests/end2end_options.rb # Ruby 3.1 and 4.0 ``` CI also runs the unit suite without Bundler on Ruby 3.1 and 4.0. For Ruby 3.1/3.2 E2E, generate scratch examples with their matching `rubyVersion` and install the generated Gemfile before running the suite. diff --git a/tests/end2end_options.rb b/tests/end2end_options.rb index 70f6692..f8f51bd 100644 --- a/tests/end2end_options.rb +++ b/tests/end2end_options.rb @@ -3,6 +3,7 @@ gem 'minitest' require 'minitest/autorun' +require 'bundler' require 'fileutils' require 'connection_pool' require 'open3' @@ -62,15 +63,15 @@ def assert_model_defined(source, name) def assert_sqlite_runtime(generated_dir) gemfile = File.join(generated_dir, 'Gemfile') - stdout, stderr, status = Open3.capture3( - { 'BUNDLE_GEMFILE' => gemfile, 'OPTIONS_SQLITE_DIR' => generated_dir, - 'OPTIONS_SCHEMA' => File.join(OPTIONS_DIR, 'sqlite_schema.sql') }, - 'bundle', 'install' + bundle_env = Bundler.unbundled_env.merge( + 'BUNDLE_BIN_PATH' => nil, 'BUNDLE_PATH' => ENV['BUNDLE_PATH'], + 'BUNDLE_GEMFILE' => gemfile, 'OPTIONS_SQLITE_DIR' => generated_dir, + 'OPTIONS_SCHEMA' => File.join(OPTIONS_DIR, 'sqlite_schema.sql') ) + stdout, stderr, status = Open3.capture3(bundle_env, 'bundle', 'install') assert status.success?, "generated-Gemfile bundle install failed:\n#{stdout}\n#{stderr}" stdout, stderr, status = Open3.capture3( - { 'BUNDLE_GEMFILE' => gemfile, 'OPTIONS_SQLITE_DIR' => generated_dir, - 'OPTIONS_SCHEMA' => File.join(OPTIONS_DIR, 'sqlite_schema.sql') }, + bundle_env, 'bundle', 'exec', RbConfig.ruby, '-e', <<~'RUBY' require File.join(ENV.fetch('OPTIONS_SQLITE_DIR'), 'first_sql') require File.join(ENV.fetch('OPTIONS_SQLITE_DIR'), 'second_sql') From 33c91dc20848f13f4b96f8151d7549fb7b079146 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 21:12:34 +0000 Subject: [PATCH 9/9] Run generated bundle without outer Bundler env Co-Authored-By: Doron Eli Rachman --- tests/end2end_options.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/end2end_options.rb b/tests/end2end_options.rb index f8f51bd..5615c39 100644 --- a/tests/end2end_options.rb +++ b/tests/end2end_options.rb @@ -64,7 +64,7 @@ def assert_model_defined(source, name) def assert_sqlite_runtime(generated_dir) gemfile = File.join(generated_dir, 'Gemfile') bundle_env = Bundler.unbundled_env.merge( - 'BUNDLE_BIN_PATH' => nil, 'BUNDLE_PATH' => ENV['BUNDLE_PATH'], + 'BUNDLE_BIN_PATH' => nil, 'BUNDLE_PATH' => ENV['BUNDLE_PATH'], 'RUBYOPT' => nil, 'BUNDLE_GEMFILE' => gemfile, 'OPTIONS_SQLITE_DIR' => generated_dir, 'OPTIONS_SCHEMA' => File.join(OPTIONS_DIR, 'sqlite_schema.sql') )