UNPKG

rhombic

Version:

SQL parsing, lineage extraction and manipulation

135 lines 4.85 kB
import { ErrorNode } from "antlr4ts/tree/ErrorNode"; import { TablePrimaryIncomplete } from ".."; import { QuotableIdentifier } from "./common"; import { CursorQuery } from "./Cursor"; import { QueryRelation, QueryStructureVisitor, TableRelation } from "./QueryStructureVisitor"; import { AliasedRelationContext, ColumnReferenceContext, DereferenceContext, PrimaryExpressionContext, QueryTermDefaultContext, TableNameContext } from "./SqlBaseParser"; import { SqlBaseVisitor } from "./SqlBaseVisitor"; /** * Completion suggestions in a given context. */ export declare type ContextCompletions<C> = { type: "column"; columns: { relation?: string; name: string; desc?: C; }[]; } | { type: "relation"; incompleteReference?: TablePrimaryIncomplete; relations: string[]; } | { type: "other"; }; /** * A snippet suggestion to be used in monaco */ export interface Snippet { label: string; template: string; } /** * Contains a list of available snippets */ export interface Snippets { snippets: Snippet[]; } /** * Completion suggestions in a specific context + snippets */ export declare type Completions<C> = ContextCompletions<C> & Snippets; /** * The visitor responsible for computing possible completion items based on the cursor position. * * This class performs two related tasks: identifying the subquery (and part of the query) where * the cursor is found in and collecting relevant details for completion at that point, such as * visible CTEs and columns of relations in the `FROM` clause. * * The cursor handling is delegated to the `Cursor` instance that is provided in the constructor. */ export declare class CompletionVisitor<C extends { name: string; }> extends QueryStructureVisitor<void> implements SqlBaseVisitor<void> { private readonly cursor; /** * @param cursor Utility functions to identify the cursor within the query * @param getTables Fetch a list of available tables (to present as completion items) * @param getTable Fetch details (such as available columns) for a table */ constructor(cursor: CursorQuery, getColumns: (arg: { table: string; catalogOrSchema?: string; schema?: string; }) => C[]); defaultResult(): void; aggregateResult(_cur: void, _next: void): void; private caretScope?; private hasCompletions; private completions; getSuggestions(): Completions<C>; /** * Checks if tyhe provided query is the innermost query containing the cursor. * If so, compute the completion suggestions for that query. * * @param relation the current relation to consider */ private updateCompletionItems; /** * In case a query has been completely handled by this visitor we may need to update the completions. * * This is called for the whole query (including query organization features). For simple query terms * (like the individual parts of a UNION query) are handled by the `visitQueryTermDefault` method. * * @param relation * @param _alias * @returns */ onRelation(relation: TableRelation | QueryRelation, _alias?: string): void; /** * In case we handled a query term we need to potentially update the completions. * * This is called for query terms e.g. within set operations. We need this in addition * to `onRelation` to make sure we can complete in the individual sub-queries of set * operations. * * @param ctx */ visitQueryTermDefault(ctx: QueryTermDefaultContext): void; /** * This method handles the case of an empty input to then provide the SELECTFROM snippet as * a completion item. * * @param node */ visitErrorNode(node: ErrorNode): void; /** * Handle cases where the cursor is at the end of a table name (prefix). * * @param ctx */ visitTableName(ctx: TableNameContext): void; /** * Handle cases where the cursor is in the position of a table name, with no prefix provided. * * @param ctx */ visitAliasedRelation(ctx: AliasedRelationContext): void; /** * Handle cases where the cursor is in the position of a column. We distinguish between * the cursor in a SELECT clause and in any other position (e.h. WHERE clause). * @param ctx */ visitColumnReference(ctx: ColumnReferenceContext): void; /** * Handle cases, where the cursor is right after a dot to suggest columns for the relevant prefix. * * @param ctx */ visitDereference(ctx: DereferenceContext): void; protected extractTableAndColumn(ctx: PrimaryExpressionContext): { table?: QuotableIdentifier; column: QuotableIdentifier; } | undefined; } //# sourceMappingURL=CompletionVisitor.d.ts.map