1
1
import NIOCore
2
2
import struct Foundation. UUID
3
3
4
+ // MARK: Protocols
5
+
4
6
/// 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 {
6
8
static var psqlArrayType : PostgresDataType { get }
7
- static var psqlArrayElementType : PostgresDataType { get }
8
9
}
9
10
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 }
13
20
}
14
21
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 }
18
26
}
19
27
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 }
23
32
}
24
33
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 }
28
39
}
29
40
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 }
33
45
}
34
46
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 }
38
51
}
39
52
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
+ }
48
62
}
49
63
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 }
53
68
}
54
69
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 }
58
74
}
59
75
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 }
63
80
}
64
81
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 }
68
86
}
69
87
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 {
72
92
Element . psqlArrayType
73
93
}
74
-
75
- var psqlFormat : PostgresFormat {
94
+
95
+ public static var psqlFormat : PostgresFormat {
76
96
. binary
77
97
}
78
-
79
- func encode< JSONEncoder: PostgresJSONEncoder > (
98
+
99
+ @inlinable
100
+ public func encode< JSONEncoder: PostgresJSONEncoder > (
80
101
into buffer: inout ByteBuffer ,
81
102
context: PostgresEncodingContext < JSONEncoder >
82
103
) throws {
@@ -85,13 +106,13 @@ extension Array: PostgresEncodable where Element: PSQLArrayElement {
85
106
// b
86
107
buffer. writeInteger ( 0 , as: Int32 . self)
87
108
// array element type
88
- buffer. writeInteger ( Element . psqlArrayElementType . rawValue)
109
+ buffer. writeInteger ( Element . psqlType . rawValue)
89
110
90
111
// continue if the array is not empty
91
112
guard !self . isEmpty else {
92
113
return
93
114
}
94
-
115
+
95
116
// length of array
96
117
buffer. writeInteger ( numericCast ( self . count) , as: Int32 . self)
97
118
// dimensions
@@ -103,20 +124,6 @@ extension Array: PostgresEncodable where Element: PSQLArrayElement {
103
124
}
104
125
}
105
126
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 { }
120
127
121
128
extension Array : PostgresDecodable where Element: PostgresArrayDecodable , Element == Element . _DecodableType {
122
129
public init < JSONDecoder: PostgresJSONDecoder > (
0 commit comments