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

templates: how to create WHERE string for multiple primary keys #317

Open
fho opened this issue Sep 9, 2021 · 0 comments
Open

templates: how to create WHERE string for multiple primary keys #317

fho opened this issue Sep 9, 2021 · 0 comments

Comments

@fho
Copy link
Contributor

fho commented Sep 9, 2021

Hello,

I wonder what the intended way is to build a WHERE statements to match all primary key in a template like:
WHERE id1 = $1 AND id2 = $2 AND id3 = $3.
With intended I means, without using legacy or custom functions.

The only way I found is really ugly:

sqlstr + = " WHERE"
{{ range $i, $field := $t.PrimaryKeys -}}
       // Sprintf code is generated because of a missing arith. add template function
	sqlstr+= " {{ $field.SQLName }} =  $" + fmt.Sprint(i - 1)
       // if statement code is generated because comparision can not be done in template, because of missing add function
	if {{ $i }} + 1 != {{ (len $t.PrimaryKeys) }} {
		sqlstr += " AND"
	}
{{ end -}}

I could define add, sub functions in funcs.go.tpl to improve it.
This seems such a basic scenario though that I think that I'm probably missing the simple intended way to do it? :-)

thanks

@fho fho changed the title templates: how to create WHERE string for multiple primarykeys templates: how to create WHERE string for multiple primary keys Sep 21, 2021
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