All files / src/modules/implementations/database decimal-sql-functions.js

100% Statements 28/28
85.71% Branches 12/14
100% Functions 4/4
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 2954x 54x 785x 2068x 2068x 2068x 37452x 2068x 2068x 2068x 2068x 5087x 5087x 5082x 2068x 2068x 4679x 4679x 4679x 2068x 2068x 4113x 4113x 4113x 2068x 2068x 785x 54x  
'use strict';
module.exports = ({ numeric: { decimal: { addDecimals, compareDecimals, formatDecimal, multiplyDecimals, parseDecimal } } }) => {
    function registerDecimalFunctions(database) {
        database.aggregate('decimal_sum', {
            start: () => ({ coefficient: 0n, scale: 0 }),
            step: (total, value) => value === null
                ? total
                : addDecimals(total, parseDecimal(value)),
            result: formatDecimal,
        });
        database.function('decimal_mul', { deterministic: true }, (left, right) => {
            if (left === null || right === null)
                return null;
            return formatDecimal(multiplyDecimals(parseDecimal(left), parseDecimal(right)));
        });
        database.function('decimal_add', { deterministic: true }, (left, right) => {
            if (left === null || right === null)
                return null;
            return formatDecimal(addDecimals(parseDecimal(left), parseDecimal(right)));
        });
        database.function('decimal_cmp', { deterministic: true }, (left, right) => {
            if (left === null || right === null)
                return null;
            return compareDecimals(parseDecimal(left), parseDecimal(right));
        });
    }
    return { registerDecimalFunctions };
};