ddl-manager
Version:
store postgres procedures and triggers in files
55 lines • 2.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CacheParser = void 0;
const psql_lang_1 = require("psql-lang");
const SelectParser_1 = require("./SelectParser");
const ast_1 = require("../ast");
const CacheIndex_1 = require("../ast/CacheIndex");
const CacheSyntax_1 = require("./CacheSyntax");
const TableID_1 = require("../database/schema/TableID");
const TableReference_1 = require("../database/schema/TableReference");
const defaults_1 = require("./defaults");
const utils_1 = require("./utils");
class CacheParser {
constructor(input) {
this.selectParser = new SelectParser_1.SelectParser();
if (input instanceof CacheSyntax_1.CacheSyntax) {
this.syntax = input;
}
else if (input instanceof psql_lang_1.Sql) {
this.syntax = input.parse(CacheSyntax_1.CacheSyntax);
}
else {
this.syntax = psql_lang_1.Sql.code(input).parse(CacheSyntax_1.CacheSyntax);
}
}
static parse(cacheSQL) {
const parser = new CacheParser(cacheSQL);
return parser.parse();
}
parse() {
var _a, _b;
const forTable = new TableReference_1.TableReference(new TableID_1.TableID(((_a = this.syntax.row.for.row.schema) === null || _a === void 0 ? void 0 : _a.toValue()) || defaults_1.DEFAULT_SCHEMA, this.syntax.row.for.row.name.toValue()), (_b = this.syntax.row.as) === null || _b === void 0 ? void 0 : _b.toValue());
const select = this.parseSelect();
const cache = new ast_1.Cache(this.syntax.row.name.toValue(), forTable, select, this.parseTables(this.syntax.row.withoutTriggersOn), this.parseTables(this.syntax.row.withoutInsertOn), this.parseIndexes());
return cache;
}
parseSelect() {
const selectSyntax = this.syntax.row.cache;
return this.selectParser.parse(selectSyntax);
}
parseTables(tables = []) {
return tables.map(table => utils_1.parseFromTable(table).table.toString());
}
parseIndexes() {
var _a;
return (_a = this.syntax.row.indexes) === null || _a === void 0 ? void 0 : _a.map(cacheIndexSyntax => {
const index = cacheIndexSyntax.row.index;
const onSyntaxes = cacheIndexSyntax.row.columns || [];
const on = onSyntaxes.map(onSyntax => onSyntax.row.expression.toString());
return new CacheIndex_1.CacheIndex(index, on);
});
}
}
exports.CacheParser = CacheParser;
//# sourceMappingURL=CacheParser.js.map