Skip to content

Commit 770fee8

Browse files
author
Tim De Jong
committed
Fixing the indentation in the WALSnapshot.swift
1 parent f6f9157 commit 770fee8

File tree

1 file changed

+93
-93
lines changed

1 file changed

+93
-93
lines changed

GRDB/Core/WALSnapshot.swift

Lines changed: 93 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,108 @@
11
#if (SQLITE_ENABLE_SNAPSHOT || (!GRDBCUSTOMSQLITE && !GRDBCIPHER)) && !os(Linux)
2-
// Import C SQLite functions
3-
#if SWIFT_PACKAGE
4-
import GRDBSQLite
5-
#elseif GRDBCIPHER
6-
import SQLCipher
7-
#elseif !GRDBCUSTOMSQLITE && !GRDBCIPHER
8-
import SQLite3
9-
#endif
10-
11-
/// An instance of WALSnapshot records the state of a WAL mode database for some
12-
/// specific point in history.
13-
///
14-
/// We use `WALSnapshot` to help `ValueObservation` check for changes
15-
/// that would happen between the initial fetch, and the start of the
16-
/// actual observation. This class has no other purpose, and is not intended to
17-
/// become public.
18-
///
19-
/// It does not work with SQLCipher, because SQLCipher does not support
20-
/// `SQLITE_ENABLE_SNAPSHOT` correctly: we have linker errors.
21-
/// See <https://github.com/ericsink/SQLitePCL.raw/issues/452>.
22-
///
23-
/// With custom SQLite builds, it only works if `SQLITE_ENABLE_SNAPSHOT`
24-
/// is defined.
25-
///
26-
/// With system SQLite, it works because the SDK exposes the C apis and
27-
/// since XCode 14.
28-
///
29-
/// Yes, this is an awfully complex logic.
30-
///
31-
/// See <https://www.sqlite.org/c3ref/snapshot.html>.
32-
final class WALSnapshot: @unchecked Sendable {
33-
// @unchecked because sqlite3_snapshot has no threading requirements.
34-
// <https://www.sqlite.org/c3ref/snapshot.html>
35-
let sqliteSnapshot: UnsafeMutablePointer<sqlite3_snapshot>
36-
37-
init(_ db: Database) throws {
38-
var sqliteSnapshot: UnsafeMutablePointer<sqlite3_snapshot>?
39-
let code = withUnsafeMutablePointer(to: &sqliteSnapshot) {
40-
return sqlite3_snapshot_get(db.sqliteConnection, "main", $0)
41-
}
42-
guard code == SQLITE_OK else {
43-
// <https://www.sqlite.org/c3ref/snapshot_get.html>
44-
//
45-
// > The following must be true for sqlite3_snapshot_get() to succeed. [...]
46-
// >
47-
// > 1. The database handle must not be in autocommit mode.
48-
// > 2. Schema S of database connection D must be a WAL
49-
// > mode database.
50-
// > 3. There must not be a write transaction open on schema S
51-
// > of database connection D.
52-
// > 4. One or more transactions must have been written to the
53-
// > current wal file since it was created on disk (by any
54-
// > connection). This means that a snapshot cannot be taken
55-
// > on a wal mode database with no wal file immediately
56-
// > after it is first opened. At least one transaction must
57-
// > be written to it first.
2+
// Import C SQLite functions
3+
#if SWIFT_PACKAGE
4+
import GRDBSQLite
5+
#elseif GRDBCIPHER
6+
import SQLCipher
7+
#elseif !GRDBCUSTOMSQLITE && !GRDBCIPHER
8+
import SQLite3
9+
#endif
5810

59-
// Test condition 1:
60-
if sqlite3_get_autocommit(db.sqliteConnection) != 0 {
61-
throw DatabaseError(
62-
resultCode: code,
63-
message: """
64-
Can't create snapshot because database is in autocommit mode.
65-
""")
66-
}
11+
/// An instance of WALSnapshot records the state of a WAL mode database for some
12+
/// specific point in history.
13+
///
14+
/// We use `WALSnapshot` to help `ValueObservation` check for changes
15+
/// that would happen between the initial fetch, and the start of the
16+
/// actual observation. This class has no other purpose, and is not intended to
17+
/// become public.
18+
///
19+
/// It does not work with SQLCipher, because SQLCipher does not support
20+
/// `SQLITE_ENABLE_SNAPSHOT` correctly: we have linker errors.
21+
/// See <https://github.com/ericsink/SQLitePCL.raw/issues/452>.
22+
///
23+
/// With custom SQLite builds, it only works if `SQLITE_ENABLE_SNAPSHOT`
24+
/// is defined.
25+
///
26+
/// With system SQLite, it works because the SDK exposes the C apis and
27+
/// since XCode 14.
28+
///
29+
/// Yes, this is an awfully complex logic.
30+
///
31+
/// See <https://www.sqlite.org/c3ref/snapshot.html>.
32+
final class WALSnapshot: @unchecked Sendable {
33+
// @unchecked because sqlite3_snapshot has no threading requirements.
34+
// <https://www.sqlite.org/c3ref/snapshot.html>
35+
let sqliteSnapshot: UnsafeMutablePointer<sqlite3_snapshot>
6736

68-
// Test condition 2:
69-
if let journalMode = try? String.fetchOne(db, sql: "PRAGMA journal_mode"),
70-
journalMode != "wal"
71-
{
72-
throw DatabaseError(
73-
resultCode: code,
74-
message: """
75-
Can't create snapshot because database is not in WAL mode.
76-
""")
77-
}
37+
init(_ db: Database) throws {
38+
var sqliteSnapshot: UnsafeMutablePointer<sqlite3_snapshot>?
39+
let code = withUnsafeMutablePointer(to: &sqliteSnapshot) {
40+
return sqlite3_snapshot_get(db.sqliteConnection, "main", $0)
41+
}
42+
guard code == SQLITE_OK else {
43+
// <https://www.sqlite.org/c3ref/snapshot_get.html>
44+
//
45+
// > The following must be true for sqlite3_snapshot_get() to succeed. [...]
46+
// >
47+
// > 1. The database handle must not be in autocommit mode.
48+
// > 2. Schema S of database connection D must be a WAL
49+
// > mode database.
50+
// > 3. There must not be a write transaction open on schema S
51+
// > of database connection D.
52+
// > 4. One or more transactions must have been written to the
53+
// > current wal file since it was created on disk (by any
54+
// > connection). This means that a snapshot cannot be taken
55+
// > on a wal mode database with no wal file immediately
56+
// > after it is first opened. At least one transaction must
57+
// > be written to it first.
7858

79-
// Condition 3 can't happen because GRDB only calls this
80-
// initializer from read transactions.
81-
//
82-
// Hence it is condition 4 that is false:
59+
// Test condition 1:
60+
if sqlite3_get_autocommit(db.sqliteConnection) != 0 {
8361
throw DatabaseError(
8462
resultCode: code,
8563
message: """
86-
Can't create snapshot from a missing or empty wal file.
64+
Can't create snapshot because database is in autocommit mode.
8765
""")
8866
}
89-
guard let sqliteSnapshot else {
90-
throw DatabaseError(resultCode: .SQLITE_INTERNAL) // WTF SQLite?
67+
68+
// Test condition 2:
69+
if let journalMode = try? String.fetchOne(db, sql: "PRAGMA journal_mode"),
70+
journalMode != "wal"
71+
{
72+
throw DatabaseError(
73+
resultCode: code,
74+
message: """
75+
Can't create snapshot because database is not in WAL mode.
76+
""")
9177
}
92-
self.sqliteSnapshot = sqliteSnapshot
93-
}
9478

95-
deinit {
96-
sqlite3_snapshot_free(sqliteSnapshot)
79+
// Condition 3 can't happen because GRDB only calls this
80+
// initializer from read transactions.
81+
//
82+
// Hence it is condition 4 that is false:
83+
throw DatabaseError(
84+
resultCode: code,
85+
message: """
86+
Can't create snapshot from a missing or empty wal file.
87+
""")
9788
}
98-
99-
/// Compares two WAL snapshots.
100-
///
101-
/// `a.compare(b) < 0` iff a is older than b.
102-
///
103-
/// See <https://www.sqlite.org/c3ref/snapshot_cmp.html>.
104-
func compare(_ other: WALSnapshot) -> CInt {
105-
sqlite3_snapshot_cmp(sqliteSnapshot, other.sqliteSnapshot)
89+
guard let sqliteSnapshot else {
90+
throw DatabaseError(resultCode: .SQLITE_INTERNAL) // WTF SQLite?
10691
}
92+
self.sqliteSnapshot = sqliteSnapshot
93+
}
94+
95+
deinit {
96+
sqlite3_snapshot_free(sqliteSnapshot)
97+
}
98+
99+
/// Compares two WAL snapshots.
100+
///
101+
/// `a.compare(b) < 0` iff a is older than b.
102+
///
103+
/// See <https://www.sqlite.org/c3ref/snapshot_cmp.html>.
104+
func compare(_ other: WALSnapshot) -> CInt {
105+
sqlite3_snapshot_cmp(sqliteSnapshot, other.sqliteSnapshot)
107106
}
107+
}
108108
#endif

0 commit comments

Comments
 (0)