Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/spanner/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,15 @@ function generateTypeHintsFromParams(params?: Record<string, any>): Record<strin
if (!params) return undefined;

const typeHints: Record<string, string> = {};
let hasKeys = false;
for (const key in params) {
if (Object.prototype.hasOwnProperty.call(params, key)) {
typeHints[key] = inferSpannerTypeFromValue(params[key]);
hasKeys = true;
}
}
return typeHints;
// Return undefined if params is empty to avoid sending empty types object to Spanner
return hasKeys ? typeHints : undefined;
}

// Helper function to merge provided hints with inferred hints
Expand Down
16 changes: 15 additions & 1 deletion test/spanner/adapter-type-hints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,15 @@ class TestableSpannerAdapter {
if (!params) return undefined;

const typeHints: Record<string, string> = {};
let hasKeys = false;
for (const key in params) {
if (Object.prototype.hasOwnProperty.call(params, key)) {
typeHints[key] = this.inferSpannerTypeFromValue(params[key]);
hasKeys = true;
}
}
return typeHints;
// Return undefined if params is empty to avoid sending empty types object to Spanner
return hasKeys ? typeHints : undefined;
}

// Helper function to merge provided hints with inferred hints
Expand Down Expand Up @@ -673,6 +676,17 @@ describe("SpannerAdapter Type Hints", () => {
});

describe("Automatic Type Inference", () => {
it("should handle queries without parameters correctly", async () => {
// No parameters at all
const result1 = await adapter.query("SELECT * FROM test");
// Mock returns "no_types" when no types are passed
expect(result1).toEqual([{ result: "no_types" }]);

// Empty parameters object
const result2 = await adapter.query("SELECT * FROM test", {});
expect(result2).toEqual([{ result: "no_types" }]);
});

it("should automatically infer types when no hints provided", async () => {
const params = {
stringParam: "test",
Expand Down