forked from go-reform/reform
/
base.go
152 lines (118 loc) · 4.67 KB
/
base.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package reform
import (
"database/sql"
"errors"
)
var (
// ErrNoRows is returned from various methods when query produced no rows.
ErrNoRows = sql.ErrNoRows
// ErrNoPK is returned from various methods when primary key is required and not set.
ErrNoPK = errors.New("reform: no primary key")
)
// View represents SQL database view or table.
type View interface {
// Schema returns a schema name in SQL database.
Schema() string
// Name returns a view or table name in SQL database.
Name() string
// Columns returns a new slice of column names for that view or table in SQL database.
Columns() []string
// NewStruct makes a new struct for that view or table.
NewStruct() Struct
}
// Table represents SQL database table with single-column primary key.
// It extends View.
type Table interface {
View
// NewRecord makes a new record for that table.
NewRecord() Record
// PKColumnIndex returns an index of primary key column for that table in SQL database.
PKColumnIndex() uint
}
// Struct represents a row in SQL database view or table.
type Struct interface {
// String returns a string representation of this struct or record.
String() string
// Values returns a slice of struct or record field values.
// Returned interface{} values are never untyped nils.
Values() []interface{}
// Pointers returns a slice of pointers to struct or record fields.
// Returned interface{} values are never untyped nils.
Pointers() []interface{}
// View returns View object for that struct.
View() View
}
// Record represents a row in SQL database table with single-column primary key.
type Record interface {
Struct
// Table returns Table object for that record.
Table() Table
// PKValue returns a value of primary key for that record.
// Returned interface{} value is never untyped nil.
PKValue() interface{}
// PKPointer returns a pointer to primary key field for that record.
// Returned interface{} value is never untyped nil.
PKPointer() interface{}
// HasPK returns true if record has non-zero primary key set, false otherwise.
HasPK() bool
// SetPK sets record primary key.
SetPK(pk interface{})
}
// BeforeInserter is an optional interface for Record which is used by Querier.Insert.
// It can be used to set record's timestamp fields, convert timezones, change data precision, etc.
// Returning error aborts operation.
type BeforeInserter interface {
BeforeInsert() error
}
// BeforeUpdater is an optional interface for Record which is used by Querier.Update and Querier.UpdateColumns.
// It can be used to set record's timestamp fields, convert timezones, change data precision, etc.
// Returning error aborts operation.
type BeforeUpdater interface {
BeforeUpdate() error
}
// AfterFinder is an optional interface for Record which is used by Querier's finders and selectors.
// It can be used to convert timezones, change data precision, etc.
// Returning error aborts operation.
type AfterFinder interface {
AfterFind() error
}
// DBTX is an interface for database connection or transaction.
// It's implemented by *sql.DB, *sql.Tx, *DB, *TX and *Querier.
type DBTX interface {
// Exec executes a query without returning any rows.
// The args are for any placeholder parameters in the query.
Exec(query string, args ...interface{}) (sql.Result, error)
// Query executes a query that returns rows, typically a SELECT.
// The args are for any placeholder parameters in the query.
Query(query string, args ...interface{}) (*sql.Rows, error)
// QueryRow executes a query that is expected to return at most one row.
// QueryRow always returns a non-nil value. Errors are deferred until Row's Scan method is called.
QueryRow(query string, args ...interface{}) *sql.Row
}
// LastInsertIdMethod is a method of receiving primary key of last inserted row.
type LastInsertIdMethod int
const (
// LastInsertId is method using sql.Result.LastInsertId().
LastInsertId LastInsertIdMethod = iota
// Returning is method using "RETURNING id" SQL syntax.
Returning
)
// Dialect represents differences in various SQL dialects.
type Dialect interface {
// Placeholder returns representation of placeholder parameter for given index,
// typically "?" or "$1".
Placeholder(index int) string
// Placeholders returns representation of placeholder parameters for given start index and count,
// typically []{"?", "?"} or []{"$1", "$2"}.
Placeholders(start, count int) []string
// QuoteIdentifier returns quoted database identifier,
// typically "identifier" or `identifier`.
QuoteIdentifier(identifier string) string
// LastInsertIdMethod returns a method of receiving primary key of last inserted row.
LastInsertIdMethod() LastInsertIdMethod
}
// check interface
var (
_ DBTX = new(sql.DB)
_ DBTX = new(sql.Tx)
)