knex-dialect-athena
Version:
A Knex dialect for AWS Athena
64 lines (62 loc) • 1.79 kB
JavaScript
// src/athena-querycompiler.ts
import QueryCompiler from "knex/lib/query/querycompiler";
import compact from "lodash/compact";
var components = [
"comments",
"columns",
"join",
"where",
"union",
"group",
"having",
"order",
"offset",
"limit",
// TODO: remove
"lock",
"waitMode"
];
var QueryCompiler_Athena = class extends QueryCompiler {
// Compiles the `select` statement, or nested sub-selects by calling each of
// the component compilers, trimming out the empties, and returning a
// generated query string.
select() {
var _a;
let sql = this.with();
let unionStatement = "";
const firstStatements = [];
const endStatements = [];
components.forEach((component) => {
const statement = this[component](this);
switch (component) {
case "union":
unionStatement = statement;
break;
case "comments":
case "columns":
case "join":
case "where":
firstStatements.push(statement);
break;
default:
endStatements.push(statement);
break;
}
});
const wrapMainQuery = (_a = this.grouped.union) == null ? void 0 : _a.map((u) => u.wrap).some((u) => u);
if (this.onlyUnions()) {
const statements = compact(firstStatements.concat(endStatements)).join(
" "
);
sql += unionStatement + (statements ? " " + statements : "");
} else {
const allStatements = (wrapMainQuery ? "(" : "") + compact(firstStatements).join(" ") + (wrapMainQuery ? ")" : "");
const endStat = compact(endStatements).join(" ");
sql += allStatements + (unionStatement ? " " + unionStatement : "") + (endStat ? " " + endStat : endStat);
}
return sql;
}
};
export {
QueryCompiler_Athena
};