diff --git a/CHANGELOG.md b/CHANGELOG.md index 65e0fa31..368cf88f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This release uses a new Raft consensus version, with the move to Hashicorp Raft - [PR #601](https://github.com/rqlite/rqlite/pull/601): By default use Raft network address as node ID. - [PR #602](https://github.com/rqlite/rqlite/pull/602): Add method to Store that returns leader ID. - [PR #603](https://github.com/rqlite/rqlite/pull/603): Fix up status key name style. +- [PR #604](https://github.com/rqlite/rqlite/pull/604): JSON types are also text. ## 4.6.0 (November 29th 2019) _This release adds significant new functionality to the command-line tool, including much more control over backup and restore of the database. [Visit the Releases page](https://github.com/rqlite/rqlite/releases/tag/v4.6.0) to download this release._ diff --git a/db/db.go b/db/db.go index 39bcdd6e..73b2eac0 100644 --- a/db/db.go +++ b/db/db.go @@ -526,6 +526,7 @@ func normalizeRowValues(row []driver.Value, types []string) []interface{} { // http://www.sqlite.org/datatype3.html func isTextType(t string) bool { return t == "text" || + t == "json" || t == "" || strings.HasPrefix(t, "varchar") || strings.HasPrefix(t, "varying character") || diff --git a/db/db_test.go b/db/db_test.go index af6cf336..87aa987d 100644 --- a/db/db_test.go +++ b/db/db_test.go @@ -180,6 +180,30 @@ func Test_SimpleSingleStatements(t *testing.T) { } } +func Test_SimpleSingleJSONStatements(t *testing.T) { + db, path := mustCreateDatabase() + defer db.Close() + defer os.Remove(path) + + _, err := db.Execute([]string{"CREATE TABLE foo (c0 VARCHAR(36), c1 JSON, c2 NCHAR, c3 NVARCHAR, c4 CLOB)"}, false, false) + if err != nil { + t.Fatalf("failed to create table: %s", err.Error()) + } + + _, err = db.Execute([]string{`INSERT INTO foo(c0, c1, c2, c3, c4) VALUES("fiona", '{"mittens": "foobar"}', "bob", "dana", "declan")`}, false, false) + if err != nil { + t.Fatalf("failed to insert record: %s", err.Error()) + } + + r, err := db.Query([]string{"SELECT * FROM foo"}, false, false) + if err != nil { + t.Fatalf("failed to query: %s", err.Error()) + } + if exp, got := `[{"columns":["c0","c1","c2","c3","c4"],"types":["varchar(36)","json","nchar","nvarchar","clob"],"values":[["fiona","{\"mittens\": \"foobar\"}","bob","dana","declan"]]}]`, asJSON(r); exp != got { + t.Fatalf("unexpected results for query, expected %s, got %s", exp, got) + } +} + func Test_SimpleJoinStatements(t *testing.T) { db, path := mustCreateDatabase() defer db.Close()