Support bytea sql literal in postgres (#2943)

This commit is contained in:
David Martos 2024-04-06 14:37:28 +02:00 committed by GitHub
parent 37f120d287
commit 039838b2ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 15 deletions

View File

@ -115,9 +115,17 @@ final class SqlTypes {
return (dart.millisecondsSinceEpoch ~/ 1000).toString();
}
} else if (dart is Uint8List) {
// BLOB literals are string literals containing hexadecimal data and
// preceded by a single "x" or "X" character. Example: X'53514C697465'
return "x'${hex.encode(dart)}'";
final String hexString = hex.encode(dart);
if (dialect == SqlDialect.postgres) {
// Postgres BYTEA hex format
// https://www.postgresql.org/docs/current/datatype-binary.html#DATATYPE-BINARY-BYTEA-HEX-FORMAT
return "'\\x$hexString'::bytea";
} else {
// BLOB literals are string literals containing hexadecimal data and
// preceded by a single "x" or "X" character. Example: X'53514C697465'
return "x'${hex.encode(dart)}'";
}
} else if (dart is DriftAny) {
return mapToSqlLiteral(dart.rawSqlValue);
}

View File

@ -26,19 +26,19 @@ void main() {
return row.read(expression)!;
}
void testWith<T extends Object>(CustomSqlType<T>? type, T value) {
test('with variable', () async {
final variable = Variable(value, type);
expect(await eval(variable), value);
});
test('with constant', () async {
final constant = Constant(value, type);
expect(await eval(constant), value);
});
}
group('custom types pass through', () {
void testWith<T extends Object>(CustomSqlType<T> type, T value) {
test('with variable', () async {
final variable = Variable(value, type);
expect(await eval(variable), value);
});
test('with constant', () async {
final constant = Constant(value, type);
expect(await eval(constant), value);
});
}
group('uuid', () => testWith(PgTypes.uuid, Uuid().v4obj()));
group(
'interval',
@ -60,6 +60,8 @@ void main() {
);
});
group('bytea', () => testWith(null, Uint8List.fromList([1, 2, 3, 4, 5])));
test('compare datetimes', () async {
final time = DateTime.now();
final before = Variable(