Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

D1 queries like select ?1 + ?2 broken after #480 PR #526

Closed
ruslantalpa opened this issue Mar 1, 2023 · 1 comment · Fixed by #544
Closed

D1 queries like select ?1 + ?2 broken after #480 PR #526

ruslantalpa opened this issue Mar 1, 2023 · 1 comment · Fixed by #544

Comments

@ruslantalpa
Copy link

I added these lines to one of the tests (to make sure it's not my code):

const sum = await db.prepare("SELECT ?1 + ?2 as sum").bind(1, 2).first("sum");
t.is(sum, 3);

and the test fails

d1 › test › d1js.ts › D1PreparedStatement: first

  packages/d1/test/d1js.spec.ts:212

   211:
   212:   const sum = await db.prepare("SELECT ?1 + ?2 as sum").bind(1, 2).first("sum");
   213:   t.is(sum, 3);

  Rejected promise returned by test. Reason:

  Error {
    message: 'D1_ERROR',
  }

  › D1Database._send (packages/d1/src/d1js.ts:148:13)
  › D1PreparedStatement.first (packages/d1/src/d1js.ts:215:7)
  › packages/d1/test/d1js.spec.ts:212:15

  ─

statements.ts used to have this code which was deleted

      // For statements using ?1 ?2, etc, we want to pass them as varargs but
      // "better" sqlite3 wants them as an object of {1: params[0], 2: params[1], ...}
      if (this.#bindings.length > 0 && typeof this.#bindings[0] !== "object") {
        return prepared.bind(
          Object.fromEntries(this.#bindings.map((v, i) => [i + 1, v]))
        );
      } else {
        throw e;
      }
@mrbbot
Copy link
Contributor

mrbbot commented Mar 2, 2023

Hey! 👋 Thanks opening this issue. It looks like a duplicate of #504, so I'm going to close this and keep tracking everything over there. 👍

@mrbbot mrbbot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 2, 2023
mrbbot added a commit that referenced this issue Mar 22, 2023
`better-sqlite3` expects parameters of the form `?1, ?2, ...` to be
bound as an object of the form `{ 1: params[0], 2: params[1], ...}`.
In #480, we accidentally removed the code that handled this case.
This PR adds it back, and lifts out some common functionality into a
`#prepareAndBind()` function. :)

Thanks @ruslantalpa for spotting the removed code.

Closes #526
Closes cloudflare/workers-sdk#2811
Closes cloudflare/workers-sdk#2887
mrbbot added a commit that referenced this issue Mar 23, 2023
…544)

`better-sqlite3` expects parameters of the form `?1, ?2, ...` to be
bound as an object of the form `{ 1: params[0], 2: params[1], ...}`.
In #480, we accidentally removed the code that handled this case.
This PR adds it back, and lifts out some common functionality into a
`#prepareAndBind()` function. :)

Thanks @ruslantalpa for spotting the removed code.

Closes #526
Closes cloudflare/workers-sdk#2811
Closes cloudflare/workers-sdk#2887
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants