import { DataTable, DbSchemaNode, DuckDbConnector, QualifiedTableName, QueryHandle } from '@sqlrooms/duckdb-core'; import { BaseRoomStoreState } from '@sqlrooms/room-store'; import * as arrow from 'apache-arrow'; import { StateCreator } from 'zustand'; export type SchemaAndDatabase = { schema?: string; database?: string; }; /** * State and actions for the DuckDB slice */ export type DuckDbSliceState = { db: { /** * The DuckDB connector instance */ connector: DuckDbConnector; /** * @deprecated We shouldn't limit the schema to a single one. */ schema: string; currentSchema: string | undefined; currentDatabase: string | undefined; /** * Cache of refreshed table schemas */ tables: DataTable[]; /** * Cache of row counts for tables */ tableRowCounts: { [tableName: string]: number; }; /** * Cache of schema trees for tables */ schemaTrees?: DbSchemaNode[]; /** * Cache of currently running query handles. * This is only used for running queries to deduplicate them (especially for useSql), * the cache is cleared when the query is completed. */ queryCache: { [key: string]: QueryHandle; }; /** * Whether the table schemas are being refreshed */ isRefreshingTableSchemas: boolean; /** * Set a new DuckDB connector */ setConnector: (connector: DuckDbConnector) => void; /** * Initialize the connector (creates a WasmDuckDbConnector if none exists) */ initialize: () => Promise; /** * Close and clean up the connector */ destroy: () => Promise; /** * Add a table to the room. * @param tableName - The name of the table to add. * @param data - The data to add to the table: an arrow table or an array of records. * @returns A promise that resolves to the table that was added. */ addTable(tableName: string | QualifiedTableName, data: arrow.Table | Record[]): Promise; /** * Load the schemas of the tables in the database. */ loadTableSchemas(filter?: SchemaAndDatabase & { table?: string; }): Promise; /** * @deprecated Use findTableByName instead */ getTable(tableName: string): DataTable | undefined; /** * @internal Avoid using this directly, it's for internal use. */ setTableRowCount(tableName: string | QualifiedTableName, rowCount: number): void; /** * Find a table by name in the last refreshed table schemas. * If no schema or database is provided, the table will be found in the current schema * and database (from last table schemas refresh). * @param tableName - The name of the table to find or a qualified table name. * @returns The table or undefined if not found. */ findTableByName(tableName: string | QualifiedTableName): DataTable | undefined; /** * Refresh table schemas from the database. * @returns A promise that resolves to the updated tables. */ refreshTableSchemas(): Promise; /** * Get the connector. If it's not initialized, it will be initialized. */ getConnector: () => Promise; /** * @deprecated Use .loadTableRowCount() instead */ getTableRowCount: (table: string, schema?: string) => Promise; /** * Load the row count of a table */ loadTableRowCount: (tableName: string | QualifiedTableName) => Promise; /** * Execute a query with query handle (not result) caching and deduplication * @param query - The SQL query to execute * @returns The QueryHandle for the query or null if disabled */ executeSql: (query: string) => Promise; /** * @deprecated Use .tables or .loadTableSchemas() instead */ getTables: (schema?: string) => Promise; /** * @deprecated Use .loadTableSchemas() instead */ getTableSchema: (tableName: string, schema?: string) => Promise; /** * @deprecated Use .tables or .loadTableSchemas() instead */ getTableSchemas: (schema?: string) => Promise; /** * Check if a table exists */ checkTableExists: (tableName: string | QualifiedTableName) => Promise; /** * Delete a table with optional schema and database * @param tableName - The name of the table to delete (qualified or plain) */ dropTable: (tableName: string | QualifiedTableName) => Promise; /** * Create a table or view from a query. * @param tableName - The name of the table/view to create. * @param query - The query to create the table/view from. * @param options - Creation options. * @returns The table/view name and rowCount (undefined for views). */ createTableFromQuery: (tableName: string | QualifiedTableName, query: string, options?: { replace?: boolean; temp?: boolean; view?: boolean; allowMultipleStatements?: boolean; abortSignal?: AbortSignal; }) => Promise<{ tableName: string | QualifiedTableName; rowCount: number | undefined; }>; /** * Parse a SQL SELECT statement to JSON * @param sql - The SQL SELECT statement to parse. * @returns A promise that resolves to the parsed JSON. */ sqlSelectToJson: (sql: string) => Promise<{ error: true; error_type: string; error_message: string; error_subtype: string; position: string; } | { error: false; statements: { node: { from_table: { alias: string; show_type: string; table_name: string; }; select_list: Record[]; type: string; }; }[]; }>; }; }; type CreateDuckDbSliceProps = { connector?: DuckDbConnector; }; /** * Create a DuckDB slice for managing the connector */ export declare function createDuckDbSlice({ connector, }?: CreateDuckDbSliceProps): StateCreator; /** * @internal * Select values from the room store that includes the DuckDB slice. * * This is a typed wrapper around `useBaseRoomStore` that narrows the * state to `RoomStateWithDuckDb` so selectors can access `db` safely. * * @typeParam T - The selected slice of state returned by the selector * @param selector - Function that selects a value from the store state * @returns The selected value of type `T` */ export declare function useStoreWithDuckDb(selector: (state: BaseRoomStoreState & DuckDbSliceState) => T): T; export {}; //# sourceMappingURL=DuckDbSlice.d.ts.map