Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 1 | Rev 17 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
//
2
//  UserNotificationDao.swift
3
//  twogetskills
4
//
5
//  Created by Efrain Yanez Recanatini on 7/31/22.
6
//
7
 
8
 
9
import UIKit
10
import SQLite3
11
 
12
class UserNotificationDao {
13
    private let SQLITE_TRANSIENT = unsafeBitCast(-1, to: sqlite3_destructor_type.self)
14
    private var db : OpaquePointer?
15
 
16
    static let sharedInstance: UserNotificationDao = {
17
           let instance = UserNotificationDao()
18
 
19
           // setup code
20
           return instance
21
    }()
22
 
23
    init() {
24
        self.db = Database.sharedInstance.open()
25
    }
26
 
27
    func selectById(id : Int)-> UserNotificationModel {
28
        var model = UserNotificationModel ()
29
 
30
        var query = "SELECT "
31
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_ID + ", "
32
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_USER_UUID + ", "
33
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_TITLE + ", "
34
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_DESCRIPTION + ", "
35
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_URL + " , "
36
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_VIEWED + " , "
37
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_ADDED_ON
38
        query = query + " FROM " + Constants.TABLE_USER_NOTIFICATION
39
        query = query + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_ID + " = '\(id)'  LIMIT 1;"
40
 
41
        var statement : OpaquePointer? = nil
42
 
43
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
44
            if (sqlite3_step(statement) == SQLITE_ROW) {
45
 
46
 
47
                model.id = Int(sqlite3_column_int(statement, 0))
48
 
49
                model.user_uuid = String(describing: String(cString: sqlite3_column_text(statement, 1)))
50
 
51
                model.title = String(describing: String(cString: sqlite3_column_text(statement, 2)))
52
 
53
                model.description = String(describing: String(cString: sqlite3_column_text(statement, 3)))
54
 
55
                model.description = String(describing: String(cString: sqlite3_column_text(statement, 4)))
56
 
57
                model.url = String(describing: String(cString: sqlite3_column_text(statement, 5)))
58
 
59
                model.viewed = Int(sqlite3_column_int(statement, 6))
60
 
61
                model.addedOn = String(describing: String(cString: sqlite3_column_text(statement, 7)))
62
 
11 efrain 63
               //print("\nSuccessfully get record")
1 efrain 64
 
65
            }
66
        }
67
        sqlite3_finalize(statement)
68
        return model
69
    }
70
 
71
 
72
    func selectAll()-> [UserNotificationModel] {
73
        var records = [UserNotificationModel]()
74
 
75
        var query = "SELECT "
76
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_ID + ", "
77
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_USER_UUID + ", "
78
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_TITLE + ", "
79
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_DESCRIPTION + ", "
80
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_URL + " , "
81
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_VIEWED + " , "
82
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_ADDED_ON
83
        query = query + " FROM " + Constants.TABLE_USER_NOTIFICATION
84
        query = query + " ORDER BY  " + Constants.TABLE_USER_NOTIFICATION_FIELD_ADDED_ON + " DESC ;"
85
 
86
        var statement : OpaquePointer? = nil
87
 
88
 
89
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
90
            while (sqlite3_step(statement) == SQLITE_ROW) {
91
 
92
                var  model = UserNotificationModel()
93
                model.id = Int(sqlite3_column_int(statement, 0))
94
 
95
                model.user_uuid = String(describing: String(cString: sqlite3_column_text(statement, 1)))
96
 
97
                model.title = String(describing: String(cString: sqlite3_column_text(statement, 2)))
98
 
99
                model.description = String(describing: String(cString: sqlite3_column_text(statement, 3)))
100
 
101
                model.description = String(describing: String(cString: sqlite3_column_text(statement, 4)))
102
 
103
                model.url = String(describing: String(cString: sqlite3_column_text(statement, 5)))
104
 
105
                model.viewed = Int(sqlite3_column_int(statement, 6))
106
 
107
                model.addedOn = String(describing: String(cString: sqlite3_column_text(statement, 7)))
108
 
109
 
110
                records.append(model)
111
 
112
            }
113
        }
114
        sqlite3_finalize(statement)
115
        return records
116
    }
117
 
118
 
119
 
120
    func getCount() -> Int {
121
        let query = "SELECT COUNT(*) AS total FROM " + Constants.TABLE_USER_NOTIFICATION;
122
 
123
        var statement : OpaquePointer? = nil
124
        var count = 0;
125
 
126
 
127
        if (sqlite3_prepare(db, query, -1, &statement, nil) == SQLITE_OK) {
128
            if(sqlite3_step(statement) == SQLITE_ROW){
129
                count = Int(sqlite3_column_int(statement, 0))
11 efrain 130
                //print("\(count)")
1 efrain 131
            }
132
        } else {
133
            count = -1
134
        }
135
        sqlite3_finalize(statement)
136
        return count
137
    }
138
 
139
    func insert(userNotification : UserNotificationModel) -> Int {
140
        var result : Int = 0
141
        var query = "INSERT INTO " + Constants.TABLE_USER_NOTIFICATION + " ( "
142
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_USER_UUID + ", "
143
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_TITLE + ", "
144
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_DESCRIPTION + ", "
145
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_URL + ", "
146
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_VIEWED + ", "
147
        query = query + Constants.TABLE_USER_NOTIFICATION_FIELD_ADDED_ON
148
        query = query + " ) VALUES (?, ?, ?, ?, ?, ?);"
149
        var statement : OpaquePointer?
150
 
151
 
152
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
153
 
154
            sqlite3_bind_text(statement, 1, userNotification.user_uuid, -1, SQLITE_TRANSIENT)
155
            sqlite3_bind_text(statement, 2, userNotification.title, -1 , SQLITE_TRANSIENT)
156
            sqlite3_bind_text(statement, 3, userNotification.description , -1, SQLITE_TRANSIENT)
157
            sqlite3_bind_text(statement, 4, userNotification.url , -1, SQLITE_TRANSIENT)
158
            sqlite3_bind_int(statement, 5, Int32(userNotification.viewed))
159
            sqlite3_bind_text(statement, 6, userNotification.addedOn , -1, SQLITE_TRANSIENT)
160
 
161
            if (sqlite3_step(statement) == SQLITE_DONE) {
162
                result = Int(sqlite3_last_insert_rowid(db))
163
            } else {
164
                 print("No se pudo insertar el registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
165
 
166
            }
167
        } else {
168
            print("Fallo la preparación del insertar un registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
169
        }
170
 
171
        sqlite3_finalize(statement)
172
 
173
        return result
174
    }
175
 
176
    func markViewed(id : Int) {
177
        var query = "UPDATE " + Constants.TABLE_USER_NOTIFICATION
178
        query = query + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_ID + " = '\(id)';"
179
        var statement : OpaquePointer? = nil
180
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
181
            if (sqlite3_step(statement) != SQLITE_DONE) {
182
                print("No se pudo actualizar un registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION) ")
183
            }
184
        } else {
185
            print("Fallo la preparación de la actualización de un registro en la tabla \(Constants.TABLE_USER_NOTIFICATION) ")
186
        }
187
        sqlite3_finalize(statement)
188
    }
189
 
190
    func markViewedAllPending() {
191
        var query = "UPDATE " + Constants.TABLE_USER_NOTIFICATION
192
        query = query + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_VIEWED + " = 0;"
193
        var statement : OpaquePointer? = nil
194
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
195
            if (sqlite3_step(statement) != SQLITE_DONE) {
196
                print("No se pudo actualizar un registro en la tabla: \(Constants.TABLE_USER_NOTIFICATION) ")
197
            }
198
        } else {
199
            print("Fallo la preparación de la actualización de un registro en la tabla \(Constants.TABLE_USER_NOTIFICATION) ")
200
        }
201
        sqlite3_finalize(statement)
202
    }
203
 
204
    func remove(id: Int) {
205
        let query = "DELETE FROM " + Constants.TABLE_USER_NOTIFICATION
206
            + " WHERE " + Constants.TABLE_USER_NOTIFICATION_FIELD_ID + " = '\(id)' ;"
207
        var statement : OpaquePointer? = nil
208
        if (sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK) {
209
            if (sqlite3_step(statement) != SQLITE_DONE) {
210
                print("No se pudo borrar el registro con el id: \(id) en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
211
            }
212
        } else {
213
            print("Fallo la preparación del borrado del registro con el id: \(id) en la tabla: \(Constants.TABLE_USER_NOTIFICATION)" )
214
        }
215
        sqlite3_finalize(statement)
216
    }
217
 
218
    func removeAll() {
219
        let query = "DELETE FROM " + Constants.TABLE_USER_NOTIFICATION + ";"
220
        var statement : OpaquePointer? = nil
221
        if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
222
            if sqlite3_step(statement) != SQLITE_DONE {
223
                print("Fallo el borrado de todos los registros en la tabla: \(Constants.TABLE_USER_NOTIFICATION)")
224
            }
225
        } else {
226
            print("No se pudo preparar el borrado de todos los registros en la tabla: \(Constants.TABLE_USER_NOTIFICATION) ")
227
        }
228
        sqlite3_finalize(statement)
229
    }
230
 
231
}
232