Skip to content

Commit 723cdc8

Browse files
committed
Lint fixes, swap param order
1 parent e7bed4a commit 723cdc8

File tree

5 files changed

+97
-85
lines changed

5 files changed

+97
-85
lines changed

SQLite.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
03A65E941C6BB3030062603F /* ValueTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE247B331C3F142E00AE3E12 /* ValueTests.swift */; };
4949
03A65E951C6BB3030062603F /* TestHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE247B161C3F127200AE3E12 /* TestHelpers.swift */; };
5050
03A65E971C6BB3210062603F /* libsqlite3.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 03A65E961C6BB3210062603F /* libsqlite3.tbd */; };
51+
19A17073552293CA063BEA66 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A17E723300E5ED3771DCB5 /* Result.swift */; };
5152
19A1709C3E7A406E62293B2A /* Fixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A17B93B48B5560E6E51791 /* Fixtures.swift */; };
5253
19A17152E32A9585831E3FE0 /* DateAndTimeFunctions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A17BA55DABB480F9020C8A /* DateAndTimeFunctions.swift */; };
5354
19A1717B10CC941ACB5533D6 /* FTS5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1730E4390C775C25677D1 /* FTS5.swift */; };
@@ -60,6 +61,7 @@
6061
19A172EB202970561E5C4245 /* DateAndTimeFunctionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1729B75C33F9A0B9A89C1 /* DateAndTimeFunctionTests.swift */; };
6162
19A173668D948AD4DF1F5352 /* DateAndTimeFunctions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A17BA55DABB480F9020C8A /* DateAndTimeFunctions.swift */; };
6263
19A1737286A74F3CF7412906 /* DateAndTimeFunctions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A17BA55DABB480F9020C8A /* DateAndTimeFunctions.swift */; };
64+
19A173EFEF0B3BD0B3ED406C /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A17E723300E5ED3771DCB5 /* Result.swift */; };
6365
19A17408007B182F884E3A53 /* Fixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A17B93B48B5560E6E51791 /* Fixtures.swift */; };
6466
19A17490543609FCED53CACC /* Errors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1710E73A46D5AC721CDA9 /* Errors.swift */; };
6567
19A174D78559CD30679BCCCB /* FTS5Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1721B8984686B9963B45D /* FTS5Tests.swift */; };
@@ -82,6 +84,7 @@
8284
19A17E04C4C0956715C5676A /* FoundationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1794CC4D7827E997E32A7 /* FoundationTests.swift */; };
8385
19A17E29278A12BC4F542506 /* DateAndTimeFunctions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A17BA55DABB480F9020C8A /* DateAndTimeFunctions.swift */; };
8486
19A17EC0D68BA8C03288ADF7 /* FTS5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1730E4390C775C25677D1 /* FTS5.swift */; };
87+
19A17F1B3F0A3C96B5ED6D64 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A17E723300E5ED3771DCB5 /* Result.swift */; };
8588
19A17F3E1F7ACA33BD43E138 /* fixtures in Resources */ = {isa = PBXBuildFile; fileRef = 19A17E2695737FAB5D6086E3 /* fixtures */; };
8689
19A17F60B685636D1F83C2DD /* Fixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A17B93B48B5560E6E51791 /* Fixtures.swift */; };
8790
19A17FB80B94E882050AA908 /* FoundationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1794CC4D7827E997E32A7 /* FoundationTests.swift */; };
@@ -241,6 +244,7 @@
241244
19A17BA55DABB480F9020C8A /* DateAndTimeFunctions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DateAndTimeFunctions.swift; sourceTree = "<group>"; };
242245
19A17BA6B4E282C1315A115C /* QueryIntegrationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QueryIntegrationTests.swift; sourceTree = "<group>"; };
243246
19A17E2695737FAB5D6086E3 /* fixtures */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = folder; path = fixtures; sourceTree = "<group>"; };
247+
19A17E723300E5ED3771DCB5 /* Result.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Result.swift; sourceTree = "<group>"; };
244248
19A17EA3A313F129011B3FA0 /* Release.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = Release.md; sourceTree = "<group>"; };
245249
3717F907221F5D7C00B9BD3D /* CustomAggregationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomAggregationTests.swift; sourceTree = "<group>"; };
246250
3D67B3E51DB2469200A4F4C6 /* libsqlite3.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libsqlite3.tbd; path = Platforms/WatchOS.platform/Developer/SDKs/WatchOS3.0.sdk/usr/lib/libsqlite3.tbd; sourceTree = DEVELOPER_DIR; };
@@ -456,6 +460,7 @@
456460
EE247AF31C3F06E900AE3E12 /* Value.swift */,
457461
19A1710E73A46D5AC721CDA9 /* Errors.swift */,
458462
02A43A9722738CF100FEC494 /* Backup.swift */,
463+
19A17E723300E5ED3771DCB5 /* Result.swift */,
459464
);
460465
path = Core;
461466
sourceTree = "<group>";
@@ -843,6 +848,7 @@
843848
02A43A9A22738CF100FEC494 /* Backup.swift in Sources */,
844849
19A17FF4A10B44D3937C8CAC /* Errors.swift in Sources */,
845850
19A1737286A74F3CF7412906 /* DateAndTimeFunctions.swift in Sources */,
851+
19A17073552293CA063BEA66 /* Result.swift in Sources */,
846852
);
847853
runOnlyForDeploymentPostprocessing = 0;
848854
};
@@ -936,6 +942,7 @@
936942
02A43A9822738CF100FEC494 /* Backup.swift in Sources */,
937943
19A1792C0520D4E83C2EB075 /* Errors.swift in Sources */,
938944
19A17E29278A12BC4F542506 /* DateAndTimeFunctions.swift in Sources */,
945+
19A173EFEF0B3BD0B3ED406C /* Result.swift in Sources */,
939946
);
940947
runOnlyForDeploymentPostprocessing = 0;
941948
};
@@ -998,6 +1005,7 @@
9981005
02A43A9922738CF100FEC494 /* Backup.swift in Sources */,
9991006
19A17490543609FCED53CACC /* Errors.swift in Sources */,
10001007
19A17152E32A9585831E3FE0 /* DateAndTimeFunctions.swift in Sources */,
1008+
19A17F1B3F0A3C96B5ED6D64 /* Result.swift in Sources */,
10011009
);
10021010
runOnlyForDeploymentPostprocessing = 0;
10031011
};

Sources/SQLite/Core/Backup.swift

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ import SQLite3
3838
///
3939
/// See: <https://www.sqlite.org/backup.html>
4040
public final class Backup {
41-
41+
4242
/// The name of the database to backup
4343
public enum DatabaseName {
44-
44+
4545
/// The main database
4646
case main
47-
47+
4848
/// The temporary database
4949
case temp
50-
50+
5151
/// A database added to the connection with ATTACH statement
5252
case attached(name: String)
53-
53+
5454
var name: String {
5555
switch self {
5656
case .main:
@@ -62,16 +62,16 @@ public final class Backup {
6262
}
6363
}
6464
}
65-
65+
6666
/// Number of pages to copy while performing a backup step
6767
public enum Pages {
68-
68+
6969
/// Indicates all remaining pages should be copied
7070
case all
71-
71+
7272
/// Indicates the maximal number of pages to be copied in single step
7373
case limited(number: Int32)
74-
74+
7575
var number: Int32 {
7676
switch self {
7777
case .all:
@@ -81,63 +81,60 @@ public final class Backup {
8181
}
8282
}
8383
}
84-
84+
8585
/// Total number of pages to copy
8686
///
8787
/// See: <https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backuppagecount>
8888
public var pageCount: Int32 {
8989
return handle.map { sqlite3_backup_pagecount($0) } ?? 0
9090
}
91-
91+
9292
/// Number of remaining pages to copy.
9393
///
9494
/// See: <https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupremaining>
9595
public var remainingPages: Int32 {
9696
return handle.map { sqlite3_backup_remaining($0) } ?? 0
9797
}
98-
98+
9999
private let targetConnection: Connection
100100
private let sourceConnection: Connection
101-
101+
102102
private var handle: OpaquePointer?
103-
103+
104104
/// Initializes a new SQLite backup.
105105
///
106106
/// - Parameters:
107107
///
108-
/// - targetConnection: The connection to the database to save backup into.
109-
///
110-
/// - targetName: The name of the database to save backup into.
111-
///
112-
/// Default: `.main`.
113-
///
114108
/// - sourceConnection: The connection to the database to backup.
115-
///
116109
/// - sourceName: The name of the database to backup.
117-
///
110+
/// Default: `.main`.
111+
///
112+
/// - targetConnection: The connection to the database to save backup into.
113+
/// - targetName: The name of the database to save backup into.
118114
/// Default: `.main`.
119115
///
120116
/// - Returns: A new database backup.
121117
///
122118
/// See: <https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupinit>
123-
public init(targetConnection: Connection,
124-
targetName: DatabaseName = .main,
125-
sourceConnection: Connection,
126-
sourceName: DatabaseName = .main) throws {
127-
119+
public init(sourceConnection: Connection,
120+
sourceName: DatabaseName = .main,
121+
targetConnection: Connection,
122+
targetName: DatabaseName = .main) throws {
123+
128124
self.targetConnection = targetConnection
129125
self.sourceConnection = sourceConnection
130-
126+
131127
self.handle = sqlite3_backup_init(targetConnection.handle,
132128
targetName.name,
133129
sourceConnection.handle,
134130
sourceName.name)
135-
136-
if self.handle == nil, let error = Result(errorCode: sqlite3_errcode(targetConnection.handle), connection: targetConnection) {
131+
132+
if handle == nil, let error = Result(errorCode: sqlite3_errcode(targetConnection.handle),
133+
connection: targetConnection) {
137134
throw error
138135
}
139136
}
140-
137+
141138
/// Performs a backup step.
142139
///
143140
/// - Parameter pagesToCopy: The maximal number of pages to copy in one step
@@ -147,29 +144,29 @@ public final class Backup {
147144
/// See: <https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupstep>
148145
public func step(pagesToCopy pages: Pages = .all) throws {
149146
let status = sqlite3_backup_step(handle, pages.number)
150-
147+
151148
guard status != SQLITE_DONE else {
152149
finish()
153150
return
154151
}
155-
152+
156153
if let error = Result(errorCode: status, connection: targetConnection) {
157154
throw error
158155
}
159156
}
160-
157+
161158
/// Finalizes backup.
162159
///
163160
/// See: <https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupfinish>
164161
public func finish() {
165162
guard let handle = self.handle else {
166163
return
167164
}
168-
165+
169166
sqlite3_backup_finish(handle)
170167
self.handle = nil
171168
}
172-
169+
173170
deinit {
174171
finish()
175172
}

Sources/SQLite/Core/Connection.swift

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -790,9 +790,9 @@ public final class Connection {
790790
}
791791
fileprivate typealias Collation = @convention(block) (UnsafeRawPointer, UnsafeRawPointer) -> Int32
792792
fileprivate var collations = [String: Collation]()
793-
793+
794794
// MARK: - Backup
795-
795+
796796
/// Prepares a new backup for current connection.
797797
///
798798
/// - Parameters:
@@ -808,16 +808,14 @@ public final class Connection {
808808
/// Default: `.main`.
809809
///
810810
/// - Returns: A new database backup.
811-
811+
812812
public func backup(databaseName: Backup.DatabaseName = .main,
813813
usingConnection targetConnection: Connection,
814814
andDatabaseName targetDatabaseName: Backup.DatabaseName = .main) throws -> Backup {
815-
return try Backup(targetConnection: targetConnection,
816-
targetName: targetDatabaseName,
817-
sourceConnection: self,
818-
sourceName: databaseName)
815+
try Backup(sourceConnection: self, sourceName: databaseName, targetConnection: targetConnection,
816+
targetName: targetDatabaseName)
819817
}
820-
818+
821819
// MARK: - Error Handling
822820

823821
func sync<T>(_ block: () throws -> T) rethrows -> T {
@@ -866,39 +864,3 @@ extension Connection.Location: CustomStringConvertible {
866864
}
867865

868866
}
869-
870-
public enum Result: Error {
871-
872-
fileprivate static let successCodes: Set = [SQLITE_OK, SQLITE_ROW, SQLITE_DONE]
873-
874-
/// Represents a SQLite specific [error code](https://sqlite.org/rescode.html)
875-
///
876-
/// - message: English-language text that describes the error
877-
///
878-
/// - code: SQLite [error code](https://sqlite.org/rescode.html#primary_result_code_list)
879-
///
880-
/// - statement: the statement which produced the error
881-
case error(message: String, code: Int32, statement: Statement?)
882-
883-
init?(errorCode: Int32, connection: Connection, statement: Statement? = nil) {
884-
guard !Result.successCodes.contains(errorCode) else { return nil }
885-
886-
let message = String(cString: sqlite3_errmsg(connection.handle))
887-
self = .error(message: message, code: errorCode, statement: statement)
888-
}
889-
890-
}
891-
892-
extension Result: CustomStringConvertible {
893-
894-
public var description: String {
895-
switch self {
896-
case let .error(message, errorCode, statement):
897-
if let statement = statement {
898-
return "\(message) (\(statement)) (code: \(errorCode))"
899-
} else {
900-
return "\(message) (code: \(errorCode))"
901-
}
902-
}
903-
}
904-
}

Sources/SQLite/Core/Result.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#if SQLITE_SWIFT_STANDALONE
2+
import sqlite3
3+
#elseif SQLITE_SWIFT_SQLCIPHER
4+
import SQLCipher
5+
#elseif os(Linux)
6+
import CSQLite
7+
#else
8+
import SQLite3
9+
#endif
10+
11+
public enum Result: Error {
12+
13+
fileprivate static let successCodes: Set = [SQLITE_OK, SQLITE_ROW, SQLITE_DONE]
14+
15+
/// Represents a SQLite specific [error code](https://sqlite.org/rescode.html)
16+
///
17+
/// - message: English-language text that describes the error
18+
///
19+
/// - code: SQLite [error code](https://sqlite.org/rescode.html#primary_result_code_list)
20+
///
21+
/// - statement: the statement which produced the error
22+
case error(message: String, code: Int32, statement: Statement?)
23+
24+
init?(errorCode: Int32, connection: Connection, statement: Statement? = nil) {
25+
guard !Result.successCodes.contains(errorCode) else { return nil }
26+
27+
let message = String(cString: sqlite3_errmsg(connection.handle))
28+
self = .error(message: message, code: errorCode, statement: statement)
29+
}
30+
31+
}
32+
33+
extension Result: CustomStringConvertible {
34+
35+
public var description: String {
36+
switch self {
37+
case let .error(message, errorCode, statement):
38+
if let statement = statement {
39+
return "\(message) (\(statement)) (code: \(errorCode))"
40+
} else {
41+
return "\(message) (code: \(errorCode))"
42+
}
43+
}
44+
}
45+
}

Tests/SQLiteTests/ConnectionTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,15 @@ class ConnectionTests: SQLiteTestCase {
147147

148148
assertSQL("BEGIN EXCLUSIVE TRANSACTION")
149149
}
150-
150+
151151
func test_backup_copiesDatabase() throws {
152152
let target = try Connection()
153-
154-
try InsertUsers("alice", "betsy")
155-
153+
154+
try insertUsers("alice", "betsy")
155+
156156
let backup = try db.backup(usingConnection: target)
157157
try backup.step()
158-
158+
159159
let users = try target.prepare("SELECT email FROM users ORDER BY email")
160160
XCTAssertEqual(users.map { $0[0] as? String }, ["[email protected]", "[email protected]"])
161161
}

0 commit comments

Comments
 (0)