Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add builtin time, geometric and most range types #294

Merged
merged 8 commits into from
Feb 17, 2024
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 3.0.9

Added the following PostgreSQL builtin types:
- Geometric types `line`, `lseg`,`path`,`polygon`,`box`,`circle`
- Range types `int4range`, `int8range`, `daterange`, `tsrange`,`tstzrange`
- Time type `time`

## 3.0.8

- Properly react to connection losses by reporting the database connection as
Expand Down
6 changes: 6 additions & 0 deletions lib/postgres.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export 'src/exceptions.dart';
export 'src/pool/pool_api.dart';
export 'src/replication.dart';
export 'src/types.dart';
export 'src/types/geo_types.dart';
export 'src/types/range_types.dart';
export 'src/types/type_registry.dart' show TypeRegistry;

/// A description of a SQL query as interpreted by this package.
Expand Down Expand Up @@ -227,6 +229,7 @@ abstract class ResultStream implements Stream<ResultRow> {
abstract class ResultStreamSubscription
implements StreamSubscription<ResultRow> {
Future<int> get affectedRows;

Future<ResultSchema> get schema;
}

Expand Down Expand Up @@ -335,7 +338,9 @@ abstract class Channels {
Stream<Notification> get all;

Stream<String> operator [](String channel);

Future<void> notify(String channel, [String? payload]);

Future<void> cancelAll();
}

Expand Down Expand Up @@ -415,6 +420,7 @@ enum SslMode {
;

bool get ignoreCertificateIssues => this == SslMode.require;

bool get allowCleartextPassword => this == SslMode.disable;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/messages/server_messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ class UnknownMessage extends ServerMessage {
}

@override
bool operator ==(dynamic other) {
bool operator ==(Object other) {
if (other is! UnknownMessage) {
return false;
}
Expand Down
129 changes: 116 additions & 13 deletions lib/src/types.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'dart:convert';
import 'dart:core';
import 'dart:core' as core;
import 'dart:core';
import 'dart:typed_data';

import 'package:meta/meta.dart';

import 'types/generic_type.dart';
import 'types/geo_types.dart';
import 'types/range_types.dart';
import 'types/type_registry.dart';

/// In Postgresql `interval` values are stored as [months], [days], and [microseconds].
Expand Down Expand Up @@ -119,27 +121,84 @@ class LSN {
int get hashCode => value.hashCode;
}

/// Describes PostgreSQL's geometric type: `point`.
@immutable
class Point {
/// also referred as `x`
final double latitude;
/// Describes PostgreSQL's `time without time zone` type.
///
/// See https://www.postgresql.org/docs/current/datatype-datetime.html
///
/// `time with time zone` is not implemented as Postgres wiki recommends against its use:
///
/// https://wiki.postgresql.org/wiki/Don't_Do_This#Don.27t_use_timetz
class Time {
/// The time in microseconds
late final int microseconds;

/// Construct a [Time] instance from [microseconds].
///
/// [microseconds] must be positive and not resolve to a time larger than 24:00:00.000000.
Time.fromMicroseconds(this.microseconds) {
_check();
}

/// Construct a [Time] instance.
///
/// [Time] value must be positive and not larger than 24:00:00.000000.
Time(
[int hour = 0,
int minute = 0,
int second = 0,
int millisecond = 0,
int microsecond = 0]) {
microseconds = hour * Duration.microsecondsPerHour +
minute * Duration.microsecondsPerMinute +
second * Duration.microsecondsPerSecond +
millisecond * Duration.microsecondsPerMillisecond +
microsecond;
_check();
}

_check() {
if (microseconds > Duration.microsecondsPerDay) {
throw ArgumentError(
'Time: values greater than 24:00:00.000000 are not allowed');
}
if (microseconds < 0) {
throw ArgumentError('Time: negative value not allowed');
}
}

DateTime get utcDateTime =>
DateTime.fromMicrosecondsSinceEpoch(microseconds, isUtc: true);

/// The hour of the day, expressed as in a 25-hour clock `0...24`.
int get hour =>
microseconds == Duration.microsecondsPerDay ? 24 : utcDateTime.hour;

/// also referred as `y`
final double longitude;
/// The minute `[0...59]`.
int get minute => utcDateTime.minute;

const Point(this.latitude, this.longitude);
/// The second `[0...59]`.
int get second => utcDateTime.second;

/// The millisecond `[0...999]`.
int get millisecond => utcDateTime.millisecond;

/// The microsecond `[0...999]`.
int get microsecond => utcDateTime.microsecond;

@override
String toString() => microseconds == Duration.microsecondsPerDay
? 'Time(24:00:00.000)'
: 'Time(${utcDateTime.toIso8601String().split('T')[1].replaceAll('Z', '')})';

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Point &&
other is Time &&
runtimeType == other.runtimeType &&
latitude == other.latitude &&
longitude == other.longitude;
microseconds == other.microseconds;

@override
int get hashCode => Object.hash(latitude, longitude);
int get hashCode => microseconds;
}

/// Supported data types.
Expand Down Expand Up @@ -177,6 +236,9 @@ abstract class Type<T extends Object> {
/// Must be a [bool]
static const boolean = GenericType<bool>(TypeOid.boolean);

/// Must be a [Time]
static const time = GenericType<Time>(TypeOid.time);

/// Must be a [DateTime] (microsecond date and time precision)
static const timestampWithoutTimezone =
GenericType<DateTime>(TypeOid.timestampWithoutTimezone);
Expand Down Expand Up @@ -227,6 +289,24 @@ abstract class Type<T extends Object> {
/// Must be a [Point]
static const point = GenericType<Point>(TypeOid.point);

/// Must be a [Line]
static const line = GenericType<Line>(TypeOid.line);

/// Must be a [LineSegment]
static const lineSegment = GenericType<LineSegment>(TypeOid.lineSegment);

/// Must be a [Box]
static const box = GenericType<Box>(TypeOid.box);

/// Must be a [Polygon]
static const polygon = GenericType<Polygon>(TypeOid.polygon);

/// Must be a [Path]
static const path = GenericType<Path>(TypeOid.path);

/// Must be a [Circle]
static const circle = GenericType<Circle>(TypeOid.circle);

/// Must be a [List<bool>]
static const booleanArray = GenericType<List<bool>>(TypeOid.booleanArray);

Expand Down Expand Up @@ -259,6 +339,29 @@ abstract class Type<T extends Object> {
/// Impossible to bind to, always null when read.
static const voidType = GenericType<Object>(TypeOid.voidType);

/// Must be a [IntRange]
static const int4range = GenericType<IntRange>(TypeOid.int4range);

/// Must be a [IntRange]
static const int8range = GenericType<IntRange>(TypeOid.int8range);

/// Must be a [DateRange]
static const dateRange = GenericType<DateRange>(TypeOid.dateRange);

/// Must be a [Range<Object>]
///
/// Supported element types are the same as for [Type.numeric].
// static const numrange =
// GenericType<ContinuousRange<String>>(TypeOid.numrange);

/// Must be a [Range<DateTime>]
static const timestampRange =
GenericType<DateTimeRange>(TypeOid.timestampRange);

/// Must be a [Range<DateTime>]
static const timestampTzRange =
GenericType<DateTimeRange>(TypeOid.timestampTzRange);

/// The object ID of this data type.
final int? oid;

Expand Down
Loading
Loading