Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
17 changes: 17 additions & 0 deletions Sources/SQLite/Core/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,23 @@ public final class Connection {
Int(sqlite3_total_changes(handle))
}

/// Gets 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? {
Copy link
Collaborator

@jberkel jberkel Jan 18, 2022

Choose a reason for hiding this comment

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

var userVersion: Int32? {
   get { }
   set { }
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried that first, but I ended with problems with try. I can eventually ignore them with optionals

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, I think it's ok to ignore the error here

guard let userVersion = try scalar("PRAGMA user_version") as? Int64 else {
return nil
}
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

/// Executes a batch of SQL statements.
Expand Down
5 changes: 5 additions & 0 deletions Tests/SQLiteTests/ConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ class ConnectionTests: SQLiteTestCase {
XCTAssertEqual(2, db.totalChanges)
}

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

func test_prepare_preparesAndReturnsStatements() {
_ = try! db.prepare("SELECT * FROM users WHERE admin = 0")
_ = try! db.prepare("SELECT * FROM users WHERE admin = ?", 0)
Expand Down