Skip to content

Commit 40fe0ff

Browse files
committed
Allow DateStyle to be "ISO, MDY", "ISO, DMY", or "ISO, YMD".
See #30.
1 parent 6cefb33 commit 40fe0ff

File tree

3 files changed

+27
-32
lines changed

3 files changed

+27
-32
lines changed

Sources/PostgresClientKit/Parameter.swift

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,72 +31,67 @@ internal struct Parameter {
3131
/// The parameter name.
3232
internal let name: String
3333

34-
/// The parameter value desired by PostgresClientKit.
35-
internal let value: String
34+
/// The parameter values allowed by PostgresClientKit, or `nil` for any value.
35+
internal let allowedValues: [String]?
3636

37-
/// Whether PostgresClientKit sets the value of this parameter in creating a `Connection`.
38-
internal let isSetWhenConnecting: Bool
39-
40-
/// Whether PostgresClientKit checks the value of this parameter when it receives a
41-
/// `ParameterStatusResponse`.
42-
internal let isCheckedUponParameterStatusResponse: Bool
37+
/// The parameter value set by PostgresClientKit in creating a `Connection`, or `nil` to not
38+
/// set a value.
39+
internal let valueSetWhenConnecting: String?
4340

4441
/// The parameters of interest to PostgresClientKit.
4542
internal static let values = [
4643

4744
// PostgresClientKit requires strings received from the Postgres server to be UTF8 format.
4845
Parameter(name: "client_encoding",
49-
value: "UTF8",
50-
isSetWhenConnecting: true,
51-
isCheckedUponParameterStatusResponse: true),
46+
allowedValues: [ "UTF8" ],
47+
valueSetWhenConnecting: "UTF8"),
5248

5349
// PostgresClientKit requires timestamps, dates, and times received from the Postgres server
5450
// to be ISO-8601 format.
5551
Parameter(name: "DateStyle",
56-
value: "ISO, MDY",
57-
isSetWhenConnecting: true,
58-
isCheckedUponParameterStatusResponse: true),
52+
allowedValues: [ "ISO, MDY", "ISO, DMY", "ISO, YMD" ],
53+
valueSetWhenConnecting: "ISO, MDY"),
5954

6055
// PostgresClientKit requires timestamps and times received from the Postgres server to be
6156
// in the UTC/GMT time zone.
6257
Parameter(name: "TimeZone",
63-
value: "GMT",
64-
isSetWhenConnecting: true,
65-
isCheckedUponParameterStatusResponse: true),
66-
58+
allowedValues: [ "GMT" ],
59+
valueSetWhenConnecting: "GMT"),
60+
6761
// PostgresClientKit requires `bytea` values received from the Postgres server to be hex
6862
// encoded.
6963
Parameter(name: "bytea_output",
70-
value: "hex",
71-
isSetWhenConnecting: true,
72-
isCheckedUponParameterStatusResponse: true),
64+
allowedValues: [ "hex" ],
65+
valueSetWhenConnecting: "hex"),
7366
]
7467

75-
/// Checks whether the parameter in the specified `ParameterStatusResponse` is required by
76-
/// PostgresClientKit to have a certain value and, if so, whether it has that value.
68+
/// Checks whether the parameter in the specified `ParameterStatusResponse` is constrained by
69+
/// PostgresClientKit to certain values and, if so, whether that constraint is satisfied.
7770
///
7871
/// - Parameter response: the response to check
79-
/// - Throws: `PostgresError.invalidParameterValue` if the parameter does not have the required
72+
/// - Throws: `PostgresError.invalidParameterValue` if the parameter does not have an allowed
8073
/// value
8174
internal static func checkParameterStatusResponse(_ response: ParameterStatusResponse,
8275
connection: Connection) throws {
8376

8477
if let parameter = values.first(where: {
8578
$0.name == response.name
86-
&& $0.isCheckedUponParameterStatusResponse
87-
&& $0.value != response.value } ) {
79+
&& $0.allowedValues != nil
80+
&& !$0.allowedValues!.contains(response.value) } ) {
81+
82+
let allowedValues = parameter.allowedValues!
8883

8984
connection.log(
9085
.warning,
91-
"Invalid value for Postgres parameter (response.name): " +
92-
"\(response.value) (must be \(parameter.value)); closing connection")
86+
"Invalid value for Postgres parameter \(response.name): " +
87+
"\(response.value) (allowedValues \(allowedValues)); closing connection")
9388

9489
// The invalid parameter change already ocurred. This connection is toast.
9590
connection.close()
9691

9792
throw PostgresError.invalidParameterValue(name: response.name,
9893
value: response.value,
99-
requiredValue: parameter.value)
94+
allowedValues: allowedValues)
10095
}
10196
}
10297
}

Sources/PostgresClientKit/PostgresError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public enum PostgresError: Error {
3333
case cursorClosed
3434

3535
/// The Postgres server has a parameter set to a value incompatible with PostgresClientKit.
36-
case invalidParameterValue(name: String, value: String, requiredValue: String)
36+
case invalidParameterValue(name: String, value: String, allowedValues: [String])
3737

3838
/// The specified username does not meet the SCRAM-SHA-256 requirements for a username.
3939
case invalidUsernameString

Sources/PostgresClientKit/Request/StartupRequest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ internal class StartupRequest: Request {
5454
body.append(applicationName.dataZero)
5555

5656
for parameter in Parameter.values {
57-
if parameter.isSetWhenConnecting {
57+
if let value = parameter.valueSetWhenConnecting {
5858
body.append(parameter.name.dataZero)
59-
body.append(parameter.value.dataZero)
59+
body.append(value.dataZero)
6060
}
6161
}
6262

0 commit comments

Comments
 (0)