GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: Postgres Numeric and database support (#1592) · googleapis/google-cloud-node@70d5e5e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handwritten/spanner
    • README.md
    • src
      • codec.ts
      • index.ts
      • instance.ts
    • test
      • codec.ts
      • index.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handwritten/spanner/README.md‎

Copy file name to clipboardExpand all lines: handwritten/spanner/README.md
+12Lines changed: 12 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre
129129
| Numeric-add-column | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/numeric-add-column.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/numeric-add-column.js,samples/README.md) |
130130
| Numeric-query-parameter | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/numeric-query-parameter.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/numeric-query-parameter.js,samples/README.md) |
131131
| Numeric-update-data | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/numeric-update-data.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/numeric-update-data.js,samples/README.md) |
132+
| Showcase the rules for case-sensitivity and case folding for a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-case-sensitivity.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-case-sensitivity.js,samples/README.md) |
133+
| Creates a PostgreSQL Database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-database-create.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-database-create.js,samples/README.md) |
134+
| Use cast operator to cast from one data type to another in a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-datatypes-casting.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-datatypes-casting.js,samples/README.md) |
135+
| Execute a batch of DML statements on a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-dml-batch.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-dml-batch.js,samples/README.md) |
136+
| Execute a Partitioned DML on a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-dml-partitioned.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-dml-partitioned.js,samples/README.md) |
137+
| Execute a DML statement with parameters on a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-dml-with-parameter.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-dml-with-parameter.js,samples/README.md) |
138+
| Calls a server side function on a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-functions.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-functions.js,samples/README.md) |
139+
| Created interleaved table hierarchy using PostgreSQL dialect. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-interleaving.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-interleaving.js,samples/README.md) |
140+
| Showcase how to work with the PostgreSQL NUMERIC/DECIMAL data type on a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-numeric-data-type.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-numeric-data-type.js,samples/README.md) |
141+
| Showcases how a Spanner PostgreSQL database orders null values in a query. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-ordering-nulls.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-ordering-nulls.js,samples/README.md) |
142+
| Execute a query with parameters on a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-query-parameter.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-query-parameter.js,samples/README.md) |
143+
| Query the information schema metadata in a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-schema-information.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-schema-information.js,samples/README.md) |
132144
| Queryoptions | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/queryoptions.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/queryoptions.js,samples/README.md) |
133145
| Quickstart | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/quickstart.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/quickstart.js,samples/README.md) |
134146
| Sets a request tag for a single query | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/request-tag.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/request-tag.js,samples/README.md) |
Collapse file

‎handwritten/spanner/src/codec.ts‎

Copy file name to clipboardExpand all lines: handwritten/spanner/src/codec.ts
+45-2Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ import {PreciseDate} from '@google-cloud/precise-date';
1818
import arrify = require('arrify');
1919
import {Big} from 'big.js';
2020
import * as is from 'is';
21-
import {common as p} from 'protobufjs';
21+
import {common as p, util} from 'protobufjs';
2222
import {google as spannerClient} from '../protos/protos';
2323
import {GoogleError} from 'google-gax';
24+
import float = util.float;
2425

2526
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2627
export type Value = any;
@@ -216,6 +217,26 @@ export class Numeric {
216217
}
217218
}
218219

220+
/**
221+
* @typedef PGNumeric
222+
* @see Spanner.pgNumeric
223+
*/
224+
export class PGNumeric {
225+
value: string;
226+
constructor(pgValue: string | number) {
227+
this.value = pgValue.toString();
228+
}
229+
valueOf(): Big {
230+
if (this.value.toLowerCase() === 'nan') {
231+
throw new Error(`${this.value} cannot be converted to a numeric value`);
232+
}
233+
return new Big(this.value);
234+
}
235+
toJSON(): string {
236+
return this.valueOf().toJSON();
237+
}
238+
}
239+
219240
/**
220241
* @typedef JSONOptions
221242
* @property {boolean} [wrapNumbers=false] Indicates if the numbers should be
@@ -333,6 +354,14 @@ function decode(value: Value, type: spannerClient.spanner.v1.Type): Value {
333354
break;
334355
case spannerClient.spanner.v1.TypeCode.NUMERIC:
335356
case 'NUMERIC':
357+
if (
358+
type.typeAnnotation ===
359+
spannerClient.spanner.v1.TypeAnnotationCode.PG_NUMERIC ||
360+
type.typeAnnotation === 'PG_NUMERIC'
361+
) {
362+
decoded = new PGNumeric(decoded);
363+
break;
364+
}
336365
decoded = new Numeric(decoded);
337366
break;
338367
case spannerClient.spanner.v1.TypeCode.TIMESTAMP:
@@ -413,6 +442,10 @@ function encodeValue(value: Value): Value {
413442
return value.value;
414443
}
415444

445+
if (value instanceof PGNumeric) {
446+
return value.value;
447+
}
448+
416449
if (Buffer.isBuffer(value)) {
417450
return value.toString('base64');
418451
}
@@ -446,6 +479,7 @@ const TypeCode: {
446479
int64: 'INT64',
447480
float64: 'FLOAT64',
448481
numeric: 'NUMERIC',
482+
pgNumeric: 'NUMERIC',
449483
timestamp: 'TIMESTAMP',
450484
date: 'DATE',
451485
string: 'STRING',
@@ -524,6 +558,10 @@ function getType(value: Value): Type {
524558
return {type: 'numeric'};
525559
}
526560

561+
if (value instanceof PGNumeric) {
562+
return {type: 'pgNumeric'};
563+
}
564+
527565
if (is.boolean(value)) {
528566
return {type: 'bool'};
529567
}
@@ -639,7 +677,7 @@ function createTypeObject(
639677
friendlyType = 'unspecified';
640678
}
641679

642-
if (is.string(friendlyType)) {
680+
if (typeof friendlyType === 'string') {
643681
friendlyType = {type: friendlyType} as Type;
644682
}
645683

@@ -662,6 +700,10 @@ function createTypeObject(
662700
};
663701
}
664702

703+
if (friendlyType.type === 'pgNumeric') {
704+
type.typeAnnotation =
705+
spannerClient.spanner.v1.TypeAnnotationCode.PG_NUMERIC;
706+
}
665707
return type;
666708
}
667709

@@ -674,6 +716,7 @@ export const codec = {
674716
Float,
675717
Int,
676718
Numeric,
719+
PGNumeric,
677720
convertFieldsToJson,
678721
decode,
679722
encode,
Collapse file

‎handwritten/spanner/src/index.ts‎

Copy file name to clipboardExpand all lines: handwritten/spanner/src/index.ts
+31-2Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ import * as path from 'path';
2424
import {common as p} from 'protobufjs';
2525
import * as streamEvents from 'stream-events';
2626
import * as through from 'through2';
27-
import {codec, Float, Int, Numeric, SpannerDate, Struct} from './codec';
27+
import {
28+
codec,
29+
Float,
30+
Int,
31+
Numeric,
32+
PGNumeric,
33+
SpannerDate,
34+
Struct,
35+
} from './codec';
2836
import {Backup} from './backup';
2937
import {Database} from './database';
3038
import {
@@ -33,7 +41,7 @@ import {
3341
CreateInstanceResponse,
3442
} from './instance';
3543
import {grpc, GrpcClientOptions, CallOptions, GoogleError} from 'google-gax';
36-
import {google as instanceAdmin} from '../protos/protos';
44+
import {google, google as instanceAdmin} from '../protos/protos';
3745
import {
3846
PagedOptions,
3947
PagedResponse,
@@ -185,6 +193,10 @@ class Spanner extends GrpcService {
185193
* @type {string}
186194
*/
187195
static COMMIT_TIMESTAMP = 'spanner.commit_timestamp()';
196+
static POSTGRESQL =
197+
google.spanner.admin.database.v1.DatabaseDialect.POSTGRESQL;
198+
static GOOGLE_STANDARD_SQL =
199+
google.spanner.admin.database.v1.DatabaseDialect.GOOGLE_STANDARD_SQL;
188200

189201
/**
190202
* Gets the configured Spanner emulator host from an environment variable.
@@ -1274,6 +1286,22 @@ class Spanner extends GrpcService {
12741286
return new codec.Numeric(value);
12751287
}
12761288

1289+
/**
1290+
* Helper function to get a Cloud Spanner pgNumeric object.
1291+
*
1292+
* @param {string} value The pgNumeric value as a string.
1293+
* @returns {PGNumeric}
1294+
*
1295+
* @example
1296+
* ```
1297+
* const {Spanner} = require('@google-cloud/spanner');
1298+
* const pgNumeric = Spanner.pgNumeric("3.141592653");
1299+
* ```
1300+
*/
1301+
static pgNumeric(value): PGNumeric {
1302+
return new codec.PGNumeric(value);
1303+
}
1304+
12771305
/**
12781306
* Helper function to get a Cloud Spanner Struct object.
12791307
*
@@ -1309,6 +1337,7 @@ promisifyAll(Spanner, {
13091337
'instance',
13101338
'int',
13111339
'numeric',
1340+
'pgNumeric',
13121341
'operation',
13131342
'timestamp',
13141343
],
Collapse file

‎handwritten/spanner/src/instance.ts‎

Copy file name to clipboardExpand all lines: handwritten/spanner/src/instance.ts
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,10 +888,17 @@ class Instance extends common.GrpcServiceObject {
888888

889889
const poolOptions = options.poolOptions;
890890
const poolCtor = options.poolCtor;
891+
let createStatement = 'CREATE DATABASE `' + name.split('/').pop() + '`';
892+
if (
893+
databaseAdmin.spanner.admin.database.v1.DatabaseDialect.POSTGRESQL ===
894+
options.databaseDialect
895+
) {
896+
createStatement = 'CREATE DATABASE "' + name.split('/').pop() + '"';
897+
}
891898
const reqOpts = extend(
892899
{
893900
parent: this.formattedName_,
894-
createStatement: 'CREATE DATABASE `' + name.split('/').pop() + '`',
901+
createStatement: createStatement,
895902
},
896903
options
897904
);
Collapse file

‎handwritten/spanner/test/codec.ts‎

Copy file name to clipboardExpand all lines: handwritten/spanner/test/codec.ts
+83Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,46 @@ describe('codec', () => {
188188
});
189189
});
190190

191+
describe('PGNumeric', () => {
192+
it('should store value as a string', () => {
193+
const value = '8.01911';
194+
const pgNumeric = new codec.PGNumeric(value);
195+
196+
assert.strictEqual(pgNumeric.value, '8.01911');
197+
});
198+
199+
it('should store NaN value as a string', () => {
200+
const value = 'NaN';
201+
const pgNumeric = new codec.PGNumeric(value);
202+
203+
assert.strictEqual(pgNumeric.value, 'NaN');
204+
});
205+
206+
it('should return as a Big', () => {
207+
const value = '8.01911';
208+
const pgNumeric = new codec.PGNumeric(value);
209+
210+
const expected = new Big(value);
211+
assert.ok(pgNumeric.valueOf().eq(expected));
212+
});
213+
214+
it('should throw an error when trying to return NaN as a Big', () => {
215+
const value = 'NaN';
216+
const pgNumeric = new codec.PGNumeric(value);
217+
218+
assert.throws(() => {
219+
pgNumeric.valueOf();
220+
}, new RegExp('NaN cannot be converted to a numeric value'));
221+
});
222+
223+
it('toJSON', () => {
224+
const value = '8.01911';
225+
const pgNumeric = new codec.PGNumeric(value);
226+
227+
assert.strictEqual(pgNumeric.toJSON(), value);
228+
});
229+
});
230+
191231
describe('Struct', () => {
192232
describe('toJSON', () => {
193233
it('should covert the struct to JSON', () => {
@@ -463,6 +503,18 @@ describe('codec', () => {
463503
assert.strictEqual(decoded.value, value);
464504
});
465505

506+
it('should decode PG NUMERIC', () => {
507+
const value = '8.01911';
508+
509+
const decoded = codec.decode(value, {
510+
code: google.spanner.v1.TypeCode.NUMERIC,
511+
typeAnnotation: google.spanner.v1.TypeAnnotationCode.PG_NUMERIC,
512+
});
513+
514+
assert(decoded instanceof codec.PGNumeric);
515+
assert.strictEqual(decoded.value, value);
516+
});
517+
466518
it('should decode JSON', () => {
467519
const value = '{"result":true, "count":42}';
468520
const expected = JSON.parse(value);
@@ -618,6 +670,14 @@ describe('codec', () => {
618670
assert.strictEqual(encoded, value.value);
619671
});
620672

673+
it('should stringify PG NUMERIC', () => {
674+
const value = new codec.PGNumeric('8.01911');
675+
676+
const encoded = codec.encode(value);
677+
678+
assert.strictEqual(encoded, value.value);
679+
});
680+
621681
it('should encode ARRAY and inner members', () => {
622682
const value = [5];
623683

@@ -811,6 +871,12 @@ describe('codec', () => {
811871
},
812872
});
813873
});
874+
875+
it('should determine if the value is a PGNumeric', () => {
876+
assert.deepStrictEqual(codec.getType(new codec.PGNumeric('7248')), {
877+
type: 'pgNumeric',
878+
});
879+
});
814880
});
815881

816882
describe('convertToListValue', () => {
@@ -1019,5 +1085,22 @@ describe('codec', () => {
10191085
},
10201086
});
10211087
});
1088+
it('should set code and typeAnnotation for pgNumeric string', () => {
1089+
const type = codec.createTypeObject('pgNumeric');
1090+
1091+
assert.deepStrictEqual(type, {
1092+
code: google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.NUMERIC],
1093+
typeAnnotation: google.spanner.v1.TypeAnnotationCode.PG_NUMERIC,
1094+
});
1095+
});
1096+
1097+
it('should set code and typeAnnotation for pgNumeric friendlyType object', () => {
1098+
const type = codec.createTypeObject({type: 'pgNumeric'});
1099+
1100+
assert.deepStrictEqual(type, {
1101+
code: google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.NUMERIC],
1102+
typeAnnotation: google.spanner.v1.TypeAnnotationCode.PG_NUMERIC,
1103+
});
1104+
});
10221105
});
10231106
});
Collapse file

‎handwritten/spanner/test/index.ts‎

Copy file name to clipboardExpand all lines: handwritten/spanner/test/index.ts
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const fakePfy = extend({}, pfy, {
8585
'instance',
8686
'int',
8787
'numeric',
88+
'pgNumeric',
8889
'operation',
8990
'timestamp',
9091
]);
@@ -514,6 +515,23 @@ describe('Spanner', () => {
514515
});
515516
});
516517

518+
describe('pgNumeric', () => {
519+
it('should create a PGNumeric instance', () => {
520+
const value = '3.145';
521+
const customValue = {value: '3.145'};
522+
523+
fakeCodec.PGNumeric = class {
524+
constructor(value_) {
525+
assert.strictEqual(value_, value);
526+
return customValue;
527+
}
528+
};
529+
530+
const pgNumeric = Spanner.pgNumeric(value);
531+
assert.strictEqual(pgNumeric, customValue);
532+
});
533+
});
534+
517535
describe('createInstance', () => {
518536
const NAME = 'instance-name';
519537
let PATH;

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.