* Executes a query plan and returns the result. * * @param queryPlan - The query plan to execute * @param scope - Placeholder values for the query * @param comments - Pre-computed SQL commenter tags from the client * @param resourceLimits - Resource limits for the query * @param tr
(
queryPlan: QueryPlanNode,
scope: Record<string, unknown>,
comments: Record<string, string> | undefined,
resourceLimits: ResourceLimits,
transactionId: string | null,
)
| 83 | * @param transactionId - Transaction ID if running within a transaction |
| 84 | */ |
| 85 | async query( |
| 86 | queryPlan: QueryPlanNode, |
| 87 | scope: Record<string, unknown>, |
| 88 | comments: Record<string, string> | undefined, |
| 89 | resourceLimits: ResourceLimits, |
| 90 | transactionId: string | null, |
| 91 | ): Promise<unknown> { |
| 92 | const queryable = |
| 93 | transactionId !== null ? await this.#transactionManager.getTransaction({ id: transactionId }, 'query') : this.#db |
| 94 | |
| 95 | // Create constant commenter plugin if comments were provided |
| 96 | const sqlCommenter: QueryInterpreterSqlCommenter | undefined = |
| 97 | comments && Object.keys(comments).length > 0 |
| 98 | ? { |
| 99 | plugins: [() => comments], |
| 100 | // For pre-computed comments, we use a placeholder queryInfo since the actual |
| 101 | // query info was already used on the client side to compute the comments |
| 102 | queryInfo: { type: 'single', action: 'queryRaw', query: {} }, |
| 103 | } |
| 104 | : undefined |
| 105 | |
| 106 | const result = await Promise.race([ |
| 107 | this.#interpreter.run(queryPlan, { |
| 108 | queryable, |
| 109 | transactionManager: |
| 110 | transactionId === null ? { enabled: true, manager: this.#transactionManager } : { enabled: false }, |
| 111 | scope, |
| 112 | sqlCommenter, |
| 113 | }), |
| 114 | timers.setTimeout(resourceLimits.queryTimeout.total('milliseconds'), undefined, { ref: false }).then(() => { |
| 115 | throw new ResourceLimitError('Query timeout exceeded') |
| 116 | }), |
| 117 | ]) |
| 118 | |
| 119 | return normalizeJsonProtocolValues(result) |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Starts a new transaction. |