Skip to content
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

feat: Bit reverse sequence #1846

Merged
merged 27 commits into from Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 94 additions & 0 deletions samples/pg-sequence-alter.js
@@ -0,0 +1,94 @@
// 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 table.
asthamohta marked this conversation as resolved.
Show resolved Hide resolved
// 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",
});

const rowCount = Math.floor(stats[stats.rowCount]);
console.log(
`Successfully inserted ${rowCount} record into the Customers table.`
);
rows.forEach(row => {
console.log(
`CustomerId: ${row.toJSON({wrapNumbers: true}).customerid.value}`
asthamohta marked this conversation as resolved.
Show resolved Hide resolved
);
});

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));
98 changes: 98 additions & 0 deletions samples/pg-sequence-create.js
@@ -0,0 +1,98 @@
// 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 table.
asthamohta marked this conversation as resolved.
Show resolved Hide resolved
// 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",
});

const rowCount = Math.floor(stats[stats.rowCount]);
console.log(
`Successfully inserted ${rowCount} record into the Customers table.`
);
rows.forEach(row => {
console.log(
`CustomerId: ${row.toJSON({wrapNumbers: true}).customerid.value}`
);
});

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));
73 changes: 73 additions & 0 deletions samples/pg-sequence-drop.js
@@ -0,0 +1,73 @@
// 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: Drops a sequence in PostgreSQL database table.
asthamohta marked this conversation as resolved.
Show resolved Hide resolved
// usage: node pg-sequence-drop.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>

'use strict';

async function main(instanceId, databaseId, projectId) {
// [START spanner_drop_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 dropSequence(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 TABLE Customers ALTER COLUMN CustomerId DROP DEFAULT',
'DROP SEQUENCE Seq',
];

// Drop sequence from DDL
try {
const [operation] = await database.updateSchema(request);

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

console.log(
'Altered Customers table to drop DEFAULT from CustomerId column and dropped the Seq sequence.'
);
} catch (err) {
console.error('ERROR:', err);
} finally {
// Close the database when finished.
await database.close();
}
}
await dropSequence(instanceId, databaseId);
// [END spanner_drop_sequence]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
96 changes: 96 additions & 0 deletions samples/sequence-alter.js
@@ -0,0 +1,96 @@
// 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 database table.
asthamohta marked this conversation as resolved.
Show resolved Hide resolved
// usage: node sequence-alter.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>

'use strict';

async function main(instanceId, databaseId, projectId) {
// [START spanner_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 SET OPTIONS (skip_range_min = 1000, skip_range_max = 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') THEN RETURN CustomerId",
});

const rowCount = Math.floor(stats[stats.rowCount]);
console.log(
`Successfully inserted ${rowCount} record into the Customers table.`
);
rows.forEach(row => {
console.log(
`CustomerId: ${row.toJSON({wrapNumbers: true}).CustomerId.value}`
);
});

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

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