diff --git a/docs/quick-start/connecting-mysql/page.md b/docs/quick-start/connecting-mysql/page.md index b6043eb5c..c5674db0b 100644 --- a/docs/quick-start/connecting-mysql/page.md +++ b/docs/quick-start/connecting-mysql/page.md @@ -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. @@ -50,7 +55,7 @@ import ( "errors" "github.com/redis/go-redis/v9" - + "gofr.dev/pkg/gofr" ) @@ -105,7 +110,7 @@ func main() { // return the customer return customers, nil }) - + app.Run() } ``` diff --git a/pkg/gofr/datasource/file/ftp/go.mod b/pkg/gofr/datasource/file/ftp/go.mod index 69d4422eb..6919e322a 100644 --- a/pkg/gofr/datasource/file/ftp/go.mod +++ b/pkg/gofr/datasource/file/ftp/go.mod @@ -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 -) +) \ No newline at end of file diff --git a/pkg/gofr/datasource/file/ftp/go.sum b/pkg/gofr/datasource/file/ftp/go.sum index 11c7d1680..ad22d4586 100644 --- a/pkg/gofr/datasource/file/ftp/go.sum +++ b/pkg/gofr/datasource/file/ftp/go.sum @@ -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= \ No newline at end of file diff --git a/pkg/gofr/datasource/file/s3/go.mod b/pkg/gofr/datasource/file/s3/go.mod index 4fa4d32ae..323efce2a 100644 --- a/pkg/gofr/datasource/file/s3/go.mod +++ b/pkg/gofr/datasource/file/s3/go.mod @@ -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 -) +) \ No newline at end of file diff --git a/pkg/gofr/datasource/file/s3/go.sum b/pkg/gofr/datasource/file/s3/go.sum index 284ef1896..0e1c602d8 100644 --- a/pkg/gofr/datasource/file/s3/go.sum +++ b/pkg/gofr/datasource/file/s3/go.sum @@ -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= \ No newline at end of file diff --git a/pkg/gofr/datasource/file/sftp/go.mod b/pkg/gofr/datasource/file/sftp/go.mod index 8c6c99a07..a955395ad 100644 --- a/pkg/gofr/datasource/file/sftp/go.mod +++ b/pkg/gofr/datasource/file/sftp/go.mod @@ -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 -) +) \ No newline at end of file diff --git a/pkg/gofr/datasource/file/sftp/go.sum b/pkg/gofr/datasource/file/sftp/go.sum index 23a4e010a..1b978f9bf 100644 --- a/pkg/gofr/datasource/file/sftp/go.sum +++ b/pkg/gofr/datasource/file/sftp/go.sum @@ -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= \ No newline at end of file diff --git a/pkg/gofr/datasource/sql/sql.go b/pkg/gofr/datasource/sql/sql.go index 8c30e4196..09e4c4b2b 100644 --- a/pkg/gofr/datasource/sql/sql.go +++ b/pkg/gofr/datasource/sql/sql.go @@ -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 { @@ -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", diff --git a/pkg/gofr/datasource/sql/sql_test.go b/pkg/gofr/datasource/sql/sql_test.go index ed1828446..343526523 100644 --- a/pkg/gofr/datasource/sql/sql_test.go +++ b/pkg/gofr/datasource/sql/sql_test.go @@ -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{ @@ -118,6 +119,7 @@ func TestSQL_GetDBConfig(t *testing.T) { SSLMode: "require", MaxIdleConn: 25, MaxOpenConn: 50, + Charset: "utf8mb4", } configs := getDBConfig(mockConfig) @@ -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{