Masterminds / squirrel

Fluent SQL generation for golang
Other
6.67k stars 458 forks source link

Having both $ed and raw values in `SetMap` map #377

Open HugoMYM opened 2 months ago

HugoMYM commented 2 months ago

Hi and thanks for your great jobs. It's nice to have such a tool to write sql queries nicely, you do a great job for maintenability.

I'm in a situation where I need to have both parameterized and raw values in an insert statement. The point is to generate a request like

INSERT INTO my_table COLUMNS (col_a, col_b, col_c) VALUES ($1, 42, $2)

I didn't find anything to do so in the lib, but I've found this piece of code that inspired me to find a solution to my problem

// insert.go 127

for v, val := range row {
    if vs, ok := val.(Sqlizer); ok {
        vsql, vargs, err := vs.ToSql()
        if err != nil {
            return nil, err
        }
        valueStrings[v] = vsql
        args = append(args, vargs...)
    } else {
        valueStrings[v] = "?"
        args = append(args, val)
    }
}

So for now I'm just having a type RawSQL such as

type Raw string

func (r Raw) ToSql() (string, []interface{}, error) {
    return string(r), nil, nil
}

This works, and I was wondering if I'm missing anything from the lib or if it could be an interesting idea to implement it directly on the lib