diff --git a/db_test.go b/db_test.go index e36c191..44a03c9 100644 --- a/db_test.go +++ b/db_test.go @@ -15,30 +15,30 @@ type User struct { Salt string } -func ScanUser(u *User, scan ScanFunc) error { - return scan(&u.Name, &u.Hash, &u.Salt) +func UserDecoder(u *User, decode DecodeFunc) error { + return decode(&u.Name, &u.Hash, &u.Salt) } -func ExecUser(u *User, exec ExecFunc) error { - return exec(u.Name, u.Hash, u.Salt) +func InsertUserEncoder(u *User, encode EncodeFunc) error { + return encode(u.Name, u.Hash, u.Salt) } -func UpdateUser(u *User, exec ExecFunc) error { - return exec(u.Salt, u.Name) +func UpdateUserByNameEncoder(u *User, encode EncodeFunc) error { + return encode(u.Salt, u.Name) } func TestDB(t *testing.T) { - db, err := NewDatabase("192.168.178.2", 3306, "root", "ZAuJsLdPYFSxHdZon7xpMyh5LW7TPhmM", "users") + db, err := NewMysqlDatabase("ip", 3306, "username", "password", "database") if err != nil { t.Fatal(err) } defer db.Close() - insertUser := Insert(db, "INSERT INTO users VALUES (?, ?, ?)", ExecUser) + insertUser := Insert(db, "INSERT INTO users VALUES (?, ?, ?)", InsertUserEncoder) - updateUser := Insert(db, "UPDATE users SET salt = ? WHERE name = ?", UpdateUser) + updateUser := Insert(db, "UPDATE users SET salt = ? WHERE name = ?", UpdateUserByNameEncoder) - getUsers := Query(db, "SELECT * FROM users WHERE name = ?", ScanUser) + getUsers := Query(db, "SELECT * FROM users WHERE name = ?", UserDecoder) pw := sha512.Sum512([]byte("weiter")) timon := &User{ diff --git a/insert.go b/insert.go index 8fa7e92..cfcf307 100644 --- a/insert.go +++ b/insert.go @@ -1,13 +1,13 @@ package advsql -func Insert[T any](db *Database, query string, exec func(v *T, exec ExecFunc) error) InsertFunc[T] { +func Insert[T any](db *Database, query string, encoder func(v *T, encode EncodeFunc) error) InsertFunc[T] { s, err := db.prepare(query) if err != nil { return nil } return func(value *T) error { - return exec(value, func(args ...interface{}) error { + return encoder(value, func(args ...interface{}) error { _, err := s.Exec(args...) return err }) diff --git a/query.go b/query.go index a711004..4aef703 100644 --- a/query.go +++ b/query.go @@ -2,7 +2,7 @@ package advsql import "context" -func Query[T any](db *Database, query string, scan func(v *T, scan ScanFunc) error) QueryManyFunc[T] { +func Query[T any](db *Database, query string, decoder func(v *T, decode DecodeFunc) error) QueryManyFunc[T] { s, err := db.prepare(query) if err != nil { return nil @@ -21,7 +21,7 @@ func Query[T any](db *Database, query string, scan func(v *T, scan ScanFunc) err defer close(out) for rows.Next() { v := new(T) - if scan(v, rows.Scan) == nil { + if decoder(v, rows.Scan) == nil { out <- v } } @@ -31,7 +31,7 @@ func Query[T any](db *Database, query string, scan func(v *T, scan ScanFunc) err } } -func QueryContext[T any](db *Database, query string, scan func(v *T, scan ScanFunc) error) QueryManyContextFunc[T] { +func QueryContext[T any](db *Database, query string, decoder func(v *T, decode DecodeFunc) error) QueryManyContextFunc[T] { s, err := db.db.Prepare(query) if err != nil { return nil @@ -51,7 +51,7 @@ func QueryContext[T any](db *Database, query string, scan func(v *T, scan ScanFu defer close(out) for rows.Next() { v := new(T) - if scan(v, rows.Scan) == nil { + if decoder(v, rows.Scan) == nil { out <- v } } diff --git a/scanner.go b/scanner.go index e3cf9b6..c03bb53 100644 --- a/scanner.go +++ b/scanner.go @@ -1,5 +1,4 @@ package advsql -type ScanFunc func(args ...interface{}) error - -type ExecFunc func(args ...interface{}) error +type DecodeFunc func(args ...interface{}) error +type EncodeFunc func(args ...interface{}) error diff --git a/update.go b/update.go index c4365c0..b97ad72 100644 --- a/update.go +++ b/update.go @@ -1,5 +1,5 @@ package advsql -func Update[T any](db *Database, query string, exec func(v *T, exec ExecFunc) error) InsertFunc[T] { - return Insert(db, query, exec) +func Update[T any](db *Database, query string, encoder func(v *T, encode EncodeFunc) error) InsertFunc[T] { + return Insert(db, query, encoder) }