forked from PerfectlySoft/Perfect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQL.swift
257 lines (208 loc) · 6.13 KB
/
PostgreSQL.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//
// PostgreSQL.swift
// PostgreSQL
//
// Created by Kyle Jessup on 2015-07-29.
// Copyright (C) 2015 PerfectlySoft, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version, as supplemented by the
// Perfect Additional Terms.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License, as supplemented by the
// Perfect Additional Terms, for more details.
//
// You should have received a copy of the GNU Affero General Public License
// and the Perfect Additional Terms that immediately follow the terms and
// conditions of the GNU Affero General Public License along with this
// program. If not, see <http://www.perfect.org/AGPL_3_0_With_Perfect_Additional_Terms.txt>.
//
import libpq
public final class PGResult {
public enum StatusType {
case EmptyQuery
case CommandOK
case TuplesOK
case BadResponse
case NonFatalError
case FatalError
case SingleTuple
case Unknown
}
var res: COpaquePointer
init(_ res: COpaquePointer) {
self.res = res
}
deinit {
self.close()
}
public func close() {
self.clear()
}
public func clear() {
if self.res != nil {
PQclear(self.res)
self.res = COpaquePointer()
}
}
public func statusInt() -> Int {
let s = PQresultStatus(self.res)
return Int(s.rawValue)
}
public func status() -> StatusType {
let s = PQresultStatus(self.res)
switch(s.rawValue) {
case PGRES_EMPTY_QUERY.rawValue:
return .EmptyQuery
case PGRES_COMMAND_OK.rawValue:
return .CommandOK
case PGRES_TUPLES_OK.rawValue:
return .TuplesOK
case PGRES_BAD_RESPONSE.rawValue:
return .BadResponse
case PGRES_NONFATAL_ERROR.rawValue:
return .NonFatalError
case PGRES_FATAL_ERROR.rawValue:
return .FatalError
case PGRES_SINGLE_TUPLE.rawValue:
return .SingleTuple
default:
print("Unhandled PQresult status type \(s.rawValue)")
}
return .Unknown
}
public func errorMessage() -> String {
return String.fromCString(PQresultErrorMessage(self.res)) ?? ""
}
public func numFields() -> Int {
return Int(PQnfields(self.res))
}
public func fieldName(index: Int) -> String? {
let fn = PQfname(self.res, Int32(index))
if fn != nil {
return String.fromCString(fn) ?? ""
}
return nil
}
public func fieldType(index: Int) -> Oid? {
let fn = PQftype(self.res, Int32(index))
return fn
}
public func numTuples() -> Int {
return Int(PQntuples(self.res))
}
public func fieldIsNull(tupleIndex: Int, fieldIndex: Int) -> Bool {
return 1 == PQgetisnull(self.res, Int32(tupleIndex), Int32(fieldIndex))
}
public func getFieldString(tupleIndex: Int, fieldIndex: Int) -> String {
let v = PQgetvalue(self.res, Int32(tupleIndex), Int32(fieldIndex))
return String.fromCString(v) ?? ""
}
public func getFieldInt(tupleIndex: Int, fieldIndex: Int) -> Int {
let s = getFieldString(tupleIndex, fieldIndex: fieldIndex)
return Int(s) ?? 0
}
public func getFieldBool(tupleIndex: Int, fieldIndex: Int) -> Bool {
let s = getFieldString(tupleIndex, fieldIndex: fieldIndex)
return s == "t"
}
public func getFieldInt8(tupleIndex: Int, fieldIndex: Int) -> Int8 {
let s = getFieldString(tupleIndex, fieldIndex: fieldIndex)
return Int8(s) ?? 0
}
public func getFieldInt16(tupleIndex: Int, fieldIndex: Int) -> Int16 {
let s = getFieldString(tupleIndex, fieldIndex: fieldIndex)
return Int16(s) ?? 0
}
public func getFieldInt32(tupleIndex: Int, fieldIndex: Int) -> Int32 {
let s = getFieldString(tupleIndex, fieldIndex: fieldIndex)
return Int32(s) ?? 0
}
public func getFieldInt64(tupleIndex: Int, fieldIndex: Int) -> Int64 {
let s = getFieldString(tupleIndex, fieldIndex: fieldIndex)
return Int64(s) ?? 0
}
public func getFieldDouble(tupleIndex: Int, fieldIndex: Int) -> Double {
let s = getFieldString(tupleIndex, fieldIndex: fieldIndex)
return Double(s) ?? 0
}
public func getFieldFloat(tupleIndex: Int, fieldIndex: Int) -> Float {
let s = getFieldString(tupleIndex, fieldIndex: fieldIndex)
return Float(s) ?? 0
}
public func getFieldBlob(tupleIndex: Int, fieldIndex: Int) -> [Int8] {
let v = PQgetvalue(self.res, Int32(tupleIndex), Int32(fieldIndex))
let length = Int(PQgetlength(self.res, Int32(tupleIndex), Int32(fieldIndex)))
let ip = UnsafePointer<Int8>(v)
var ret = [Int8]()
for idx in 0..<length {
ret.append(ip[idx])
}
return ret
}
}
public final class PGConnection {
public enum StatusType {
case OK
case Bad
}
var conn = COpaquePointer()
var connectInfo: String = ""
public init() {
}
deinit {
self.close()
}
public func connectdb(info: String) -> StatusType {
self.conn = PQconnectdb(info)
self.connectInfo = info
return self.status()
}
public func close() {
self.finish()
}
public func finish() {
if self.conn != nil {
PQfinish(self.conn)
self.conn = COpaquePointer()
}
}
public func status() -> StatusType {
let status = PQstatus(self.conn)
return status == CONNECTION_OK ? .OK : .Bad
}
public func errorMessage() -> String {
return String.fromCString(PQerrorMessage(self.conn)) ?? ""
}
public func exec(statement: String) -> PGResult {
return PGResult(PQexec(self.conn, statement))
}
// !FIX! does not handle binary data
public func exec(statement: String, params: [String]) -> PGResult {
var asStrings = [String]()
for item in params {
asStrings.append(String(item))
}
let count = asStrings.count
let values = UnsafeMutablePointer<UnsafePointer<Int8>>.alloc(count)
defer {
values.destroy() ; values.dealloc(count)
}
var temps = [Array<UInt8>]()
for idx in 0..<count {
let s = asStrings[idx]
let utf8 = s.utf8
var aa = Array<UInt8>(utf8)
aa.append(0)
temps.append(aa)
values[idx] = UnsafePointer<Int8>(temps.last!)
}
let r = PQexecParams(self.conn, statement, Int32(count), nil, values, nil, nil, Int32(0))
return PGResult(r)
}
}