Skip to content

Commit

Permalink
Merge branch 'development' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
Umang01-hash authored Nov 7, 2024
2 parents d0f432a + d755a97 commit bd0bea5
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 9 deletions.
9 changes: 7 additions & 2 deletions docs/quick-start/connecting-mysql/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ DB_PASSWORD=root123
DB_NAME=test_db
DB_PORT=3306
DB_DIALECT=mysql
DB_CHARSET=
# DB_CHARSET: The character set for database connection (default: utf8).
# The `DB_CHARSET` defaults to utf8, but setting it to utf8mb4 is recommended if you need full Unicode support,
# including emojis and special characters.
```

Now in the following example, we'll store customer data using **POST** `/customer` and then use **GET** `/customer` to retrieve the same.
Expand All @@ -50,7 +55,7 @@ import (
"errors"

"github.com/redis/go-redis/v9"

"gofr.dev/pkg/gofr"
)

Expand Down Expand Up @@ -105,7 +110,7 @@ func main() {
// return the customer
return customers, nil
})

app.Run()
}
```
Expand Down
2 changes: 1 addition & 1 deletion pkg/gofr/datasource/file/ftp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ require (
github.com/rogpeppe/go-internal v1.10.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
)
2 changes: 1 addition & 1 deletion pkg/gofr/datasource/file/ftp/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion pkg/gofr/datasource/file/s3/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
)
2 changes: 1 addition & 1 deletion pkg/gofr/datasource/file/s3/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion pkg/gofr/datasource/file/sftp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.26.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
)
2 changes: 1 addition & 1 deletion pkg/gofr/datasource/file/sftp/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9 changes: 8 additions & 1 deletion pkg/gofr/datasource/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type DBConfig struct {
SSLMode string
MaxIdleConn int
MaxOpenConn int
Charset string
}

func NewSQL(configs config.Config, logger datasource.Logger, metrics Metrics) *DB {
Expand Down Expand Up @@ -159,18 +160,24 @@ func getDBConfig(configs config.Config) *DBConfig {
MaxIdleConn: maxIdleConn,
// only for postgres
SSLMode: configs.GetOrDefault("DB_SSL_MODE", "disable"),
Charset: configs.Get("DB_CHARSET"),
}
}

func getDBConnectionString(dbConfig *DBConfig) (string, error) {
switch dbConfig.Dialect {
case "mysql":
return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local&interpolateParams=true",
if dbConfig.Charset == "" {
dbConfig.Charset = "utf8"
}

return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=True&loc=Local&interpolateParams=true",
dbConfig.User,
dbConfig.Password,
dbConfig.HostName,
dbConfig.Port,
dbConfig.Database,
dbConfig.Charset,
), nil
case "postgres":
return fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
Expand Down
15 changes: 15 additions & 0 deletions pkg/gofr/datasource/sql/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func TestSQL_GetDBConfig(t *testing.T) {
"DB_SSL_MODE": "require",
"DB_MAX_IDLE_CONNECTION": "25",
"DB_MAX_OPEN_CONNECTION": "50",
"DB_CHARSET": "utf8mb4",
})

expectedComfigs := &DBConfig{
Expand All @@ -118,6 +119,7 @@ func TestSQL_GetDBConfig(t *testing.T) {
SSLMode: "require",
MaxIdleConn: 25,
MaxOpenConn: 50,
Charset: "utf8mb4",
}

configs := getDBConfig(mockConfig)
Expand Down Expand Up @@ -187,6 +189,19 @@ func TestSQL_getDBConnectionString(t *testing.T) {
},
expOut: "user:password@tcp(host:3201)/test?charset=utf8&parseTime=True&loc=Local&interpolateParams=true",
},
{
desc: "mysql dialect with Configurable charset",
configs: &DBConfig{
Dialect: "mysql",
HostName: "host",
User: "user",
Password: "password",
Port: "3201",
Database: "test",
Charset: "utf8mb4",
},
expOut: "user:password@tcp(host:3201)/test?charset=utf8mb4&parseTime=True&loc=Local&interpolateParams=true",
},
{
desc: "postgresql dialect",
configs: &DBConfig{
Expand Down

0 comments on commit bd0bea5

Please sign in to comment.