-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.go
More file actions
41 lines (35 loc) · 1.21 KB
/
Copy pathdatabase.go
File metadata and controls
41 lines (35 loc) · 1.21 KB
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
package struct2sql
import (
"context"
"database/sql"
)
type DB struct {
*sql.DB
}
type Tx struct {
*sql.Tx
}
type IDB interface {
Exec(query string, args ...interface{}) (sql.Result, error)
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
Prepare(query string) (*sql.Stmt, error)
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
CreateTable(model interface{}) error
DropTable(model ...interface{}) error
Insert(model interface{}) error
Select(model interface{}, condition string, cargs ...interface{}) error
Update(model interface{}, condition string, cargs ...interface{}) error
Delete(model interface{}, condition string, cargs ...interface{}) error
}
func Open(driverName, dataSourceName string) (*DB, error) {
sqlDB, err := sql.Open(driverName, dataSourceName)
return &DB{sqlDB}, err
}
func (db DB) Begin() (*Tx, error) {
tx, err := db.DB.Begin()
return &Tx{tx}, err
}