-
Notifications
You must be signed in to change notification settings - Fork 231
How to register value handler to return non-string (not quoted) value in multi row insert query? #118
Comments
At the moment there is no elegant way for just I'll need to fix this. The result will enable you to do something like: q.setFieldsRows([
{ id: 1, col1: 'val1', col2: new Default() }
{ id: 2, col1: 'val2', col2: new Default() }
], {
col2: {
dontQuote: true
}
}) |
Thanks for answer. Note that
Maybe it would be easier to just make it available to somehow mark value as unquotable when returning it from
|
@hiddentao I have created PR with my idea - what do you think? |
looking for this feature as well |
@alekbarszczewski Please see my comments in #119 |
The new squel = require 'squel'
q = squel.insert()
q.into('table_name')
q.setFieldsRows [
{ id: 1, col1: 'val1', col2: squel.fval('DEFAULT') }
{ id: 2, col1: 'val2', col2: squel.fval('DEFAULT') }
]
console.log q.toString()
# INSERT INTO table_name (id, col1, col2) VALUES
(1, 'val1', (DEFAULT))
(2, 'val2', (DEFAULT)) |
The Also, in the version I'm testing with (4.0.0), |
|
I just updated to the latest version and tried the Just to give an example, though, here's where I'm using that. // Set up a custom value handler for dates
squel.registerValueHandler(Date, function(date) {
if (date.getTime() === constants.DateKeywords.Now.getTime()) {
return squel.str('utc_timestamp()');
}
if (date.getTime() === constants.DateKeywords.Never.getTime()) {
return '9999-9-9';
}
return utils.getUTCTimestamp(date);
}); |
Actually I just did some more testing with this, and it looks like now everything that is returned from a custom value handler is inserted into the final query unquoted. Is this expected? This did not used to be the case with 3.10. This handler: squel.registerValueHandler(Date, function(date) {
return 'utc_timestamp()';
}); will now return an unquoted string even without a call to squel.insert()
.into('table')
.set('col', new Date())
.toString();
// Generated Query
"INSERT INTO table (col) VALUES ((utc_timestamp()))" |
Yes, this is expected. This is so that you have more flexibility over how the custom value output gets rendered within the final query. If you want it quoted you can now add them yourself. |
Ah okay. That makes sense. However, I noticed that this isn't the case for all value handlers. A value returned from a string value handler will still be quoted, but numbers and Dates will not unless it is done explicitly. How is it determined which types get auto-quoted and which don't? And is this something that could be added to the docs? |
@abgivant That's interesting. I thought none of the returned values get quoted. I just pushed 5.4.0 which slightly modifies how it detects if custom value formatting took place - would you be able to try again and let me know if this fixes that discrepancy you're seeing? |
I just tested the changes a bit more and it seems to be consistently returning everything unquoted now, unless done explicitly. Thanks for the quick update! However, a change like this could potentially cause a lot of problems if/when someone updates to the newer versions, so would it be possible to have a statement at the top of the "Custom value types" section of the docs specifically describing this behavior? It's implied from the examples, but I think stating it would still be beneficial. |
Updated the docs for custom value types with some info on quoting. Not super prominent but it's in there. |
Just ran into another issue with this. It looks like registering a string value handler overrides the squel.registerValueHandler('string', function(str) {
return "'" + str + "'";
}); squel.insert().into('table').set('col', 'blah', {dontQuote: true}).toString()
> "INSERT INTO table (col) VALUES (('blah'))" I can see how these can be conflicting, but what do you think about |
@abgivant Sorry about the late reply. If On the other hand, it would be good if the custom value handlers had more information on the context in which they're running in order to know whether to add quotes or not. I'm going to see if that can be made possible. |
I don't think it'd be that weird, to strip the quotes if For example, in the case where I ran into this, I have a string value handler that does some extra escaping before saving the string, but now anywhere I'm trying to insert using a SQL function (utc_timestamp() in this case) fails because of the quotes I'm having to manually add to the string after escaping it in the value handler. Allowing the value handlers to be aware of the specified options would solve the problem as well, though. |
I like this idea. Will add it into the next release. |
In master. |
My goal is to set
DEFAULT
literal for a column in multi row insert query - tried it like this:result is:
How I can force
'DEFAULT'
to not be quoted?The text was updated successfully, but these errors were encountered: