dt-sql-parser-semantic-analyse-plugin
TypeScript icon, indicating that this package has built-in type declarations

0.0.1-alpha.10 • Public • Published

dt-sql-parser-semantic-analyse-plugin

A dt-sql-parser plugin with semantic result. Theory(zh-CN).

Installation

npm install dt-sql-parser-semantic-analyse-plugin

Quick Usage

const myPlugin = new DtSqlParserSemAnalysePlugin()
const sql = 'SELECT a| FROM t'
const caretColumn = sql.indexOf('|') + 1
const result = myPlugin.parse(sql.replace('|', ''), { lineNumber: 1, columnNumber: caretColumn })
console.log(result)

This will use a postgresql Parser and build-in rules/preprocessors

Add a Rule Chain

import { PostgresSQL } from 'dt-sql-parser'
import { PostgreSQLParser } from 'dt-sql-parser/dist/lib/pgsql/PostgreSQLParser'

const myPlugin = new DtSqlParserSemAnalysePlugin({
  parse: {
    parser: new PostgresSQL()
    alias: {
      selectstmt: 'selectStatement',
      target_el: 'target_label'
    }
    stmts: [
      'selectstmt'
    ],
    entities: [
      'column_name'
    ],
    rules: {
      'select_columns': [
        PostgreSQLParser.RULE_insertstmt,
        PostgreSQLParser.RULE_column_name
      ]      
    }
  }
})
const sql = 'SELECT a| FROM t'
const caretColumn = sql.indexOf('|') + 1
const result = myPlugin.parse(sql.replace('|', ''), { lineNumber: 1, columnNumber: caretColumn })
console.log(result)

Notice: A rule must start with a/an statement/entity and stop with an entity. You should add a node keywords(keyword is in your parser with format: RULE_[keyword]) into stmts/entities before using it.

Rule Chain Opeator

You can set a negative number whose abs equals to a ruleIndex. That means exclude this rule.

Example:

select_target_alias: [
  PostgreSqlParser.RULE_target_el,
  -PostgreSqlParser.RULE_attr_name,
  PostgreSqlParser.RULE_collabel
]

🚧 I will later work on Operator.AND and Operator.OR.

Alias

Some node names in dt-sql-parser code are different from their antlr4's definition.

Example:

alt text

You can find possible alias in https://github.com/DTStack/dt-sql-parser/blob/main/src/grammar/postgresql/PostgreSqlParser.g4, then add it into alias option.

Add a preprocessor

const myPlugin = new DtSqlParserSemAnalysePlugin({
  preprocessor: [
    (sql) => sql.toUpperCase(),
    ...
  ]
})

Readme

Keywords

none

Package Sidebar

Install

npm i dt-sql-parser-semantic-analyse-plugin

Weekly Downloads

9

Version

0.0.1-alpha.10

License

none

Unpacked Size

5.95 MB

Total Files

20

Last publish

Collaborators

  • kijinseija