rhombic
Version:
SQL parsing, lineage extraction and manipulation
71 lines • 2.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TablePrimaryVisitor = void 0;
const SqlParser_1 = require("../SqlParser");
const getRange_1 = require("../utils/getRange");
const Visitor = SqlParser_1.parser.getBaseCstVisitorConstructorWithDefaults();
/**
* Visitor to check if a tableReference is part of the query
*/
class TablePrimaryVisitor extends Visitor {
constructor(name = "") {
super();
this.hasTablePrimary = false;
this.tables = [];
this.validateVisitor();
this.name = name
.split(".")
.map(sanitizeTableName)
.join(".");
}
tableReference(ctx) {
this.tablePrimary(ctx.tablePrimary[0].children);
// Add alias information
if (ctx.As && ctx.Identifier) {
this.tables[this.tables.length - 1].alias = ctx.Identifier[0].image;
}
}
tablePrimary(ctx) {
if (ctx.Identifier) {
const tableName = ctx.Identifier.map(i => sanitizeTableName(i.image));
const range = getRange_1.getRange(ctx.Identifier);
if (tableName.length === 3) {
this.tables.push({
catalogName: tableName[0],
schemaName: tableName[1],
tableName: tableName[2],
range
});
}
if (tableName.length === 2) {
this.tables.push({
schemaName: tableName[0],
tableName: tableName[1],
range
});
}
if (tableName.length === 1) {
this.tables.push({
tableName: tableName[0],
range
});
}
if (this.name === tableName.join(".")) {
this.hasTablePrimary = true;
}
}
}
}
exports.TablePrimaryVisitor = TablePrimaryVisitor;
/**
* Deal with quotes.
*
* @param raw chunk of table name
*/
const sanitizeTableName = (raw) => {
if (raw[0] === '"' && raw[raw.length - 1] === '"') {
return raw.slice(1, -1);
}
return raw;
};
//# sourceMappingURL=TablePrimaryVisitor.js.map