From 1d09d8eac3f803d799222b7d743292d5e46c90ce Mon Sep 17 00:00:00 2001 From: Lennart Altenhof Date: Tue, 30 Aug 2022 23:09:40 +0200 Subject: [PATCH] fix: fix sql generation for json.RawMessage Fixes an issue where SQL for json.RawMessage-expressions is not generated correctly (as it is with byte slices). Closes #350 --- sqlgen/expression_sql_generator.go | 3 +++ sqlgen/expression_sql_generator_test.go | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/sqlgen/expression_sql_generator.go b/sqlgen/expression_sql_generator.go index 82ce15c5..18f6e9d5 100644 --- a/sqlgen/expression_sql_generator.go +++ b/sqlgen/expression_sql_generator.go @@ -2,6 +2,7 @@ package sqlgen import ( "database/sql/driver" + "encoding/json" "reflect" "strconv" "time" @@ -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: diff --git a/sqlgen/expression_sql_generator_test.go b/sqlgen/expression_sql_generator_test.go index ed75e12f..a859e16e 100644 --- a/sqlgen/expression_sql_generator_test.go +++ b/sqlgen/expression_sql_generator_test.go @@ -2,6 +2,7 @@ package sqlgen_test import ( "database/sql/driver" + "encoding/json" "fmt" "regexp" "testing" @@ -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'")}}, ) }