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