Skip to content

Commit 4c008b8

Browse files
authored
add bool support (vapor#33)
* add bool support * update readme
1 parent 7a6536f commit 4c008b8

File tree

6 files changed

+89
-2
lines changed

6 files changed

+89
-2
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ print(data.uint16) // UInt16?
196196
print(data.uint32) // UInt32?
197197
print(data.uint64) // UInt64?
198198

199+
print(data.bool) // Bool?
200+
199201
print(data.float) // Float?
200202
print(data.double) // Double?
201203

@@ -205,4 +207,4 @@ print(data.uuid) // UUID?
205207
print(data.numeric) // PostgresNumeric?
206208
```
207209

208-
`PostgresData` is also used for sending data _to_ the server via parameterized values. To create `PostgresData` from a Swift type, use the available intializer methods.
210+
`PostgresData` is also used for sending data _to_ the server via parameterized values. To create `PostgresData` from a Swift type, use the available intializer methods.

Sources/CMD5/cmd5.c renamed to Sources/CMD5/CMD5.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
#include <string.h>
3939

40-
#include "cmd5.h"
40+
#include "CMD5.h"
4141

4242
/*
4343
* The basic MD5 functions.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
extension PostgresData {
2+
public init(bool: Bool) {
3+
var buffer = ByteBufferAllocator().buffer(capacity: 1)
4+
buffer.writeInteger(bool ? 1 : 0, as: UInt8.self)
5+
self.init(type: .bool, formatCode: .binary, value: buffer)
6+
}
7+
8+
public var bool: Bool? {
9+
guard var value = self.value else {
10+
return nil
11+
}
12+
guard value.readableBytes == 1 else {
13+
return nil
14+
}
15+
guard let byte = value.readInteger(as: UInt8.self) else {
16+
return nil
17+
}
18+
19+
switch self.formatCode {
20+
case .text:
21+
switch byte {
22+
case Character("t").asciiValue!:
23+
return true
24+
case Character("f").asciiValue!:
25+
return false
26+
default:
27+
return nil
28+
}
29+
case .binary:
30+
switch byte {
31+
case 1:
32+
return true
33+
case 0:
34+
return false
35+
default:
36+
return nil
37+
}
38+
}
39+
}
40+
}
41+
42+
extension PostgresData: ExpressibleByBooleanLiteral {
43+
public init(booleanLiteral value: Bool) {
44+
self.init(bool: value)
45+
}
46+
}
47+
48+
extension Bool: PostgresDataConvertible {
49+
public static var postgresDataType: PostgresDataType {
50+
return .bool
51+
}
52+
53+
public var postgresData: PostgresData? {
54+
return .init(bool: self)
55+
}
56+
57+
public init?(postgresData: PostgresData) {
58+
guard let bool = postgresData.bool else {
59+
return nil
60+
}
61+
self = bool
62+
}
63+
}

Tests/PostgresNIOTests/NIOPostgresTests.swift

+21
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,27 @@ final class NIOPostgresTests: XCTestCase {
366366
XCTAssertEqual(rows[0].column("array")?.array(of: Int.self), [])
367367
}
368368

369+
func testBoolSerialize() throws {
370+
let conn = try PostgresConnection.test(on: eventLoop).wait()
371+
defer { try! conn.close().wait() }
372+
do {
373+
let rows = try conn.query("select $1::bool as bool", [true]).wait()
374+
XCTAssertEqual(rows[0].column("bool")?.bool, true)
375+
}
376+
do {
377+
let rows = try conn.query("select $1::bool as bool", [false]).wait()
378+
XCTAssertEqual(rows[0].column("bool")?.bool, false)
379+
}
380+
do {
381+
let rows = try conn.simpleQuery("select true::bool as bool").wait()
382+
XCTAssertEqual(rows[0].column("bool")?.bool, true)
383+
}
384+
do {
385+
let rows = try conn.simpleQuery("select false::bool as bool").wait()
386+
XCTAssertEqual(rows[0].column("bool")?.bool, false)
387+
}
388+
}
389+
369390
func testRemoteTLSServer() throws {
370391
let url = "postgres://uymgphwj:7_tHbREdRwkqAdu4KoIS7hQnNxr8J1LA@elmer.db.elephantsql.com:5432/uymgphwj"
371392
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)

Tests/PostgresNIOTests/XCTestManifests.swift

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extension NIOPostgresTests {
88
static let __allTests__NIOPostgresTests = [
99
("testAverageLengthNumeric", testAverageLengthNumeric),
1010
("testBindInteger", testBindInteger),
11+
("testBoolSerialize", testBoolSerialize),
1112
("testColumnsInJoin", testColumnsInJoin),
1213
("testConnectAndClose", testConnectAndClose),
1314
("testDates", testDates),

0 commit comments

Comments
 (0)