Use postgres function as second argument to set #49

Closed
rubymaniac opened this Issue Jan 17, 2014 · 9 comments

Comments

Projects
None yet
3 participants
@rubymaniac

I want to construct the following query:

UPDATE users SET password = crypt('my super awesome password', gen_salt('md5'))

and it is not obvious how I have to go in order to not have the crypt function to get interpreted as a string value. I do not want to use the usingValuePlaceholders option because it is deprecated. Is there any other way?

@hiddentao

This comment has been minimized.

Show comment Hide comment
@hiddentao

hiddentao Jan 21, 2014

Owner

Yep, you can use a custom value type. Just represent the crypt('my super awesome password', gen_salt('md5')) part as a CryptFunction class or something and then register that value type with squel along with a handler. Then everytime squel encounters an instance of that class it will pass it off to your custom handler to get the string representation.

Owner

hiddentao commented Jan 21, 2014

Yep, you can use a custom value type. Just represent the crypt('my super awesome password', gen_salt('md5')) part as a CryptFunction class or something and then register that value type with squel along with a handler. Then everytime squel encounters an instance of that class it will pass it off to your custom handler to get the string representation.

@andreyvit

This comment has been minimized.

Show comment Hide comment
@andreyvit

andreyvit Mar 1, 2014

Nope, doesn't work for me:

squel = require('squel')

class RawSql
  constructor: (@sql) ->

squel.registerValueHandler RawSql, (rawSql) ->
  return rawSql.sql

squel.sql = (sql) -> new RawSql(sql)

console.log('%s', squel.insert().into('widgets').set('foo', 42).set('created_at', squel.sql('NOW()')))
# INSERT INTO widgets (foo, created_at) VALUES (42, 'NOW()')

What I need is an unquoted raw value. I think this absolutely must be a part of the library; pretty much every app requires access to NOW().

Nope, doesn't work for me:

squel = require('squel')

class RawSql
  constructor: (@sql) ->

squel.registerValueHandler RawSql, (rawSql) ->
  return rawSql.sql

squel.sql = (sql) -> new RawSql(sql)

console.log('%s', squel.insert().into('widgets').set('foo', 42).set('created_at', squel.sql('NOW()')))
# INSERT INTO widgets (foo, created_at) VALUES (42, 'NOW()')

What I need is an unquoted raw value. I think this absolutely must be a part of the library; pretty much every app requires access to NOW().

@hiddentao

This comment has been minimized.

Show comment Hide comment
@hiddentao

hiddentao Mar 1, 2014

Owner

@andreyvit That code works fine for me. I'm running against squel 2.0.0 on node 0.11.10

Owner

hiddentao commented Mar 1, 2014

@andreyvit That code works fine for me. I'm running against squel 2.0.0 on node 0.11.10

@andreyvit

This comment has been minimized.

Show comment Hide comment
@andreyvit

andreyvit Mar 1, 2014

@hiddentao By ‘works fine’, do you mean it produces this?

INSERT INTO widgets (foo, created_at) VALUES (42, NOW())

Anyways, obviously toString isn't the primary use case, and I cannot see how it can possibly work with toParam, given that you give all the output strings to the native driver as values.

@hiddentao By ‘works fine’, do you mean it produces this?

INSERT INTO widgets (foo, created_at) VALUES (42, NOW())

Anyways, obviously toString isn't the primary use case, and I cannot see how it can possibly work with toParam, given that you give all the output strings to the native driver as values.

@hiddentao

This comment has been minimized.

Show comment Hide comment
@hiddentao

hiddentao Mar 20, 2014

Owner

@andreyvit Yep, that's what I get when I run the code. Have you tried using it with toParam()?

Owner

hiddentao commented Mar 20, 2014

@andreyvit Yep, that's what I get when I run the code. Have you tried using it with toParam()?

@hiddentao

This comment has been minimized.

Show comment Hide comment
@hiddentao

hiddentao Mar 20, 2014

Owner

Also, just released v3.0.1 which should improve toParam() a little bit.

Owner

hiddentao commented Mar 20, 2014

Also, just released v3.0.1 which should improve toParam() a little bit.

@hiddentao

This comment has been minimized.

Show comment Hide comment
@hiddentao

hiddentao Jul 11, 2014

Owner

Is this still an issue?

Owner

hiddentao commented Jul 11, 2014

Is this still an issue?

@andreyvit

This comment has been minimized.

Show comment Hide comment
@andreyvit

andreyvit Jul 12, 2014

No idea, I've stopped using squel. @rubymaniac?

No idea, I've stopped using squel. @rubymaniac?

@rubymaniac

This comment has been minimized.

Show comment Hide comment
@rubymaniac

rubymaniac Jul 12, 2014

Sorry I too stopped using squel. I have no idea if this is still an issue.

Sorry I too stopped using squel. I have no idea if this is still an issue.

@hiddentao hiddentao closed this Jul 12, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment