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

feat(templates): Adding a new const to contain the table name #55

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading