11import NIOCore
22import struct Foundation. UUID
33
4+ // MARK: Protocols
5+
46/// A type, of which arrays can be encoded into and decoded from a postgres binary format
5- protocol PSQLArrayElement : PostgresCodable {
7+ public protocol PostgresArrayEncodable : PostgresEncodable {
68 static var psqlArrayType : PostgresDataType { get }
7- static var psqlArrayElementType : PostgresDataType { get }
89}
910
10- extension Bool : PSQLArrayElement {
11- static var psqlArrayType : PostgresDataType { . boolArray }
12- static var psqlArrayElementType : PostgresDataType { . bool }
11+ /// A type that can be decoded into a Swift Array of its own type from a Postgres array.
12+ public protocol PostgresArrayDecodable : PostgresDecodable { }
13+
14+ // MARK: Element conformances
15+
16+ extension Bool : PostgresArrayDecodable { }
17+
18+ extension Bool : PostgresArrayEncodable {
19+ public static var psqlArrayType : PostgresDataType { . boolArray }
1320}
1421
15- extension ByteBuffer : PSQLArrayElement {
16- static var psqlArrayType : PostgresDataType { . byteaArray }
17- static var psqlArrayElementType : PostgresDataType { . bytea }
22+ extension ByteBuffer : PostgresArrayDecodable { }
23+
24+ extension ByteBuffer : PostgresArrayEncodable {
25+ public static var psqlArrayType : PostgresDataType { . byteaArray }
1826}
1927
20- extension UInt8 : PSQLArrayElement {
21- static var psqlArrayType : PostgresDataType { . charArray }
22- static var psqlArrayElementType : PostgresDataType { . char }
28+ extension UInt8 : PostgresArrayDecodable { }
29+
30+ extension UInt8 : PostgresArrayEncodable {
31+ public static var psqlArrayType : PostgresDataType { . charArray }
2332}
2433
25- extension Int16 : PSQLArrayElement {
26- static var psqlArrayType : PostgresDataType { . int2Array }
27- static var psqlArrayElementType : PostgresDataType { . int2 }
34+
35+ extension Int16 : PostgresArrayDecodable { }
36+
37+ extension Int16 : PostgresArrayEncodable {
38+ public static var psqlArrayType : PostgresDataType { . int2Array }
2839}
2940
30- extension Int32 : PSQLArrayElement {
31- static var psqlArrayType : PostgresDataType { . int4Array }
32- static var psqlArrayElementType : PostgresDataType { . int4 }
41+ extension Int32 : PostgresArrayDecodable { }
42+
43+ extension Int32 : PostgresArrayEncodable {
44+ public static var psqlArrayType : PostgresDataType { . int4Array }
3345}
3446
35- extension Int64 : PSQLArrayElement {
36- static var psqlArrayType : PostgresDataType { . int8Array }
37- static var psqlArrayElementType : PostgresDataType { . int8 }
47+ extension Int64 : PostgresArrayDecodable { }
48+
49+ extension Int64 : PostgresArrayEncodable {
50+ public static var psqlArrayType : PostgresDataType { . int8Array }
3851}
3952
40- extension Int : PSQLArrayElement {
41- #if (arch(i386) || arch(arm))
42- static var psqlArrayType : PostgresDataType { . int4Array }
43- static var psqlArrayElementType : PostgresDataType { . int4 }
44- #else
45- static var psqlArrayType : PostgresDataType { . int8Array }
46- static var psqlArrayElementType : PostgresDataType { . int8 }
47- #endif
53+ extension Int : PostgresArrayDecodable { }
54+
55+ extension Int : PostgresArrayEncodable {
56+ public static var psqlArrayType : PostgresDataType {
57+ if MemoryLayout< Int> . size == 8 {
58+ return . int8Array
59+ }
60+ return . int4Array
61+ }
4862}
4963
50- extension Float : PSQLArrayElement {
51- static var psqlArrayType : PostgresDataType { . float4Array }
52- static var psqlArrayElementType : PostgresDataType { . float4 }
64+ extension Float : PostgresArrayDecodable { }
65+
66+ extension Float : PostgresArrayEncodable {
67+ public static var psqlArrayType : PostgresDataType { . float4Array }
5368}
5469
55- extension Double : PSQLArrayElement {
56- static var psqlArrayType : PostgresDataType { . float8Array }
57- static var psqlArrayElementType : PostgresDataType { . float8 }
70+ extension Double : PostgresArrayDecodable { }
71+
72+ extension Double : PostgresArrayEncodable {
73+ public static var psqlArrayType : PostgresDataType { . float8Array }
5874}
5975
60- extension String : PSQLArrayElement {
61- static var psqlArrayType : PostgresDataType { . textArray }
62- static var psqlArrayElementType : PostgresDataType { . text }
76+ extension String : PostgresArrayDecodable { }
77+
78+ extension String : PostgresArrayEncodable {
79+ public static var psqlArrayType : PostgresDataType { . textArray }
6380}
6481
65- extension UUID : PSQLArrayElement {
66- static var psqlArrayType : PostgresDataType { . uuidArray }
67- static var psqlArrayElementType : PostgresDataType { . uuid }
82+ extension UUID : PostgresArrayDecodable { }
83+
84+ extension UUID : PostgresArrayEncodable {
85+ public static var psqlArrayType : PostgresDataType { . uuidArray }
6886}
6987
70- extension Array : PostgresEncodable where Element: PSQLArrayElement {
71- var psqlType : PostgresDataType {
88+ // MARK: Array conformances
89+
90+ extension Array : PostgresEncodable where Element: PostgresArrayEncodable {
91+ public static var psqlType : PostgresDataType {
7292 Element . psqlArrayType
7393 }
74-
75- var psqlFormat : PostgresFormat {
94+
95+ public static var psqlFormat : PostgresFormat {
7696 . binary
7797 }
78-
79- func encode< JSONEncoder: PostgresJSONEncoder > (
98+
99+ @inlinable
100+ public func encode< JSONEncoder: PostgresJSONEncoder > (
80101 into buffer: inout ByteBuffer ,
81102 context: PostgresEncodingContext < JSONEncoder >
82103 ) throws {
@@ -85,13 +106,13 @@ extension Array: PostgresEncodable where Element: PSQLArrayElement {
85106 // b
86107 buffer. writeInteger ( 0 , as: Int32 . self)
87108 // array element type
88- buffer. writeInteger ( Element . psqlArrayElementType . rawValue)
109+ buffer. writeInteger ( Element . psqlType . rawValue)
89110
90111 // continue if the array is not empty
91112 guard !self . isEmpty else {
92113 return
93114 }
94-
115+
95116 // length of array
96117 buffer. writeInteger ( numericCast ( self . count) , as: Int32 . self)
97118 // dimensions
@@ -103,20 +124,6 @@ extension Array: PostgresEncodable where Element: PSQLArrayElement {
103124 }
104125}
105126
106- /// A type that can be decoded into a Swift Array of its own type from a Postgres array.
107- public protocol PostgresArrayDecodable : PostgresDecodable { }
108-
109- extension Bool : PostgresArrayDecodable { }
110- extension ByteBuffer : PostgresArrayDecodable { }
111- extension UInt8 : PostgresArrayDecodable { }
112- extension Int16 : PostgresArrayDecodable { }
113- extension Int32 : PostgresArrayDecodable { }
114- extension Int64 : PostgresArrayDecodable { }
115- extension Int : PostgresArrayDecodable { }
116- extension Float : PostgresArrayDecodable { }
117- extension Double : PostgresArrayDecodable { }
118- extension String : PostgresArrayDecodable { }
119- extension UUID : PostgresArrayDecodable { }
120127
121128extension Array : PostgresDecodable where Element: PostgresArrayDecodable , Element == Element . _DecodableType {
122129 public init < JSONDecoder: PostgresJSONDecoder > (
0 commit comments