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

Add bind params to Spanner samples #418

Merged
merged 2 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions spanner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ Commands:
createStoringIndex <instanceName> <databaseName> Creates a new value-storing index in an example Cloud Spanner table.
queryIndex <instanceName> <databaseName> Executes a read-only SQL query against an example Cloud Spanner
table using an existing index.
Returns results with titles between a start title (default:
'Ardvark') and an end title (default: 'Goo').
readIndex <instanceName> <databaseName> Reads data from an example Cloud Spanner table using an existing
index.
readStoringIndex <instanceName> <databaseName> Reads data from an example Cloud Spanner table using an existing
Expand Down
33 changes: 27 additions & 6 deletions spanner/indexing.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function createStoringIndex (instanceId, databaseId) {
// [END create_storing_index]
}

function queryDataWithIndex (instanceId, databaseId) {
function queryDataWithIndex (instanceId, databaseId, startTitle, endTitle) {
// [START query_data_with_index]
// Imports the Google Cloud client library
const Spanner = require('@google-cloud/spanner');
Expand All @@ -100,14 +100,22 @@ function queryDataWithIndex (instanceId, databaseId) {
// const instanceId = 'my-instance';
// const databaseId = 'my-database';

// Uncomment these lines to specify the start and end title(s)
// const startTitle = 'Ardvark';
// const endTitle = 'Goo';

// Gets a reference to a Cloud Spanner instance and database
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);

const query = {
sql: `SELECT AlbumId, AlbumTitle, MarketingBudget
FROM Albums@{FORCE_INDEX=AlbumsByAlbumTitle}
WHERE AlbumTitle >= 'Ardvark' AND AlbumTitle < 'Goo'`
WHERE AlbumTitle >= @startTitle AND AlbumTitle <= @endTitle`,
params: {
startTitle: startTitle,
endTitle: endTitle
}
};

// Queries rows from the Albums table
Expand All @@ -117,7 +125,8 @@ function queryDataWithIndex (instanceId, databaseId) {

rows.forEach((row) => {
const json = row.toJSON();
console.log(`AlbumId: ${json.AlbumId.value}, AlbumTitle: ${json.AlbumTitle}, MarketingBudget: ${json.MarketingBudget.value}`);
const marketingBudget = json.MarketingBudget ? json.MarketingBudget.value : null; // This value is nullable
console.log(`AlbumId: ${json.AlbumId.value}, AlbumTitle: ${json.AlbumTitle}, MarketingBudget: ${marketingBudget}`);
});
});
// [END query_data_with_index]
Expand Down Expand Up @@ -222,9 +231,21 @@ const cli = require(`yargs`)
)
.command(
`queryIndex <instanceName> <databaseName>`,
`Executes a read-only SQL query against an example Cloud Spanner table using an existing index.`,
{},
(opts) => queryDataWithIndex(opts.instanceName, opts.databaseName)
`Executes a read-only SQL query against an example Cloud Spanner table using an existing index.
Returns results with titles between a start title (default: 'Ardvark') and an end title (default: 'Goo').`,
{
startTitle: {
type: 'string',
alias: 's',
default: 'Ardvark'
},
endTitle: {
type: 'string',
alias: 'e',
default: 'Goo'
}
},
(opts) => queryDataWithIndex(opts.instanceName, opts.databaseName, opts.startTitle, opts.endTitle)
)
.command(
`readIndex <instanceName> <databaseName>`,
Expand Down
7 changes: 7 additions & 0 deletions spanner/system-test/spanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ test.serial(`should create a storing index in an example table`, async (t) => {
test.serial(`should query an example table with an index and return matching rows`, async (t) => {
const output = await tools.runAsync(`${indexingCmd} queryIndex ${INSTANCE_ID} ${DATABASE_ID}`, cwd);
t.true(output.includes(`AlbumId: 1, AlbumTitle: Go, Go, Go, MarketingBudget:`));
t.false(output.includes(`AlbumId: 2, AlbumTitle: Total Junk, MarketingBudget:`));
});

test.serial(`should respect query boundaries when querying an example table with an index`, async (t) => {
const output = await tools.runAsync(`${indexingCmd} queryIndex ${INSTANCE_ID} ${DATABASE_ID} -s Ardvark -e Zoo`, cwd);
t.true(output.includes(`AlbumId: 1, AlbumTitle: Go, Go, Go, MarketingBudget:`));
t.true(output.includes(`AlbumId: 2, AlbumTitle: Total Junk, MarketingBudget:`));
});

// read_data_with_index
Expand Down