question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support Typecasting Function Parameters (Postgis ST_Transform Bug)

See original GitHub issue

I have come across a problem, with different solutions, but I would like to propose a feature that would allow the calling of functions to be typecasted in the parameterization.

First the code:

function ST_Transform(shape, srid)
    {
        let sridParam = '?';

        if (_.isNumber(srid))
        {
            sridParam += '::integer';
        }

        return knex.raw(`st_transform(?, ${sridParam})`, [shape, srid]);
    };

The Problem:

This is in a file called ‘postgis.js’ in my architecture. What it allows me to do is import postgis and call postgis.ST_Transform(geom, 4326). Originally this code was 1 line long that got wrapped up in a convenience function. That was until Postgis 2.3.0 dropped and they changed it so that ST_Transform now takes a string or an integer. This causes an error because parameterized raw statements do not get typecasted naturally.

The Solution:

If knex had a property that was .executeFunction() that:

  • Took a string representation of a function name.
  • An array of values to be passed in.
  • An array of typecastings to be used.

It could be a very clean implementation for executing functions with correct types which has been requested multiple times from other issues. If we could start a discussion about a good way to accomplish this via code it would be appreciated and I would happily help with the implementation.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
cpriegercommented, May 19, 2017

Didn’t want to just disappear on this issue or have it go cold. I will try and get some work in this weekend on a good proposal for changes (maybe even a pull request Chronos willing).

But in defense of @elhigu point wrapping this in your own Knex.raw helper class works just as well.

This discussion is mainly for “Is there a clean implementation that could be adapted to meet Knex’s standards and fit the overall model?” Which is actually fairly non-trivial when consideration is given to the different databases, typecastings, and syntactic sugars that can take place.

1reaction
cpriegercommented, Apr 21, 2017

Sure. Give me a bit to work on it some more. I want to make sure I include the weird dichotomy between PLPGSQL and T-SQL function execution. So i’ll work up some good examples for you. Thanks for discussing this feature request.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chapter 8. PostGIS Reference
A type cast converts values of one data type into another type. PostgreSQL allows defining casting behavior for custom types, along with the...
Read more >
PostGIS: ST_Transform function conversion problem
I started with a polygon layer in 4326, and created a new point layer, also in 4326. Then I INSERTed a point into...
Read more >
Chapter 8. PostGIS Reference - Crunchy Data
There are other functions which are required support functions to the PostGIS ... cast to both box types, this produces an "ambiguous function"...
Read more >
PostgreSQL Cast Function 101: How to Convert One Data ...
For example, we can convert a string data type to an integer data type and vice versa. This article will help you develop...
Read more >
sf.pdf
BugReports https://github.com/r-spatial/sf/issues/ ... In case we need to pass an argument to the join function: (p_ag4 = aggregate(p, pol, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found