-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.swift
54 lines (41 loc) · 1.17 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import PostgresWireServer
import Dispatch
class MyPreparedStatement: PreparedStatement {
let sql: String
init(sql: String) throws {
self.sql = sql
}
public var willReturnRows: Bool {
return true
}
public func fields(for parameters: [PQValue]) throws -> [PQField] {
return [PQField(name: "foo", type: .text)]
}
}
class MyResultSet: ResultSet {
let rows = [["bar"]]
var idx = 0
func row() throws -> [PQValue] {
assert(self.hasRow, "should not request next row when has no row")
let v = rows[idx].map { PQValue.text($0) }
idx += 1
return v
}
var hasRow: Bool {
return idx < self.rows.count
}
var error: String? {
return nil
}
}
class MyQueryServer: QueryServer<MyPreparedStatement> {
override public func prepare(_ sql: String, connection: QueryClientConnection<MyPreparedStatement>) throws -> MyPreparedStatement {
return try MyPreparedStatement(sql: sql)
}
public override func query(_ query: MyPreparedStatement, parameters: [PQValue], connection: QueryClientConnection<MyPreparedStatement>, callback: @escaping (ResultSet?) throws -> ()) throws {
try callback(MyResultSet())
}
}
let server = MyQueryServer(port: 6789)
server.run()
dispatchMain()