@@ -2,29 +2,62 @@ import NIOCore
22import class Foundation. JSONEncoder
33import class Foundation. JSONDecoder
44
5+ /// A type that can encode itself to a Postgres wire binary representation.
6+ /// Dynamic types are types that don't have a well-known Postgres type OID at compile time.
7+ /// For example, custom types created at runtime, such as enums, or extension types whose OID is not stable between
8+ /// databases.
9+ public protocol PostgresThrowingDynamicTypeEncodable {
10+ /// The data type encoded into the `byteBuffer` in ``encode(into:context:)``
11+ var psqlType : PostgresDataType { get }
12+
13+ /// The Postgres encoding format used to encode the value into `byteBuffer` in ``encode(into:context:)``.
14+ var psqlFormat : PostgresFormat { get }
15+
16+ /// Encode the entity into ``byteBuffer`` in the format specified by ``psqlFormat``,
17+ /// using the provided ``context`` as needed, without setting the byte count.
18+ ///
19+ /// This method is called by ``PostgresBindings``.
20+ func encode< JSONEncoder: PostgresJSONEncoder > (
21+ into byteBuffer: inout ByteBuffer ,
22+ context: PostgresEncodingContext < JSONEncoder >
23+ ) throws
24+ }
25+
26+ /// A type that can encode itself to a Postgres wire binary representation.
27+ /// Dynamic types are types that don't have a well-known Postgres type OID at compile time.
28+ /// For example, custom types created at runtime, such as enums, or extension types whose OID is not stable between
29+ /// databases.
30+ ///
31+ /// This is the non-throwing alternative to ``PostgresThrowingDynamicTypeEncodable``. It allows users
32+ /// to create ``PostgresQuery``s via `ExpressibleByStringInterpolation` without having to spell `try`.
33+ public protocol PostgresDynamicTypeEncodable : PostgresThrowingDynamicTypeEncodable {
34+ /// Encode the entity into ``byteBuffer`` in the format specified by ``psqlFormat``,
35+ /// using the provided ``context`` as needed, without setting the byte count.
36+ ///
37+ /// This method is called by ``PostgresBindings``.
38+ func encode< JSONEncoder: PostgresJSONEncoder > (
39+ into byteBuffer: inout ByteBuffer ,
40+ context: PostgresEncodingContext < JSONEncoder >
41+ )
42+ }
43+
544/// A type that can encode itself to a postgres wire binary representation.
6- public protocol PostgresEncodable {
45+ public protocol PostgresEncodable : PostgresThrowingDynamicTypeEncodable {
746 // TODO: Rename to `PostgresThrowingEncodable` with next major release
847
9- /// identifies the data type that we will encode into `byteBuffer` in `encode`
48+ /// The data type encoded into the `byteBuffer` in `` encode(into:context:)``.
1049 static var psqlType : PostgresDataType { get }
1150
12- /// identifies the postgres format that is used to encode the value into `byteBuffer` in `encode`
51+ /// The Postgres encoding format used to encode the value into `byteBuffer` in `` encode(into:context:)``.
1352 static var psqlFormat : PostgresFormat { get }
14-
15- /// Encode the entity into the `byteBuffer` in Postgres binary format, without setting
16- /// the byte count. This method is called from the ``PostgresBindings``.
17- func encode< JSONEncoder: PostgresJSONEncoder > ( into byteBuffer: inout ByteBuffer , context: PostgresEncodingContext < JSONEncoder > ) throws
1853}
1954
2055/// A type that can encode itself to a postgres wire binary representation. It enforces that the
2156/// ``PostgresEncodable/encode(into:context:)-1jkcp`` does not throw. This allows users
22- /// to create ``PostgresQuery``s using the `ExpressibleByStringInterpolation` without
57+ /// to create ``PostgresQuery``s via `ExpressibleByStringInterpolation` without
2358/// having to spell `try`.
24- public protocol PostgresNonThrowingEncodable : PostgresEncodable {
59+ public protocol PostgresNonThrowingEncodable : PostgresEncodable , PostgresDynamicTypeEncodable {
2560 // TODO: Rename to `PostgresEncodable` with next major release
26-
27- func encode< JSONEncoder: PostgresJSONEncoder > ( into byteBuffer: inout ByteBuffer , context: PostgresEncodingContext < JSONEncoder > )
2861}
2962
3063/// A type that can decode itself from a postgres wire binary representation.
@@ -84,6 +117,14 @@ extension PostgresDecodable {
84117public typealias PostgresCodable = PostgresEncodable & PostgresDecodable
85118
86119extension PostgresEncodable {
120+ @inlinable
121+ public var psqlType : PostgresDataType { Self . psqlType }
122+
123+ @inlinable
124+ public var psqlFormat : PostgresFormat { Self . psqlFormat }
125+ }
126+
127+ extension PostgresThrowingDynamicTypeEncodable {
87128 @inlinable
88129 func encodeRaw< JSONEncoder: PostgresJSONEncoder > (
89130 into buffer: inout ByteBuffer ,
@@ -103,7 +144,7 @@ extension PostgresEncodable {
103144 }
104145}
105146
106- extension PostgresNonThrowingEncodable {
147+ extension PostgresDynamicTypeEncodable {
107148 @inlinable
108149 func encodeRaw< JSONEncoder: PostgresJSONEncoder > (
109150 into buffer: inout ByteBuffer ,
0 commit comments