From 3dc7af8d93511258a8f3a3cde6fb7baedf9dccd8 Mon Sep 17 00:00:00 2001 From: Bob Jackman Date: Thu, 18 Apr 2019 11:33:41 -0700 Subject: [PATCH] Make fields accessible by name in `BinaryDataPacket`s, too --- .../binary_data_packet.dart | 1 + lib/src/results/row.dart | 2 +- test/integration/one_test.dart | 35 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/src/prepared_statements/binary_data_packet.dart b/lib/src/prepared_statements/binary_data_packet.dart index 28d34a6..6d0c004 100644 --- a/lib/src/prepared_statements/binary_data_packet.dart +++ b/lib/src/prepared_statements/binary_data_packet.dart @@ -42,6 +42,7 @@ class BinaryDataPacket extends Row { } var field = fieldPackets[i]; values[i] = readField(field, buffer); + fields[field.name] = values[i]; } } diff --git a/lib/src/results/row.dart b/lib/src/results/row.dart index 55f6e80..728bca7 100644 --- a/lib/src/results/row.dart +++ b/lib/src/results/row.dart @@ -36,7 +36,7 @@ abstract class Row extends ListBase { throw new UnsupportedError("Cannot modify row"); } - String toString() => "Value: $values"; + String toString() => "Fields: $fields"; Object readField(Field field, Buffer buffer); } diff --git a/test/integration/one_test.dart b/test/integration/one_test.dart index 09ebb8c..b3fbccd 100644 --- a/test/integration/one_test.dart +++ b/test/integration/one_test.dart @@ -380,6 +380,41 @@ void main() { expect(dt, equals(dt2)); }); + test('result fields are accessible by name', () async { + var results = await conn.query( + "insert into test1 (atinyint, asmallint, amediumint, abigint, aint, " + "adecimal, afloat, adouble, areal, " + "aboolean, abit, aserial, " + "adate, adatetime, atimestamp, atime, ayear, " + "achar, avarchar, atinytext, atext, amediumtext, alongtext, " + "abinary, avarbinary, atinyblob, amediumblob, ablob, alongblob, " + "aenum, aset) values" + "(?, ?, ?, ?, ?, " + "?, ?, ?, ?, " + "?, ?, ?, " + "?, ?, ?, ?, ?, " + "?, ?, ?, ?, ?, ?, " + "?, ?, ?, ?, ?, ?, " + "?, ?)", + insertValues); + + // Normal + results = await conn.query('select atinyint from test1'); + int v1 = results.first.fields['atinyint']; + int v2 = results.first['atinyint']; + expect(v1, isNotNull); + expect(v2, equals(v1)); + + // Binary packet + results = await conn.query('select atinyint from test1 WHERE ? = ?', [1, 1]); + int v3 = results.first.fields['atinyint']; + int v4 = results.first['atinyint']; + expect(v3, isNotNull); + expect(v4, equals(v3)); + + expect(v1, equals(v3)); + }); + test('disallow non-utc datetime serialization', () async { expect(() async { var results = await conn.query(