diff --git a/templates/_delete.tmpl b/templates/_delete.tmpl index 278789f..b8acaf1 100644 --- a/templates/_delete.tmpl +++ b/templates/_delete.tmpl @@ -2,7 +2,7 @@ {{- $struct := .Name | structify -}} // Delete deletes the {{ $struct }} from the database. func (m *{{ $struct }}) Delete(db DB) error { - t := prometheus.NewTimer(DatabaseLatency.WithLabelValues("delete_{{ $struct | structify -}}")) + t := prometheus.NewTimer(DatabaseLatency.WithLabelValues("delete_" + {{ $struct }}TableName)) defer t.ObserveDuration() {{ if identity_columns . -}} diff --git a/templates/_insert.tmpl b/templates/_insert.tmpl index 0346a29..7d514fd 100644 --- a/templates/_insert.tmpl +++ b/templates/_insert.tmpl @@ -2,7 +2,7 @@ {{- $struct := .Name | structify -}} // Insert inserts the {{ $struct }} to the database. func (m *{{ $struct }}) Insert(db DB) error { - t := prometheus.NewTimer(DatabaseLatency.WithLabelValues("insert_{{ $struct | structify -}}")) + t := prometheus.NewTimer(DatabaseLatency.WithLabelValues("insert_" + {{ $struct | structify }}TableName)) defer t.ObserveDuration() {{ $autoinc := autoinc_column . }} @@ -37,7 +37,7 @@ func InsertMany{{ $struct }}s(db DB, ms ...*{{ $struct }}) error { return nil } - t := prometheus.NewTimer(DatabaseLatency.WithLabelValues("insert_many_{{ $struct | structify -}}")) + t := prometheus.NewTimer(DatabaseLatency.WithLabelValues("insert_many_" + {{ $struct | structify }}TableName)) defer t.ObserveDuration() vals := make([]any, 0, len(ms)) @@ -46,7 +46,7 @@ func InsertMany{{ $struct }}s(db DB, ms ...*{{ $struct }}) error { vals = append(vals, []any{*m}) } - sqlstr, args, err := inserter.NewBatch(vals, inserter.WithTable("{{ .Name }}")).GenerateSQL() + sqlstr, args, err := inserter.NewBatch(vals, inserter.WithTable({{ $struct | structify }}TableName)).GenerateSQL() if err != nil { return fmt.Errorf("failed to create batch insert: %w", err) } diff --git a/templates/_insert_update.tmpl b/templates/_insert_update.tmpl index 4dab1e5..cc6bfc8 100644 --- a/templates/_insert_update.tmpl +++ b/templates/_insert_update.tmpl @@ -3,7 +3,7 @@ // InsertWithUpdate inserts the {{ $struct }} to the database, and tries to update // on unique constraint violations. func (m *{{ $struct }}) InsertWithUpdate(db DB) error { - t := prometheus.NewTimer(DatabaseLatency.WithLabelValues("insert_update_{{ $struct | structify -}}")) + t := prometheus.NewTimer(DatabaseLatency.WithLabelValues("insert_update_" + {{ $struct }}TableName)) defer t.ObserveDuration() {{ $autoinc := autoinc_column . }} diff --git a/templates/_update.tmpl b/templates/_update.tmpl index cb22ebe..90a9de3 100644 --- a/templates/_update.tmpl +++ b/templates/_update.tmpl @@ -2,7 +2,7 @@ {{- $struct := .Name | structify -}} // Update updates the {{ $struct }} in the database. func (m *{{ $struct }}) Update(db DB) error { - t := prometheus.NewTimer(DatabaseLatency.WithLabelValues("update_{{ $struct | structify -}}")) + t := prometheus.NewTimer(DatabaseLatency.WithLabelValues("update_" + {{ $struct }}TableName)) defer t.ObserveDuration() {{ $cols := non_identity_columns . -}} @@ -32,7 +32,7 @@ func (m *{{ $struct }}) Patch(db DB, newT *{{ $struct }}) error { return errors.New("new {{ .Name }} is nil") } - res, err := patcher.NewDiffSQLPatch(m, newT, patcher.WithTable("{{ .Name }}")) + res, err := patcher.NewDiffSQLPatch(m, newT, patcher.WithTable({{ $struct | structify -}}TableName)) if err != nil { return fmt.Errorf("new diff sql patch: %w", err) } diff --git a/templates/model.tmpl b/templates/model.tmpl index a10d942..97bca19 100644 --- a/templates/model.tmpl +++ b/templates/model.tmpl @@ -16,6 +16,12 @@ import ( ) {{ with .Table }} {{ $struct := .Name | structify }} + +const ( + // {{ $struct }}TableName is the name of the table for the {{ $struct }} model. + {{ $struct }}TableName = "{{ .Name }}" +) + // {{ $struct }} represents a row from '{{ .Name }}'. {{- if .Comment }} // {{ .Comment }} @@ -136,7 +142,7 @@ func (m *{{ $struct }}) Get{{ $local_col | structify }}{{ $foreign_struct }}(db // // Generated from index '{{ $key.Name }}' of type '{{ $key.Type }}'. func {{ $struct }}By{{ range $i, $col := $key.Columns }}{{ $col.Name | structify }}{{ end }}(db DB, {{ range $i, $col := $key.Columns }}{{ if $i }}, {{ end }}{{ $col.Name | structify | lcfirst }} {{ get_type $col}}{{ end }}) ({{ if not $uniq }}[]{{ end }}*{{ $struct }}, error) { - t := prometheus.NewTimer(DatabaseLatency.WithLabelValues("insert_{{ $struct | structify -}}")) + t := prometheus.NewTimer(DatabaseLatency.WithLabelValues("insert_" + {{ $struct | structify }}TableName)) defer t.ObserveDuration() const sqlstr = "SELECT {{ range $i, $column := $.Table.Columns }}{{ if $i }}, {{ end }}`{{ $column.Name }}`{{ end }} " +