-
Notifications
You must be signed in to change notification settings - Fork 990
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
feat: Added support for datetime format #957
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems fine to me.
Some small things:
errcheck
(via golangci-lint) reports a few issues- I think this should have some tests
How would this affect upgrades and compatibility?
Thanks for the detailed example in the description
Your example didn't include imports or content of go.mod and seems to use a go-sql-driver style dsn. Could you provide a full example? This is what I used to test this: package main
import (
"fmt"
"log"
"time"
_ "github.com/go-mysql-org/go-mysql/driver"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type UserInfo struct {
ID int
CreatedAt time.Time
UpdatedAt *time.Time
}
func (qc *UserInfo) TableName() string {
return "user_info"
}
func main() {
User := "root"
Passwd := ""
Host := "127.0.0.1"
Port := 4000
DbName := "test"
constr := fmt.Sprintf("%s:%s@%s:%d/%s", User, Passwd, Host, Port, DbName)
client, err := gorm.Open(mysql.New(mysql.Config{
DriverName: "gomysql",
DSN: constr,
DefaultStringSize: 1 << 10,
DisableDatetimePrecision: true,
DontSupportRenameIndex: true,
DontSupportRenameColumn: true,
SkipInitializeWithVersion: false,
}))
if err != nil {
panic(fmt.Errorf("failed to connect to mysql: %v", err))
}
var user UserInfo
err = client.Where("id = ?", 1).First(&user).Error
if err != nil {
log.Println(err)
return
}
fmt.Printf("%+v\n", user)
} module issue957
go 1.23.3
require (
github.com/go-mysql-org/go-mysql v1.10.0
gorm.io/driver/mysql v1.5.7
gorm.io/gorm v1.25.12
)
require (
github.com/Masterminds/semver v1.5.0 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/klauspost/compress v1.17.8 // indirect
github.com/pingcap/errors v0.11.5-0.20240311024730-e056997136bb // indirect
github.com/pingcap/log v1.1.1-0.20230317032135-a0d097d16e22 // indirect
github.com/pingcap/tidb/pkg/parser v0.0.0-20241118164214-4f047be191be // indirect
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726 // indirect
github.com/siddontang/go-log v0.0.0-20180807004314-8d05993dda07 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
)
replace github.com/go-mysql-org/go-mysql => /home/dvaneeden/dev/go-mysql The replace is to use a checked out version of this PR I also had to do this: diff --git a/driver/driver.go b/driver/driver.go
index 4054db2..fa36dc8 100644
--- a/driver/driver.go
+++ b/driver/driver.go
@@ -389,13 +389,15 @@ func (r *rows) Next(dest []sqldriver.Value) error {
return nil
}
+var driverName = "mysql"
+
func init() {
options["compress"] = CompressOption
options["collation"] = CollationOption
options["readTimeout"] = ReadTimeoutOption
options["writeTimeout"] = WriteTimeoutOption
- sql.Register("mysql", driver{})
+ sql.Register(driverName, driver{})
}
// SetCustomTLSConfig sets a custom TLSConfig for the address (host:port) of the supplied DSN.
This is to avoid the name collision between go-mysql and go-sql-driver/mysql which both register The result is this:
So it looks like this isn't working as intended. |
I created #958 to make it easier to use go-mysql with GORM |
Thank you for your reply I created a sample library, you can have a look at this. |
Isn't this PR working on the server part of your example code instead of the client side? |
I use replace to point to the local test after I clone the project locally |
Hi, please fix linter error
|
Okay, I took care of it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rest lgtm. Thanks!
|
||
"github.com/pingcap/errors" | ||
"github.com/siddontang/go/hack" | ||
) | ||
|
||
func toBinaryDateTime(t time.Time) ([]byte, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's FormatBinaryDateTime
under mysql/util.go
. Can you move this function adjacent to FormatBinaryDateTime
? So we can compare their logic easier.
Also, please add a unit test for FormatBinaryDateTime
and ToBinaryDateTime
, covert some value back and forth
Please resolve the conflict |
Cannot set the entity field to time.Time when connected via gorm.
Add formatBinaryValue support for time.Time type