Repository navigation
fix(sql-orm-client, sql-builder): refuse an INSERT past the target's bind-parameter limit with an ORM error #31163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Inf1mum
wants to merge
5
commits into
prisma:main
Choose a base branch
from
Inf1mum:fix/30873-createall-bind-limit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+481
−9
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a592a95
fix(sql-orm-client, sql-builder): refuse an INSERT past the target's …
Inf1mum 4fd6339
fix(sql-orm-client): re-check the bind-parameter limit after payload …
Inf1mum db7d5ad
fix(sql-orm-client, sql-builder, relational-core): count bind paramet…
Inf1mum 335aaaa
docs: clarify partial-write semantics for nested bind-parameter refusals
Inf1mum 79f86ee
Merge branch 'main' into fix/30873-createall-bind-limit
Inf1mum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/2-sql-family/4-lanes/relational-core/src/bind-parameter-limit.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import type { AnyQueryAst } from './ast/types'; | ||
| import { collectOrderedParamRefs } from './ast/util'; | ||
|
|
||
| const targetNames: Record<string, string> = { mysql: 'MySQL', postgres: 'PostgreSQL' }; | ||
|
|
||
| /** The name a message about a target's own limit uses, e.g. `PostgreSQL` for `postgres`. */ | ||
| export function targetDisplayNameOf(target: string): string { | ||
| return targetNames[target] ?? target; | ||
| } | ||
|
|
||
| /** | ||
| * The bind parameters one statement may carry on a target whose wire protocol or server fixes that number: PostgreSQL numbers a Bind message's parameters in a 16-bit field and MySQL allows 65,535 placeholders per statement, and both reject a statement past the limit with an error that reads as a broken connection. SQLite's limit is a compile-time option of the bundled engine, so no number is declared for it here. | ||
| */ | ||
| export function bindParameterLimitOfTarget(target: string): number | undefined { | ||
| return target === 'postgres' || target === 'mysql' ? 65_535 : undefined; | ||
| } | ||
|
|
||
| /** | ||
| * The bind parameters `ast` occupies in one statement on `target`: the number its limit counts. PostgreSQL numbers a parameter reference once and writes that `$N` wherever the statement repeats it, so a reference that several rows share is bound once. MySQL takes only positional parameters, and its driver writes a repeated reference as one `?` per occurrence with the value repeated, so there every occurrence is bound. | ||
| */ | ||
| export function bindParameterCountOf(ast: AnyQueryAst, target: string): number { | ||
| return (target === 'mysql' ? ast.collectParamRefs() : collectOrderedParamRefs(ast)).length; | ||
| } |
1 change: 1 addition & 0 deletions
1
packages/2-sql-family/4-lanes/relational-core/src/exports/plan.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export * from '../bind-parameter-limit'; | ||
| export * from '../plan'; | ||
| export type { SqlExecutionPlan } from '../sql-execution-plan'; |
28 changes: 28 additions & 0 deletions
28
packages/2-sql-family/4-lanes/relational-core/test/bind-parameter-limit.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { bindParameterCountOf, bindParameterLimitOfTarget } from '../src/bind-parameter-limit'; | ||
| import { InsertAst, ParamRef, TableSource } from '../src/exports/ast'; | ||
|
|
||
| describe('bindParameterLimitOfTarget', () => { | ||
| it('declares the same limit for PostgreSQL and MySQL, and none for a target whose limit is not fixed', () => { | ||
| expect(bindParameterLimitOfTarget('postgres')).toBe(65_535); | ||
| expect(bindParameterLimitOfTarget('mysql')).toBe(65_535); | ||
| expect(bindParameterLimitOfTarget('sqlite')).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('bindParameterCountOf', () => { | ||
| // Three rows of one parameter of their own, and a parent key they all share: four references, | ||
| // six occurrences. | ||
| const parentKey = ParamRef.of(1); | ||
| const ast = InsertAst.into(TableSource.named('post')).withRows( | ||
| [0, 1, 2].map((i) => ({ id: ParamRef.of(i + 1), user_id: parentKey })), | ||
| ); | ||
|
|
||
| it('counts a shared parameter once on PostgreSQL, which numbers a reference once', () => { | ||
| expect(bindParameterCountOf(ast, 'postgres')).toBe(4); | ||
| }); | ||
|
|
||
| it('counts a shared parameter once per occurrence on MySQL, whose driver binds a value per placeholder', () => { | ||
| expect(bindParameterCountOf(ast, 'mysql')).toBe(6); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/3-extensions/sql-orm-client/src/statement-graph/bind-parameter-limit.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import type { Contract } from '@internal/contract/types'; | ||
| import type { SqlStorage } from '@internal/sql-contract/types'; | ||
| import type { InsertAst } from '@internal/sql-relational-core/ast'; | ||
| import { bindParameterCountOf, targetDisplayNameOf } from '@internal/sql-relational-core/plan'; | ||
| import { bindParameterLimitOf } from '../collection-contract'; | ||
| import { ormError } from '../orm-errors'; | ||
| import type { WrittenRowOwner } from '../written-keys'; | ||
|
|
||
| /** | ||
| * Refuses INSERT statements that bind more parameters than the target allows in one statement. | ||
| * | ||
| * `construction` runs on the ASTs a graph is built from, before any statement is sent. `execution` | ||
| * runs in `Insert.execute` on the ASTs after payload parameters (parent keys, junction keys) have | ||
| * been added to every row, because those push the statement past a limit its construction-time | ||
| * count was under. The count is the target's, from `bindParameterCountOf`: MySQL binds a parameter | ||
| * the statement repeats once per occurrence, so the one payload parameter every row shares counts | ||
| * once per row there. The `why` of each refusal matches its point: at construction nothing was sent, | ||
| * while at execution earlier statements of the same run may already have written rows. | ||
| */ | ||
| export function requireBindParametersWithinLimit( | ||
| contract: Contract<SqlStorage>, | ||
| asts: readonly InsertAst[], | ||
| owner: WrittenRowOwner, | ||
| point: 'construction' | 'execution' = 'construction', | ||
| ): void { | ||
| const limit = bindParameterLimitOf(contract); | ||
| if (limit === undefined) return; | ||
| for (const ast of asts) { | ||
| const parameters = bindParameterCountOf(ast, contract.target); | ||
| if (parameters <= limit) continue; | ||
| throw ormError( | ||
| 'ORM.ARGUMENT_INVALID', | ||
| `${owner.operation}() on model "${owner.modelName}" would bind ${parameters} parameters in one INSERT statement; ${targetDisplayNameOf(contract.target)} allows ${limit} per statement.`, | ||
| { | ||
| why: | ||
| point === 'construction' | ||
| ? 'The database rejects a statement past its bind-parameter limit with a driver error that reads as a broken connection, so the ORM refuses the write before sending it; nothing was written.' | ||
| : 'The database rejects a statement past its bind-parameter limit with a driver error that reads as a broken connection, so the ORM refuses the statement before sending it; statements earlier in the same run may already have written rows, and the surrounding transaction decides whether they stay.', | ||
| fix: `Split the rows into smaller batches so no single INSERT statement binds more than ${limit} parameters.`, | ||
| meta: { | ||
| problem: 'too-many-bind-parameters', | ||
| method: `${owner.operation}()`, | ||
| model: owner.modelName, | ||
| parameters, | ||
| limit, | ||
| target: contract.target, | ||
| }, | ||
| }, | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.