Skip to content

Commit

Permalink
🐛 fix(dbio): handle nil connection instance
Browse files Browse the repository at this point in the history
- added error handling for nil connection instance in Prepare and ExecContext methods
- improved error messages for better debugging
- updated Dataset struct to include json tags for better serialization
  • Loading branch information
flarco committed Dec 29, 2024
1 parent 9f2fdc5 commit 8db9dc9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
8 changes: 6 additions & 2 deletions core/dbio/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,8 +1070,10 @@ func (conn *BaseConn) Rollback() (err error) {
func (conn *BaseConn) Prepare(query string) (stmt *sql.Stmt, err error) {
if conn.tx != nil {
stmt, err = conn.tx.Prepare(query)
} else {
} else if conn.db != nil {
stmt, err = conn.db.PrepareContext(conn.Context().Ctx, query)
} else {
err = g.Error("no connection instance")
}
if err != nil {
err = g.Error(err, "could not prepare statement")
Expand Down Expand Up @@ -1125,9 +1127,11 @@ func (conn *BaseConn) ExecContext(ctx context.Context, q string, args ...interfa
if conn.tx != nil {
result, err = conn.tx.ExecContext(ctx, q, args...)
q = q + noDebugKey // just to not show twice the sql in error since tx does
} else {
} else if conn.db != nil {
conn.LogSQL(q, args...)
result, err = conn.db.ExecContext(ctx, q, args...)
} else {
err = g.Error("no connection instance")
}
if err != nil {
if strings.Contains(q, noDebugKey) {
Expand Down
18 changes: 9 additions & 9 deletions core/dbio/iop/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import (

// Dataset is a query returned dataset
type Dataset struct {
Result *sqlx.Rows
Columns Columns
Rows [][]interface{}
SQL string
Duration float64
Sp *StreamProcessor
Inferred bool
SafeInference bool
NoDebug bool
Result *sqlx.Rows `json:"-"`
Columns Columns `json:"columns"`
Rows [][]any `json:"rows"`
SQL string `json:"sql"`
Duration float64 `json:"duration"`
Sp *StreamProcessor `json:"-"`
Inferred bool `json:"inferred"`
SafeInference bool `json:"safe_inference"`
NoDebug bool `json:"no_debug"`
}

// NewDataset return a new dataset
Expand Down

0 comments on commit 8db9dc9

Please sign in to comment.