@@ -372,12 +372,16 @@ public final class Database {
372372 /// returns true, it will try again. If it returns false,
373373 /// no further attempts will be made.
374374 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)
379382 }
380383 }
384+ private var busy : SQLiteBusyHandlerCallback ?
381385
382386 /// Sets a handler to call when a statement is executed with the compiled
383387 /// SQL.
@@ -387,11 +391,13 @@ public final class Database {
387391 /// act as a logger.
388392 public func trace( callback: ( String -> ( ) ) ? ) {
389393 if let callback = callback {
390- SQLiteTrace ( handle ) { callback ( String . fromCString ( $0) !) }
394+ trace = { callback ( String . fromCString ( $0) !) }
391395 } else {
392- SQLiteTrace ( handle , nil )
396+ trace = nil
393397 }
398+ SQLiteTrace ( handle, trace)
394399 }
400+ private var trace : SQLiteTraceCallback ?
395401
396402 /// Creates or redefines a custom SQL function.
397403 ///
@@ -412,7 +418,8 @@ public final class Database {
412418 /// should return a raw SQL value (or nil).
413419 public func create( #function: String, argc: Int = - 1 , deterministic: Bool = false, _ block: [ Binding? ] -> Binding? ) {
414420 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
416423 let arguments : [ Binding ? ] = map ( 0 ..< Int ( argc) ) { idx in
417424 let value = argv [ idx]
418425 switch sqlite3_value_type ( value) {
@@ -447,8 +454,10 @@ public final class Database {
447454 assertionFailure ( " unsupported result type: \( result) " )
448455 }
449456 }
457+ return SQLiteCreateFunction ( self . handle, function, Int32 ( argc) , deterministic ? 1 : 0 , self . functions [ function] ? [ argc] )
450458 }
451459 }
460+ private var functions = [ String: [ Int: SQLiteCreateFunctionCallback] ] ( )
452461
453462 /// The return type of a collation comparison function.
454463 public typealias ComparisonResult = NSComparisonResult
@@ -461,11 +470,13 @@ public final class Database {
461470 /// returns the comparison result.
462471 public func create( #collation: String, _ block: ( String, String) -> ComparisonResult ) {
463472 try {
464- SQLiteCreateCollation ( self . handle , collation) { lhs, rhs in
473+ self . collations [ collation] = { lhs, rhs in
465474 return Int32 ( block ( String . fromCString ( lhs) !, String . fromCString ( rhs) !) . rawValue)
466475 }
476+ return SQLiteCreateCollation ( self . handle, collation, self . collations [ collation] )
467477 }
468478 }
479+ private var collations = [ String: SQLiteCreateCollationCallback] ( )
469480
470481 // MARK: - Error Handling
471482
0 commit comments