- Notifications
You must be signed in to change notification settings - Fork 2.3k
Make TimeTruncate functional option#1552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
7 commits Select commit Hold shift + click to select a range
0c5e07e dsn: make Config.TimeTrancate private
methane 5c9a97b make Config.TimeTruncate a functional option
methane f4c212e fixup
methane 5f2654f revert NewConfig incompatible change
methane e42533e make Option public, and rename SetOptions to Apply
methane 86075b6 add doc comment
methane 5b603c9 private -> unexported
methane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -34,6 +34,8 @@ var ( | ||
| // If a new Config is created instead of being parsed from a DSN string, | ||
| // the NewConfig function should be used, which sets default values. | ||
| type Config struct{ | ||
| // non boolean fields | ||
| User string // Username | ||
| Passwd string // Password (requires User) | ||
| Net string // Network (e.g. "tcp", "tcp6", "unix". default: "tcp") | ||
| @@ -45,15 +47,15 @@ type Config struct{ | ||
| Loc *time.Location // Location for time.Time values | ||
| MaxAllowedPacket int // Max packet size allowed | ||
| ServerPubKey string // Server public key name | ||
| pubKey *rsa.PublicKey // Server public key | ||
| TLSConfig string // TLS configuration name | ||
| TLS *tls.Config // TLS configuration, its priority is higher than TLSConfig | ||
| TimeTruncate time.Duration // Truncate time.Time values to the specified duration | ||
| Timeout time.Duration // Dial timeout | ||
| ReadTimeout time.Duration // I/O read timeout | ||
| WriteTimeout time.Duration // I/O write timeout | ||
| Logger Logger // Logger | ||
| // boolean fields | ||
| AllowAllFiles bool // Allow all files to be used with LOAD DATA LOCAL INFILE | ||
| AllowCleartextPasswords bool // Allows the cleartext client side plugin | ||
| AllowFallbackToPlaintext bool // Allows fallback to unencrypted connection if server does not support TLS | ||
| @@ -66,17 +68,48 @@ type Config struct{ | ||
| MultiStatements bool // Allow multiple statements in one query | ||
| ParseTime bool // Parse time values to time.Time | ||
| RejectReadOnly bool // Reject read-only connections | ||
| // unexported fields. new options should be come here | ||
| pubKey *rsa.PublicKey // Server public key | ||
| timeTruncate time.Duration // Truncate time.Time values to the specified duration | ||
| } | ||
| // Functional Options Pattern | ||
| // https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis | ||
| type Option func(*Config) error | ||
| // NewConfig creates a new Config and sets default values. | ||
| func NewConfig() *Config{ | ||
| return &Config{ | ||
| cfg := &Config{ | ||
| Loc: time.UTC, | ||
| MaxAllowedPacket: defaultMaxAllowedPacket, | ||
| Logger: defaultLogger, | ||
| AllowNativePasswords: true, | ||
| CheckConnLiveness: true, | ||
| } | ||
| return cfg | ||
| } | ||
| // Apply applies the given options to the Config object. | ||
| func (c *Config) Apply(opts ...Option) error{ | ||
| for _, opt := range opts{ | ||
| err := opt(c) | ||
| if err != nil{ | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
| // TimeTruncate sets the time duration to truncate time.Time values in | ||
| // query parameters. | ||
| func TimeTruncate(d time.Duration) Option{ | ||
| return func(cfg *Config) error{ | ||
| cfg.timeTruncate = d | ||
| return nil | ||
| } | ||
| } | ||
| func (cfg *Config) Clone() *Config{ | ||
| @@ -263,8 +296,8 @@ func (cfg *Config) FormatDSN() string{ | ||
| writeDSNParam(&buf, &hasParam, "parseTime", "true") | ||
| } | ||
| if cfg.TimeTruncate > 0{ | ||
| writeDSNParam(&buf, &hasParam, "timeTruncate", cfg.TimeTruncate.String()) | ||
| if cfg.timeTruncate > 0{ | ||
| writeDSNParam(&buf, &hasParam, "timeTruncate", cfg.timeTruncate.String()) | ||
| } | ||
| if cfg.ReadTimeout > 0{ | ||
| @@ -509,9 +542,9 @@ func parseDSNParams(cfg *Config, params string) (err error){ | ||
| // time.Time truncation | ||
| case "timeTruncate": | ||
| cfg.TimeTruncate, err = time.ParseDuration(value) | ||
| cfg.timeTruncate, err = time.ParseDuration(value) | ||
methane marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| if err != nil{ | ||
| return | ||
| return fmt.Errorf("invalid timeTruncate value: %v, error: %w", value, err) | ||
| } | ||
| // I/O read Timeout | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -76,7 +76,7 @@ var testDSNs = []struct{ | ||
| &Config{Net: "tcp", Addr: "[de:ad:be:ef::ca:fe]:3306", DBName: "dbname", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, Logger: defaultLogger, AllowNativePasswords: true, CheckConnLiveness: true}, | ||
| },{ | ||
| "user:password@/dbname?loc=UTC&timeout=30s&parseTime=true&timeTruncate=1h", | ||
| &Config{User: "user", Passwd: "password", Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Loc: time.UTC, Timeout: 30 * time.Second, ParseTime: true, MaxAllowedPacket: defaultMaxAllowedPacket, Logger: defaultLogger, AllowNativePasswords: true, CheckConnLiveness: true, TimeTruncate: time.Hour}, | ||
| &Config{User: "user", Passwd: "password", Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Loc: time.UTC, Timeout: 30 * time.Second, ParseTime: true, MaxAllowedPacket: defaultMaxAllowedPacket, Logger: defaultLogger, AllowNativePasswords: true, CheckConnLiveness: true, timeTruncate: time.Hour}, | ||
| }, | ||
| } | ||
methane marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -15,9 +15,8 @@ import "database/sql/driver" | ||
| // This is accessible by executing statements using sql.Conn.Raw() and | ||
| // downcasting the returned result: | ||
| // | ||
| // res, err := rawConn.Exec(...) | ||
| // res.(mysql.Result).AllRowsAffected() | ||
| // | ||
| // res, err := rawConn.Exec(...) | ||
methane marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| // res.(mysql.Result).AllRowsAffected() | ||
| type Result interface{ | ||
| driver.Result | ||
| // AllRowsAffected returns a slice containing the affected rows for each | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.