Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

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

Rev 1 Rev 19
Línea 8... Línea 8...
8
import UIKit
8
import UIKit
9
import SQLite3
9
import SQLite3
Línea 10... Línea 10...
10
 
10
 
11
class UserLogDao {
11
class UserLogDao {
12
    private let SQLITE_TRANSIENT = unsafeBitCast(-1, to: sqlite3_destructor_type.self)
12
    private let SQLITE_TRANSIENT = unsafeBitCast(-1, to: sqlite3_destructor_type.self)
Línea 13... Línea 13...
13
    private var db : OpaquePointer?
13
    private var database = Database.sharedInstance
14
    
14
    
Línea 15... Línea 15...
15
    static let sharedInstance: UserLogDao = {
15
    static let sharedInstance: UserLogDao = {
16
           let instance = UserLogDao()
16
           let instance = UserLogDao()
17
           
17
           
18
           // setup code
18
           // setup code
19
           return instance
-
 
20
    }()
-
 
21
    
-
 
Línea 22... Línea 19...
22
    init() {
19
           return instance
-
 
20
    }()
23
        self.db = Database.sharedInstance.open()
21
 
24
    }
22
 
25
 
23
    func selectByUserUuidAndActivityAndAddedOn(userUuid: String, activity : String, addedOn : String)-> UserLogModel {
26
    func selectByUserUuidAndActivityAndAddedOn(userUuid: String, activity : String, addedOn : String)-> UserLogModel {
24
        let db = database.open()
27
        var model = UserLogModel()
25
        var model = UserLogModel()
Línea 54... Línea 52...
54
                model.slideUuid = String(describing: String(cString: sqlite3_column_text(statement,5)))
52
                model.slideUuid = String(describing: String(cString: sqlite3_column_text(statement,5)))
55
                model.activity = String(describing: String(cString: sqlite3_column_text(statement, 6)))
53
                model.activity = String(describing: String(cString: sqlite3_column_text(statement, 6)))
56
                model.addedOn = String(describing: String(cString: sqlite3_column_text(statement, 7)))
54
                model.addedOn = String(describing: String(cString: sqlite3_column_text(statement, 7)))
Línea 57... Línea 55...
57
 
55
 
-
 
56
            }
-
 
57
        } else {
58
            }
58
            database.printError()
59
        }
59
        }
60
        sqlite3_finalize(statement)
60
        sqlite3_finalize(statement)
61
        return model
61
        return model
Línea 62... Línea 62...
62
    }
62
    }
-
 
63
    
63
    
64
   
64
   
65
    
65
    
66
    func selectAll()-> [UserLogModel] {
66
    func selectAll()-> [UserLogModel] {
67
        let db = database.open()
67
        var records = [UserLogModel]()
68
        var records = [UserLogModel]()
Línea 91... Línea 92...
91
                model.activity = String(describing: String(cString: sqlite3_column_text(statement, 6)))
92
                model.activity = String(describing: String(cString: sqlite3_column_text(statement, 6)))
92
                model.addedOn = String(describing: String(cString: sqlite3_column_text(statement, 7)))
93
                model.addedOn = String(describing: String(cString: sqlite3_column_text(statement, 7)))
Línea 93... Línea 94...
93
 
94
 
94
                records.append(model)
95
                records.append(model)
-
 
96
            }
-
 
97
        } else {
95
            }
98
            database.printError()
96
        }
99
        }
97
        sqlite3_finalize(statement)
100
        sqlite3_finalize(statement)
98
        return records
101
        return records
Línea 99... Línea 102...
99
    }
102
    }
-
 
103
     
100
     
104
    func selectAllByUserUuid(userUuid : String)-> [UserLogModel] {
101
    func selectAllByUserUuid(userUuid : String)-> [UserLogModel] {
105
        let db = database.open()
102
        var records = [UserLogModel]()
106
        var records = [UserLogModel]()
103
        var query = "SELECT "
107
        var query = "SELECT "
104
        query = query +  Constants.TABLE_USER_LOG_FIELD_ID + " , "
108
        query = query +  Constants.TABLE_USER_LOG_FIELD_ID + " , "
Línea 128... Línea 132...
128
                model.activity = String(describing: String(cString: sqlite3_column_text(statement, 6)))
132
                model.activity = String(describing: String(cString: sqlite3_column_text(statement, 6)))
129
                model.addedOn = String(describing: String(cString: sqlite3_column_text(statement, 7)))
133
                model.addedOn = String(describing: String(cString: sqlite3_column_text(statement, 7)))
Línea 130... Línea 134...
130
 
134
 
131
                records.append(model)
135
                records.append(model)
-
 
136
            }
-
 
137
        } else {
132
            }
138
            database.printError()
133
        }
139
        }
134
        sqlite3_finalize(statement)
140
        sqlite3_finalize(statement)
135
        return records
141
        return records
Línea 136... Línea 142...
136
    }
142
    }
-
 
143
    
137
    
144
    func insert(record : UserLogModel) {
138
    func insert(record : UserLogModel) {
145
        let db = database.open()
139
        var query = "INSERT INTO " + Constants.TABLE_USER_LOG + " ( "
146
        var query = "INSERT INTO " + Constants.TABLE_USER_LOG + " ( "
140
        query = query + Constants.TABLE_USER_LOG_FIELD_COMPANY_UUID + " , "
147
        query = query + Constants.TABLE_USER_LOG_FIELD_COMPANY_UUID + " , "
141
        query = query + Constants.TABLE_USER_LOG_FIELD_USER_UUID + " , "
148
        query = query + Constants.TABLE_USER_LOG_FIELD_USER_UUID + " , "
Línea 159... Línea 166...
159
            sqlite3_bind_text(statement, 6, record.activity , -1, SQLITE_TRANSIENT)
166
            sqlite3_bind_text(statement, 6, record.activity , -1, SQLITE_TRANSIENT)
160
            sqlite3_bind_text(statement, 7, record.addedOn , -1, SQLITE_TRANSIENT)
167
            sqlite3_bind_text(statement, 7, record.addedOn , -1, SQLITE_TRANSIENT)
Línea 161... Línea 168...
161
 
168
 
162
            if (sqlite3_step(statement) != SQLITE_DONE) {
169
            if (sqlite3_step(statement) != SQLITE_DONE) {
-
 
170
                print("No se pudo insertar un registro en la tabla: \(Constants.TABLE_USER_LOG)")
163
                print("No se pudo insertar un registro en la tabla: \(Constants.TABLE_USER_LOG)")
171
                database.printError()
164
            } else {
172
            } else {
-
 
173
                print("Insertamos un registo de log")
165
                print("Insertamos un registo de log")
174
                database.printError()
166
            }
175
            }
167
        } else {
176
        } else {
-
 
177
            print("Fallo la preparación en el insertar un registro en la tabla: \(Constants.TABLE_USER_LOG)")
168
            print("Fallo la preparación en el insertar un registro en la tabla: \(Constants.TABLE_USER_LOG)")
178
            database.printError()
Línea 169... Línea 179...
169
        }
179
        }
170
       
180
       
Línea 171... Línea 181...
171
        sqlite3_finalize(statement)
181
        sqlite3_finalize(statement)
-
 
182
    }
172
    }
183
 
173
 
184
    func remove(id: Int) {
174
    func remove(id: Int) {
185
        let db = database.open()
175
        let query = "DELETE FROM " + Constants.TABLE_USER_LOG + " WHERE " + Constants.TABLE_USER_LOG_FIELD_ID + " = " + "\(id) ;"
186
        let query = "DELETE FROM " + Constants.TABLE_USER_LOG + " WHERE " + Constants.TABLE_USER_LOG_FIELD_ID + " = " + "\(id) ;"
176
        var statement : OpaquePointer? = nil
187
        var statement : OpaquePointer? = nil
-
 
188
        if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
177
        if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
189
            if sqlite3_step(statement) != SQLITE_DONE {
178
            if sqlite3_step(statement) != SQLITE_DONE {
190
                print("No se pudo borrar el registro con el id: \(id) en la tabla: \(Constants.TABLE_USER_LOG)")
179
                print("No se pudo borrar el registro con el id: \(id) en la tabla: \(Constants.TABLE_USER_LOG)")
191
                database.printError()
-
 
192
            }
180
            }
193
        } else {
181
        } else {
194
            print("No se pudo borrar el registro con id: \(id) en la tabla: \(Constants.TABLE_USER_LOG)")
182
            print("No se pudo borrar el registro con id: \(id) en la tabla: \(Constants.TABLE_USER_LOG)")
195
            database.printError()
Línea 183... Línea 196...
183
        }
196
        }
-
 
197
        sqlite3_finalize(statement)
184
        sqlite3_finalize(statement)
198
    }
185
    }
199
 
186
 
200
    func removeAll() {
187
    func removeAll() {
201
        let db = database.open()
188
        let query = "DELETE FROM " + Constants.TABLE_USER_LOG + ";"
202
        let query = "DELETE FROM " + Constants.TABLE_USER_LOG + ";"
-
 
203
        var statement : OpaquePointer? = nil
-
 
204
        if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
189
        var statement : OpaquePointer? = nil
205
            if sqlite3_step(statement) != SQLITE_DONE {
190
        if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
206
                print("No se pudo borrar todos los registros en la tabla: \(Constants.TABLE_USER_LOG)")
-
 
207
                database.printError()
191
            if sqlite3_step(statement) != SQLITE_DONE {
208
            }
192
                print("No se pudo borrar todos los registros en la tabla: \(Constants.TABLE_USER_LOG)")            }
209
        } else {
193
        } else {
210
            print("Fallo la preparación de borrar todos los registros en la tabla: \(Constants.TABLE_USER_LOG)")
Línea 194... Línea 211...
194
            print("Fallo la preparación de borrar todos los registros en la tabla: \(Constants.TABLE_USER_LOG)")
211
            database.printError()
-
 
212
        }
195
        }
213
        sqlite3_finalize(statement)
196
        sqlite3_finalize(statement)
214
    }
Línea 197... Línea 215...
197
    }
215
    
198
    
216
    func removeAllUserUuidNotEqual(userUuid : String) {
199
    func removeAllUserUuidNotEqual(userUuid : String) {
217
        let db = database.open()
200
        let query = "DELETE FROM " + Constants.TABLE_USER_LOG
218
        let query = "DELETE FROM " + Constants.TABLE_USER_LOG
201
            + " WHERE " + Constants.TABLE_USER_LOG_FIELD_USER_UUID + " <> '" + userUuid + "' ";
219
            + " WHERE " + Constants.TABLE_USER_LOG_FIELD_USER_UUID + " <> '" + userUuid + "' ";
-
 
220
        
Línea 202... Línea 221...
202
        
221
        var statement : OpaquePointer? = nil
203
        var statement : OpaquePointer? = nil
222
        if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
204
        if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
223
            if sqlite3_step(statement) != SQLITE_DONE {
-
 
224
                print("No se pudo borrar todos los registros en la tabla: \(Constants.TABLE_USER_LOG) " +
205
            if sqlite3_step(statement) != SQLITE_DONE {
225
                        " de usuarios diferents a : \(userUuid) ")
206
                print("No se pudo borrar todos los registros en la tabla: \(Constants.TABLE_USER_LOG) " +
226
                database.printError()
207
                        " de usuarios diferents a : \(userUuid) ")
227
                
-
 
228
            } else {
208
                
229
                print("Se borraron todos los registros en la tabla: \(Constants.TABLE_USER_LOG) " +
209
            } else {
230
                        " de usuarios diferents a : \(userUuid) ")
210
                print("Se borraron todos los registros en la tabla: \(Constants.TABLE_USER_LOG) " +
231
                database.printError()