(params: {
config: SnowflakeConfig;
statement: string;
bindings?: SnowflakeBinding[];
timeoutSeconds?: number;
signal?: AbortSignal;
})
| 268 | * column-declaration order. |
| 269 | */ |
| 270 | export async function executeSnowflakeStatement(params: { |
| 271 | config: SnowflakeConfig; |
| 272 | statement: string; |
| 273 | bindings?: SnowflakeBinding[]; |
| 274 | timeoutSeconds?: number; |
| 275 | signal?: AbortSignal; |
| 276 | }): Promise<SnowflakeRow[]> { |
| 277 | const token = getOrBuildJwt(params.config); |
| 278 | const requestId = crypto.randomUUID(); |
| 279 | |
| 280 | const url = new URL(`https://${params.config.accountHost}/api/v2/statements`); |
| 281 | url.searchParams.set('requestId', requestId); |
| 282 | |
| 283 | const body: Record<string, unknown> = { |
| 284 | statement: params.statement, |
| 285 | warehouse: params.config.warehouse, |
| 286 | database: params.config.database, |
| 287 | schema: params.config.schema, |
| 288 | role: params.config.role, |
| 289 | }; |
| 290 | |
| 291 | if (params.bindings && params.bindings.length > 0) { |
| 292 | body.bindings = Object.fromEntries(params.bindings.map((b, i) => [String(i + 1), b])); |
| 293 | } |
| 294 | |
| 295 | if (params.timeoutSeconds !== undefined) { |
| 296 | body.timeout = params.timeoutSeconds; |
| 297 | } |
| 298 | |
| 299 | const response = await fetch(url, { |
| 300 | method: 'POST', |
| 301 | headers: { ...authHeaders(token), 'content-type': 'application/json' }, |
| 302 | body: JSON.stringify(body), |
| 303 | signal: params.signal, |
| 304 | }); |
| 305 | |
| 306 | if (response.status === 200) { |
| 307 | const payload = (await response.json()) as SnowflakeApiResponse; |
| 308 | return parseAllRows(params.config, token, payload, params.signal); |
| 309 | } |
| 310 | |
| 311 | if (response.status === 202) { |
| 312 | const payload = (await response.json()) as SnowflakeApiResponse; |
| 313 | if (!payload.statementStatusUrl) { |
| 314 | throw new Error('Snowflake response missing statementStatusUrl'); |
| 315 | } |
| 316 | const completed = await pollStatement( |
| 317 | params.config, |
| 318 | token, |
| 319 | payload.statementStatusUrl, |
| 320 | params.signal |
| 321 | ); |
| 322 | if (completed.code === '090001' || Array.isArray(completed.data)) { |
| 323 | return parseAllRows(params.config, token, completed, params.signal); |
| 324 | } |
| 325 | throw new Error(completed.message ?? 'Snowflake async query failed'); |
| 326 | } |
| 327 |
no test coverage detected