Skip to content

Commit 0cc847f

Browse files
committed
Cleanup execute endpoints
1 parent 9b3f155 commit 0cc847f

File tree

1 file changed

+61
-48
lines changed

1 file changed

+61
-48
lines changed

lib/client.js

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -319,56 +319,50 @@ class Client extends events.EventEmitter {
319319
await this.#connect();
320320
}
321321

322-
return new Promise((resolve, reject) => {
323-
this.#executePromise(resolve, reject, query, params, execOptions);
324-
});
325-
}
322+
let rustOptions = execOptions.getRustOptions();
323+
let result;
326324

327-
/**
328-
* Core part of executing rust queries
329-
* @param {ResolveCallback} resolve
330-
* @param {RejectCallback} reject
331-
* @param {string | rust.PreparedStatementWrapper} query
332-
* @param {Array} params
333-
* @param {ExecOptions.ExecutionOptions} execOptions
334-
*/
335-
async #executePromise(resolve, reject, query, params, execOptions) {
336-
try {
337-
let rustOptions = execOptions.getRustOptions();
338-
let result;
339-
if (execOptions.isPrepared()) {
340-
// Execute prepared statement, as requested by the user
341-
let statement =
342-
query instanceof rust.PreparedStatementWrapper
343-
? query
344-
: await this.rustClient.prepareStatement(query);
345-
let parsedParams = parseParams(
346-
statement.getExpectedTypes(),
347-
params,
348-
);
349-
result = await this.rustClient.executePreparedUnpaged(
350-
statement,
351-
parsedParams,
352-
rustOptions,
353-
);
354-
} else {
355-
if (query instanceof rust.PreparedStatementWrapper) {
356-
throw new Error(
357-
"Unexpected prepared statement wrapper for unprepared queries",
358-
);
359-
}
360-
let expectedTypes = convertHints(execOptions.getHints() || []);
361-
let parsedParams = parseParams(expectedTypes, params, true);
362-
result = await this.rustClient.queryUnpaged(
363-
query,
364-
parsedParams,
365-
rustOptions,
325+
if (execOptions.isPrepared()) {
326+
// If the statement is already prepared, skip the preparation process
327+
// Otherwise call Rust part to prepare a statement
328+
let statement =
329+
query instanceof rust.PreparedStatementWrapper
330+
? query
331+
: await this.rustClient.prepareStatement(query);
332+
333+
// Parse parameters according to expected types
334+
let parsedParams = parseParams(
335+
statement.getExpectedTypes(),
336+
params,
337+
false,
338+
);
339+
340+
// Execute query
341+
result = await this.rustClient.executePreparedUnpaged(
342+
statement,
343+
parsedParams,
344+
rustOptions,
345+
);
346+
} else {
347+
// We do not accept already prepared statements for unprepared queries
348+
if (query instanceof rust.PreparedStatementWrapper) {
349+
throw new Error(
350+
"Unexpected prepared statement wrapper for unprepared queries",
366351
);
367352
}
368-
resolve(new ResultSet(result));
369-
} catch (e) {
370-
reject(e);
353+
// Get expected types from user provided hints
354+
let expectedTypes = convertHints(execOptions.getHints() || []);
355+
// Parse parameters according to provided hints, with type guessing
356+
let parsedParams = parseParams(expectedTypes, params, true);
357+
358+
// Execute query
359+
result = await this.rustClient.queryUnpaged(
360+
query,
361+
parsedParams,
362+
rustOptions,
363+
);
371364
}
365+
return new ResultSet(result);
372366
}
373367

374368
/**
@@ -523,28 +517,47 @@ class Client extends events.EventEmitter {
523517
const rustOptions = queryOptions.queryOptionsIntoWrapper(execOptions);
524518
let result;
525519
if (execOptions.isPrepared()) {
526-
// Execute prepared statement, as requested by the user
527-
const statement = await this.rustClient.prepareStatement(query);
520+
// If the statement is already prepared, skip the preparation process
521+
// Otherwise call Rust part to prepare a statement
522+
let statement =
523+
query instanceof rust.PreparedStatementWrapper
524+
? query
525+
: await this.rustClient.prepareStatement(query);
526+
527+
// Parse parameters according to expected types
528528
let parsedParams = parseParams(
529529
statement.getExpectedTypes(),
530530
params,
531531
);
532+
533+
// Execute query
532534
result = await this.rustClient.executeSinglePage(
533535
statement,
534536
parsedParams,
535537
rustOptions,
536538
pageState,
537539
);
538540
} else {
541+
// We do not accept already prepared statements for unprepared queries
542+
if (query instanceof rust.PreparedStatementWrapper) {
543+
throw new Error(
544+
"Unexpected prepared statement wrapper for unprepared queries",
545+
);
546+
}
547+
// Get expected types from user provided hints
539548
const expectedTypes = convertHints(execOptions.getHints() || []);
549+
// Parse parameters according to provided hints, with type guessing
540550
const parsedParams = parseParams(expectedTypes, params, true);
551+
// Execute query
541552
result = await this.rustClient.querySinglePage(
542553
query,
543554
parsedParams,
544555
rustOptions,
545556
pageState,
546557
);
547558
}
559+
// result[0] - information about page state
560+
// result[1] - object representing result itself
548561
result[1] = new ResultSet(result[1], result[0]);
549562
return result;
550563
}

0 commit comments

Comments
 (0)