Skip to content

Latest commit

 

History

History
70 lines (53 loc) · 1.58 KB

README.md

File metadata and controls

70 lines (53 loc) · 1.58 KB

cuopyue


parse sparksql syntax, format SQL

NPM version

Getting Started

Install cuopyue using npm:

npm install cuopyue -S

Import coupyue in your js file

import { parser } from 'cuopyue';

Then, call parserSparksql method

const sql = 'select age, nickname from userInfo; select id, price from payInfo;'
const tree = parser.parserSparkSql(sql);
...

You will get an array of syntax tree.

Complex example

Catch Syntax Error

import { parser } from 'cuopyue';
const sql = 'select age, nickname fr1om userInfo;'
parser.parserSparkSql(sql, (recognizer, offendingSymbol, line, column, msg) => {
    const errorText = offendingSymbol.text;
    console.error(msg);
});

Get Table Name

import { parser, visitor } from 'cuopyue';
const sql = 'select age, nickname from userInfo as u;'
class Visitor extends visitor.SparkTreeVisitor {
    visitTableName (ctx) {
        const tableName = ctx.tableIdentifier().getText();
        const tableAlias = ctx.tableAlias().strictIdentifier().getText();
        super.visitTableName(ctx);
    }
}
parser.parserSparkSql(sql, null, new Visitor());

Format SQL

import { parser, visitor } from 'cuopyue';
const sql = 'select age, nickname from userInfo as u;'
let formatVisitor = new visitor.SparkFormatVisitor();
parser.parserSparkSql(sql, null, formatVisitor);
console.log(formatVisitor.getFormatText());