Skip to content

Commit a2d387b

Browse files
authored
Add support for VACUUM INTO (#51)
- Add support for VACUUM INTO - Update to Swift 5.9
1 parent 28a7b34 commit a2d387b

8 files changed

Lines changed: 70 additions & 32 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88

99
steps:
1010
- uses: actions/checkout@v3
11-
- name: Select Xcode 14
12-
run: sudo xcode-select -s /Applications/Xcode_14.3.1.app
11+
- name: Select Xcode 15
12+
run: sudo xcode-select -s /Applications/Xcode_15.0.app
1313
- name: Test
1414
run: swift test

.swift-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
5.8.0
1+
5.9.0
22

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.8
1+
// swift-tools-version:5.9
22
import PackageDescription
33

44
let package = Package(

Sources/SQLite/SQLiteDatabase.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -579,18 +579,21 @@ public extension SQLiteDatabase {
579579
}
580580

581581
func incrementalVacuum(_ pages: Int? = nil) throws {
582-
let sql: SQL
583-
if let pages {
584-
sql = "PRAGMA incremental_vacuum(\(pages));"
582+
let sql: SQL = if let pages {
583+
"PRAGMA incremental_vacuum(\(pages));"
585584
} else {
586-
sql = "PRAGMA incremental_vacuum;"
585+
"PRAGMA incremental_vacuum;"
587586
}
588587
try execute(raw: sql)
589588
}
590589

591-
func vacuum() throws {
590+
func vacuum(into path: String? = nil) throws {
592591
do {
593-
try database.writer.vacuum()
592+
if let path {
593+
try database.writer.vacuum(into: path)
594+
} else {
595+
try database.writer.vacuum()
596+
}
594597
} catch {
595598
os_log(
596599
"vacuum: error=%s",
@@ -722,15 +725,15 @@ private enum Database {
722725

723726
var reader: AnyDatabaseReader {
724727
switch self {
725-
case let .pool(pool): return AnyDatabaseReader(pool)
726-
case let .queue(queue): return AnyDatabaseReader(queue)
728+
case let .pool(pool): AnyDatabaseReader(pool)
729+
case let .queue(queue): AnyDatabaseReader(queue)
727730
}
728731
}
729732

730733
var writer: AnyDatabaseWriter {
731734
switch self {
732-
case let .pool(pool): return AnyDatabaseWriter(pool)
733-
case let .queue(queue): return AnyDatabaseWriter(queue)
735+
case let .pool(pool): AnyDatabaseWriter(pool)
736+
case let .queue(queue): AnyDatabaseWriter(queue)
734737
}
735738
}
736739

Sources/SQLite/SQLiteError.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ public extension SQLiteError {
146146
var isInterrupt: Bool {
147147
switch self {
148148
case SQLiteError.SQLITE_ABORT, SQLiteError.SQLITE_INTERRUPT:
149-
return true
149+
true
150150
default:
151-
return false
151+
false
152152
}
153153
}
154154
}

Sources/SQLite/SQLiteValue.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ public extension [UInt8]? {
130130
var sqliteValue: SQLiteValue {
131131
switch self {
132132
case .none:
133-
return .null
133+
.null
134134
case let .some(bytes):
135-
return .data(Data(bytes))
135+
.data(Data(bytes))
136136
}
137137
}
138138
}
@@ -141,9 +141,9 @@ public extension Optional where Wrapped: BinaryInteger {
141141
var sqliteValue: SQLiteValue {
142142
switch self {
143143
case .none:
144-
return .null
144+
.null
145145
case let .some(int):
146-
return int.sqliteValue
146+
int.sqliteValue
147147
}
148148
}
149149
}
@@ -152,9 +152,9 @@ public extension Bool? {
152152
var sqliteValue: SQLiteValue {
153153
switch self {
154154
case .none:
155-
return .null
155+
.null
156156
case let .some(bool):
157-
return bool.sqliteValue
157+
bool.sqliteValue
158158
}
159159
}
160160
}
@@ -163,9 +163,9 @@ public extension Data? {
163163
var sqliteValue: SQLiteValue {
164164
switch self {
165165
case .none:
166-
return .null
166+
.null
167167
case let .some(data):
168-
return .data(data)
168+
.data(data)
169169
}
170170
}
171171
}
@@ -174,9 +174,9 @@ public extension Date? {
174174
var sqliteValue: SQLiteValue {
175175
switch self {
176176
case .none:
177-
return .null
177+
.null
178178
case let .some(date):
179-
return date.sqliteValue
179+
date.sqliteValue
180180
}
181181
}
182182
}
@@ -185,9 +185,9 @@ public extension Optional where Wrapped: StringProtocol {
185185
var sqliteValue: SQLiteValue {
186186
switch self {
187187
case .none:
188-
return .null
188+
.null
189189
case let .some(string):
190-
return string.sqliteValue
190+
string.sqliteValue
191191
}
192192
}
193193
}

Tests/SQLiteTests/SQLiteDatabaseTests.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,42 @@ final class SQLiteDatabaseTests: XCTestCase {
207207
XCTAssertEqual(2, try getPageCount())
208208
}
209209

210+
func testVacuumIntoFile() throws {
211+
try Sandbox.execute { directory in
212+
let path1 = directory.appendingPathComponent("one.db").path
213+
let db1 = try SQLiteDatabase(path: path1)
214+
defer { try? db1.close() }
215+
216+
let path2 = directory.appendingPathComponent("two.db").path
217+
218+
XCTAssertEqual(.none, db1.autoVacuumMode)
219+
220+
try db1.execute(raw: _createTableWithBlob)
221+
222+
try db1.inTransaction { db in
223+
try (0 ..< 1000).forEach { index in
224+
let args: SQLiteArguments = [
225+
"id": .integer(Int64(index)), "data": .data(_textData),
226+
]
227+
try db.write(_insertIDAndData, arguments: args)
228+
}
229+
}
230+
231+
try db1.vacuum(into: path2)
232+
233+
XCTAssertTrue(FileManager().fileExists(atPath: path2))
234+
235+
let db2 = try SQLiteDatabase(path: path2)
236+
defer { try? db2.close() }
237+
238+
XCTAssertEqual(try db1.tables(), try db2.tables())
239+
XCTAssertEqual(
240+
try db1.read("SELECT * FROM test;"),
241+
try db2.read("SELECT * FROM test;")
242+
)
243+
}
244+
}
245+
210246
func testCreateTable() throws {
211247
XCTAssertNoThrow(try database.execute(raw: _createTableWithBlob))
212248
let tableNames = try database.tables()

Tests/SQLiteTests/Types/PetOwner.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,10 @@ extension Person: SQLiteTransformable {
6969
}
7070

7171
var asArguments: SQLiteArguments {
72-
let titleValue: SQLiteValue
73-
if let title {
74-
titleValue = .text(title)
72+
let titleValue: SQLiteValue = if let title {
73+
.text(title)
7574
} else {
76-
titleValue = .null
75+
.null
7776
}
7877

7978
return [

0 commit comments

Comments
 (0)