lava-dt-sql-parser-semantic.../src/parse/index.ts

24 lines
800 B
TypeScript
Raw Normal View History

import { PostgreSQL } from 'dt-sql-parser'
2024-04-07 19:10:38 +08:00
import { SQLVisitor } from './visitor'
import { type SQLParseResult } from '../types'
import { type BasicSQL } from 'dt-sql-parser/dist/parser/common/basicSQL'
2024-04-07 19:10:38 +08:00
export function parse (
sql: string,
parser: BasicSQL = new PostgreSQL(),
2024-04-07 19:10:38 +08:00
stmts: string[] = [],
entities: string[] = [],
rules: Record<string, number[]> = {},
alias: Record<string, string> = {},
2024-04-07 19:10:38 +08:00
): SQLParseResult {
const tree = parser.parse(sql)
console.log('tree', tree)
const visitor = new SQLVisitor()
visitor.visitorAlias = alias
2024-04-07 19:10:38 +08:00
stmts.forEach(stmt => { visitor.addStmt(stmt) })
entities.forEach(entity => { visitor.addEntity(entity) })
Object.keys(rules).forEach(name => { visitor.addRules(name, rules[name]) })
visitor.visit(tree)
return visitor.getResult()
}