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

fix sending json with execute #11

Merged
merged 2 commits into from
Aug 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.4.1+2

- Fix error when sending json data with `execute()` [#11](https://github.com/isoos/postgresql-dart/pull/11)

## 2.4.1+1

- Fix error when passing `allowReuse: null` into `query()` [#8](https://github.com/isoos/postgresql-dart/pull/8)
Expand Down
6 changes: 3 additions & 3 deletions lib/src/text_codec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class PostgresTextEncoder {
}

if (value is Map) {
return _encodeJSON(value);
return _encodeJSON(value, escapeStrings);
}

if (value is PgPoint) {
Expand Down Expand Up @@ -152,7 +152,7 @@ class PostgresTextEncoder {
return "'$string'";
}

String _encodeJSON(dynamic value) {
String _encodeJSON(dynamic value, bool escapeStrings) {
if (value == null) {
return 'null';
}
Expand All @@ -161,7 +161,7 @@ class PostgresTextEncoder {
return "'${json.encode(value)}'";
}

return json.encode(value);
return _encodeString(json.encode(value), escapeStrings);
}

String _encodePoint(PgPoint value) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: postgres
description: PostgreSQL database driver. Supports statement reuse and binary protocol.
version: 2.4.1+1
version: 2.4.1+2
homepage: https://github.com/isoos/postgresql-dart

environment:
Expand Down
9 changes: 6 additions & 3 deletions test/encoding_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -546,9 +546,12 @@ void main() {
});

test('Encode JSONB', () {
expect(encoder.convert({'a': 'b'}), '{"a":"b"}');
expect(encoder.convert({'a': true}), '{"a":true}');
expect(encoder.convert({'b': false}), '{"b":false}');
expect(encoder.convert({'a': 'b'}, escapeStrings: false), '{"a":"b"}');
expect(encoder.convert({'a': true}, escapeStrings: false), '{"a":true}');
expect(
encoder.convert({'b': false}, escapeStrings: false), '{"b":false}');
expect(encoder.convert({'a': true}), '\'{"a":true}\'');
expect(encoder.convert({'b': false}), '\'{"b":false}\'');
});

test('Attempt to infer unknown type throws exception', () {
Expand Down
14 changes: 14 additions & 0 deletions test/json_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ void main() {
]
]);
});
test('Can store JSON map with execute', () async {
final result = await connection.execute(
'INSERT INTO t (j) VALUES (@a:jsonb) RETURNING j',
substitutionValues: {
'a': {'a': 4}
});
expect(result, 1);
final resultQuery = await connection.query('SELECT j FROM t');
expect(resultQuery, [
[
{'a': 4}
]
]);
});

test('Can store JSON list', () async {
var result = await connection
Expand Down