-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLibsql.swift
432 lines (357 loc) · 12 KB
/
Libsql.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import CLibsql
import Foundation
public enum Value {
case integer(Int64)
case text(String)
case blob(Data)
case real(Double)
case null
}
public protocol ValueRepresentable {
func toValue() -> Value
}
extension Value: ValueRepresentable {
public func toValue() -> Value { self }
}
extension Int: ValueRepresentable {
public func toValue() -> Value { .integer(Int64(self)) }
}
extension Int64: ValueRepresentable {
public func toValue() -> Value { .integer(self) }
}
extension String: ValueRepresentable {
public func toValue() -> Value { .text(self) }
}
extension Data: ValueRepresentable {
public func toValue() -> Value { .blob(self) }
}
extension Double: ValueRepresentable {
public func toValue() -> Value { .real(self) }
}
public protocol Prepareable {
func prepare(_ sql: String) throws -> Statement
}
extension Prepareable {
func execute(_ sql: String) throws -> Int {
return try self.prepare(sql).execute()
}
func execute(_ sql: String, _ params: [String: ValueRepresentable]) throws -> Int {
return try self.prepare(sql).bind(params).execute()
}
func execute(_ sql: String, _ params: [ValueRepresentable]) throws -> Int {
return try self.prepare(sql).bind(params).execute()
}
func query(_ sql: String) throws -> Rows {
return try self.prepare(sql).query()
}
func query(_ sql: String, _ params: [String: ValueRepresentable]) throws -> Rows {
return try self.prepare(sql).bind(params).query()
}
func query(_ sql: String, _ params: [ValueRepresentable]) throws -> Rows {
return try self.prepare(sql).bind(params).query()
}
}
extension String? {
func withCString<Result>(_ body: (UnsafePointer<Int8>?) throws -> Result) rethrows -> Result {
if self == nil {
return try body(nil)
} else {
return try self!.withCString(body)
}
}
}
func errIf(_ err: OpaquePointer!) throws {
if (err != nil) {
defer { libsql_error_deinit(err) }
throw LibsqlError.runtimeError(String(cString: libsql_error_message(err)!))
}
}
enum LibsqlError: Error {
case runtimeError(String)
case typeMismatch
}
public class Row {
var inner: libsql_row_t
fileprivate init?(from inner: libsql_row_t?) {
guard let inner = inner else {
return nil
}
self.inner = inner
}
public func get(_ index: Int32) throws -> Value {
let result = libsql_row_value(self.inner, index)
try errIf(result.err)
switch result.ok.type {
case LIBSQL_TYPE_BLOB:
let slice = result.ok.value.blob
defer { libsql_slice_deinit(slice) }
return .blob(Data(bytes: slice.ptr, count: Int(slice.len)))
case LIBSQL_TYPE_TEXT:
let slice = result.ok.value.text
defer { libsql_slice_deinit(slice) }
return .text(String(cString: slice.ptr.assumingMemoryBound(to: UInt8.self)))
case LIBSQL_TYPE_INTEGER:
return .integer(result.ok.value.integer)
case LIBSQL_TYPE_REAL:
return .real(result.ok.value.real)
case LIBSQL_TYPE_NULL:
return .null
default:
preconditionFailure()
}
}
public func getData(_ index: Int32) throws -> Data {
guard case let .blob(data) = try self.get(index) else {
throw LibsqlError.typeMismatch
}
return data
}
public func getDouble(_ index: Int32) throws -> Double {
guard case let .real(double) = try self.get(index) else {
throw LibsqlError.typeMismatch
}
return double
}
public func getString(_ index: Int32) throws -> String {
guard case let .text(string) = try self.get(index) else {
throw LibsqlError.typeMismatch
}
return string
}
public func getInt(_ index: Int32) throws -> Int {
guard case let .integer(int) = try self.get(index) else {
throw LibsqlError.typeMismatch
}
return Int(int)
}
}
public class Rows: Sequence, IteratorProtocol {
var inner: libsql_rows_t
fileprivate init(from inner: libsql_rows_t) {
self.inner = inner
}
deinit {
libsql_rows_deinit(self.inner)
}
public func next() -> Row? {
let row = libsql_rows_next(self.inner)
try! errIf(row.err)
if libsql_row_empty(row) {
return nil
}
return Row(from: row)
}
}
public class Statement {
var inner: libsql_statement_t
deinit {
libsql_statement_deinit(self.inner)
}
fileprivate init(from inner: libsql_statement_t) {
self.inner = inner
}
public func execute() throws -> Int {
let exec = libsql_statement_execute(self.inner)
try errIf(exec.err)
return Int(exec.rows_changed)
}
public func query() throws -> Rows {
let rows = libsql_statement_query(self.inner)
try errIf(rows.err)
return Rows(from: rows)
}
public func bind(_ params: [String: ValueRepresentable]) throws -> Self {
for (name, value) in params {
switch value.toValue() {
case .integer(let integer):
let bind = libsql_statement_bind_named(
self.inner,
name,
libsql_integer(integer)
)
try errIf(bind.err)
case .text(let text):
let len = text.count + 1
try text.withCString { text in
let bind = libsql_statement_bind_named(
self.inner,
name,
libsql_text(text, len)
)
try errIf(bind.err)
}
case .blob(let slice):
try slice.withUnsafeBytes { slice in
let bind = libsql_statement_bind_named(
self.inner,
name,
libsql_blob(slice.baseAddress, slice.count)
)
try errIf(bind.err)
}
case .real(let real):
let bind = libsql_statement_bind_named(
self.inner,
name,
libsql_real(real)
)
try errIf(bind.err)
case .null:
let bind = libsql_statement_bind_named(
self.inner,
name,
libsql_value_t(value: .init(), type: LIBSQL_TYPE_NULL)
)
try errIf(bind.err)
}
}
return self;
}
public func bind(_ params: [ValueRepresentable]) throws -> Self {
for value in params {
switch value.toValue() {
case .integer(let integer):
let bind = libsql_statement_bind_value(
self.inner,
libsql_integer(integer)
)
try errIf(bind.err)
case .text(let text):
let len = text.count + 1
try text.withCString { text in
let bind = libsql_statement_bind_value(
self.inner,
libsql_text(text, len)
)
try errIf(bind.err)
}
case .blob(let slice):
try slice.withUnsafeBytes { slice in
let bind = libsql_statement_bind_value(
self.inner,
libsql_blob(slice.baseAddress, slice.count)
)
try errIf(bind.err)
}
case .real(let real):
let bind = libsql_statement_bind_value(self.inner, libsql_real(real))
try errIf(bind.err)
case .null:
let bind = libsql_statement_bind_value(
self.inner,
libsql_value_t(value: .init(), type: LIBSQL_TYPE_NULL)
)
try errIf(bind.err)
}
}
return self;
}
}
public class Transaction: Prepareable {
var inner: libsql_transaction_t
public consuming func commit() {
libsql_transaction_commit(self.inner)
}
public consuming func rollback() {
libsql_transaction_rollback(self.inner)
}
fileprivate init(from inner: libsql_transaction_t) {
self.inner = inner
}
public func execute_batch(_ sql: String) {
libsql_transaction_batch(self.inner, sql)
}
public func prepare(_ sql: String) throws -> Statement {
let stmt = libsql_transaction_prepare(self.inner, sql);
try errIf(stmt.err)
return Statement(from: stmt)
}
}
public class Connection: Prepareable {
var inner: libsql_connection_t
deinit {
libsql_connection_deinit(self.inner)
}
fileprivate init(from inner: libsql_connection_t) {
self.inner = inner
}
public func transaction() throws -> Transaction {
let tx = libsql_connection_transaction(self.inner)
try errIf(tx.err);
return Transaction(from: tx)
}
public func execute_batch(_ sql: String) {
libsql_connection_batch(self.inner, sql)
}
public func prepare(_ sql: String) throws -> Statement {
let stmt = libsql_connection_prepare(self.inner, sql);
try errIf(stmt.err)
return Statement(from: stmt)
}
}
public class Database {
var inner: libsql_database_t
deinit {
libsql_database_deinit(self.inner)
}
public func sync() throws {
let sync = libsql_database_sync(self.inner)
try errIf(sync.err)
}
public func connect() throws -> Connection {
let conn = libsql_database_connect(self.inner)
try errIf(conn.err)
return Connection(from: conn)
}
public init(_ path: String) throws {
self.inner = try path.withCString { path in
var desc = libsql_database_desc_t()
desc.path = path
let db = libsql_database_init(desc)
try errIf(db.err)
return db
}
}
public init(url: String, authToken: String, withWebpki: Bool = false) throws {
self.inner = try url.withCString { url in
try authToken.withCString { authToken in
var desc = libsql_database_desc_t()
desc.url = url
desc.auth_token = authToken
desc.webpki = withWebpki
let db = libsql_database_init(desc)
try errIf(db.err)
return db
}
}
}
public init(
path: String,
url: String,
authToken: String,
readYourWrites: Bool = true,
encryptionKey: String? = nil,
syncInterval: UInt64 = 0,
withWebpki: Bool = false
) throws {
self.inner = try path.withCString { path in
try url.withCString { url in
try authToken.withCString { authToken in
try encryptionKey.withCString { encryptionKey in
var desc = libsql_database_desc_t()
desc.path = path
desc.url = url
desc.auth_token = authToken
desc.encryption_key = encryptionKey
desc.not_read_your_writes = !readYourWrites
desc.sync_interval = syncInterval
desc.webpki = withWebpki
let db = libsql_database_init(desc)
try errIf(db.err)
return db
}
}
}
}
}
}