cuopyue
v0.1.1
Published
sql parser
Downloads
2
Readme
cuopyue
parse sparksql syntax, format SQL
Getting Started
Install cuopyue using npm:
npm install coupyue -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());