Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion dist/ast/converter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ export default class Converter {
rexpr: any;
};
};
GreaterThanOrEqualConverter: (expression: any) => {
GreaterThanOrEqualConverter: (expression: any, options?: {}) => {
A_Expr: {
kind: any;
name: {
Expand Down Expand Up @@ -488,6 +488,18 @@ export default class Converter {
nulltesttype: any;
};
} | null;
DateBinaryConverter: (kind: any, operator: any, expression: any, options: any) => {
A_Expr: {
kind: any;
name: {
String: {
str: any;
};
}[];
lexpr: any;
rexpr: any;
};
};
BinaryConverter: (kind: any, operator: any, expression: any) => {
A_Expr: {
kind: any;
Expand Down
25 changes: 22 additions & 3 deletions dist/ast/converter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/ast/converter.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fulcrum-query-sql",
"version": "1.1.13",
"version": "1.1.14",
"description": "Fulcrum Query SQL Utilities",
"homepage": "http://github.com/fulcrumapp/fulcrum-query-sql",
"main": "dist/index.js",
Expand Down
48 changes: 48 additions & 0 deletions src/ast/__tests__/converter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,54 @@ describe('NotEmpty converter', () => {
});
});

describe('ConstValue converter', () => {
describe('given a Number column', () => {
const numValue = 72828292;
const numColumn = {
isNumber: true,
element: {
isCalculatedElement: false,
display: {
isDate: false,
},
},
};

const expectedValue = (val) => (
{
A_Const: {
val: {
Float: {
str: val != null ? val.toString() : '',
}
},
},
}
);

it('correctly structures a ConstValue with a Float type', () => {
expect(new Converter().ConstValue(numColumn, numValue)).toEqual(expectedValue(numValue));
});

describe('given a calculated field with a date display', () => {
const dateValue = '2025-07-22';
const calculatedColumn = {
isNumber: true,
element: {
isCalculatedElement: true,
display: {
isDate: true,
}
}
};

it('correctly structures a ConstValue with a Float type', () => {
expect(new Converter().ConstValue(calculatedColumn, dateValue)).toEqual(expectedValue(1753142400));
});
});
});
});

describe('Empty converter', () => {
describe('given a non-array', () => {
it('creates a subquery with a null test', () => {
Expand Down
29 changes: 25 additions & 4 deletions src/ast/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ export default class Converter {
targetList = [ ResTarget(FuncCall('unnest', [ valueColumn ]), 'value') ];
} else if (options.column.element && options.column.element.isCalculatedElement && options.column.element.display.isDate) {
// SELECT pg_catalog.timezone('UTC', to_timestamp(column_name))::date
console.log('are we in toDistinctValuesAST for date?', options.column.element.display.isDate);

const timeZoneCast = (param) => {
return FuncCall([ StringValue('pg_catalog'), StringValue('timezone') ], [ AConst(StringValue('UTC')), param ]);
Expand Down Expand Up @@ -874,8 +875,8 @@ export default class Converter {
[OperatorType.TextNotEqual.name]: this.TextNotEqualConverter,
[OperatorType.TextMatch.name]: this.TextMatchConverter,
[OperatorType.TextNotMatch.name]: this.TextNotMatchConverter,
[OperatorType.DateEqual.name]: this.EqualConverter,
[OperatorType.DateNotEqual.name]: this.NotEqualConverter,
[OperatorType.DateEqual.name]: this.BetweenConverter, // DateEqual is a special case of Between
[OperatorType.DateNotEqual.name]: this.NotBetweenConverter, // DateNotEqual is a special case of NotBetween
[OperatorType.DateAfter.name]: this.GreaterThanConverter,
[OperatorType.DateOnOrAfter.name]: this.GreaterThanOrEqualConverter,
[OperatorType.DateBefore.name]: this.LessThanConverter,
Expand Down Expand Up @@ -986,7 +987,10 @@ export default class Converter {
return this.BinaryConverter(0, '>', expression);
}

GreaterThanOrEqualConverter = (expression) => {
GreaterThanOrEqualConverter = (expression, options = {}) => {
if(expression.isDateOperator) {
return this.DateBinaryConverter(0, '>=', expression, options);
}
return this.BinaryConverter(0, '>=', expression);
}

Expand Down Expand Up @@ -1030,6 +1034,19 @@ export default class Converter {
return this.NotIn(expression.column, expression.arrayValue);
}

DateBinaryConverter = (kind, operator, expression, options) => {
const dates = {
'>': moment.utc(expression.scalarValue),
'<=': moment.utc(expression.scalarValue),
'<': moment.utc(expression.scalarValue),
'>=': moment.utc(expression.scalarValue),
};
const dateStr = dates[operator].toISOString();
console.log('DateBinaryConverter, date', dates[operator], 'dateStr', dateStr);
return AExpr(kind, operator, columnRef(expression.column),
this.ConstValue(expression.column, dateStr));
}

BinaryConverter = (kind, operator, expression) => {
return AExpr(kind, operator, columnRef(expression.column),
this.ConstValue(expression.column, expression.scalarValue));
Expand Down Expand Up @@ -1133,7 +1150,6 @@ export default class Converter {
// the same range. So 'Today' is midnight to midnight in the user's local time. It would
// be much less useful and confusing if we forced "Today" to always be London's today.
const now = this.GetDate(null, options);

const range = calculateDateRange(expression.column, expression.operator, expression.value, now);

const value1 = this.ConvertDateValue(expression, range[0]);
Expand Down Expand Up @@ -1236,6 +1252,11 @@ export default class Converter {
}

if (column.isNumber) {
if (column.element.isCalculatedElement && column.element.display.isDate) {
const doubleValue = moment(value).valueOf() / 1000;
return AConst(FloatValue(doubleValue));
}

return AConst(FloatValue(value));
}

Expand Down