Repository: proullon/ramsql
Pull request #75 (2023-05-31 15:35:24)

get json to work with ?
This is the second PR because I couldn't update the first one. @proullon  Sorry for the failed test.


Patch:
diff --git a/driver/stmt.go b/driver/stmt.go
index 8dc72b2..8bb4c8d 100644
--- a/driver/stmt.go
+++ b/driver/stmt.go
@@ -163,8 +163,8 @@ func replaceArguments(query string, args []driver.Value) string {
 		var v string
 		if args[index-1] == nil {
 			v = "null"
-		} else if _, ok := args[index-1].([]byte); ok {
-			v = fmt.Sprintf("$$%v$$", string(args[index-1].([]byte)))
+		} else if b, ok := args[index-1].([]byte); ok {
+			v = fmt.Sprintf("$$%s$$", b)
 		} else {
 			v = fmt.Sprintf("$$%v$$", args[index-1])
 		}
@@ -181,19 +181,29 @@ func replaceArguments(query string, args []driver.Value) string {
 }
 
 func replaceArgumentsODBC(query string, args []driver.Value) string {
-	var finalQuery string
+	finalQuery := &strings.Builder{}
 
 	queryParts := strings.Split(query, "?")
-	finalQuery = queryParts[0]
+
+	finalQuery.WriteString(queryParts[0])
+
 	for i := range args {
-		arg := fmt.Sprintf("%v", args[i])
-		_, ok := args[i].(string)
-		if ok && !strings.HasSuffix(query, "'") {
-			arg = "$$" + arg + "$$"
+		var arg string
+		switch v := args[i].(type) {
+		case string:
+			if !strings.HasSuffix(query, "'") {
+				arg = fmt.Sprintf("$$%s$$", v)
+			}
+		case []byte:
+			if !strings.HasSuffix(query, "'") {
+				arg = fmt.Sprintf("$$%s$$", v)
+			}
+		default:
+			arg = fmt.Sprintf("%v", v)
 		}
-		finalQuery += arg
-		finalQuery += queryParts[i+1]
+		finalQuery.WriteString(arg)
+		finalQuery.WriteString(queryParts[i+1])
 	}
 
-	return finalQuery
+	return finalQuery.String()
 }


Test patch:
diff --git a/driver/driver_test.go b/driver/driver_test.go
index 37c6298..30a14f6 100644
--- a/driver/driver_test.go
+++ b/driver/driver_test.go
@@ -4,6 +4,7 @@ import (
 	"database/sql"
 	"encoding/json"
 	"fmt"
+	"reflect"
 	"testing"
 	"time"
 
@@ -1077,7 +1078,7 @@ func TestBracketWhereClause(t *testing.T) {
 func TestInsertByteArray(t *testing.T) {
 	log.UseTestLogger(t)
 
-	db, err := sql.Open("ramsql", "TestBracketWhereClause")
+	db, err := sql.Open("ramsql", "TestInsertByteArray")
 	if err != nil {
 		t.Fatalf("sql.Open : Error : %s\n", err)
 	}
@@ -1103,3 +1104,34 @@ func TestInsertByteArray(t *testing.T) {
 		t.Fatalf("Expected JSON to be '%s', got '%s'", string(j), s)
 	}
 }
+
+func TestInsertByteArrayODBC(t *testing.T) {
+	log.UseTestLogger(t)
+
+	db, err := sql.Open("ramsql", "TestInsertByteArrayODBC")
+	if err != nil {
+		t.Fatalf("sql.Open : Error : %s\n", err)
+	}
+
+	_, err = db.Exec(`CREATE TABLE test_json (sequence_number BIGSERIAL PRIMARY KEY, json JSON, created_at TIMESTAMP)`)
+	if err != nil {
+		t.Fatalf("sql.Exec: Error: %s\n", err)
+	}
+
+	j, _ := json.Marshal(map[string]string{"a": "a"})
+
+	_, err = db.Exec("INSERT INTO test_json (json) values (?)", j)
+	if err != nil {
+		t.Fatalf("sql.Exec: Error: %s\n", err)
+	}
+
+	var s []byte
+	err = db.QueryRow("SELECT json FROM test_json limit 1").Scan(&s)
+	if err != nil {
+		t.Fatalf("sql.Select: Error: %s\n", err)
+	}
+
+	if !reflect.DeepEqual(s, j) {
+		t.Fatalf("Expected JSON to be '%s', got '%s'", j, s)
+	}
+}