1
+ - - - - - - - - - - - - - - - - -
2
+ sql
3
+ - - - - - - - - - - - - - - - - -
4
+ # SQL的驱动需要导入 ,跟Java一样 ,加载到内存就行
5
+ import (
6
+ _ "github.com/go-sql-driver/mysql"
7
+ )
8
+
9
+
10
+
11
+ - - - - - - - - - - - - - - - - -
12
+ var
13
+ - - - - - - - - - - - - - - - - -
14
+ # 异常信息
15
+ var ErrConnDone = errors .New ("sql: connection is already closed" )
16
+ * 连接一个关闭
17
+ var ErrNoRows = errors .New ("sql: no rows in result set" )
18
+ * 空结果
19
+ var ErrTxDone = errors .New ("sql: transaction has already been committed or rolled back" )
20
+ * 异常已经回滚或者提交
21
+
22
+
23
+ - - - - - - - - - - - - - - - - -
24
+ type
25
+ - - - - - - - - - - - - - - - - -
26
+ # type ColumnType struct {
27
+ }
28
+
29
+ * 列类型
30
+ func (ci * ColumnType ) DatabaseTypeName () string
31
+ func (ci * ColumnType ) DecimalSize () (precision , scale int64 , ok bool )
32
+ func (ci * ColumnType ) Length () (length int64 , ok bool )
33
+ func (ci * ColumnType ) Name () string
34
+ func (ci * ColumnType ) Nullable () (nullable , ok bool )
35
+ func (ci * ColumnType ) ScanType () reflect.Type
36
+
37
+ # type Conn struct {
38
+ }
39
+
40
+ * 数据库连接
41
+
42
+ func (c * Conn ) BeginTx (ctx context.Context , opts * TxOptions ) (* Tx , error )
43
+ func (c * Conn ) Close () error
44
+ func (c * Conn ) ExecContext (ctx context.Context , query string , args ... interface {}) (Result , error )
45
+ func (c * Conn ) PingContext (ctx context.Context ) error
46
+ func (c * Conn ) PrepareContext (ctx context.Context , query string ) (* Stmt , error )
47
+ func (c * Conn ) QueryContext (ctx context.Context , query string , args ... interface {}) (* Rows , error )
48
+ func (c * Conn ) QueryRowContext (ctx context.Context , query string , args ... interface {}) * Row
49
+ func (c * Conn ) Raw (f func (driverConn interface {}) error ) (err error )
50
+
51
+ # type DB struct {
52
+ }
53
+
54
+ * 数据库连接池
55
+
56
+ func Open (driverName , dataSourceName string ) (* DB , error )
57
+ * 返回DB ,自己检索驱动 ,驱动需要先被加载进内存
58
+ db , err := sql .Open ("mysql" , "user:password@/dbname" )
59
+
60
+ func OpenDB (c driver.Connector ) * DB
61
+ * 根据驱动返回DB
62
+
63
+ func (db * DB ) Begin () (* Tx , error )
64
+ func (db * DB ) BeginTx (ctx context.Context , opts * TxOptions ) (* Tx , error )
65
+ func (db * DB ) Close () error
66
+ func (db * DB ) Conn (ctx context.Context ) (* Conn , error )
67
+ func (db * DB ) Driver () driver.Driver
68
+ func (db * DB ) Exec (query string , args ... interface {}) (Result , error )
69
+ func (db * DB ) ExecContext (ctx context.Context , query string , args ... interface {}) (Result , error )
70
+ func (db * DB ) Ping () error
71
+ func (db * DB ) PingContext (ctx context.Context ) error
72
+ func (db * DB ) Prepare (query string ) (* Stmt , error )
73
+ func (db * DB ) PrepareContext (ctx context.Context , query string ) (* Stmt , error )
74
+ func (db * DB ) Query (query string , args ... interface {}) (* Rows , error )
75
+ func (db * DB ) QueryContext (ctx context.Context , query string , args ... interface {}) (* Rows , error )
76
+ func (db * DB ) QueryRow (query string , args ... interface {}) * Row
77
+ func (db * DB ) QueryRowContext (ctx context.Context , query string , args ... interface {}) * Row
78
+
79
+ func (db * DB ) SetConnMaxIdleTime (d time.Duration )
80
+ * 连接最大空闲时间
81
+ func (db * DB ) SetConnMaxLifetime (d time.Duration )
82
+ * 连接最大存活时间
83
+ func (db * DB ) SetMaxIdleConns (n int )
84
+ * 最大空闲连接数量
85
+ func (db * DB ) SetMaxOpenConns (n int )
86
+ * 最大连接数据
87
+ func (db * DB ) Stats () DBStats
88
+
89
+ # type DBStats struct {
90
+ MaxOpenConnections int // Maximum number of open connections to the database.
91
+
92
+ // Pool Status
93
+ OpenConnections int // The number of established connections both in use and idle.
94
+ InUse int // The number of connections currently in use.
95
+ Idle int // The number of idle connections.
96
+
97
+ // Counters
98
+ WaitCount int64 // The total number of connections waited for.
99
+ WaitDuration time.Duration // The total time blocked waiting for a new connection.
100
+ MaxIdleClosed int64 // The total number of connections closed due to SetMaxIdleConns.
101
+ MaxIdleTimeClosed int64 // The total number of connections closed due to SetConnMaxIdleTime.
102
+ MaxLifetimeClosed int64 // The total number of connections closed due to SetConnMaxLifetime.
103
+ }
104
+
105
+ * 数据库状态
106
+
107
+ # type IsolationLevel int
108
+ * 事务隔离级别
109
+
110
+ const (
111
+ LevelDefault IsolationLevel = iota
112
+ LevelReadUncommitted
113
+ LevelReadCommitted
114
+ LevelWriteCommitted
115
+ LevelRepeatableRead
116
+ LevelSnapshot
117
+ LevelSerializable
118
+ LevelLinearizable
119
+ )
120
+ func (i IsolationLevel ) String () string
121
+
122
+
123
+ # type NamedArg struct {
124
+ Name string
125
+ Value interface {} // contains filtered or unexported fields
126
+ }
127
+ * SQL中的具名参数
128
+ func Named (name string , value interface {}) NamedArg
129
+
130
+ # type NullBool struct {
131
+ Bool bool
132
+ Valid bool // Valid is true if Bool is not NULL
133
+ }
134
+ func (n * NullBool ) Scan (value interface {}) error
135
+ func (n NullBool ) Value () (driver.Value , error )
136
+
137
+ # type NullFloat64 struct {
138
+ Float64 float64
139
+ Valid bool // Valid is true if Float64 is not NULL
140
+ }
141
+ func (n * NullFloat64 ) Scan (value interface {}) error
142
+ func (n NullFloat64 ) Value () (driver.Value , error )
143
+
144
+ # type NullInt32 struct {
145
+ Int32 int32
146
+ Valid bool // Valid is true if Int32 is not NULL
147
+ }
148
+ func (n * NullInt32 ) Scan (value interface {}) error
149
+ func (n NullInt32 ) Value () (driver.Value , error )
150
+
151
+ # type NullInt64 struct {
152
+ Int64 int64
153
+ Valid bool // Valid is true if Int64 is not NULL
154
+ }
155
+ func (n * NullInt64 ) Scan (value interface {}) error
156
+ func (n NullInt64 ) Value () (driver.Value , error )
157
+
158
+ # type NullString struct {
159
+ String string
160
+ Valid bool // Valid is true if String is not NULL
161
+ }
162
+ func (ns * NullString ) Scan (value interface {}) error
163
+ func (ns NullString ) Value () (driver.Value , error )
164
+
165
+ # type NullTime struct {
166
+ Valid bool // Valid is true if Time is not NULL
167
+ }
168
+ func (n * NullTime ) Scan (value interface {}) error
169
+ func (n NullTime ) Value () (driver.Value , error )
170
+
171
+ # type Out struct {
172
+ Dest interface {}
173
+ In bool // contains filtered or unexported fields
174
+ }
175
+
176
+ # type RawBytes []byte
177
+ # type Result interface {
178
+ LastInsertId () (int64 , error )
179
+ RowsAffected () (int64 , error )
180
+ }
181
+ * 查询结果
182
+
183
+ # type Row struct {
184
+ }
185
+ func (r * Row ) Err () error
186
+ func (r * Row ) Scan (dest ... interface {}) error
187
+
188
+ # type Rows struct {
189
+ }
190
+ func (rs * Rows ) Close () error
191
+ func (rs * Rows ) ColumnTypes () ([]* ColumnType , error )
192
+ func (rs * Rows ) Columns () ([]string , error )
193
+ func (rs * Rows ) Err () error
194
+ func (rs * Rows ) Next () bool
195
+ func (rs * Rows ) NextResultSet () bool
196
+ func (rs * Rows ) Scan (dest ... interface {}) error
197
+
198
+ # type Scanner interface {
199
+ Scan (src interface {}) error
200
+ }
201
+ # type Stmt struct {
202
+ }
203
+ func (s * Stmt ) Close () error
204
+ func (s * Stmt ) Exec (args ... interface {}) (Result , error )
205
+ func (s * Stmt ) ExecContext (ctx context.Context , args ... interface {}) (Result , error )
206
+ func (s * Stmt ) Query (args ... interface {}) (* Rows , error )
207
+ func (s * Stmt ) QueryContext (ctx context.Context , args ... interface {}) (* Rows , error )
208
+ func (s * Stmt ) QueryRow (args ... interface {}) * Row
209
+ func (s * Stmt ) QueryRowContext (ctx context.Context , args ... interface {}) * Row
210
+
211
+ # type Tx struct {
212
+ }
213
+ * 事务
214
+ func (tx * Tx ) Commit () error
215
+ func (tx * Tx ) Exec (query string , args ... interface {}) (Result , error )
216
+ func (tx * Tx ) ExecContext (ctx context.Context , query string , args ... interface {}) (Result , error )
217
+ func (tx * Tx ) Prepare (query string ) (* Stmt , error )
218
+ func (tx * Tx ) PrepareContext (ctx context.Context , query string ) (* Stmt , error )
219
+ func (tx * Tx ) Query (query string , args ... interface {}) (* Rows , error )
220
+ func (tx * Tx ) QueryContext (ctx context.Context , query string , args ... interface {}) (* Rows , error )
221
+ func (tx * Tx ) QueryRow (query string , args ... interface {}) * Row
222
+ func (tx * Tx ) QueryRowContext (ctx context.Context , query string , args ... interface {}) * Row
223
+ func (tx * Tx ) Rollback () error
224
+ func (tx * Tx ) Stmt (stmt * Stmt ) * Stmt
225
+ func (tx * Tx ) StmtContext (ctx context.Context , stmt * Stmt ) * Stmt
226
+
227
+
228
+ - - - - - - - - - - - - - - - - -
229
+ func
230
+ -- -- -- -- -- -- -- -- -
231
+ func Drivers () []string
232
+ func Register (name string , driver driver .Driver )
233
+
234
+
235
+ - - - - - - - - - - - - - - - - -
236
+ demo
237
+ - - - - - - - - - - - - - - - - -
238
+
0 commit comments