Skip to content

Commit

Permalink
Add bind params to Spanner samples (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace Nassri authored and jmdobry committed Jul 7, 2017
1 parent 8867ca3 commit f6d008d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
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

0 comments on commit f6d008d

Please sign in to comment.