diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md
index bf67ecb..54b6b15 100644
--- a/.github/pull_request_template.md
+++ b/.github/pull_request_template.md
@@ -5,4 +5,4 @@
- [ ] `examples/` is regenerated (`rake sqlc_generate_process`) and committed
- [ ] Tests added or updated (`rake test`, and `rake end2end_*` when the generated code changes)
- [ ] The generated code stays RuboCop-clean (`rake rubocop`) and Steep-clean (`rake steep`)
-- [ ] Labelled (`feature`, `bug`, `breaking`, `documentation`) so the release notes are grouped correctly
+- [ ] Labelled (`bug`, `enhancement`, `documentation`, or `breaking`) so the release notes are grouped correctly
diff --git a/CLAUDE.md b/CLAUDE.md
index e98a5f5..94079c4 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -36,7 +36,7 @@ UPDATE_GOLDEN=1 bundle exec ruby -Itest test/extended_golden_test.rb # rewrite
relative to the config file.
## Git, CI and releases
-* Develop on a feature branch, small commits; PRs target `main`. Label PRs (`feature`, `bug`, `breaking`, `documentation`).
+* 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, E2E on real
PostgreSQL/MySQL/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
diff --git a/README.md b/README.md
index 5d583de..9cd6b8c 100644
--- a/README.md
+++ b/README.md
@@ -2,59 +2,14 @@
[](https://github.com/DaredevilOSS/sqlc-gen-ruby/actions/workflows/tests.yml)
# sqlc-gen-ruby
-A [sqlc](https://sqlc.dev) plugin that generates Ruby code (plus optional RBS type signatures) from SQL.
-Supported drivers: `pg`, `mysql2` and `sqlite3`.
-
-The plugin itself is written in Ruby: it is pure Ruby with no native dependencies (it has its own tiny protobuf
-codec), and it is shipped as a single self-contained WebAssembly module — the Ruby interpreter
-([ruby.wasm](https://github.com/ruby/ruby.wasm)) with the plugin embedded. See [How it works](#documentation).
-
-## Usage
-
-### Options Documentation
-| Option | Possible values | Optional | Info |
-|-----------------|------------------------------------------------|----------|-----------------------------------------------------------------------------------------------------------|
-| driver | values: `mysql2`,`pg`, `sqlite3` | No | Choosing the driver to use - see [here](docs/Examples.md) for more details info. |
-| rubyVersion | default: `3.3`
values: `3.1` - `3.4` | Yes | Determines the Ruby version the generated code should support.. |
-| generateGemfile | default: `false`
values: `false`,`true` | Yes | Assists you with the integration of SQLC and Ruby by generating a `Gemfile` with the needed dependencies. |
-| generateTypes | default: `true`
values: `false`,`true` | Yes | Determines whether to generate type signatures in addition to the code. |
-| overrides | a list, see [below](#override-option) | Yes | Overrides the generated (RBS) type of specific columns / parameters. |
-| moduleName | default: `Codegen` (e.g. `PgCodegen`) | Yes | The Ruby module the generated code lives in, e.g. `MyApp::Db`. |
-| preparedStatements | default: `true`
values: `false`,`true` | Yes | `pg` only: `false` sends the SQL with every call (`exec_params`) - needed behind pgbouncer in transaction mode. |
-| rename | an object, e.g. `{authors: Writer, bio: biography}` | Yes | Renames generated identifiers: a table name (or `schema.table`) gives the model class, a column name gives the row / model attribute. |
-| exactTableNames | default: `false`
values: `false`,`true` | Yes | `true` keeps table names as they are for model classes (`authors` => `Authors`) instead of singularizing them. |
-
-Unknown options are rejected (with a hint for near misses such as `generate_types`). Options can also be set once for
-all `sql:` blocks in the top-level `options:` of `sqlc.yaml`, under the plugin's name (`options: {ruby: {moduleName: MyApp::Db}}`).
-The `options:` of a codegen entry win over these, and the `overrides` of both apply. A driver used with another
-`engine:` (e.g. `pg` with `engine: mysql`) is an error.
-
-Table, column and enum comments (`COMMENT ON ...` in PostgreSQL, `COMMENT '...'` in MySQL) become doc comments of the
-generated models (column comments in the RBS).
-
-Unknown option names (e.g. a typo such as `moduleNmae`) and `preparedStatements` with a driver other than `pg` are errors.
-
-### Override option
-```yaml
-overrides:
- - column: ":" # one query, e.g. "GetAuthor:bio"
- ruby_type:
- type: "Symbol"
- notNull: true
- - column: "*:" # every query and model
- ruby_type:
- type: "Pathname"
- notNull: false
- - column: "." # a table column (".." too): its model and query columns
- ruby_type:
- type: "Symbol"
- - db_type: "uuid" # every column / parameter of a database type
- ruby_type:
- type: "String"
-```
-The first match wins, in this order: `:`, `.`, `*:`, `db_type`.
+A [sqlc](https://sqlc.dev) plugin that turns your SQL into Ruby methods, with RBS type signatures.
+Works with `pg` (PostgreSQL), `mysql2` (MySQL) and `sqlite3` (SQLite), and Ruby 3.1 – 3.4.
+
+The plugin is written in pure Ruby and ships as one self-contained WebAssembly file
+(the [ruby.wasm](https://github.com/ruby/ruby.wasm) interpreter with the plugin inside), so you don't need to install anything besides sqlc.
-### Configuration
+## Quick start
+**1. Point sqlc at the plugin** (`sqlc.yaml`; the URL and sha256 of each version are in its [release notes](https://github.com/DaredevilOSS/sqlc-gen-ruby/releases)):
```yaml
version: "2"
plugins:
@@ -63,162 +18,186 @@ plugins:
url: https://github.com/DaredevilOSS/sqlc-gen-ruby/releases/download/${RELEASE_TAG}/sqlc-gen-ruby.wasm
sha256: ${RELEASE_SHA}
sql:
- # PostgreSQL Example
- - schema: "examples/authors/postgresql/schema.sql"
- queries: "examples/authors/postgresql/query.sql"
- engine: "postgresql"
- codegen:
- - plugin: ruby
- out: examples/pg
- options:
- driver: pg
- rubyVersion: "3.3"
- generateTypes: true
- generateGemfile: false
- # MySQL Example
- - schema: "examples/authors/mysql/schema.sql"
- queries: "examples/authors/mysql/query.sql"
- engine: "mysql"
+ - schema: "db/schema.sql"
+ queries: "db/query.sql"
+ engine: "postgresql" # or "mysql" / "sqlite"
codegen:
- plugin: ruby
- out: examples/mysql2
+ out: app/db
options:
- driver: mysql2
- rubyVersion: "3.3"
- generateTypes: true
- generateGemfile: false
- # Sqlite3 Example
- - schema: "examples/authors/sqlite/schema.sql"
- queries: "examples/authors/sqlite/query.sql"
- engine: "sqlite"
- codegen:
- - plugin: ruby
- out: examples/sqlite3
- options:
- driver: sqlite3
- rubyVersion: "3.3"
- generateTypes: true
- generateGemfile: false
+ driver: pg # or mysql2 / sqlite3
```
-### [Generated Code Examples](docs/Examples.md)
+**2. Write queries** (`db/query.sql`):
+```sql
+-- name: GetAuthor :one
+SELECT * FROM authors WHERE id = $1;
-## Using the generated code
+-- name: CreateAuthor :one
+INSERT INTO authors (name, bio) VALUES ($1, $2) RETURNING *;
+```
+
+**3. Run `sqlc generate` and use the code:**
```ruby
-require_relative 'db/query_sql'
+require_relative 'app/db/query_sql'
-queries = PgCodegen::QuerySql.connect({ dbname: 'app', host: 'localhost' }, size: 5) # a ConnectionPool
+queries = PgCodegen::QuerySql.connect({ dbname: 'app', host: 'localhost' }, size: 5) # a connection pool
-author = queries.create_author(name: 'Seuss', bio: nil) # :one with RETURNING -> a frozen Data row
-queries.get_author(id: author.id) # => # or nil
-queries.list_authors_paged(limit: 10, offset: 0) # :many -> Array of rows
-queries.get_authors_by_ids(ids: [1, 2, 3]) # sqlc.slice / array parameter
-queries.copy_authors([PgCodegen::CopyAuthorsArgs.new(name: 'a', bio: nil)]) # :copyfrom
+author = queries.create_author(name: 'Seuss', bio: nil) # => #
+queries.get_author(id: author.id) # the row, or nil
+queries.get_author!(id: 42) # raises QuerySql::NotFound instead of returning nil
-queries.transaction(isolation: :serializable) do |tx| # COMMIT on return (also break / return / throw), ROLLBACK on error
+queries.transaction do |tx| # COMMIT when the block ends, ROLLBACK if it raises
+ tx.create_author(name: 'a', bio: nil)
+end
+```
+More: [the generated code for every driver](docs/Examples.md) (it is the plugin's real output, from
+[these schemas and queries](examples/authors)).
+
+## Options
+| Option | Default | What it does |
+|---|---|---|
+| `driver` | required | `pg`, `mysql2` or `sqlite3`. Must match the sqlc `engine:`. |
+| `rubyVersion` | `3.3` | Oldest Ruby the code must run on, `3.1` – `3.4`. Rows are `Data` classes from 3.2 and `Struct`s below. |
+| `generateTypes` | `true` | Also generate `.rbs` type signatures. |
+| `generateGemfile` | `false` | Also generate a `Gemfile` with the gems the code needs. |
+| `moduleName` | `Codegen` | The module the code lives in, e.g. `MyApp::Db`. |
+| `overrides` | none | Change the type of columns / parameters, see [below](#overrides). |
+| `rename` | none | Rename tables (model class) and columns (attribute), e.g. `{authors: Writer, bio: biography}`; `schema.table` works too. |
+| `exactTableNames` | `false` | Keep table names as model names (`authors` → `Authors`) instead of singularizing them (`Author`). |
+| `preparedStatements` | `true` | `pg` only (an error with other drivers). `false` sends the SQL with every call instead of preparing it (needed behind pgbouncer in transaction mode). |
+
+Unknown or misspelled options are errors, with a hint for near misses (`generate_types` → `generateTypes`).
+Options shared by several `sql:` blocks can go in the top-level `options:` of `sqlc.yaml` under the plugin's name
+(`options: {ruby: {moduleName: MyApp::Db}}`); a codegen entry's own options win, and the `overrides` of both apply.
+
+### Overrides
+```yaml
+overrides:
+ - column: "GetAuthor:bio" # one column of one query (:)
+ ruby_type: { type: "Symbol", notNull: true }
+ - column: "authors.bio" # a table column, in its model and in every query (.. too)
+ ruby_type: { type: "Symbol" }
+ - column: "*:bio" # a column name in every query and model
+ ruby_type: { type: "Pathname", notNull: false }
+ - db_type: "uuid" # a database type, everywhere (also the elements of uuid[] arrays)
+ ruby_type: { type: "String" }
+```
+The first match wins in this order: `:`, `.`, `*:`, `db_type`.
+Overrides only change the RBS types; the values are whatever the driver returns. Unknown keys are errors.
+
+## The generated code
+For each query file you get `query_sql.rb` with a `QuerySql` class (one method per query), plus `models.rb` (one class
+per table, one module of constants per enum), `connection.rb` (shared helpers) and the matching `.rbs` files.
+
+* **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: }`.
+* **Bang finders:** every `:one` query also gets a `get_author!` that raises `QuerySql::NotFound` instead of returning `nil`.
+* **Connections:** `QuerySql.connect(params, **pool_options)` builds a [`connection_pool`](https://github.com/mperham/connection_pool).
+ To use a pool you already have (or a fake in tests) pass it to `QuerySql.new(pool)`; anything with `#with` works.
+ For SQLite pass a `SQLite3::Database`: `QuerySql.new(db)`.
+* **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.
+* **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`.
+
+### Transactions
+```ruby
+queries.transaction(isolation: :serializable) do |tx|
id = tx.create_author_return_id(name: 'a', bio: nil)
- tx.transaction { |inner| inner.create_book(name: 'b', author_id: id) } # nested -> SAVEPOINT
+ tx.transaction { tx.create_book(name: 'b', author_id: id) } # nested: a SAVEPOINT
+ :done # the block's value is returned
end
```
-* **Keyword arguments:** every query parameter is a keyword argument (missing / unknown ones raise `ArgumentError`). Parameters named with `sqlc.arg` / `sqlc.narg` that are nullable default to `nil`; a plain nullable column (`INSERT ... VALUES ($1, $2)`) has to be passed explicitly. Names are snake_cased, Ruby keywords and the method's own locals (`client`, `sql`, ...) get a trailing underscore (`class_:`); a `sqlc.arg` used twice is one keyword, repeated `?` of the same column become `id:, id_2:, id_3:`.
-* **Bang finders:** every `:one` query also gets a `get_author!` that raises `QuerySql::NotFound` instead of returning `nil` (like `find_by` vs `find`).
-* **Logging:** `QuerySql.connect(params, logger: Rails.logger)` / `QuerySql.new(pool, logger: logger)` logs `QuerySql#get_author (0.4ms)` at debug level for every query and transaction (SQL parameters are never logged). Without a logger there is no overhead.
-* **Rows** are immutable `Data` objects (a `keyword_init` `Struct` for `rubyVersion` below 3.2; both are built with keywords: `Row.new(id: 1, name: 'a')`), so they work with pattern matching: `case row in { id:, name: }`. Column names that are not valid identifiers are snake_cased (`"Weird Name"` -> `weird_name`), names of the row's own methods get a trailing underscore (`hash` -> `hash_`).
-* **Connections:** `QuerySql.connect(params, **pool_options)` builds a [`connection_pool`](https://github.com/mperham/connection_pool); to share a pool you already have (or to inject a fake in tests) use `QuerySql.new(pool)` - anything that responds to `#with` and yields a connection. For SQLite pass a `SQLite3::Database` to `QuerySql.new(db)`.
-* **Async / fibers:** the generated code is plain synchronous Ruby and works with the Fiber scheduler (the [`async`](https://github.com/socketry/async) gem): inside `Sync { ... }` pooled queries on `pg` and `mysql2` overlap instead of blocking (see `tests/end2end_pg.rb`). SQLite calls are synchronous C calls and do not overlap.
-* **SQL** is kept in frozen heredoc constants (`GET_AUTHOR_SQL`); query comments in the `.sql` file become doc comments on the method; `.rbs` signatures are generated for everything.
-* The generated code is RuboCop-clean (`rake rubocop`) and 2-space indented.
+* 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.
+* `isolation:` (`pg`, `mysql2`) takes `:read_uncommitted`, `:read_committed`, `:repeatable_read`, `:serializable`;
+ `mode:` (`sqlite3`) takes `:deferred`, `:immediate`, `:exclusive`. Other values raise before anything runs.
+* PostgreSQL: if a statement fails and the block rescues the error, PostgreSQL still rolls the whole transaction
+ back, so `transaction` raises `PG::InFailedSqlTransaction` instead of pretending it committed.
+* SQLite: threads and fibers sharing one `SQLite3::Database` take turns; a transaction holds the database until it ends.
## Supported features
-✅ supported · 🚫 not offered by the database / sqlc
-
### Query annotations
-| Annotation | PostgreSQL | MySQL | SQLite | Notes |
-|---------------|:---:|:---:|:---:|-------|
-| `:one` | ✅ | ✅ | ✅ | returns the row or `nil` |
-| `:many` | ✅ | ✅ | ✅ | returns an `Array` of rows |
-| `:exec` | ✅ | ✅ | ✅ | |
-| `:execrows` | ✅ | ✅ | ✅ | returns the number of affected rows |
-| `:execlastid` | ✅ | ✅ | ✅ | PostgreSQL / SQLite need a `RETURNING ` clause (the value of that column is returned); MySQL returns `client.last_id` |
-| `:execresult` | ✅ | ✅ | ✅ | returns `ExecResult` (`rows_affected`, `last_insert_id`; `last_insert_id` is `nil` on PostgreSQL, use `RETURNING`) |
-| `:batchexec` / `:batchone` / `:batchmany` | ✅ | ✅ | ✅ | take an `Array` of `Args` and run the prepared statement once per element on one connection; return `nil` / `Array[Row?]` / `Array[Array[Row]]` |
-| `:copyfrom` | ✅ | ✅ | ✅ | takes an `Array` of `Args` (a generated `Data` class). PostgreSQL: `COPY ... FROM STDIN`. MySQL: `LOAD DATA LOCAL INFILE` from a temporary CSV - needs `local_infile: true` in the `Mysql2::Client` params and `local_infile=1` on the server. SQLite: batched multi-row `INSERT` |
+| Annotation | Returns | Notes |
+|---|---|---|
+| `:one` | the row or `nil` | plus a bang finder (`get_author!`) |
+| `:many` | `Array` of rows | |
+| `:exec` | `nil` | |
+| `:execrows` | number of affected rows | |
+| `:execlastid` | the new id | PostgreSQL / SQLite: needs `RETURNING id`; MySQL: the auto-increment id |
+| `:execresult` | `ExecResult` (`rows_affected`, `last_insert_id`) | `last_insert_id` is `nil` on PostgreSQL (use `RETURNING`) |
+| `:batchexec` / `:batchone` / `:batchmany` | `nil` / `Array` of rows or `nil` / `Array` of `Array`s of rows | take an `Array` of `Args` and run the query once per element, on one connection |
+| `:copyfrom` | `nil` | bulk insert of an `Array` of `Args`; all rows or none (see below) |
+
+`:copyfrom` uses `COPY ... FROM STDIN` on PostgreSQL and batched multi-row `INSERT`s on SQLite. On MySQL it uses
+`LOAD DATA LOCAL INFILE`, which needs `local_infile: true` in the `Mysql2::Client` params and `local_infile=1` on the
+server; duplicate keys and invalid values raise `Mysql2::Error` instead of being skipped.
### Macros
-| Macro | PostgreSQL | MySQL | SQLite | Notes |
-|---------------|:---:|:---:|:---:|-------|
-| `sqlc.arg` | ✅ | ✅ | ✅ | names the attribute of the `Args` class (anonymous parameters such as `ANY($1::BIGINT[])` are called `param1`, `param2`, ...) |
-| `sqlc.narg` | ✅ | ✅ | ✅ | nullable parameter, typed `T?` |
-| `sqlc.slice` | 🚫 | ✅ | ✅ | the `IN (...)` list is expanded at runtime (an empty list matches nothing). PostgreSQL: pass an array to `= ANY($1::T[])` instead |
-| `sqlc.embed` | ✅ | ✅ | ✅ | the row gets an attribute holding the table's model (`author`, `author2` for the same table twice). In a query with a `LEFT` / `RIGHT` / `FULL JOIN` the embedded models are optional (`Book?`) and `nil` when the join found no row |
-
-### Other
-* **Models:** a `models.rb` / `models.rbs` with one `Data` (`Struct` below Ruby 3.2) class per table, and one module with constants per enum (`ExtendedBioType::MEMOIR`, `ExtendedBioType::ALL`). Enum columns are `String`s.
-* **Transactions:** `queries.transaction { |tx| tx.create_author(...) }` runs everything in the block on one connection in a transaction (commit when the block returns, also through `break` / `return` / `throw`; rollback when it raises or the thread is killed) and returns the block's value. Nested calls become savepoints; `isolation:` (`pg`, `mysql2`) / `mode:` (`sqlite3`) choose the isolation level / locking mode.
-* **Nullability:** nullable columns / parameters are typed `T?` in the `.rbs`.
-* **Arrays (PostgreSQL):** `Array[T]` for columns and parameters (`Array[Array[T]]` for `T[][]`); arrays of types without a decoder (`uuid[]`, enum arrays, ...) are `Array[String]`.
-* **Names:** a parameter repeated in a query gets a suffix (`id:`, `id_2:`); result columns named like a keyword or a method every row has (`class`, `hash`, `end`, ...) get a trailing `_` (`class_`); 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 rejected.
-* **Other PostgreSQL types:** geometric, range / multirange, full-text, `hstore`, `ltree`, `timetz`, `reg*` and composite types are `String`s (the text the pg gem returns); domains and types the plugin does not know are `untyped` instead of failing the generation.
-* **SQLite declared types** (`VARCHAR(255)`, `DECIMAL(10,5)`, `UNSIGNED BIG INT`, ...) follow SQLite's [type affinity](https://www.sqlite.org/datatype3.html#determination_of_column_affinity).
-* Types follow the Ruby classes the drivers actually return (e.g. `BigDecimal` for `numeric`/`decimal`, `Date`, `Time`, `IPAddr`; `json`/`jsonb` are parsed, so `untyped`).
-
-Not applicable to the Ruby plugin (they exist in the C# plugin because of its runtime): `withAsyncSuffix` / `withCancellationToken` (Ruby has no async/await: use the Fiber scheduler, see above), `useDapper`, target framework / csproj / namespace options (`moduleName` is the equivalent of the namespace).
+| Macro | PostgreSQL | MySQL | SQLite | Notes |
+|---|:---:|:---:|:---:|---|
+| `sqlc.arg` | ✅ | ✅ | ✅ | names a parameter (unnamed ones such as `ANY($1::BIGINT[])` become `param1`, ...) |
+| `sqlc.narg` | ✅ | ✅ | ✅ | a nullable parameter (`T?`, defaults to `nil`) |
+| `sqlc.slice` | 🚫 | ✅ | ✅ | `IN (sqlc.slice(ids))` takes an `Array` (an empty one matches nothing). PostgreSQL: use `= ANY($1::bigint[])` with an array |
+| `sqlc.embed` | ✅ | ✅ | ✅ | the row gets an attribute holding the table's model (`author`, `author2` for the same table twice); `nil` when an outer join found no row |
+
+🚫 = not offered by sqlc for that database.
+
+### Types
+* Types follow what the drivers return: `Integer`, `Float`, `BigDecimal` (`numeric` / `decimal`), `String`, `Date`, `Time`,
+ `IPAddr`, `true` / `false`; `json` / `jsonb` are parsed (`untyped`); nullable columns and parameters are `T?`.
+* PostgreSQL arrays are `Array[T]` (`Array[Array[T]]` for `T[][]`); arrays of types the pg gem does not decode
+ (`uuid[]`, enum arrays, ...) are `Array[String]`. Geometric, range, full-text, `hstore`, `ltree`, `timetz`, `reg*` and
+ composite types are `String`s; domains and unknown types are `untyped`.
+* Enums are `String`s, with constants in `models.rb` (`ExtendedBioType::MEMOIR`, `ExtendedBioType::ALL`).
+* SQLite follows its [type affinity rules](https://www.sqlite.org/datatype3.html#determination_of_column_affinity)
+ (`VARCHAR(255)` → `String`, `DECIMAL(10,5)` → `Float`, `UNSIGNED BIG INT` → `Integer`); `JSON` and `UUID` columns are `String`s.
+ The generated SQLite code uses `Monitor`: if you run Steep, add `library "monitor"` to your `Steepfile`.
+
+### Names
+* Names are snake_cased. Ruby keywords and names a row or method already uses get a trailing `_` (`class_`, `hash_`).
+* 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.
+
+### Limitations
+* 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`.
+* `rubyVersion` takes `3.4` but not a patch version of it (`3.4.1`).
+* SQLite's `NUMERIC` affinity can return an `Integer` where `Float` is declared.
+* 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).
# Contributing
-## Local plugin development
-### Prerequisites
-* Ruby 3.3 and `bundle install`
-* [sqlc](https://docs.sqlc.dev/en/latest/overview/install.html) (version in [`scripts/tool-versions.env`](scripts/tool-versions.env))
-* `curl` + `tar` for building the WASM plugin (it downloads the prebuilt ruby.wasm interpreter)
-* PostgreSQL / MySQL servers for the end-to-end tests (or Docker, see below)
-
-### Layout
-| Path | What |
-|---|---|
-| `exe/plugin.rb` | plugin entry point: `GenerateRequest` on stdin → `GenerateResponse` on stdout |
-| `lib/sqlc_gen_ruby/` | the generator (`generator.rb`, `method_gen.rb`, `drivers/*.rb`, `options.rb`, ...) |
-| `test/` | unit + golden tests (`rake test`); `test/fixtures` are real requests captured from sqlc |
-| `tests/` | end-to-end tests of the generated code against real databases |
-| `examples/` | the plugin's output for `examples/authors/*` (kept in sync, CI runs `sqlc diff`) |
-| `scripts/wasm/` | building the WASM plugin |
-
-### Everyday commands
+You need Ruby 3.3 (`bundle install`), [sqlc](https://docs.sqlc.dev/en/latest/overview/install.html) (version in
+[`scripts/tool-versions.env`](scripts/tool-versions.env)), `curl` + `tar` to build the WASM, and PostgreSQL / MySQL
+servers (or Docker) for the end-to-end tests.
+
```
bundle exec rake test # unit + golden tests (no database needed)
-rake sqlc_generate_process # regenerate examples/ with exe/plugin.rb (needs `ruby` on PATH)
-rake sqlc_generate_wasm # build dist/plugin.wasm, then regenerate examples/ with it
bundle exec rake rubocop steep # the generated code must stay RuboCop- and Steep-clean
+rake sqlc_generate_process # regenerate examples/ with exe/plugin.rb
+rake sqlc_generate_wasm # build dist/plugin.wasm, then regenerate examples/ with it
rake end2end_sqlite3 # end-to-end, no server needed
-rake end2end_pg end2end_mysql2 # end-to-end, needs the servers below
+rake end2end_pg end2end_mysql2 # end-to-end, needs servers (see docs/Testing.md)
rake run_end2end_tests # all of it in Docker (docker-compose)
```
-The end-to-end tests read `DB_NAME`, `DB_USER`, `DB_PASS`, `POSTGRES_HOST`, `MYSQL_HOST` (and optionally
-`POSTGRES_PORT`, `MYSQL_PORT`); load `examples/authors//schema.sql` into the database first. MySQL needs
-`local_infile=1` for `:copyfrom`.
-
-The golden tests feed the plugin real `GenerateRequest`s captured from sqlc and compare with [examples/](examples) and
-`test/golden` (`UPDATE_GOLDEN=1 ruby -Itest test/extended_golden_test.rb` rewrites the snapshots - review the diff).
-To refresh a fixture see `scripts/capture_request.rb`.
-
-### CI
-* **Build** (`build.yml`): builds `plugin.wasm`; lint (Ruby warnings, RuboCop on the generated code); codegen tests -
- `sqlc diff` for the WASM *and* the process plugin, Steep, and generation with different options (Ruby 3.1 / 3.2 / 3.3,
- types, Gemfile) checked with the matching Ruby.
-* **Tests** (`tests.yml`): unit tests, the plugin on Ruby 3.1 / 3.2 / 3.4, and the end-to-end tests against PostgreSQL
- and MySQL service containers and SQLite.
-* Tool versions live in `scripts/tool-versions.env`; dependabot keeps actions and gems current.
-
-## Release flow
-Releases are started by hand and fully automated after that: **Actions → Release → Run workflow** (patch / minor / major,
-optional dry run). The workflow runs the whole test gate on the commit, then tags, publishes the GitHub release with the
-tested `sqlc-gen-ruby.wasm` and its sha256, and verifies the published file with sqlc. Details: [docs/Releasing.md](docs/Releasing.md).
-
-## Documentation
+`examples/` is the plugin's committed output and CI fails when it drifts, so regenerate it with every change to the
+generated code. Pull requests need both CI workflows (`build.yml`, `tests.yml`) green.
+
+Releases are started by a maintainer (**Actions → Release → Run workflow**) and are automated from there: the full
+test gate, the tag, the GitHub release with the tested `sqlc-gen-ruby.wasm` and its sha256, and a check of the published file.
+
| Doc | What |
|---|---|
| [docs/Architecture.md](docs/Architecture.md) | how the generator works, where to change what, adding a feature / driver / option |
| [docs/Testing.md](docs/Testing.md) | the test layers, CI workflows, what to run before pushing |
| [docs/Releasing.md](docs/Releasing.md) | the release process and versioning rules |
-| [docs/Design.md](docs/Design.md) | the WASM packaging, design decisions, known limitations |
+| [docs/Design.md](docs/Design.md) | the WASM packaging and design decisions |
| [docs/Examples.md](docs/Examples.md) | per-driver configs and links to the generated examples |
| [CLAUDE.md](CLAUDE.md) | the same orientation condensed for AI coding assistants |
diff --git a/Steepfile b/Steepfile
index cf78099..58fbc55 100644
--- a/Steepfile
+++ b/Steepfile
@@ -2,6 +2,6 @@ target :gen do
check "examples"
signature "examples"
- # stdlib signatures for the types the generated code refers to (BigDecimal, Date, Time, IPAddr, Tempfile; Set is core since RBS 4)
- library "bigdecimal", "date", "time", "ipaddr", "tempfile"
+ # stdlib signatures for the types the generated code refers to (BigDecimal, Date, Time, IPAddr, Tempfile, Monitor; Set is core since RBS 4)
+ library "bigdecimal", "date", "time", "ipaddr", "tempfile", "monitor"
end
diff --git a/docs/Architecture.md b/docs/Architecture.md
index 851d678..99225f5 100644
--- a/docs/Architecture.md
+++ b/docs/Architecture.md
@@ -21,14 +21,14 @@ stdout ◄─ Proto (encode) ◄─ GenerateResponse (query_sql.rb, query_sql.rb
| `exe/plugin.rb` | entry point. Errors go to stderr with exit code 1 (sqlc shows them); `SQLC_GEN_RUBY_DEBUG=1` adds a backtrace |
| `proto.rb`, `plugin_pb.rb` | a ~150 line protobuf wire codec and the subset of sqlc's `codegen.proto` we read. Field numbers must match upstream. `google-protobuf` is a C extension and cannot run inside ruby.wasm |
| `options.rb`, `ruby_version.rb` | parse + validate the `options:` JSON (`driver`, `rubyVersion`, `overrides`, `rename`, ...; unknown keys are errors) |
-| `generator.rb` | the orchestrator: one file per query file (`query_sql.rb`), plus `models.rb`, `connection.rb`, `Gemfile`; builds the module / class / `Data` skeleton |
-| `method_gen.rb` | the code of one query: signature (keyword arguments), SQL constant, command (`:one`, `:many`, `:exec`, `:execrows`, `:execlastid`, `:copyfrom`), `sqlc.slice`, bang finder, and the RBS signature |
+| `generator.rb` | the orchestrator: one file per query file (`query_sql.rb`), plus `models.rb`, `connection.rb` (per-connection state and helpers the query classes share), `Gemfile`; builds the module / class / `Data` skeleton |
+| `method_gen.rb` | the code of one query: signature (keyword arguments), SQL constant, command (`:one`, `:many`, `:exec`, `:execrows`, `:execlastid`, `:execresult`, `:batchexec` / `:batchone` / `:batchmany`, `:copyfrom`), `sqlc.slice`, bang finder, and the RBS signature |
| `row_layout.rb` | turns result columns into row attributes; `sqlc.embed` collapses a table's columns into one model attribute |
-| `type_resolver.rb` | RBS type of a column / parameter: driver type table, arrays, nullability (`T?`), `overrides:` |
+| `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` |
-| `code.rb` | text helpers: indentation, `snake_case`, `camelize`, Ruby-keyword-safe parameter names |
+| `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
supplies *snippets* (how to prepare a statement, how to read the affected rows, ...). If you find a `case driver` in
@@ -67,6 +67,10 @@ Design decisions worth knowing (the *why* is in the comments of `drivers/base.rb
* **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`).
+* **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
+ `LOAD DATA` warnings (skipped duplicates, coerced values) are raised as errors.
* **Nothing from the plugin leaks into the app.** The generated code only requires the driver gem and `connection_pool`.
## Adding things
diff --git a/docs/Design.md b/docs/Design.md
index f2fe36b..2a8783f 100644
--- a/docs/Design.md
+++ b/docs/Design.md
@@ -4,30 +4,22 @@ sqlc plugins receive a protobuf `GenerateRequest` on stdin and answer with a `Ge
process or as a WASI module. This plugin is pure Ruby and is distributed as a WASI module (`plugin.wasm`).
## Source layout
-
-| File | Role |
-|---|---|
-| `exe/plugin.rb` | entry point (stdin → `Generator` → stdout) |
-| `lib/sqlc_gen_ruby/proto.rb` | ~150 line protobuf wire codec. `google-protobuf` is a C extension and cannot be loaded inside ruby.wasm, so the plugin brings its own |
-| `lib/sqlc_gen_ruby/plugin_pb.rb` | the subset of sqlc's `codegen.proto` the generator needs (field numbers must match upstream) |
-| `lib/sqlc_gen_ruby/options.rb`, `ruby_version.rb` | the `options:` JSON, `rubyVersion` parsing / validation (3.1 – 3.4; `Struct` below 3.2, `Data` from 3.2) |
-| `lib/sqlc_gen_ruby/generator.rb` | module / class / dataclass (`Data.define`, `Struct.new`) layout, `.rb` / `.rbs` / `Gemfile` files, `models.rb` (tables + enums) |
-| `lib/sqlc_gen_ruby/method_gen.rb` | one generated method per query: `:one`, `:many`, `:exec`, `:execrows`, `:execlastid`, `:copyfrom`, `sqlc.slice` |
-| `lib/sqlc_gen_ruby/row_layout.rb` | how result columns become `Row` attributes (`sqlc.embed` expands a table into one model attribute) |
-| `lib/sqlc_gen_ruby/type_resolver.rb`, `catalog.rb` | RBS type of a column (DB type mapping + array + nullability + `overrides:`), tables / enums of the catalog |
-| `lib/sqlc_gen_ruby/drivers/*.rb` | `Base` + `Pg`, `Mysql2`, `Sqlite3`: column type mapping, statement preparation / execution, pooling, row access |
-| `lib/sqlc_gen_ruby/code.rb` | indent / snake_case helpers |
-
-Driver differences are data, not copy/paste: `Base` has `pooling_enabled?` (ConnectionPool vs a single injected
-connection), `row_data_type` (`row['col']` vs `row[0]`), `supports_exec_last_id?` (mysql2 only) and a column mapping
-table (exact or prefix based matching).
+What each file in `lib/sqlc_gen_ruby/` does is in [Architecture.md](Architecture.md). Two constraints shape it:
+* `google-protobuf` is a C extension and cannot be loaded inside ruby.wasm, so the plugin brings its own ~150 line
+ protobuf codec (`proto.rb`) and the subset of sqlc's `codegen.proto` it reads (`plugin_pb.rb`, field numbers must
+ match upstream).
+* Driver differences are data, not copy/paste: `Drivers::Base` defines hooks (how to prepare and run a statement, how
+ rows are read - `row_data_type` `:hash` for `row['col']` vs `:array` for `row[0]` -, how `:execlastid` /
+ `:execresult` / `:copyfrom` / transactions work) and a column mapping table; `Pg`, `Mysql2` and `Sqlite3` fill them in.
## The WASM build (`scripts/wasm/build.sh`)
1. downloads the *prebuilt* `ruby-3.3-wasm32-unknown-wasip1-full` release from ruby/ruby.wasm (this avoids compiling
CRuby with a wasi-sdk);
2. strips the ~18 MB of DWARF debug info / name section from the interpreter (`strip_wasm.rb`);
-3. stages a slim stdlib (the plugin only uses core + `json`) + `lib/` + `exe/`;
+3. stages a slim stdlib (the plugin only uses core + `json`) + `lib/` + `exe/`, plus an empty
+ `syntax_suggest/core_ext.rb` (the interpreter loads it to format errors and would otherwise print a warning before
+ the plugin's error message);
4. embeds that filesystem into the interpreter with `rbwasm pack` (wasi-vfs), because sqlc gives plugins no filesystem.
**The argv trick:** sqlc runs the module as `plugin.wasm /plugin.CodegenService/Generate`, so the interpreter treats that
@@ -52,7 +44,7 @@ transactions, nullability, `overrides`) and Ruby conventions (bang finders, logg
now idiomatic Ruby rather than a copy of the C# one.
## Known limitations
-* The SQLite `NUMERIC` affinity can return Integers where `Float` is declared.
-* `:copyfrom` for MySQL writes binary (`BLOB`) values through a text CSV; only text-safe values round-trip.
-* The generated query classes are not thread-safe for SQLite (a single connection is injected).
-* `3.3.x` (a patch version) is rejected as `rubyVersion`; use `3.3`.
+See [Limitations in the README](../README.md#limitations). Design choices that look like limitations:
+* Overrides only change the RBS types; values are never converted, so the generated code stays a thin layer over the driver.
+* SQLite queries do not overlap: a `SQLite3::Database` is one connection, so the generated code takes one lock per
+ database (held for a whole transaction) to keep threads and fibers from mixing their transactions.
diff --git a/docs/Releasing.md b/docs/Releasing.md
index c625f7b..b2259c6 100644
--- a/docs/Releasing.md
+++ b/docs/Releasing.md
@@ -39,7 +39,8 @@ code and the options**, because that is what users depend on:
| minor | new options, new query features, new drivers; output that only adds API (e.g. a new method) |
| major | existing generated method signatures, constants or options change incompatibly |
-Label pull requests (`breaking`, `feature`, `bug`, `documentation`; `skip-changelog` to hide one) so the generated notes
+Label pull requests `bug`, `enhancement` or `documentation` (`breaking` for incompatible changes, `skip-changelog` to
+hide one; create those two labels the first time they are needed) so the generated notes
are grouped sensibly, and pick the bump from the labels of what is merged since the last release.
## Using a release
diff --git a/docs/Testing.md b/docs/Testing.md
index a9551bb..b245bda 100644
--- a/docs/Testing.md
+++ b/docs/Testing.md
@@ -28,10 +28,13 @@ They `require` the generated code from `examples/` and run it against a real dat
`rbs_types.rb` additionally checks that returned values match the generated `.rbs` types.
* SQLite needs nothing: `rake end2end_sqlite3`.
* PostgreSQL / MySQL: provide a server, load `examples/authors//schema.sql`, and set `DB_NAME DB_USER DB_PASS
- POSTGRES_HOST MYSQL_HOST` (optionally `*_PORT`). MySQL needs `local_infile=1` for `:copyfrom`.
+ POSTGRES_HOST MYSQL_HOST` (optionally `*_PORT`). MySQL needs `local_infile=1` for `:copyfrom`, and the MySQL tests
+ connect without a password (`DB_PASS` is only used for PostgreSQL), so use a user without one.
Or run everything in Docker: `rake run_end2end_tests` (docker-compose).
-* They cover every query command, transactions (nested, isolation), pool injection, logging, bang finders, `copyfrom`
- with awkward values, Fiber-scheduler concurrency (`async` gem) and statement-leak checks.
+* They cover every query command (batches with encoded `bytea` / `json` values too), transactions (nested,
+ isolation, `break` / `throw`, a rescued failed statement on PostgreSQL, SQLite threads sharing a database), pool
+ injection, logging, bang finders, `:copyfrom` with awkward and binary values and its all-or-nothing behaviour,
+ Fiber-scheduler concurrency (`async` gem) and statement-leak checks.
## CI workflows (`.github/workflows/`)
| Workflow | When | What |
diff --git a/examples/authors/mysql/query.sql b/examples/authors/mysql/query.sql
index 13e6835..6ee7430 100644
--- a/examples/authors/mysql/query.sql
+++ b/examples/authors/mysql/query.sql
@@ -110,5 +110,11 @@ SELECT id, `order`, note FROM regressions WHERE id IN (?, ?, ?) ORDER BY id;
-- name: CopyRegressions :copyfrom
INSERT INTO regressions (`order`, note) VALUES (?, ?);
+-- name: CopyRegressionRows :copyfrom
+INSERT INTO regressions (id, `order`, note, payload) VALUES (?, ?, ?, ?);
+
+-- name: ListRegressionPayloads :many
+SELECT id, payload FROM regressions ORDER BY id;
+
-- name: TruncateRegressions :exec
TRUNCATE TABLE regressions;
diff --git a/examples/authors/mysql/schema.sql b/examples/authors/mysql/schema.sql
index ebde0cd..99a4904 100644
--- a/examples/authors/mysql/schema.sql
+++ b/examples/authors/mysql/schema.sql
@@ -67,5 +67,6 @@ CREATE TABLE extended_bios (
CREATE TABLE regressions (
id bigint NOT NULL AUTO_INCREMENT PRIMARY KEY,
`order` varchar(255) NOT NULL,
- note text
+ note text,
+ payload varbinary(16)
);
diff --git a/examples/authors/postgresql/query.sql b/examples/authors/postgresql/query.sql
index 1ee4431..e348817 100644
--- a/examples/authors/postgresql/query.sql
+++ b/examples/authors/postgresql/query.sql
@@ -155,6 +155,9 @@ INSERT INTO regressions (id, "order") VALUES ($1, $2) ON CONFLICT (id) DO NOTHIN
-- name: CopyRegressions :copyfrom
INSERT INTO regressions (id, "order", payload, doc) VALUES ($1, $2, $3, $4);
+-- name: CreateRegressionsBatch :batchexec
+INSERT INTO regressions (id, "order", payload, doc) VALUES ($1, $2, $3, $4);
+
-- name: ListRegressions :many
SELECT * FROM regressions WHERE (sqlc.narg(order_name)::text IS NULL OR "order" = sqlc.narg(order_name)) ORDER BY id;
diff --git a/examples/authors/sqlite/query.sql b/examples/authors/sqlite/query.sql
index a439cba..9455e03 100644
--- a/examples/authors/sqlite/query.sql
+++ b/examples/authors/sqlite/query.sql
@@ -87,7 +87,7 @@ SELECT * FROM authors WHERE id = ?;
SELECT * FROM authors WHERE name = ? ORDER BY id;
-- name: InsertDeclaredTypes :exec
-INSERT INTO declared_types (c_varchar, c_decimal, c_unsigned_big_int, c_native, c_double) VALUES (?, ?, ?, ?, ?);
+INSERT INTO declared_types (c_varchar, c_decimal, c_unsigned_big_int, c_native, c_double, c_json, c_uuid) VALUES (?, ?, ?, ?, ?, ?, ?);
-- name: GetDeclaredTypes :one
SELECT * FROM declared_types LIMIT 1;
diff --git a/examples/authors/sqlite/schema.sql b/examples/authors/sqlite/schema.sql
index 4b72f2e..59e653c 100644
--- a/examples/authors/sqlite/schema.sql
+++ b/examples/authors/sqlite/schema.sql
@@ -18,7 +18,9 @@ CREATE TABLE declared_types (
c_decimal DECIMAL(10, 5),
c_unsigned_big_int UNSIGNED BIG INT,
c_native NATIVE CHARACTER(70),
- c_double DOUBLE PRECISION
+ c_double DOUBLE PRECISION,
+ c_json JSON,
+ c_uuid UUID
);
CREATE TABLE regressions (
diff --git a/examples/mysql2/models.rb b/examples/mysql2/models.rb
index 2bce311..95ad226 100644
--- a/examples/mysql2/models.rb
+++ b/examples/mysql2/models.rb
@@ -67,6 +67,6 @@ class Book < Data.define(:id, :name, :author_id, :description)
class ExtendedBio < Data.define(:author_name, :name, :bio_type, :author_type)
end
- class Regression < Data.define(:id, :order, :note)
+ class Regression < Data.define(:id, :order, :note, :payload)
end
end
diff --git a/examples/mysql2/models.rbs b/examples/mysql2/models.rbs
index e19b214..b97f70b 100644
--- a/examples/mysql2/models.rbs
+++ b/examples/mysql2/models.rbs
@@ -120,6 +120,7 @@ module Mysql2Codegen
attr_reader id: Integer
attr_reader order: String
attr_reader note: String?
- def initialize: ( Integer id, String order, String? note) -> void
+ attr_reader payload: String?
+ def initialize: ( Integer id, String order, String? note, String? payload) -> void
end
end
diff --git a/examples/mysql2/query_sql.rb b/examples/mysql2/query_sql.rb
index f940cf5..9ce3b60 100644
--- a/examples/mysql2/query_sql.rb
+++ b/examples/mysql2/query_sql.rb
@@ -235,6 +235,16 @@ class ListRegressionsByIdsRow < Data.define(:id, :order, :note)
class CopyRegressionsArgs < Data.define(:order, :note)
end
+ class CopyRegressionRowsArgs < Data.define(:id, :order, :note, :payload)
+ end
+
+ LIST_REGRESSION_PAYLOADS_SQL = <<~'SQL'
+ SELECT id, payload FROM regressions ORDER BY id
+ SQL
+
+ class ListRegressionPayloadsRow < Data.define(:id, :payload)
+ end
+
TRUNCATE_REGRESSIONS_SQL = <<~'SQL'
TRUNCATE TABLE regressions
SQL
@@ -523,7 +533,7 @@ def copy_authors(rows)
file.write(csv_line([row.name, row.bio]))
end
file.flush
- client.query("LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `authors` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`name`, `bio`)")
+ load_data(client, "LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `authors` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`name`, `bio`)")
end
nil
end
@@ -748,12 +758,41 @@ def copy_regressions(rows)
file.write(csv_line([row.order, row.note]))
end
file.flush
- client.query("LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `regressions` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`order`, `note`)")
+ load_data(client, "LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `regressions` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`order`, `note`)")
+ end
+ nil
+ end
+ end
+
+ def copy_regression_rows(rows)
+ with_client('copy_regression_rows') do |client|
+ return if rows.empty?
+
+ Tempfile.create(['copy_regression_rows', '.csv']) do |file|
+ rows.each do |row|
+ file.write(csv_line([row.id, row.order, row.note, row.payload]))
+ end
+ file.flush
+ load_data(client, "LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `regressions` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`id`, `order`, `note`, `payload`)")
end
nil
end
end
+ 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
+ end
+ end
+ end
+
def truncate_regressions
with_client('truncate_regressions') do |client|
stmt = client.prepare(TRUNCATE_REGRESSIONS_SQL)
@@ -803,6 +842,28 @@ def csv_line(values)
"#{fields.join(',')}\n"
end
+ # Runs LOAD DATA in a transaction (a savepoint inside #transaction): all rows or none, and an error instead
+ # of the warnings LOAD DATA LOCAL gives for duplicate keys and invalid values.
+ def load_data(client, sql)
+ outermost = !client.in_transaction
+ client.query(outermost ? 'BEGIN' : 'SAVEPOINT sqlc_copy')
+ done = false
+ begin
+ client.query(sql)
+ unless client.warning_count.zero?
+ warnings = client.query('SHOW WARNINGS', as: :array).reject { |level, *| level == 'Note' }
+ raise Mysql2::Error, "LOAD DATA: #{warnings.map { |w| w[2] }.join('; ')}" unless warnings.empty?
+ end
+ done = true
+ ensure
+ if outermost
+ client.query(done ? 'COMMIT' : 'ROLLBACK')
+ else
+ client.query(done ? 'RELEASE SAVEPOINT sqlc_copy' : 'ROLLBACK TO SAVEPOINT sqlc_copy')
+ end
+ end
+ end
+
def slice_placeholders(values)
values.empty? ? 'NULL' : Array.new(values.size, '?').join(', ')
end
diff --git a/examples/mysql2/query_sql.rbs b/examples/mysql2/query_sql.rbs
index 1ccc945..fc0d044 100644
--- a/examples/mysql2/query_sql.rbs
+++ b/examples/mysql2/query_sql.rbs
@@ -255,6 +255,22 @@ module Mysql2Codegen
def initialize: ( String order, String? note) -> void
end
+ class CopyRegressionRowsArgs
+ 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_REGRESSION_PAYLOADS_SQL: String
+
+ class ListRegressionPayloadsRow
+ attr_reader id: Integer
+ attr_reader payload: String?
+ def initialize: ( Integer id, String? payload) -> void
+ end
+
TRUNCATE_REGRESSIONS_SQL: String
class QuerySql
@@ -337,6 +353,10 @@ module Mysql2Codegen
def copy_regressions: (Array[CopyRegressionsArgs] rows) -> void
+ def copy_regression_rows: (Array[CopyRegressionRowsArgs] rows) -> void
+
+ def list_regression_payloads: -> Array[ListRegressionPayloadsRow]
+
def truncate_regressions: -> void
private
@@ -347,6 +367,8 @@ module Mysql2Codegen
def csv_line: (Array[untyped] values) -> String
+ def load_data: (untyped client, String sql) -> void
+
def slice_placeholders: (Array[untyped] values) -> String
end
end
diff --git a/examples/pg/query_sql.rb b/examples/pg/query_sql.rb
index de218d9..3bdf842 100644
--- a/examples/pg/query_sql.rb
+++ b/examples/pg/query_sql.rb
@@ -318,6 +318,13 @@ class CreateRegressionIfNewRow < Data.define(:id)
class CopyRegressionsArgs < Data.define(:id, :order, :payload, :doc)
end
+ CREATE_REGRESSIONS_BATCH_SQL = <<~'SQL'
+ INSERT INTO regressions (id, "order", payload, doc) VALUES ($1, $2, $3, $4)
+ SQL
+
+ class CreateRegressionsBatchArgs < Data.define(:id, :order, :payload, :doc)
+ end
+
LIST_REGRESSIONS_SQL = <<~'SQL'
SELECT id, "order", payload, doc, bio_types, uids FROM regressions WHERE ($1::text IS NULL OR "order" = $1) ORDER BY id
SQL
@@ -376,7 +383,10 @@ def transaction(isolation: nil)
ensure
unless failed
commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ result = client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ # COMMIT of a transaction aborted by an error the block rescued is a ROLLBACK
+ rolled_back = outermost && result.cmd_status == 'ROLLBACK' && Thread.current.status != 'aborting'
+ raise PG::InFailedSqlTransaction, 'transaction rolled back: a statement in it failed' if rolled_back
end
end
end
@@ -984,6 +994,20 @@ def copy_regressions(rows)
end
end
+ def create_regressions_batch(rows)
+ with_client('create_regressions_batch') do |client|
+ unless client.prepared_statements.include?('query_sql_create_regressions_batch')
+ client.prepare('query_sql_create_regressions_batch', CREATE_REGRESSIONS_BATCH_SQL)
+ client.prepared_statements.add('query_sql_create_regressions_batch')
+ end
+ rows.each do |args|
+ query_params = [args.id, args.order, encode_bytea(args.payload), encode_json(args.doc)]
+ client.exec_prepared('query_sql_create_regressions_batch', query_params)
+ end
+ nil
+ end
+ end
+
def list_regressions(order_name: nil)
with_client('list_regressions') do |client|
query_params = [order_name]
diff --git a/examples/pg/query_sql.rbs b/examples/pg/query_sql.rbs
index 7d2cea6..bb90ab1 100644
--- a/examples/pg/query_sql.rbs
+++ b/examples/pg/query_sql.rbs
@@ -325,6 +325,16 @@ module PgCodegen
def initialize: ( Integer id, String order, String? payload, untyped doc) -> void
end
+ CREATE_REGRESSIONS_BATCH_SQL: String
+
+ class CreateRegressionsBatchArgs
+ attr_reader id: Integer
+ attr_reader order: String
+ attr_reader payload: String?
+ attr_reader doc: untyped
+ def initialize: ( Integer id, String order, String? payload, untyped doc) -> void
+ end
+
LIST_REGRESSIONS_SQL: String
class ListRegressionsRow
@@ -454,6 +464,8 @@ module PgCodegen
def copy_regressions: (Array[CopyRegressionsArgs] rows) -> void
+ def create_regressions_batch: (Array[CreateRegressionsBatchArgs] rows) -> void
+
def list_regressions: (?order_name: String?) -> Array[ListRegressionsRow]
def truncate_regressions: -> void
diff --git a/examples/sqlite3/connection.rb b/examples/sqlite3/connection.rb
new file mode 100644
index 0000000..f29c5b1
--- /dev/null
+++ b/examples/sqlite3/connection.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+# auto-generated by sqlc - do not edit
+
+module Sqlite3Codegen
+ module ConnectionLock
+ MUTEX = Mutex.new
+
+ def self.for(db)
+ MUTEX.synchronize do
+ db.extend(self) unless db.is_a?(self)
+ db.sqlc_lock
+ end
+ end
+
+ def sqlc_lock = @sqlc_lock ||= Monitor.new
+ end
+
+ TRANSACTION_MODES = %i[deferred immediate exclusive].freeze
+end
diff --git a/examples/sqlite3/connection.rbs b/examples/sqlite3/connection.rbs
new file mode 100644
index 0000000..056f8a2
--- /dev/null
+++ b/examples/sqlite3/connection.rbs
@@ -0,0 +1,12 @@
+# auto-generated by sqlc - do not edit
+module Sqlite3Codegen
+ module ConnectionLock
+ MUTEX: Thread::Mutex
+ @sqlc_lock: Monitor
+
+ def self.for: (untyped db) -> Monitor
+ def sqlc_lock: () -> Monitor
+ end
+
+ TRANSACTION_MODES: Array[Symbol]
+end
diff --git a/examples/sqlite3/models.rb b/examples/sqlite3/models.rb
index 3d30bfb..a880702 100644
--- a/examples/sqlite3/models.rb
+++ b/examples/sqlite3/models.rb
@@ -14,7 +14,9 @@ class DeclaredType < Data.define(
:c_decimal,
:c_unsigned_big_int,
:c_native,
- :c_double
+ :c_double,
+ :c_json,
+ :c_uuid
)
end
diff --git a/examples/sqlite3/models.rbs b/examples/sqlite3/models.rbs
index 10e489a..7b76c71 100644
--- a/examples/sqlite3/models.rbs
+++ b/examples/sqlite3/models.rbs
@@ -21,12 +21,16 @@ module Sqlite3Codegen
attr_reader c_unsigned_big_int: Integer?
attr_reader c_native: String?
attr_reader c_double: Float?
+ attr_reader c_json: String?
+ attr_reader c_uuid: String?
def initialize: (
String? c_varchar,
Float? c_decimal,
Integer? c_unsigned_big_int,
String? c_native,
- Float? c_double
+ Float? c_double,
+ String? c_json,
+ String? c_uuid
) -> void
end
diff --git a/examples/sqlite3/query_sql.rb b/examples/sqlite3/query_sql.rb
index 5b3b136..18a779d 100644
--- a/examples/sqlite3/query_sql.rb
+++ b/examples/sqlite3/query_sql.rb
@@ -3,6 +3,8 @@
# auto-generated by sqlc - do not edit
require 'connection_pool'
require 'sqlite3'
+require 'monitor'
+require_relative 'connection'
require_relative 'models'
module Sqlite3Codegen
@@ -159,11 +161,11 @@ class ListAuthorsByNameBatchArgs < Data.define(:name)
end
INSERT_DECLARED_TYPES_SQL = <<~'SQL'
- INSERT INTO declared_types (c_varchar, c_decimal, c_unsigned_big_int, c_native, c_double) VALUES (?, ?, ?, ?, ?)
+ INSERT INTO declared_types (c_varchar, c_decimal, c_unsigned_big_int, c_native, c_double, c_json, c_uuid) VALUES (?, ?, ?, ?, ?, ?, ?)
SQL
GET_DECLARED_TYPES_SQL = <<~'SQL'
- SELECT c_varchar, c_decimal, c_unsigned_big_int, c_native, c_double FROM declared_types LIMIT 1
+ SELECT c_varchar, c_decimal, c_unsigned_big_int, c_native, c_double, c_json, c_uuid FROM declared_types LIMIT 1
SQL
class GetDeclaredTypesRow < Data.define(
@@ -171,7 +173,9 @@ class GetDeclaredTypesRow < Data.define(
:c_decimal,
:c_unsigned_big_int,
:c_native,
- :c_double
+ :c_double,
+ :c_json,
+ :c_uuid
)
end
@@ -218,33 +222,39 @@ class QuerySql
# Raised by the bang variants (get_author!) when the query returns no row.
class NotFound < StandardError; end
- # db: a SQLite3::Database (a single connection, not shared across threads).
+ # db: a SQLite3::Database (a single connection; threads and fibers using it take turns).
# logger: optional, anything with #debug (Logger, Rails.logger); logs the name and duration of every query.
def initialize(db, logger: nil)
@db = db
@logger = logger
+ @lock = ConnectionLock.for(db)
end
# Runs the block in a transaction and returns its value: COMMIT when the block returns (also through
# break / return / throw), ROLLBACK when it raises or the thread is killed. A nested call becomes a
- # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive.
+ # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive. Other threads / fibers using the same
+ # database wait until it ends.
def transaction(mode: nil)
+ raise ArgumentError, "unknown transaction mode: #{mode.inspect}" unless [nil, *TRANSACTION_MODES].include?(mode)
+
instrument('transaction') do
- outermost = !@db.transaction_active?
- savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
- @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
- rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
- failed = false
- begin
- yield self
- rescue Exception # rubocop:disable Lint/RescueException
- failed = true
- @db.execute(rollback)
- raise
- ensure
- unless failed
- commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ @lock.synchronize do
+ outermost = !@db.transaction_active?
+ savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
+ @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
+ rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
+ failed = false
+ begin
+ yield self
+ rescue Exception # rubocop:disable Lint/RescueException
+ failed = true
+ @db.execute(rollback)
+ raise
+ ensure
+ unless failed
+ commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
+ @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ end
end
end
end
@@ -407,9 +417,11 @@ def get_authors_by_ids_and_names(ids:, names:)
# Bulk insert: pass an Array of CopyAuthorsArgs.
def copy_authors(rows)
with_client('copy_authors') do |client|
- 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] })
+ 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
@@ -548,14 +560,16 @@ def list_authors_by_name_batch(rows)
end
end
- def insert_declared_types(c_varchar:, c_decimal:, c_unsigned_big_int:, c_native:, c_double:)
+ def insert_declared_types(c_varchar:, c_decimal:, c_unsigned_big_int:, c_native:, c_double:, c_json:, c_uuid:)
with_client('insert_declared_types') do |client|
query_params = [
c_varchar,
c_decimal,
c_unsigned_big_int,
c_native,
- c_double
+ c_double,
+ c_json,
+ c_uuid
]
stmt = client.prepare(INSERT_DECLARED_TYPES_SQL)
begin
@@ -578,7 +592,9 @@ def get_declared_types
row[1],
row[2],
row[3],
- row[4]
+ row[4],
+ row[5],
+ row[6]
)
ensure
stmt.close
@@ -631,9 +647,11 @@ def update_regression_prices(price:)
def copy_regressions(rows)
with_client('copy_regressions') do |client|
- rows.each_slice(499) do |batch|
- sql = %(INSERT INTO "regressions" ("order", "price") VALUES #{Array.new(batch.size, '(?, ?)').join(', ')})
- client.execute(sql, batch.flat_map { |row| [row.order, row.price] })
+ atomically(client) do
+ rows.each_slice(499) do |batch|
+ sql = %(INSERT INTO "regressions" ("order", "price") VALUES #{Array.new(batch.size, '(?, ?)').join(', ')})
+ client.execute(sql, batch.flat_map { |row| [row.order, row.price] })
+ end
end
nil
end
@@ -698,7 +716,19 @@ def instrument(name)
end
def with_client(name, &block)
- instrument(name) { block.call(@db) }
+ instrument(name) { @lock.synchronize { block.call(@db) } }
+ end
+
+ def atomically(client)
+ client.execute('SAVEPOINT sqlc_copy')
+ done = false
+ begin
+ yield
+ done = true
+ ensure
+ client.execute('ROLLBACK TO SAVEPOINT sqlc_copy') unless done
+ client.execute('RELEASE SAVEPOINT sqlc_copy')
+ end
end
def slice_placeholders(values)
diff --git a/examples/sqlite3/query_sql.rbs b/examples/sqlite3/query_sql.rbs
index 26c6770..1276e0a 100644
--- a/examples/sqlite3/query_sql.rbs
+++ b/examples/sqlite3/query_sql.rbs
@@ -156,12 +156,16 @@ module Sqlite3Codegen
attr_reader c_unsigned_big_int: Integer?
attr_reader c_native: String?
attr_reader c_double: Float?
+ attr_reader c_json: String?
+ attr_reader c_uuid: String?
def initialize: (
String? c_varchar,
Float? c_decimal,
Integer? c_unsigned_big_int,
String? c_native,
- Float? c_double
+ Float? c_double,
+ String? c_json,
+ String? c_uuid
) -> void
end
@@ -261,7 +265,7 @@ module Sqlite3Codegen
def list_authors_by_name_batch: (Array[ListAuthorsByNameBatchArgs] rows) -> Array[Array[ListAuthorsByNameBatchRow]]
- def insert_declared_types: (c_varchar: String?, c_decimal: Float?, c_unsigned_big_int: Integer?, c_native: String?, c_double: Float?) -> void
+ def insert_declared_types: (c_varchar: String?, c_decimal: Float?, c_unsigned_big_int: Integer?, c_native: String?, c_double: Float?, c_json: String?, c_uuid: String?) -> void
def get_declared_types: -> GetDeclaredTypesRow?
@@ -289,6 +293,8 @@ module Sqlite3Codegen
def with_client: [T] (String name) { (untyped) -> T } -> T
+ def atomically: (untyped client) { () -> void } -> void
+
def slice_placeholders: (Array[untyped] values) -> String
end
end
diff --git a/lib/sqlc_gen_ruby/code.rb b/lib/sqlc_gen_ruby/code.rb
index 2b24e67..41787ba 100644
--- a/lib/sqlc_gen_ruby/code.rb
+++ b/lib/sqlc_gen_ruby/code.rb
@@ -92,6 +92,24 @@ def member_name(name, struct: false)
name
end
+ # Naming/MethodParameterName's default AllowedNames
+ SHORT_NAMES = %w[as at by cc db id if in io ip of on os pp to].freeze
+
+ # The Naming cops that names taken from the schema (a "UserId" column, a 2-letter parameter, non-ASCII) trip.
+ def naming_cops(attributes: [], params: [])
+ cops = []
+ cops << 'Naming/MethodName' unless attributes.all? { |n| n.match?(/\A[\d[[:lower:]]_]+\z/) }
+ cops << 'Naming/MethodParameterName' if params.any? { |n| n.length < 3 && !n.match?(/\d\z/) && !SHORT_NAMES.include?(n) }
+ cops << 'Naming/AsciiIdentifiers' unless params.all?(&:ascii_only?)
+ cops
+ end
+
+ def without_cops(code, cops)
+ return code if cops.empty?
+
+ "# rubocop:disable #{cops.join(', ')}\n#{code}\n# rubocop:enable #{cops.join(', ')}"
+ end
+
def identifier(name)
return '_' if name.empty?
diff --git a/lib/sqlc_gen_ruby/drivers/mysql2.rb b/lib/sqlc_gen_ruby/drivers/mysql2.rb
index 5270402..7e7467c 100644
--- a/lib/sqlc_gen_ruby/drivers/mysql2.rb
+++ b/lib/sqlc_gen_ruby/drivers/mysql2.rb
@@ -70,7 +70,8 @@ def extra_requires(queries, _ruby_version) = copy_from?(queries) ? %w[tempfile]
def helper_methods(queries)
return [] unless copy_from?(queries)
- [[CSV_LINE, 'def csv_line: (Array[untyped] values) -> String']]
+ [[CSV_LINE, 'def csv_line: (Array[untyped] values) -> String'],
+ [LOAD_DATA_HELPER, 'def load_data: (untyped client, String sql) -> void']]
end
CONNECTION_STATE = <<~'RUBY'.chomp
@@ -147,7 +148,32 @@ def with_client(name)
# Template of the generated statement (single-quoted heredoc: backslashes are copied verbatim).
LOAD_DATA = <<~'RUBY'.chomp
- client.query("LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE %s FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (%s)")
+ load_data(client, "LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE %s CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (%s)")
+ RUBY
+
+ # LOAD DATA LOCAL skips duplicate keys and converts invalid values with a warning instead of failing.
+ LOAD_DATA_HELPER = <<~'RUBY'.chomp
+ # Runs LOAD DATA in a transaction (a savepoint inside #transaction): all rows or none, and an error instead
+ # of the warnings LOAD DATA LOCAL gives for duplicate keys and invalid values.
+ def load_data(client, sql)
+ outermost = !client.in_transaction
+ client.query(outermost ? 'BEGIN' : 'SAVEPOINT sqlc_copy')
+ done = false
+ begin
+ client.query(sql)
+ unless client.warning_count.zero?
+ warnings = client.query('SHOW WARNINGS', as: :array).reject { |level, *| level == 'Note' }
+ raise Mysql2::Error, "LOAD DATA: #{warnings.map { |w| w[2] }.join('; ')}" unless warnings.empty?
+ end
+ done = true
+ ensure
+ if outermost
+ client.query(done ? 'COMMIT' : 'ROLLBACK')
+ else
+ client.query(done ? 'RELEASE SAVEPOINT sqlc_copy' : 'ROLLBACK TO SAVEPOINT sqlc_copy')
+ end
+ end
+ end
RUBY
# \N is LOAD DATA's NULL; every other value is enclosed in quotes (an unquoted NULL would also be read as
diff --git a/lib/sqlc_gen_ruby/drivers/pg.rb b/lib/sqlc_gen_ruby/drivers/pg.rb
index 6b380b2..da6e89b 100644
--- a/lib/sqlc_gen_ruby/drivers/pg.rb
+++ b/lib/sqlc_gen_ruby/drivers/pg.rb
@@ -185,7 +185,10 @@ def transaction(isolation: nil)
ensure
unless failed
commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ result = client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ # COMMIT of a transaction aborted by an error the block rescued is a ROLLBACK
+ rolled_back = outermost && result.cmd_status == 'ROLLBACK' && Thread.current.status != 'aborting'
+ raise PG::InFailedSqlTransaction, 'transaction rolled back: a statement in it failed' if rolled_back
end
end
end
diff --git a/lib/sqlc_gen_ruby/drivers/sqlite3.rb b/lib/sqlc_gen_ruby/drivers/sqlite3.rb
index 95b5652..5482496 100644
--- a/lib/sqlc_gen_ruby/drivers/sqlite3.rb
+++ b/lib/sqlc_gen_ruby/drivers/sqlite3.rb
@@ -12,6 +12,16 @@ class Sqlite3 < Base
def name = 'Sqlite3'
+ def module_members = [[CONNECTION_LOCK, CONNECTION_LOCK_RBS]]
+
+ def extra_requires(_queries, _ruby_version) = %w[monitor]
+
+ def helper_methods(queries)
+ return [] unless queries.any? { |q| q.cmd == ':copyfrom' }
+
+ [[ATOMICALLY, 'def atomically: (untyped client) { () -> void } -> void']]
+ end
+
def class_methods
[
[NOT_FOUND, "class NotFound < StandardError\nend"],
@@ -23,7 +33,7 @@ def class_methods
def private_methods
[
[INSTRUMENT, INSTRUMENT_RBS],
- ["def with_client(name, &block)\n instrument(name) { block.call(@db) }\nend",
+ ["def with_client(name, &block)\n instrument(name) { @lock.synchronize { block.call(@db) } }\nend",
'def with_client: [T] (String name) { (untyped) -> T } -> T']
]
end
@@ -64,45 +74,97 @@ def copy_from_lines(rows_var, table, columns, values, _label)
"sql = %(INSERT INTO #{table} (#{columns.join(', ')}) VALUES \#{Array.new(batch.size, '#{placeholders}').join(', ')})",
"client.execute(sql, batch.flat_map { |row| [#{values.join(', ')}] })"
].join("\n")
- [Code.block("#{rows_var}.each_slice(#{rows_per_batch}) do |batch|", body), 'nil']
+ [Code.block('atomically(client) do', Code.block("#{rows_var}.each_slice(#{rows_per_batch}) do |batch|", body)), 'nil']
end
INITIALIZE = <<~'RUBY'.chomp
- # db: a SQLite3::Database (a single connection, not shared across threads).
+ # db: a SQLite3::Database (a single connection; threads and fibers using it take turns).
# logger: optional, anything with #debug (Logger, Rails.logger); logs the name and duration of every query.
def initialize(db, logger: nil)
@db = db
@logger = logger
+ @lock = ConnectionLock.for(db)
end
RUBY
TRANSACTION = <<~'RUBY'.chomp
# Runs the block in a transaction and returns its value: COMMIT when the block returns (also through
# break / return / throw), ROLLBACK when it raises or the thread is killed. A nested call becomes a
- # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive.
+ # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive. Other threads / fibers using the same
+ # database wait until it ends.
def transaction(mode: nil)
+ raise ArgumentError, "unknown transaction mode: #{mode.inspect}" unless [nil, *TRANSACTION_MODES].include?(mode)
+
instrument('transaction') do
- outermost = !@db.transaction_active?
- savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
- @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
- rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
- failed = false
- begin
- yield self
- rescue Exception # rubocop:disable Lint/RescueException
- failed = true
- @db.execute(rollback)
- raise
- ensure
- unless failed
- commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ @lock.synchronize do
+ outermost = !@db.transaction_active?
+ savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
+ @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
+ rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
+ failed = false
+ begin
+ yield self
+ rescue Exception # rubocop:disable Lint/RescueException
+ failed = true
+ @db.execute(rollback)
+ raise
+ ensure
+ unless failed
+ commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
+ @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ end
end
end
end
end
RUBY
+ # A SQLite3::Database runs one transaction at a time: every query class using it shares one lock, kept on the
+ # database (a Monitor, owned per Fiber, so a transaction's own queries re-enter it).
+ CONNECTION_LOCK = <<~'RUBY'.chomp
+ module ConnectionLock
+ MUTEX = Mutex.new
+
+ def self.for(db)
+ MUTEX.synchronize do
+ db.extend(self) unless db.is_a?(self)
+ db.sqlc_lock
+ end
+ end
+
+ def sqlc_lock = @sqlc_lock ||= Monitor.new
+ end
+
+ TRANSACTION_MODES = %i[deferred immediate exclusive].freeze
+ RUBY
+
+ CONNECTION_LOCK_RBS = <<~'RBS'.chomp
+ module ConnectionLock
+ MUTEX: Thread::Mutex
+ @sqlc_lock: Monitor
+
+ def self.for: (untyped db) -> Monitor
+ def sqlc_lock: () -> Monitor
+ end
+
+ TRANSACTION_MODES: Array[Symbol]
+ RBS
+
+ # All rows or none: a SAVEPOINT is a transaction of its own outside #transaction.
+ ATOMICALLY = <<~'RUBY'.chomp
+ def atomically(client)
+ client.execute('SAVEPOINT sqlc_copy')
+ done = false
+ begin
+ yield
+ done = true
+ ensure
+ client.execute('ROLLBACK TO SAVEPOINT sqlc_copy') unless done
+ client.execute('RELEASE SAVEPOINT sqlc_copy')
+ end
+ end
+ RUBY
+
private
# VARCHAR(255) -> varchar, UNSIGNED BIG INT -> unsignedbigint
@@ -125,7 +187,7 @@ def column_mappings
# Integers, blobs are Strings, dates are text.
@column_mappings ||= [
mapping('Integer', %w[int integer tinyint smallint mediumint bigint unsignedbigint int2 int8 boolean bool]),
- mapping('String', %w[blob datetime date timestamp time]),
+ mapping('String', %w[blob datetime date timestamp time json jsonb uuid]),
mapping('String', %w[character varchar varyingcharacter nchar nativecharacter nvarchar text clob],
prefix_based: true),
mapping('Float', %w[real double doubleprecision float decimal numeric], prefix_based: true)
diff --git a/lib/sqlc_gen_ruby/generator.rb b/lib/sqlc_gen_ruby/generator.rb
index 24f130c..09e574c 100644
--- a/lib/sqlc_gen_ruby/generator.rb
+++ b/lib/sqlc_gen_ruby/generator.rb
@@ -195,6 +195,7 @@ def dataclass(name, pairs, doc: nil, attr_docs: {})
raise ArgumentError, "duplicate column names in #{name}: #{dupes.join(', ')}" unless dupes.empty?
code, rbs = @options.ruby_version.immutable_data_supported? ? data_define(name, pairs, attr_docs) : new_struct(name, pairs, attr_docs)
+ code = Code.without_cops(code, Code.naming_cops(attributes: pairs.map(&:first)))
comment = Code.comment(doc)
comment.empty? ? [code, rbs] : ["#{comment}\n#{code}", "#{comment}\n#{rbs}"]
end
diff --git a/lib/sqlc_gen_ruby/method_gen.rb b/lib/sqlc_gen_ruby/method_gen.rb
index b6f14f2..3a5dddf 100644
--- a/lib/sqlc_gen_ruby/method_gen.rb
+++ b/lib/sqlc_gen_ruby/method_gen.rb
@@ -69,6 +69,7 @@ def code(query)
end
doc = query.comments.map { |c| "# #{c.strip}".rstrip }.join("\n")
method = Code.block("def #{func}#{signature}", method_body(func, statements))
+ method = Code.without_cops(method, Code.naming_cops(params: params.map(&:name)))
method = "#{doc}\n#{method}" unless doc.empty?
query.cmd == ':one' ? "#{method}\n\n#{bang_method(query, func)}" : method
end
@@ -189,7 +190,7 @@ def batch_statements(query, func, params)
statements = [@driver.prepare_stmt(stmt_name, const)].compact
cmd = BATCH_COMMANDS.fetch(query.cmd)
body = []
- body << "query_params = [#{Code.comma_join(params.map { |p| "args.#{p.name}" })}]" unless params.empty?
+ body << "query_params = [#{Code.comma_join(params.map { |p| @driver.encode_param("args.#{p.name}", p.column) })}]" unless params.empty?
body.concat(command_statements(query, stmt_name, const, !params.empty?, cmd: cmd))
body.pop if cmd == ':exec' # the trailing nil
block_arg = params.empty? ? '_args' : 'args'
diff --git a/lib/sqlc_gen_ruby/options.rb b/lib/sqlc_gen_ruby/options.rb
index d190278..2c6d465 100644
--- a/lib/sqlc_gen_ruby/options.rb
+++ b/lib/sqlc_gen_ruby/options.rb
@@ -9,6 +9,7 @@ class Options
DRIVERS = %w[mysql2 pg sqlite3].freeze
KNOWN = %w[driver generateGemfile generateTypes rubyVersion overrides moduleName preparedStatements rename
exactTableNames].freeze
+ OVERRIDE_KEYS = %w[column db_type ruby_type].freeze
# `overrides:` entry, matched by `column:` (":", "*:", ".",
# "..") or `db_type:`; type/not_null come from `ruby_type:`
@@ -96,6 +97,12 @@ def parse_overrides(list)
list.map do |entry|
raise ArgumentError, 'each override must be an object' unless entry.is_a?(Hash)
+ unknown = entry.keys - OVERRIDE_KEYS
+ unless unknown.empty?
+ hints = unknown.map { |key| (near = OVERRIDE_KEYS.find { |k| k.start_with?(key[0, 3]) }) ? "#{key} (did you mean #{near}?)" : key }
+ raise ArgumentError, "unknown override keys: #{hints.join(', ')}; supported: #{OVERRIDE_KEYS.join(', ')}"
+ end
+
label = entry['column'] || entry['db_type']
type = entry['ruby_type']
raise ArgumentError, "override #{label} needs ruby_type.type" unless type.is_a?(Hash) && type['type'].is_a?(String)
diff --git a/lib/sqlc_gen_ruby/ruby_version.rb b/lib/sqlc_gen_ruby/ruby_version.rb
index deebd40..5c619d3 100644
--- a/lib/sqlc_gen_ruby/ruby_version.rb
+++ b/lib/sqlc_gen_ruby/ruby_version.rb
@@ -21,10 +21,10 @@ def initialize(major, minor = nil, patch = nil)
def self.parse(pattern)
version = parse_semantic(pattern)
if version < MIN_SUPPORTED
- raise ArgumentError, "Provided version #{version} exceeds min version #{MIN_SUPPORTED}"
+ raise ArgumentError, "Provided version #{version} is below the minimum supported version #{MIN_SUPPORTED}"
end
if version > MAX_SUPPORTED
- raise ArgumentError, "Provided version #{version} exceeds max version #{MAX_SUPPORTED}"
+ raise ArgumentError, "Provided version #{version} is above the maximum supported version #{MAX_SUPPORTED}"
end
version
diff --git a/lib/sqlc_gen_ruby/type_resolver.rb b/lib/sqlc_gen_ruby/type_resolver.rb
index 8436afa..83623cf 100644
--- a/lib/sqlc_gen_ruby/type_resolver.rb
+++ b/lib/sqlc_gen_ruby/type_resolver.rb
@@ -15,7 +15,8 @@ def initialize(driver, options)
def type(column, query_name: nil, table: column.table, fallback: 'untyped')
override = @options.override_for(query_name, column, table: table)
base = override ? override.type : driver_type(column, query_name, fallback)
- base = array_of(base, column) if column.is_array && !override
+ # a db_type override names the element type; a column override the whole type
+ base = array_of(base, column) if column.is_array && (override.nil? || override.db_type)
not_null?(column, override) || base == 'untyped' ? base : "#{base}?"
end
diff --git a/scripts/wasm/build.sh b/scripts/wasm/build.sh
index bca558b..bbff518 100755
--- a/scripts/wasm/build.sh
+++ b/scripts/wasm/build.sh
@@ -33,6 +33,9 @@ cp -r "$SRC/lib/ruby/3.3.0" "$STAGE/usr/local/lib/ruby/3.3.0"
psych psych.rb yaml yaml.rb unicode_normalize optparse optparse.rb open3 open3.rb ripper ripper.rb \
random rinda benchmark benchmark.rb pstore.rb tempfile.rb tmpdir.rb un.rb webrick expect.rb getoptlong.rb \
observer.rb abbrev.rb base64.rb mutex_m.rb nkf.rb syslog.rb)
+# the interpreter requires syntax_suggest/core_ext to format errors and warns "`syntax_suggest' was not loaded"
+# before the plugin's own message when it is missing: keep an empty stub
+mkdir -p "$STAGE/usr/local/lib/ruby/3.3.0/syntax_suggest" && : > "$STAGE/usr/local/lib/ruby/3.3.0/syntax_suggest/core_ext.rb"
mkdir -p "$STAGE/usr/local/lib/ruby/site_ruby" "$STAGE/usr/local/lib/ruby/vendor_ruby" "$STAGE/usr/local/lib/ruby/gems"
cp -r lib exe "$STAGE/app/"
# sqlc runs the module as `plugin.wasm /plugin.CodegenService/Generate`, so the interpreter treats that
diff --git a/steep_expectations.yml b/steep_expectations.yml
index 0bb84f3..e6d8dc6 100644
--- a/steep_expectations.yml
+++ b/steep_expectations.yml
@@ -3,182 +3,202 @@
diagnostics:
- range:
start:
- line: 249
+ line: 259
character: 10
end:
- line: 249
+ line: 259
character: 24
severity: WARNING
message: 'Cannot find the declaration of constant: `ConnectionPool`'
code: Ruby::UnknownConstant
- range:
start:
- line: 249
+ line: 259
character: 47
end:
- line: 249
+ line: 259
character: 53
severity: WARNING
message: 'Cannot find the declaration of constant: `Mysql2`'
code: Ruby::UnknownConstant
- range:
start:
- line: 249
+ line: 259
character: 66
end:
- line: 249
+ line: 259
character: 81
severity: WARNING
message: Empty kwargs doesn't have type annotation
code: Ruby::UnannotatedEmptyCollection
+ - range:
+ start:
+ line: 855
+ character: 16
+ end:
+ line: 855
+ character: 22
+ severity: WARNING
+ message: 'Cannot find the declaration of constant: `Mysql2`'
+ code: Ruby::UnknownConstant
- file: examples/pg/query_sql.rb
diagnostics:
- range:
start:
- line: 346
+ line: 353
character: 10
end:
- line: 346
+ line: 353
character: 24
severity: WARNING
message: 'Cannot find the declaration of constant: `ConnectionPool`'
code: Ruby::UnknownConstant
- range:
start:
- line: 346
+ line: 353
character: 47
end:
- line: 346
+ line: 353
character: 49
severity: WARNING
message: 'Cannot find the declaration of constant: `PG`'
code: Ruby::UnknownConstant
- range:
start:
- line: 346
+ line: 353
character: 58
end:
- line: 346
+ line: 353
character: 69
severity: WARNING
message: Empty kwargs doesn't have type annotation
code: Ruby::UnannotatedEmptyCollection
- range:
start:
- line: 361
+ line: 368
character: 49
end:
- line: 361
+ line: 368
character: 51
severity: WARNING
message: 'Cannot find the declaration of constant: `PG`'
code: Ruby::UnknownConstant
- range:
start:
- line: 571
+ line: 389
+ character: 18
+ end:
+ line: 389
+ character: 20
+ severity: WARNING
+ message: 'Cannot find the declaration of constant: `PG`'
+ code: Ruby::UnknownConstant
+ - range:
+ start:
+ line: 581
character: 18
end:
- line: 571
+ line: 581
character: 20
severity: WARNING
message: 'Cannot find the declaration of constant: `PG`'
code: Ruby::UnknownConstant
- range:
start:
- line: 977
+ line: 987
character: 18
end:
- line: 977
+ line: 987
character: 20
severity: WARNING
message: 'Cannot find the declaration of constant: `PG`'
code: Ruby::UnknownConstant
- range:
start:
- line: 1044
+ line: 1068
character: 17
end:
- line: 1044
+ line: 1068
character: 19
severity: WARNING
message: 'Cannot find the declaration of constant: `PG`'
code: Ruby::UnknownConstant
- range:
start:
- line: 1047
+ line: 1071
character: 36
end:
- line: 1047
+ line: 1071
character: 38
severity: WARNING
message: 'Cannot find the declaration of constant: `PG`'
code: Ruby::UnknownConstant
- range:
start:
- line: 1053
+ line: 1077
character: 16
end:
- line: 1053
+ line: 1077
character: 18
severity: WARNING
message: 'Cannot find the declaration of constant: `PG`'
code: Ruby::UnknownConstant
- range:
start:
- line: 1054
+ line: 1078
character: 15
end:
- line: 1054
+ line: 1078
character: 17
severity: WARNING
message: 'Cannot find the declaration of constant: `PG`'
code: Ruby::UnknownConstant
- range:
start:
- line: 1055
+ line: 1079
character: 79
end:
- line: 1055
+ line: 1079
character: 81
severity: WARNING
message: Empty array doesn't have type annotation
code: Ruby::UnannotatedEmptyCollection
- range:
start:
- line: 1056
+ line: 1080
character: 25
end:
- line: 1056
+ line: 1080
character: 27
severity: WARNING
message: 'Cannot find the declaration of constant: `PG`'
code: Ruby::UnknownConstant
- range:
start:
- line: 1057
+ line: 1081
character: 67
end:
- line: 1057
+ line: 1081
character: 69
severity: WARNING
message: 'Cannot find the declaration of constant: `PG`'
code: Ruby::UnknownConstant
- range:
start:
- line: 1065
+ line: 1089
character: 15
end:
- line: 1065
+ line: 1089
character: 17
severity: WARNING
message: 'Cannot find the declaration of constant: `PG`'
code: Ruby::UnknownConstant
- range:
start:
- line: 1070
+ line: 1094
character: 27
end:
- line: 1070
+ line: 1094
character: 29
severity: WARNING
message: 'Cannot find the declaration of constant: `PG`'
diff --git a/test/features_test.rb b/test/features_test.rb
index e427d11..f41eee7 100644
--- a/test/features_test.rb
+++ b/test/features_test.rb
@@ -442,6 +442,51 @@ def test_parameters_named_like_generated_locals_are_renamed
assert_includes generate(request).fetch('query_sql.rb'), "def get_author(client_:)\n"
end
+ def test_db_type_overrides_also_type_array_elements
+ overrides = [{ db_type: 'uuid', ruby_type: { type: 'UUIDString' } }]
+ rbs = generate(load_request('pg', overrides: overrides)).fetch('query_sql.rbs')
+ assert_includes rbs, 'attr_reader uids: Array[UUIDString]?'
+ end
+
+ def test_batch_parameters_are_encoded_like_other_parameters
+ request = load_request('pg')
+ query(request, 'CreateAuthorsBatch').params[1].column.type.name = 'bytea'
+ assert_includes generate(request).fetch('query_sql.rb'), 'query_params = [args.name, encode_bytea(args.bio)]'
+ end
+
+ def test_sqlite_json_and_uuid_declarations_are_strings
+ request = load_request('sqlite3')
+ query(request, 'GetAuthor').columns[1].type.name = 'JSON'
+ query(request, 'GetAuthor').columns[2].type.name = 'UUID'
+ assert_match(/class GetAuthorRow\n\s+attr_reader id: Integer\n\s+attr_reader name: String\n\s+attr_reader bio: String\?/,
+ generate(request).fetch('query_sql.rbs'))
+ end
+
+ def test_schema_names_breaking_naming_cops_are_exempted_where_they_occur
+ request = load_request('pg')
+ get_author = query(request, 'GetAuthor')
+ get_author.columns[1].name = 'UserId'
+ get_author.columns[2].name = 'naïve'
+ get_author.params.first.column.name = 'm'
+ code = generate(request).fetch('query_sql.rb')
+ assert_match(/# rubocop:disable Naming\/MethodName\n\s+class GetAuthorRow < Data.define\(:id, :UserId, :naïve\)\n\s+end\n\s+# rubocop:enable/, code)
+ assert_match(/# rubocop:disable Naming\/MethodParameterName\n\s+def get_author\(m:\)/, code)
+ assert_equal 1, code.scan('rubocop:disable Naming/MethodParameterName').size
+ out, status = rubocop(code)
+ assert status.success?, out
+ end
+
+ def rubocop(source)
+ exe = begin
+ Gem.bin_path('rubocop', 'rubocop')
+ rescue Gem::GemNotFoundException
+ skip 'rubocop not installed'
+ end
+ out, _err, status = Open3.capture3(RbConfig.ruby, exe, '--stdin', 'examples/pg/query_sql.rb',
+ '--format', 'emacs', stdin_data: source, chdir: ROOT)
+ [out, status]
+ end
+
def test_columns_become_valid_non_overriding_attributes
request = load_request('pg')
columns = query(request, 'GetAuthor').columns
diff --git a/test/fixtures/mysql2.bin b/test/fixtures/mysql2.bin
index 0a8560c..ad0f6e3 100644
--- a/test/fixtures/mysql2.bin
+++ b/test/fixtures/mysql2.bin
@@ -1,7 +1,7 @@
-2mysql¤public"™public€
+2mysqlÍpublic"Âpublic€
authors&
id0ÿÿÿÿÿÿÿÿÿR authorsbbigint&
name0ÿÿÿÿÿÿÿÿÿR authorsbtext#
@@ -70,11 +70,12 @@ c_longtext0
author_name0dR
extended_biosb varchar$
name0dR
extended_biosb varchar7
bio_type0
R
extended_biosbextended_bios_bio_type=
-author_type0R
extended_biosbextended_bios_author_type�
+author_type0R
extended_biosbextended_bios_author_type¶
regressions*
id0ÿÿÿÿÿÿÿÿÿR
regressionsbbigint&
order0ÿR
regressionsb varchar(
-note0ÿÿÿÿÿÿÿÿÿR
regressionsbtext":
+note0ÿÿÿÿÿÿÿÿÿR
regressionsbtext'
+payload0R
regressionsb varbinary":
extended_bios_bio_type
Autobiography BiographyMemoir"7
extended_bios_author_typeAuthorEditor
TranslatorË
@@ -271,5 +272,13 @@ KSELECT id, `order`, note FROM regressions WHERE id IN (?, ?, ?) ORDER BY idLi
id0ÿÿÿÿÿÿÿÿÿR
regressionsbbigintzid: query.sqlä
5INSERT INTO regressions (`order`, note) VALUES (?, ?)CopyRegressions :copyfrom*95
order0ÿRpublicregressionsb varcharzorder*:6
-note0ÿÿÿÿÿÿÿÿÿRpublicregressionsbtextznote: query.sqlB
regressionsC
+note0ÿÿÿÿÿÿÿÿÿRpublicregressionsbtextznote: query.sqlB
regressionsô
+HINSERT INTO regressions (id, `order`, note, payload) VALUES (?, ?, ?, ?)CopyRegressionRows :copyfrom*:6
+id0ÿÿÿÿÿÿÿÿÿRpublicregressionsbbigintzid*95
+order0ÿRpublicregressionsb varcharzorder*:6
+note0ÿÿÿÿÿÿÿÿÿRpublicregressionsbtextznote*<8
+payload0Rpublicregressionsb varbinaryzpayload: query.sqlB
regressions½
+/SELECT id, payload FROM regressions ORDER BY idListRegressionPayloads:many".
+id0ÿÿÿÿÿÿÿÿÿR
regressionsbbigintzid"0
+payload0R
regressionsb varbinaryzpayload: query.sqlC
TRUNCATE TABLE regressionsTruncateRegressions: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 6b3223e..5c89375 100644
--- a/test/fixtures/pg.bin
+++ b/test/fixtures/pg.bin
@@ -355,6 +355,12 @@ KINSERT INTO regressions (id, "order", payload, doc) VALUES ($1, $2, $3, $4)Co
id0ÿÿÿÿÿÿÿÿÿRpublicregressionsbpg_catalog.int8zid*>:
order0ÿÿÿÿÿÿÿÿÿRpublicregressionsbtextzorder*A=
payload0ÿÿÿÿÿÿÿÿÿRpublicregressionsbbyteazpayload*95
+doc0ÿÿÿÿÿÿÿÿÿRpublicregressionsbjsonbzdoc: query.sqlB
regressionsŽ
+KINSERT INTO regressions (id, "order", payload, doc) VALUES ($1, $2, $3, $4)CreateRegressionsBatch
+:batchexec*C?
+id0ÿÿÿÿÿÿÿÿÿRpublicregressionsbpg_catalog.int8zid*>:
+order0ÿÿÿÿÿÿÿÿÿRpublicregressionsbtextzorder*A=
+payload0ÿÿÿÿÿÿÿÿÿRpublicregressionsbbyteazpayload*95
doc0ÿÿÿÿÿÿÿÿÿRpublicregressionsbjsonbzdoc: query.sqlB
regressions—
wSELECT id, "order", payload, doc, bio_types, uids FROM regressions WHERE ($1::text IS NULL OR "order" = $1) ORDER BY idListRegressions:many"8
id0ÿÿÿÿÿÿÿÿÿR
regressionsb
diff --git a/test/fixtures/sqlite3.bin b/test/fixtures/sqlite3.bin
index cd5aec5..e50bb15 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 2bce311..95ad226 100644
--- a/test/golden/mysql2/default/models.rb
+++ b/test/golden/mysql2/default/models.rb
@@ -67,6 +67,6 @@ class Book < Data.define(:id, :name, :author_id, :description)
class ExtendedBio < Data.define(:author_name, :name, :bio_type, :author_type)
end
- class Regression < Data.define(:id, :order, :note)
+ class Regression < Data.define(:id, :order, :note, :payload)
end
end
diff --git a/test/golden/mysql2/default/models.rbs b/test/golden/mysql2/default/models.rbs
index e19b214..b97f70b 100644
--- a/test/golden/mysql2/default/models.rbs
+++ b/test/golden/mysql2/default/models.rbs
@@ -120,6 +120,7 @@ module Mysql2Codegen
attr_reader id: Integer
attr_reader order: String
attr_reader note: String?
- def initialize: ( Integer id, String order, String? note) -> void
+ attr_reader payload: String?
+ def initialize: ( Integer id, String order, String? note, String? payload) -> void
end
end
diff --git a/test/golden/mysql2/default/query_sql.rb b/test/golden/mysql2/default/query_sql.rb
index e9aff17..6ad9117 100644
--- a/test/golden/mysql2/default/query_sql.rb
+++ b/test/golden/mysql2/default/query_sql.rb
@@ -235,6 +235,16 @@ class ListRegressionsByIdsRow < Data.define(:id, :order, :note)
class CopyRegressionsArgs < Data.define(:order, :note)
end
+ class CopyRegressionRowsArgs < Data.define(:id, :order, :note, :payload)
+ end
+
+ LIST_REGRESSION_PAYLOADS_SQL = <<~'SQL'
+ SELECT id, payload FROM regressions ORDER BY id
+ SQL
+
+ class ListRegressionPayloadsRow < Data.define(:id, :payload)
+ end
+
TRUNCATE_REGRESSIONS_SQL = <<~'SQL'
TRUNCATE TABLE regressions
SQL
@@ -553,7 +563,7 @@ def copy_authors(rows)
file.write(csv_line([row.name, row.bio]))
end
file.flush
- client.query("LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `authors` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`name`, `bio`)")
+ load_data(client, "LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `authors` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`name`, `bio`)")
end
nil
end
@@ -778,12 +788,41 @@ def copy_regressions(rows)
file.write(csv_line([row.order, row.note]))
end
file.flush
- client.query("LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `regressions` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`order`, `note`)")
+ load_data(client, "LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `regressions` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`order`, `note`)")
+ end
+ nil
+ end
+ end
+
+ def copy_regression_rows(rows)
+ with_client('copy_regression_rows') do |client|
+ return if rows.empty?
+
+ Tempfile.create(['copy_regression_rows', '.csv']) do |file|
+ rows.each do |row|
+ file.write(csv_line([row.id, row.order, row.note, row.payload]))
+ end
+ file.flush
+ load_data(client, "LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `regressions` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`id`, `order`, `note`, `payload`)")
end
nil
end
end
+ 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
+ end
+ end
+ end
+
def truncate_regressions
with_client('truncate_regressions') do |client|
stmt = client.prepare(TRUNCATE_REGRESSIONS_SQL)
@@ -893,6 +932,28 @@ def csv_line(values)
"#{fields.join(',')}\n"
end
+ # Runs LOAD DATA in a transaction (a savepoint inside #transaction): all rows or none, and an error instead
+ # of the warnings LOAD DATA LOCAL gives for duplicate keys and invalid values.
+ def load_data(client, sql)
+ outermost = !client.in_transaction
+ client.query(outermost ? 'BEGIN' : 'SAVEPOINT sqlc_copy')
+ done = false
+ begin
+ client.query(sql)
+ unless client.warning_count.zero?
+ warnings = client.query('SHOW WARNINGS', as: :array).reject { |level, *| level == 'Note' }
+ raise Mysql2::Error, "LOAD DATA: #{warnings.map { |w| w[2] }.join('; ')}" unless warnings.empty?
+ end
+ done = true
+ ensure
+ if outermost
+ client.query(done ? 'COMMIT' : 'ROLLBACK')
+ else
+ client.query(done ? 'RELEASE SAVEPOINT sqlc_copy' : 'ROLLBACK TO SAVEPOINT sqlc_copy')
+ end
+ end
+ end
+
def slice_placeholders(values)
values.empty? ? 'NULL' : Array.new(values.size, '?').join(', ')
end
diff --git a/test/golden/mysql2/default/query_sql.rbs b/test/golden/mysql2/default/query_sql.rbs
index d08c449..e38d25c 100644
--- a/test/golden/mysql2/default/query_sql.rbs
+++ b/test/golden/mysql2/default/query_sql.rbs
@@ -255,6 +255,22 @@ module Mysql2Codegen
def initialize: ( String order, String? note) -> void
end
+ class CopyRegressionRowsArgs
+ 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_REGRESSION_PAYLOADS_SQL: String
+
+ class ListRegressionPayloadsRow
+ attr_reader id: Integer
+ attr_reader payload: String?
+ def initialize: ( Integer id, String? payload) -> void
+ end
+
TRUNCATE_REGRESSIONS_SQL: String
MANY_PARAMS_SQL: String
@@ -372,6 +388,10 @@ module Mysql2Codegen
def copy_regressions: (Array[CopyRegressionsArgs] rows) -> void
+ def copy_regression_rows: (Array[CopyRegressionRowsArgs] rows) -> void
+
+ def list_regression_payloads: -> Array[ListRegressionPayloadsRow]
+
def truncate_regressions: -> void
def many_params: (c0: Integer, c1: String, c2: Float, c3: String, c4: Integer) -> ManyParamsRow?
@@ -390,6 +410,8 @@ module Mysql2Codegen
def csv_line: (Array[untyped] values) -> String
+ def load_data: (untyped client, String sql) -> void
+
def slice_placeholders: (Array[untyped] values) -> String
end
end
diff --git a/test/golden/mysql2/ruby31_gemfile/models.rb b/test/golden/mysql2/ruby31_gemfile/models.rb
index 4966c7c..c22168b 100644
--- a/test/golden/mysql2/ruby31_gemfile/models.rb
+++ b/test/golden/mysql2/ruby31_gemfile/models.rb
@@ -76,5 +76,11 @@ module ExtendedBiosAuthorType
keyword_init: true
)
- Regression = Struct.new(:id, :order, :note, keyword_init: true)
+ Regression = Struct.new(
+ :id,
+ :order,
+ :note,
+ :payload,
+ keyword_init: true
+ )
end
diff --git a/test/golden/mysql2/ruby31_gemfile/models.rbs b/test/golden/mysql2/ruby31_gemfile/models.rbs
index 8dce5e5..26d60db 100644
--- a/test/golden/mysql2/ruby31_gemfile/models.rbs
+++ b/test/golden/mysql2/ruby31_gemfile/models.rbs
@@ -162,7 +162,8 @@ module Mysql2Codegen
attr_accessor id: Integer
attr_accessor order: String
attr_accessor note: String?
- def initialize: ( id: Integer, order: String, note: String?) -> void
- def self.new: ( id: Integer, order: String, note: String?) -> Regression
+ 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?) -> Regression
end
end
diff --git a/test/golden/mysql2/ruby31_gemfile/query_sql.rb b/test/golden/mysql2/ruby31_gemfile/query_sql.rb
index 9448b30..e767e3e 100644
--- a/test/golden/mysql2/ruby31_gemfile/query_sql.rb
+++ b/test/golden/mysql2/ruby31_gemfile/query_sql.rb
@@ -228,6 +228,20 @@ module Mysql2Codegen
CopyRegressionsArgs = Struct.new(:order, :note, keyword_init: true)
+ CopyRegressionRowsArgs = Struct.new(
+ :id,
+ :order,
+ :note,
+ :payload,
+ keyword_init: true
+ )
+
+ LIST_REGRESSION_PAYLOADS_SQL = <<~'SQL'
+ SELECT id, payload FROM regressions ORDER BY id
+ SQL
+
+ ListRegressionPayloadsRow = Struct.new(:id, :payload, keyword_init: true)
+
TRUNCATE_REGRESSIONS_SQL = <<~'SQL'
TRUNCATE TABLE regressions
SQL
@@ -549,7 +563,7 @@ def copy_authors(rows)
file.write(csv_line([row.name, row.bio]))
end
file.flush
- client.query("LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `authors` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`name`, `bio`)")
+ load_data(client, "LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `authors` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`name`, `bio`)")
end
nil
end
@@ -787,12 +801,41 @@ def copy_regressions(rows)
file.write(csv_line([row.order, row.note]))
end
file.flush
- client.query("LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `regressions` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`order`, `note`)")
+ load_data(client, "LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `regressions` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`order`, `note`)")
+ end
+ nil
+ end
+ end
+
+ def copy_regression_rows(rows)
+ with_client('copy_regression_rows') do |client|
+ return if rows.empty?
+
+ Tempfile.create(['copy_regression_rows', '.csv']) do |file|
+ rows.each do |row|
+ file.write(csv_line([row.id, row.order, row.note, row.payload]))
+ end
+ file.flush
+ load_data(client, "LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `regressions` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`id`, `order`, `note`, `payload`)")
end
nil
end
end
+ 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
+ end
+ end
+ end
+
def truncate_regressions
with_client('truncate_regressions') do |client|
stmt = client.prepare(TRUNCATE_REGRESSIONS_SQL)
@@ -902,6 +945,28 @@ def csv_line(values)
"#{fields.join(',')}\n"
end
+ # Runs LOAD DATA in a transaction (a savepoint inside #transaction): all rows or none, and an error instead
+ # of the warnings LOAD DATA LOCAL gives for duplicate keys and invalid values.
+ def load_data(client, sql)
+ outermost = !client.in_transaction
+ client.query(outermost ? 'BEGIN' : 'SAVEPOINT sqlc_copy')
+ done = false
+ begin
+ client.query(sql)
+ unless client.warning_count.zero?
+ warnings = client.query('SHOW WARNINGS', as: :array).reject { |level, *| level == 'Note' }
+ raise Mysql2::Error, "LOAD DATA: #{warnings.map { |w| w[2] }.join('; ')}" unless warnings.empty?
+ end
+ done = true
+ ensure
+ if outermost
+ client.query(done ? 'COMMIT' : 'ROLLBACK')
+ else
+ client.query(done ? 'RELEASE SAVEPOINT sqlc_copy' : 'ROLLBACK TO SAVEPOINT sqlc_copy')
+ end
+ end
+ end
+
def slice_placeholders(values)
values.empty? ? 'NULL' : Array.new(values.size, '?').join(', ')
end
diff --git a/test/golden/mysql2/ruby31_gemfile/query_sql.rbs b/test/golden/mysql2/ruby31_gemfile/query_sql.rbs
index c7d3264..1409ee8 100644
--- a/test/golden/mysql2/ruby31_gemfile/query_sql.rbs
+++ b/test/golden/mysql2/ruby31_gemfile/query_sql.rbs
@@ -313,6 +313,24 @@ module Mysql2Codegen
def self.new: ( order: String, note: String?) -> CopyRegressionsArgs
end
+ class CopyRegressionRowsArgs < ::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?) -> CopyRegressionRowsArgs
+ end
+
+ LIST_REGRESSION_PAYLOADS_SQL: String
+
+ class ListRegressionPayloadsRow < ::Struct[untyped]
+ attr_accessor id: Integer
+ attr_accessor payload: String?
+ def initialize: ( id: Integer, payload: String?) -> void
+ def self.new: ( id: Integer, payload: String?) -> ListRegressionPayloadsRow
+ end
+
TRUNCATE_REGRESSIONS_SQL: String
MANY_PARAMS_SQL: String
@@ -440,6 +458,10 @@ module Mysql2Codegen
def copy_regressions: (Array[CopyRegressionsArgs] rows) -> void
+ def copy_regression_rows: (Array[CopyRegressionRowsArgs] rows) -> void
+
+ def list_regression_payloads: -> Array[ListRegressionPayloadsRow]
+
def truncate_regressions: -> void
def many_params: (c0: Integer, c1: String, c2: Float, c3: String, c4: Integer) -> ManyParamsRow?
@@ -458,6 +480,8 @@ module Mysql2Codegen
def csv_line: (Array[untyped] values) -> String
+ def load_data: (untyped client, String sql) -> void
+
def slice_placeholders: (Array[untyped] values) -> String
end
end
diff --git a/test/golden/mysql2/ruby32_notypes/models.rb b/test/golden/mysql2/ruby32_notypes/models.rb
index 2bce311..95ad226 100644
--- a/test/golden/mysql2/ruby32_notypes/models.rb
+++ b/test/golden/mysql2/ruby32_notypes/models.rb
@@ -67,6 +67,6 @@ class Book < Data.define(:id, :name, :author_id, :description)
class ExtendedBio < Data.define(:author_name, :name, :bio_type, :author_type)
end
- class Regression < Data.define(:id, :order, :note)
+ class Regression < Data.define(:id, :order, :note, :payload)
end
end
diff --git a/test/golden/mysql2/ruby32_notypes/query_sql.rb b/test/golden/mysql2/ruby32_notypes/query_sql.rb
index e9aff17..6ad9117 100644
--- a/test/golden/mysql2/ruby32_notypes/query_sql.rb
+++ b/test/golden/mysql2/ruby32_notypes/query_sql.rb
@@ -235,6 +235,16 @@ class ListRegressionsByIdsRow < Data.define(:id, :order, :note)
class CopyRegressionsArgs < Data.define(:order, :note)
end
+ class CopyRegressionRowsArgs < Data.define(:id, :order, :note, :payload)
+ end
+
+ LIST_REGRESSION_PAYLOADS_SQL = <<~'SQL'
+ SELECT id, payload FROM regressions ORDER BY id
+ SQL
+
+ class ListRegressionPayloadsRow < Data.define(:id, :payload)
+ end
+
TRUNCATE_REGRESSIONS_SQL = <<~'SQL'
TRUNCATE TABLE regressions
SQL
@@ -553,7 +563,7 @@ def copy_authors(rows)
file.write(csv_line([row.name, row.bio]))
end
file.flush
- client.query("LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `authors` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`name`, `bio`)")
+ load_data(client, "LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `authors` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`name`, `bio`)")
end
nil
end
@@ -778,12 +788,41 @@ def copy_regressions(rows)
file.write(csv_line([row.order, row.note]))
end
file.flush
- client.query("LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `regressions` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`order`, `note`)")
+ load_data(client, "LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `regressions` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`order`, `note`)")
+ end
+ nil
+ end
+ end
+
+ def copy_regression_rows(rows)
+ with_client('copy_regression_rows') do |client|
+ return if rows.empty?
+
+ Tempfile.create(['copy_regression_rows', '.csv']) do |file|
+ rows.each do |row|
+ file.write(csv_line([row.id, row.order, row.note, row.payload]))
+ end
+ file.flush
+ load_data(client, "LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `regressions` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`id`, `order`, `note`, `payload`)")
end
nil
end
end
+ 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
+ end
+ end
+ end
+
def truncate_regressions
with_client('truncate_regressions') do |client|
stmt = client.prepare(TRUNCATE_REGRESSIONS_SQL)
@@ -893,6 +932,28 @@ def csv_line(values)
"#{fields.join(',')}\n"
end
+ # Runs LOAD DATA in a transaction (a savepoint inside #transaction): all rows or none, and an error instead
+ # of the warnings LOAD DATA LOCAL gives for duplicate keys and invalid values.
+ def load_data(client, sql)
+ outermost = !client.in_transaction
+ client.query(outermost ? 'BEGIN' : 'SAVEPOINT sqlc_copy')
+ done = false
+ begin
+ client.query(sql)
+ unless client.warning_count.zero?
+ warnings = client.query('SHOW WARNINGS', as: :array).reject { |level, *| level == 'Note' }
+ raise Mysql2::Error, "LOAD DATA: #{warnings.map { |w| w[2] }.join('; ')}" unless warnings.empty?
+ end
+ done = true
+ ensure
+ if outermost
+ client.query(done ? 'COMMIT' : 'ROLLBACK')
+ else
+ client.query(done ? 'RELEASE SAVEPOINT sqlc_copy' : 'ROLLBACK TO SAVEPOINT sqlc_copy')
+ end
+ end
+ end
+
def slice_placeholders(values)
values.empty? ? 'NULL' : Array.new(values.size, '?').join(', ')
end
diff --git a/test/golden/pg/default/my_queries_sql.rb b/test/golden/pg/default/my_queries_sql.rb
index 28073e2..c162668 100644
--- a/test/golden/pg/default/my_queries_sql.rb
+++ b/test/golden/pg/default/my_queries_sql.rb
@@ -52,7 +52,10 @@ def transaction(isolation: nil)
ensure
unless failed
commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ result = client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ # COMMIT of a transaction aborted by an error the block rescued is a ROLLBACK
+ rolled_back = outermost && result.cmd_status == 'ROLLBACK' && Thread.current.status != 'aborting'
+ raise PG::InFailedSqlTransaction, 'transaction rolled back: a statement in it failed' if rolled_back
end
end
end
diff --git a/test/golden/pg/default/other_sql.rb b/test/golden/pg/default/other_sql.rb
index a0f1daf..8bd3b15 100644
--- a/test/golden/pg/default/other_sql.rb
+++ b/test/golden/pg/default/other_sql.rb
@@ -65,7 +65,10 @@ def transaction(isolation: nil)
ensure
unless failed
commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ result = client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ # COMMIT of a transaction aborted by an error the block rescued is a ROLLBACK
+ rolled_back = outermost && result.cmd_status == 'ROLLBACK' && Thread.current.status != 'aborting'
+ raise PG::InFailedSqlTransaction, 'transaction rolled back: a statement in it failed' if rolled_back
end
end
end
diff --git a/test/golden/pg/default/query_sql.rb b/test/golden/pg/default/query_sql.rb
index f4732e1..050e2d4 100644
--- a/test/golden/pg/default/query_sql.rb
+++ b/test/golden/pg/default/query_sql.rb
@@ -318,6 +318,13 @@ class CreateRegressionIfNewRow < Data.define(:id)
class CopyRegressionsArgs < Data.define(:id, :order, :payload, :doc)
end
+ CREATE_REGRESSIONS_BATCH_SQL = <<~'SQL'
+ INSERT INTO regressions (id, "order", payload, doc) VALUES ($1, $2, $3, $4)
+ SQL
+
+ class CreateRegressionsBatchArgs < Data.define(:id, :order, :payload, :doc)
+ end
+
LIST_REGRESSIONS_SQL = <<~'SQL'
SELECT id, "order", payload, doc, bio_types, uids FROM regressions WHERE ($1::text IS NULL OR "order" = $1) ORDER BY id
SQL
@@ -406,7 +413,10 @@ def transaction(isolation: nil)
ensure
unless failed
commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ result = client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ # COMMIT of a transaction aborted by an error the block rescued is a ROLLBACK
+ rolled_back = outermost && result.cmd_status == 'ROLLBACK' && Thread.current.status != 'aborting'
+ raise PG::InFailedSqlTransaction, 'transaction rolled back: a statement in it failed' if rolled_back
end
end
end
@@ -1014,6 +1024,20 @@ def copy_regressions(rows)
end
end
+ def create_regressions_batch(rows)
+ with_client('create_regressions_batch') do |client|
+ unless client.prepared_statements.include?('query_sql_create_regressions_batch')
+ client.prepare('query_sql_create_regressions_batch', CREATE_REGRESSIONS_BATCH_SQL)
+ client.prepared_statements.add('query_sql_create_regressions_batch')
+ end
+ rows.each do |args|
+ query_params = [args.id, args.order, encode_bytea(args.payload), encode_json(args.doc)]
+ client.exec_prepared('query_sql_create_regressions_batch', query_params)
+ end
+ nil
+ end
+ end
+
def list_regressions(order_name: nil)
with_client('list_regressions') do |client|
query_params = [order_name]
diff --git a/test/golden/pg/default/query_sql.rbs b/test/golden/pg/default/query_sql.rbs
index 7eafbba..c39e328 100644
--- a/test/golden/pg/default/query_sql.rbs
+++ b/test/golden/pg/default/query_sql.rbs
@@ -325,6 +325,16 @@ module PgCodegen
def initialize: ( Integer id, String order, String? payload, untyped doc) -> void
end
+ CREATE_REGRESSIONS_BATCH_SQL: String
+
+ class CreateRegressionsBatchArgs
+ attr_reader id: Integer
+ attr_reader order: String
+ attr_reader payload: String?
+ attr_reader doc: untyped
+ def initialize: ( Integer id, String order, String? payload, untyped doc) -> void
+ end
+
LIST_REGRESSIONS_SQL: String
class ListRegressionsRow
@@ -489,6 +499,8 @@ module PgCodegen
def copy_regressions: (Array[CopyRegressionsArgs] rows) -> void
+ def create_regressions_batch: (Array[CreateRegressionsBatchArgs] rows) -> void
+
def list_regressions: (?order_name: String?) -> Array[ListRegressionsRow]
def truncate_regressions: -> void
diff --git a/test/golden/pg/ruby31_gemfile/my_queries_sql.rb b/test/golden/pg/ruby31_gemfile/my_queries_sql.rb
index a5cce42..044886b 100644
--- a/test/golden/pg/ruby31_gemfile/my_queries_sql.rb
+++ b/test/golden/pg/ruby31_gemfile/my_queries_sql.rb
@@ -53,7 +53,10 @@ def transaction(isolation: nil)
ensure
unless failed
commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ result = client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ # COMMIT of a transaction aborted by an error the block rescued is a ROLLBACK
+ rolled_back = outermost && result.cmd_status == 'ROLLBACK' && Thread.current.status != 'aborting'
+ raise PG::InFailedSqlTransaction, 'transaction rolled back: a statement in it failed' if rolled_back
end
end
end
diff --git a/test/golden/pg/ruby31_gemfile/other_sql.rb b/test/golden/pg/ruby31_gemfile/other_sql.rb
index 681af89..571ea0e 100644
--- a/test/golden/pg/ruby31_gemfile/other_sql.rb
+++ b/test/golden/pg/ruby31_gemfile/other_sql.rb
@@ -65,7 +65,10 @@ def transaction(isolation: nil)
ensure
unless failed
commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ result = client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ # COMMIT of a transaction aborted by an error the block rescued is a ROLLBACK
+ rolled_back = outermost && result.cmd_status == 'ROLLBACK' && Thread.current.status != 'aborting'
+ raise PG::InFailedSqlTransaction, 'transaction rolled back: a statement in it failed' if rolled_back
end
end
end
diff --git a/test/golden/pg/ruby31_gemfile/query_sql.rb b/test/golden/pg/ruby31_gemfile/query_sql.rb
index c8ce28c..8d8dc80 100644
--- a/test/golden/pg/ruby31_gemfile/query_sql.rb
+++ b/test/golden/pg/ruby31_gemfile/query_sql.rb
@@ -307,6 +307,18 @@ module PgCodegen
keyword_init: true
)
+ CREATE_REGRESSIONS_BATCH_SQL = <<~'SQL'
+ INSERT INTO regressions (id, "order", payload, doc) VALUES ($1, $2, $3, $4)
+ SQL
+
+ CreateRegressionsBatchArgs = Struct.new(
+ :id,
+ :order,
+ :payload,
+ :doc,
+ keyword_init: true
+ )
+
LIST_REGRESSIONS_SQL = <<~'SQL'
SELECT id, "order", payload, doc, bio_types, uids FROM regressions WHERE ($1::text IS NULL OR "order" = $1) ORDER BY id
SQL
@@ -399,7 +411,10 @@ def transaction(isolation: nil)
ensure
unless failed
commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ result = client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ # COMMIT of a transaction aborted by an error the block rescued is a ROLLBACK
+ rolled_back = outermost && result.cmd_status == 'ROLLBACK' && Thread.current.status != 'aborting'
+ raise PG::InFailedSqlTransaction, 'transaction rolled back: a statement in it failed' if rolled_back
end
end
end
@@ -1022,6 +1037,20 @@ def copy_regressions(rows)
end
end
+ def create_regressions_batch(rows)
+ with_client('create_regressions_batch') do |client|
+ unless client.prepared_statements.include?('query_sql_create_regressions_batch')
+ client.prepare('query_sql_create_regressions_batch', CREATE_REGRESSIONS_BATCH_SQL)
+ client.prepared_statements.add('query_sql_create_regressions_batch')
+ end
+ rows.each do |args|
+ query_params = [args.id, args.order, encode_bytea(args.payload), encode_json(args.doc)]
+ client.exec_prepared('query_sql_create_regressions_batch', query_params)
+ end
+ nil
+ end
+ end
+
def list_regressions(order_name: nil)
with_client('list_regressions') do |client|
query_params = [order_name]
diff --git a/test/golden/pg/ruby31_gemfile/query_sql.rbs b/test/golden/pg/ruby31_gemfile/query_sql.rbs
index e5901eb..a747ec7 100644
--- a/test/golden/pg/ruby31_gemfile/query_sql.rbs
+++ b/test/golden/pg/ruby31_gemfile/query_sql.rbs
@@ -396,6 +396,17 @@ module PgCodegen
def self.new: ( id: Integer, order: String, payload: String?, doc: untyped) -> CopyRegressionsArgs
end
+ CREATE_REGRESSIONS_BATCH_SQL: String
+
+ class CreateRegressionsBatchArgs < ::Struct[untyped]
+ attr_accessor id: Integer
+ attr_accessor order: String
+ attr_accessor payload: String?
+ attr_accessor doc: untyped
+ def initialize: ( id: Integer, order: String, payload: String?, doc: untyped) -> void
+ def self.new: ( id: Integer, order: String, payload: String?, doc: untyped) -> CreateRegressionsBatchArgs
+ end
+
LIST_REGRESSIONS_SQL: String
class ListRegressionsRow < ::Struct[untyped]
@@ -578,6 +589,8 @@ module PgCodegen
def copy_regressions: (Array[CopyRegressionsArgs] rows) -> void
+ def create_regressions_batch: (Array[CreateRegressionsBatchArgs] rows) -> void
+
def list_regressions: (?order_name: String?) -> Array[ListRegressionsRow]
def truncate_regressions: -> void
diff --git a/test/golden/pg/ruby32_notypes/my_queries_sql.rb b/test/golden/pg/ruby32_notypes/my_queries_sql.rb
index 28073e2..c162668 100644
--- a/test/golden/pg/ruby32_notypes/my_queries_sql.rb
+++ b/test/golden/pg/ruby32_notypes/my_queries_sql.rb
@@ -52,7 +52,10 @@ def transaction(isolation: nil)
ensure
unless failed
commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ result = client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ # COMMIT of a transaction aborted by an error the block rescued is a ROLLBACK
+ rolled_back = outermost && result.cmd_status == 'ROLLBACK' && Thread.current.status != 'aborting'
+ raise PG::InFailedSqlTransaction, 'transaction rolled back: a statement in it failed' if rolled_back
end
end
end
diff --git a/test/golden/pg/ruby32_notypes/other_sql.rb b/test/golden/pg/ruby32_notypes/other_sql.rb
index a0f1daf..8bd3b15 100644
--- a/test/golden/pg/ruby32_notypes/other_sql.rb
+++ b/test/golden/pg/ruby32_notypes/other_sql.rb
@@ -65,7 +65,10 @@ def transaction(isolation: nil)
ensure
unless failed
commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ result = client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ # COMMIT of a transaction aborted by an error the block rescued is a ROLLBACK
+ rolled_back = outermost && result.cmd_status == 'ROLLBACK' && Thread.current.status != 'aborting'
+ raise PG::InFailedSqlTransaction, 'transaction rolled back: a statement in it failed' if rolled_back
end
end
end
diff --git a/test/golden/pg/ruby32_notypes/query_sql.rb b/test/golden/pg/ruby32_notypes/query_sql.rb
index f4732e1..050e2d4 100644
--- a/test/golden/pg/ruby32_notypes/query_sql.rb
+++ b/test/golden/pg/ruby32_notypes/query_sql.rb
@@ -318,6 +318,13 @@ class CreateRegressionIfNewRow < Data.define(:id)
class CopyRegressionsArgs < Data.define(:id, :order, :payload, :doc)
end
+ CREATE_REGRESSIONS_BATCH_SQL = <<~'SQL'
+ INSERT INTO regressions (id, "order", payload, doc) VALUES ($1, $2, $3, $4)
+ SQL
+
+ class CreateRegressionsBatchArgs < Data.define(:id, :order, :payload, :doc)
+ end
+
LIST_REGRESSIONS_SQL = <<~'SQL'
SELECT id, "order", payload, doc, bio_types, uids FROM regressions WHERE ($1::text IS NULL OR "order" = $1) ORDER BY id
SQL
@@ -406,7 +413,10 @@ def transaction(isolation: nil)
ensure
unless failed
commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ result = client.exec(Thread.current.status == 'aborting' ? rollback : commit)
+ # COMMIT of a transaction aborted by an error the block rescued is a ROLLBACK
+ rolled_back = outermost && result.cmd_status == 'ROLLBACK' && Thread.current.status != 'aborting'
+ raise PG::InFailedSqlTransaction, 'transaction rolled back: a statement in it failed' if rolled_back
end
end
end
@@ -1014,6 +1024,20 @@ def copy_regressions(rows)
end
end
+ def create_regressions_batch(rows)
+ with_client('create_regressions_batch') do |client|
+ unless client.prepared_statements.include?('query_sql_create_regressions_batch')
+ client.prepare('query_sql_create_regressions_batch', CREATE_REGRESSIONS_BATCH_SQL)
+ client.prepared_statements.add('query_sql_create_regressions_batch')
+ end
+ rows.each do |args|
+ query_params = [args.id, args.order, encode_bytea(args.payload), encode_json(args.doc)]
+ client.exec_prepared('query_sql_create_regressions_batch', query_params)
+ end
+ nil
+ end
+ end
+
def list_regressions(order_name: nil)
with_client('list_regressions') do |client|
query_params = [order_name]
diff --git a/test/golden/sqlite3/default/connection.rb b/test/golden/sqlite3/default/connection.rb
new file mode 100644
index 0000000..f29c5b1
--- /dev/null
+++ b/test/golden/sqlite3/default/connection.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+# auto-generated by sqlc - do not edit
+
+module Sqlite3Codegen
+ module ConnectionLock
+ MUTEX = Mutex.new
+
+ def self.for(db)
+ MUTEX.synchronize do
+ db.extend(self) unless db.is_a?(self)
+ db.sqlc_lock
+ end
+ end
+
+ def sqlc_lock = @sqlc_lock ||= Monitor.new
+ end
+
+ TRANSACTION_MODES = %i[deferred immediate exclusive].freeze
+end
diff --git a/test/golden/sqlite3/default/connection.rbs b/test/golden/sqlite3/default/connection.rbs
new file mode 100644
index 0000000..056f8a2
--- /dev/null
+++ b/test/golden/sqlite3/default/connection.rbs
@@ -0,0 +1,12 @@
+# auto-generated by sqlc - do not edit
+module Sqlite3Codegen
+ module ConnectionLock
+ MUTEX: Thread::Mutex
+ @sqlc_lock: Monitor
+
+ def self.for: (untyped db) -> Monitor
+ def sqlc_lock: () -> Monitor
+ end
+
+ TRANSACTION_MODES: Array[Symbol]
+end
diff --git a/test/golden/sqlite3/default/models.rb b/test/golden/sqlite3/default/models.rb
index 3d30bfb..a880702 100644
--- a/test/golden/sqlite3/default/models.rb
+++ b/test/golden/sqlite3/default/models.rb
@@ -14,7 +14,9 @@ class DeclaredType < Data.define(
:c_decimal,
:c_unsigned_big_int,
:c_native,
- :c_double
+ :c_double,
+ :c_json,
+ :c_uuid
)
end
diff --git a/test/golden/sqlite3/default/models.rbs b/test/golden/sqlite3/default/models.rbs
index 10e489a..7b76c71 100644
--- a/test/golden/sqlite3/default/models.rbs
+++ b/test/golden/sqlite3/default/models.rbs
@@ -21,12 +21,16 @@ module Sqlite3Codegen
attr_reader c_unsigned_big_int: Integer?
attr_reader c_native: String?
attr_reader c_double: Float?
+ attr_reader c_json: String?
+ attr_reader c_uuid: String?
def initialize: (
String? c_varchar,
Float? c_decimal,
Integer? c_unsigned_big_int,
String? c_native,
- Float? c_double
+ Float? c_double,
+ String? c_json,
+ String? c_uuid
) -> void
end
diff --git a/test/golden/sqlite3/default/my_queries_sql.rb b/test/golden/sqlite3/default/my_queries_sql.rb
index 93204ef..2e11fbe 100644
--- a/test/golden/sqlite3/default/my_queries_sql.rb
+++ b/test/golden/sqlite3/default/my_queries_sql.rb
@@ -3,6 +3,8 @@
# auto-generated by sqlc - do not edit
require 'connection_pool'
require 'sqlite3'
+require 'monitor'
+require_relative 'connection'
require_relative 'models'
module Sqlite3Codegen
@@ -15,33 +17,39 @@ class My_queriesSql
# Raised by the bang variants (get_author!) when the query returns no row.
class NotFound < StandardError; end
- # db: a SQLite3::Database (a single connection, not shared across threads).
+ # db: a SQLite3::Database (a single connection; threads and fibers using it take turns).
# logger: optional, anything with #debug (Logger, Rails.logger); logs the name and duration of every query.
def initialize(db, logger: nil)
@db = db
@logger = logger
+ @lock = ConnectionLock.for(db)
end
# Runs the block in a transaction and returns its value: COMMIT when the block returns (also through
# break / return / throw), ROLLBACK when it raises or the thread is killed. A nested call becomes a
- # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive.
+ # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive. Other threads / fibers using the same
+ # database wait until it ends.
def transaction(mode: nil)
+ raise ArgumentError, "unknown transaction mode: #{mode.inspect}" unless [nil, *TRANSACTION_MODES].include?(mode)
+
instrument('transaction') do
- outermost = !@db.transaction_active?
- savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
- @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
- rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
- failed = false
- begin
- yield self
- rescue Exception # rubocop:disable Lint/RescueException
- failed = true
- @db.execute(rollback)
- raise
- ensure
- unless failed
- commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ @lock.synchronize do
+ outermost = !@db.transaction_active?
+ savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
+ @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
+ rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
+ failed = false
+ begin
+ yield self
+ rescue Exception # rubocop:disable Lint/RescueException
+ failed = true
+ @db.execute(rollback)
+ raise
+ ensure
+ unless failed
+ commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
+ @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ end
end
end
end
@@ -75,7 +83,7 @@ def instrument(name)
end
def with_client(name, &block)
- instrument(name) { block.call(@db) }
+ instrument(name) { @lock.synchronize { block.call(@db) } }
end
end
end
diff --git a/test/golden/sqlite3/default/other_sql.rb b/test/golden/sqlite3/default/other_sql.rb
index 0e3e160..8089748 100644
--- a/test/golden/sqlite3/default/other_sql.rb
+++ b/test/golden/sqlite3/default/other_sql.rb
@@ -3,6 +3,8 @@
# auto-generated by sqlc - do not edit
require 'connection_pool'
require 'sqlite3'
+require 'monitor'
+require_relative 'connection'
require_relative 'models'
module Sqlite3Codegen
@@ -28,33 +30,39 @@ class OtherSql
# Raised by the bang variants (get_author!) when the query returns no row.
class NotFound < StandardError; end
- # db: a SQLite3::Database (a single connection, not shared across threads).
+ # db: a SQLite3::Database (a single connection; threads and fibers using it take turns).
# logger: optional, anything with #debug (Logger, Rails.logger); logs the name and duration of every query.
def initialize(db, logger: nil)
@db = db
@logger = logger
+ @lock = ConnectionLock.for(db)
end
# Runs the block in a transaction and returns its value: COMMIT when the block returns (also through
# break / return / throw), ROLLBACK when it raises or the thread is killed. A nested call becomes a
- # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive.
+ # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive. Other threads / fibers using the same
+ # database wait until it ends.
def transaction(mode: nil)
+ raise ArgumentError, "unknown transaction mode: #{mode.inspect}" unless [nil, *TRANSACTION_MODES].include?(mode)
+
instrument('transaction') do
- outermost = !@db.transaction_active?
- savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
- @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
- rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
- failed = false
- begin
- yield self
- rescue Exception # rubocop:disable Lint/RescueException
- failed = true
- @db.execute(rollback)
- raise
- ensure
- unless failed
- commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ @lock.synchronize do
+ outermost = !@db.transaction_active?
+ savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
+ @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
+ rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
+ failed = false
+ begin
+ yield self
+ rescue Exception # rubocop:disable Lint/RescueException
+ failed = true
+ @db.execute(rollback)
+ raise
+ ensure
+ unless failed
+ commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
+ @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ end
end
end
end
@@ -125,7 +133,7 @@ def instrument(name)
end
def with_client(name, &block)
- instrument(name) { block.call(@db) }
+ instrument(name) { @lock.synchronize { block.call(@db) } }
end
end
end
diff --git a/test/golden/sqlite3/default/query_sql.rb b/test/golden/sqlite3/default/query_sql.rb
index c8a0caf..d44d1f0 100644
--- a/test/golden/sqlite3/default/query_sql.rb
+++ b/test/golden/sqlite3/default/query_sql.rb
@@ -3,6 +3,8 @@
# auto-generated by sqlc - do not edit
require 'connection_pool'
require 'sqlite3'
+require 'monitor'
+require_relative 'connection'
require_relative 'models'
module Sqlite3Codegen
@@ -159,11 +161,11 @@ class ListAuthorsByNameBatchArgs < Data.define(:name)
end
INSERT_DECLARED_TYPES_SQL = <<~'SQL'
- INSERT INTO declared_types (c_varchar, c_decimal, c_unsigned_big_int, c_native, c_double) VALUES (?, ?, ?, ?, ?)
+ INSERT INTO declared_types (c_varchar, c_decimal, c_unsigned_big_int, c_native, c_double, c_json, c_uuid) VALUES (?, ?, ?, ?, ?, ?, ?)
SQL
GET_DECLARED_TYPES_SQL = <<~'SQL'
- SELECT c_varchar, c_decimal, c_unsigned_big_int, c_native, c_double FROM declared_types LIMIT 1
+ SELECT c_varchar, c_decimal, c_unsigned_big_int, c_native, c_double, c_json, c_uuid FROM declared_types LIMIT 1
SQL
class GetDeclaredTypesRow < Data.define(
@@ -171,7 +173,9 @@ class GetDeclaredTypesRow < Data.define(
:c_decimal,
:c_unsigned_big_int,
:c_native,
- :c_double
+ :c_double,
+ :c_json,
+ :c_uuid
)
end
@@ -248,33 +252,39 @@ class QuerySql
# Raised by the bang variants (get_author!) when the query returns no row.
class NotFound < StandardError; end
- # db: a SQLite3::Database (a single connection, not shared across threads).
+ # db: a SQLite3::Database (a single connection; threads and fibers using it take turns).
# logger: optional, anything with #debug (Logger, Rails.logger); logs the name and duration of every query.
def initialize(db, logger: nil)
@db = db
@logger = logger
+ @lock = ConnectionLock.for(db)
end
# Runs the block in a transaction and returns its value: COMMIT when the block returns (also through
# break / return / throw), ROLLBACK when it raises or the thread is killed. A nested call becomes a
- # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive.
+ # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive. Other threads / fibers using the same
+ # database wait until it ends.
def transaction(mode: nil)
+ raise ArgumentError, "unknown transaction mode: #{mode.inspect}" unless [nil, *TRANSACTION_MODES].include?(mode)
+
instrument('transaction') do
- outermost = !@db.transaction_active?
- savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
- @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
- rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
- failed = false
- begin
- yield self
- rescue Exception # rubocop:disable Lint/RescueException
- failed = true
- @db.execute(rollback)
- raise
- ensure
- unless failed
- commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ @lock.synchronize do
+ outermost = !@db.transaction_active?
+ savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
+ @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
+ rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
+ failed = false
+ begin
+ yield self
+ rescue Exception # rubocop:disable Lint/RescueException
+ failed = true
+ @db.execute(rollback)
+ raise
+ ensure
+ unless failed
+ commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
+ @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ end
end
end
end
@@ -437,9 +447,11 @@ def get_authors_by_ids_and_names(ids:, names:)
# Bulk insert: pass an Array of CopyAuthorsArgs.
def copy_authors(rows)
with_client('copy_authors') do |client|
- 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] })
+ 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
@@ -578,14 +590,16 @@ def list_authors_by_name_batch(rows)
end
end
- def insert_declared_types(c_varchar:, c_decimal:, c_unsigned_big_int:, c_native:, c_double:)
+ def insert_declared_types(c_varchar:, c_decimal:, c_unsigned_big_int:, c_native:, c_double:, c_json:, c_uuid:)
with_client('insert_declared_types') do |client|
query_params = [
c_varchar,
c_decimal,
c_unsigned_big_int,
c_native,
- c_double
+ c_double,
+ c_json,
+ c_uuid
]
stmt = client.prepare(INSERT_DECLARED_TYPES_SQL)
begin
@@ -608,7 +622,9 @@ def get_declared_types
row[1],
row[2],
row[3],
- row[4]
+ row[4],
+ row[5],
+ row[6]
)
ensure
stmt.close
@@ -661,9 +677,11 @@ def update_regression_prices(price:)
def copy_regressions(rows)
with_client('copy_regressions') do |client|
- rows.each_slice(499) do |batch|
- sql = %(INSERT INTO "regressions" ("order", "price") VALUES #{Array.new(batch.size, '(?, ?)').join(', ')})
- client.execute(sql, batch.flat_map { |row| [row.order, row.price] })
+ atomically(client) do
+ rows.each_slice(499) do |batch|
+ sql = %(INSERT INTO "regressions" ("order", "price") VALUES #{Array.new(batch.size, '(?, ?)').join(', ')})
+ client.execute(sql, batch.flat_map { |row| [row.order, row.price] })
+ end
end
nil
end
@@ -788,7 +806,19 @@ def instrument(name)
end
def with_client(name, &block)
- instrument(name) { block.call(@db) }
+ instrument(name) { @lock.synchronize { block.call(@db) } }
+ end
+
+ def atomically(client)
+ client.execute('SAVEPOINT sqlc_copy')
+ done = false
+ begin
+ yield
+ done = true
+ ensure
+ client.execute('ROLLBACK TO SAVEPOINT sqlc_copy') unless done
+ client.execute('RELEASE SAVEPOINT sqlc_copy')
+ end
end
def slice_placeholders(values)
diff --git a/test/golden/sqlite3/default/query_sql.rbs b/test/golden/sqlite3/default/query_sql.rbs
index 07e7dd5..0d6b211 100644
--- a/test/golden/sqlite3/default/query_sql.rbs
+++ b/test/golden/sqlite3/default/query_sql.rbs
@@ -156,12 +156,16 @@ module Sqlite3Codegen
attr_reader c_unsigned_big_int: Integer?
attr_reader c_native: String?
attr_reader c_double: Float?
+ attr_reader c_json: String?
+ attr_reader c_uuid: String?
def initialize: (
String? c_varchar,
Float? c_decimal,
Integer? c_unsigned_big_int,
String? c_native,
- Float? c_double
+ Float? c_double,
+ String? c_json,
+ String? c_uuid
) -> void
end
@@ -296,7 +300,7 @@ module Sqlite3Codegen
def list_authors_by_name_batch: (Array[ListAuthorsByNameBatchArgs] rows) -> Array[Array[ListAuthorsByNameBatchRow]]
- def insert_declared_types: (c_varchar: String?, c_decimal: Float?, c_unsigned_big_int: Integer?, c_native: String?, c_double: Float?) -> void
+ def insert_declared_types: (c_varchar: String?, c_decimal: Float?, c_unsigned_big_int: Integer?, c_native: String?, c_double: Float?, c_json: String?, c_uuid: String?) -> void
def get_declared_types: -> GetDeclaredTypesRow?
@@ -332,6 +336,8 @@ module Sqlite3Codegen
def with_client: [T] (String name) { (untyped) -> T } -> T
+ def atomically: (untyped client) { () -> void } -> void
+
def slice_placeholders: (Array[untyped] values) -> String
end
end
diff --git a/test/golden/sqlite3/ruby31_gemfile/connection.rb b/test/golden/sqlite3/ruby31_gemfile/connection.rb
new file mode 100644
index 0000000..f29c5b1
--- /dev/null
+++ b/test/golden/sqlite3/ruby31_gemfile/connection.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+# auto-generated by sqlc - do not edit
+
+module Sqlite3Codegen
+ module ConnectionLock
+ MUTEX = Mutex.new
+
+ def self.for(db)
+ MUTEX.synchronize do
+ db.extend(self) unless db.is_a?(self)
+ db.sqlc_lock
+ end
+ end
+
+ def sqlc_lock = @sqlc_lock ||= Monitor.new
+ end
+
+ TRANSACTION_MODES = %i[deferred immediate exclusive].freeze
+end
diff --git a/test/golden/sqlite3/ruby31_gemfile/connection.rbs b/test/golden/sqlite3/ruby31_gemfile/connection.rbs
new file mode 100644
index 0000000..056f8a2
--- /dev/null
+++ b/test/golden/sqlite3/ruby31_gemfile/connection.rbs
@@ -0,0 +1,12 @@
+# auto-generated by sqlc - do not edit
+module Sqlite3Codegen
+ module ConnectionLock
+ MUTEX: Thread::Mutex
+ @sqlc_lock: Monitor
+
+ def self.for: (untyped db) -> Monitor
+ def sqlc_lock: () -> Monitor
+ end
+
+ TRANSACTION_MODES: Array[Symbol]
+end
diff --git a/test/golden/sqlite3/ruby31_gemfile/models.rb b/test/golden/sqlite3/ruby31_gemfile/models.rb
index 65fd342..5c3baaf 100644
--- a/test/golden/sqlite3/ruby31_gemfile/models.rb
+++ b/test/golden/sqlite3/ruby31_gemfile/models.rb
@@ -19,6 +19,8 @@ module Sqlite3Codegen
:c_unsigned_big_int,
:c_native,
:c_double,
+ :c_json,
+ :c_uuid,
keyword_init: true
)
diff --git a/test/golden/sqlite3/ruby31_gemfile/models.rbs b/test/golden/sqlite3/ruby31_gemfile/models.rbs
index af850e9..18006c9 100644
--- a/test/golden/sqlite3/ruby31_gemfile/models.rbs
+++ b/test/golden/sqlite3/ruby31_gemfile/models.rbs
@@ -23,19 +23,25 @@ module Sqlite3Codegen
attr_accessor c_unsigned_big_int: Integer?
attr_accessor c_native: String?
attr_accessor c_double: Float?
+ attr_accessor c_json: String?
+ attr_accessor c_uuid: String?
def initialize: (
c_varchar: String?,
c_decimal: Float?,
c_unsigned_big_int: Integer?,
c_native: String?,
- c_double: Float?
+ c_double: Float?,
+ c_json: String?,
+ c_uuid: String?
) -> void
def self.new: (
c_varchar: String?,
c_decimal: Float?,
c_unsigned_big_int: Integer?,
c_native: String?,
- c_double: Float?
+ c_double: Float?,
+ c_json: String?,
+ c_uuid: String?
) -> DeclaredType
end
diff --git a/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rb b/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rb
index 93204ef..2e11fbe 100644
--- a/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rb
+++ b/test/golden/sqlite3/ruby31_gemfile/my_queries_sql.rb
@@ -3,6 +3,8 @@
# auto-generated by sqlc - do not edit
require 'connection_pool'
require 'sqlite3'
+require 'monitor'
+require_relative 'connection'
require_relative 'models'
module Sqlite3Codegen
@@ -15,33 +17,39 @@ class My_queriesSql
# Raised by the bang variants (get_author!) when the query returns no row.
class NotFound < StandardError; end
- # db: a SQLite3::Database (a single connection, not shared across threads).
+ # db: a SQLite3::Database (a single connection; threads and fibers using it take turns).
# logger: optional, anything with #debug (Logger, Rails.logger); logs the name and duration of every query.
def initialize(db, logger: nil)
@db = db
@logger = logger
+ @lock = ConnectionLock.for(db)
end
# Runs the block in a transaction and returns its value: COMMIT when the block returns (also through
# break / return / throw), ROLLBACK when it raises or the thread is killed. A nested call becomes a
- # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive.
+ # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive. Other threads / fibers using the same
+ # database wait until it ends.
def transaction(mode: nil)
+ raise ArgumentError, "unknown transaction mode: #{mode.inspect}" unless [nil, *TRANSACTION_MODES].include?(mode)
+
instrument('transaction') do
- outermost = !@db.transaction_active?
- savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
- @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
- rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
- failed = false
- begin
- yield self
- rescue Exception # rubocop:disable Lint/RescueException
- failed = true
- @db.execute(rollback)
- raise
- ensure
- unless failed
- commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ @lock.synchronize do
+ outermost = !@db.transaction_active?
+ savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
+ @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
+ rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
+ failed = false
+ begin
+ yield self
+ rescue Exception # rubocop:disable Lint/RescueException
+ failed = true
+ @db.execute(rollback)
+ raise
+ ensure
+ unless failed
+ commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
+ @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ end
end
end
end
@@ -75,7 +83,7 @@ def instrument(name)
end
def with_client(name, &block)
- instrument(name) { block.call(@db) }
+ instrument(name) { @lock.synchronize { block.call(@db) } }
end
end
end
diff --git a/test/golden/sqlite3/ruby31_gemfile/other_sql.rb b/test/golden/sqlite3/ruby31_gemfile/other_sql.rb
index 0bd78f0..a8cb909 100644
--- a/test/golden/sqlite3/ruby31_gemfile/other_sql.rb
+++ b/test/golden/sqlite3/ruby31_gemfile/other_sql.rb
@@ -3,6 +3,8 @@
# auto-generated by sqlc - do not edit
require 'connection_pool'
require 'sqlite3'
+require 'monitor'
+require_relative 'connection'
require_relative 'models'
module Sqlite3Codegen
@@ -27,33 +29,39 @@ class OtherSql
# Raised by the bang variants (get_author!) when the query returns no row.
class NotFound < StandardError; end
- # db: a SQLite3::Database (a single connection, not shared across threads).
+ # db: a SQLite3::Database (a single connection; threads and fibers using it take turns).
# logger: optional, anything with #debug (Logger, Rails.logger); logs the name and duration of every query.
def initialize(db, logger: nil)
@db = db
@logger = logger
+ @lock = ConnectionLock.for(db)
end
# Runs the block in a transaction and returns its value: COMMIT when the block returns (also through
# break / return / throw), ROLLBACK when it raises or the thread is killed. A nested call becomes a
- # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive.
+ # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive. Other threads / fibers using the same
+ # database wait until it ends.
def transaction(mode: nil)
+ raise ArgumentError, "unknown transaction mode: #{mode.inspect}" unless [nil, *TRANSACTION_MODES].include?(mode)
+
instrument('transaction') do
- outermost = !@db.transaction_active?
- savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
- @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
- rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
- failed = false
- begin
- yield self
- rescue Exception # rubocop:disable Lint/RescueException
- failed = true
- @db.execute(rollback)
- raise
- ensure
- unless failed
- commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ @lock.synchronize do
+ outermost = !@db.transaction_active?
+ savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
+ @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
+ rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
+ failed = false
+ begin
+ yield self
+ rescue Exception # rubocop:disable Lint/RescueException
+ failed = true
+ @db.execute(rollback)
+ raise
+ ensure
+ unless failed
+ commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
+ @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ end
end
end
end
@@ -124,7 +132,7 @@ def instrument(name)
end
def with_client(name, &block)
- instrument(name) { block.call(@db) }
+ instrument(name) { @lock.synchronize { block.call(@db) } }
end
end
end
diff --git a/test/golden/sqlite3/ruby31_gemfile/query_sql.rb b/test/golden/sqlite3/ruby31_gemfile/query_sql.rb
index 4738c99..a0ac386 100644
--- a/test/golden/sqlite3/ruby31_gemfile/query_sql.rb
+++ b/test/golden/sqlite3/ruby31_gemfile/query_sql.rb
@@ -3,6 +3,8 @@
# auto-generated by sqlc - do not edit
require 'connection_pool'
require 'sqlite3'
+require 'monitor'
+require_relative 'connection'
require_relative 'models'
module Sqlite3Codegen
@@ -148,11 +150,11 @@ module Sqlite3Codegen
ListAuthorsByNameBatchArgs = Struct.new(:name, keyword_init: true)
INSERT_DECLARED_TYPES_SQL = <<~'SQL'
- INSERT INTO declared_types (c_varchar, c_decimal, c_unsigned_big_int, c_native, c_double) VALUES (?, ?, ?, ?, ?)
+ INSERT INTO declared_types (c_varchar, c_decimal, c_unsigned_big_int, c_native, c_double, c_json, c_uuid) VALUES (?, ?, ?, ?, ?, ?, ?)
SQL
GET_DECLARED_TYPES_SQL = <<~'SQL'
- SELECT c_varchar, c_decimal, c_unsigned_big_int, c_native, c_double FROM declared_types LIMIT 1
+ SELECT c_varchar, c_decimal, c_unsigned_big_int, c_native, c_double, c_json, c_uuid FROM declared_types LIMIT 1
SQL
GetDeclaredTypesRow = Struct.new(
@@ -161,6 +163,8 @@ module Sqlite3Codegen
:c_unsigned_big_int,
:c_native,
:c_double,
+ :c_json,
+ :c_uuid,
keyword_init: true
)
@@ -236,33 +240,39 @@ class QuerySql
# Raised by the bang variants (get_author!) when the query returns no row.
class NotFound < StandardError; end
- # db: a SQLite3::Database (a single connection, not shared across threads).
+ # db: a SQLite3::Database (a single connection; threads and fibers using it take turns).
# logger: optional, anything with #debug (Logger, Rails.logger); logs the name and duration of every query.
def initialize(db, logger: nil)
@db = db
@logger = logger
+ @lock = ConnectionLock.for(db)
end
# Runs the block in a transaction and returns its value: COMMIT when the block returns (also through
# break / return / throw), ROLLBACK when it raises or the thread is killed. A nested call becomes a
- # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive.
+ # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive. Other threads / fibers using the same
+ # database wait until it ends.
def transaction(mode: nil)
+ raise ArgumentError, "unknown transaction mode: #{mode.inspect}" unless [nil, *TRANSACTION_MODES].include?(mode)
+
instrument('transaction') do
- outermost = !@db.transaction_active?
- savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
- @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
- rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
- failed = false
- begin
- yield self
- rescue Exception # rubocop:disable Lint/RescueException
- failed = true
- @db.execute(rollback)
- raise
- ensure
- unless failed
- commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ @lock.synchronize do
+ outermost = !@db.transaction_active?
+ savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
+ @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
+ rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
+ failed = false
+ begin
+ yield self
+ rescue Exception # rubocop:disable Lint/RescueException
+ failed = true
+ @db.execute(rollback)
+ raise
+ ensure
+ unless failed
+ commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
+ @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ end
end
end
end
@@ -424,9 +434,11 @@ def get_authors_by_ids_and_names(ids:, names:)
# Bulk insert: pass an Array of CopyAuthorsArgs.
def copy_authors(rows)
with_client('copy_authors') do |client|
- 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] })
+ 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
@@ -573,14 +585,16 @@ def list_authors_by_name_batch(rows)
end
end
- def insert_declared_types(c_varchar:, c_decimal:, c_unsigned_big_int:, c_native:, c_double:)
+ def insert_declared_types(c_varchar:, c_decimal:, c_unsigned_big_int:, c_native:, c_double:, c_json:, c_uuid:)
with_client('insert_declared_types') do |client|
query_params = [
c_varchar,
c_decimal,
c_unsigned_big_int,
c_native,
- c_double
+ c_double,
+ c_json,
+ c_uuid
]
stmt = client.prepare(INSERT_DECLARED_TYPES_SQL)
begin
@@ -603,7 +617,9 @@ def get_declared_types
c_decimal: row[1],
c_unsigned_big_int: row[2],
c_native: row[3],
- c_double: row[4]
+ c_double: row[4],
+ c_json: row[5],
+ c_uuid: row[6]
)
ensure
stmt.close
@@ -656,9 +672,11 @@ def update_regression_prices(price:)
def copy_regressions(rows)
with_client('copy_regressions') do |client|
- rows.each_slice(499) do |batch|
- sql = %(INSERT INTO "regressions" ("order", "price") VALUES #{Array.new(batch.size, '(?, ?)').join(', ')})
- client.execute(sql, batch.flat_map { |row| [row.order, row.price] })
+ atomically(client) do
+ rows.each_slice(499) do |batch|
+ sql = %(INSERT INTO "regressions" ("order", "price") VALUES #{Array.new(batch.size, '(?, ?)').join(', ')})
+ client.execute(sql, batch.flat_map { |row| [row.order, row.price] })
+ end
end
nil
end
@@ -783,7 +801,19 @@ def instrument(name)
end
def with_client(name, &block)
- instrument(name) { block.call(@db) }
+ instrument(name) { @lock.synchronize { block.call(@db) } }
+ end
+
+ def atomically(client)
+ client.execute('SAVEPOINT sqlc_copy')
+ done = false
+ begin
+ yield
+ done = true
+ ensure
+ client.execute('ROLLBACK TO SAVEPOINT sqlc_copy') unless done
+ client.execute('RELEASE SAVEPOINT sqlc_copy')
+ end
end
def slice_placeholders(values)
diff --git a/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs b/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs
index 94f5746..4f449d8 100644
--- a/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs
+++ b/test/golden/sqlite3/ruby31_gemfile/query_sql.rbs
@@ -173,19 +173,25 @@ module Sqlite3Codegen
attr_accessor c_unsigned_big_int: Integer?
attr_accessor c_native: String?
attr_accessor c_double: Float?
+ attr_accessor c_json: String?
+ attr_accessor c_uuid: String?
def initialize: (
c_varchar: String?,
c_decimal: Float?,
c_unsigned_big_int: Integer?,
c_native: String?,
- c_double: Float?
+ c_double: Float?,
+ c_json: String?,
+ c_uuid: String?
) -> void
def self.new: (
c_varchar: String?,
c_decimal: Float?,
c_unsigned_big_int: Integer?,
c_native: String?,
- c_double: Float?
+ c_double: Float?,
+ c_json: String?,
+ c_uuid: String?
) -> GetDeclaredTypesRow
end
@@ -335,7 +341,7 @@ module Sqlite3Codegen
def list_authors_by_name_batch: (Array[ListAuthorsByNameBatchArgs] rows) -> Array[Array[ListAuthorsByNameBatchRow]]
- def insert_declared_types: (c_varchar: String?, c_decimal: Float?, c_unsigned_big_int: Integer?, c_native: String?, c_double: Float?) -> void
+ def insert_declared_types: (c_varchar: String?, c_decimal: Float?, c_unsigned_big_int: Integer?, c_native: String?, c_double: Float?, c_json: String?, c_uuid: String?) -> void
def get_declared_types: -> GetDeclaredTypesRow?
@@ -371,6 +377,8 @@ module Sqlite3Codegen
def with_client: [T] (String name) { (untyped) -> T } -> T
+ def atomically: (untyped client) { () -> void } -> void
+
def slice_placeholders: (Array[untyped] values) -> String
end
end
diff --git a/test/golden/sqlite3/ruby32_notypes/connection.rb b/test/golden/sqlite3/ruby32_notypes/connection.rb
new file mode 100644
index 0000000..f29c5b1
--- /dev/null
+++ b/test/golden/sqlite3/ruby32_notypes/connection.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+# auto-generated by sqlc - do not edit
+
+module Sqlite3Codegen
+ module ConnectionLock
+ MUTEX = Mutex.new
+
+ def self.for(db)
+ MUTEX.synchronize do
+ db.extend(self) unless db.is_a?(self)
+ db.sqlc_lock
+ end
+ end
+
+ def sqlc_lock = @sqlc_lock ||= Monitor.new
+ end
+
+ TRANSACTION_MODES = %i[deferred immediate exclusive].freeze
+end
diff --git a/test/golden/sqlite3/ruby32_notypes/models.rb b/test/golden/sqlite3/ruby32_notypes/models.rb
index 3d30bfb..a880702 100644
--- a/test/golden/sqlite3/ruby32_notypes/models.rb
+++ b/test/golden/sqlite3/ruby32_notypes/models.rb
@@ -14,7 +14,9 @@ class DeclaredType < Data.define(
:c_decimal,
:c_unsigned_big_int,
:c_native,
- :c_double
+ :c_double,
+ :c_json,
+ :c_uuid
)
end
diff --git a/test/golden/sqlite3/ruby32_notypes/my_queries_sql.rb b/test/golden/sqlite3/ruby32_notypes/my_queries_sql.rb
index 93204ef..2e11fbe 100644
--- a/test/golden/sqlite3/ruby32_notypes/my_queries_sql.rb
+++ b/test/golden/sqlite3/ruby32_notypes/my_queries_sql.rb
@@ -3,6 +3,8 @@
# auto-generated by sqlc - do not edit
require 'connection_pool'
require 'sqlite3'
+require 'monitor'
+require_relative 'connection'
require_relative 'models'
module Sqlite3Codegen
@@ -15,33 +17,39 @@ class My_queriesSql
# Raised by the bang variants (get_author!) when the query returns no row.
class NotFound < StandardError; end
- # db: a SQLite3::Database (a single connection, not shared across threads).
+ # db: a SQLite3::Database (a single connection; threads and fibers using it take turns).
# logger: optional, anything with #debug (Logger, Rails.logger); logs the name and duration of every query.
def initialize(db, logger: nil)
@db = db
@logger = logger
+ @lock = ConnectionLock.for(db)
end
# Runs the block in a transaction and returns its value: COMMIT when the block returns (also through
# break / return / throw), ROLLBACK when it raises or the thread is killed. A nested call becomes a
- # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive.
+ # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive. Other threads / fibers using the same
+ # database wait until it ends.
def transaction(mode: nil)
+ raise ArgumentError, "unknown transaction mode: #{mode.inspect}" unless [nil, *TRANSACTION_MODES].include?(mode)
+
instrument('transaction') do
- outermost = !@db.transaction_active?
- savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
- @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
- rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
- failed = false
- begin
- yield self
- rescue Exception # rubocop:disable Lint/RescueException
- failed = true
- @db.execute(rollback)
- raise
- ensure
- unless failed
- commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ @lock.synchronize do
+ outermost = !@db.transaction_active?
+ savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
+ @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
+ rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
+ failed = false
+ begin
+ yield self
+ rescue Exception # rubocop:disable Lint/RescueException
+ failed = true
+ @db.execute(rollback)
+ raise
+ ensure
+ unless failed
+ commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
+ @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ end
end
end
end
@@ -75,7 +83,7 @@ def instrument(name)
end
def with_client(name, &block)
- instrument(name) { block.call(@db) }
+ instrument(name) { @lock.synchronize { block.call(@db) } }
end
end
end
diff --git a/test/golden/sqlite3/ruby32_notypes/other_sql.rb b/test/golden/sqlite3/ruby32_notypes/other_sql.rb
index 0e3e160..8089748 100644
--- a/test/golden/sqlite3/ruby32_notypes/other_sql.rb
+++ b/test/golden/sqlite3/ruby32_notypes/other_sql.rb
@@ -3,6 +3,8 @@
# auto-generated by sqlc - do not edit
require 'connection_pool'
require 'sqlite3'
+require 'monitor'
+require_relative 'connection'
require_relative 'models'
module Sqlite3Codegen
@@ -28,33 +30,39 @@ class OtherSql
# Raised by the bang variants (get_author!) when the query returns no row.
class NotFound < StandardError; end
- # db: a SQLite3::Database (a single connection, not shared across threads).
+ # db: a SQLite3::Database (a single connection; threads and fibers using it take turns).
# logger: optional, anything with #debug (Logger, Rails.logger); logs the name and duration of every query.
def initialize(db, logger: nil)
@db = db
@logger = logger
+ @lock = ConnectionLock.for(db)
end
# Runs the block in a transaction and returns its value: COMMIT when the block returns (also through
# break / return / throw), ROLLBACK when it raises or the thread is killed. A nested call becomes a
- # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive.
+ # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive. Other threads / fibers using the same
+ # database wait until it ends.
def transaction(mode: nil)
+ raise ArgumentError, "unknown transaction mode: #{mode.inspect}" unless [nil, *TRANSACTION_MODES].include?(mode)
+
instrument('transaction') do
- outermost = !@db.transaction_active?
- savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
- @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
- rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
- failed = false
- begin
- yield self
- rescue Exception # rubocop:disable Lint/RescueException
- failed = true
- @db.execute(rollback)
- raise
- ensure
- unless failed
- commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ @lock.synchronize do
+ outermost = !@db.transaction_active?
+ savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
+ @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
+ rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
+ failed = false
+ begin
+ yield self
+ rescue Exception # rubocop:disable Lint/RescueException
+ failed = true
+ @db.execute(rollback)
+ raise
+ ensure
+ unless failed
+ commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
+ @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ end
end
end
end
@@ -125,7 +133,7 @@ def instrument(name)
end
def with_client(name, &block)
- instrument(name) { block.call(@db) }
+ instrument(name) { @lock.synchronize { block.call(@db) } }
end
end
end
diff --git a/test/golden/sqlite3/ruby32_notypes/query_sql.rb b/test/golden/sqlite3/ruby32_notypes/query_sql.rb
index c8a0caf..d44d1f0 100644
--- a/test/golden/sqlite3/ruby32_notypes/query_sql.rb
+++ b/test/golden/sqlite3/ruby32_notypes/query_sql.rb
@@ -3,6 +3,8 @@
# auto-generated by sqlc - do not edit
require 'connection_pool'
require 'sqlite3'
+require 'monitor'
+require_relative 'connection'
require_relative 'models'
module Sqlite3Codegen
@@ -159,11 +161,11 @@ class ListAuthorsByNameBatchArgs < Data.define(:name)
end
INSERT_DECLARED_TYPES_SQL = <<~'SQL'
- INSERT INTO declared_types (c_varchar, c_decimal, c_unsigned_big_int, c_native, c_double) VALUES (?, ?, ?, ?, ?)
+ INSERT INTO declared_types (c_varchar, c_decimal, c_unsigned_big_int, c_native, c_double, c_json, c_uuid) VALUES (?, ?, ?, ?, ?, ?, ?)
SQL
GET_DECLARED_TYPES_SQL = <<~'SQL'
- SELECT c_varchar, c_decimal, c_unsigned_big_int, c_native, c_double FROM declared_types LIMIT 1
+ SELECT c_varchar, c_decimal, c_unsigned_big_int, c_native, c_double, c_json, c_uuid FROM declared_types LIMIT 1
SQL
class GetDeclaredTypesRow < Data.define(
@@ -171,7 +173,9 @@ class GetDeclaredTypesRow < Data.define(
:c_decimal,
:c_unsigned_big_int,
:c_native,
- :c_double
+ :c_double,
+ :c_json,
+ :c_uuid
)
end
@@ -248,33 +252,39 @@ class QuerySql
# Raised by the bang variants (get_author!) when the query returns no row.
class NotFound < StandardError; end
- # db: a SQLite3::Database (a single connection, not shared across threads).
+ # db: a SQLite3::Database (a single connection; threads and fibers using it take turns).
# logger: optional, anything with #debug (Logger, Rails.logger); logs the name and duration of every query.
def initialize(db, logger: nil)
@db = db
@logger = logger
+ @lock = ConnectionLock.for(db)
end
# Runs the block in a transaction and returns its value: COMMIT when the block returns (also through
# break / return / throw), ROLLBACK when it raises or the thread is killed. A nested call becomes a
- # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive.
+ # SAVEPOINT. mode: :deferred (default), :immediate or :exclusive. Other threads / fibers using the same
+ # database wait until it ends.
def transaction(mode: nil)
+ raise ArgumentError, "unknown transaction mode: #{mode.inspect}" unless [nil, *TRANSACTION_MODES].include?(mode)
+
instrument('transaction') do
- outermost = !@db.transaction_active?
- savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
- @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
- rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
- failed = false
- begin
- yield self
- rescue Exception # rubocop:disable Lint/RescueException
- failed = true
- @db.execute(rollback)
- raise
- ensure
- unless failed
- commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
- @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ @lock.synchronize do
+ outermost = !@db.transaction_active?
+ savepoint = outermost ? nil : "sqlc_sp_#{rand(0xffff_ffff).to_s(16)}"
+ @db.execute(outermost ? "BEGIN#{" #{mode.to_s.upcase}" if mode}" : "SAVEPOINT #{savepoint}")
+ rollback = outermost ? 'ROLLBACK' : "ROLLBACK TO SAVEPOINT #{savepoint}"
+ failed = false
+ begin
+ yield self
+ rescue Exception # rubocop:disable Lint/RescueException
+ failed = true
+ @db.execute(rollback)
+ raise
+ ensure
+ unless failed
+ commit = outermost ? 'COMMIT' : "RELEASE SAVEPOINT #{savepoint}"
+ @db.execute(Thread.current.status == 'aborting' ? rollback : commit)
+ end
end
end
end
@@ -437,9 +447,11 @@ def get_authors_by_ids_and_names(ids:, names:)
# Bulk insert: pass an Array of CopyAuthorsArgs.
def copy_authors(rows)
with_client('copy_authors') do |client|
- 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] })
+ 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
@@ -578,14 +590,16 @@ def list_authors_by_name_batch(rows)
end
end
- def insert_declared_types(c_varchar:, c_decimal:, c_unsigned_big_int:, c_native:, c_double:)
+ def insert_declared_types(c_varchar:, c_decimal:, c_unsigned_big_int:, c_native:, c_double:, c_json:, c_uuid:)
with_client('insert_declared_types') do |client|
query_params = [
c_varchar,
c_decimal,
c_unsigned_big_int,
c_native,
- c_double
+ c_double,
+ c_json,
+ c_uuid
]
stmt = client.prepare(INSERT_DECLARED_TYPES_SQL)
begin
@@ -608,7 +622,9 @@ def get_declared_types
row[1],
row[2],
row[3],
- row[4]
+ row[4],
+ row[5],
+ row[6]
)
ensure
stmt.close
@@ -661,9 +677,11 @@ def update_regression_prices(price:)
def copy_regressions(rows)
with_client('copy_regressions') do |client|
- rows.each_slice(499) do |batch|
- sql = %(INSERT INTO "regressions" ("order", "price") VALUES #{Array.new(batch.size, '(?, ?)').join(', ')})
- client.execute(sql, batch.flat_map { |row| [row.order, row.price] })
+ atomically(client) do
+ rows.each_slice(499) do |batch|
+ sql = %(INSERT INTO "regressions" ("order", "price") VALUES #{Array.new(batch.size, '(?, ?)').join(', ')})
+ client.execute(sql, batch.flat_map { |row| [row.order, row.price] })
+ end
end
nil
end
@@ -788,7 +806,19 @@ def instrument(name)
end
def with_client(name, &block)
- instrument(name) { block.call(@db) }
+ instrument(name) { @lock.synchronize { block.call(@db) } }
+ end
+
+ def atomically(client)
+ client.execute('SAVEPOINT sqlc_copy')
+ done = false
+ begin
+ yield
+ done = true
+ ensure
+ client.execute('ROLLBACK TO SAVEPOINT sqlc_copy') unless done
+ client.execute('RELEASE SAVEPOINT sqlc_copy')
+ end
end
def slice_placeholders(values)
diff --git a/test/golden_test.rb b/test/golden_test.rb
index a95b2ea..f670e65 100644
--- a/test/golden_test.rb
+++ b/test/golden_test.rb
@@ -12,7 +12,7 @@ class GoldenTest < Minitest::Test
FILES = {
'pg' => COMMON + %w[connection.rb connection.rbs],
'mysql2' => COMMON + %w[connection.rb connection.rbs],
- 'sqlite3' => COMMON
+ 'sqlite3' => COMMON + %w[connection.rb connection.rbs]
}.freeze
FILES.each do |driver, expected_files|
diff --git a/test/options_test.rb b/test/options_test.rb
index 4f0fccb..656e360 100644
--- a/test/options_test.rb
+++ b/test/options_test.rb
@@ -23,12 +23,17 @@ def test_driver_is_case_insensitive_and_validated
assert_raises(ArgumentError) { opts({}) }
end
+ def test_unknown_override_keys_are_rejected
+ err = assert_raises(ArgumentError) { opts(driver: 'pg', overrides: [{ colum: 'authors.bio', ruby_type: { type: 'X' } }]) }
+ assert_equal 'unknown override keys: colum (did you mean column?); supported: column, db_type, ruby_type', err.message
+ end
+
def test_version_bounds
%w[3.1 3.2 3.3 3.4 3].each { |v| RubyVersion.parse(v) }
err = assert_raises(ArgumentError) { RubyVersion.parse('3.5') }
- assert_equal 'Provided version 3.5 exceeds max version 3.4', err.message
+ assert_equal 'Provided version 3.5 is above the maximum supported version 3.4', err.message
err = assert_raises(ArgumentError) { RubyVersion.parse('3.0') }
- assert_equal 'Provided version 3.0 exceeds min version 3.1', err.message
+ assert_equal 'Provided version 3.0 is below the minimum supported version 3.1', err.message
assert_raises(ArgumentError) { RubyVersion.parse('2.7') }
assert_raises(ArgumentError) { RubyVersion.parse('three') }
assert_raises(ArgumentError) { RubyVersion.parse('3.03') }
diff --git a/tests/end2end_mysql2.rb b/tests/end2end_mysql2.rb
index 93fa717..62fd135 100755
--- a/tests/end2end_mysql2.rb
+++ b/tests/end2end_mysql2.rb
@@ -386,6 +386,31 @@ def test_copyfrom_keeps_strings_that_look_like_null_and_special_characters
assert_equal notes, rows.map(&:note)
end
+ def test_copyfrom_loads_binary_values
+ binary = "\x00\xFF\x01\"\\,\n".b
+ @query_sql.copy_regression_rows([CopyRegressionRowsArgs.new(id: 1, order: 'é', note: nil, payload: binary),
+ CopyRegressionRowsArgs.new(id: 2, order: 'b', note: 'n', payload: nil)])
+ assert_equal [[1, binary], [2, nil]], @query_sql.list_regression_payloads.map { |r| [r.id, r.payload] }
+ assert_equal 'é', @query_sql.list_regressions_by_ids(id: 1, id_2: 0, id_3: 0).first.order
+ end
+
+ def test_copyfrom_fails_on_duplicate_keys_and_invalid_values_and_loads_nothing
+ @query_sql.copy_regression_rows([CopyRegressionRowsArgs.new(id: 1, order: 'first', note: nil, payload: nil)])
+ duplicate = [CopyRegressionRowsArgs.new(id: 2, order: 'new', note: nil, payload: nil),
+ CopyRegressionRowsArgs.new(id: 1, order: 'again', note: nil, payload: nil)]
+ assert_raises(Mysql2::Error) { @query_sql.copy_regression_rows(duplicate) }
+ assert_raises(Mysql2::Error) { @query_sql.copy_regression_rows([CopyRegressionRowsArgs.new(id: 'x', order: 'bad', note: nil, payload: nil)]) }
+ assert_equal [[1, 'first']], @query_sql.list_regressions_by_ids(id: 1, id_2: 2, id_3: 3).map { |r| [r.id, r.order] }
+ end
+
+ def test_failed_copyfrom_in_a_transaction_rolls_back_only_itself
+ @query_sql.transaction do |tx|
+ tx.copy_regression_rows([CopyRegressionRowsArgs.new(id: 1, order: 'kept', note: nil, payload: nil)])
+ assert_raises(Mysql2::Error) { tx.copy_regression_rows([CopyRegressionRowsArgs.new(id: 1, order: 'dup', note: nil, payload: nil)]) }
+ end
+ assert_equal ['kept'], @query_sql.list_regressions_by_ids(id: 1, id_2: 2, id_3: 3).map(&:order)
+ end
+
def test_transaction_left_with_break_or_throw_commits_and_frees_the_connection
pooled = QuerySql.connect(mysql2_params, size: 1)
2.times do |i|
diff --git a/tests/end2end_pg.rb b/tests/end2end_pg.rb
index 6c25c59..0bec6df 100755
--- a/tests/end2end_pg.rb
+++ b/tests/end2end_pg.rb
@@ -514,6 +514,29 @@ def test_prepared_statements_survive_a_connection_reset
client&.close
end
+ 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] }
+ end
+
+ def test_transaction_whose_failed_statement_was_rescued_raises_instead_of_committing
+ @query_sql.create_regression(id: 1, order: 'taken', payload: nil, doc: nil, bio_types: nil, uids: nil)
+ assert_raises(PG::InFailedSqlTransaction) do
+ @query_sql.transaction do |tx|
+ tx.create_author_return_id(name: 'lost', bio: nil)
+ begin
+ tx.create_regression(id: 1, order: 'duplicate', payload: nil, doc: nil, bio_types: nil, uids: nil)
+ rescue PG::UniqueViolation
+ nil
+ end
+ end
+ end
+ assert_empty @query_sql.list_authors
+ assert_equal ['taken'], @query_sql.list_regressions.map(&:order)
+ end
+
def test_transaction_left_with_break_or_throw_commits_and_frees_the_connection
pooled = QuerySql.connect(pg_params, size: 1)
2.times do |i|
diff --git a/tests/end2end_sqlite3.rb b/tests/end2end_sqlite3.rb
index cfc7484..7c33379 100644
--- a/tests/end2end_sqlite3.rb
+++ b/tests/end2end_sqlite3.rb
@@ -72,9 +72,11 @@ def test_batch_commands_run_once_per_args
end
def test_declared_types_with_parameters
- @query_sql.insert_declared_types(c_varchar: 'v', c_decimal: 1.5, c_unsigned_big_int: 7, c_native: 'n', c_double: 2.5)
+ 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], row.to_h.values
+ 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')
end
@@ -284,6 +286,42 @@ def test_copyfrom_quotes_reserved_column_names_and_untyped_expressions
assert_in_delta 3.5, @query_sql.get_max_regression_price!.max_price
end
+ def test_copyfrom_is_all_or_nothing
+ rows = (1..3000).map { |i| CopyRegressionsArgs.new(order: i == 2900 ? nil : "o#{i}", price: i) }
+ assert_raises(SQLite3::ConstraintException) { @query_sql.copy_regressions(rows) }
+ assert_empty @query_sql.list_regressions
+ end
+
+ def test_failed_copyfrom_in_a_transaction_rolls_back_only_itself
+ @query_sql.transaction do |tx|
+ tx.create_author_return_id(name: 'kept', bio: nil)
+ bad = [CopyRegressionsArgs.new(order: 'a', price: 1), CopyRegressionsArgs.new(order: nil, price: 2)]
+ assert_raises(SQLite3::ConstraintException) { tx.copy_regressions(bad) }
+ end
+ assert_equal ['kept'], @query_sql.list_authors.map(&:name)
+ assert_empty @query_sql.list_regressions
+ end
+
+ def test_threads_sharing_a_database_take_turns_in_transactions
+ other = QuerySql.new(@db)
+ threads = Array.new(8) do |i|
+ Thread.new do
+ 20.times do |j|
+ (i.even? ? @query_sql : other).transaction do |tx|
+ tx.transaction { tx.create_author_return_id(name: "t#{i}-#{j}", bio: nil) }
+ end
+ end
+ end
+ end
+ threads.each(&:join)
+ assert_equal 160, @query_sql.list_authors.size
+ end
+
+ def test_unknown_transaction_mode_is_rejected_before_beginning
+ assert_raises(ArgumentError) { @query_sql.transaction(mode: :bogus) { flunk } }
+ refute_predicate @db, :transaction_active?
+ end
+
def test_transaction_left_with_break_or_throw_commits
2.times do |i|
@query_sql.transaction do |tx|