{{- /*
rom, reads schema information from various DBMS and generates according code.
Copyright (C) 2021 Ruben Schuller <code@rbn.im>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ -}}
const (
// SQL to insert a row in {{.QualifiedName}}
sqlInsert{{ .GoName }} = "INSERT INTO {{ .QualifiedName }} ({{ range $i, $c := .Columns }}{{ if $i }}, {{ end }}{{ $c.QualifiedName }}{{ end }}) VALUES ({{ range $i, $c := .Columns }}{{ if $i }}, {{end}}?{{end}});"
)
func (x *{{ .GoName }}) prepareInsert(p Preparer) (*sql.Stmt, error) {
return p.Prepare(sqlInsert{{ .GoName }})
}
func (x *{{ .GoName }}) prepareContextInsert(ctx context.Context, p ContextPreparer) (*sql.Stmt, error) {
return p.PrepareContext(ctx, sqlInsert{{ .GoName }})
}
// Insert inserts {{.GoName}} into the table {{.QualifiedName}}.
func (x *{{ .GoName }}) Insert(p Preparer) (sql.Result, error) {
stmt, err := x.prepareInsert(p)
if err != nil {
return nil, err
}
defer stmt.Close()
return stmt.Exec(
{{- range $i, $c := .Columns -}}
{{- if $i }}, {{ end }}x.{{ $c.GoName }}
{{- end -}}
)
}
// InsertContext inserts {{.GoName}} into the table {{.QualifiedName}}
func (x *{{ .GoName }}) InsertContext(ctx context.Context, p ContextPreparer) (sql.Result, error) {
stmt, err := x.prepareContextInsert(ctx, p)
if err != nil {
return nil, err
}
defer stmt.Close()
return stmt.ExecContext(ctx,
{{- range $i, $c := .Columns -}}
{{- if $i }}, {{ end }}x.{{ $c.GoName }}
{{- end -}}
)
}