Skip to content

Commit 6ba2468

Browse files
authored
fix sending json with execute (#11)
* create test failure for sending json with execute * fix json encoding with `execute`
1 parent 7763f98 commit 6ba2468

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 2.4.1+2
4+
5+
- Fix error when sending json data with `execute()` [#11](https://github.com/isoos/postgresql-dart/pull/11)
6+
37
## 2.4.1+1
48

59
- Fix error when passing `allowReuse: null` into `query()` [#8](https://github.com/isoos/postgresql-dart/pull/8)

lib/src/text_codec.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class PostgresTextEncoder {
2929
}
3030

3131
if (value is Map) {
32-
return _encodeJSON(value);
32+
return _encodeJSON(value, escapeStrings);
3333
}
3434

3535
if (value is PgPoint) {
@@ -152,7 +152,7 @@ class PostgresTextEncoder {
152152
return "'$string'";
153153
}
154154

155-
String _encodeJSON(dynamic value) {
155+
String _encodeJSON(dynamic value, bool escapeStrings) {
156156
if (value == null) {
157157
return 'null';
158158
}
@@ -161,7 +161,7 @@ class PostgresTextEncoder {
161161
return "'${json.encode(value)}'";
162162
}
163163

164-
return json.encode(value);
164+
return _encodeString(json.encode(value), escapeStrings);
165165
}
166166

167167
String _encodePoint(PgPoint value) {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: postgres
22
description: PostgreSQL database driver. Supports statement reuse and binary protocol.
3-
version: 2.4.1+1
3+
version: 2.4.1+2
44
homepage: https://github.com/isoos/postgresql-dart
55

66
environment:

test/encoding_test.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,12 @@ void main() {
546546
});
547547

548548
test('Encode JSONB', () {
549-
expect(encoder.convert({'a': 'b'}), '{"a":"b"}');
550-
expect(encoder.convert({'a': true}), '{"a":true}');
551-
expect(encoder.convert({'b': false}), '{"b":false}');
549+
expect(encoder.convert({'a': 'b'}, escapeStrings: false), '{"a":"b"}');
550+
expect(encoder.convert({'a': true}, escapeStrings: false), '{"a":true}');
551+
expect(
552+
encoder.convert({'b': false}, escapeStrings: false), '{"b":false}');
553+
expect(encoder.convert({'a': true}), '\'{"a":true}\'');
554+
expect(encoder.convert({'b': false}), '\'{"b":false}\'');
552555
});
553556

554557
test('Attempt to infer unknown type throws exception', () {

test/json_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ void main() {
103103
]
104104
]);
105105
});
106+
test('Can store JSON map with execute', () async {
107+
final result = await connection.execute(
108+
'INSERT INTO t (j) VALUES (@a:jsonb) RETURNING j',
109+
substitutionValues: {
110+
'a': {'a': 4}
111+
});
112+
expect(result, 1);
113+
final resultQuery = await connection.query('SELECT j FROM t');
114+
expect(resultQuery, [
115+
[
116+
{'a': 4}
117+
]
118+
]);
119+
});
106120

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

0 commit comments

Comments
 (0)