From bee9e953d266772b42d58d9caadbfd41e0b73d08 Mon Sep 17 00:00:00 2001 From: Jacob Brewer Date: Fri, 31 Jan 2025 12:56:14 +0000 Subject: [PATCH] feat(templates): updating get all template to take in filters using the patcher wherer interface (#75) updating get all template to take in filters using the patcher wherer interface --- templates/model.tmpl | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/templates/model.tmpl b/templates/model.tmpl index 0c1d671..912824e 100644 --- a/templates/model.tmpl +++ b/templates/model.tmpl @@ -220,19 +220,40 @@ func {{ $struct }}By{{ range $i, $col := $key.Columns }}{{ $col.Name | structify {{ end }} {{ end }} -// GetAll{{ $struct }} retrieves all rows from '{{ .Name }}' as a slice of {{ $struct }}. +// GetAll{{ $struct }}s retrieves all rows from '{{ .Name }}' as a slice of {{ $struct }}. // // Generated from table '{{ .Name }}'. -func GetAll{{ $struct }}(db DB) ([]*{{ $struct }}, error) { +func GetAll{{ $struct }}s(db DB, wheres ...patcher.Wherer) ([]*{{ $struct }}, error) { t := prometheus.NewTimer(DatabaseLatency.WithLabelValues("get_all_" + {{ $struct | structify }}TableName)) defer t.ObserveDuration() - const sqlstr = "SELECT {{ range $i, $column := $.Table.Columns }}{{ if $i }}, {{ end }}`{{ $column.Name }}`{{ end }} " + - "FROM {{ $.Table.Name }}" + args := make([]any, 0) + builder := new(strings.Builder) + builder.WriteString("SELECT {{ range $i, $column := $.Table.Columns }}{{ if $i }}, {{ end }}`{{ $column.Name }}`{{ end }}") + builder.WriteString(" FROM {{ $.Table.Name }}") + + if len(wheres) > 0 { + builder.WriteString(" WHERE ") + for i, where := range wheres { + if i > 0 { + wtStr := patcher.WhereTypeAnd // default to AND + wt, ok := where.(patcher.WhereTyper) + if ok && wt.WhereType().IsValid() { + wtStr = wt.WhereType() + } + builder.WriteString(string(wtStr) + " ") + } + whereStr, whereArgs := where.Where() + builder.WriteString(whereStr) + args = append(args, whereArgs...) + } + } + + sqlstr := builder.String() + DBLog(sqlstr, args...) - DBLog(sqlstr) m := make([]*{{ $struct }}, 0) - if err := db.Select(&m, sqlstr); err != nil { + if err := db.Select(&m, sqlstr, args...); err != nil { return nil, fmt.Errorf("failed to get all {{ $struct }}: %w", err) }