From 8db9dc983d1dc7339d615e5878f15691f21bdb39 Mon Sep 17 00:00:00 2001 From: Fritz Larco Date: Sun, 29 Dec 2024 20:23:10 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(dbio):=20handle=20nil=20conn?= =?UTF-8?q?ection=20instance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- core/dbio/database/database.go | 8 ++++++-- core/dbio/iop/dataset.go | 18 +++++++++--------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/core/dbio/database/database.go b/core/dbio/database/database.go index 0268135a..e2cfa20f 100755 --- a/core/dbio/database/database.go +++ b/core/dbio/database/database.go @@ -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") @@ -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) { diff --git a/core/dbio/iop/dataset.go b/core/dbio/iop/dataset.go index 4886f83e..4c198f42 100644 --- a/core/dbio/iop/dataset.go +++ b/core/dbio/iop/dataset.go @@ -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