# dt-sql-parser-semantic-analyse-plugin

> an dt-sql-parser plugin with semantic result

Latest version **0.1.0** (published 2024-06-20) · 0 weekly downloads

## Install

```sh
npm install dt-sql-parser-semantic-analyse-plugin
pnpm add dt-sql-parser-semantic-analyse-plugin
yarn add dt-sql-parser-semantic-analyse-plugin
bun add dt-sql-parser-semantic-analyse-plugin
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2024-06-20 |
| First published | 2024-04-08 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 523.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | kijinseija |

## Links

- npm: https://www.npmjs.com/package/dt-sql-parser-semantic-analyse-plugin
- npm.io page: https://npm.io/package/dt-sql-parser-semantic-analyse-plugin

## Recent versions

- 0.1.0 (latest) — 2024-06-20
- 0.0.1-alpha.10 — 2024-05-28
- 0.0.1-alpha.9 — 2024-05-28
- 0.0.1-alpha.8 — 2024-04-15
- 0.0.1-alpha.7 — 2024-04-15
- 0.0.1-alpha.6 — 2024-04-12
- 0.0.1-alpha.5 — 2024-04-10
- 0.0.1-alpha.4 — 2024-04-09
- 0.0.1-alpha.3 — 2024-04-09
- 0.0.1-alpha.2 — 2024-04-09
- 0.0.1-alpha.1 — 2024-04-08

## README

# dt-sql-parser-semantic-analyse-plugin

A [dt-sql-parser](https://github.com/DTStack/dt-sql-parser) plugin with semantic result. [Theory(zh-CN)](https://github.com/Kijin-Seija/dt-sql-parser-analyse-demo).

## Installation

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

## Quick Usage
```typescript
import { PostgreSQL } from 'dt-sql-parser'
import { PostgreSqlParser } from 'dt-sql-parser/dist/lib/postgresql/PostgreSqlParser'

const myPlugin = new DtSqlParserSemAnalysePlugin({
  parse: {
    sql: new PostgreSQL(),
    parser: PostgreSqlParser,
    alias: {
      selectstmt: 'selectStatement',
    },
    stmts: [
      'selectstmt',
    ],
    entities: [
      'target_el',
    ],
    rules: {
      select_target: [
        PostgreSqlParser.RULE_selectstmt,
        PostgreSqlParser.RULE_target_el,
      ]
    }
  }
})

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. You can get `select_target` text from parse 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.**

## Add a preprocessor

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

## Rule Chain Operator

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

Example:

```typescript
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`'s code are different from their antlr4's definition. You should declare them in `alias` params.

Example:

```typescript
const myPlugin = new DtSqlParserSemAnalysePlugin({
  parse: {
    ...
    alias: {
      selectstmt: 'selectStatement',
      target_el: 'target_label',
      table_name: 'tableName',
      ...
    }
    ...
  }
})
```

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.

![alt text](./assets/alias-example.png)

## Custom dt-sql-parser

If you want to use your custom `dt-sql-parser`, with external grammar(such as `DISTRIBUTED`, etc.), follow steps below:
1. fork [dt-sql-parser](https://github.com/DTStack/dt-sql-parser).
2. change its ANTLR4 file as whatever you want.
3. build asn publish your `dt-sql-parser` npm package(`npm run antlr4 && npm publish`). Suppose to be `my-dt-sql-parser`.
4. use `my-dt-sql-parser` instead of `dt-sql-parser` to provide `parse.sql` and `parse.parser`.

Example:

```typescript
import { PostgreSQL } from 'my-dt-sql-parser'
import { PostgreSqlParser } from 'my-dt-sql-parser/dist/lib/postgresql/PostgreSqlParser'
const myPlugin = new DtSqlParserSemAnalysePlugin({
  parse: {
    sql: new PostgreSQL(),
    parser: PostgreSqlParser,
    ...
  }
})
```

---
_Source: https://npm.io/package/dt-sql-parser-semantic-analyse-plugin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
