-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
70 lines (59 loc) · 1.59 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package gromer
import (
"context"
"database/sql"
)
type SQLInterface[T any] interface {
SqlOne(ctx context.Context, query string, args ...any) (*T, error)
SqlMany(ctx context.Context, query string, args ...any) ([]*T, error)
SqlExecute(ctx context.Context, query string, args ...any) error
}
type DBSQLInterface[T any] struct {
db *sql.DB
t T
}
func (p *DBSQLInterface[T]) SqlOne(ctx context.Context, query string, args ...any) (*T, error) {
conn, err := p.db.Conn(ctx)
if err != nil {
return nil, err
}
rows, err := conn.QueryContext(ctx, query, args)
if err != nil {
return nil, err
}
defer rows.Close()
// cols, _ := rows.Columns()
for rows.Next() {
// rows.Scan()
// for _, c := range cols {
// if rows.Err()
// }
}
// row.Scan()
// switch {
// case err == sql.ErrNoRows:
// log.Fatalf("no user with id %d", id)
// case err != nil:
// log.Fatal(err)
// default:
// log.Printf("username is %s\n", username)
// }
return nil, nil
}
func (p *DBSQLInterface[T]) SqlMany(ctx context.Context, query string, args ...any) ([]*T, error) {
return nil, nil
}
func (p *DBSQLInterface[T]) SqlExecute(ctx context.Context, query string, args ...any) error {
return nil
}
// type User struct {
// }
// func UsersTable(ctx context.Context) SQLInterface[User] {
// return &DBSQLInterface[User]{t: User{}}
// }
// func GetNote(ctx context.Context, id string) (*User, error) {
// return UsersTable(ctx).SqlOne(ctx, "select * from users where id = :id", id)
// }
// func GetNotes2(ctx context.Context) ([]*User, error) {
// return UsersTable(ctx).SqlMany(ctx, "select * from users")
// }