MCPcopy Create free account
hub / github.com/antvis/AVA / SQLServerEngine

Class SQLServerEngine

src/remote/sqlserver/engine.ts:53–137  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected