Skip to content

Commit 9632cf6

Browse files
committed
refactor
1 parent 1bb9439 commit 9632cf6

File tree

2 files changed

+30
-39
lines changed

2 files changed

+30
-39
lines changed

src/connection/base.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ export abstract class Connection {
172172

173173
abstract cancelAsyncQuery(token: string): Promise<void>;
174174

175-
async execute(
175+
protected async prepareAndExecuteQuery(
176176
query: string,
177-
executeQueryOptions: ExecuteQueryOptions = {}
178-
): Promise<Statement> {
177+
executeQueryOptions: ExecuteQueryOptions
178+
): Promise<{ formattedQuery: string; response: Response }> {
179179
const { httpClient } = this.context;
180180

181181
executeQueryOptions.response = {
@@ -213,14 +213,8 @@ export abstract class Connection {
213213

214214
try {
215215
const response = await request.ready();
216-
const text = await response.text();
217216
await this.processHeaders(response.headers);
218-
await this.throwErrorIfErrorBody(text, response);
219-
return new Statement(this.context, {
220-
query: formattedQuery,
221-
text,
222-
executeQueryOptions
223-
});
217+
return { formattedQuery, response };
224218
} catch (error) {
225219
// In case it was a set query, remove set parameter if query fails
226220
if (setKey.length > 0) {
@@ -232,6 +226,24 @@ export abstract class Connection {
232226
}
233227
}
234228

229+
async execute(
230+
query: string,
231+
executeQueryOptions: ExecuteQueryOptions = {}
232+
): Promise<Statement> {
233+
const { formattedQuery, response } = await this.prepareAndExecuteQuery(
234+
query,
235+
executeQueryOptions
236+
);
237+
238+
const text = await response.text();
239+
await this.throwErrorIfErrorBody(text, response);
240+
return new Statement(this.context, {
241+
query: formattedQuery,
242+
text,
243+
executeQueryOptions
244+
});
245+
}
246+
235247
protected async throwErrorIfErrorBody(text: string, response: Response) {
236248
// Hack, but looks like this is a limitation of the fetch API
237249
// In order to read the body here and elesewhere, we need to clone the response

src/connection/connection_v2.ts

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -77,44 +77,23 @@ export class ConnectionV2 extends BaseConnection {
7777
query: string,
7878
executeQueryOptions: ExecuteQueryOptions = {}
7979
): 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;
10280
const asyncExecuteQueryOptions = {
10381
...executeQueryOptions,
10482
settings: {
10583
...executeQueryOptions.settings,
10684
async: true
10785
}
10886
};
109-
const url = this.getRequestUrl(asyncExecuteQueryOptions);
11087

111-
const request = httpClient.request<Response>("POST", url, {
112-
headers: { "user-agent": this.userAgent },
113-
body,
114-
raw: true
115-
});
88+
if (this.queryFormatter.isSetStatement(query)) {
89+
// can't have an async set query
90+
throw new Error("SET statements cannot be executed asynchronously.");
91+
}
92+
const { formattedQuery, response } = await this.prepareAndExecuteQuery(
93+
query,
94+
asyncExecuteQueryOptions
95+
);
11696

117-
const response = await request.ready();
11897
const text = await response.text();
11998
await this.throwErrorIfErrorBody(text, response);
12099
return new AsyncStatement(this.context, {

0 commit comments

Comments
 (0)