Skip to content
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
53 changes: 52 additions & 1 deletion gen/integration/sqlx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
"strings"
"time"

_ "github.com/mattn/go-sqlite3"
defc "github.com/x5iu/defc/runtime"

_ "github.com/mattn/go-sqlite3"
)

var executor Executor
Expand Down Expand Up @@ -158,6 +159,40 @@ func main() {
}

log.Println("All constbind tests passed!")

// Test that panic in Scan propagates correctly
// When a struct field's Scan method panics, the panic should propagate
// to the caller without causing deadlock.
func() {
// Create a separate database connection for this test
panicDB := defc.MustOpen("sqlite3", ":memory:")
panicDB.Exec("CREATE TABLE user (id INTEGER PRIMARY KEY, name TEXT)")
panicDB.Exec("INSERT INTO user (id, name) VALUES (1, 'test')")
panicCore := &sqlc{panicDB}
panicExecutor := NewExecutorFromCore(panicCore)

done := make(chan struct{})
go func() {
defer close(done)
defer func() {
if r := recover(); r == nil {
log.Fatalln("Expected panic but got none")
} else {
log.Printf("Expected panic occurred: %v\n", r)
}
}()
// This should panic, not return an error
_, _ = panicExecutor.GetPanicUser(ctx, 1)
}()
select {
case <-done:
// Test completed successfully (panic was caught in goroutine)
case <-time.After(30 * time.Second):
log.Fatalln("Test timeout: panic test exceeded 30 seconds")
}
}()

log.Println("All tests passed!")
}

type sqlc struct {
Expand Down Expand Up @@ -272,6 +307,11 @@ type Executor interface {
// /* {"name": "defc", "action": "test"} */
// update user set name = ${newName} where id = ${id};
UpdateUserName(ctx context.Context, id int64, newName string) (sql.Result, error)

// GetPanicUser query constbind
// /* {"name": "defc", "action": "test"} */
// SELECT id, name from user where id = ${id};
GetPanicUser(ctx context.Context, id int64) (*PanicUser, error)
}

type UserID struct {
Expand Down Expand Up @@ -350,6 +390,17 @@ func (project *Project) FromRow(row defc.Row) error {
)
}

type PanicUser struct {
ID PanicID `db:"id"`
Name string `db:"name"`
}

type PanicID int64

func (pid *PanicID) Scan(src any) error {
panic("PanicID.Scan: should panic")
}

func sqlComment(context.Context) string {
return `/* {"name": "defc", "action": "test"} */`
}
7 changes: 4 additions & 3 deletions gen/template/sqlx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,6 @@ var (
if {{ $tx }} == nil {
panic("tx is nil")
}
if !__imp.__withTx{
defer {{ $tx }}.Rollback()
}

{{ $offset := printf "offset%s" $method.Ident -}}
{{ $args := printf "args%s" $method.Ident -}}
Expand All @@ -268,6 +265,7 @@ var (
{{- if $.HasFeature "sqlx/in" }}
{{ $query }}, {{ $args }}, {{ $err }} := {{ if $.HasFeature "sqlx/nort" }}{{ $inFunc }}{{ else }}__rt.In{{ end }}({{ $query }}, {{ $argList }})
if {{ $err }} != nil {
if !__imp.__withTx { {{ $tx }}.Rollback() }
return {{ range $index, $type := $method.Out -}}
{{- if lt $index (sub (len $method.Out) 1) -}}
v{{- $index -}}{{- $method.Ident }},
Expand Down Expand Up @@ -296,6 +294,7 @@ var (

{{ $splitSql }}, {{ $argList }}, {{ $err }} = sqlx.Named({{ $splitSql }}, {{ $args }})
if {{ $err }} != nil {
if !__imp.__withTx { {{ $tx }}.Rollback() }
return {{ range $index, $type := $method.Out -}}
{{- if lt $index (sub (len $method.Out) 1) -}}
v{{- $index -}}{{- $method.Ident }},
Expand All @@ -309,6 +308,7 @@ var (
{{ $splitSql }}, {{ $argList }}, {{ $err }} = sqlx.In({{ $splitSql }}, {{ $argList }}...)
{{ end -}}
if {{ $err }} != nil {
if !__imp.__withTx { {{ $tx }}.Rollback() }
return {{ range $index, $type := $method.Out -}}
{{- if lt $index (sub (len $method.Out) 1) -}}
v{{- $index -}}{{- $method.Ident }},
Expand Down Expand Up @@ -368,6 +368,7 @@ var (
{{ end }}

if {{ $err }} != nil {
if !__imp.__withTx { {{ $tx }}.Rollback() }
return {{ range $index, $type := $method.Out -}}
{{- if lt $index (sub (len $method.Out) 1) -}}
v{{- $index -}}{{- $method.Ident }},
Expand Down
2 changes: 1 addition & 1 deletion runtime/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package defc

const Version = "v1.44.2"
const Version = "v1.44.3"
29 changes: 24 additions & 5 deletions sqlx/sqlx.refined.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading