Skip to content
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
21 changes: 16 additions & 5 deletions lib/core/database/postgres_result_cells.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ String postgresResultCellToDisplayString(
}

if (value is DateTime) {
final iso = value.toIso8601String();
if (_isDateOnly(typeOid, dataTypeName)) {
return value.toIso8601String().split('T').first;
// The text before `T` (also right for years outside 0..9999, which
// carry a sign and six digits), without a split() list (#1529).
final t = iso.indexOf('T');
return t < 0 ? iso : iso.substring(0, t);
}
return value.toIso8601String();
return iso;
}

if (value is bool) return value ? 'true' : 'false';
Expand All @@ -77,11 +81,18 @@ String postgresResultCellToDisplayString(

/// PostgreSQL bytea text `\xdeadbeef`.
String postgresByteaHexDisplay(Uint8List bytes) {
final out = StringBuffer(r'\x');
// Two lower-case hex digits per byte, written as code units into one buffer:
// no toRadixString / padLeft string per byte (#1529, #1521).
const hexDigits = '0123456789abcdef';
final out = Uint8List(2 + bytes.length * 2);
out[0] = 0x5C; // \
out[1] = 0x78; // x
var k = 2;
for (final b in bytes) {
out.write(b.toRadixString(16).padLeft(2, '0'));
out[k++] = hexDigits.codeUnitAt(b >> 4);
out[k++] = hexDigits.codeUnitAt(b & 0x0F);
}
return out.toString();
return String.fromCharCodes(out);
}

String postgresByteaHexDisplayFromCell(Object value) {
Expand Down
73 changes: 73 additions & 0 deletions test/core/database/postgres_result_cells_scanner_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'dart:typed_data';

import 'package:flutter_test/flutter_test.dart';
import 'package:postgres/postgres.dart';
import 'package:querya_desktop/core/database/postgres_result_cells.dart';

void main() {
group('postgresByteaHexDisplay (#1529, #1521)', () {
test('writes \\x and two lower-case hex digits per byte', () {
expect(postgresByteaHexDisplay(Uint8List(0)), r'\x');
expect(
postgresByteaHexDisplay(Uint8List.fromList([0, 1, 15, 16, 171, 255])),
r'\x00010f10abff',
);
});

test('every byte value maps to its two digits', () {
final all = Uint8List.fromList(List.generate(256, (i) => i));
final text = postgresByteaHexDisplay(all);
expect(text.length, 2 + 512);
for (var i = 0; i < 256; i++) {
expect(text.substring(2 + i * 2, 4 + i * 2),
i.toRadixString(16).padLeft(2, '0'));
}
});

test('cell display goes through the same encoder', () {
expect(
postgresResultCellToDisplayString(Uint8List.fromList([222, 173])),
r'\xdead',
);
});
});

group('date-only cells (#1529, #1521)', () {
test('a date column shows only the date', () {
expect(
postgresResultCellToDisplayString(
DateTime.utc(2026, 1, 5),
typeOid: Type.date.oid,
),
'2026-01-05',
);
expect(
postgresResultCellToDisplayString(
DateTime(2026, 1, 5, 13, 30),
dataTypeName: 'date',
),
'2026-01-05',
);
});

test('years outside 0..9999 keep their sign and digits', () {
expect(
postgresResultCellToDisplayString(
DateTime.utc(12000, 3, 4),
typeOid: Type.date.oid,
),
'+012000-03-04',
);
});

test('timestamps keep the full ISO text', () {
expect(
postgresResultCellToDisplayString(
DateTime.utc(2026, 1, 5, 7, 8, 9),
typeOid: Type.timestamp.oid,
),
'2026-01-05T07:08:09.000Z',
);
});
});
}
Loading