show sql query on sql error

This commit is contained in:
Timon Ringwald 2022-07-12 21:05:45 +02:00
parent 96484eba5a
commit e39d4c3a13
2 changed files with 6 additions and 4 deletions

3
db.go
View File

@ -2,6 +2,7 @@ package advsql
import ( import (
"database/sql" "database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
@ -53,7 +54,7 @@ func (db *Database) stmt(query string) *sql.Stmt {
stmt, err := db.db.Prepare(query) stmt, err := db.db.Prepare(query)
if err != nil { if err != nil {
panic(err) panic(fmt.Errorf("compilation failed for query '%s' reason: %w", query, err))
} }
db.stmts[query] = stmt db.stmts[query] = stmt
return stmt return stmt

View File

@ -9,7 +9,7 @@ import (
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
var TestDatabase *Database //= new(Database) var TestDatabase = new(Database)
type User struct { type User struct {
Name string Name string
@ -28,11 +28,12 @@ func ScanUserPkLast(u *User, encode ScanFunc) error {
var ( var (
InsertUser = Insert(TestDatabase, "INSERT INTO users VALUES (?, ?, ?)", ScanUserPkFirst) InsertUser = Insert(TestDatabase, "INSERT INTO users VALUES (?, ?, ?)", ScanUserPkFirst)
UpdateUser = Update(TestDatabase, "UPDATE users SET hash = ?, salt = ? WHERE name = ?", ScanUserPkLast) 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) { 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() defer TestDatabase.Close()
pw := sha512.Sum512([]byte("weiter")) pw := sha512.Sum512([]byte("weiter"))