-
Notifications
You must be signed in to change notification settings - Fork 802
Commit
What is this As the title said, this PR wants to add support for CAST function in MySQL. This PR is based from PR by @ryanpbrewster here (which unfortunately he didn't send here, and only exist in his repository). Why is this PR created Currently sqlc unable to infer the correct type from SQL function like MAX, MIN, SUM, etc. For those function, sqlc will return its value as interface{}. This behavior can be seen in this playground. As workaround, it advised to use CAST function to explicitly tell what is the type for that column, as mentioned in #1574. Unfortunately, currently sqlc only support CAST function in PostgreSQL and not in MySQL. Thanks to this, right now MySQL users have to parse the interface{} manually, which is not really desirable. What does this PR do? Implement convertFuncCast function for MySQL. Add better nil pointer check in some functions that related to convertFuncCast. I haven't write any test because I'm not sure how and where to put it. However, as far as I know the code that handle ast.TypeCast for PostgreSQL also don't have any test, so I guess it's fine 🤷♂️ Related issues Support CAST ... AS #687, which currently is the oldest MySQL issue that still opened. Using MYSQL functions ( CONVERT and CAST) result in removing column from struct #1622 Unable to Type Alias #1866 sum in select result in model field type interface{} #1901 MIN() returns an interface{} #1965
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- name: Demo :one | ||
SELECT CAST(GREATEST(1,2,3,4,5) AS UNSIGNED) as col1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"version": "1", | ||
"packages": [ | ||
{ | ||
"path": "go", | ||
"engine": "mysql", | ||
"name": "querytest", | ||
"schema": "query.sql", | ||
"queries": "query.sql" | ||
} | ||
] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
CREATE TABLE foo (bar BOOLEAN NOT NULL); | ||
|
||
-- name: SelectColumnCast :many | ||
SELECT CAST(bar AS UNSIGNED) FROM foo; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"version": "1", | ||
"packages": [ | ||
{ | ||
"path": "go", | ||
"engine": "mysql", | ||
"name": "querytest", | ||
"schema": "query.sql", | ||
"queries": "query.sql" | ||
} | ||
] | ||
} |