Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to put function to select? #169

Closed
kobalicek opened this issue Jan 24, 2014 · 7 comments
Closed

How to put function to select? #169

kobalicek opened this issue Jan 24, 2014 · 7 comments
Labels

Comments

@kobalicek
Copy link
Contributor

Hi,

How can I use function in select. For example I would like to do something like this:
...
knex.column("to_char(dateCreated) as dateCreated");
...
without having to write the whole select as a raw select.

Thanks

@johanneslumpe
Copy link
Contributor

You can just pass in a knew.raw statement, like this:

knex.column(knex.raw('to_char(dateCreated) as dateCreated'));

@kobalicek
Copy link
Contributor Author

Hi, thanks!

I found this solution myself, although it took me some time before I tried to use raw here.

Anyway I think that it would be nice to have direct support for having functions in select, so knex can autoescape field/table names there.

I think that this can be closed, thanks!

@bendrucker
Copy link
Member

@kobalicekp Certainly something to consider. But it is worth noting that knex doesn't support arbitrary SQL functions anywhere. It's either a supported command or you use knex.raw. Out of curiosity, why are you worried about escaping? Are you using this somewhere else where there's an injection vulnerability?

@kobalicek
Copy link
Contributor Author

@bendrucker I meant escaping just to make the queries more portable. My query from the example above would look like:

knex.column(knex.raw('to_char("dateCreated", \'YYYY-MM-DD\') as "dateCreated"'));

I'm not sure how portable it would be outside of postgres. But if I think about it it would be difficult to write a good wrapper for this, covering all possible functions and their arguments.

@bendrucker
Copy link
Member

You hit on the exact problem. RDBMSes have 100s of functions and even where there's overlap the syntaxes are usually different. But by all means you should monkey patch functions you need often to keep things clean.

elliotf pushed a commit to elliotf/knex that referenced this issue Nov 24, 2014
@eduardobcastro
Copy link

It would be good to have wrappers to the most common functions like COALESCE / NVL, UPPER, LOWER, etc, and also CASE/WHEN/ELSE

@elhigu
Copy link
Member

elhigu commented Apr 1, 2019

@eduardobcastro please do those if you wish. knex.fn.* is place to implement them and remember to add also tests + docs. And remember to test also that they works in a nested manner.

To be honest I don't see much use to have them as own functions, since many times one still needs to add casting etc. to when used and using knex not that complicated to use, unless there are subtle differences between how those functions work between db dialects .

JakobJoonas pushed a commit to JakobJoonas/knex that referenced this issue Sep 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants