package advsql import ( "crypto/sha512" "encoding/hex" "fmt" "testing" _ "github.com/go-sql-driver/mysql" ) var TestDatabase *Database //= new(Database) type User struct { Name string Hash []byte Salt string } func ScanUserPkFirst(u *User, encode ScanFunc) error { return encode(&u.Name, &u.Hash, &u.Salt) } func ScanUserPkLast(u *User, encode ScanFunc) error { return encode(&u.Hash, &u.Salt, &u.Name) } 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) ) func TestDB(t *testing.T) { InitMysqlDatabase(TestDatabase, "ip", 3306, "username", "password", "database") defer TestDatabase.Close() pw := sha512.Sum512([]byte("weiter")) timon := &User{ Name: "timon", Hash: pw[:], Salt: "salt", } fmt.Println("insert:", InsertUser(timon)) timon.Hash = []byte("asd") fmt.Println("update:", UpdateUser(timon)) user := GetUserByName("tordarus") fmt.Printf("name: \"%s\" | hash: \"%s\" | salt: \"%s\"\n", user.Name, hex.EncodeToString(user.Hash), user.Salt) }