renamed ExecFunc to EncodeFunc and ScanFunc to DecodeFunc
This commit is contained in:
parent
26975dc3a1
commit
b4c0849e29
20
db_test.go
20
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{
|
||||
|
@ -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
|
||||
})
|
||||
|
8
query.go
8
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
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user