Skip to content

Commit

Permalink
Refactor priceCurrentAndPreviousDiffField function
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubknejzlik committed Feb 15, 2024
1 parent 23d530e commit eade220
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
14 changes: 14 additions & 0 deletions src/Function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,19 @@ describe("Expression", () => {
expect(Q.deserialize(fn3.serialize()).toSQL(flavor)).toEqual(
fn3.toSQL(flavor)
);

const fn4 = Q.select().addField(
Fn.priceCurrentAndPreviousDiffField({
thisYearColumn: Fn.sum("price_this_year"),
lastYearColumn: Fn.avg("price_last_year"),
}),
"blah"
);
expect(fn4.toSQL(flavor)).toEqual(
"SELECT IF((SUM(`price_this_year`) = 0 AND AVG(`price_last_year`) = 0),`0`,IF(AVG(`price_last_year`) = 0,NULL,IF(SUM(`price_this_year`) = 0,-1,((SUM(`price_this_year`) - AVG(`price_last_year`)) / AVG(`price_last_year`))))) AS `blah` "
);
expect(Q.deserialize(fn4.serialize()).toSQL(flavor)).toEqual(
fn4.toSQL(flavor)
);
});
});
51 changes: 25 additions & 26 deletions src/Function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import { MySQLFlavor } from "./flavors/mysql";

const formatDayjs = (dayjs: Dayjs) => dayjs.format("YYYY-MM-DD");

const ifFn = (
condition: Condition,
trueValue: ExpressionValue,
falseValue: ExpressionValue
) => {
return new FunctionExpression("IF", condition, trueValue, falseValue);
};

export const Function = {
add: (...columns: ExpressionValue[]) => {
return new OperationExpression("+", ...columns);
Expand Down Expand Up @@ -91,13 +99,7 @@ export const Function = {
concat: (...values: ExpressionValue[]) => {
return new FunctionExpression("CONCAT", ...values);
},
if: (
condition: Condition,
trueValue: ExpressionValue,
falseValue: ExpressionValue
) => {
return new FunctionExpression("IF", condition, trueValue, falseValue);
},
if: ifFn,
dateRangeSumField: ({
dateColumn,
valueColumn,
Expand Down Expand Up @@ -130,25 +132,22 @@ export const Function = {
thisYearColumn: ExpressionValue;
lastYearColumn: ExpressionValue;
}) =>
Q.expr(
"CASE " +
`WHEN ${Expression.escapeExpressionValue(
thisYearColumn
)} = 0 AND ${Expression.escapeExpressionValue(
lastYearColumn
)} = 0 THEN 0 ` +
`WHEN ${Expression.escapeExpressionValue(
lastYearColumn
)} = 0 THEN null ` +
`WHEN ${Expression.escapeExpressionValue(
thisYearColumn
)} = 0 THEN -1 ` +
`ELSE (${Expression.escapeExpressionValue(
thisYearColumn
)} - ${Expression.escapeExpressionValue(
lastYearColumn
)}) / ${Expression.escapeExpressionValue(lastYearColumn)} ` +
"END"
ifFn(
Cond.and([Cond.equal(thisYearColumn, 0), Cond.equal(lastYearColumn, 0)]),
0,
ifFn(
Cond.equal(lastYearColumn, 0),
Q.expr("NULL"),
ifFn(
Cond.equal(thisYearColumn, 0),
-1,
new OperationExpression(
"/",
new OperationExpression("-", thisYearColumn, lastYearColumn),
lastYearColumn
)
)
)
),
};

Expand Down

0 comments on commit eade220

Please sign in to comment.