This repository was archived by the owner on Jun 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCamembertModel.swift
executable file
·486 lines (431 loc) · 17.6 KB
/
CamembertModel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//
// SwiftSQL.swift
// SwiftSQL
//
// Created by Remi Robert on 20/08/14.
// Copyright (c) 2014 remirobert. All rights reserved.
//
import Foundation
class CamembertModel :NSObject {
private var nameTable :String! = nil
var id :Int? = nil
func setId(id :Int) {
self.id = id
}
private class func openConnection() {
Camembert.closeDataBase()
if let dbFolder = DataAccess.access.DbPath {
Camembert.initDataBase(dbFolder, nameDatabase: DataAccess.access.nameDataBase!)
}else{
Camembert.initDataBase(DataAccess.access.nameDataBase!)
}
}
enum OperationResult{
case Success, Error_DuplicatedID, Error_NoRecordFoundWithID, Error_GeneralFailure
}
func push() -> OperationResult{
if self.id != nil {
return OperationResult.Error_DuplicatedID;
}
CamembertModel.openConnection()
let mirror = Mirror(reflecting: self)
let children = mirror.children
let lastIndex = children.endIndex
var requestPush = "INSERT INTO " + self.nameTable + " ("
for i in children.indices
{
if (i.successor() == lastIndex)
{
requestPush += children[i].label! + ")"
}
else
{
requestPush += children[i].label! + ", "
}
}
requestPush += " VALUES ("
for i in children.indices
{
let currentValue = children[i].value
switch currentValue
{
case _ where (currentValue as? TEXT != nil): requestPush += "\"\(currentValue)\""
case _ where (currentValue as? DATE_TIME != nil):
let dateformatter = NSDateFormatter();
dateformatter.dateFormat = Camembert.Date_Time_Format;
let date = (currentValue as! NSDate)
_ = dateformatter.dateFromString("\(date)")
requestPush += "\"\(date)\""
break;
case _ where (currentValue as? BIT != nil):
if (currentValue as! Bool)
{
requestPush += "1";
}
else
{
requestPush += "0";
}
break;
default: requestPush += "\(currentValue)"
}
if (i.successor() == lastIndex)
{
requestPush += ");"
}
else
{
requestPush += ", "
}
}
let result = camembertExecSqlite3(UnsafeMutablePointer<Void>(DataAccess.access.dataAccess),
requestPush.cStringUsingEncoding(NSUTF8StringEncoding)!)
self.id = Int(sqlite3_last_insert_rowid(DataAccess.access.dataAccess))
var opResult: OperationResult = OperationResult.Success
if !result{
opResult = OperationResult.Error_GeneralFailure
}
return opResult
}
func update() -> OperationResult
{
if self.id == -1
{
return OperationResult.Error_NoRecordFoundWithID
}
CamembertModel.openConnection()
let mirror = Mirror(reflecting: self)
let children = mirror.children // .dropFirst()
let lastIndex = children.endIndex
var requestUpdate :String = "UPDATE \(self.nameTable) SET "
for i in children.indices
{
let currentValue = children[i].value
switch currentValue
{
case _ where (currentValue as? TEXT != nil): requestUpdate += "\(children[i].label!) = \"\(currentValue)\""
case _ where (currentValue as? DATE_TIME != nil):
let dateformatter = NSDateFormatter();
dateformatter.dateFormat = Camembert.Date_Time_Format;
let date = (currentValue as! NSDate)
requestUpdate += "\(children[i].label!) = \"\(date)\""
break;
case _ where (currentValue as? BIT != nil):
let result = (currentValue as! Bool) ? "1" : "0";
requestUpdate += "\(children[i].label!) = \"\(result)\""
default: requestUpdate += "\(children[i].label!) = \(currentValue)"
}
if (i.successor() == lastIndex)
{
requestUpdate += " WHERE id = \(self.id!);"
}
else
{
requestUpdate += ", "
}
}
let result = camembertExecSqlite3(UnsafeMutablePointer<Void>(DataAccess.access.dataAccess),
requestUpdate.cStringUsingEncoding(NSUTF8StringEncoding)!)
var opResult = OperationResult.Success
if !result{
opResult = OperationResult.Error_GeneralFailure
}
return opResult;
}
func remove() -> OperationResult{
if self.id == nil {
return OperationResult.Error_NoRecordFoundWithID;
}
CamembertModel.openConnection()
let requestDelete :String = "DELETE FROM \(self.nameTable) WHERE id=\(self.id!)"
let result = camembertExecSqlite3(UnsafeMutablePointer<Void>(DataAccess.access.dataAccess),
requestDelete.cStringUsingEncoding(NSUTF8StringEncoding)!)
self.id = -1
if !result{
return OperationResult.Error_GeneralFailure
}
return OperationResult.Success
}
func getSchemaTable() -> [String]! {
CamembertModel.openConnection()
var arrayString :[String] = []
let mirror = Mirror(reflecting: self)
let children = mirror.children // .dropFirst()
for i in children.indices
{
let currentValue = children[i].value
switch currentValue
{
case _ where (currentValue as? INTEGER != nil):
arrayString.append("\(children[i].label!) INTEGER")
case _ where (currentValue as? REAL != nil):
arrayString.append("\(children[i].label!) REAL")
case _ where (currentValue as? TEXT != nil):
arrayString.append("\(children[i].label!) TEXT")
case _ where (currentValue as? DATE_TIME != nil):
arrayString.append("\(children[i].label!) TEXT")
case _ where (currentValue as? BIT != nil):
arrayString.append("\(children[i].label!) INTEGER")
default: return nil
}
}
return arrayString
}
func isTableExist() -> Bool {
CamembertModel.openConnection()
for currentTable in Camembert.getListTable() {
if currentTable == self.nameTable {
return true
}
}
return false
}
class func getNameTable(inout tmpNameTable :String) -> String {
let parseString = "0123456789"
for currentNumberParse in parseString.characters {
var parseName = tmpNameTable.componentsSeparatedByString(String(currentNumberParse))
if parseName.count > 0 {
tmpNameTable = parseName[parseName.count - 1]
}
}
return tmpNameTable
}
func _initNameTable() {
CamembertModel.openConnection()
var tmpNameTable = NSString(CString: object_getClassName(self), encoding: NSUTF8StringEncoding) as! String
self.nameTable = CamembertModel.getNameTable(&tmpNameTable).componentsSeparatedByString(".")[1]
}
func sendRequest(inout ptrRequest :COpaquePointer, request :String) -> Bool {
CamembertModel.openConnection()
if sqlite3_prepare_v2(DataAccess.access.dataAccess,
request.cStringUsingEncoding(NSUTF8StringEncoding)!, -1, &ptrRequest, nil) != SQLITE_OK {
sqlite3_finalize(ptrRequest);
return false
}
sqlite3_finalize(ptrRequest);
return true
}
func createTable() -> Bool {
CamembertModel.openConnection()
if self.isTableExist() == false {
var requestCreateTable :String = "CREATE TABLE " + self.nameTable + " (id INTEGER PRIMARY KEY AUTOINCREMENT, "
if let configurationTable = self.getSchemaTable() {
for var index = 0; index < configurationTable.count; index++ {
switch index {
case configurationTable.count - 1: requestCreateTable += configurationTable[index]
default: requestCreateTable += configurationTable[index] + ", "
}
}
requestCreateTable += ");"
// let request :COpaquePointer = nil
camembertExecSqlite3(UnsafeMutablePointer<Void>(DataAccess.access.dataAccess),
requestCreateTable.cStringUsingEncoding(NSUTF8StringEncoding)!)
}
}
return true
}
class func numberElement() -> Int {
CamembertModel.openConnection()
var tmpNameTable = NSString(CString: class_getName(self), encoding: NSUTF8StringEncoding) as! String
tmpNameTable = tmpNameTable.componentsSeparatedByString(".")[1]
let requestNumberlement :String = "SELECT COUNT(*) FROM \(CamembertModel.getNameTable(&tmpNameTable));"
var ptrRequest :COpaquePointer = nil
if sqlite3_prepare_v2(DataAccess.access.dataAccess,
requestNumberlement.cStringUsingEncoding(NSUTF8StringEncoding)!, -1, &ptrRequest, nil) != SQLITE_OK {
sqlite3_finalize(ptrRequest);
return 0
}
if sqlite3_step(ptrRequest) == SQLITE_ROW {
let number = Int(sqlite3_column_int(ptrRequest, 0))
sqlite3_finalize(ptrRequest);
return number
}
return 0
}
func _initWithId(id :Int) {
CamembertModel.openConnection()
let requestInit :String = "SELECT * FROM \(self.nameTable) WHERE id=\(id);"
var ptrRequest :COpaquePointer = nil
if sqlite3_prepare_v2(DataAccess.access.dataAccess,
requestInit.cStringUsingEncoding(NSUTF8StringEncoding)!, -1, &ptrRequest, nil) != SQLITE_OK {
sqlite3_finalize(ptrRequest);
return Void()
}
if sqlite3_step(ptrRequest) == SQLITE_ROW
{
self.setId(Int(sqlite3_column_int(ptrRequest, 0)))
for var index = 1; index < Int(sqlite3_column_count(ptrRequest)); index++ {
let columName :String = NSString(CString: sqlite3_column_name(ptrRequest,
CInt(index)), encoding: NSUTF8StringEncoding)! as String
switch sqlite3_column_type(ptrRequest, CInt(index)) {
case SQLITE_INTEGER:
self.setValue((Int(sqlite3_column_int(ptrRequest,
CInt(index))) as AnyObject), forKey: columName)
case SQLITE_FLOAT:
self.setValue((Float(sqlite3_column_double(ptrRequest,
CInt(index))) as AnyObject), forKey: columName)
case SQLITE_TEXT:
let stringValue = String.fromCString(UnsafePointer<CChar>(sqlite3_column_text(ptrRequest, CInt(index))))
self.setValue(stringValue, forKey: columName)
default: Void()
}
}
}
sqlite3_finalize(ptrRequest);
}
class func getRawClassName() -> String? {
let name = NSStringFromClass(self)
let components = name.componentsSeparatedByString(".")
return components.last
}
class func select(selectRequest select: Select) -> [AnyObject]? {
let camembert = Camembert()
let table = getRawClassName()
var requestSelect: String? = nil
var m_OrderBy = "1";
switch select {
case .SelectAll(let OrderOperator, let OrderBy):
var op: String;
if !OrderBy.isEmpty {
m_OrderBy = OrderBy
}
switch OrderOperator{
case .Ascending:
op = "asc"
default:
op = "desc"
}
requestSelect = "SELECT * FROM \(table!) ORDER BY \(m_OrderBy) \(op)"
case .Limit(let value, let OrderOperator, let OrderBy):
var op: String;
if !OrderBy.isEmpty {
m_OrderBy = OrderBy
}
switch OrderOperator{
case .Ascending:
op = "asc"
default:
op = "desc"
}
requestSelect = "SELECT * FROM \(table!) LIMIT \(value) ORDER BY \(m_OrderBy) \(op)"
case .Between(let startValue, let endValue, let OrderOperator, let OrderBy):
var op: String;
if !OrderBy.isEmpty {
m_OrderBy = OrderBy
}
switch OrderOperator{
case .Ascending:
op = "asc"
default:
op = "desc"
}
requestSelect = "SELECT * FROM \(table!) WHERE ID BETWEEN \(startValue) AND \(endValue) ORDER BY \(m_OrderBy) \(op)"
case .CustomRequest(let request):
requestSelect = request
case .Where(let Field, let Operator, let value, let OrderOperator, let OrderBy):
var op: String;
if !OrderBy.isEmpty {
m_OrderBy = OrderBy
}
switch OrderOperator{
case .Ascending:
op = "asc"
default:
op = "desc"
}
switch Operator{
case .EqualsTo:
var resultValue = String();
if let _ = value as? BIT{
return nil
}else if let x = value as? TEXT {
resultValue = "\"\(x)\""
}else if let x = value as? DATE_TIME{
resultValue = "\"\(x)\""
}else{
resultValue = "\(value)";
}
requestSelect = "SELECT * FROM \(table!) WHERE \(Field) = \(resultValue) ORDER BY \(m_OrderBy) \(op)"
case .IsNull:
requestSelect = "SELECT * FROM \(table!) WHERE \(Field) IS NULL"
break;
case .LargerOrEqual:
var resultValue = String();
if let _ = value as? BIT{
return nil
}else if let x = value as? TEXT {
resultValue = "\"\(x)\""
}else if let x = value as? DATE_TIME{
resultValue = "\"\(x)\""
}else{
resultValue = "\(value)";
}
requestSelect = "SELECT * FROM \(table!) WHERE \(Field) >= \(resultValue) ORDER BY \(m_OrderBy) \(op)"
case .LargerThan:
var resultValue = String();
if let _ = value as? BIT{
return nil
}else if let x = value as? TEXT {
resultValue = "\"\(x)\""
}else if let x = value as? DATE_TIME{
resultValue = "\"\(x)\""
}else{
resultValue = "\(value)";
}
requestSelect = "SELECT * FROM \(table!) WHERE \(Field) > \(resultValue) ORDER BY \(m_OrderBy) \(op)"
case .NotNull:
requestSelect = "SELECT * FROM \(table!) WHERE \(Field) IS NOT NULL ORDER BY \(m_OrderBy) \(op)"
case .SmallerOrEqual:
var resultValue = String();
if let _ = value as? BIT{
return nil
}else if let x = value as? TEXT {
resultValue = "\"\(x)\""
}else if let x = value as? DATE_TIME{
resultValue = "\"\(x)\""
}else{
resultValue = "\(value)";
}
requestSelect = "SELECT * FROM \(table!) WHERE \(Field) <= \(resultValue) ORDER BY \(m_OrderBy) \(op)"
case .SmallerThan:
var resultValue = String();
if let _ = value as? BIT{
return nil
}else if let x = value as? TEXT {
resultValue = "\"\(x)\""
}else if let x = value as? DATE_TIME{
resultValue = "\"\(x)\""
}else{
resultValue = "\(value)";
}
requestSelect = "SELECT * FROM \(table!) WHERE \(Field) < \(resultValue) ORDER BY \(m_OrderBy) \(op)"
// case .IsNull:
// requestSelect = "SELECT * FROM \(table!) WHERE \(Field) IS NULL ORDER BY \(m_OrderBy) \(op)"
}
break;
}
CamembertModel.openConnection()
if let ret = camembert.getObjectsWithQuery(requestSelect!, table: table!) {
return ret
}
return nil
}
class func removeTable() {
CamembertModel.openConnection()
let table = getRawClassName()
let requestRemove :String = "DROP TABLE IF EXISTS \(table!);"
camembertExecSqlite3(UnsafeMutablePointer<Void>(DataAccess.access.dataAccess),
requestRemove.cStringUsingEncoding(NSUTF8StringEncoding)!)
}
override init() {
super.init()
self._initNameTable()
self.createTable()
}
init(id :Int) {
super.init()
self._initNameTable()
self.createTable()
self._initWithId(id)
}
}