squirrel icon indicating copy to clipboard operation
squirrel copied to clipboard

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

Open HugoMYM opened this issue 1 year ago • 0 comments

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

HugoMYM avatar Apr 21 '24 20:04 HugoMYM