Skip to content

feat: Bit reverse sequence #1846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ef99a6d
changes for brs
asthamohta May 10, 2023
67f340f
changes
asthamohta May 10, 2023
8f9d8ff
changes
asthamohta May 10, 2023
ff826c6
changes
asthamohta May 10, 2023
295d655
changes
asthamohta May 10, 2023
e666112
changes
asthamohta May 10, 2023
e38f941
changes
asthamohta May 10, 2023
bba8f7e
comment change
asthamohta Jul 26, 2023
d9a4723
Merge branch 'main' into brs
surbhigarg92 Jul 27, 2023
f51dd22
changes
asthamohta Aug 3, 2023
54a7004
Merge branch 'brs' of github.com:asthamohta/nodejs-spanner into brs
asthamohta Aug 3, 2023
42075a3
Update samples/sequence-create.js
asthamohta Aug 3, 2023
e7caef7
changes
asthamohta Aug 3, 2023
b6676fe
Merge branch 'brs' of github.com:asthamohta/nodejs-spanner into brs
asthamohta Aug 3, 2023
67d55ec
changes
asthamohta Aug 4, 2023
e881910
lint
asthamohta Aug 4, 2023
3e31faa
Update samples/pg-sequence-alter.js
asthamohta Aug 4, 2023
04556ef
Update samples/pg-sequence-drop.js
asthamohta Aug 4, 2023
175eaa1
Update samples/sequence-drop.js
asthamohta Aug 4, 2023
4c88e8e
Update samples/sequence-alter.js
asthamohta Aug 4, 2023
81a87da
Update samples/sequence-create.js
asthamohta Aug 4, 2023
3a64656
Update samples/pg-sequence-create.js
asthamohta Aug 4, 2023
cfcb8bc
Changes
asthamohta Aug 4, 2023
80a4baf
Merge branch 'brs' of github.com:asthamohta/nodejs-spanner into brs
asthamohta Aug 4, 2023
8dc4ddc
Merge branch 'main' into brs
asthamohta Aug 4, 2023
e9d4b47
Merge branch 'main' into brs
asthamohta Aug 4, 2023
3d4f3de
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 4, 2023
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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));
Loading