33 */
44
55import { assertStoreExists , requireStorePath } from "./file-store"
6- import type { ConnectionConfig , Connector , ConnectorResult , ExecuteOptions , SchemaColumn } from "./types"
6+ import type { ConnectionConfig , Connector , ConnectorResult , ExecuteOptions , SchemaColumn , SchemaMetadata } from "./types"
77import { loadOptionalDriver } from "./resolve"
88
99// altimate_change start — configurable, generous open budget
@@ -101,15 +101,34 @@ export async function connect(config: ConnectionConfig): Promise<Connector> {
101101 }
102102 // altimate_change end
103103
104- function query ( sql : string ) : Promise < any [ ] > {
104+ function query ( sql : string , signal ?: AbortSignal ) : Promise < any [ ] > {
105105 return new Promise ( ( resolve , reject ) => {
106- connection . all ( sql , ( err : Error | null , rows : any [ ] ) => {
107- if ( err ) reject ( wrapDuckDBError ( err ) )
108- else resolve ( rows ?? [ ] )
109- } )
106+ signal ?. throwIfAborted ( )
107+ // The legacy DuckDB SDK's interrupt() is a no-op. Stop waiting and avoid
108+ // subsequent work on abort; the native query still finishes in the SDK.
109+ const onAbort = ( ) => reject ( signal ! . reason )
110+ signal ?. addEventListener ( "abort" , onAbort , { once : true } )
111+ try {
112+ connection . all ( sql , ( err : Error | null , rows : any [ ] ) => {
113+ signal ?. removeEventListener ( "abort" , onAbort )
114+ if ( err ) reject ( wrapDuckDBError ( err ) )
115+ else resolve ( rows ?? [ ] )
116+ } )
117+ } catch ( err ) {
118+ signal ?. removeEventListener ( "abort" , onAbort )
119+ reject ( err )
120+ }
110121 } )
111122 }
112123
124+ async function schemaNames ( signal ?: AbortSignal ) : Promise < string [ ] > {
125+ const rows = await query (
126+ "SELECT DISTINCT schema_name FROM information_schema.schemata WHERE catalog_name = current_database() ORDER BY schema_name" ,
127+ signal ,
128+ )
129+ return rows . map ( ( r ) => r . schema_name as string )
130+ }
131+
113132 function queryWithParams ( sql : string , params : any [ ] ) : Promise < any [ ] > {
114133 return new Promise ( ( resolve , reject ) => {
115134 connection . all ( sql , ...params , ( err : Error | null , rows : any [ ] ) => {
@@ -282,17 +301,14 @@ export async function connect(config: ConnectionConfig): Promise<Connector> {
282301 } ,
283302
284303 async listSchemas ( ) : Promise < string [ ] > {
285- const rows = await query (
286- "SELECT schema_name FROM information_schema.schemata ORDER BY schema_name" ,
287- )
288- return rows . map ( ( r ) => r . schema_name as string )
304+ return schemaNames ( )
289305 } ,
290306
291307 async listTables (
292308 schema : string ,
293309 ) : Promise < Array < { name : string ; type : string } > > {
294310 const rows = await queryWithParams (
295- `SELECT table_name, table_type FROM information_schema.tables WHERE table_schema = $1 ORDER BY table_name` ,
311+ `SELECT table_name, table_type FROM information_schema.tables WHERE table_catalog = current_database() AND table_schema = $1 ORDER BY table_name` ,
296312 [ schema ] ,
297313 )
298314 return rows . map ( ( r ) => ( {
@@ -306,7 +322,7 @@ export async function connect(config: ConnectionConfig): Promise<Connector> {
306322 table : string ,
307323 ) : Promise < SchemaColumn [ ] > {
308324 const rows = await queryWithParams (
309- `SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_schema = $1 AND table_name = $2 ORDER BY ordinal_position` ,
325+ `SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_catalog = current_database() AND table_schema = $1 AND table_name = $2 ORDER BY ordinal_position` ,
310326 [ schema , table ] ,
311327 )
312328 return rows . map ( ( r ) => ( {
@@ -316,6 +332,43 @@ export async function connect(config: ConnectionConfig): Promise<Connector> {
316332 } ) )
317333 } ,
318334
335+ async introspectSchema ( signal ?: AbortSignal ) : Promise < SchemaMetadata > {
336+ const schemas = await schemaNames ( signal )
337+ const rows = await query (
338+ `SELECT t.table_schema, t.table_name, t.table_type,
339+ c.column_name, c.data_type, c.is_nullable
340+ FROM information_schema.tables t
341+ LEFT JOIN information_schema.columns c
342+ ON c.table_catalog = t.table_catalog
343+ AND c.table_schema = t.table_schema
344+ AND c.table_name = t.table_name
345+ WHERE t.table_catalog = current_database()
346+ ORDER BY t.table_schema, t.table_name, c.ordinal_position` ,
347+ signal ,
348+ )
349+ const tables : SchemaMetadata [ "tables" ] = [ ]
350+ let table : SchemaMetadata [ "tables" ] [ number ] | undefined
351+ for ( const row of rows ) {
352+ if ( ! table || table . schema !== row . table_schema || table . name !== row . table_name ) {
353+ table = {
354+ schema : row . table_schema ,
355+ name : row . table_name ,
356+ type : row . table_type === "VIEW" ? "view" : "table" ,
357+ columns : [ ] ,
358+ }
359+ tables . push ( table )
360+ }
361+ if ( row . column_name !== null ) {
362+ table . columns . push ( {
363+ name : row . column_name ,
364+ data_type : row . data_type ,
365+ nullable : row . is_nullable === "YES" ,
366+ } )
367+ }
368+ }
369+ return { schemas, tables }
370+ } ,
371+
319372 async close ( ) {
320373 if ( db ) {
321374 await new Promise < void > ( ( resolve ) => {
0 commit comments