2024-04-15 17:48:24 +08:00
|
|
|
import { PostgreSQL } from 'dt-sql-parser'
|
2024-04-07 19:10:38 +08:00
|
|
|
import { SQLVisitor } from './visitor'
|
|
|
|
import { type SQLParseResult } from '../types'
|
2024-04-15 17:48:24 +08:00
|
|
|
import { type BasicSQL } from 'dt-sql-parser/dist/parser/common/basicSQL'
|
2024-04-07 19:10:38 +08:00
|
|
|
|
|
|
|
export function parse (
|
|
|
|
sql: string,
|
2024-04-15 17:48:24 +08:00
|
|
|
parser: BasicSQL = new PostgreSQL(),
|
2024-04-07 19:10:38 +08:00
|
|
|
stmts: string[] = [],
|
|
|
|
entities: string[] = [],
|
2024-04-15 17:48:24 +08:00
|
|
|
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()
|
2024-04-15 17:48:24 +08:00
|
|
|
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()
|
|
|
|
}
|