Skip to content

Commit

Permalink
chore: adapt mysql mappings to new driver behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
atzoum committed Apr 16, 2024
1 parent 555d1be commit 03f0c32
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
15 changes: 14 additions & 1 deletion sqlconnect/internal/mysql/legacy_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mysql

import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
Expand All @@ -14,7 +15,7 @@ func legacyJsonRowMapper(databaseTypeName string, value any) any {
}
databaseTypeName = strings.Replace(databaseTypeName, "UNSIGNED ", "", 1)
switch databaseTypeName {
case "CHAR", "VARCHAR", "BLOB", "TEXT", "TINYBLOB", "TINYTEXT", "MEDIUMBLOB", "MEDIUMTEXT", "LONGBLOB", "LONGTEXT", "ENUM":
case "CHAR", "VARCHAR", "BLOB", "TEXT", "TINYBLOB", "TINYTEXT", "MEDIUMBLOB", "MEDIUMTEXT", "LONGBLOB", "LONGTEXT", "ENUM", "SET":
switch v := value.(type) {
case []uint8:
return string(v)
Expand All @@ -23,6 +24,8 @@ func legacyJsonRowMapper(databaseTypeName string, value any) any {
}
case "DATE", "DATETIME", "TIMESTAMP", "TIME", "YEAR":
switch v := value.(type) {
case int, int32, int64:
return fmt.Sprintf("%d", v)
case []uint8:
return string(v)
default:
Expand All @@ -31,6 +34,8 @@ func legacyJsonRowMapper(databaseTypeName string, value any) any {

case "FLOAT", "DOUBLE", "DECIMAL":
switch v := value.(type) {
case int, int32, int64, float32, float64:
return v
case []uint8:
n, err := strconv.ParseFloat(string(v), 64)
if err != nil {
Expand All @@ -46,6 +51,8 @@ func legacyJsonRowMapper(databaseTypeName string, value any) any {
}
case "INT", "TINYINT", "SMALLINt", "MEDIUMINT", "BIGINT", "UNSIGNED BIGINT":
switch v := value.(type) {
case int, int32, int64, float32, float64:
return v
case []uint8:
n, err := strconv.ParseInt(string(v), 10, 64)
if err != nil {
Expand All @@ -59,6 +66,12 @@ func legacyJsonRowMapper(databaseTypeName string, value any) any {
}
return n
}
case "SMALLINT":
switch v := value.(type) {
case int, int32, int64, float32, float64:
// converting to []byte to be backwards compatible
return []byte(fmt.Sprintf("%d", v))
}
}

return value
Expand Down
11 changes: 11 additions & 0 deletions sqlconnect/internal/mysql/mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mysql
import (
"encoding/binary"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -70,6 +71,10 @@ func jsonRowMapper(databaseTypeName string, value interface{}) interface{} {
stringValue = v.String()
case string:
stringValue = v
case int, int32, int64:
stringValue = fmt.Sprintf("%d", v)
default:
return value
}

switch databaseTypeName {
Expand Down Expand Up @@ -100,12 +105,18 @@ func jsonRowMapper(databaseTypeName string, value interface{}) interface{} {
case "JSON":
return json.RawMessage(stringValue)
case "FLOAT", "DOUBLE", "DECIMAL":
if stringValue == "" {
return nil

Check warning on line 109 in sqlconnect/internal/mysql/mappings.go

View check run for this annotation

Codecov / codecov/patch

sqlconnect/internal/mysql/mappings.go#L109

Added line #L109 was not covered by tests
}
n, err := strconv.ParseFloat(stringValue, 64)
if err != nil {
panic(err)
}
return n
case "INT", "TINYINT", "SMALLINT", "MEDIUMINT", "BIGINT":
if stringValue == "" {
return nil

Check warning on line 118 in sqlconnect/internal/mysql/mappings.go

View check run for this annotation

Codecov / codecov/patch

sqlconnect/internal/mysql/mappings.go#L118

Added line #L118 was not covered by tests
}
n, err := strconv.ParseInt(stringValue, 10, 64)
if err != nil {
panic(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"_tinytext": "TEXT",
"_mediumtext": "TEXT",
"_longtext": "TEXT",
"_enum": "CHAR",
"_set": "CHAR",
"_enum": "ENUM",
"_set": "SET",
"_date": "DATE",
"_datetime": "DATETIME",
"_timestamp": "TIMESTAMP",
Expand Down

0 comments on commit 03f0c32

Please sign in to comment.