Skip to content
This repository was archived by the owner on Feb 5, 2026. It is now read-only.

Commit 123f39b

Browse files
committed
feat: update constructors and add cancel method
1 parent aa9e4b1 commit 123f39b

File tree

7 files changed

+38
-28
lines changed

7 files changed

+38
-28
lines changed

src/query/builder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
import {protos} from '../';
1616

1717
/**
18-
* QueryFromSQL creates a query configuration from a SQL string.
18+
* fromSQL creates a query configuration from a SQL string.
1919
* @param {string} sql The SQL query.
2020
* @returns {protos.google.cloud.bigquery.v2.IPostQueryRequest}
2121
*/
22-
export function queryFromSQL(
22+
export function fromSQL(
2323
projectId: string,
2424
sql: string,
2525
): protos.google.cloud.bigquery.v2.IPostQueryRequest {

src/query/client.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
import {
1616
BigQueryClient,
1717
BigQueryClientOptions,
18-
SubClientOptions,
1918
} from '../bigquery';
2019
import {QueryJob, CallOptions} from './job';
2120
import {protos} from '../';
22-
import {queryFromSQL as builderQueryFromSQL} from './builder';
21+
import {fromSQL as builderFromSQL} from './builder';
2322

2423
/**
2524
* QueryClient is a client for running queries in BigQuery.
@@ -34,9 +33,8 @@ export class QueryClient {
3433
*/
3534
constructor(
3635
options?: BigQueryClientOptions,
37-
subClientOptions?: SubClientOptions,
3836
) {
39-
this.client = new BigQueryClient(options, subClientOptions);
37+
this.client = new BigQueryClient(options);
4038
this.projectId = '';
4139
this.billingProjectId = '';
4240
void this.initialize();
@@ -80,12 +78,12 @@ export class QueryClient {
8078
}
8179

8280
/**
83-
* QueryFromSQL creates a query configuration from a SQL string.
81+
* fromSQL creates a query configuration from a SQL string.
8482
* @param {string} sql The SQL query.
8583
* @returns {protos.google.cloud.bigquery.v2.IPostQueryRequest}
8684
*/
87-
queryFromSQL(sql: string): protos.google.cloud.bigquery.v2.IPostQueryRequest {
88-
const req = builderQueryFromSQL(this.projectId, sql);
85+
fromSQL(sql: string): protos.google.cloud.bigquery.v2.IPostQueryRequest {
86+
const req = builderFromSQL(this.projectId, sql);
8987
return req;
9088
}
9189

src/query/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ export {QueryClient} from './client';
1616
export {QueryJob} from './job';
1717
export {Row} from './row';
1818
export {RowIterator} from './iterator';
19-
export {queryFromSQL} from './builder';
19+
export {fromSQL} from './builder';

src/query/job.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface CallOptions extends GaxCallOptions {
2929
*/
3030
export class QueryJob {
3131
private client: QueryClient;
32-
private complete: boolean;
32+
private jobComplete: boolean;
3333
private projectId: string;
3434
private jobId: string;
3535
private location: string;
@@ -49,7 +49,7 @@ export class QueryJob {
4949
this.cachedSchema = {};
5050
this.cachedPageToken = '';
5151
this.cachedTotalRows = 0;
52-
this.complete = false;
52+
this.jobComplete = false;
5353

5454
this.consumeQueryResponse({
5555
jobComplete: response.jobComplete,
@@ -84,6 +84,10 @@ export class QueryJob {
8484
return this.cachedSchema;
8585
}
8686

87+
get complete(): boolean {
88+
return this.jobComplete
89+
}
90+
8791
/**
8892
* Waits for the query to complete.
8993
*
@@ -103,6 +107,22 @@ export class QueryJob {
103107
}
104108
}
105109

110+
/**
111+
* Cancel a running query.
112+
*
113+
* @param {CallOptions} [options]
114+
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
115+
*/
116+
async cancel(options?: CallOptions): Promise<protos.google.cloud.bigquery.v2.IJobCancelResponse> {
117+
const {jobClient} = this.client.getBigQueryClient();
118+
const [response] = await jobClient.cancelJob({
119+
projectId: this.projectId,
120+
jobId: this.jobId,
121+
location: this.location,
122+
}, options);
123+
return response;
124+
}
125+
106126
private async waitFor(signal?: AbortSignal): Promise<void> {
107127
const delay = 1000; // TODO: backoff settings
108128
return new Promise((resolve, reject) => {
@@ -156,7 +176,7 @@ export class QueryJob {
156176
private consumeQueryResponse(
157177
response: protos.google.cloud.bigquery.v2.IGetQueryResultsResponse,
158178
) {
159-
this.complete = response.jobComplete?.value ?? false;
179+
this.jobComplete = response.jobComplete?.value ?? false;
160180
this.cachedSchema = response.schema!;
161181
this.cachedPageToken = response.pageToken!;
162182
this.cachedTotalRows = Number(response.totalRows);

system-test/fixtures/transport.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,14 @@ import { QueryClient } from '../../src/query';
1919
export const describeWithBothTransports = (title: string, fn: (client: QueryClient) => void) => {
2020
describe(title, () => {
2121
describe("REST", () => {
22-
const client = new query.QueryClient({}, {
23-
opts: {
24-
fallback: true,
25-
}
26-
});
22+
const client = new query.QueryClient({ fallback: true });
2723
before(async () => {
2824
await client.initialize();
2925
});
3026
fn(client);
3127
});
3228
describe("GRPC", () => {
33-
const client = new query.QueryClient({}, {
34-
opts: {
35-
fallback: false,
36-
}
37-
});
29+
const client = new query.QueryClient({ fallback: false });
3830
before(async () => {
3931
await client.initialize();
4032
});

system-test/query/query.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describeWithBothTransports('Run Query', client => {
3737
});
3838

3939
it('should run a stateless query', async () => {
40-
const req = client.queryFromSQL(
40+
const req = client.fromSQL(
4141
'SELECT CURRENT_TIMESTAMP() as foo, SESSION_USER() as bar',
4242
);
4343
req.queryRequest!.jobCreationMode =
@@ -55,7 +55,7 @@ describeWithBothTransports('Run Query', client => {
5555
});
5656

5757
it('should stop waiting for query to complete', async () => {
58-
const req = client.queryFromSQL(
58+
const req = client.fromSQL(
5959
'SELECT num FROM UNNEST(GENERATE_ARRAY(1,1000000)) as num',
6060
);
6161
req.queryRequest!.useQueryCache = {value: false};
@@ -78,7 +78,7 @@ describeWithBothTransports('Run Query', client => {
7878
}).timeout(5000);
7979

8080
it('should read a query job without cache', async () => {
81-
const req = client.queryFromSQL(
81+
const req = client.fromSQL(
8282
'SELECT CURRENT_TIMESTAMP() as foo, SESSION_USER() as bar',
8383
);
8484
req.queryRequest!.jobCreationMode =

system-test/query/value.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describeWithBothTransports('Read Query Values', client => {
2222
describe('types', () => {
2323
for (const tc of queryParameterTestCases()) {
2424
it(tc.name, async () => {
25-
const req = client.queryFromSQL(tc.query);
25+
const req = client.fromSQL(tc.query);
2626
req.queryRequest!.queryParameters = tc.parameters;
2727

2828
const q = await client.startQuery(req);
@@ -40,7 +40,7 @@ describeWithBothTransports('Read Query Values', client => {
4040
});
4141

4242
it('should read nested objects', async () => {
43-
const req = client.queryFromSQL(
43+
const req = client.fromSQL(
4444
"SELECT 40 as age, [STRUCT(STRUCT('1' as a, '2' as b) as object)] as nested",
4545
);
4646
const q = await client.startQuery(req);

0 commit comments

Comments
 (0)