|
1 | 1 | import { AccountNotFoundError, ApiError } from "../common/errors"; |
2 | 2 | import { ACCOUNT_SYSTEM_ENGINE, QUERY_URL } from "../common/api"; |
3 | 3 |
|
4 | | -import { Connection as BaseConnection } from "./base"; |
| 4 | +import { Connection as BaseConnection, defaultResponseSettings } from "./base"; |
5 | 5 | import { Cache, inMemoryCache, noneCache } from "../common/tokenCache"; |
| 6 | +import { ExecuteQueryOptions } from "../types"; |
| 7 | +import { AsyncStatement } from "../statement/async"; |
6 | 8 |
|
7 | 9 | export class ConnectionV2 extends BaseConnection { |
8 | 10 | private get account(): string { |
@@ -70,6 +72,93 @@ export class ConnectionV2 extends BaseConnection { |
70 | 72 | return this.engineEndpoint; |
71 | 73 | } |
72 | 74 |
|
| 75 | + // Async methods |
| 76 | + async executeAsync( |
| 77 | + query: string, |
| 78 | + executeQueryOptions: ExecuteQueryOptions = {} |
| 79 | + ): Promise<AsyncStatement> { |
| 80 | + const { httpClient } = this.context; |
| 81 | + |
| 82 | + executeQueryOptions.response = { |
| 83 | + ...defaultResponseSettings, |
| 84 | + ...(executeQueryOptions.response ?? {}) |
| 85 | + }; |
| 86 | + |
| 87 | + const { parameters, namedParameters } = executeQueryOptions; |
| 88 | + |
| 89 | + let formattedQuery: string; |
| 90 | + if (this.queryFormatter.isSetStatement(query)) { |
| 91 | + // can't have an async set query |
| 92 | + throw new Error("SET statements cannot be executed asynchronously."); |
| 93 | + } else { |
| 94 | + formattedQuery = this.queryFormatter.formatQuery( |
| 95 | + query, |
| 96 | + parameters, |
| 97 | + namedParameters |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + const body = formattedQuery; |
| 102 | + const asyncExecuteQueryOptions = { |
| 103 | + ...executeQueryOptions, |
| 104 | + settings: { |
| 105 | + ...executeQueryOptions.settings, |
| 106 | + async: true |
| 107 | + } |
| 108 | + }; |
| 109 | + const url = this.getRequestUrl(asyncExecuteQueryOptions); |
| 110 | + |
| 111 | + const request = httpClient.request<Response>("POST", url, { |
| 112 | + headers: { "user-agent": this.userAgent }, |
| 113 | + body, |
| 114 | + raw: true |
| 115 | + }); |
| 116 | + |
| 117 | + const response = await request.ready(); |
| 118 | + const text = await response.text(); |
| 119 | + await this.throwErrorIfErrorBody(text, response); |
| 120 | + return new AsyncStatement(this.context, { |
| 121 | + query: formattedQuery, |
| 122 | + text, |
| 123 | + executeQueryOptions: asyncExecuteQueryOptions |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + private async getAsyncQueryInfo(token: string) { |
| 128 | + const query = `CALL fb_GetAsyncStatus('${token}')`; |
| 129 | + |
| 130 | + const statement = await this.execute(query); |
| 131 | + const { data, meta } = await statement.fetchResult(); |
| 132 | + const result: Record<string, any> = {}; |
| 133 | + if (data.length > 0) { |
| 134 | + meta.forEach((field, index) => { |
| 135 | + result[field.name] = data[0][index]; |
| 136 | + }); |
| 137 | + } else { |
| 138 | + throw new Error("No data returned from fb_GetAsyncStatus"); |
| 139 | + } |
| 140 | + return result; |
| 141 | + } |
| 142 | + |
| 143 | + async isAsyncQueryRunning(token: string): Promise<boolean> { |
| 144 | + const info = await this.getAsyncQueryInfo(token); |
| 145 | + return info["status"] === "RUNNING"; |
| 146 | + } |
| 147 | + |
| 148 | + async isAsyncQuerySuccessful(token: string): Promise<boolean | undefined> { |
| 149 | + const info = await this.getAsyncQueryInfo(token); |
| 150 | + if (info["status"] === "RUNNING") { |
| 151 | + return undefined; |
| 152 | + } |
| 153 | + return info["status"] === "ENDED_SUCCESSFULLY"; |
| 154 | + } |
| 155 | + |
| 156 | + async cancelAsyncQuery(token: string): Promise<void> { |
| 157 | + const info = await this.getAsyncQueryInfo(token); |
| 158 | + const async_query_id = info["query_id"]; |
| 159 | + this.execute(`CANCEL QUERY WHERE query_id='${async_query_id}'`); |
| 160 | + } |
| 161 | + |
73 | 162 | async testConnection() { |
74 | 163 | const settings = { internal: [{ auto_start_stop_control: "ignore" }] }; |
75 | 164 | await this.execute("select 1", { settings }); |
|
0 commit comments