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
64 changes: 53 additions & 11 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,61 @@
# The commented part below is just for inspiration. Read the guide here:
# https://github.com/dart-lang/sdk/tree/master/pkg/analyzer#configuring-the-analyzer

include: package:pedantic/analysis_options.yaml

analyzer:
# strong-mode:
# implicit-casts: false
# excludes:
# - path/to/excluded/files/**
errors:
override_on_non_overriding_method: error
unused_element: error
unused_import: error
unused_local_variable: error
dead_code: error
strong-mode:
implicit-casts: false

linter:
rules:
# see catalogue here: http://dart-lang.github.io/linter/lints/
- camel_case_types
- hash_and_equals
- iterable_contains_unrelated_type
- list_remove_unrelated_type
- unawaited_futures
- unrelated_type_equality_checks
- valid_regexps
- annotate_overrides
- avoid_unused_constructor_parameters
- await_only_futures
- camel_case_types
- cancel_subscriptions
- directives_ordering
# - empty_catches
- empty_statements
- hash_and_equals
- iterable_contains_unrelated_type
- list_remove_unrelated_type
- no_adjacent_strings_in_list
- no_duplicate_case_values
- non_constant_identifier_names
- only_throw_errors
- overridden_fields
- prefer_collection_literals
- prefer_conditional_assignment
- prefer_contains
- prefer_final_fields
- prefer_final_locals
- prefer_initializing_formals
- prefer_interpolation_to_compose_strings
- prefer_is_empty
- prefer_is_not_empty
- prefer_single_quotes
- prefer_typing_uninitialized_variables
- recursive_getters
- slash_for_doc_comments
- test_types_in_equals
- throw_in_finally
- type_init_formals
- unawaited_futures
- unnecessary_brace_in_string_interps
- unnecessary_getters_setters
- unnecessary_lambdas
- unnecessary_new
- unnecessary_null_aware_assignments
- unnecessary_statements
- unnecessary_this
- unrelated_type_equality_checks
- use_rethrow_when_possible
- valid_regexps
4 changes: 2 additions & 2 deletions lib/postgres.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library postgres;

export 'src/connection.dart';
export 'src/types.dart';
export 'src/substituter.dart';
export 'src/execution_context.dart';
export 'src/substituter.dart';
export 'src/types.dart';
181 changes: 85 additions & 96 deletions lib/src/binary_codec.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import 'dart:convert';

import 'dart:typed_data';

import 'package:postgres/postgres.dart';
import 'package:postgres/src/types.dart';
import 'package:buffer/buffer.dart';

import '../postgres.dart';
import 'types.dart';

class PostgresBinaryEncoder extends Converter<dynamic, Uint8List> {
const PostgresBinaryEncoder(this.dataType);
Expand All @@ -19,125 +20,114 @@ class PostgresBinaryEncoder extends Converter<dynamic, Uint8List> {
switch (dataType) {
case PostgreSQLDataType.boolean:
{
if (value is! bool) {
throw new FormatException(
"Invalid type for parameter value. Expected: bool Got: ${value.runtimeType}");
if (value is bool) {
final bd = ByteData(1);
bd.setUint8(0, value ? 1 : 0);
return bd.buffer.asUint8List();
}

var bd = new ByteData(1);
bd.setUint8(0, value ? 1 : 0);
return bd.buffer.asUint8List();
throw FormatException(
'Invalid type for parameter value. Expected: bool Got: ${value.runtimeType}');
}
case PostgreSQLDataType.bigSerial:
case PostgreSQLDataType.bigInteger:
{
if (value is! int) {
throw new FormatException(
"Invalid type for parameter value. Expected: int Got: ${value.runtimeType}");
if (value is int) {
final bd = ByteData(8);
bd.setInt64(0, value);
return bd.buffer.asUint8List();
}

var bd = new ByteData(8);
bd.setInt64(0, value);
return bd.buffer.asUint8List();
throw FormatException(
'Invalid type for parameter value. Expected: int Got: ${value.runtimeType}');
}
case PostgreSQLDataType.serial:
case PostgreSQLDataType.integer:
{
if (value is! int) {
throw new FormatException(
"Invalid type for parameter value. Expected: int Got: ${value.runtimeType}");
if (value is int) {
final bd = ByteData(4);
bd.setInt32(0, value);
return bd.buffer.asUint8List();
}

var bd = new ByteData(4);
bd.setInt32(0, value);
return bd.buffer.asUint8List();
throw FormatException(
'Invalid type for parameter value. Expected: int Got: ${value.runtimeType}');
}
case PostgreSQLDataType.smallInteger:
{
if (value is! int) {
throw new FormatException(
"Invalid type for parameter value. Expected: int Got: ${value.runtimeType}");
if (value is int) {
final bd = ByteData(2);
bd.setInt16(0, value);
return bd.buffer.asUint8List();
}

var bd = new ByteData(2);
bd.setInt16(0, value);
return bd.buffer.asUint8List();
throw FormatException(
'Invalid type for parameter value. Expected: int Got: ${value.runtimeType}');
}
case PostgreSQLDataType.name:
case PostgreSQLDataType.text:
{
if (value is! String) {
throw new FormatException(
"Invalid type for parameter value. Expected: String Got: ${value.runtimeType}");
if (value is String) {
return castBytes(utf8.encode(value));
}

return utf8.encode(value);
throw FormatException(
'Invalid type for parameter value. Expected: String Got: ${value.runtimeType}');
}
case PostgreSQLDataType.real:
{
if (value is! double) {
throw new FormatException(
"Invalid type for parameter value. Expected: double Got: ${value.runtimeType}");
if (value is double) {
final bd = ByteData(4);
bd.setFloat32(0, value);
return bd.buffer.asUint8List();
}

var bd = new ByteData(4);
bd.setFloat32(0, value);
return bd.buffer.asUint8List();
throw FormatException(
'Invalid type for parameter value. Expected: double Got: ${value.runtimeType}');
}
case PostgreSQLDataType.double:
{
if (value is! double) {
throw new FormatException(
"Invalid type for parameter value. Expected: double Got: ${value.runtimeType}");
if (value is double) {
final bd = ByteData(8);
bd.setFloat64(0, value);
return bd.buffer.asUint8List();
}

var bd = new ByteData(8);
bd.setFloat64(0, value);
return bd.buffer.asUint8List();
throw FormatException(
'Invalid type for parameter value. Expected: double Got: ${value.runtimeType}');
}
case PostgreSQLDataType.date:
{
if (value is! DateTime) {
throw new FormatException(
"Invalid type for parameter value. Expected: DateTime Got: ${value.runtimeType}");
if (value is DateTime) {
final bd = ByteData(4);
bd.setInt32(0, value.toUtc().difference(DateTime.utc(2000)).inDays);
return bd.buffer.asUint8List();
}

var bd = new ByteData(4);
bd.setInt32(
0, value.toUtc().difference(new DateTime.utc(2000)).inDays);
return bd.buffer.asUint8List();
throw FormatException(
'Invalid type for parameter value. Expected: DateTime Got: ${value.runtimeType}');
}

case PostgreSQLDataType.timestampWithoutTimezone:
{
if (value is! DateTime) {
throw new FormatException(
"Invalid type for parameter value. Expected: DateTime Got: ${value.runtimeType}");
if (value is DateTime) {
final bd = ByteData(8);
final diff = value.toUtc().difference(DateTime.utc(2000));
bd.setInt64(0, diff.inMicroseconds);
return bd.buffer.asUint8List();
}

var bd = new ByteData(8);
var diff = value.toUtc().difference(new DateTime.utc(2000));
bd.setInt64(0, diff.inMicroseconds);
return bd.buffer.asUint8List();
throw FormatException(
'Invalid type for parameter value. Expected: DateTime Got: ${value.runtimeType}');
}

case PostgreSQLDataType.timestampWithTimezone:
{
if (value is! DateTime) {
throw new FormatException(
"Invalid type for parameter value. Expected: DateTime Got: ${value.runtimeType}");
if (value is DateTime) {
final bd = ByteData(8);
bd.setInt64(
0, value.toUtc().difference(DateTime.utc(2000)).inMicroseconds);
return bd.buffer.asUint8List();
}

var bd = new ByteData(8);
bd.setInt64(0,
value.toUtc().difference(new DateTime.utc(2000)).inMicroseconds);
return bd.buffer.asUint8List();
throw FormatException(
'Invalid type for parameter value. Expected: DateTime Got: ${value.runtimeType}');
}

case PostgreSQLDataType.json:
{
var jsonBytes = utf8.encode(json.encode(value));
final outBuffer = new Uint8List(jsonBytes.length + 1);
final jsonBytes = utf8.encode(json.encode(value));
final outBuffer = Uint8List(jsonBytes.length + 1);
outBuffer[0] = 1;
for (var i = 0; i < jsonBytes.length; i++) {
outBuffer[i + 1] = jsonBytes[i];
Expand All @@ -148,28 +138,28 @@ class PostgresBinaryEncoder extends Converter<dynamic, Uint8List> {

case PostgreSQLDataType.byteArray:
{
if (value is! List) {
throw new FormatException(
"Invalid type for parameter value. Expected: List<int> Got: ${value.runtimeType}");
if (value is List<int>) {
return castBytes(value);
}
return new Uint8List.fromList(value);
throw FormatException(
'Invalid type for parameter value. Expected: List<int> Got: ${value.runtimeType}');
}

case PostgreSQLDataType.uuid:
{
if (value is! String) {
throw new FormatException(
"Invalid type for parameter value. Expected: String Got: ${value.runtimeType}");
throw FormatException(
'Invalid type for parameter value. Expected: String Got: ${value.runtimeType}');
}

final dashUnit = "-".codeUnits.first;
final dashUnit = '-'.codeUnits.first;
final hexBytes = (value as String)
.toLowerCase()
.codeUnits
.where((c) => c != dashUnit)
.toList();
if (hexBytes.length != 32) {
throw new FormatException(
throw FormatException(
"Invalid UUID string. There must be exactly 32 hexadecimal (0-9 and a-f) characters and any number of '-' characters.");
}

Expand All @@ -180,11 +170,11 @@ class PostgresBinaryEncoder extends Converter<dynamic, Uint8List> {
return charCode - 87;
}

throw new FormatException(
"Invalid UUID string. Contains non-hexadecimal character (0-9 and a-f).");
throw FormatException(
'Invalid UUID string. Contains non-hexadecimal character (0-9 and a-f).');
};

final outBuffer = new Uint8List(16);
final outBuffer = Uint8List(16);
for (var i = 0; i < hexBytes.length; i += 2) {
final upperByte = byteConvert(hexBytes[i]);
final lowerByte = byteConvert(hexBytes[i + 1]);
Expand All @@ -195,7 +185,7 @@ class PostgresBinaryEncoder extends Converter<dynamic, Uint8List> {
}
}

throw new PostgreSQLException("Unsupported datatype");
throw PostgreSQLException('Unsupported datatype');
}
}

Expand All @@ -212,8 +202,8 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
return null;
}

final buffer = new ByteData.view(
value.buffer, value.offsetInBytes, value.lengthInBytes);
final buffer =
ByteData.view(value.buffer, value.offsetInBytes, value.lengthInBytes);

switch (dataType) {
case PostgreSQLDataType.name:
Expand All @@ -236,12 +226,11 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
return buffer.getFloat64(0);
case PostgreSQLDataType.timestampWithoutTimezone:
case PostgreSQLDataType.timestampWithTimezone:
return new DateTime.utc(2000)
.add(new Duration(microseconds: buffer.getInt64(0)));
return DateTime.utc(2000)
.add(Duration(microseconds: buffer.getInt64(0)));

case PostgreSQLDataType.date:
return new DateTime.utc(2000)
.add(new Duration(days: buffer.getInt32(0)));
return DateTime.utc(2000).add(Duration(days: buffer.getInt32(0)));

case PostgreSQLDataType.json:
{
Expand All @@ -257,7 +246,7 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {

case PostgreSQLDataType.uuid:
{
final codeDash = "-".codeUnitAt(0);
final codeDash = '-'.codeUnitAt(0);

final cipher = [
'0',
Expand All @@ -281,7 +270,7 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
return cipher[value];
};

final buf = new StringBuffer();
final buf = StringBuffer();
for (var i = 0; i < buffer.lengthInBytes; i++) {
final byteValue = buffer.getUint8(i);
final upperByteValue = byteValue ~/ 16;
Expand Down
Loading