Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/select_true_literal/mysql/db/db.go

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.

11 changes: 11 additions & 0 deletions internal/endtoend/testdata/select_true_literal/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- name: SelectTrue :one
SELECT true;

-- name: SelectFalse :one
SELECT false;

-- name: SelectTrueWithAlias :one
SELECT true AS is_active;

-- name: SelectMultipleBooleans :one
SELECT true AS col_a, false AS col_b, true AS col_c;
10 changes: 10 additions & 0 deletions internal/endtoend/testdata/select_true_literal/mysql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"version": "1",
"packages": [
{
"path": "db",
"engine": "mysql",
"queries": "query.sql"
}
]
}
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/select_true_literal/sqlite/db/db.go

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.

14 changes: 14 additions & 0 deletions internal/endtoend/testdata/select_true_literal/sqlite/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- name: SelectTrue :one
SELECT true;

-- name: SelectFalse :one
SELECT false;

-- name: SelectTrueWithAlias :one
SELECT true AS is_active;

-- name: SelectMultipleBooleans :one
SELECT true AS col_a, false AS col_b, true AS col_c;

-- name: SelectOne :one
SELECT 1;
10 changes: 10 additions & 0 deletions internal/endtoend/testdata/select_true_literal/sqlite/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"version": "1",
"packages": [
{
"path": "db",
"engine": "sqlite",
"queries": "query.sql"
}
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions internal/engine/dolphin/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

type cc struct {
paramCount int
sql string // Original SQL text for this statement
}

func todo(n pcast.Node) *ast.TODO {
Expand Down Expand Up @@ -672,6 +673,41 @@ func (c *cc) convertUpdateStmt(n *pcast.UpdateStmt) *ast.UpdateStmt {
return stmt
}

// isBooleanLiteral checks if the ValueExpr represents a boolean literal (true/false)
// by examining the original SQL text
func (c *cc) isBooleanLiteral(n *driver.ValueExpr) bool {
if c.sql == "" {
return false
}

pos := n.OriginTextPosition()
if pos < 0 || pos >= len(c.sql) {
return false
}

// Extract the token from the SQL text
remaining := strings.ToLower(c.sql[pos:])

// Check if it starts with "true" or "false" and is followed by a non-identifier character
if strings.HasPrefix(remaining, "true") {
if len(remaining) == 4 || !isIdentifierChar(remaining[4]) {
return true
}
}
if strings.HasPrefix(remaining, "false") {
if len(remaining) == 5 || !isIdentifierChar(remaining[5]) {
return true
}
}

return false
}

// isIdentifierChar returns true if the character can be part of an identifier
func isIdentifierChar(c byte) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'
}

func (c *cc) convertValueExpr(n *driver.ValueExpr) *ast.A_Const {
switch n.TexprNode.Type.GetType() {
case mysql.TypeBit:
Expand All @@ -691,6 +727,15 @@ func (c *cc) convertValueExpr(n *driver.ValueExpr) *ast.A_Const {
mysql.TypeYear,
mysql.TypeLong,
mysql.TypeLonglong:
// Check if this is a boolean literal (true/false)
if c.isBooleanLiteral(n) {
return &ast.A_Const{
Val: &ast.Boolean{
Boolval: n.Datum.GetInt64() != 0,
},
Location: n.OriginTextPosition(),
}
}
return &ast.A_Const{
Val: &ast.Integer{
Ival: n.Datum.GetInt64(),
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/dolphin/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
}
var stmts []ast.Statement
for i := range stmtNodes {
converter := &cc{}
converter := &cc{sql: string(blob)}
out := converter.convert(stmtNodes[i])
if _, ok := out.(*ast.TODO); ok {
continue
Expand Down
7 changes: 1 addition & 6 deletions internal/engine/sqlite/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,13 +752,8 @@ func (c *cc) convertLiteral(n *parser.Expr_literalContext) ast.Node {
}

if literal.TRUE_() != nil || literal.FALSE_() != nil {
var i int64
if literal.TRUE_() != nil {
i = 1
}

return &ast.A_Const{
Val: &ast.Integer{Ival: i},
Val: &ast.Boolean{Boolval: literal.TRUE_() != nil},
Location: n.GetStart().GetStart(),
}
}
Expand Down
Loading