@@ -372,12 +372,16 @@ public final class Database {
372
372
/// returns true, it will try again. If it returns false,
373
373
/// no further attempts will be made.
374
374
public func busy( callback: ( Int -> Bool ) ? ) {
375
- if let callback = callback {
376
- try { SQLiteBusyHandler ( self . handle) { callback ( Int ( $0) ) ? 1 : 0 } }
377
- } else {
378
- try { SQLiteBusyHandler ( self . handle, nil ) }
375
+ try {
376
+ if let callback = callback {
377
+ self . busy = { callback ( Int ( $0) ) ? 1 : 0 }
378
+ } else {
379
+ self . busy = nil
380
+ }
381
+ return SQLiteBusyHandler ( self . handle, self . busy)
379
382
}
380
383
}
384
+ private var busy : SQLiteBusyHandlerCallback ?
381
385
382
386
/// Sets a handler to call when a statement is executed with the compiled
383
387
/// SQL.
@@ -387,11 +391,13 @@ public final class Database {
387
391
/// act as a logger.
388
392
public func trace( callback: ( String -> ( ) ) ? ) {
389
393
if let callback = callback {
390
- SQLiteTrace ( handle ) { callback ( String . fromCString ( $0) !) }
394
+ trace = { callback ( String . fromCString ( $0) !) }
391
395
} else {
392
- SQLiteTrace ( handle , nil )
396
+ trace = nil
393
397
}
398
+ SQLiteTrace ( handle, trace)
394
399
}
400
+ private var trace : SQLiteTraceCallback ?
395
401
396
402
/// Creates or redefines a custom SQL function.
397
403
///
@@ -412,7 +418,8 @@ public final class Database {
412
418
/// should return a raw SQL value (or nil).
413
419
public func create( #function: String, argc: Int = - 1 , deterministic: Bool = false, _ block: [ Binding? ] -> Binding? ) {
414
420
try {
415
- SQLiteCreateFunction ( self . handle, function, Int32 ( argc) , deterministic ? 1 : 0 ) { context, argc, argv in
421
+ if self . functions [ function] == nil { self . functions [ function] = [ : ] }
422
+ self . functions [ function] ? [ argc] = { context, argc, argv in
416
423
let arguments : [ Binding ? ] = map ( 0 ..< Int ( argc) ) { idx in
417
424
let value = argv [ idx]
418
425
switch sqlite3_value_type ( value) {
@@ -447,8 +454,10 @@ public final class Database {
447
454
assertionFailure ( " unsupported result type: \( result) " )
448
455
}
449
456
}
457
+ return SQLiteCreateFunction ( self . handle, function, Int32 ( argc) , deterministic ? 1 : 0 , self . functions [ function] ? [ argc] )
450
458
}
451
459
}
460
+ private var functions = [ String: [ Int: SQLiteCreateFunctionCallback] ] ( )
452
461
453
462
/// The return type of a collation comparison function.
454
463
public typealias ComparisonResult = NSComparisonResult
@@ -461,11 +470,13 @@ public final class Database {
461
470
/// returns the comparison result.
462
471
public func create( #collation: String, _ block: ( String, String) -> ComparisonResult ) {
463
472
try {
464
- SQLiteCreateCollation ( self . handle , collation) { lhs, rhs in
473
+ self . collations [ collation] = { lhs, rhs in
465
474
return Int32 ( block ( String . fromCString ( lhs) !, String . fromCString ( rhs) !) . rawValue)
466
475
}
476
+ return SQLiteCreateCollation ( self . handle, collation, self . collations [ collation] )
467
477
}
468
478
}
479
+ private var collations = [ String: SQLiteCreateCollationCallback] ( )
469
480
470
481
// MARK: - Error Handling
471
482
0 commit comments