Skip to content

Commit c8d45cf

Browse files
committed
更新sql库
1 parent a7d3103 commit c8d45cf

28 files changed

+1897
-917
lines changed

README.md

+78-17
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,23 @@ A MySQL-Driver for Go's [database/sql](https://golang.org/pkg/database/sql/) pac
4040
* Optional placeholder interpolation
4141

4242
## Requirements
43-
* Go 1.10 or higher. We aim to support the 3 latest versions of Go.
44-
* MySQL (4.1+), MariaDB, Percona Server, Google CloudSQL or Sphinx (2.2.3+)
43+
44+
* Go 1.19 or higher. We aim to support the 3 latest versions of Go.
45+
* MySQL (5.7+) and MariaDB (10.3+) are supported.
46+
* [TiDB](https://github.com/pingcap/tidb) is supported by PingCAP.
47+
* Do not ask questions about TiDB in our issue tracker or forum.
48+
* [Document](https://docs.pingcap.com/tidb/v6.1/dev-guide-sample-application-golang)
49+
* [Forum](https://ask.pingcap.com/)
50+
* go-mysql would work with Percona Server, Google CloudSQL or Sphinx (2.2.3+).
51+
* Maintainers won't support them. Do not expect issues are investigated and resolved by maintainers.
52+
* Investigate issues yourself and please send a pull request to fix it.
4553

4654
---------------------------------------
4755

4856
## Installation
4957
Simple install the package to your [$GOPATH](https://github.com/golang/go/wiki/GOPATH "GOPATH") with the [go tool](https://golang.org/cmd/go/ "go command") from shell:
5058
```bash
51-
$ go get -u github.com/go-sql-driver/mysql
59+
go get -u github.com/go-sql-driver/mysql
5260
```
5361
Make sure [Git is installed](https://git-scm.com/downloads) on your machine and in your system's `PATH`.
5462

@@ -85,7 +93,7 @@ db.SetMaxIdleConns(10)
8593

8694
`db.SetMaxOpenConns()` is highly recommended to limit the number of connection used by the application. There is no recommended limit number because it depends on application and MySQL server.
8795

88-
`db.SetMaxIdleConns()` is recommended to be set same to (or greater than) `db.SetMaxOpenConns()`. When it is smaller than `SetMaxOpenConns()`, connections can be opened and closed very frequently than you expect. Idle connections can be closed by the `db.SetConnMaxLifetime()`. If you want to close idle connections more rapidly, you can use `db.SetConnMaxIdleTime()` since Go 1.15.
96+
`db.SetMaxIdleConns()` is recommended to be set same to `db.SetMaxOpenConns()`. When it is smaller than `SetMaxOpenConns()`, connections can be opened and closed much more frequently than you expect. Idle connections can be closed by the `db.SetConnMaxLifetime()`. If you want to close idle connections more rapidly, you can use `db.SetConnMaxIdleTime()` since Go 1.15.
8997

9098

9199
### DSN (Data Source Name)
@@ -114,14 +122,20 @@ This has the same effect as an empty DSN string:
114122
115123
```
116124

125+
`dbname` is escaped by [PathEscape()](https://pkg.go.dev/net/url#PathEscape) since v1.8.0. If your database name is `dbname/withslash`, it becomes:
126+
127+
```
128+
/dbname%2Fwithslash
129+
```
130+
117131
Alternatively, [Config.FormatDSN](https://godoc.org/github.com/go-sql-driver/mysql#Config.FormatDSN) can be used to create a DSN string by filling a struct.
118132

119133
#### Password
120134
Passwords can consist of any character. Escaping is **not** necessary.
121135

122136
#### Protocol
123137
See [net.Dial](https://golang.org/pkg/net/#Dial) for more information which networks are available.
124-
In general you should use an Unix domain socket if available and TCP otherwise for best performance.
138+
In general you should use a Unix domain socket if available and TCP otherwise for best performance.
125139

126140
#### Address
127141
For TCP and UDP networks, addresses have the form `host[:port]`.
@@ -145,7 +159,7 @@ Default: false
145159
```
146160

147161
`allowAllFiles=true` disables the file allowlist for `LOAD DATA LOCAL INFILE` and allows *all* files.
148-
[*Might be insecure!*](http://dev.mysql.com/doc/refman/5.7/en/load-data-local.html)
162+
[*Might be insecure!*](https://dev.mysql.com/doc/refman/8.0/en/load-data.html#load-data-local)
149163

150164
##### `allowCleartextPasswords`
151165

@@ -157,6 +171,17 @@ Default: false
157171

158172
`allowCleartextPasswords=true` allows using the [cleartext client side plugin](https://dev.mysql.com/doc/en/cleartext-pluggable-authentication.html) if required by an account, such as one defined with the [PAM authentication plugin](http://dev.mysql.com/doc/en/pam-authentication-plugin.html). Sending passwords in clear text may be a security problem in some configurations. To avoid problems if there is any possibility that the password would be intercepted, clients should connect to MySQL Server using a method that protects the password. Possibilities include [TLS / SSL](#tls), IPsec, or a private network.
159173

174+
175+
##### `allowFallbackToPlaintext`
176+
177+
```
178+
Type: bool
179+
Valid Values: true, false
180+
Default: false
181+
```
182+
183+
`allowFallbackToPlaintext=true` acts like a `--ssl-mode=PREFERRED` MySQL client as described in [Command Options for Connecting to the Server](https://dev.mysql.com/doc/refman/5.7/en/connection-options.html#option_general_ssl-mode)
184+
160185
##### `allowNativePasswords`
161186

162187
```
@@ -183,10 +208,9 @@ Valid Values: <name>
183208
Default: none
184209
```
185210

186-
Sets the charset used for client-server interaction (`"SET NAMES <value>"`). If multiple charsets are set (separated by a comma), the following charset is used if setting the charset failes. This enables for example support for `utf8mb4` ([introduced in MySQL 5.5.3](http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html)) with fallback to `utf8` for older servers (`charset=utf8mb4,utf8`).
211+
Sets the charset used for client-server interaction (`"SET NAMES <value>"`). If multiple charsets are set (separated by a comma), the following charset is used if setting the charset fails. This enables for example support for `utf8mb4` ([introduced in MySQL 5.5.3](http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html)) with fallback to `utf8` for older servers (`charset=utf8mb4,utf8`).
187212

188-
Usage of the `charset` parameter is discouraged because it issues additional queries to the server.
189-
Unless you need the fallback behavior, please use `collation` instead.
213+
See also [Unicode Support](#unicode-support).
190214

191215
##### `checkConnLiveness`
192216

@@ -215,6 +239,7 @@ The default collation (`utf8mb4_general_ci`) is supported from MySQL 5.5. You s
215239

216240
Collations for charset "ucs2", "utf16", "utf16le", and "utf32" can not be used ([ref](https://dev.mysql.com/doc/refman/5.7/en/charset-connection.html#charset-connection-impermissible-client-charset)).
217241

242+
See also [Unicode Support](#unicode-support).
218243

219244
##### `clientFoundRows`
220245

@@ -268,13 +293,22 @@ Note that this sets the location for time.Time values but does not change MySQL'
268293

269294
Please keep in mind, that param values must be [url.QueryEscape](https://golang.org/pkg/net/url/#QueryEscape)'ed. Alternatively you can manually replace the `/` with `%2F`. For example `US/Pacific` would be `loc=US%2FPacific`.
270295

296+
##### `timeTruncate`
297+
298+
```
299+
Type: duration
300+
Default: 0
301+
```
302+
303+
[Truncate time values](https://pkg.go.dev/time#Duration.Truncate) to the specified duration. The value must be a decimal number with a unit suffix (*"ms"*, *"s"*, *"m"*, *"h"*), such as *"30s"*, *"0.5m"* or *"1m30s"*.
304+
271305
##### `maxAllowedPacket`
272306
```
273307
Type: decimal number
274-
Default: 4194304
308+
Default: 64*1024*1024
275309
```
276310

277-
Max packet size allowed in bytes. The default value is 4 MiB and should be adjusted to match the server settings. `maxAllowedPacket=0` can be used to automatically fetch the `max_allowed_packet` variable from server *on every connection*.
311+
Max packet size allowed in bytes. The default value is 64 MiB and should be adjusted to match the server settings. `maxAllowedPacket=0` can be used to automatically fetch the `max_allowed_packet` variable from server *on every connection*.
278312

279313
##### `multiStatements`
280314

@@ -284,9 +318,25 @@ Valid Values: true, false
284318
Default: false
285319
```
286320

287-
Allow multiple statements in one query. While this allows batch queries, it also greatly increases the risk of SQL injections. Only the result of the first query is returned, all other results are silently discarded.
321+
Allow multiple statements in one query. This can be used to bach multiple queries. Use [Rows.NextResultSet()](https://pkg.go.dev/database/sql#Rows.NextResultSet) to get result of the second and subsequent queries.
288322

289-
When `multiStatements` is used, `?` parameters must only be used in the first statement.
323+
When `multiStatements` is used, `?` parameters must only be used in the first statement. [interpolateParams](#interpolateparams) can be used to avoid this limitation unless prepared statement is used explicitly.
324+
325+
It's possible to access the last inserted ID and number of affected rows for multiple statements by using `sql.Conn.Raw()` and the `mysql.Result`. For example:
326+
327+
```go
328+
conn, _ := db.Conn(ctx)
329+
conn.Raw(func(conn any) error {
330+
ex := conn.(driver.Execer)
331+
res, err := ex.Exec(`
332+
UPDATE point SET x = 1 WHERE y = 2;
333+
UPDATE point SET x = 2 WHERE y = 3;
334+
`, nil)
335+
// Both slices have 2 elements.
336+
log.Print(res.(mysql.Result).AllRowsAffected())
337+
log.Print(res.(mysql.Result).AllLastInsertIds())
338+
})
339+
```
290340

291341
##### `parseTime`
292342

@@ -382,6 +432,15 @@ Default: 0
382432

383433
I/O write timeout. The value must be a decimal number with a unit suffix (*"ms"*, *"s"*, *"m"*, *"h"*), such as *"30s"*, *"0.5m"* or *"1m30s"*.
384434

435+
##### `connectionAttributes`
436+
437+
```
438+
Type: comma-delimited string of user-defined "key:value" pairs
439+
Valid Values: (<name1>:<value1>,<name2>:<value2>,...)
440+
Default: none
441+
```
442+
443+
[Connection attributes](https://dev.mysql.com/doc/refman/8.0/en/performance-schema-connection-attribute-tables.html) are key-value pairs that application programs can pass to the server at connect time.
385444

386445
##### System Variables
387446

@@ -454,7 +513,7 @@ user:password@/
454513
The connection pool is managed by Go's database/sql package. For details on how to configure the size of the pool and how long connections stay in the pool see `*DB.SetMaxOpenConns`, `*DB.SetMaxIdleConns`, and `*DB.SetConnMaxLifetime` in the [database/sql documentation](https://golang.org/pkg/database/sql/). The read, write, and dial timeouts for each individual connection are configured with the DSN parameters [`readTimeout`](#readtimeout), [`writeTimeout`](#writetimeout), and [`timeout`](#timeout), respectively.
455514

456515
## `ColumnType` Support
457-
This driver supports the [`ColumnType` interface](https://golang.org/pkg/database/sql/#ColumnType) introduced in Go 1.8, with the exception of [`ColumnType.Length()`](https://golang.org/pkg/database/sql/#ColumnType.Length), which is currently not supported.
516+
This driver supports the [`ColumnType` interface](https://golang.org/pkg/database/sql/#ColumnType) introduced in Go 1.8, with the exception of [`ColumnType.Length()`](https://golang.org/pkg/database/sql/#ColumnType.Length), which is currently not supported. All Unsigned database type names will be returned `UNSIGNED ` with `INT`, `TINYINT`, `SMALLINT`, `MEDIUMINT`, `BIGINT`.
458517

459518
## `context.Context` Support
460519
Go 1.8 added `database/sql` support for `context.Context`. This driver supports query timeouts and cancellation via contexts.
@@ -467,7 +526,7 @@ For this feature you need direct access to the package. Therefore you must chang
467526
import "github.com/go-sql-driver/mysql"
468527
```
469528

470-
Files must be explicitly allowed by registering them with `mysql.RegisterLocalFile(filepath)` (recommended) or the allowlist check must be deactivated by using the DSN parameter `allowAllFiles=true` ([*Might be insecure!*](http://dev.mysql.com/doc/refman/5.7/en/load-data-local.html)).
529+
Files must be explicitly allowed by registering them with `mysql.RegisterLocalFile(filepath)` (recommended) or the allowlist check must be deactivated by using the DSN parameter `allowAllFiles=true` ([*Might be insecure!*](https://dev.mysql.com/doc/refman/8.0/en/load-data.html#load-data-local)).
471530

472531
To use a `io.Reader` a handler function must be registered with `mysql.RegisterReaderHandler(name, handler)` which returns a `io.Reader` or `io.ReadCloser`. The Reader is available with the filepath `Reader::<name>` then. Choose different names for different handlers and `DeregisterReaderHandler` when you don't need it anymore.
473532

@@ -485,9 +544,11 @@ However, many want to scan MySQL `DATE` and `DATETIME` values into `time.Time` v
485544
### Unicode support
486545
Since version 1.5 Go-MySQL-Driver automatically uses the collation ` utf8mb4_general_ci` by default.
487546

488-
Other collations / charsets can be set using the [`collation`](#collation) DSN parameter.
547+
Other charsets / collations can be set using the [`charset`](#charset) or [`collation`](#collation) DSN parameter.
489548

490-
Version 1.0 of the driver recommended adding `&charset=utf8` (alias for `SET NAMES utf8`) to the DSN to enable proper UTF-8 support. This is not necessary anymore. The [`collation`](#collation) parameter should be preferred to set another collation / charset than the default.
549+
- When only the `charset` is specified, the `SET NAMES <charset>` query is sent and the server's default collation is used.
550+
- When both the `charset` and `collation` are specified, the `SET NAMES <charset> COLLATE <collation>` query is sent.
551+
- When only the `collation` is specified, the collation is specified in the protocol handshake and the `SET NAMES` query is not sent. This can save one roundtrip, but note that the server may ignore the specified collation silently and use the server's default charset/collation instead.
491552

492553
See http://dev.mysql.com/doc/refman/8.0/en/charset-unicode.html for more details on MySQL's Unicode support.
493554

atomic_bool.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package.
2+
//
3+
// Copyright 2022 The Go-MySQL-Driver Authors. All rights reserved.
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
// You can obtain one at http://mozilla.org/MPL/2.0/.
8+
//go:build go1.19
9+
// +build go1.19
10+
11+
package mysql
12+
13+
import "sync/atomic"
14+
15+
/******************************************************************************
16+
* Sync utils *
17+
******************************************************************************/
18+
19+
type atomicBool = atomic.Bool

0 commit comments

Comments
 (0)