https://github.com/zcpua/manager
Manager is used for initializing a database connection pool(sql.DB in go's standard library)
https://github.com/zcpua/manager
mysql sql
Last synced: about 1 month ago
JSON representation
Manager is used for initializing a database connection pool(sql.DB in go's standard library)
- Host: GitHub
- URL: https://github.com/zcpua/manager
- Owner: zcpua
- License: mit
- Created: 2022-01-26T07:09:46.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-03-16T12:01:49.000Z (over 4 years ago)
- Last Synced: 2025-02-01T06:27:44.054Z (over 1 year ago)
- Topics: mysql, sql
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Manager
Manager is used for initializing a database connection pool(sql.DB in go's standard library)
The original way to initialize a sql.DB is something like:
```go
pool,err := sql.Open("mysql", dataSourceName)
```
And the format of `dataSourceName` is:
```
[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...¶mN=valueN]
```
What the manager does is providing a series of simple methods which help you setting these parameters.
### Example
```go
import (
"net/url"
"database/sql"
"github.com/zcpua/manager"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
var err error
db, err = manager.New(dbName, user, password, host).Set(
manager.SetCharset("utf8"),
manager.SetParseTime(true),
manager.SetLoc("Asia/Shanghai"),
manager.SetTimeZone("Asia/Shanghai"),
).Port(3306).Open(true)
```
> `godoc` is a great tool for scanning the API documentation
### Basic API
`func New(dbName, user, password, host string) *Option`
New returns an Option
`func (o *Option) Driver(driver string) *Option`
Driver sets the driver, default mysql
`func (o *Option) Open(ping bool) (*sql.DB, error)`
Open is used for creating a *sql.DB.If ping=true,it will exec the db.Ping() after creating the db object
`func (o *Option) Port(port int) *Option`
Port sets the server port,default 3306
`func (o *Option) Set(sets ...Setting) *Option`
Set receives a series of Set*-like functions
---
### Setting APIs
For more details see [DSN-Data-Source-Name](https://github.com/go-sql-driver/mysql#dsn-data-source-name)
* `SetAllowAllFiles`: allowAllFiles=true disables the file Whitelist for LOAD DATA LOCAL INFILE and allows all files. Might be insecure!
* `SetAllowCleartextPasswords`: allowCleartextPasswords=true allows using thecleartext client side plugin if required by an account, such as one defined with the PAM authentication plugin. Sending passwords in clear text may be a security problem in some configurations.
* `SetAllowNativePasswords`: Allows the usage of the mysql native password method
* `SetAutoCommit`: Set it to true if you know what you are doing
* `SetCharset`: Sets the charset used for client-server interaction
* `SetClientFoundRows`: clientFoundRows=true causes an UPDATE to return the number of matching rows instead of the number of rows changed.
* `SetCollation`: Sets the collation used for client-server interaction on connection. In contrast to charset, collation does not issue additional queries. If the specified collation is unavailable on the target server,the connection will fail.
* `SetColumnsWithAlias`: When columnsWithAlias is true, calls to sql.Rows.Columns() will return the table alias and the column name separated by a dot.
* `SetInterpolateParams`: If interpolateParams is true, placeholders (?) in calls to db.Query() and db.Exec() are interpolated into a single query string with given parameters. This reduces the number of roundtrips, since the driver has to prepare a statement, execute it with given parameters and close the statement again with interpolateParams=false.
* `SetLoc`: SetLoc Sets the location for time.Time values (when using parseTime=true). "Local" sets the system's location. See time.LoadLocation in standard library for detail.
* `SetParseTime`: SetParseTime if set true changes the DATE and DATETIME type in mysql will be stored in a time.Time object rather than the []byte / string
* `SetReadTimeout`: SetReadTimeout I/O read timeout. timeout ∈ [1ms, 24h)
* `SetStrict`: SetStrict strict=true enables the strict mode in which MySQL warnings are treated as errors.
* `SetTimeout`: SetTimeout Driver side connection timeout. timeout ∈ [1ms, 24h)
* `SetWriteTimeout`: SetWriteTimeout I/O write timeout. timeout ∈ [1ms, 24h)