Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON objects in SQLite are returned as strings #3580

Open
waterfountain1996 opened this issue Sep 4, 2024 · 0 comments
Open

JSON objects in SQLite are returned as strings #3580

waterfountain1996 opened this issue Sep 4, 2024 · 0 comments

Comments

@waterfountain1996
Copy link

Version

1.27.0

What happened?

A json_object() function that returns a JSON-formatted object is scanned as a string instead of []byte or json.RawMessage. The return type for a :many query that does SELECT json_object() is []interface{}, while each item in the slice is of type string, therefore, when rendered as JSON, becomes an escaped JSON string. I've tried manually updating the generated code to cast strings to json.RawMessage and it worked as expected. Configuring an override of a json DB type to json.RawMessage results in "0" being returned. Configuring an override for jsonb instead results in some base64-encoded string.

I cannot find a workaround for this at the moment (apart from modifying the auto-generated code) but I think that json_object() and jsonb_object() (perhaps json_array() as well) expressions should be compiled as json.RawMessage.

Relevant log output

items, err := queries.New(db).Foo(context.Background())
if err != nil { panic(err) }
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", "  ")
enc.Encode(outbox)

// stdout:
// [
//   "{\"id\":3,\"value\":\"baz\"}",
//   "{\"id\":2,\"value\":\"bar\"}",
//   "{\"id\":1,\"value\":\"foo\"}"
// ]

Database schema

CREATE TABLE foo (
	id INTEGER PRIMARY KEY AUTOINCREMENT,
	value TEXT NOT NULL
);

SQL queries

SELECT json_object('id', id, 'value', value)
FROM foo
ORDER BY id DESC;

Configuration

version: '2'
sql:
  - engine: 'sqlite'
    schema: db/schema.sql
    queries: db/queries
    gen:
      go:
        package: 'queries'
        out: 'queries'
        emit_result_struct_pointers: true
        emit_params_struct_pointers: true

Playground URL

No response

What operating system are you using?

macOS

What database engines are you using?

SQLite

What type of code are you generating?

Go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant