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