| 52 | |
| 53 | /** SQL Server executes native T-SQL remotely; no rows are imported into DuckDB. */ |
| 54 | export class SQLServerEngine implements AnalysisEngine { |
| 55 | private pool: ConnectionPool | null = null; |
| 56 | private sourceSchema: string | undefined; |
| 57 | private readonly cancelRequests = new Set<() => void>(); |
| 58 | readonly dialect: TSQLDialect; |
| 59 | |
| 60 | constructor(llmConfig: LLMConfig) { |
| 61 | this.dialect = new TSQLDialect(llmConfig); |
| 62 | } |
| 63 | |
| 64 | async load(configs: DataSourceConfig | DataSourceConfig[]): Promise<void> { |
| 65 | const sources = Array.isArray(configs) ? configs : [configs]; |
| 66 | if (sources.length !== 1) { |
| 67 | throw new Error(`SQLServerEngine currently supports exactly one data source, received ${sources.length}`); |
| 68 | } |
| 69 | const config = sources[0]; |
| 70 | await this.dispose(); |
| 71 | if (config.type !== 'sqlserver') { |
| 72 | throw new Error(`SQLServerEngine only supports 'sqlserver' data sources, got '${config.type}'`); |
| 73 | } |
| 74 | // eslint-disable-next-line import/no-named-as-default-member -- see the comment on the mssql import |
| 75 | this.pool = new mssql.ConnectionPool(connectionConfig(config.options)); |
| 76 | // Request promises report transport errors. A pool-level listener also prevents |
| 77 | // idle-connection errors from becoming unhandled EventEmitter exceptions. |
| 78 | this.pool.on('error', () => undefined); |
| 79 | try { |
| 80 | await this.pool.connect(); |
| 81 | this.sourceSchema = config.options.schema; |
| 82 | } catch (error) { |
| 83 | await this.dispose(); |
| 84 | throw error; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | async schema(signal?: AbortSignal): Promise<Schema> { |
| 89 | if (!this.pool) throw new Error('No data loaded. Please call source() first.'); |
| 90 | return getSQLServerSchema(async (query) => (await this.runQuery(query, signal)).recordset, this.sourceSchema); |
| 91 | } |
| 92 | |
| 93 | async profile(schema: Schema, options: ProfileOptions = {}, signal?: AbortSignal): Promise<Profile> { |
| 94 | if (!this.pool) throw new Error('No data loaded. Please call source() first.'); |
| 95 | throwIfAborted(signal); |
| 96 | return profileTables( |
| 97 | async (query) => (await this.runQuery(query, signal)).recordset, |
| 98 | schema, |
| 99 | parseProfileOptions(options, { metrics: DEFAULT_METRICS }) |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | async getDSL(query: string, dataModel: DataModel, signal?: AbortSignal): Promise<TranslationResult> { |
| 104 | if (!this.pool) throw new Error('No data loaded. Please call source() first.'); |
| 105 | return signal ? this.dialect.getDSL(query, dataModel, signal) : this.dialect.getDSL(query, dataModel); |
| 106 | } |
| 107 | |
| 108 | async execute<T = Record<string, unknown>>( |
| 109 | dsl: string, |
| 110 | options?: ExecutionOptions, |
| 111 | signal?: AbortSignal |
nothing calls this directly
no outgoing calls
no test coverage detected