dt-sql-parser
Version: 
SQL Parsers for BigData, built with antlr4
133 lines (132 loc) • 5.42 kB
TypeScript
import { ParserRuleContext, Token } from 'antlr4ng';
import { TextPosition, WordPosition, WordRange } from './textAndWord';
import { EntityContextType } from './types';
/**
 * TODO: more stmt type should be supported.
 */
export declare enum StmtContextType {
    /** A self-contained and complete statement */
    COMMON_STMT = "commonStmt",
    CREATE_CATALOG_STMT = "createCatalogStmt",
    CREATE_DATABASE_STMT = "createDatabaseStmt",
    CREATE_TABLE_STMT = "createTableStmt",
    CREATE_VIEW_STMT = "createViewStmt",
    SELECT_STMT = "selectStmt",
    INSERT_STMT = "insertStmt",
    CREATE_FUNCTION_STMT = "createFunctionStmt",
    ALTER_TABLE_STMT = "alterTableStmt"
}
export interface StmtContext {
    readonly stmtContextType: StmtContextType;
    readonly position: TextPosition;
    readonly rootStmt: StmtContext | null;
    readonly parentStmt: StmtContext | null;
    readonly isContainCaret?: boolean;
}
export declare function toStmtContext(ctx: ParserRuleContext, type: StmtContextType, input: string, rootStmt: StmtContext | null, parentStmt: StmtContext | null, isContainCaret?: boolean): StmtContext | null;
/**
 * entity's attribute
 * @key comment: entity's comment attribute
 * @key colType: column entity's type attribute
 * @key alias: entity's alias attribute
 * */
export declare enum AttrName {
    comment = "_comment",
    colType = "_colType",
    alias = "_alias"
}
/**
 * ParserRuleContext with custom attributes
 * */
type ParserRuleContextWithAttr = ParserRuleContext & {
    [K in AttrName]?: Token;
};
/**
 * Function's arguments
 * */
export interface Argument {
    argMode?: WordRange;
    argName?: WordRange;
    argType: WordRange;
}
export interface BaseEntityContext {
    readonly entityContextType: EntityContextType;
    readonly text: string;
    readonly position: WordPosition;
    readonly belongStmt: StmtContext;
    reference?: string | EntityContext;
    [AttrName.comment]: WordRange | null;
    [AttrName.alias]?: WordRange | null;
}
export interface CommonEntityContext extends BaseEntityContext {
    relatedEntities: CommonEntityContext[] | null;
    columns?: ColumnEntityContext[];
}
export interface ColumnEntityContext extends BaseEntityContext {
    [AttrName.colType]: WordRange | null;
}
export interface FuncEntityContext extends BaseEntityContext {
    relatedEntities: CommonEntityContext[] | null;
    arguments: Argument[] | null;
    returns?: Argument;
}
export type EntityContext = CommonEntityContext | FuncEntityContext | ColumnEntityContext;
export declare function isCommonEntityContext(entity: EntityContext): entity is CommonEntityContext;
export declare function isFuncEntityContext(entity: EntityContext): entity is FuncEntityContext;
export declare function isColumnEntityContext(entity: EntityContext): entity is ColumnEntityContext;
/**
 *  what we need when collect attribute information
 * */
interface AttrInfo {
    attrName: AttrName;
    endContextList: string[];
}
export declare function toEntityContext(ctx: ParserRuleContext, type: EntityContextType, input: string, belongStmt: StmtContext, attrInfo?: AttrInfo[]): EntityContext | null;
export declare function findAttribute(ctx: ParserRuleContextWithAttr | null, keyName: AttrName, endContextNameList: string[]): Token | null;
/**
 * @todo: Handle alias, includes column alias, table alias, query as alias and so on.
 * @todo: [may be need] Combine the entities in each clause.
 */
export declare abstract class EntityCollector {
    constructor(input: string, allTokens?: Token[], caretTokenIndex?: number);
    private readonly _input;
    private readonly _allTokens;
    private readonly _caretTokenIndex;
    private readonly _entitiesSet;
    /** Staging statements that have already entered. */
    private readonly _stmtStack;
    /** Staging entities inside a single statement or clause. */
    private readonly _entityStack;
    /**
     * Always point to the first non-commonStmt at the bottom of the _stmtStack,
     * unless there are only commonStmts in the _stmtStack.
     * */
    private _rootStmt;
    visitTerminal(): void;
    visitErrorNode(): void;
    enterEveryRule(): void;
    exitEveryRule(): void;
    getRootStmt(): StmtContext | null;
    getEntities(): EntityContext[];
    enterProgram(): void;
    /**
     * The antlr4 will ignore hidden tokens, if we type whitespace at the end of a statement,
     * the whitespace token will not as stop token, so we consider the whitespace token as a part of the nonhidden token in front of it
     */
    protected getPrevNonHiddenTokenIndex(caretTokenIndex: number): number;
    protected pushStmt(ctx: ParserRuleContext, type: StmtContextType): StmtContext | null;
    protected popStmt(): StmtContext;
    protected pushEntity(ctx: ParserRuleContext, type: EntityContextType, attrInfo?: AttrInfo[]): EntityContext | null;
    /**
     * Combine entities that inside a single statement.
     * e.g. combine tableName and column if they are inside a same createTableStatement.
     * Then add combined entities into result.
     */
    private combineEntitiesAndAdd;
    /**
     * Combined all entities under a rootStmt.
     */
    protected combineRootStmtEntities(stmtContext: StmtContext, entitiesInsideStmt: EntityContext[]): EntityContext[];
    protected combineCreateTableOrViewStmtEntities(stmtContext: StmtContext, entitiesInsideStmt: EntityContext[]): EntityContext[];
}
export {};