Skip to content

Commit

Permalink
feat: Bit reverse sequence (#1846)
Browse files Browse the repository at this point in the history
* changes for brs

* changes

* changes

* changes

* changes

* changes

* changes

* comment change

* changes

* Update samples/sequence-create.js

Co-authored-by: Rajat Bhatta <93644539+rajatbhatta@users.noreply.github.com>

* changes

* changes

* lint

* Update samples/pg-sequence-alter.js

Co-authored-by: Rajat Bhatta <93644539+rajatbhatta@users.noreply.github.com>

* Update samples/pg-sequence-drop.js

Co-authored-by: Rajat Bhatta <93644539+rajatbhatta@users.noreply.github.com>

* Update samples/sequence-drop.js

Co-authored-by: Rajat Bhatta <93644539+rajatbhatta@users.noreply.github.com>

* Update samples/sequence-alter.js

Co-authored-by: Rajat Bhatta <93644539+rajatbhatta@users.noreply.github.com>

* Update samples/sequence-create.js

Co-authored-by: Rajat Bhatta <93644539+rajatbhatta@users.noreply.github.com>

* Update samples/pg-sequence-create.js

Co-authored-by: Rajat Bhatta <93644539+rajatbhatta@users.noreply.github.com>

* Changes

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: surbhigarg92 <surbhigarg.92@gmail.com>
Co-authored-by: Rajat Bhatta <93644539+rajatbhatta@users.noreply.github.com>
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
4 people committed Aug 4, 2023
1 parent 75f357a commit 4154c02
Show file tree
Hide file tree
Showing 9 changed files with 757 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -161,6 +161,9 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre
| 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) |
| 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) |
| 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) |
| Alters a sequence in a PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-sequence-alter.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-sequence-alter.js,samples/README.md) |
| Creates sequence in PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-sequence-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-sequence-create.js,samples/README.md) |
| Drops a sequence in PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-sequence-drop.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-sequence-drop.js,samples/README.md) |
| 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) |
| 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) |
| Read data with database role | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/read-data-with-database-role.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/read-data-with-database-role.js,samples/README.md) |
Expand All @@ -172,6 +175,9 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre
| Query data with RPC Priority | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/rpc-priority-run.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/rpc-priority-run.js,samples/README.md) |
| Run transaction with RPC priority | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/rpc-priority-transaction.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/rpc-priority-transaction.js,samples/README.md) |
| Schema | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/schema.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/schema.js,samples/README.md) |
| Alters a sequence in a GoogleSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/sequence-alter.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/sequence-alter.js,samples/README.md) |
| Creates sequence in GoogleSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/sequence-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/sequence-create.js,samples/README.md) |
| Drops a sequence in GoogleSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/sequence-drop.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/sequence-drop.js,samples/README.md) |
| Struct | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/struct.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/struct.js,samples/README.md) |
| Alters a table with foreign key delete cascade action | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/table-alter-with-foreign-key-delete-cascade.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/table-alter-with-foreign-key-delete-cascade.js,samples/README.md) |
| Creates a table with foreign key delete cascade action | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/table-create-with-foreign-key-delete-cascade.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/table-create-with-foreign-key-delete-cascade.js,samples/README.md) |
Expand Down
108 changes: 108 additions & 0 deletions samples/README.md
Expand Up @@ -86,6 +86,9 @@ and automatic, synchronous replication for high availability.
* [Showcases how a Spanner PostgreSQL database orders null values in a query.](#showcases-how-a-spanner-postgresql-database-orders-null-values-in-a-query.)
* [Execute a query with parameters on a Spanner PostgreSQL database.](#execute-a-query-with-parameters-on-a-spanner-postgresql-database.)
* [Query the information schema metadata in a Spanner PostgreSQL database.](#query-the-information-schema-metadata-in-a-spanner-postgresql-database.)
* [Alters a sequence in a PostgreSQL database.](#alters-a-sequence-in-a-postgresql-database.)
* [Creates sequence in PostgreSQL database.](#creates-sequence-in-postgresql-database.)
* [Drops a sequence in PostgreSQL database.](#drops-a-sequence-in-postgresql-database.)
* [Queryoptions](#queryoptions)
* [Quickstart](#quickstart)
* [Read data with database role](#read-data-with-database-role)
Expand All @@ -97,6 +100,9 @@ and automatic, synchronous replication for high availability.
* [Query data with RPC Priority](#query-data-with-rpc-priority)
* [Run transaction with RPC priority](#run-transaction-with-rpc-priority)
* [Schema](#schema)
* [Alters a sequence in a GoogleSQL database.](#alters-a-sequence-in-a-googlesql-database.)
* [Creates sequence in GoogleSQL database.](#creates-sequence-in-googlesql-database.)
* [Drops a sequence in GoogleSQL database.](#drops-a-sequence-in-googlesql-database.)
* [Struct](#struct)
* [Alters a table with foreign key delete cascade action](#alters-a-table-with-foreign-key-delete-cascade-action)
* [Creates a table with foreign key delete cascade action](#creates-a-table-with-foreign-key-delete-cascade-action)
Expand Down Expand Up @@ -1344,6 +1350,57 @@ __Usage:__



### Alters a sequence in a PostgreSQL database.

View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-sequence-alter.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-sequence-alter.js,samples/README.md)

__Usage:__


`node pg-sequence-alter.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>`


-----




### Creates sequence in PostgreSQL database.

View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-sequence-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-sequence-create.js,samples/README.md)

__Usage:__


`node pg-sequence-create.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>`


-----




### Drops a sequence in PostgreSQL database.

View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-sequence-drop.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-sequence-drop.js,samples/README.md)

__Usage:__


`node pg-sequence-drop.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>`


-----




### Queryoptions

View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/queryoptions.js).
Expand Down Expand Up @@ -1531,6 +1588,57 @@ __Usage:__



### Alters a sequence in a GoogleSQL database.

View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/sequence-alter.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/sequence-alter.js,samples/README.md)

__Usage:__


`node sequence-alter.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>`


-----




### Creates sequence in GoogleSQL database.

View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/sequence-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/sequence-create.js,samples/README.md)

__Usage:__


`node sequence-create.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>`


-----




### Drops a sequence in GoogleSQL database.

View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/sequence-drop.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/sequence-drop.js,samples/README.md)

__Usage:__


`node sequence-drop.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>`


-----




### Struct

View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/struct.js).
Expand Down
95 changes: 95 additions & 0 deletions samples/pg-sequence-alter.js
@@ -0,0 +1,95 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// sample-metadata:
// title: Alters a sequence in a PostgreSQL database.
// usage: node pg-sequence-alter.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>

'use strict';

async function main(instanceId, databaseId, projectId) {
// [START spanner_postgresql_alter_sequence]
// Imports the Google Cloud client library.
const {Spanner} = require('@google-cloud/spanner');

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';
// const databaseId = 'my-database';

// Creates a client
const spanner = new Spanner({
projectId: projectId,
});

async function alterSequence(instanceId, databaseId) {
// Gets a reference to a Cloud Spanner instance and database
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);

const request = ['ALTER SEQUENCE Seq SKIP RANGE 1000 5000000'];

try {
const [operation] = await database.updateSchema(request);

console.log('Waiting for operation to complete...');
await operation.promise();

console.log(
'Altered Seq sequence to skip an inclusive range between 1000 and 5000000.'
);
} catch (err) {
console.error('ERROR:', err);
}
database.runTransaction(async (err, transaction) => {
if (err) {
console.error(err);
return;
}
try {
const [rows, stats] = await transaction.run({
sql: "INSERT INTO Customers (CustomerName) VALUES ('Lea'), ('Catalina'), ('Smith') RETURNING CustomerId",
});

rows.forEach(row => {
console.log(
`Inserted customer record with CustomerId: ${
row.toJSON({wrapNumbers: true}).customerid.value
}`
);
});

const rowCount = Math.floor(stats[stats.rowCount]);
console.log(`Number of customer records inserted is: ${rowCount}`);

await transaction.commit();
} catch (err) {
console.error('ERROR:', err);
} finally {
// Close the database when finished.
await database.close();
}
});
}
await alterSequence(instanceId, databaseId);
// [END spanner_postgresql_alter_sequence]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
99 changes: 99 additions & 0 deletions samples/pg-sequence-create.js
@@ -0,0 +1,99 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// sample-metadata:
// title: Creates sequence in PostgreSQL database.
// usage: node pg-sequence-create.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>

'use strict';

async function main(instanceId, databaseId, projectId) {
// [START spanner_postgresql_create_sequence]
// Imports the Google Cloud client library.
const {Spanner} = require('@google-cloud/spanner');

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';
// const databaseId = 'my-database';

// Creates a client
const spanner = new Spanner({
projectId: projectId,
});

async function createSequence(instanceId, databaseId) {
// Gets a reference to a Cloud Spanner instance and database
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);

const request = [
'CREATE SEQUENCE Seq BIT_REVERSED_POSITIVE',
"CREATE TABLE Customers (CustomerId BIGINT DEFAULT nextval('Seq'), CustomerName character varying(1024), PRIMARY KEY (CustomerId))",
];

// Creates a new table with sequence
try {
const [operation] = await database.updateSchema(request);

console.log('Waiting for operation to complete...');
await operation.promise();

console.log(
'Created Seq sequence and Customers table, where the key column CustomerId uses the sequence as a default value'
);
} catch (err) {
console.error('ERROR:', err);
}
database.runTransaction(async (err, transaction) => {
if (err) {
console.error(err);
return;
}
try {
const [rows, stats] = await transaction.run({
sql: "INSERT INTO Customers (CustomerName) VALUES ('Alice'), ('David'), ('Marc') RETURNING CustomerId",
});

rows.forEach(row => {
console.log(
`Inserted customer record with CustomerId: ${
row.toJSON({wrapNumbers: true}).customerid.value
}`
);
});

const rowCount = Math.floor(stats[stats.rowCount]);
console.log(`Number of customer records inserted is: ${rowCount}`);

await transaction.commit();
} catch (err) {
console.error('ERROR:', err);
} finally {
// Close the database when finished.
await database.close();
}
});
}
await createSequence(instanceId, databaseId);
// [END spanner_postgresql_create_sequence]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));

0 comments on commit 4154c02

Please sign in to comment.