Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kearfy committed Oct 18, 2024
1 parent 3c54c30 commit 1f41df5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/util/prepared-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,21 @@ export class PreparedQuery {
): PreparedQuery {
const base = this.length;
this.length += values.length;

let reused = 0;
const gaps = new Map<Gap, number>();
const mapped_bindings = values.map((v, i) => {
if (v instanceof Gap) {
const index = gaps.get(v);
if (index !== undefined) {
reused++;
return [`bind___${index}`, v] as const;
}

gaps.set(v, i);
gaps.set(v, i - reused);
}

return [`bind___${base + i}`, v] as const;
return [`bind___${base + i - reused}`, v] as const;
});

for (const [k, v] of mapped_bindings) {
Expand Down
6 changes: 4 additions & 2 deletions src/util/tagged-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ export function surrealql(
query_raw: string[] | TemplateStringsArray,
...values: unknown[]
): PreparedQuery {
let reused = 0;
const gaps = new Map<Gap, number>();
const mapped_bindings = values.map((v, i) => {
if (v instanceof Gap) {
const index = gaps.get(v);
if (index !== undefined) {
reused++;
return [`bind___${index}`, v] as const;
}

gaps.set(v, i);
gaps.set(v, i - reused);
}

return [`bind___${i}`, v] as const;
return [`bind___${i - reused}`, v] as const;
});

const bindings = mapped_bindings.reduce<Record<`bind___${number}`, unknown>>(
Expand Down
18 changes: 17 additions & 1 deletion tests/integration/tests/querying.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,26 @@ describe("template literal", async () => {
"bind___3",
]);

// Ensure appended segments also re-use
query.append`; RETURN [${foo}, ${bar}, ${1}, ${foo}, ${bar}, ${2}]`;
expect(Object.keys(query.bindings)).toStrictEqual([
"bind___0",
"bind___1",
"bind___2",
"bind___3",
"bind___4",
"bind___5",
"bind___6",
"bind___7",
]);

// Check result
const res = await surreal.query(query, [foo.fill("a"), bar.fill("b")]);

expect(res).toStrictEqual(["a", "b", 1, "a", "b", 2]);
expect(res).toStrictEqual([
["a", "b", 1, "a", "b", 2],
["a", "b", 1, "a", "b", 2],
]);
});
});

Expand Down

0 comments on commit 1f41df5

Please sign in to comment.