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