Skip to content

Commit e5a10f5

Browse files
committed
Use typealiases
1 parent 5e64bb5 commit e5a10f5

File tree

2 files changed

+46
-40
lines changed

2 files changed

+46
-40
lines changed

Sources/SQLite/Core/Connection+Aggregation.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import SQLite3
1010
#endif
1111

1212
extension Connection {
13-
private typealias Aggregate = @convention(block) (Int, OpaquePointer?, Int32, UnsafeMutablePointer<OpaquePointer?>?) -> Void
13+
private typealias Aggregate = @convention(block) (Int, Context, Int32, Argv) -> Void
1414

1515
/// Creates or redefines a custom SQL aggregate.
1616
///
@@ -49,29 +49,29 @@ extension Connection {
4949
state: @escaping () -> UnsafeMutablePointer<T>) {
5050

5151
let argc = argumentCount.map { Int($0) } ?? -1
52-
let box: Aggregate = { (stepFlag: Int, context: OpaquePointer?, argc: Int32, argv: UnsafeMutablePointer<OpaquePointer?>?) in
52+
let box: Aggregate = { (stepFlag: Int, context: Context, argc: Int32, argv: Argv) in
5353
let nBytes = Int32(MemoryLayout<UnsafeMutablePointer<Int64>>.size)
5454
guard let aggregateContext = sqlite3_aggregate_context(context, nBytes) else {
5555
fatalError("Could not get aggregate context")
5656
}
5757
let mutablePointer = aggregateContext.assumingMemoryBound(to: UnsafeMutableRawPointer.self)
5858
if stepFlag > 0 {
59-
let arguments = getArguments(argc: argc, argv: argv)
59+
let arguments = argv.getBindings(argc: argc)
6060
if aggregateContext.assumingMemoryBound(to: Int64.self).pointee == 0 {
6161
mutablePointer.pointee = UnsafeMutableRawPointer(mutating: state())
6262
}
6363
step(arguments, mutablePointer.pointee.assumingMemoryBound(to: T.self))
6464
} else {
6565
let result = final(mutablePointer.pointee.assumingMemoryBound(to: T.self))
66-
set(result: result, on: context)
66+
context.set(result: result)
6767
}
6868
}
6969

70-
func xStep(context: OpaquePointer?, argc: Int32, value: UnsafeMutablePointer<OpaquePointer?>?) {
70+
func xStep(context: Context, argc: Int32, value: Argv) {
7171
unsafeBitCast(sqlite3_user_data(context), to: Aggregate.self)(1, context, argc, value)
7272
}
7373

74-
func xFinal(context: OpaquePointer?) {
74+
func xFinal(context: Context) {
7575
unsafeBitCast(sqlite3_user_data(context), to: Aggregate.self)(0, context, 0, nil)
7676
}
7777

Sources/SQLite/Core/Connection.swift

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -590,10 +590,10 @@ public final class Connection {
590590
deterministic: Bool = false,
591591
_ block: @escaping (_ args: [Binding?]) -> Binding?) {
592592
let argc = argumentCount.map { Int($0) } ?? -1
593-
let box: Function = { context, argc, argv in
594-
set(result: block(getArguments(argc: argc, argv: argv)), on: context)
593+
let box: Function = { (context: Context, argc, argv: Argv) in
594+
context.set(result: block(argv.getBindings(argc: argc)))
595595
}
596-
func xFunc(context: OpaquePointer?, argc: Int32, value: UnsafeMutablePointer<OpaquePointer?>?) {
596+
func xFunc(context: Context, argc: Int32, value: Argv) {
597597
unsafeBitCast(sqlite3_user_data(context), to: Function.self)(context, argc, value)
598598
}
599599
let flags = SQLITE_UTF8 | (deterministic ? SQLITE_DETERMINISTIC : 0)
@@ -619,7 +619,7 @@ public final class Connection {
619619
functions[functionName]?[argc] = value
620620
}
621621

622-
fileprivate typealias Function = @convention(block) (OpaquePointer?, Int32, UnsafeMutablePointer<OpaquePointer?>?) -> Void
622+
fileprivate typealias Function = @convention(block) (Context, Int32, Argv) -> Void
623623
fileprivate var functions = [String: [Int: Any]]()
624624

625625
/// Defines a new collating sequence.
@@ -723,39 +723,45 @@ extension Connection.Location: CustomStringConvertible {
723723

724724
}
725725

726-
func getArguments(argc: Int32, argv: UnsafeMutablePointer<OpaquePointer?>?) -> [Binding?] {
727-
(0..<Int(argc)).map { idx in
728-
let value = argv![idx]
729-
switch sqlite3_value_type(value) {
730-
case SQLITE_BLOB:
731-
return Blob(bytes: sqlite3_value_blob(value), length: Int(sqlite3_value_bytes(value)))
732-
case SQLITE_FLOAT:
733-
return sqlite3_value_double(value)
734-
case SQLITE_INTEGER:
735-
return sqlite3_value_int64(value)
736-
case SQLITE_NULL:
737-
return nil
738-
case SQLITE_TEXT:
739-
return String(cString: UnsafePointer(sqlite3_value_text(value)))
740-
case let type:
741-
fatalError("unsupported value type: \(type)")
726+
typealias Context = OpaquePointer?
727+
extension Context {
728+
func set(result: Binding?) {
729+
switch result {
730+
case let blob as Blob:
731+
sqlite3_result_blob(self, blob.bytes, Int32(blob.bytes.count), nil)
732+
case let double as Double:
733+
sqlite3_result_double(self, double)
734+
case let int as Int64:
735+
sqlite3_result_int64(self, int)
736+
case let string as String:
737+
sqlite3_result_text(self, string, Int32(string.lengthOfBytes(using: .utf8)), SQLITE_TRANSIENT)
738+
case .none:
739+
sqlite3_result_null(self)
740+
default:
741+
fatalError("unsupported result type: \(String(describing: result))")
742742
}
743743
}
744744
}
745745

746-
func set(result: Binding?, on context: OpaquePointer?) {
747-
switch result {
748-
case let blob as Blob:
749-
sqlite3_result_blob(context, blob.bytes, Int32(blob.bytes.count), nil)
750-
case let double as Double:
751-
sqlite3_result_double(context, double)
752-
case let int as Int64:
753-
sqlite3_result_int64(context, int)
754-
case let string as String:
755-
sqlite3_result_text(context, string, Int32(string.lengthOfBytes(using: .utf8)), SQLITE_TRANSIENT)
756-
case .none:
757-
sqlite3_result_null(context)
758-
default:
759-
fatalError("unsupported result type: \(String(describing: result))")
746+
typealias Argv = UnsafeMutablePointer<OpaquePointer?>?
747+
extension Argv {
748+
func getBindings(argc: Int32) -> [Binding?] {
749+
(0..<Int(argc)).map { idx in
750+
let value = self![idx]
751+
switch sqlite3_value_type(value) {
752+
case SQLITE_BLOB:
753+
return Blob(bytes: sqlite3_value_blob(value), length: Int(sqlite3_value_bytes(value)))
754+
case SQLITE_FLOAT:
755+
return sqlite3_value_double(value)
756+
case SQLITE_INTEGER:
757+
return sqlite3_value_int64(value)
758+
case SQLITE_NULL:
759+
return nil
760+
case SQLITE_TEXT:
761+
return String(cString: UnsafePointer(sqlite3_value_text(value)))
762+
case let type:
763+
fatalError("unsupported value type: \(type)")
764+
}
765+
}
760766
}
761767
}

0 commit comments

Comments
 (0)