~mna/webparts

ref: 2c291ec447ed4d1b6d49111daf99f9ab9dd2e9b4 webparts/sql/sql.go -rw-r--r-- 1.6 KiB
2c291ec4Martin Angers Update doc 1 year, 8 months ago
                                                                                
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
// Package sql implements the standard SQL database interface.
package sql

import (
	"context"
	"database/sql"
)

// SQL is a distinct type for SQL statements to prevent accidental
// SQL injection by directly manipulating or concatenating strings.
type SQL string

// Result summarizes an executed SQL command.
type Result = sql.Result

// DB defines the method required for a database pool.
type DB interface {
	Queryer
	Begin(context.Context) (Txer, error)
	BeginFunc(context.Context, func(Txer) error) error
	Close() error
}

// Txer is a database transaction. It implements Queryer, and adds methods
// to Commit or Rollback the transaction. Calling Rollback on a committed
// transaction returns an error and is otherwise a no-op, so a useful idiom
// is to defer a call to Rollback after starting a transaction, and call
// Commit when needed, which will invalidate the Rollback.
type Txer interface {
	Queryer
	Commit(context.Context) error
	Rollback(context.Context) error
}

// Cursor implement an efficient, iterable database result. Typical usage
// is to use an inifinite for loop and exit when Next returns false.
// It must be closed after use. Consult Err to find any error that may
// have caused early exit from the loop.
type Cursor interface {
	Close() error
	Err() error
	Next() bool
	Scan(interface{}) error
}

// Queryer is the common interface to query and execute SQL
// statements.
type Queryer interface {
	Exec(context.Context, SQL, ...interface{}) (Result, error)
	QueryOne(context.Context, interface{}, SQL, ...interface{}) error
	QueryMany(context.Context, interface{}, SQL, ...interface{}) error
	Cursor(context.Context, SQL, ...interface{}) Cursor
}