Skip to content

Commit

Permalink
Code clean up and tweaking for better usage/readability, Added an SQL…
Browse files Browse the repository at this point in the history
…Column class to encapsulate each column, Set the SQLColulmn class to return values as per the needed value type - currently, if the return type does not match the storage type, the value is empty but this should be fixed to be more flexible in future
  • Loading branch information
FahimF committed Jun 26, 2014
1 parent 3dd05d3 commit 61ee53b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 36 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ Usage
```swift
let data = db.query("SELECT * FROM customers WHERE name='John'")
let row = data[0]
let name = row.valueForKey("name")
if let name = row.["name"] {
textLabel.text = name.string
}
```
In the above, `db` is a reference to the shared SQLite database instance and `SQLRow` is a class defined to model a data row in SQLiteDB.

Expand Down
105 changes: 70 additions & 35 deletions SQLiteDB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import Foundation
import UIKit

let SQLITE_DATE = SQLITE_NULL + 1

extension String {
func positionOf(sub:String)->Int {
var pos = -1
Expand Down Expand Up @@ -36,49 +38,72 @@ extension String {
}
}

class SQLRow {
let SQLITE_DATE = SQLITE_NULL + 1
var keys:String[] = String[]()
var values:Any[] = Any[]()
var types:CInt[] = CInt[]()
class SQLColumn {
var value:Any = nil
var type:CInt = -1

init(value:Any, type:CInt) {
self.value = value
self.type = type
}

func add(key:String, value:Any, type:CInt) {
keys += key
values += value
types += type
var string:String {
if type == SQLITE_TEXT {
return value as String
} else {
return ""
}
}

func valueForKey(key:String)->Any {
var val:Any? = nil
let ndx = find(keys, key)
if ndx {
let type = types[ndx!]
let val = values[ndx!]
if type == SQLITE_INTEGER {
return val as Int
}
if type == SQLITE_FLOAT {
return val as Double
}
if type == SQLITE_BLOB {
return val as NSData
}
if type == SQLITE_NULL {
return nil
}
if type == SQLITE_DATE {
return val as NSDate
}
// Return everything else as String
return val as String
var integer:Int {
if type == SQLITE_INTEGER {
return value as Int
} else {
return 0
}
}

var double:Double {
if type == SQLITE_FLOAT {
return value as Double
} else {
return 0.0
}
}

var data:NSData? {
if type == SQLITE_BLOB {
return value as? NSData
} else {
return nil
}
}

var date:NSDate? {
if type == SQLITE_DATE {
return value as? NSDate
} else {
return nil
}
}
}

class SQLRow {
var data = Dictionary<String, SQLColumn>()

subscript(key: String) -> SQLColumn? {
get {
return data[key]
}

set(newVal) {
data[key] = newVal
}
return nil
}
}

class SQLiteDB {
let DB_NAME:CString = "data.db"
let SQLITE_DATE = SQLITE_NULL + 1
var db:COpaquePointer = nil
var queue:dispatch_queue_t = dispatch_queue_create("SQLiteDB", nil)
struct Static {
Expand Down Expand Up @@ -234,7 +259,8 @@ class SQLiteDB {
for index in 0..columnCount {
let key = columnNames[Int(index)]
let type = columnTypes[Int(index)]
row.add(key, value: self.getColumnValue(index, type: type, stmt: stmt), type: type)
let col = SQLColumn(value: self.getColumnValue(index, type: type, stmt: stmt), type: type)
row[key] = col
}
rows.append(row)
// Next row
Expand Down Expand Up @@ -399,3 +425,12 @@ class SQLiteDB {
return val
}
}

/*
-(int)columnTypeAtIndex:(int)column inStatement:(sqlite3_stmt *)statement {
if ([dataType hasPrefix:@"UNSIGNED"]) {
dataType = [dataType substringWithRange:NSMakeRange(0, 8)];
}
dataType = [dataType stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
*/

0 comments on commit 61ee53b

Please sign in to comment.