@synatic/noql
Version:
Convert SQL statements to mongo queries or aggregates
78 lines (75 loc) • 3.05 kB
JavaScript
const getParsedValueFromBinaryExpressionModule = require('./getParsedValueFromBinaryExpression');
const makeArraySubSelectPartModule = require('./makeArraySubSelectPart');
const makeCaseConditionModule = require('./makeCaseCondition');
const makeCastPartModule = require('./makeCastPart');
const makeProjectionExpressionPartModule = require('./makeProjectionExpressionPart');
exports.makeObjectFromSelect = makeObjectFromSelect;
/**
* Creates an object from a select without a from cause
*
* @param {import('../types').AST} ast - the ast for the select statement
* @param {import('../types').NoqlContext} context - The Noql context to use when generating the output
* @returns {any}
*/
function makeObjectFromSelect(ast, context) {
const toParse = {};
/** @type {import('../types').Column[]} */
// @ts-ignore
const columns = ast.columns;
columns.forEach((column) => {
if (column.expr.type === 'column_ref') {
toParse[`${column.as || column.expr.column}`] = `$${
column.expr.table ? column.expr.table + '.' : ''
}${column.expr.column}`;
} else if (column.expr.type === 'function' && column.as) {
const parsedExpr =
makeProjectionExpressionPartModule.makeProjectionExpressionPart(
column.expr,
context
);
toParse[`${column.as}`] = parsedExpr;
} else if (column.expr.type === 'binary_expr' && column.as) {
toParse[`${column.as}`] =
getParsedValueFromBinaryExpressionModule.getParsedValueFromBinaryExpression(
column.expr,
context
);
} else if (column.expr.type === 'case' && column.as) {
toParse[`${column.as}`] = makeCaseConditionModule.makeCaseCondition(
column.expr,
context
);
} else if (column.expr.type === 'cast' && column.as) {
toParse[`${column.as}`] = makeCastPartModule.makeCastPart(
column.expr,
context
);
} else if (
column.expr.type === 'select' &&
column.as &&
column.expr.from
) {
toParse[`${column.as}`] =
makeArraySubSelectPartModule.makeArraySubSelectPart(
column.expr,
context
);
} else if (
column.expr.type === 'select' &&
column.as &&
!column.expr.from
) {
toParse[`${column.as}`] = makeObjectFromSelect(
column.expr,
context
);
} else if (column.expr.type && column.as) {
toParse[`${column.as}`] = {$literal: column.expr.value};
} else if (!column.as) {
throw new Error(`Require as for calculation:${column.expr.name}`);
} else {
throw new Error(`Not Supported:${column.expr.type}`);
}
});
return toParse;
}