Giter Club home page Giter Club logo

Comments (8)

SimeonC avatar SimeonC commented on July 26, 2024

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.

hiddentao avatar hiddentao commented on July 26, 2024

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.

hiddentao avatar hiddentao commented on July 26, 2024

Also see #55 for alternative viewpoint - we could just have Squel recognize its own query builders.

from squel.

SimeonC avatar SimeonC commented on July 26, 2024

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.

hiddentao avatar hiddentao commented on July 26, 2024

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.

SimeonC avatar SimeonC commented on July 26, 2024

Doesn't seem to work it still quotes the value. Did I do something wrong?
image

from squel.

hiddentao avatar hiddentao commented on July 26, 2024

No, you did it right. Let me see what solution I can conjure up.

from squel.

hiddentao avatar hiddentao commented on July 26, 2024

Ability to disable quotes on a per-set() basis committed. Will be in 3.5.

from squel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.