Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
HugoMYM opened this issue Apr 21, 2024 · 0 comments
Open

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

HugoMYM opened this issue Apr 21, 2024 · 0 comments

Comments

@HugoMYM
Copy link

HugoMYM commented Apr 21, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant