Proyectos de Subversion Iphone Microlearning - Inconcert

Rev

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

Rev 1 Rev 15
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
    private var database = Database.sharedInstance
-
 
15
    
-
 
16
    static let sharedInstance: UserNotificationDao = {
-
 
17
           let instance = UserNotificationDao()
-
 
18
           
-
 
19
           // setup code
-
 
20
           return instance
-
 
21
    }()
14
    private let db : OpaquePointer?
22
 
-
 
-
 
15
    
23
    func selectById(id : Int)-> UserNotificationModel {
16
    func selectById(id : Int)-> UserNotificationModel {
Línea 24... Línea 17...
24
        let db = database.open()
17
        
25
        var model = UserNotificationModel ()
18
        var model = UserNotificationModel ()
26
        
19
        
Línea 65... Línea 58...
65
                
58
                
Línea 66... Línea 59...
66
               //print("\nSuccessfully get record")
59
               //print("\nSuccessfully get record")
67
                
60
                
68
            }
-
 
-
 
61
            }
69
        } else {
62
        } else {
70
            database.printError()
63
           
71
        }
64
        }
72
        sqlite3_finalize(statement)
65
        sqlite3_finalize(statement)
Línea 73... Línea 66...
73
        return model
66
        return model
74
    }
-
 
-
 
67
    }
75
    
68
    
Línea 76... Línea 69...
76
    func selectAllDistinctDateByUserUuid(userUuid : String) -> [String] {
69
    func selectAllDistinctDateByUserUuid(userUuid : String) -> [String] {
77
        let db = database.open()
70
        
78
        var records = [String]()
71
        var records = [String]()
Línea 93... Línea 86...
93
               
86
               
Línea 94... Línea 87...
94
                records.append(date)
87
                records.append(date)
95
                
88
                
96
            }
-
 
-
 
89
            }
97
        } else {
90
        } else {
98
            database.printError()
91
           
99
        }
92
        }
100
        sqlite3_finalize(statement)
93
        sqlite3_finalize(statement)
Línea 101... Línea 94...
101
        return records
94
        return records
102
    }
-
 
-
 
95
    }
103
 
96
 
Línea 104... Línea 97...
104
 
97
 
105
    func selectAllByUserUuidAndDate(userUuid : String, date : String)-> [UserNotificationModel] {
98
    func selectAllByUserUuidAndDate(userUuid : String, date : String)-> [UserNotificationModel] {
106
        let db = database.open()
99
        
Línea 151... Línea 144...
151
                
144
                
Línea 152... Línea 145...
152
                records.append(model)
145
                records.append(model)
153
                
146
                
154
            }
-
 
-
 
147
            }
155
        } else {
148
        } else {
156
            database.printError()
149
           
157
        }
150
        }
158
        sqlite3_finalize(statement)
151
        sqlite3_finalize(statement)
Línea 159... Línea 152...
159
        return records
152
        return records
160
    }
-
 
-
 
153
    }
161
    
154
    
Línea 162... Línea 155...
162
    func selectAllByUserUuid(userUuid : String)-> [UserNotificationModel] {
155
    func selectAllByUserUuid(userUuid : String)-> [UserNotificationModel] {
163
        let db = database.open()
156
        
164
        var records = [UserNotificationModel]()
157
        var records = [UserNotificationModel]()
Línea 207... Línea 200...
207
                
200
                
Línea 208... Línea 201...
208
                records.append(model)
201
                records.append(model)
209
                
202
                
210
            }
-
 
-
 
203
            }
211
        } else {
204
        } else {
212
            database.printError()
205
           
213
        }
206
        }
214
        sqlite3_finalize(statement)
207
        sqlite3_finalize(statement)
Línea 215... Línea 208...
215
        return records
208
        return records
216
    }
-
 
-
 
209
    }
217
    
210
    
218
    
211
    
Línea 219... Línea 212...
219
 
212
 
220
    
213
    
Línea 233... Línea 226...
233
                count = Int(sqlite3_column_int(statement, 0))
226
                count = Int(sqlite3_column_int(statement, 0))
234
                //print("\(count)")
227
                //print("\(count)")
235
            }
228
            }
236
        } else {
229
        } else {
237
            count = -1
230
            count = -1
238
            database.printError()
-
 
-
 
231
           
239
        }
232
        }
240
        sqlite3_finalize(statement)
233
        sqlite3_finalize(statement)
241
        return count
234
        return count
242
    }
235
    }
Línea 243... Línea 236...
243
    
236
    
244
    func insert(userNotification : UserNotificationModel) -> Int {
-
 
-
 
237
    func insert(userNotification : UserNotificationModel) -> Int {
245
        let db = database.open()
238
        
246
        var result : Int = 0
239
        var result : Int = 0
247
        var query = "INSERT INTO " + Constants.TABLE_USER_NOTIFICATION + " ( "
240
        var query = "INSERT INTO " + Constants.TABLE_USER_NOTIFICATION + " ( "
248
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_USER_UUID + ", "
241
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_USER_UUID + ", "
249
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_TITLE + ", "
242
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_TITLE + ", "
Línea 267... Línea 260...
267
            sqlite3_bind_text(statement, 6, userNotification.command, -1, SQLITE_TRANSIENT)
260
            sqlite3_bind_text(statement, 6, userNotification.command, -1, SQLITE_TRANSIENT)
268
            sqlite3_bind_text(statement, 7, userNotification.dateOn, -1, SQLITE_TRANSIENT)
261
            sqlite3_bind_text(statement, 7, userNotification.dateOn, -1, SQLITE_TRANSIENT)
269
            sqlite3_bind_text(statement, 8, userNotification.timeOn , -1, SQLITE_TRANSIENT)
262
            sqlite3_bind_text(statement, 8, userNotification.timeOn , -1, SQLITE_TRANSIENT)
Línea 270... Línea 263...
270
            
263
            
271
            if (sqlite3_step(statement) == SQLITE_DONE) {
264
            if (sqlite3_step(statement) == SQLITE_DONE) {
272
                result = Int(sqlite3_last_insert_rowid(db))
265
                result = Int(sqlite3_last_insert_rowid(conn))
273
            } else {
266
            } else {
Línea 274... Línea -...
274
                 print("No se pudo insertar el registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
-
 
-
 
267
                 print("No se pudo insertar el registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
Línea 275... Línea 268...
275
                
268
                
276
                database.printError()
269
               
277
             
270
             
Línea 278... Línea -...
278
            }
-
 
-
 
271
            }
279
        } else {
272
        } else {
Línea 280... Línea 273...
280
            print("Fallo la preparación del insertar un registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
273
            print("Fallo la preparación del insertar un registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
Línea 281... Línea 274...
281
            
274
            
282
            database.printError()
275
           
Línea 283... Línea 276...
283
        }
276
        }
284
       
-
 
-
 
277
       
285
        sqlite3_finalize(statement)
278
        sqlite3_finalize(statement)
286
        
279
        
287
        return result
280
        return result
288
    }
281
    }
289
 
282
 
290
    func markViewed(id : Int) {
283
    func markViewed(id : Int) {
291
        let db = database.open()
284
        
Línea 292... Línea -...
292
        var query = "UPDATE " + Constants.TABLE_USER_NOTIFICATION
-
 
-
 
285
        var query = "UPDATE " + Constants.TABLE_USER_NOTIFICATION
293
        query = query + " SET " + Constants.TABLE_USER_NOTIFICATION_FIELD_VIEWED + "  1 "
286
        query = query + " SET " + Constants.TABLE_USER_NOTIFICATION_FIELD_VIEWED + "  1 "
294
        query = query + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_ID + " = '\(id)';"
287
        query = query + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_ID + " = '\(id)';"
295
        var statement : OpaquePointer? = nil
288
        var statement : OpaquePointer? = nil
Línea 296... Línea -...
296
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
-
 
-
 
289
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
297
            if (sqlite3_step(statement) != SQLITE_DONE) {
290
            if (sqlite3_step(statement) != SQLITE_DONE) {
298
                print("No se pudo actualizar un registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION) ")
291
                print("No se pudo actualizar un registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION) ")
299
                
292
                
Línea 300... Línea 293...
300
                database.printError()
293
               
301
            }
-
 
-
 
294
            }
302
        } else {
295
        } else {
303
            print("Fallo la preparación de la actualización de un registro en la tabla \(Constants.TABLE_USER_NOTIFICATION) ")
296
            print("Fallo la preparación de la actualización de un registro en la tabla \(Constants.TABLE_USER_NOTIFICATION) ")
304
            
297
            
305
            database.printError()
298
           
306
        }
299
        }
307
        sqlite3_finalize(statement)
300
        sqlite3_finalize(statement)
308
    }
301
    }
309
    
302
    
Línea 310... Línea -...
310
    func markViewedAllPendingByUserUuid(userUuid : String) {
-
 
-
 
303
    func markViewedAllPendingByUserUuid(userUuid : String) {
311
        let db = database.open()
304
        
312
        var query = "UPDATE " + Constants.TABLE_USER_NOTIFICATION
305
        var query = "UPDATE " + Constants.TABLE_USER_NOTIFICATION
313
        query = query + " SET " + Constants.TABLE_USER_NOTIFICATION_FIELD_VIEWED + "  1 "
306
        query = query + " SET " + Constants.TABLE_USER_NOTIFICATION_FIELD_VIEWED + "  1 "
Línea 314... Línea -...
314
        query = query + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_VIEWED + " = 0 "
-
 
-
 
307
        query = query + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_VIEWED + " = 0 "
315
        query = query + " AND " + Constants.TABLE_USER_NOTIFICATION_FIELD_USER_UUID + " = '\(userUuid)'; "
308
        query = query + " AND " + Constants.TABLE_USER_NOTIFICATION_FIELD_USER_UUID + " = '\(userUuid)'; "
316
        var statement : OpaquePointer? = nil
309
        var statement : OpaquePointer? = nil
317
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
310
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
Línea 318... Línea 311...
318
            if (sqlite3_step(statement) != SQLITE_DONE) {
311
            if (sqlite3_step(statement) != SQLITE_DONE) {
319
                print("No se pudo actualizar un registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION) ")
-
 
-
 
312
                print("No se pudo actualizar un registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION) ")
320
                
313
                
321
                database.printError()
314
               
322
            }
315
            }
323
        } else {
316
        } else {
324
            print("Fallo la preparación de la actualización de un registro en la tabla \(Constants.TABLE_USER_NOTIFICATION) ")
317
            print("Fallo la preparación de la actualización de un registro en la tabla \(Constants.TABLE_USER_NOTIFICATION) ")
325
            
318
            
326
            database.printError()
319
           
327
        }
320
        }
328
        sqlite3_finalize(statement)
321
        sqlite3_finalize(statement)
Línea 329... Línea -...
329
    }
-
 
-
 
322
    }
330
    
323
    
331
    func markViewedAllPendingByUserUuidAndCommand(userUuid : String, command : String) {
324
    func markViewedAllPendingByUserUuidAndCommand(userUuid : String, command : String) {
332
        let db = database.open()
325
        
Línea 333... Línea -...
333
        var query = "UPDATE " + Constants.TABLE_USER_NOTIFICATION
-
 
-
 
326
        var query = "UPDATE " + Constants.TABLE_USER_NOTIFICATION
334
        query = query + " SET " + Constants.TABLE_USER_NOTIFICATION_FIELD_VIEWED + " = 1 "
327
        query = query + " SET " + Constants.TABLE_USER_NOTIFICATION_FIELD_VIEWED + " = 1 "
335
        query = query + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_VIEWED + " = 0 "
328
        query = query + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_VIEWED + " = 0 "
336
        query = query + " AND " + Constants.TABLE_USER_NOTIFICATION_FIELD_USER_UUID + " = '\(userUuid)'; "
329
        query = query + " AND " + Constants.TABLE_USER_NOTIFICATION_FIELD_USER_UUID + " = '\(userUuid)'; "
Línea 360... Línea 353...
360
        let dateFormatter = DateFormatter()
353
        let dateFormatter = DateFormatter()
361
        dateFormatter.dateFormat = Constants.FORMAT_DATE_YMD
354
        dateFormatter.dateFormat = Constants.FORMAT_DATE_YMD
Línea 362... Línea 355...
362
        
355
        
Línea 363... Línea -...
363
        let sDate = dateFormatter.string(from: date)
-
 
-
 
356
        let sDate = dateFormatter.string(from: date)
364
        
357
        
365
        let db = database.open()
358
        
366
        var query = "DELETE FROM " + Constants.TABLE_USER_NOTIFICATION
359
        var query = "DELETE FROM " + Constants.TABLE_USER_NOTIFICATION
Línea 367... Línea 360...
367
        query = query + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_USER_UUID + " = '\(userUuid)' "
360
        query = query + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_USER_UUID + " = '\(userUuid)' "
368
        query = query + " AND " + Constants.TABLE_USER_NOTIFICATION_FIELD_DATE_ON + " < '\(sDate)' "
361
        query = query + " AND " + Constants.TABLE_USER_NOTIFICATION_FIELD_DATE_ON + " < '\(sDate)' "
369
        
362
        
370
        var statement : OpaquePointer? = nil
363
        var statement : OpaquePointer? = nil
Línea 371... Línea -...
371
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
-
 
-
 
364
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
372
            if (sqlite3_step(statement) != SQLITE_DONE) {
365
            if (sqlite3_step(statement) != SQLITE_DONE) {
373
                print("No se pudo borrar los registros con fecha menor a : \(sDate) en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
366
                print("No se pudo borrar los registros con fecha menor a : \(sDate) en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
374
                
367
                
Línea 375... Línea -...
375
                database.printError()
-
 
-
 
368
               
376
            }
369
            }
377
        } else {
370
        } else {
Línea 378... Línea 371...
378
            print("Fallo la preparación del borrado de los registros con fecha menor a : \(sDate) en la tabla: \(Constants.TABLE_USER_NOTIFICATION)" )
371
            print("Fallo la preparación del borrado de los registros con fecha menor a : \(sDate) en la tabla: \(Constants.TABLE_USER_NOTIFICATION)" )
Línea 379... Línea 372...
379
            
372
            
380
            database.printError()
-
 
-
 
373
           
381
        }
374
        }
382
        sqlite3_finalize(statement)
375
        sqlite3_finalize(statement)
383
        
376
        
384
        
377
        
385
    }
378
    }
386
 
379
 
Línea 387... Línea -...
387
    func remove(id: Int) {
-
 
-
 
380
    func remove(id: Int) {
388
        let db = database.open()
381
        
389
        let query = "DELETE FROM " + Constants.TABLE_USER_NOTIFICATION
382
        let query = "DELETE FROM " + Constants.TABLE_USER_NOTIFICATION
390
            + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_ID + " = '\(id)' ;"
383
            + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_ID + " = '\(id)' ;"
Línea 391... Línea -...
391
        var statement : OpaquePointer? = nil
-
 
-
 
384
        var statement : OpaquePointer? = nil
392
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
385
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
393
            if (sqlite3_step(statement) != SQLITE_DONE) {
386
            if (sqlite3_step(statement) != SQLITE_DONE) {
394
                print("No se pudo borrar el registro con el id: \(id) en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
387
                print("No se pudo borrar el registro con el id: \(id) en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
Línea 395... Línea 388...
395
                
388
                
396
                database.printError()
389
               
397
            }
-
 
-
 
390
            }
398
        } else {
391
        } else {
399
            print("Fallo la preparación del borrado del registro con el id: \(id) en la tabla: \(Constants.TABLE_USER_NOTIFICATION)" )
392
            print("Fallo la preparación del borrado del registro con el id: \(id) en la tabla: \(Constants.TABLE_USER_NOTIFICATION)" )
400
            
393
            
401
            database.printError()
394
           
402
        }
395
        }
403
        sqlite3_finalize(statement)
396
        sqlite3_finalize(statement)
404
    }
397
    }
Línea 405... Línea -...
405
 
-
 
-
 
398
 
406
    
399
    
407
    
400
    
408
    func removeAllUserUuidNotEqual(userUuid : String)
401
    func removeAllUserUuidNotEqual(userUuid : String)
Línea 409... Línea -...
409
    {
-
 
-
 
402
    {
410
        let db = database.open()
403
        
411
        let query = "DELETE FROM " +  Constants.TABLE_USER_NOTIFICATION +
404
        let query = "DELETE FROM " +  Constants.TABLE_USER_NOTIFICATION +
412
            " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_USER_UUID + " <> '\(userUuid)' ;"
405
            " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_USER_UUID + " <> '\(userUuid)' ;"
Línea 413... Línea 406...
413
        var statement : OpaquePointer? = nil
406
        var statement : OpaquePointer? = nil