diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ab56807..12e4ca4 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.3', '4.0'] + 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,10 @@ 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: - uses: actions/checkout@v7 @@ -95,7 +103,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' || 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' || 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 --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 @@ -106,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.3', '4.0'] + ruby-version: ['3.1', '3.2', '3.3', '3.4', '4.0'] + variant: ['default'] + include: + - ruby-version: '4.0' + variant: 'module-name' services: mysql: @@ -130,6 +167,9 @@ 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: - uses: actions/checkout@v7 @@ -137,7 +177,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' || 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' || 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 --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: | @@ -148,13 +213,22 @@ 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.3', '4.0'] + 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: - uses: actions/checkout@v7 @@ -162,7 +236,81 @@ 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' || 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' || 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 --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 + + 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 + BUNDLE_GEMFILE: Gemfile.options.e2e + + steps: + - uses: actions/checkout@v7 + + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby-version }} + 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" + + - 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/.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/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/Gemfile.e2e b/Gemfile.e2e new file mode 100644 index 0000000..0b924e0 --- /dev/null +++ b/Gemfile.e2e @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +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 'minitest' +gem 'rake' 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/README.md b/README.md index 6c5b7af..9f09c74 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,7 +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` and `mysql2` queries run concurrently. 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`. @@ -115,7 +118,11 @@ 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. 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). @@ -170,7 +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 -* 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 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. @@ -179,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/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/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/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/docs/Testing.md b/docs/Testing.md index cbe6614..47344ce 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_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/examples/authors/mysql/query.sql b/examples/authors/mysql/query.sql index 9ebbb5e..e8a4d61 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,15 @@ 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: 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; @@ -139,3 +157,45 @@ 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; + +-- 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 e9985c9..ac8b339 100644 --- a/examples/authors/mysql/schema.sql +++ b/examples/authors/mysql/schema.sql @@ -78,3 +78,33 @@ 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, + 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 cbffa4c..89a04d9 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), @@ -102,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); @@ -178,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 573e1dc..5123fd8 100644 --- a/examples/authors/postgresql/schema.sql +++ b/examples/authors/postgresql/schema.sql @@ -37,8 +37,16 @@ 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 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 ( @@ -59,6 +67,28 @@ 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, + jsonb_value JSONB +); + CREATE TABLE postgres_array_types ( c_uuid_array UUID [], c_bio_type_array extended.bio_type [], @@ -102,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 faf2146..9100a64 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,10 +48,40 @@ 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: 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; @@ -113,6 +149,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 (?, ?); @@ -127,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 5b78e26..ebec882 100644 --- a/examples/authors/sqlite/schema.sql +++ b/examples/authors/sqlite/schema.sql @@ -26,10 +26,22 @@ 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 ( 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 07b421a..444d3df 100644 --- a/examples/mysql2/models.rb +++ b/examples/mysql2/models.rb @@ -78,4 +78,38 @@ 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, + :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 15dee64..224d903 100644 --- a/examples/mysql2/models.rbs +++ b/examples/mysql2/models.rbs @@ -138,4 +138,62 @@ 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? + 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, + 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, + 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 298ad97..18c82ad 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' @@ -114,6 +116,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 +264,23 @@ 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 + + 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 @@ -300,6 +336,108 @@ 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, bit_value, year_value, time_value, json_unicode 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, + :bit_value, + :year_value, + :time_value, + :json_unicode + ) + end + # rubocop:enable Naming/VariableNumber + + TRUNCATE_MYSQL_BINDING_VALUES_SQL = <<~'SQL' + 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 @@ -361,14 +499,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 @@ -379,14 +512,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 @@ -394,103 +522,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 @@ -502,14 +605,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 @@ -517,29 +615,37 @@ 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 + 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] + 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 + + def pause_query(seconds:) + with_client('pause_query') do |client| + query_params = [seconds] + client.query(bind_params(client, PAUSE_QUERY_SQL, query_params), as: :array) + nil + 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| 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 @@ -547,14 +653,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 @@ -564,14 +665,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 @@ -594,56 +690,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 @@ -651,14 +727,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 @@ -666,27 +737,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 @@ -697,26 +758,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 @@ -724,12 +775,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 @@ -739,14 +785,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 @@ -755,14 +796,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 @@ -771,14 +807,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 @@ -790,14 +821,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 @@ -834,27 +860,38 @@ 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| + 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 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| - 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 @@ -907,12 +944,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 @@ -922,50 +954,194 @@ 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], + row[12], + row[13], + row[14], + row[15] + ) + 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 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) @@ -995,6 +1171,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 31d4b61..6e6234a 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,24 @@ 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 + + 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 @@ -342,6 +376,113 @@ 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? + 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, + 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, + 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 @@ -380,6 +521,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 +571,10 @@ module Mysql2Codegen def list_regression_payloads: -> Array[ListRegressionPayloadsRow] + 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 @@ -442,12 +591,40 @@ 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 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 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/examples/pg/models.rb b/examples/pg/models.rb index b9a3073..c690651 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,26 @@ 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 + class PostgresArrayType < Data.define(:c_uuid_array, :c_bio_type_array, :c_integer_matrix) end @@ -88,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 edc0520..a9f570d 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,44 @@ 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 + 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]? @@ -151,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 e8bb3ad..6b52d98 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, @@ -203,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 @@ -398,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 @@ -551,6 +680,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 +773,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 +878,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') @@ -812,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)] @@ -1184,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) @@ -1237,9 +1631,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..3809e46 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 @@ -198,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 @@ -432,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 @@ -464,11 +585,25 @@ 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 create_author_return_id: (name: String, bio: String?) -> Integer + 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 @@ -480,7 +615,11 @@ 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 + + def truncate_deferred_books: -> void def list_all_authors_books: -> Array[ListAllAuthorsBooksRow] @@ -504,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? @@ -536,7 +683,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 @@ -558,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 498e262..5a78ef5 100644 --- a/examples/sqlite3/models.rb +++ b/examples/sqlite3/models.rb @@ -20,9 +20,23 @@ 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) 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 60532b3..4ea1aac 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 @@ -46,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 38b041b..8ee07ff 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,12 +48,22 @@ 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) 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 + CREATE_AUTHOR_RETURN_ID_SQL = <<~'SQL' INSERT INTO authors (name, bio) VALUES (?, ?) RETURNING id SQL @@ -81,9 +91,54 @@ 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 + + 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 @@ -218,6 +273,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 @@ -238,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 @@ -347,7 +482,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) @@ -375,6 +510,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, 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 +586,64 @@ 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 + + # 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| @@ -449,6 +657,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) @@ -723,6 +943,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 @@ -785,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 0f3f568..581c04e 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,12 +76,52 @@ 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 + + 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 @@ -209,6 +258,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 @@ -237,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 @@ -265,7 +399,9 @@ module Sqlite3Codegen def get_author_by_name_pattern: (?name_pattern: String?) -> Array[GetAuthorByNamePatternRow] - def create_author_return_id: (name: String, bio: String?) -> Integer + 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,11 +409,19 @@ 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 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 + def create_book: (name: String, author_id: Integer) -> Integer? def list_all_authors_books: -> Array[ListAllAuthorsBooksRow] @@ -315,6 +459,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 @@ -325,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/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..c321c61 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,15 @@ 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, 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 + # 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 +168,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 +209,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..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' @@ -15,29 +15,38 @@ 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})" + 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 @@ -46,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`). @@ -88,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 @@ -227,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/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..1e89273 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,90 @@ def closes_statements? = true def supports_slice? = true + def query_text(query) + 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, 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] } + 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 do |param| + value = "#{value_prefix}#{param.name}" + param.slice ? "*#{value}" : encode_param(value, param.column) + end + "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..8e35e1b 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)}" @@ -145,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) @@ -195,7 +199,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) @@ -222,7 +226,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? @@ -245,9 +249,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..fce5914 --- /dev/null +++ b/scripts/tests/generate_examples.rb @@ -0,0 +1,60 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require 'fileutils' +require 'json' +require 'yaml' + +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, 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['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').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") +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..8f0a535 100644 --- a/steep_expectations.yml +++ b/steep_expectations.yml @@ -3,50 +3,140 @@ diagnostics: - range: start: - line: 310 + line: 448 character: 10 end: - line: 310 + line: 448 character: 24 severity: WARNING message: 'Cannot find the declaration of constant: `ConnectionPool`' code: Ruby::UnknownConstant - range: start: - line: 310 + line: 448 character: 47 end: - line: 310 + line: 448 character: 53 severity: WARNING message: 'Cannot find the declaration of constant: `Mysql2`' code: Ruby::UnknownConstant - range: start: - line: 310 + line: 448 character: 66 end: - line: 310 + line: 448 character: 81 severity: WARNING message: Empty kwargs doesn't have type annotation code: Ruby::UnannotatedEmptyCollection - range: start: - line: 989 + line: 1165 character: 17 end: - line: 989 + line: 1165 character: 23 severity: WARNING message: 'Cannot find the declaration of constant: `Mysql2`' code: Ruby::UnknownConstant - range: start: - line: 1022 + line: 1227 + character: 20 + end: + line: 1227 + 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: 1228 + character: 78 + end: + line: 1228 + character: 84 + severity: ERROR + message: Type `(::String | nil)` does not have method `match?` + code: Ruby::NoMethod + - range: + start: + line: 1230 + character: 20 + end: + line: 1230 + 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: 1234 + character: 20 + end: + line: 1234 + 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: 1239 + character: 20 + end: + line: 1239 + 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: 1248 + character: 20 + end: + line: 1248 + 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: 1281 character: 16 end: - line: 1022 + line: 1281 character: 22 severity: WARNING message: 'Cannot find the declaration of constant: `Mysql2`' @@ -55,260 +145,260 @@ diagnostics: - range: start: - line: 408 + line: 537 character: 10 end: - line: 408 + line: 537 character: 24 severity: WARNING message: 'Cannot find the declaration of constant: `ConnectionPool`' code: Ruby::UnknownConstant - range: start: - line: 408 + line: 537 character: 47 end: - line: 408 + line: 537 character: 49 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 408 + line: 537 character: 58 end: - line: 408 + line: 537 character: 69 severity: WARNING message: Empty kwargs doesn't have type annotation code: Ruby::UnannotatedEmptyCollection - range: start: - line: 423 + line: 552 character: 49 end: - line: 423 + line: 552 character: 51 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 443 + line: 572 character: 51 end: - line: 443 + line: 572 character: 53 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 447 + line: 576 character: 18 end: - line: 447 + line: 576 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 639 + line: 847 character: 18 end: - line: 639 + line: 847 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1045 + line: 1344 character: 18 end: - line: 1045 + line: 1344 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1126 + line: 1425 character: 18 end: - line: 1126 + line: 1425 character: 20 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1204 + line: 1598 character: 43 end: - line: 1204 + line: 1598 character: 45 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1213 + line: 1607 character: 17 end: - line: 1213 + line: 1607 character: 19 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1216 + line: 1610 character: 16 end: - line: 1216 + line: 1610 character: 18 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1218 + line: 1612 character: 49 end: - line: 1218 + line: 1612 character: 51 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1225 + line: 1619 character: 16 end: - line: 1225 + line: 1619 character: 18 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1226 + line: 1620 character: 15 end: - line: 1226 + line: 1620 character: 17 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1227 + line: 1621 character: 79 end: - line: 1227 + line: 1621 character: 81 severity: WARNING message: Empty array doesn't have type annotation code: Ruby::UnannotatedEmptyCollection - range: start: - line: 1228 + line: 1622 character: 25 end: - line: 1228 + line: 1622 character: 27 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1229 + line: 1623 character: 67 end: - line: 1229 + line: 1623 character: 69 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1237 + line: 1631 character: 15 end: - line: 1237 + line: 1631 character: 17 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1242 - character: 27 + line: 1638 + character: 45 end: - line: 1242 - character: 29 + line: 1638 + character: 47 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1255 + line: 1653 character: 6 end: - line: 1255 + line: 1653 character: 8 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1255 + line: 1653 character: 48 end: - line: 1255 + line: 1653 character: 50 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1262 + line: 1660 character: 40 end: - line: 1262 + line: 1660 character: 42 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1263 + line: 1661 character: 23 end: - line: 1263 + line: 1661 character: 25 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1264 + line: 1662 character: 42 end: - line: 1264 + line: 1662 character: 44 severity: WARNING message: 'Cannot find the declaration of constant: `PG`' code: Ruby::UnknownConstant - range: start: - line: 1265 + line: 1663 character: 23 end: - line: 1265 + 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 6806a70..5340a3a 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 ------------------------------------------------------------------------ @@ -425,10 +441,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 +455,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' @@ -539,12 +583,65 @@ 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' 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.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 @@ -346,4 +419,170 @@ 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, 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 +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"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Ð +‰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 762b6bc..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 @@ -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,34 @@ 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 + +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 @@ -94,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< @@ -163,7 +215,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 +252,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– $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 c2e55dc..3251dc7 100644 Binary files a/test/fixtures/sqlite3.bin and b/test/fixtures/sqlite3.bin differ diff --git a/test/golden/mysql2/default/models.rb b/test/golden/mysql2/default/models.rb index 07b421a..444d3df 100644 --- a/test/golden/mysql2/default/models.rb +++ b/test/golden/mysql2/default/models.rb @@ -78,4 +78,38 @@ 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, + :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 15dee64..224d903 100644 --- a/test/golden/mysql2/default/models.rbs +++ b/test/golden/mysql2/default/models.rbs @@ -138,4 +138,62 @@ 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? + 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, + 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, + 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/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 d49bcfa..39d167b 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' @@ -114,6 +116,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 +264,23 @@ 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 + + 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 @@ -300,6 +336,108 @@ 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, bit_value, year_value, time_value, json_unicode 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, + :bit_value, + :year_value, + :time_value, + :json_unicode + ) + end + # rubocop:enable Naming/VariableNumber + + TRUNCATE_MYSQL_BINDING_VALUES_SQL = <<~'SQL' + 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 = ? @@ -391,14 +529,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 @@ -409,14 +542,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 @@ -424,103 +552,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 @@ -532,14 +635,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 @@ -547,29 +645,37 @@ 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 + + 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] + 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 + def pause_query(seconds:) + with_client('pause_query') do |client| + query_params = [seconds] + client.query(bind_params(client, PAUSE_QUERY_SQL, query_params), as: :array) + nil + 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| 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 @@ -577,14 +683,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 @@ -594,14 +695,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 @@ -624,56 +720,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 @@ -681,14 +757,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 @@ -696,27 +767,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 @@ -727,26 +788,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 @@ -754,12 +805,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 @@ -769,14 +815,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 @@ -785,14 +826,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 @@ -801,14 +837,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 @@ -820,14 +851,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 @@ -864,27 +890,38 @@ 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| + 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 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| - 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 @@ -937,12 +974,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 @@ -952,50 +984,194 @@ 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], + row[12], + row[13], + row[14], + row[15] + ) + 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 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 = [ @@ -1005,14 +1181,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 @@ -1024,34 +1195,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 @@ -1085,6 +1246,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 b734145..615543b 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,24 @@ 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 + + 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 @@ -342,6 +376,113 @@ 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? + 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, + 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, + 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 @@ -415,6 +556,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 +606,10 @@ module Mysql2Codegen def list_regression_payloads: -> Array[ListRegressionPayloadsRow] + 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 @@ -477,6 +626,30 @@ 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 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 @@ -491,6 +664,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 ba8c9e9..2c8274f 100644 --- a/test/golden/mysql2/ruby31_gemfile/Gemfile +++ b/test/golden/mysql2/ruby31_gemfile/Gemfile @@ -2,5 +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..d1899e5 100644 --- a/test/golden/mysql2/ruby31_gemfile/models.rb +++ b/test/golden/mysql2/ruby31_gemfile/models.rb @@ -92,4 +92,38 @@ 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, + :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 e5769a0..d782060 100644 --- a/test/golden/mysql2/ruby31_gemfile/models.rbs +++ b/test/golden/mysql2/ruby31_gemfile/models.rbs @@ -188,4 +188,90 @@ 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? + 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?, + 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?, + bit_value: String?, + year_value: Integer?, + time_value: Time?, + json_unicode: 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?, + 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/my_queries_sql.rb b/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rb index 7e3b3a8..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' @@ -47,8 +49,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 +62,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 @@ -69,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 @@ -107,5 +107,93 @@ 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 + + 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 31a9283..23d610a 100644 --- a/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rbs +++ b/test/golden/mysql2/ruby31_gemfile/my_queries_sql.rbs @@ -19,5 +19,11 @@ module Mysql2Codegen def instrument: [T] (String name) { () -> T } -> T 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 c55138e..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' @@ -59,8 +61,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 +74,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 @@ -87,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 @@ -118,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 @@ -156,5 +146,93 @@ 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 + + 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 118f7bf..ad35cec 100644 --- a/test/golden/mysql2/ruby31_gemfile/other_sql.rbs +++ b/test/golden/mysql2/ruby31_gemfile/other_sql.rbs @@ -35,5 +35,11 @@ module Mysql2Codegen def instrument: [T] (String name) { () -> T } -> T 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 da336be..20394f5 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' @@ -110,6 +112,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 +259,27 @@ 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 + ) + + 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 @@ -298,6 +336,106 @@ 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, bit_value, year_value, time_value, json_unicode 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, + :bit_value, + :year_value, + :time_value, + :json_unicode, + keyword_init: true + ) + # rubocop:enable Naming/VariableNumber + + TRUNCATE_MYSQL_BINDING_VALUES_SQL = <<~'SQL' + 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 = ? @@ -367,8 +505,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 +518,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 @@ -392,14 +533,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 @@ -410,14 +546,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 @@ -425,103 +556,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 @@ -533,14 +639,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 @@ -548,29 +649,37 @@ 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 + 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] + 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 + + def pause_query(seconds:) + with_client('pause_query') do |client| + query_params = [seconds] + client.query(bind_params(client, PAUSE_QUERY_SQL, query_params), as: :array) + nil + 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| 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 @@ -578,14 +687,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 @@ -595,14 +699,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 @@ -625,59 +724,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 @@ -685,19 +764,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 @@ -705,32 +779,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 @@ -741,26 +805,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 @@ -768,12 +822,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 @@ -783,14 +832,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 @@ -799,14 +843,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 @@ -815,14 +854,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 @@ -834,14 +868,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 @@ -878,27 +907,38 @@ 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| + 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 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| - 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 @@ -951,12 +991,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 @@ -966,50 +1001,194 @@ 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], + bit_value: row[12], + year_value: row[13], + time_value: row[14], + json_unicode: row[15] + ) + 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 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 = [ @@ -1019,14 +1198,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 @@ -1038,34 +1212,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 @@ -1099,6 +1263,94 @@ def with_client(name) end end + 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 + # 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..dcfc87f 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,26 @@ 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 + + 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] @@ -421,6 +459,153 @@ 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? + 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?, + 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?, + bit_value: String?, + year_value: Integer?, + time_value: Time?, + json_unicode: 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?, + 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] @@ -504,6 +689,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 +739,10 @@ module Mysql2Codegen def list_regression_payloads: -> Array[ListRegressionPayloadsRow] + 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 @@ -566,6 +759,30 @@ 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 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 @@ -580,6 +797,12 @@ 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 + 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..444d3df 100644 --- a/test/golden/mysql2/ruby32_notypes/models.rb +++ b/test/golden/mysql2/ruby32_notypes/models.rb @@ -78,4 +78,38 @@ 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, + :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/my_queries_sql.rb b/test/golden/mysql2/ruby32_notypes/my_queries_sql.rb index 7e3b3a8..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' @@ -47,8 +49,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 +62,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 @@ -69,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 @@ -107,5 +107,93 @@ 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 + + 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 c067da1..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' @@ -60,8 +62,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 +75,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 @@ -88,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 @@ -119,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 @@ -157,5 +147,93 @@ 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 + + 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 d49bcfa..47e9599 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' @@ -114,6 +116,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 +264,23 @@ 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 + + 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 @@ -300,6 +336,108 @@ 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, bit_value, year_value, time_value, json_unicode 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, + :bit_value, + :year_value, + :time_value, + :json_unicode + ) + end + # rubocop:enable Naming/VariableNumber + + TRUNCATE_MYSQL_BINDING_VALUES_SQL = <<~'SQL' + 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 = ? @@ -365,8 +503,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 +516,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 @@ -391,14 +532,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 @@ -409,14 +545,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 @@ -424,103 +555,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 @@ -532,14 +638,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 @@ -547,29 +648,37 @@ 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 + + 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] + 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 + def pause_query(seconds:) + with_client('pause_query') do |client| + query_params = [seconds] + client.query(bind_params(client, PAUSE_QUERY_SQL, query_params), as: :array) + nil + 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| 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 @@ -577,14 +686,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 @@ -594,14 +698,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 @@ -624,56 +723,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 @@ -681,14 +760,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 @@ -696,27 +770,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 @@ -727,26 +791,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 @@ -754,12 +808,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 @@ -769,14 +818,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 @@ -785,14 +829,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 @@ -801,14 +840,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 @@ -820,14 +854,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 @@ -864,27 +893,38 @@ 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| + 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 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| - 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 @@ -937,12 +977,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 @@ -952,50 +987,194 @@ 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], + row[12], + row[13], + row[14], + row[15] + ) + 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 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 = [ @@ -1005,14 +1184,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 @@ -1024,34 +1198,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 @@ -1085,6 +1249,94 @@ def with_client(name) end end + 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 + # 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..c690651 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,26 @@ 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 + class PostgresArrayType < Data.define(:c_uuid_array, :c_bio_type_array, :c_integer_matrix) end @@ -88,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 edc0520..a9f570d 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,44 @@ 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 + 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]? @@ -151,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 462945e..d761b02 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, @@ -203,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 @@ -398,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 = ? @@ -581,6 +710,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 +803,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 +908,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') @@ -842,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)] @@ -1214,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 = [ @@ -1324,9 +1718,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..c6bd118 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 @@ -198,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 @@ -432,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 @@ -499,11 +620,25 @@ 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 create_author_return_id: (name: String, bio: String?) -> Integer + 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 @@ -515,7 +650,11 @@ 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 + + def truncate_deferred_books: -> void def list_all_authors_books: -> Array[ListAllAuthorsBooksRow] @@ -539,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? @@ -571,7 +718,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 @@ -593,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/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..bbf8948 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,25 @@ 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) PostgresSpecialType = Struct.new( @@ -91,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 e9b3bc8..4874095 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,60 @@ 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 + 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]? @@ -211,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/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..4e27bf6 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, @@ -196,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 @@ -391,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 = ? @@ -458,8 +584,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 +598,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 +706,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 +799,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 +904,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') @@ -850,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)] @@ -1226,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 = [ @@ -1331,14 +1725,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..0e50262 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] @@ -242,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 @@ -543,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] @@ -620,11 +779,25 @@ 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 create_author_return_id: (name: String, bio: String?) -> Integer + 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 @@ -636,7 +809,11 @@ 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 + + def truncate_deferred_books: -> void def list_all_authors_books: -> Array[ListAllAuthorsBooksRow] @@ -660,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? @@ -692,7 +877,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 @@ -714,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 @@ -732,6 +929,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..c690651 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,26 @@ 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 + class PostgresArrayType < Data.define(:c_uuid_array, :c_bio_type_array, :c_integer_matrix) end @@ -88,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/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..7c91085 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, @@ -203,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 @@ -398,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 = ? @@ -461,8 +590,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 +604,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 +713,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 +806,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 +911,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') @@ -842,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)] @@ -1214,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 = [ @@ -1319,14 +1716,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..5a78ef5 100644 --- a/test/golden/sqlite3/default/models.rb +++ b/test/golden/sqlite3/default/models.rb @@ -20,9 +20,23 @@ 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) 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 60532b3..4ea1aac 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 @@ -46,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 5fe3381..e55cd17 100644 --- a/test/golden/sqlite3/default/query_sql.rb +++ b/test/golden/sqlite3/default/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,12 +48,22 @@ 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) 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 + CREATE_AUTHOR_RETURN_ID_SQL = <<~'SQL' INSERT INTO authors (name, bio) VALUES (?, ?) RETURNING id SQL @@ -81,9 +91,54 @@ 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 + + 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 @@ -218,6 +273,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 @@ -238,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 = ? @@ -377,7 +512,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) @@ -405,6 +540,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, 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 +616,64 @@ 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 + + # 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| @@ -479,6 +687,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) @@ -753,6 +973,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 @@ -815,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 8d2029f..0da7229 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,12 +76,52 @@ 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 + + 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 @@ -209,6 +258,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 @@ -237,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 @@ -300,7 +434,9 @@ module Sqlite3Codegen def get_author_by_name_pattern: (?name_pattern: String?) -> Array[GetAuthorByNamePatternRow] - def create_author_return_id: (name: String, bio: String?) -> Integer + 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,11 +444,19 @@ 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 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 + def create_book: (name: String, author_id: Integer) -> Integer? def list_all_authors_books: -> Array[ListAllAuthorsBooksRow] @@ -350,6 +494,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 @@ -360,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/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..85f97a1 100644 --- a/test/golden/sqlite3/ruby31_gemfile/models.rb +++ b/test/golden/sqlite3/ruby31_gemfile/models.rb @@ -24,7 +24,27 @@ 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) + + # 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 27900f5..bda472f 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] @@ -59,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/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..7a8a70e 100644 --- a/test/golden/sqlite3/ruby31_gemfile/query_sql.rb +++ b/test/golden/sqlite3/ruby31_gemfile/query_sql.rb @@ -38,18 +38,27 @@ 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 (? IS NULL OR name = ?) + ORDER BY id + LIMIT ? + 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,8 +83,48 @@ 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) + + 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 @@ -202,6 +251,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) @@ -218,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 = ? @@ -280,8 +417,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 +429,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 @@ -360,7 +500,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) @@ -388,6 +528,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, 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 +604,64 @@ 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 + + # 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| @@ -462,6 +675,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) @@ -744,6 +969,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 @@ -806,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 = [ @@ -884,6 +1276,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..e43ff5a 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,44 @@ 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 + + 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? @@ -81,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 @@ -240,6 +295,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 @@ -272,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] @@ -345,7 +509,9 @@ module Sqlite3Codegen def get_author_by_name_pattern: (?name_pattern: String?) -> Array[GetAuthorByNamePatternRow] - def create_author_return_id: (name: String, bio: String?) -> Integer + 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,11 +519,19 @@ 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 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 + def create_book: (name: String, author_id: Integer) -> Integer? def list_all_authors_books: -> Array[ListAllAuthorsBooksRow] @@ -395,6 +569,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 @@ -405,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 @@ -419,6 +615,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..5a78ef5 100644 --- a/test/golden/sqlite3/ruby32_notypes/models.rb +++ b/test/golden/sqlite3/ruby32_notypes/models.rb @@ -20,9 +20,23 @@ 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) 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/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..7ca312c 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,12 +48,22 @@ 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) 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 + CREATE_AUTHOR_RETURN_ID_SQL = <<~'SQL' INSERT INTO authors (name, bio) VALUES (?, ?) RETURNING id SQL @@ -81,9 +91,54 @@ 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 + + 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 @@ -218,6 +273,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 @@ -238,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 = ? @@ -296,8 +431,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 +443,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 @@ -377,7 +515,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) @@ -405,6 +543,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, 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 +619,64 @@ 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 + + # 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| @@ -479,6 +690,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) @@ -753,6 +976,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 @@ -815,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 = [ @@ -893,6 +1283,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..280c92c --- /dev/null +++ b/test/sqlite_placeholders_test.rb @@ -0,0 +1,106 @@ +# 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_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))]) + 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_mysql2.rb b/tests/end2end_mysql2.rb index fe81bea..db30b1b 100755 --- a/tests/end2end_mysql2.rb +++ b/tests/end2end_mysql2.rb @@ -4,15 +4,71 @@ require 'logger' require 'stringio' require 'json' +require 'bigdecimal' +require 'date' 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') +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 + + 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) + 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 { @@ -30,19 +86,29 @@ def setup @query_sql.truncate_extended_bios @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) @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 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 @@ -54,6 +120,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 @@ -63,11 +137,18 @@ def test_one_on_empty_table_returns_nil assert_nil @query_sql.test end - def test_prepared_statements_are_closed - 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' + 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 ----------------------------------------------------------------------------------- @@ -87,16 +168,22 @@ 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 + 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') @@ -119,6 +206,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 +265,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 @@ -194,6 +287,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 @@ -201,9 +304,18 @@ 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 + @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 ---------------------------------------------------------------------------------------------- @@ -246,7 +358,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 +371,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 @@ -302,9 +414,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 @@ -315,17 +444,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 + # 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) - 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" @@ -339,6 +463,117 @@ def test_queries_run_concurrently_under_the_fiber_scheduler 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 + ) + @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 + 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 + 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 + 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 @@ -349,13 +584,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 @@ -373,7 +615,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 @@ -381,6 +623,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) }) @@ -428,7 +684,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 +804,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_options.rb b/tests/end2end_options.rb new file mode 100644 index 0000000..5615c39 --- /dev/null +++ b/tests/end2end_options.rb @@ -0,0 +1,114 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +gem 'minitest' +require 'minitest/autorun' +require 'bundler' +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') + bundle_env = Bundler.unbundled_env.merge( + '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') + ) + 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_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') + 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 04e99a6..1eb0451 100755 --- a/tests/end2end_pg.rb +++ b/tests/end2end_pg.rb @@ -8,13 +8,67 @@ 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') +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 + + 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) + 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 { @@ -31,10 +85,14 @@ 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 def create_author(name, bio = nil) @@ -45,19 +103,27 @@ 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 + 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 @@ -89,16 +155,22 @@ 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 + 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') @@ -123,6 +195,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 +245,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 @@ -203,14 +281,33 @@ 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 @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 + 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 @@ -253,6 +350,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 @@ -295,6 +397,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 +428,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 +437,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 @@ -325,6 +450,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 @@ -337,7 +499,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 +513,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 +524,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 @@ -410,6 +572,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 @@ -426,11 +589,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 @@ -456,13 +618,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 @@ -493,8 +662,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 +689,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 +727,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 +855,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 +886,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..b46a313 100644 --- a/tests/end2end_sqlite3.rb +++ b/tests/end2end_sqlite3.rb @@ -4,19 +4,72 @@ require 'logger' 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' -require_relative '../examples/sqlite3/query_sql' +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 + + 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}" + 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 +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 @@ -39,6 +92,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 @@ -61,23 +122,29 @@ 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 + 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, 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 +169,26 @@ 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 + + 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 @@ -121,6 +208,56 @@ 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 + + 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 @@ -133,6 +270,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 @@ -151,7 +299,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 @@ -172,6 +320,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 @@ -197,7 +355,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 @@ -243,6 +401,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 @@ -253,13 +448,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 @@ -336,17 +538,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 +620,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) / 101.0, + 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/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 +); diff --git a/tests/rbs_types.rb b/tests/rbs_types.rb index 8fb14e3..2afc583 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 @@ -24,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('?')