Skip to content

Commit

Permalink
fix(templates): updating templates to check cast was okay and prevent…
Browse files Browse the repository at this point in the history
… panic (#90)

* updating templates to check cast was okay and prevent panic

* models update
  • Loading branch information
Jacobbrewer1 authored Mar 6, 2025
1 parent 2f09456 commit 206830e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
4 changes: 2 additions & 2 deletions pkg/models/db.xo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type DB interface {
Exec(string, ...any) (sql.Result, error)
Query(string, ...any) (*sql.Rows, error)
QueryRow(string, ...any) *sql.Row
Get(dest any, query string, args ...interface{}) error
Select(dest any, query string, args ...interface{}) error
Get(dest any, query string, args ...any) error
Select(dest any, query string, args ...any) error
}

// Transactioner is the interface that a database connection that can start
Expand Down
14 changes: 8 additions & 6 deletions pkg/models/goschema_migration_history.xo.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ func GoschemaMigrationHistoryById(db DB, id int) (*GoschemaMigrationHistory, err
}

type goschemaMigrationHistoryPKWherer struct {
ids []interface{}
ids []any
}

func (m goschemaMigrationHistoryPKWherer) Where() (string, []interface{}) {
func (m goschemaMigrationHistoryPKWherer) Where() (string, []any) {
return "`id` = ?", m.ids
}

Expand All @@ -223,7 +223,7 @@ func (m *GoschemaMigrationHistory) Patch(db DB, newT *GoschemaMigrationHistory)
newT,
patcher.WithTable(GoschemaMigrationHistoryTableName),
patcher.WithWhere(&goschemaMigrationHistoryPKWherer{
ids: []interface{}{m.Id},
ids: []any{m.Id},
}),
patcher.WithIgnoredFields(
"Id",
Expand Down Expand Up @@ -261,11 +261,12 @@ func GetAllGoschemaMigrationHistorys(db DB, filters ...any) ([]*GoschemaMigratio

args := make([]any, 0)
builder := new(strings.Builder)
builder.WriteString("SELECT `t.id`, `t.version`, `t.action`, `t.created_at`")
builder.WriteString("SELECT t.id, t.version, t.action, t.created_at")

if len(filters) > 0 {
for _, filter := range filters {
if joiner := filter.(patcher.Joiner); joiner != nil {
joiner, ok := filter.(patcher.Joiner)
if ok {
joinSql, joinArgs := joiner.Join()
builder.WriteString(joinSql)
args = append(args, joinArgs...)
Expand All @@ -278,7 +279,8 @@ func GetAllGoschemaMigrationHistorys(db DB, filters ...any) ([]*GoschemaMigratio
if len(filters) > 0 {
builder.WriteString("\nWHERE\n")
for i, filter := range filters {
if where := filter.(patcher.Wherer); where != nil {
where, ok := filter.(patcher.Wherer)
if ok {
if i > 0 {
wtStr := patcher.WhereTypeAnd
if wt, ok := filter.(patcher.WhereTyper); ok {
Expand Down
14 changes: 8 additions & 6 deletions pkg/models/goschema_migration_version.xo.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ func GoschemaMigrationVersionByVersion(db DB, version string) (*GoschemaMigratio
}

type goschemaMigrationVersionPKWherer struct {
ids []interface{}
ids []any
}

func (m goschemaMigrationVersionPKWherer) Where() (string, []interface{}) {
func (m goschemaMigrationVersionPKWherer) Where() (string, []any) {
return "`version` = ?", m.ids
}

Expand All @@ -192,7 +192,7 @@ func (m *GoschemaMigrationVersion) Patch(db DB, newT *GoschemaMigrationVersion)
newT,
patcher.WithTable(GoschemaMigrationVersionTableName),
patcher.WithWhere(&goschemaMigrationVersionPKWherer{
ids: []interface{}{m.Version},
ids: []any{m.Version},
}),
patcher.WithIgnoredFields(
"Version",
Expand Down Expand Up @@ -230,11 +230,12 @@ func GetAllGoschemaMigrationVersions(db DB, filters ...any) ([]*GoschemaMigratio

args := make([]any, 0)
builder := new(strings.Builder)
builder.WriteString("SELECT `t.version`, `t.is_current`, `t.created_at`")
builder.WriteString("SELECT t.version, t.is_current, t.created_at")

if len(filters) > 0 {
for _, filter := range filters {
if joiner := filter.(patcher.Joiner); joiner != nil {
joiner, ok := filter.(patcher.Joiner)
if ok {
joinSql, joinArgs := joiner.Join()
builder.WriteString(joinSql)
args = append(args, joinArgs...)
Expand All @@ -247,7 +248,8 @@ func GetAllGoschemaMigrationVersions(db DB, filters ...any) ([]*GoschemaMigratio
if len(filters) > 0 {
builder.WriteString("\nWHERE\n")
for i, filter := range filters {
if where := filter.(patcher.Wherer); where != nil {
where, ok := filter.(patcher.Wherer)
if ok {
if i > 0 {
wtStr := patcher.WhereTypeAnd
if wt, ok := filter.(patcher.WhereTyper); ok {
Expand Down
2 changes: 1 addition & 1 deletion pkg/models/helpers.xo.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func NewLoggableDBTransactionHandler(db Transactioner, l *slog.Logger) *Loggable
// 1. x is an integer and greater than zero.
// 2. x not an integer and is not the zero value.
// Otherwise, returns false
func IsKeySet(x interface{}) bool {
func IsKeySet(x any) bool {
switch x := x.(type) {
case int:
return x > 0
Expand Down
8 changes: 5 additions & 3 deletions templates/model.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,12 @@ func GetAll{{ $struct }}s(db DB, filters ...any) ([]*{{ $struct }}, error) {

args := make([]any, 0)
builder := new(strings.Builder)
builder.WriteString("SELECT {{ range $i, $column := $.Table.Columns }}{{ if $i }}, {{ end }}`t.{{ $column.Name }}`{{ end }}")
builder.WriteString("SELECT {{ range $i, $column := $.Table.Columns }}{{ if $i }}, {{ end }}t.{{ $column.Name }}{{ end }}")

if len(filters) > 0 {
for _, filter := range filters {
if joiner := filter.(patcher.Joiner); joiner != nil {
joiner, ok := filter.(patcher.Joiner)
if ok {
joinSql, joinArgs := joiner.Join()
builder.WriteString(joinSql)
args = append(args, joinArgs...)
Expand All @@ -246,7 +247,8 @@ func GetAll{{ $struct }}s(db DB, filters ...any) ([]*{{ $struct }}, error) {
if len(filters) > 0 {
builder.WriteString("\nWHERE\n")
for i, filter := range filters {
if where := filter.(patcher.Wherer); where != nil {
where, ok := filter.(patcher.Wherer)
if ok {
if i > 0 {
wtStr := patcher.WhereTypeAnd
if wt, ok := filter.(patcher.WhereTyper); ok {
Expand Down

0 comments on commit 206830e

Please sign in to comment.