UNPKG

rhombic

Version:

SQL parsing, lineage extraction and manipulation

184 lines 5.28 kB
import { ProjectionItemsVisitor } from "./visitors/ProjectionItemsVisitor"; import { CstNode } from "chevrotain"; import { Range } from "./utils/getRange"; import { needToBeEscaped } from "./utils/needToBeEscaped"; import { printFilter } from "./utils/printFilter"; import { FilterTree } from "./FilterTree"; export { default as antlr } from "./antlr"; export { LineageHelper } from "./LineageHelper"; export { needToBeEscaped, printFilter }; declare const rhombic: { /** * Parse a sql statement. * * @param sql */ parse(sql: string): ParsedSql; /** * Returns `true` if the query is empty (no executable sql) * * Note: the sql don't have to be valid, we are just checking if everything is commented * @param sql */ isEmpty(sql: string): boolean; /** * Returns `true` if the filter is valid. * * @param filter */ isFilterValid(filter: string): boolean; }; export interface ProjectionItemMetadata { expression: string; alias?: string; cast?: { value: string; type: string; }; fn?: { identifier: string; values: { expression: string; path?: { catalogName?: string; schemaName?: string; tableName?: string; columnName: string; }; }[]; }; sort?: { order: "asc" | "desc"; nullsOrder?: "first" | "last"; }; path?: { catalogName?: string; schemaName?: string; tableName?: string; columnName: string; }; range?: Range; } export interface TablePrimary { catalogName?: string; schemaName?: string; tableName: string; alias?: string; range?: Range; } export interface TablePrimaryIncomplete { references: string[]; } /** * Parsed SQL AST together with various operations that can be applied. */ export interface ParsedSql { /** * Return the sql as a raw string. */ toString(): string; /** * Concrete Syntax Tree. */ cst: CstNode; /** * Returns `true` if the statement has a `FROM`. */ hasFrom(): boolean; /** * Returns `true` if the `tablePrimary` is part of the query. * * @param name */ hasTablePrimary(name: string): boolean; /** * Get all `tablePrimary`. */ getTablePrimaries(): TablePrimary[]; /** * Get a projectionItem from result index. * * @param options * @param options.columns Query columns results, needed to be able to expands `*` * @param options.index Index of the `projectionItem` to rename * * @param internalVisitor used internally by `getProjectionItems` to reused the same visitor (the visitor must already have a valid `output`) */ getProjectionItem(options: { columns: string[]; index: number; }, internalVisitor?: ProjectionItemsVisitor): ProjectionItemMetadata; /** * Get all projectionItems metadata. * * @param columns Query columns results, needed to be able to expands `*` */ getProjectionItems(columns: string[]): ProjectionItemMetadata[]; /** * Add a projectionItem to the query. * * @param projectionItem * @param options * @param options.removeAsterisk Remove `*` from the original query (default: `true`) * @param options.escapeReservedKeywords Escape reserved keywords (default: `true`) * @param options.alias Provide an alias to the projection item (`AS {alias}`) */ addProjectionItem(projectionItem: string, options?: { removeAsterisk?: boolean; escapeReservedKeywords?: boolean; alias?: string; }): ParsedSql; /** * Update a projectionItem. * * @param options * @param options.columns Query columns results, needed to be able to expands `*` * @param options.index Index of the `projectionItem` to rename * @param options.value Replace value for the `projectionItem` */ updateProjectionItem(options: { columns: string[]; index: number; value: string; }): ParsedSql; /** * Remove a projectionItem. * * @param options * @param options.columns Query columns results, needed to be able to expands `*` * @param options.index Index of the `projectionItem` to rename */ removeProjectionItem(options: { columns: string[]; index: number; }): ParsedSql; /** * Add/Update an ORDER BY expression. * * @param options * @param options.expression * @param options.order * @param options.nullsOrder */ orderBy(options: { expression: string; order?: "asc" | "desc"; nullsOrder?: "last" | "first"; }): ParsedSql; /** * Retrieve a UI friendly object that represent the current filter (`WHERE` statement). */ getFilterTree(): FilterTree; /** * Get the filter as a string (`WHERE statement`). */ getFilterString(): string; /** * Update the current filter. * * @param filter */ updateFilter(filter: FilterTree | string): ParsedSql; } export default rhombic; //# sourceMappingURL=index.d.ts.map