Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 17 | Rev 22 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 17 Rev 19
Línea 9... Línea 9...
9
import UIKit
9
import UIKit
10
import SQLite3
10
import SQLite3
Línea 11... Línea 11...
11
 
11
 
12
class UserNotificationDao {
12
class UserNotificationDao {
13
    private let SQLITE_TRANSIENT = unsafeBitCast(-1, to: sqlite3_destructor_type.self)
13
    private let SQLITE_TRANSIENT = unsafeBitCast(-1, to: sqlite3_destructor_type.self)
Línea 14... Línea 14...
14
    private var db : OpaquePointer?
14
    private var database = Database.sharedInstance
15
    
15
    
Línea 16... Línea 16...
16
    static let sharedInstance: UserNotificationDao = {
16
    static let sharedInstance: UserNotificationDao = {
17
           let instance = UserNotificationDao()
17
           let instance = UserNotificationDao()
18
           
18
           
19
           // setup code
-
 
20
           return instance
-
 
21
    }()
-
 
22
    
-
 
Línea 23... Línea 19...
23
    init() {
19
           // setup code
-
 
20
           return instance
24
        self.db = Database.sharedInstance.open()
21
    }()
Línea 25... Línea 22...
25
    }
22
 
26
 
23
    func selectById(id : Int)-> UserNotificationModel {
27
    func selectById(id : Int)-> UserNotificationModel {
24
        let db = database.open()
Línea 61... Línea 58...
61
                model.addedOn = String(describing: String(cString: sqlite3_column_text(statement, 7)))
58
                model.addedOn = String(describing: String(cString: sqlite3_column_text(statement, 7)))
Línea 62... Línea 59...
62
                
59
                
Línea 63... Línea 60...
63
               //print("\nSuccessfully get record")
60
               //print("\nSuccessfully get record")
-
 
61
                
-
 
62
            }
64
                
63
        } else {
65
            }
64
            database.printError()
66
        }
65
        }
67
        sqlite3_finalize(statement)
66
        sqlite3_finalize(statement)
Línea 68... Línea 67...
68
        return model
67
        return model
-
 
68
    }
69
    }
69
 
Línea 70... Línea 70...
70
 
70
 
71
 
71
    func selectAll()-> [UserNotificationModel] {
72
    func selectAll()-> [UserNotificationModel] {
72
        let db = database.open()
Línea 108... Línea 108...
108
               
108
               
Línea 109... Línea 109...
109
                
109
                
-
 
110
                records.append(model)
-
 
111
                
110
                records.append(model)
112
            }
111
                
113
        } else {
112
            }
114
            database.printError()
113
        }
115
        }
Línea 114... Línea 116...
114
        sqlite3_finalize(statement)
116
        sqlite3_finalize(statement)
-
 
117
        return records
115
        return records
118
    }
Línea 116... Línea 119...
116
    }
119
 
117
 
120
    
Línea 129... Línea 132...
129
                count = Int(sqlite3_column_int(statement, 0))
132
                count = Int(sqlite3_column_int(statement, 0))
130
                //print("\(count)")
133
                //print("\(count)")
131
            }
134
            }
132
        } else {
135
        } else {
133
            count = -1
136
            count = -1
-
 
137
            database.printError()
134
        }
138
        }
135
        sqlite3_finalize(statement)
139
        sqlite3_finalize(statement)
136
        return count
140
        return count
137
    }
141
    }
Línea 138... Línea 142...
138
    
142
    
-
 
143
    func insert(userNotification : UserNotificationModel) -> Int {
139
    func insert(userNotification : UserNotificationModel) -> Int {
144
        let db = database.open()
140
        var result : Int = 0
145
        var result : Int = 0
141
        var query = "INSERT INTO " + Constants.TABLE_USER_NOTIFICATION + " ( "
146
        var query = "INSERT INTO " + Constants.TABLE_USER_NOTIFICATION + " ( "
142
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_USER_UUID + ", "
147
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_USER_UUID + ", "
143
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_TITLE + ", "
148
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_TITLE + ", "
Línea 160... Línea 165...
160
   
165
   
161
            if (sqlite3_step(statement) == SQLITE_DONE) {
166
            if (sqlite3_step(statement) == SQLITE_DONE) {
162
                result = Int(sqlite3_last_insert_rowid(db))
167
                result = Int(sqlite3_last_insert_rowid(db))
163
            } else {
168
            } else {
-
 
169
                 print("No se pudo insertar el registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
-
 
170
                
Línea 164... Línea 171...
164
                 print("No se pudo insertar el registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
171
                database.printError()
165
             
172
             
166
            }
173
            }
-
 
174
        } else {
-
 
175
            print("Fallo la preparación del insertar un registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
167
        } else {
176
            
Línea 168... Línea 177...
168
            print("Fallo la preparación del insertar un registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
177
            database.printError()
Línea 169... Línea 178...
169
        }
178
        }
170
       
179
       
Línea 171... Línea 180...
171
        sqlite3_finalize(statement)
180
        sqlite3_finalize(statement)
-
 
181
        
172
        
182
        return result
173
        return result
183
    }
174
    }
184
 
175
 
185
    func markViewed(id : Int) {
176
    func markViewed(id : Int) {
186
        let db = database.open()
177
        var query = "UPDATE " + Constants.TABLE_USER_NOTIFICATION
187
        var query = "UPDATE " + Constants.TABLE_USER_NOTIFICATION
-
 
188
        query = query + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_ID + " = '\(id)';"
-
 
189
        var statement : OpaquePointer? = nil
178
        query = query + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_ID + " = '\(id)';"
190
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
179
        var statement : OpaquePointer? = nil
191
            if (sqlite3_step(statement) != SQLITE_DONE) {
180
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
192
                print("No se pudo actualizar un registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION) ")
-
 
193
                
-
 
194
                database.printError()
181
            if (sqlite3_step(statement) != SQLITE_DONE) {
195
            }
182
                print("No se pudo actualizar un registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION) ")
196
        } else {
183
            }
197
            print("Fallo la preparación de la actualización de un registro en la tabla \(Constants.TABLE_USER_NOTIFICATION) ")
Línea 184... Línea 198...
184
        } else {
198
            
-
 
199
            database.printError()
185
            print("Fallo la preparación de la actualización de un registro en la tabla \(Constants.TABLE_USER_NOTIFICATION) ")
200
        }
186
        }
201
        sqlite3_finalize(statement)
187
        sqlite3_finalize(statement)
202
    }
188
    }
203
    
189
    
204
    func markViewedAllPending() {
190
    func markViewedAllPending() {
205
        let db = database.open()
-
 
206
        var query = "UPDATE " + Constants.TABLE_USER_NOTIFICATION
-
 
207
        query = query + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_VIEWED + " = 0;"
191
        var query = "UPDATE " + Constants.TABLE_USER_NOTIFICATION
208
        var statement : OpaquePointer? = nil
192
        query = query + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_VIEWED + " = 0;"
209
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
193
        var statement : OpaquePointer? = nil
210
            if (sqlite3_step(statement) != SQLITE_DONE) {
-
 
211
                print("No se pudo actualizar un registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION) ")
-
 
212
                
194
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
213
                database.printError()
195
            if (sqlite3_step(statement) != SQLITE_DONE) {
214
            }
196
                print("No se pudo actualizar un registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION) ")
215
        } else {
Línea 197... Línea 216...
197
            }
216
            print("Fallo la preparación de la actualización de un registro en la tabla \(Constants.TABLE_USER_NOTIFICATION) ")
-
 
217
            
198
        } else {
218
            database.printError()
199
            print("Fallo la preparación de la actualización de un registro en la tabla \(Constants.TABLE_USER_NOTIFICATION) ")
219
        }
200
        }
220
        sqlite3_finalize(statement)
201
        sqlite3_finalize(statement)
221
    }
202
    }
222
 
203
 
223
    func remove(id: Int) {
-
 
224
        let db = database.open()
-
 
225
        let query = "DELETE FROM " + Constants.TABLE_USER_NOTIFICATION
204
    func remove(id: Int) {
226
            + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_ID + " = '\(id)' ;"
205
        let query = "DELETE FROM " + Constants.TABLE_USER_NOTIFICATION
227
        var statement : OpaquePointer? = nil
206
            + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_ID + " = '\(id)' ;"
228
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
-
 
229
            if (sqlite3_step(statement) != SQLITE_DONE) {
-
 
230
                print("No se pudo borrar el registro con el id: \(id) en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
207
        var statement : OpaquePointer? = nil
231
                
208
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
232
                database.printError()
209
            if (sqlite3_step(statement) != SQLITE_DONE) {
233
            }
Línea 210... Línea 234...
210
                print("No se pudo borrar el registro con el id: \(id) en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
234
        } else {
-
 
235
            print("Fallo la preparación del borrado del registro con el id: \(id) en la tabla: \(Constants.TABLE_USER_NOTIFICATION)" )
211
            }
236
            
212
        } else {
237
            database.printError()
213
            print("Fallo la preparación del borrado del registro con el id: \(id) en la tabla: \(Constants.TABLE_USER_NOTIFICATION)" )
238
        }
214
        }
239
        sqlite3_finalize(statement)
215
        sqlite3_finalize(statement)
240
    }
-
 
241
 
-
 
242
    func removeAll() {
216
    }
243
        let db = database.open()
217
 
244
        let query = "DELETE FROM " + Constants.TABLE_USER_NOTIFICATION + ";"
218
    func removeAll() {
245
        var statement : OpaquePointer? = nil
-
 
246
        if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
-
 
247
            if sqlite3_step(statement) != SQLITE_DONE {
219
        let query = "DELETE FROM " + Constants.TABLE_USER_NOTIFICATION + ";"
248
                print("Fallo el borrado de todos los registros en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
220
        var statement : OpaquePointer? = nil
249
                
221
        if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
250
                database.printError()
Línea 222... Línea 251...
222
            if sqlite3_step(statement) != SQLITE_DONE {
251
            }
223
                print("Fallo el borrado de todos los registros en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
252
        } else {
-
 
253
            print("No se pudo preparar el borrado de todos los registros en la tabla: \(Constants.TABLE_USER_NOTIFICATION) ")
224
            }
254
            
225
        } else {
255
            database.printError()
226
            print("No se pudo preparar el borrado de todos los registros en la tabla: \(Constants.TABLE_USER_NOTIFICATION) ")
256
        }
227
        }
257
        sqlite3_finalize(statement)
228
        sqlite3_finalize(statement)
258
    }
229
    }
259
    
230
    
260
    func removeAllUserUuidNotEqual(userUuid : String)
-
 
261
    {
-
 
262
        let db = database.open()
231
    func removeAllUserUuidNotEqual(userUuid : String)
263
        let query = "DELETE FROM " +  Constants.TABLE_USER_NOTIFICATION +
232
    {
264
            " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_USER_UUID + " <> '\(userUuid)' ;"
233
        let query = "DELETE FROM " +  Constants.TABLE_USER_NOTIFICATION +
265
        var statement : OpaquePointer? = nil
-
 
266
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
-
 
267
            if sqlite3_step(statement) != SQLITE_DONE {
234
            " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_USER_UUID + " <> '\(userUuid)' ;"
268
                print("No se pudo borrar todos los registros en la tabla: \(Constants.TABLE_USER_NOTIFICATION)" +
235
        var statement : OpaquePointer? = nil
269
                " de los usuarios diferentes de : \(userUuid)" )
236
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
270
                
Línea 237... Línea 271...
237
            if sqlite3_step(statement) != SQLITE_DONE {
271
                database.printError()