-
Notifications
You must be signed in to change notification settings - Fork 61
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
feature: Add function to set journal mode on sqlite databases #443
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,26 @@ public final class SQLiteDotSwiftDatabase: SQLiteDatabase { | |
private let records: Table | ||
private let keyColumn: SQLite.Expression<CacheKey> | ||
private let recordColumn: SQLite.Expression<String> | ||
|
||
|
||
public enum JournalMode: String { | ||
/// The rollback journal is deleted at the conclusion of each transaction. This is the default behaviour. | ||
case delete = "DELETE" | ||
/// Commits transactions by truncating the rollback journal to zero-length instead of deleting it. | ||
case truncate = "TRUNCATE" | ||
/// Prevents the rollback journal from being deleted at the end of each transaction. Instead, the header | ||
/// of the journal is overwritten with zeros. | ||
case persist = "PERSIST" | ||
/// Stores the rollback journal in volatile RAM. This saves disk I/O but at the expense of database | ||
/// safety and integrity. | ||
case memory = "MEMORY" | ||
/// Uses a write-ahead log instead of a rollback journal to implement transactions. The WAL journaling | ||
/// mode is persistent; after being set it stays in effect across multiple database connections and after | ||
/// closing and reopening the database. | ||
case wal = "WAL" | ||
/// Disables the rollback journal completely | ||
case off = "OFF" | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible some other SQLite implementation would have different journal modes? Are these modes intrinsic to SQLite or just to the If the are universally the journal modes of SQLite, then I don't think this should be part of our specific implementation. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose there might be but these come directly from sqlite.org documentation so if there is an sqlite implementation that differs enough you'll probably want to write your own As for the generic parameter I simply wanted something that was able to be expressible as a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactored as we discussed in 27cc399. |
||
|
||
public init(fileURL: URL) throws { | ||
self.records = Table(Self.tableName) | ||
self.keyColumn = Expression<CacheKey>(Self.keyColumnName) | ||
|
@@ -66,4 +85,11 @@ public final class SQLiteDotSwiftDatabase: SQLiteDatabase { | |
try self.db.prepare("VACUUM;").run() | ||
} | ||
} | ||
|
||
/// Sets the journal mode for the current database. | ||
/// | ||
/// - Parameter mode: Use ``JournalMode``. | ||
public func setJournalMode<T>(mode: T) throws where T : RawRepresentable, T.RawValue == String { | ||
try self.db.run("PRAGMA journal_mode = \(mode.rawValue)") | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This extension ensures it's not a breaking change for projects that have written their own
SQLiteDatabase
implementation.