From e39d4c3a13e90581ff0d75b5a134f019665f9b11 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Tue, 12 Jul 2022 21:05:45 +0200 Subject: [PATCH] show sql query on sql error --- db.go | 3 ++- db_test.go | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/db.go b/db.go index 5edd74d..7d8bfb5 100644 --- a/db.go +++ b/db.go @@ -2,6 +2,7 @@ package advsql import ( "database/sql" + "fmt" _ "github.com/go-sql-driver/mysql" ) @@ -53,7 +54,7 @@ func (db *Database) stmt(query string) *sql.Stmt { stmt, err := db.db.Prepare(query) if err != nil { - panic(err) + panic(fmt.Errorf("compilation failed for query '%s' reason: %w", query, err)) } db.stmts[query] = stmt return stmt diff --git a/db_test.go b/db_test.go index 07e051a..4ad9160 100644 --- a/db_test.go +++ b/db_test.go @@ -9,7 +9,7 @@ import ( _ "github.com/go-sql-driver/mysql" ) -var TestDatabase *Database //= new(Database) +var TestDatabase = new(Database) type User struct { Name string @@ -28,11 +28,12 @@ func ScanUserPkLast(u *User, encode ScanFunc) error { var ( InsertUser = Insert(TestDatabase, "INSERT INTO users VALUES (?, ?, ?)", ScanUserPkFirst) UpdateUser = Update(TestDatabase, "UPDATE users SET hash = ?, salt = ? WHERE name = ?", ScanUserPkLast) - GetUserByName = QueryOne(TestDatabase, "SELECT * FROM users WHERE name = ?", ScanUserPkFirst) + GetUserByName = QueryOne(TestDatabase, "SELECT * FROM users WHERE namea = ?", ScanUserPkFirst) ) func TestDB(t *testing.T) { - InitMysqlDatabase(TestDatabase, "ip", 3306, "username", "password", "database") + InitMysqlDatabase(TestDatabase, "192.168.178.2", 3306, "root", "6MvtRA4iZJtbenR537otFnoHpzt9vYN7", "users") + //InitMysqlDatabase(TestDatabase, "ip", 3306, "username", "password", "database") defer TestDatabase.Close() pw := sha512.Sum512([]byte("weiter"))