Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

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

Rev 11 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 TopicDao {
11
class TopicDao {
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: TopicDao = {
15
    static let sharedInstance: TopicDao = {
16
           let instance = TopicDao()
16
           let instance = TopicDao()
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
 
Línea 24... Línea 22...
24
    }
22
 
25
 
23
    func selectByUuid(uuid: String)-> TopicModel {
26
    func selectByUuid(uuid: String)-> TopicModel {
24
        let db = database.open()
Línea 51... Línea 49...
51
                model.addedOn = String(describing: String(cString: sqlite3_column_text(statement, 6)))
49
                model.addedOn = String(describing: String(cString: sqlite3_column_text(statement, 6)))
52
                model.updatedOn = String(describing: String(cString: sqlite3_column_text(statement, 7)))
50
                model.updatedOn = String(describing: String(cString: sqlite3_column_text(statement, 7)))
53
                //print("\nSuccessfully get record")
51
                //print("\nSuccessfully get record")
Línea 54... Línea 52...
54
                
52
                
-
 
53
            }
-
 
54
        } else {
55
            }
55
            database.printError()
56
        }
56
        }
57
        sqlite3_finalize(statement)
57
        sqlite3_finalize(statement)
58
        return model
58
        return model
Línea 59... Línea 59...
59
    }
59
    }
-
 
60
 
60
 
61
 
Línea 61... Línea 62...
61
 
62
    func selectAllBycompanyUuid(companyUuid: String)-> [TopicModel] {
62
    func selectAllBycompanyUuid(companyUuid: String)-> [TopicModel] {
63
        let db = database.open()
63
        var records = [TopicModel]()
64
        var records = [TopicModel]()
Línea 92... Línea 93...
92
                model.updatedOn = String(describing: String(cString: sqlite3_column_text(statement, 7)))
93
                model.updatedOn = String(describing: String(cString: sqlite3_column_text(statement, 7)))
Línea 93... Línea 94...
93
                
94
                
Línea 94... Línea 95...
94
                records.append(model)
95
                records.append(model)
-
 
96
                
-
 
97
            }
95
                
98
        } else {
96
            }
99
            database.printError()
97
        }
100
        }
98
        sqlite3_finalize(statement)
101
        sqlite3_finalize(statement)
Línea 99... Línea 102...
99
        return records
102
        return records
-
 
103
    }
100
    }
104
 
101
 
105
    func selectAll()-> [TopicModel] {
102
    func selectAll()-> [TopicModel] {
106
        let db = database.open()
103
        var records = [TopicModel]()
107
        var records = [TopicModel]()
104
        var query = "SELECT "
108
        var query = "SELECT "
Línea 132... Línea 136...
132
    
136
    
Línea 133... Línea 137...
133
                
137
                
-
 
138
                records.append(model)
-
 
139
                
134
                records.append(model)
140
            }
135
                
141
        } else {
136
            }
142
            database.printError()
137
        }
143
        }
Línea 138... Línea 144...
138
        sqlite3_finalize(statement)
144
        sqlite3_finalize(statement)
-
 
145
        return records
139
        return records
146
    }
Línea 140... Línea 147...
140
    }
147
    
141
    
148
    func getCount() -> Int {
Línea 151... Línea 158...
151
                count = Int(sqlite3_column_int(statement, 0))
158
                count = Int(sqlite3_column_int(statement, 0))
152
                //print("\(count)")
159
                //print("\(count)")
153
            }
160
            }
154
        } else {
161
        } else {
155
            count = -1
162
            count = -1
-
 
163
            database.printError()
156
        }
164
        }
157
        sqlite3_finalize(statement)
165
        sqlite3_finalize(statement)
158
        return count
166
        return count
159
    }
167
    }
Línea 160... Línea 168...
160
    
168
    
-
 
169
    func insert(topic : TopicModel) {
161
    func insert(topic : TopicModel) {
170
        let db = database.open()
162
        var query = "INSERT INTO " + Constants.TABLE_TOPIC + " ( "
171
        var query = "INSERT INTO " + Constants.TABLE_TOPIC + " ( "
163
        query = query + Constants.TABLE_TOPIC_FIELD_UUID + ", "
172
        query = query + Constants.TABLE_TOPIC_FIELD_UUID + ", "
164
        query = query + Constants.TABLE_TOPIC_FIELD_COMPANY_UUID + ", "
173
        query = query + Constants.TABLE_TOPIC_FIELD_COMPANY_UUID + ", "
165
        query = query + Constants.TABLE_TOPIC_FIELD_NAME + ", "
174
        query = query + Constants.TABLE_TOPIC_FIELD_NAME + ", "
Línea 182... Línea 191...
182
            sqlite3_bind_int(statement, 6, Int32(topic.position))
191
            sqlite3_bind_int(statement, 6, Int32(topic.position))
183
            sqlite3_bind_text(statement, 7, topic.addedOn , -1, SQLITE_TRANSIENT)
192
            sqlite3_bind_text(statement, 7, topic.addedOn , -1, SQLITE_TRANSIENT)
184
            sqlite3_bind_text(statement, 8, topic.updatedOn , -1, SQLITE_TRANSIENT)
193
            sqlite3_bind_text(statement, 8, topic.updatedOn , -1, SQLITE_TRANSIENT)
Línea 185... Línea 194...
185
 
194
 
186
           if (sqlite3_step(statement) != SQLITE_DONE) {
195
           if (sqlite3_step(statement) != SQLITE_DONE) {
-
 
196
                print("No se pudo insertar un registro en la tabla: \(Constants.TABLE_TOPIC)")
-
 
197
                database.printError()
187
            print("No se pudo insertar un registro en la tabla: \(Constants.TABLE_TOPIC)")
198
            
188
           }
199
           }
189
        } else {
200
        } else {
-
 
201
            print("Fallo la preparación del insertar un registro en la tabla: \(Constants.TABLE_TOPIC)")
-
 
202
            
190
            print("Fallo la preparación del insertar un registro en la tabla: \(Constants.TABLE_TOPIC)")
203
            database.printError()
Línea 191... Línea 204...
191
        }
204
        }
192
       
205
       
Línea 193... Línea 206...
193
        sqlite3_finalize(statement)
206
        sqlite3_finalize(statement)
-
 
207
    }
194
    }
208
 
195
 
209
    func update(topic: TopicModel) {
196
    func update(topic: TopicModel) {
210
        let db = database.open()
197
        var query = "UPDATE " + Constants.TABLE_TOPIC
211
        var query = "UPDATE " + Constants.TABLE_TOPIC
198
        query = query + " SET " + Constants.TABLE_TOPIC_FIELD_NAME + " = '\(topic.name)', "
212
        query = query + " SET " + Constants.TABLE_TOPIC_FIELD_NAME + " = '\(topic.name)', "
Línea 204... Línea 218...
204
        query = query + " WHERE " + Constants.TABLE_TOPIC_FIELD_UUID + " = '\(topic.uuid)';"
218
        query = query + " WHERE " + Constants.TABLE_TOPIC_FIELD_UUID + " = '\(topic.uuid)';"
205
        var statement : OpaquePointer? = nil
219
        var statement : OpaquePointer? = nil
206
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
220
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
207
            if (sqlite3_step(statement) != SQLITE_DONE) {
221
            if (sqlite3_step(statement) != SQLITE_DONE) {
208
                print("No se pudo actualizar un registro en la tabla: \(Constants.TABLE_TOPIC) ")
222
                print("No se pudo actualizar un registro en la tabla: \(Constants.TABLE_TOPIC) ")
-
 
223
                database.printError()
209
            }
224
            }
210
        } else {
225
        } else {
211
            print("Fallo la preparación de la actualización de un registro en la tabla \(Constants.TABLE_TOPIC) ")
226
            print("Fallo la preparación de la actualización de un registro en la tabla \(Constants.TABLE_TOPIC) ")
-
 
227
            database.printError()
212
        }
228
        }
213
        sqlite3_finalize(statement)
229
        sqlite3_finalize(statement)
214
    }
230
    }
Línea 215... Línea 231...
215
 
231
 
-
 
232
    func remove(uuid: String) {
216
    func remove(uuid: String) {
233
        let db = database.open()
217
        let query = "DELETE FROM " + Constants.TABLE_TOPIC
234
        let query = "DELETE FROM " + Constants.TABLE_TOPIC
218
            + " WHERE " + Constants.TABLE_TOPIC_FIELD_UUID + " = '\(uuid)' ;"
235
            + " WHERE " + Constants.TABLE_TOPIC_FIELD_UUID + " = '\(uuid)' ;"
219
        var statement : OpaquePointer? = nil
236
        var statement : OpaquePointer? = nil
220
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
237
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
221
            if (sqlite3_step(statement) != SQLITE_DONE) {
238
            if (sqlite3_step(statement) != SQLITE_DONE) {
-
 
239
                print("No se pudo borrar el registro con el uuid: \(uuid) en la tabla: \(Constants.TABLE_TOPIC)")
222
                print("No se pudo borrar el registro con el uuid: \(uuid) en la tabla: \(Constants.TABLE_TOPIC)")
240
                database.printError()
223
            }
241
            }
224
        } else {
242
        } else {
-
 
243
            print("Fallo la preparación del borrado del registro con el uuid: \(uuid) en la tabla: \(Constants.TABLE_TOPIC)" )
225
            print("Fallo la preparación del borrado del registro con el uuid: \(uuid) en la tabla: \(Constants.TABLE_TOPIC)" )
244
            database.printError()
226
        }
245
        }
227
        sqlite3_finalize(statement)
246
        sqlite3_finalize(statement)
Línea 228... Línea 247...
228
    }
247
    }
-
 
248
 
229
 
249
    func removeAll() {
230
    func removeAll() {
250
        let db = database.open()
231
        let query = "DELETE FROM " + Constants.TABLE_TOPIC + ";"
251
        let query = "DELETE FROM " + Constants.TABLE_TOPIC + ";"
232
        var statement : OpaquePointer? = nil
252
        var statement : OpaquePointer? = nil
233
        if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
253
        if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
-
 
254
            if sqlite3_step(statement) != SQLITE_DONE {
234
            if sqlite3_step(statement) != SQLITE_DONE {
255
                print("Fallo el borrado de todos los registros en la tabla: \(Constants.TABLE_TOPIC)")
235
                print("Fallo el borrado de todos los registros en la tabla: \(Constants.TABLE_TOPIC)")
256
                database.printError()
236
            }
257
            }
-
 
258
        } else {
237
        } else {
259
            print("No se pudo preparar el borrado de todos los registros en la tabla: \(Constants.TABLE_TOPIC) ")
238
            print("No se pudo preparar el borrado de todos los registros en la tabla: \(Constants.TABLE_TOPIC) ")
260
            database.printError()
239
        }
261
        }
Línea 240... Línea 262...
240
        sqlite3_finalize(statement)
262
        sqlite3_finalize(statement)