planetarydev/json-sql-builder2

help: psql function call

gopalakrishnan-subramani opened this issue · 2 comments

Thanks for the wonderful library.
What is the best way to define the call to the psql [postgresql] function using json-sql-builder2?

for example, we would like to call

select * from analytics.compute_salary($1, $2, $3)

where as $1,$2,$3 might be values/params in json-sql-builder2.

Any help would be helpful here.

Hey, Thanks for your feedback.
I've add a new helper $tabeFunction to call table-valued-functin from inside the from clause.

Here you can see some examples from the new docs:

function() {
    return sql.build({
        $select: {
            $from: {
                test: {
                    $tableFunction: { $name: 'my_table_valued_function', $args: ['Param1', 2, 'Param3', 4] }
                }
            }
        }
    });
}

// or

function() {
    return sql.build({
        $select: {
            $from: {
                test: sql.tableFunction('my_table_valued_function', { $args: ['Param1', 2, 'Param3', 4] })
            }
        }
    });
}

// or (without aliasing)

function() {
    return sql.build({
        $select: {
            $from: {
                my_table_valued_function: ['Param1', 2, 'Param3', 4]
            }
        }
    });
}

// or

function() {
    return sql.build({
        $select: {
            $from: sql.tableFunction('my_table_valued_function', { $args: ['Param1', 2, 'Param3', 4] })
        }
    });
}

// SQL output
SELECT
    *
FROM
    my_table_valued_function($1, $2, $3, $4) AS test

// Values
{
    "$1": "Param1",
    "$2": 2,
    "$3": "Param3",
    "$4": 4
}

I hope this will work for you as expected.

@gopalakrishnan-subramani
I am closing this as answered/solved, please feel free to continue the issue/discussion if it's not the case.