-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/parser,v2/pkg/orm: support mysql functions and adds generic …
…ToResult helper MySQL driver will scan `any` type field into `[]byte` as [it mentioned](go-sql-driver/mysql#441) However, we cannot determine the underlying type of every mysql functions currently. We need a helper function to generic the `[]byte` -> wellknown SQL type(e.g.: `NullInt64`) to avoid spamming the conversion code all over our business codebase.
- Loading branch information
Showing
6 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
SELECT | ||
COUNT(id) as count | ||
FROM | ||
blogs | ||
WHERE | ||
id > 1; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
SELECT | ||
ROUND(id,1) as rid, | ||
UPPER(title) as u_title, | ||
LENGTH(title) as len_title | ||
FROM | ||
blogs | ||
WHERE | ||
id = 1; |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package orm | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
) | ||
|
||
type Result interface { | ||
sql.NullInt64 | sql.NullString | sql.NullFloat64 | ||
} | ||
|
||
var ErrScanResultTypeUnsupported = fmt.Errorf("scan result type unsupported") | ||
|
||
// ToResult is helper function which sits in the background | ||
// when we cannot determine the underlying type of MySQL scan result(such as generic function result). | ||
|
||
// Inspired by https://github.com/go-sql-driver/mysql/issues/86, | ||
// if the Go SQL driver cannot determine the underlying type of scan result(`interface{}` or `any`), | ||
// it will fallback to TEXT protocol to communicate with MySQL server, | ||
// therefore, the result can only be `[]uint8`(`[]byte`) or raw `string` or `int64` | ||
func ToResult[T Result](rawField any) (T, error) { | ||
var t T | ||
switch rawField := rawField.(type) { | ||
default: | ||
return t, fmt.Errorf("rawField type got %T", rawField) | ||
case int64: | ||
switch any(t).(type) { | ||
default: | ||
return t, ErrScanResultTypeUnsupported | ||
case sql.NullInt64: | ||
i := &sql.NullInt64{} | ||
if err := i.Scan(rawField); err != nil { | ||
return t, err | ||
} | ||
t = any(*i).(T) | ||
} | ||
case string: | ||
switch any(t).(type) { | ||
default: | ||
return t, ErrScanResultTypeUnsupported | ||
case sql.NullString: | ||
s := &sql.NullString{} | ||
if err := s.Scan(rawField); err != nil { | ||
return t, err | ||
} | ||
t = any(*s).(T) | ||
} | ||
case []byte: | ||
switch any(t).(type) { | ||
default: | ||
return t, ErrScanResultTypeUnsupported | ||
case sql.NullInt64: | ||
i := &sql.NullInt64{} | ||
if err := i.Scan(rawField); err != nil { | ||
return t, err | ||
} | ||
t = any(*i).(T) | ||
case sql.NullFloat64: | ||
f := &sql.NullFloat64{} | ||
if err := f.Scan(rawField); err != nil { | ||
return t, err | ||
} | ||
t = any(*f).(T) | ||
} | ||
} | ||
return t, nil | ||
} |