Comments (8)
FYI I have a workaround as follows (Plus MSSQL output clause as I'm too lazy to edit it out):
# (INSERT INTO) ... field ... value
class InsertFieldValueBlock extends squel.cls.InsertFieldValueBlock
constructor: (options) ->
super options
@outputs = []
@sqlFields = []
@sqlValues = []
# add fields to the output clause
output: (fields) -> @outputs.push "INSERTED.#{f}" for f in fields
# Update the given field with the given value.
# This will override any previously set value for the given field.
setSQL: (field, value) ->
throw new Error "Cannot call set or setSQL or setFields on multiple rows of fields." if @sqlValues.length > 1 or @values.length > 1
# Explicity overwrite existing fields
index = @sqlFields.indexOf(@_sanitizeField(field))
if index isnt -1
@sqlValues[0][index] = value
else
@sqlFields.push @_sanitizeField(field)
index = @sqlFields.length - 1
# The first value added needs to create the array of values for the row
if Array.isArray(@sqlValues[0])
@sqlValues[0][index] = value
else
@sqlValues.push [value]
@
buildStr: (queryBuilder) ->
if 0 >= @fields.length and 0 >= @sqlFields.length then throw new Error "set() needs to be called"
vals = []
for i in [0...@values.length]
for j in [0...@values[i].length]
formattedValue = @_formatValue(@values[i][j])
if 'string' is typeof vals[i]
vals[i] += ', ' + formattedValue
else
vals[i] = '' + formattedValue
for i in [0...@sqlValues.length]
for j in [0...@sqlValues[i].length]
if 'string' is typeof vals[i]
vals[i] += ', ' + @sqlValues[i][j]
else
vals[i] = '' + @sqlValues[i][j]
"(#{@fields.concat(@sqlFields).join(', ')}) #{if @outputs.length isnt 0 then ("OUTPUT #{@outputs.join ', '} ") else ''}VALUES (#{vals.join('), (')})"
origInsert = squel.insert
squel.insert = -> origInsert options, [
new squel.cls.StringBlock(options, 'INSERT'),
new squel.cls.IntoTableBlock(options),
new InsertFieldValueBlock(options)
]
Means that this:
squel.insert()
.into "tasks"
.output ["id","orderid"]
.setSQL "orderid", "((#{squel.select()
.field "COUNT(*)", "count"
.from "tasks"
.where "ticketid = ?", 24}) + 1)"
.setSQL "createdat", "GETDATE()"
Outputs this (Line breaks for readability):
INSERT INTO tasks (orderid, createdat)
OUTPUT INSERTED.id, INSERTED.orderid
VALUES (((SELECT COUNT(*) AS count FROM tasks WHERE (ticketid = 24)) + 1), GETDATE())
from squel.
It seems that it might be nice to be able to tell Squel not to quote a particular string. I'll look into adding something of this sort.
from squel.
Also see #55 for alternative viewpoint - we could just have Squel recognize its own query builders.
from squel.
Yea, #55 would definitely solve my first case where I'm using a sub-query. How would it solve my second use case of sql function calls like GETDATE()
?
from squel.
Ok, field values can now be query builder instances (will be published in 3.5 release).
As for doing this like GETDATE(), since that's not a built-in query type supported by squel I suggest registering a custom value handler, like so:
var SqlValue = function(str) {this._str = str;}
squel.registerValueHandler(SqlValue, function(sqlValue) {
return sqlValue.str;
});
squel.insert()
.into("students")
.set("field", new SqlValue('GETDATE()'))
;
/* INSERT INTO students (field) VALUES (GETDATE()) */
Please give that a shot and let me know.
from squel.
Doesn't seem to work it still quotes the value. Did I do something wrong?
from squel.
No, you did it right. Let me see what solution I can conjure up.
from squel.
Ability to disable quotes on a per-set() basis committed. Will be in 3.5.
from squel.
Related Issues (20)
- Add option to add column/table name as a parameter HOT 1
- Add semantic-comparison logic for queries/expressions
- Feature request: Placeholders in the squel.case(...) expression?
- MSSQL Boolean type
- .group().having() is not a function HOT 1
- string escape is incorrect, especially when string contains \n or ', which may cause SQL injection HOT 1
- How to select into array? HOT 1
- Scalar values aliasing
- squel.select().from is undefined after minimizing HOT 6
- Numbered parameters in postgres flavour are substituted inside a string literal
- Incorrect syntax near '`'.
- SQL Server Update Fails with Trigger ? HOT 3
- Use a normal SQL string HOT 2
- Can you get the result value from an object like the result map of mybtis?
- SQL Server Select HOT 1
- Interested in Becoming an Active co-maintainer HOT 1
- How to use IGNORE in INSERT in mysql?
- Mark as deprecated HOT 1
- Grouping conditions together in expression HOT 1
- Lateral Joins
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from squel.