@@ -415,17 +415,17 @@ public final class Connection {
415415 ///
416416 /// db.trace { SQL in print(SQL) }
417417 public func trace( _ callback: ( ( String ) -> Void ) ? ) {
418- #if SQLITE_SWIFT_SQLCIPHER || os(Linux)
418+ if #available( iOS 10 . 0 , OSX 10 . 12 , tvOS 10 . 0 , watchOS 3 . 0 , * ) {
419+ trace_v2 ( callback)
420+ } else {
419421 trace_v1 ( callback)
420- #else
421- if #available( iOS 10 . 0 , OSX 10 . 12 , tvOS 10 . 0 , watchOS 3 . 0 , * ) {
422- trace_v2 ( callback)
423- } else {
424- trace_v1 ( callback)
425- }
426- #endif
422+ }
427423 }
428424
425+ @available ( OSX, deprecated: 10.2 )
426+ @available ( iOS, deprecated: 10.0 )
427+ @available ( watchOS, deprecated: 3.0 )
428+ @available ( tvOS, deprecated: 10.0 )
429429 fileprivate func trace_v1( _ callback: ( ( String ) -> Void ) ? ) {
430430 guard let callback = callback else {
431431 sqlite3_trace ( handle, nil /* xCallback */, nil /* pCtx */)
@@ -447,8 +447,36 @@ public final class Connection {
447447 trace = box
448448 }
449449
450+ @available ( iOS 10 . 0 , OSX 10 . 12 , tvOS 10 . 0 , watchOS 3 . 0 , * )
451+ fileprivate func trace_v2( _ callback: ( ( String ) -> Void ) ? ) {
452+ guard let callback = callback else {
453+ // If the X callback is NULL or if the M mask is zero, then tracing is disabled.
454+ sqlite3_trace_v2 ( handle, 0 /* mask */, nil /* xCallback */, nil /* pCtx */)
455+ trace = nil
456+ return
457+ }
450458
451-
459+ let box : Trace = { ( pointer: UnsafeRawPointer ) in
460+ callback ( String ( cString: pointer. assumingMemoryBound ( to: UInt8 . self) ) )
461+ }
462+ sqlite3_trace_v2 ( handle, UInt32 ( SQLITE_TRACE_STMT) /* mask */,
463+ {
464+ // A trace callback is invoked with four arguments: callback(T,C,P,X).
465+ // The T argument is one of the SQLITE_TRACE constants to indicate why the
466+ // callback was invoked. The C argument is a copy of the context pointer.
467+ // The P and X arguments are pointers whose meanings depend on T.
468+ ( T: UInt32 , C: UnsafeMutableRawPointer ? , P: UnsafeMutableRawPointer ? , X: UnsafeMutableRawPointer ? ) in
469+ if let P = P,
470+ let expandedSQL = sqlite3_expanded_sql ( OpaquePointer ( P) ) {
471+ unsafeBitCast ( C, to: Trace . self) ( expandedSQL)
472+ sqlite3_free ( expandedSQL)
473+ }
474+ return Int32 ( 0 ) // currently ignored
475+ } ,
476+ unsafeBitCast ( box, to: UnsafeMutableRawPointer . self) /* pCtx */
477+ )
478+ trace = box
479+ }
452480
453481 fileprivate typealias Trace = @convention ( block) ( UnsafeRawPointer ) -> Void
454482 fileprivate var trace : Trace ?
@@ -712,39 +740,3 @@ extension Result : CustomStringConvertible {
712740 }
713741 }
714742}
715-
716- #if !SQLITE_SWIFT_SQLCIPHER && !os(Linux)
717- @available ( iOS 10 . 0 , OSX 10 . 12 , tvOS 10 . 0 , watchOS 3 . 0 , * )
718- extension Connection {
719- fileprivate func trace_v2( _ callback: ( ( String ) -> Void ) ? ) {
720- guard let callback = callback else {
721- // If the X callback is NULL or if the M mask is zero, then tracing is disabled.
722- sqlite3_trace_v2 ( handle, 0 /* mask */, nil /* xCallback */, nil /* pCtx */)
723- trace = nil
724- return
725- }
726-
727- let box : Trace = { ( pointer: UnsafeRawPointer ) in
728- callback ( String ( cString: pointer. assumingMemoryBound ( to: UInt8 . self) ) )
729- }
730- sqlite3_trace_v2 ( handle,
731- UInt32 ( SQLITE_TRACE_STMT) /* mask */,
732- {
733- // A trace callback is invoked with four arguments: callback(T,C,P,X).
734- // The T argument is one of the SQLITE_TRACE constants to indicate why the
735- // callback was invoked. The C argument is a copy of the context pointer.
736- // The P and X arguments are pointers whose meanings depend on T.
737- ( T: UInt32 , C: UnsafeMutableRawPointer ? , P: UnsafeMutableRawPointer ? , X: UnsafeMutableRawPointer ? ) in
738- if let P = P,
739- let expandedSQL = sqlite3_expanded_sql ( OpaquePointer ( P) ) {
740- unsafeBitCast ( C, to: Trace . self) ( expandedSQL)
741- sqlite3_free ( expandedSQL)
742- }
743- return Int32 ( 0 ) // currently ignored
744- } ,
745- unsafeBitCast ( box, to: UnsafeMutableRawPointer . self) /* pCtx */
746- )
747- trace = box
748- }
749- }
750- #endif
0 commit comments