|
| 1 | +// |
| 2 | +// SQLite.swift |
| 3 | +// https://github.com/stephencelis/SQLite.swift |
| 4 | +// Copyright © 2014-2015 Stephen Celis. |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in |
| 14 | +// all copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +// THE SOFTWARE. |
| 23 | +// |
| 24 | + |
| 25 | +import Foundation |
| 26 | +import Dispatch |
| 27 | +#if SQLITE_SWIFT_STANDALONE |
| 28 | +import sqlite3 |
| 29 | +#elseif SQLITE_SWIFT_SQLCIPHER |
| 30 | +import SQLCipher |
| 31 | +#elseif os(Linux) |
| 32 | +import CSQLite |
| 33 | +#else |
| 34 | +import SQLite3 |
| 35 | +#endif |
| 36 | + |
| 37 | +/// An object representing database backup. |
| 38 | +/// |
| 39 | +/// See: <https://www.sqlite.org/backup.html> |
| 40 | +public final class Backup { |
| 41 | + |
| 42 | + /// The name of the database to backup |
| 43 | + public enum DatabaseName { |
| 44 | + |
| 45 | + /// The main database |
| 46 | + case main |
| 47 | + |
| 48 | + /// The temporary database |
| 49 | + case temp |
| 50 | + |
| 51 | + /// A database added to the connection with ATTACH statement |
| 52 | + case attached(name: String) |
| 53 | + |
| 54 | + var name: String { |
| 55 | + switch self { |
| 56 | + case .main: |
| 57 | + return "main" |
| 58 | + case .temp: |
| 59 | + return "temp" |
| 60 | + case .attached(let name): |
| 61 | + return name |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /// Number of pages to copy while performing a backup step |
| 67 | + public enum Pages { |
| 68 | + |
| 69 | + /// Indicates all remaining pages should be copied |
| 70 | + case all |
| 71 | + |
| 72 | + /// Indicates the maximal number of pages to be copied in single step |
| 73 | + case limited(number: Int32) |
| 74 | + |
| 75 | + var number: Int32 { |
| 76 | + switch self { |
| 77 | + case .all: |
| 78 | + return -1 |
| 79 | + case .limited(let number): |
| 80 | + return number |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// Total number of pages to copy |
| 86 | + /// |
| 87 | + /// See: <https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backuppagecount> |
| 88 | + public var pageCount: Int32 { |
| 89 | + return handle.map { sqlite3_backup_pagecount($0) } ?? 0 |
| 90 | + } |
| 91 | + |
| 92 | + /// Number of remaining pages to copy. |
| 93 | + /// |
| 94 | + /// See: <https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupremaining> |
| 95 | + public var remainingPages: Int32 { |
| 96 | + return handle.map { sqlite3_backup_remaining($0) } ?? 0 |
| 97 | + } |
| 98 | + |
| 99 | + private let targetConnection: Connection |
| 100 | + private let sourceConnection: Connection |
| 101 | + |
| 102 | + private var handle: OpaquePointer? |
| 103 | + |
| 104 | + /// Initializes a new SQLite backup. |
| 105 | + /// |
| 106 | + /// - Parameters: |
| 107 | + /// |
| 108 | + /// - targetConnection: The connection to the database to save backup into. |
| 109 | + /// |
| 110 | + /// - targetName: The name of the database to save backup into. |
| 111 | + /// |
| 112 | + /// Default: `.main`. |
| 113 | + /// |
| 114 | + /// - sourceConnection: The connection to the database to backup. |
| 115 | + /// |
| 116 | + /// - sourceName: The name of the database to backup. |
| 117 | + /// |
| 118 | + /// Default: `.main`. |
| 119 | + /// |
| 120 | + /// - Returns: A new database backup. |
| 121 | + /// |
| 122 | + /// See: <https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupinit> |
| 123 | + public init(targetConnection: Connection, |
| 124 | + targetName: DatabaseName = .main, |
| 125 | + sourceConnection: Connection, |
| 126 | + sourceName: DatabaseName = .main) throws { |
| 127 | + |
| 128 | + self.targetConnection = targetConnection |
| 129 | + self.sourceConnection = sourceConnection |
| 130 | + |
| 131 | + self.handle = sqlite3_backup_init(targetConnection.handle, |
| 132 | + targetName.name, |
| 133 | + sourceConnection.handle, |
| 134 | + sourceName.name) |
| 135 | + |
| 136 | + if self.handle == nil, let error = Result(errorCode: sqlite3_errcode(targetConnection.handle), connection: targetConnection) { |
| 137 | + throw error |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /// Performs a backup step. |
| 142 | + /// |
| 143 | + /// - Parameter pagesToCopy: The maximal number of pages to copy in one step |
| 144 | + /// |
| 145 | + /// - Throws: `Result.Error` if step fails. |
| 146 | + // |
| 147 | + /// See: <https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupstep> |
| 148 | + public func step(pagesToCopy pages: Pages = .all) throws { |
| 149 | + let status = sqlite3_backup_step(handle, pages.number) |
| 150 | + |
| 151 | + guard status != SQLITE_DONE else { |
| 152 | + finish() |
| 153 | + return |
| 154 | + } |
| 155 | + |
| 156 | + if let error = Result(errorCode: status, connection: targetConnection) { |
| 157 | + throw error |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /// Finalizes backup. |
| 162 | + /// |
| 163 | + /// See: <https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupfinish> |
| 164 | + public func finish() { |
| 165 | + guard let handle = self.handle else { |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + sqlite3_backup_finish(handle) |
| 170 | + self.handle = nil |
| 171 | + } |
| 172 | + |
| 173 | + deinit { |
| 174 | + finish() |
| 175 | + } |
| 176 | +} |
0 commit comments