Skip to content

Commit

Permalink
Fixing some more in query_test.dart (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Oct 12, 2023
1 parent 0287791 commit 5b120c0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
19 changes: 13 additions & 6 deletions lib/src/v3/query_description.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ class InternalQueryDescription implements PgSql {
}
}

PgTypedParameter _toParameter(Object? value, PgDataType? knownType) {
PgTypedParameter _toParameter(
Object? value,
PgDataType? knownType, {
String? name,
}) {
if (value is PgTypedParameter) {
return value;
} else if (knownType != null) {
Expand All @@ -83,12 +87,16 @@ class InternalQueryDescription implements PgSql {
return PgTypedParameter(PgDataType.double, value);
} else if (value is DateTime) {
return PgTypedParameter(PgDataType.timestampWithoutTimezone, value);
} else if (value is Map<String, dynamic>) {
return PgTypedParameter(PgDataType.jsonb, value);
} else if (value is PgPoint) {
return PgTypedParameter(PgDataType.point, value);
} else {
throw ArgumentError.value(
value,
'parameter',
name == null ? 'parameter' : '$name parameter',
'Is not a `PgTypedParameter` and appears in a location for which no '
'type could be inferred.',
'type could be inferred.',
);
}
}
Expand All @@ -113,8 +121,7 @@ class InternalQueryDescription implements PgSql {
for (var i = 0; i < params.length; i++) {
final param = params[i];
final knownType = knownTypes != null ? knownTypes[i] : null;

parameters.add(_toParameter(param, knownType));
parameters.add(_toParameter(param, knownType, name: '[$i]'));
}
} else if (params is Map) {
final byName = namedVariables;
Expand All @@ -138,7 +145,7 @@ class InternalQueryDescription implements PgSql {

final value = params[name];
unmatchedVariables.remove(name);
parameters.add(_toParameter(value, type));
parameters.add(_toParameter(value, type, name: name));

variableIndex++;
}
Expand Down
14 changes: 11 additions & 3 deletions test/query_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:postgres/postgres.dart';
import 'package:postgres/src/v2_v3_delegate.dart';
import 'package:test/test.dart';

import 'docker.dart';
Expand Down Expand Up @@ -42,7 +43,10 @@ void main() {

result = await connection.query('select t from t');
expect(result.columnDescriptions, hasLength(1));
expect(result.columnDescriptions.single.tableName, 't');
expect(
result.columnDescriptions.single.tableName,
// v3 does not query the table oids
connection is V3BackedPostgreSQLConnection ? '' : 't');
expect(result.columnDescriptions.single.columnName, 't');
expect(result, [expectedRow]);
});
Expand Down Expand Up @@ -468,8 +472,12 @@ void main() {
substitutionValues: {'i1': '1', 'i2': 1});
expect(true, false);
} on FormatException catch (e) {
expect(e.toString(), contains('Invalid type code'));
expect(e.toString(), contains("'@i1:qwerty"));
expect(
e.toString(),
connection is V3BackedPostgreSQLConnection
? contains('Unknown type')
: contains('Invalid type code'));
expect(e.toString(), contains('qwerty'));
}
});
});
Expand Down

0 comments on commit 5b120c0

Please sign in to comment.