Skip to content

Commit

Permalink
fix: fix sql generation for json.RawMessage
Browse files Browse the repository at this point in the history
Fixes an issue where SQL for json.RawMessage-expressions is not generated correctly (as it is with byte slices).

Closes doug-martin#350
  • Loading branch information
lefinal committed Aug 30, 2022
1 parent 31d438d commit 1d09d8e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sqlgen/expression_sql_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sqlgen

import (
"database/sql/driver"
"encoding/json"
"reflect"
"strconv"
"time"
Expand Down Expand Up @@ -137,6 +138,8 @@ func (esg *expressionSQLGenerator) reflectSQL(b sb.SQLBuilder, val interface{})
switch t := val.(type) {
case []byte:
esg.literalBytes(b, t)
case json.RawMessage:
esg.literalBytes(b, t)
case []exp.CommonTableExpression:
esg.commonTablesSliceSQL(b, t)
default:
Expand Down
7 changes: 7 additions & 0 deletions sqlgen/expression_sql_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sqlgen_test

import (
"database/sql/driver"
"encoding/json"
"fmt"
"regexp"
"testing"
Expand Down Expand Up @@ -221,6 +222,12 @@ func (esgs *expressionSQLGeneratorSuite) TestGenerate_BytesTypes() {

expressionTestCase{val: []byte("Hello'"), sql: "'Hello'''"},
expressionTestCase{val: []byte("Hello'"), sql: "?", isPrepared: true, args: []interface{}{[]byte("Hello'")}},

expressionTestCase{val: json.RawMessage("Hello"), sql: "'Hello'"},
expressionTestCase{val: json.RawMessage("Hello"), sql: "?", isPrepared: true, args: []interface{}{[]byte("Hello")}},

expressionTestCase{val: json.RawMessage("Hello'"), sql: "'Hello'''"},
expressionTestCase{val: json.RawMessage("Hello'"), sql: "?", isPrepared: true, args: []interface{}{[]byte("Hello'")}},
)
}

Expand Down

0 comments on commit 1d09d8e

Please sign in to comment.