Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native user_version support in Connection #1105

Merged
merged 8 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions Sources/SQLite/Core/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,20 @@ public final class Connection {
Int(sqlite3_total_changes(handle))
}

/// Gets the user version of the database.
/// The user version of the database.
/// See SQLite [PRAGMA user_version](https://sqlite.org/pragma.html#pragma_user_version)
/// - Returns: the user version of the database
public func getUserVersion() throws -> Int32? {
guard let userVersion = try scalar("PRAGMA user_version") as? Int64 else {
return nil
public var userVersion: Int32? {
get {
guard let userVersion = try? scalar("PRAGMA user_version") as? Int64 else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(try? scalar("PRAGMA user_version") as? Int64).map(Int32.init)

return nil
}
return Int32(userVersion)
}
set {
if let userVersion = newValue {
_ = try? run("PRAGMA user_version = \(userVersion)")
}
nathanfallet marked this conversation as resolved.
Show resolved Hide resolved
}
return Int32(userVersion)
}

/// Sets the user version of the database.
/// See SQLite [PRAGMA user_version](https://sqlite.org/pragma.html#pragma_user_version)
/// - Parameter userVersion: the new user version of the database
public func setUserVersion(to userVersion: Int32) throws {
try run("PRAGMA user_version = \(userVersion)")
}

// MARK: - Execute
Expand Down
4 changes: 2 additions & 2 deletions Tests/SQLiteTests/ConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ class ConnectionTests: SQLiteTestCase {
}

func test_userVersion() {
try! db.setUserVersion(to: 2)
XCTAssertEqual(2, try! db.getUserVersion()!)
db.userVersion = 2
XCTAssertEqual(2, db.userVersion!)
}

func test_prepare_preparesAndReturnsStatements() {
Expand Down