From ad2471da7476af1cfe59ba7d851ff0c63c4a2a0e Mon Sep 17 00:00:00 2001 From: Andrey Avtomonov Date: Mon, 25 May 2026 13:46:22 +0200 Subject: [PATCH] refactor(cli): route local query execution through driver registry --- .../connections/local-query-executor.ts | 39 +++++++++++++++---- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/packages/cli/src/context/connections/local-query-executor.ts b/packages/cli/src/context/connections/local-query-executor.ts index 72cefe2a..3a2e34c9 100644 --- a/packages/cli/src/context/connections/local-query-executor.ts +++ b/packages/cli/src/context/connections/local-query-executor.ts @@ -1,3 +1,4 @@ +import { driverRegistrations, getDriverRegistration } from './drivers.js'; import { createPostgresQueryExecutor } from './postgres-query-executor.js'; import type { KtxSqlQueryExecutionInput, @@ -5,6 +6,7 @@ import type { KtxSqlQueryExecutorPort, } from './query-executor.js'; import { createSqliteQueryExecutor } from './sqlite-query-executor.js'; +import type { KtxConnectionDriver } from '../scan/types.js'; export interface DefaultLocalQueryExecutorOptions { postgres?: KtxSqlQueryExecutorPort; @@ -15,20 +17,43 @@ function driverFor(input: KtxSqlQueryExecutionInput): string { return String(input.connection?.driver ?? '').toLowerCase(); } +function localExecutorMap( + options: DefaultLocalQueryExecutorOptions, +): Partial> { + const wiredExecutors: Partial> = { + postgres: options.postgres ?? createPostgresQueryExecutor(), + sqlite: options.sqlite ?? createSqliteQueryExecutor(), + }; + + const executors: Partial> = {}; + for (const registration of Object.values(driverRegistrations)) { + if (!registration.hasLocalQueryExecutor) continue; + const executor = wiredExecutors[registration.driver]; + if (executor) { + executors[registration.driver] = executor; + } + } + return executors; +} + export function createDefaultLocalQueryExecutor(options: DefaultLocalQueryExecutorOptions = {}): KtxSqlQueryExecutorPort { - const postgres = options.postgres ?? createPostgresQueryExecutor(); - const sqlite = options.sqlite ?? createSqliteQueryExecutor(); + const executors = localExecutorMap(options); return { async execute(input: KtxSqlQueryExecutionInput): Promise { const driver = driverFor(input); - if (driver === 'postgres') { - return postgres.execute(input); + const registration = getDriverRegistration(driver); + if (!registration?.hasLocalQueryExecutor) { + throw new Error(`No local query executor is configured for driver "${input.connection?.driver ?? 'unknown'}".`); } - if (driver === 'sqlite') { - return sqlite.execute(input); + + const executor = executors[registration.driver]; + if (!executor) { + throw new Error( + `Local query executor flag is enabled for driver "${registration.driver}", but no executor factory is wired.`, + ); } - throw new Error(`No local query executor is configured for driver "${input.connection?.driver ?? 'unknown'}".`); + return executor.execute(input); }, }; }