@@ -38,19 +38,19 @@ import SQLite3
3838///
3939/// See: <https://www.sqlite.org/backup.html>
4040public final class Backup {
41-
41+
4242 /// The name of the database to backup
4343 public enum DatabaseName {
44-
44+
4545 /// The main database
4646 case main
47-
47+
4848 /// The temporary database
4949 case temp
50-
50+
5151 /// A database added to the connection with ATTACH statement
5252 case attached( name: String )
53-
53+
5454 var name : String {
5555 switch self {
5656 case . main:
@@ -62,16 +62,16 @@ public final class Backup {
6262 }
6363 }
6464 }
65-
65+
6666 /// Number of pages to copy while performing a backup step
6767 public enum Pages {
68-
68+
6969 /// Indicates all remaining pages should be copied
7070 case all
71-
71+
7272 /// Indicates the maximal number of pages to be copied in single step
7373 case limited( number: Int32 )
74-
74+
7575 var number : Int32 {
7676 switch self {
7777 case . all:
@@ -81,63 +81,60 @@ public final class Backup {
8181 }
8282 }
8383 }
84-
84+
8585 /// Total number of pages to copy
8686 ///
8787 /// See: <https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backuppagecount>
8888 public var pageCount : Int32 {
8989 return handle. map { sqlite3_backup_pagecount ( $0) } ?? 0
9090 }
91-
91+
9292 /// Number of remaining pages to copy.
9393 ///
9494 /// See: <https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupremaining>
9595 public var remainingPages : Int32 {
9696 return handle. map { sqlite3_backup_remaining ( $0) } ?? 0
9797 }
98-
98+
9999 private let targetConnection : Connection
100100 private let sourceConnection : Connection
101-
101+
102102 private var handle : OpaquePointer ?
103-
103+
104104 /// Initializes a new SQLite backup.
105105 ///
106106 /// - Parameters:
107107 ///
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- ///
114108 /// - sourceConnection: The connection to the database to backup.
115- ///
116109 /// - sourceName: The name of the database to backup.
117- ///
110+ /// Default: `.main`.
111+ ///
112+ /// - targetConnection: The connection to the database to save backup into.
113+ /// - targetName: The name of the database to save backup into.
118114 /// Default: `.main`.
119115 ///
120116 /// - Returns: A new database backup.
121117 ///
122118 /// 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-
119+ public init ( sourceConnection : Connection ,
120+ sourceName : DatabaseName = . main,
121+ targetConnection : Connection ,
122+ targetName : DatabaseName = . main) throws {
123+
128124 self . targetConnection = targetConnection
129125 self . sourceConnection = sourceConnection
130-
126+
131127 self . handle = sqlite3_backup_init ( targetConnection. handle,
132128 targetName. name,
133129 sourceConnection. handle,
134130 sourceName. name)
135-
136- if self . handle == nil , let error = Result ( errorCode: sqlite3_errcode ( targetConnection. handle) , connection: targetConnection) {
131+
132+ if handle == nil , let error = Result ( errorCode: sqlite3_errcode ( targetConnection. handle) ,
133+ connection: targetConnection) {
137134 throw error
138135 }
139136 }
140-
137+
141138 /// Performs a backup step.
142139 ///
143140 /// - Parameter pagesToCopy: The maximal number of pages to copy in one step
@@ -147,29 +144,29 @@ public final class Backup {
147144 /// See: <https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupstep>
148145 public func step( pagesToCopy pages: Pages = . all) throws {
149146 let status = sqlite3_backup_step ( handle, pages. number)
150-
147+
151148 guard status != SQLITE_DONE else {
152149 finish ( )
153150 return
154151 }
155-
152+
156153 if let error = Result ( errorCode: status, connection: targetConnection) {
157154 throw error
158155 }
159156 }
160-
157+
161158 /// Finalizes backup.
162159 ///
163160 /// See: <https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupfinish>
164161 public func finish( ) {
165162 guard let handle = self . handle else {
166163 return
167164 }
168-
165+
169166 sqlite3_backup_finish ( handle)
170167 self . handle = nil
171168 }
172-
169+
173170 deinit {
174171 finish ( )
175172 }
0 commit comments