Skip to content

Commit

Permalink
feat(templates): Adding a new const to contain the table name (#55)
Browse files Browse the repository at this point in the history
Adding a new const that is the table name
  • Loading branch information
Jacobbrewer1 authored Dec 20, 2024
1 parent d459496 commit 45db16d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion templates/_delete.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 . -}}
Expand Down
6 changes: 3 additions & 3 deletions templates/_insert.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 . }}
Expand Down Expand Up @@ -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))
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion templates/_insert_update.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 . }}
Expand Down
4 changes: 2 additions & 2 deletions templates/_update.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 . -}}
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 7 additions & 1 deletion templates/model.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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 }} " +
Expand Down

0 comments on commit 45db16d

Please sign in to comment.