Skip to content

Commit 6ef44c3

Browse files
authored
feat: untyped param types (#1869)
This features allows customers to not pass paramType in parametrised queries. It gives backend to flexibly match the type. Example: Previously is timestamp was passed as string, it would be passed to backend with string type code and hence an error would be thrown. Now it is passed without any type code and hence backend handles checking its time and the code succeeds.
1 parent bed4832 commit 6ef44c3

File tree

8 files changed

+420
-27
lines changed

8 files changed

+420
-27
lines changed

samples/dml.js

+15
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,21 @@ function updateUsingDmlWithStruct(instanceId, databaseId, projectId) {
288288
params: {
289289
name: nameStruct,
290290
},
291+
types: {
292+
name: {
293+
type: 'struct',
294+
fields: [
295+
{
296+
name: 'FirstName',
297+
type: 'string',
298+
},
299+
{
300+
name: 'LastName',
301+
type: 'string',
302+
},
303+
],
304+
},
305+
},
291306
});
292307

293308
console.log(`Successfully updated ${rowCount} record.`);

samples/struct.js

+30
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,21 @@ async function queryDataWithStruct(instanceId, databaseId, projectId) {
111111
params: {
112112
name: nameStruct,
113113
},
114+
types: {
115+
name: {
116+
type: 'struct',
117+
fields: [
118+
{
119+
name: 'FirstName',
120+
type: 'string',
121+
},
122+
{
123+
name: 'LastName',
124+
type: 'string',
125+
},
126+
],
127+
},
128+
},
114129
};
115130

116131
// Queries rows from the Singers table
@@ -250,6 +265,21 @@ async function queryStructField(instanceId, databaseId, projectId) {
250265
params: {
251266
name: nameStruct,
252267
},
268+
types: {
269+
name: {
270+
type: 'struct',
271+
fields: [
272+
{
273+
name: 'FirstName',
274+
type: 'string',
275+
},
276+
{
277+
name: 'LastName',
278+
type: 'string',
279+
},
280+
],
281+
},
282+
},
253283
};
254284

255285
// Queries rows from the Singers table

src/codec.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -597,10 +597,6 @@ function getType(value: Value): Type {
597597
return {type: 'bool'};
598598
}
599599

600-
if (is.string(value)) {
601-
return {type: 'string'};
602-
}
603-
604600
if (Buffer.isBuffer(value)) {
605601
return {type: 'bytes'};
606602
}
@@ -643,6 +639,7 @@ function getType(value: Value): Type {
643639
return {type: 'json'};
644640
}
645641

642+
// String type is also returned as unspecified to allow untyped parameters
646643
return {type: 'unspecified'};
647644
}
648645

src/transaction.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,10 @@ export class Snapshot extends EventEmitter {
13001300
if (!is.empty(typeMap)) {
13011301
Object.keys(typeMap).forEach(param => {
13021302
const type = typeMap[param];
1303-
paramTypes[param] = codec.createTypeObject(type);
1303+
const typeObject = codec.createTypeObject(type);
1304+
if (typeObject.code !== 'TYPE_CODE_UNSPECIFIED') {
1305+
paramTypes[param] = codec.createTypeObject(type);
1306+
}
13041307
});
13051308
}
13061309

0 commit comments

Comments
 (0)