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 SlideDao {
11
class SlideDao {
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: SlideDao = {
15
    static let sharedInstance: SlideDao = {
16
           let instance = SlideDao()
16
           let instance = SlideDao()
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
 
Línea 25... Línea 23...
25
 
23
    func getCountByCapsuleUuid(capsuleUuid: String) -> Int {
26
    func getCountByCapsuleUuid(capsuleUuid: String) -> Int {
24
        let db = database.open()
Línea 34... Línea 32...
34
        if (sqlite3_prepare(db, query, -1, &statement, nil) == SQLITE_OK) {
32
        if (sqlite3_prepare(db, query, -1, &statement, nil) == SQLITE_OK) {
35
            if(sqlite3_step(statement) == SQLITE_ROW){
33
            if(sqlite3_step(statement) == SQLITE_ROW){
36
                count = Int(sqlite3_column_int(statement, 0))
34
                count = Int(sqlite3_column_int(statement, 0))
37
                //print("\(count)")
35
                //print("\(count)")
38
            }
36
            }
-
 
37
        } else {
-
 
38
            database.printError()
39
        }
39
        }
40
        sqlite3_finalize(statement)
40
        sqlite3_finalize(statement)
41
        return count
41
        return count
42
    }
42
    }
Línea 43... Línea 43...
43
 
43
 
44
    func getCountByTopicUuid(topicUuid: String) -> Int {
-
 
-
 
44
    func getCountByTopicUuid(topicUuid: String) -> Int {
45
 
45
        let db = database.open()
46
        let query = "SELECT COUNT(*) AS total FROM " + Constants.TABLE_SLIDE +
46
        let query = "SELECT COUNT(*) AS total FROM " + Constants.TABLE_SLIDE +
Línea 47... Línea 47...
47
            " WHERE " + Constants.TABLE_SLIDE_FIELD_TOPIC_UUID + " = '\(topicUuid)' ;"
47
            " WHERE " + Constants.TABLE_SLIDE_FIELD_TOPIC_UUID + " = '\(topicUuid)' ;"
48
 
48
 
Línea 53... Línea 53...
53
        if (sqlite3_prepare(db, query, -1, &statement, nil) == SQLITE_OK) {
53
        if (sqlite3_prepare(db, query, -1, &statement, nil) == SQLITE_OK) {
54
            if(sqlite3_step(statement) == SQLITE_ROW){
54
            if(sqlite3_step(statement) == SQLITE_ROW){
55
                count = Int(sqlite3_column_int(statement, 0))
55
                count = Int(sqlite3_column_int(statement, 0))
56
                //print("\(count)")
56
                //print("\(count)")
57
            }
57
            }
-
 
58
        } else {
-
 
59
            database.printError()
58
        }
60
        }
59
        sqlite3_finalize(statement)
61
        sqlite3_finalize(statement)
60
        return count
62
        return count
61
    }
63
    }
Línea 62... Línea 64...
62
 
64
 
-
 
65
    func selectAllByCapsuleUuid(capsuleUuid: String)-> [SlideModel] {
63
    func selectAllByCapsuleUuid(capsuleUuid: String)-> [SlideModel] {
66
        let db = database.open()
Línea 64... Línea 67...
64
        var records = [SlideModel]()
67
        var records = [SlideModel]()
65
        
68
        
66
        var query = "SELECT " + Constants.TABLE_SLIDE_FIELD_UUID
69
        var query = "SELECT " + Constants.TABLE_SLIDE_FIELD_UUID
Línea 96... Línea 99...
96
                model.position = Int(sqlite3_column_int(statement, 9))
99
                model.position = Int(sqlite3_column_int(statement, 9))
97
                model.addedOn = String(describing: String(cString: sqlite3_column_text(statement, 10)))
100
                model.addedOn = String(describing: String(cString: sqlite3_column_text(statement, 10)))
98
                model.updatedOn = String(describing: String(cString: sqlite3_column_text(statement, 11)))
101
                model.updatedOn = String(describing: String(cString: sqlite3_column_text(statement, 11)))
99
                records.append(model)
102
                records.append(model)
100
            }
103
            }
-
 
104
        } else {
-
 
105
            database.printError()
101
        }
106
        }
102
        sqlite3_finalize(statement)
107
        sqlite3_finalize(statement)
103
        return records
108
        return records
104
    }
109
    }
Línea 105... Línea 110...
105
    
110
    
-
 
111
 
106
 
112
    func selectByUuid(uuid: String)-> SlideModel {
107
    func selectByUuid(uuid: String)-> SlideModel {
113
        let db = database.open()
108
        var model = SlideModel()
114
        var model = SlideModel()
109
        var query = "SELECT " + Constants.TABLE_SLIDE_FIELD_UUID
115
        var query = "SELECT " + Constants.TABLE_SLIDE_FIELD_UUID
110
        query = query + ", " + Constants.TABLE_SLIDE_FIELD_TOPIC_UUID
116
        query = query + ", " + Constants.TABLE_SLIDE_FIELD_TOPIC_UUID
Línea 135... Línea 141...
135
                model.background = String(describing: String(cString: sqlite3_column_text(statement, 8)))
141
                model.background = String(describing: String(cString: sqlite3_column_text(statement, 8)))
136
                model.position = Int(sqlite3_column_int(statement, 9))
142
                model.position = Int(sqlite3_column_int(statement, 9))
137
                model.addedOn = String(describing: String(cString: sqlite3_column_text(statement, 10)))
143
                model.addedOn = String(describing: String(cString: sqlite3_column_text(statement, 10)))
138
                model.updatedOn = String(describing: String(cString: sqlite3_column_text(statement, 11)))
144
                model.updatedOn = String(describing: String(cString: sqlite3_column_text(statement, 11)))
139
            }
145
            }
-
 
146
        } else {
-
 
147
            database.printError()
140
        }
148
        }
141
        sqlite3_finalize(statement)
149
        sqlite3_finalize(statement)
142
        return model
150
        return model
143
    }
151
    }
Línea 144... Línea 152...
144
        
152
        
-
 
153
    func insert(slide : SlideModel) {
145
    func insert(slide : SlideModel) {
154
        let db = database.open()
146
        var query = "INSERT INTO " + Constants.TABLE_SLIDE + " ( "
155
        var query = "INSERT INTO " + Constants.TABLE_SLIDE + " ( "
147
        query = query + Constants.TABLE_SLIDE_FIELD_UUID + ", "
156
        query = query + Constants.TABLE_SLIDE_FIELD_UUID + ", "
148
        query = query + Constants.TABLE_SLIDE_FIELD_TOPIC_UUID + ", "
157
        query = query + Constants.TABLE_SLIDE_FIELD_TOPIC_UUID + ", "
149
        query = query + Constants.TABLE_SLIDE_FIELD_CAPSULE_UUID + ", "
158
        query = query + Constants.TABLE_SLIDE_FIELD_CAPSULE_UUID + ", "
Línea 175... Línea 184...
175
            sqlite3_bind_int(statement, 10, Int32(slide.position))
184
            sqlite3_bind_int(statement, 10, Int32(slide.position))
176
            sqlite3_bind_text(statement,11, slide.addedOn , -1, SQLITE_TRANSIENT)
185
            sqlite3_bind_text(statement,11, slide.addedOn , -1, SQLITE_TRANSIENT)
177
            sqlite3_bind_text(statement, 12, slide.updatedOn, -1, SQLITE_TRANSIENT)
186
            sqlite3_bind_text(statement, 12, slide.updatedOn, -1, SQLITE_TRANSIENT)
Línea 178... Línea 187...
178
 
187
 
179
           if sqlite3_step(statement) != SQLITE_DONE {
188
           if sqlite3_step(statement) != SQLITE_DONE {
-
 
189
                print("No se pudo insertar un registro en la tabla: \(Constants.TABLE_SLIDE)")
180
            print("No se pudo insertar un registro en la tabla: \(Constants.TABLE_SLIDE)")
190
                database.printError()
181
           }
191
           }
182
        } else {
192
        } else {
-
 
193
            print("Fallo la preparación de insertar un registro en la tabla: \(Constants.TABLE_SLIDE)")
183
            print("Fallo la preparación de insertar un registro en la tabla: \(Constants.TABLE_SLIDE)")
194
            database.printError()
Línea 184... Línea 195...
184
        }
195
        }
185
       
196
       
Línea 186... Línea 197...
186
        sqlite3_finalize(statement)
197
        sqlite3_finalize(statement)
-
 
198
    }
187
    }
199
 
188
 
200
    func update(slide: SlideModel) {
189
    func update(slide: SlideModel) {
201
        let db = database.open()
190
        var query = "UPDATE " + Constants.TABLE_SLIDE
202
        var query = "UPDATE " + Constants.TABLE_SLIDE
191
        query = query + " SET " + Constants.TABLE_SLIDE_FIELD_QUIZ_UUID + " = '\(slide.quizUuid)', "
203
        query = query + " SET " + Constants.TABLE_SLIDE_FIELD_QUIZ_UUID + " = '\(slide.quizUuid)', "
Línea 200... Línea 212...
200
        query = query + " WHERE " + Constants.TABLE_SLIDE_FIELD_UUID + " = '\(slide.uuid)'  ;"
212
        query = query + " WHERE " + Constants.TABLE_SLIDE_FIELD_UUID + " = '\(slide.uuid)'  ;"
201
        var statement : OpaquePointer? = nil
213
        var statement : OpaquePointer? = nil
202
        if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
214
        if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
203
            if sqlite3_step(statement) != SQLITE_DONE {
215
            if sqlite3_step(statement) != SQLITE_DONE {
204
                print("No se puedo actualizar un registro en la tabla: \(Constants.TABLE_SLIDE) ")
216
                print("No se puedo actualizar un registro en la tabla: \(Constants.TABLE_SLIDE) ")
-
 
217
                database.printError()
205
            }
218
            }
206
        } else {
219
        } else {
207
            print("Fallo la preparación de actualizar un registro en la tabla: \(Constants.TABLE_SLIDE)")
220
            print("Fallo la preparación de actualizar un registro en la tabla: \(Constants.TABLE_SLIDE)")
-
 
221
            database.printError()
208
        }
222
        }
209
        sqlite3_finalize(statement)
223
        sqlite3_finalize(statement)
210
    }
224
    }
Línea 211... Línea 225...
211
 
225
 
-
 
226
    func remove(uuid: String) {
212
    func remove(uuid: String) {
227
        let db = database.open()
213
        let query = "DELETE FROM " + Constants.TABLE_SLIDE +
228
        let query = "DELETE FROM " + Constants.TABLE_SLIDE +
214
            " WHERE " + Constants.TABLE_SLIDE_FIELD_UUID + " = '\(uuid)' ;"
229
            " WHERE " + Constants.TABLE_SLIDE_FIELD_UUID + " = '\(uuid)' ;"
215
        var statement : OpaquePointer? = nil
230
        var statement : OpaquePointer? = nil
216
        if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
231
        if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
217
            if sqlite3_step(statement) != SQLITE_DONE {
232
            if sqlite3_step(statement) != SQLITE_DONE {
-
 
233
                print("No se pudo borrar el registro con el uuid: \(uuid) en la tabla: \(Constants.TABLE_SLIDE)")
218
                print("No se pudo borrar el registro con el uuid: \(uuid) en la tabla: \(Constants.TABLE_SLIDE)")
234
                database.printError()
219
            }
235
            }
220
        } else {
236
        } else {
-
 
237
            print("Fallo la preparación de borrar un registro con el uuid: \(uuid) en la tabla: \(Constants.TABLE_SLIDE)")
221
            print("Fallo la preparación de borrar un registro con el uuid: \(uuid) en la tabla: \(Constants.TABLE_SLIDE)")
238
            database.printError()
222
        }
239
        }
223
        sqlite3_finalize(statement)
240
        sqlite3_finalize(statement)
Línea 224... Línea 241...
224
    }
241
    }
-
 
242
 
225
 
243
    func removeAll() {
226
    func removeAll() {
244
        let db = database.open()
227
        let query = "DELETE FROM " + Constants.TABLE_SLIDE + ";"
245
        let query = "DELETE FROM " + Constants.TABLE_SLIDE + ";"
228
        var statement : OpaquePointer? = nil
246
        var statement : OpaquePointer? = nil
229
        if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
247
        if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
-
 
248
            if sqlite3_step(statement) != SQLITE_DONE {
230
            if sqlite3_step(statement) != SQLITE_DONE {
249
                print("No se pudo borrar todos los registros en la tabla: \(Constants.TABLE_SLIDE)")
231
                print("No se pudo borrar todos los registros en la tabla: \(Constants.TABLE_SLIDE)")
250
                database.printError()
232
            }
251
            }
-
 
252
        } else {
233
        } else {
253
            print("Fallo la preparación de borrar todos los registros en la tabla: \(Constants.TABLE_SLIDE) ")
234
            print("Fallo la preparación de borrar todos los registros en la tabla: \(Constants.TABLE_SLIDE) ")
254
            database.printError()
235
        }
255
        }
Línea 236... Línea 256...
236
        sqlite3_finalize(statement)
256
        sqlite3_finalize(statement)