Skip to content

Commit

Permalink
UPDATE: 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sgxiang committed Jan 8, 2015
1 parent 1749fa5 commit 07e5e38
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 149 deletions.
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ YTKKeyValueStore_Swfit

![License MIT](https://go-shields.herokuapp.com/license-MIT-blue.png)

## 关于更新

在不修改数据库结构的情况下优化了源版本提供的读写接口,去除了`putString`,`putNumber`,`getString`,`getNumber`的接口,统计集成在`putObject``getObject`中。写入时会自动判断并插入,读出时返回一个`YTKObject`对象,提供了以下属性读取相关的数据:

```
objectValue : 读取出AnyObject?的类型
stringValue : 读取出String?的类型
numberValue : 读取出NSNumber?的类型
dictionaryValue : 读取出Dictionary<String , AnyObject>?的类型
arrayValue : 读取出Array<AnyObject>?的类型
```
`YTKKeyValueItem_Swift``itemObject`也修改为`YTKObject`属性方便读取。

旧的版本在`releases 0.1.0`中。地址: [YTKKeyValueStore_Swift(0.1.0)](https://github.com/sgxiang/YTKKeyValueStore_Swift/archive/0.1.0.zip)

## 使用示例

```swift
Expand All @@ -18,7 +33,7 @@ let key = "1"
let user = ["id":1 , "name" : "tangqiao" , "age" : 30]
store.putObject(user, withId: key, intoTable: tableName)
//查询
if let queryUser: AnyObject = store.getObjectById(key, fromTable: tableName){
if let queryUser: AnyObject = store.getObjectById(key, fromTable: tableName)?.dictionaryValue{
println("[swift] query data result: \(queryUser)")
}
```
Expand Down Expand Up @@ -54,17 +69,13 @@ store.createTable(tableName: tableName)
`YTKKeyValueStore_Swift`类支持的value类型包括:String, CGFloat, Dictionary和Array以及对应的oc类型,为此提供了以下接口:

```
putString(string:withId:intoTable:)
putNumber(number:withId:intoTable:)
putObject(objct:withId:intoTable:)
```

与此对应,有以下value为String, CGFloat, Dictionary和Array的读取接口:

```
getStringById(stringId:fromTable:)->String?
getNumberById(numberId:fromTable:)->CGFloat?
getObjectById(objectId:fromTable:)->AnyObject?
getObjectById(objectId:fromTable:)->YTKObject?
```

### 删除数据接口
Expand Down Expand Up @@ -93,7 +104,7 @@ deleteObjectsByIdPrefix(objectIdfix:fromTable:)
// 获得指定key的数据
getYTKKeyValueItemById(objectId:fromTable:)->YTKKeyValueItem_Swift?
// 获得所有数据
getAllItemsFromTable(tableName:)->[AnyObject]?
getAllItemsFromTable(tableName:)->[YTKKeyValueItem_Swift]?
```

由于`YTKKeyValueItem_Swift`类带有`createdTime`字段,可以获得该条数据的插入(或更新)时间,以便上层做复杂的处理(例如用来做缓存过期逻辑)。
Binary file not shown.
3 changes: 2 additions & 1 deletion YTKKeyValueStore_Swift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
let tableName = "user_table_swift"
var store = YTKKeyValueStore_Swift(dbName: "test_siwft.db")
store.createTable(tableName: tableName)
store.clearTable(tableName: tableName)
let key = "1"
let user = ["id":1 , "name" : "tangqiao" , "age" : 30]
store.putObject(user, withId: key, intoTable: tableName)

if let queryUser: AnyObject = store.getObjectById(key, fromTable: tableName){
if let queryUser: AnyObject = store.getObjectById(key, fromTable: tableName)?.dictionaryValue{
println("[swift] query data result: \(queryUser)")
}

Expand Down
232 changes: 131 additions & 101 deletions YTKKeyValueStore_Swift/YTKKeyValueStore_Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import UIKit


public class YTKKeyValueItem_Swift:NSObject{
var itemId : String?
var itemObject : AnyObject?
var createdTime : NSDate?
public var itemId : String?
public var itemObject : YTKObject?
public var createdTime : NSDate?

func description() -> String{
public func description() -> String{
return "id=\(itemId), value=\(itemObject), timeStamp=\(createdTime)"
}

Expand Down Expand Up @@ -116,31 +116,56 @@ public class YTKKeyValueStore_Swift: NSObject {

//MARK: 对象

private enum YTKKeyValueType{
case String,Number,Object
}

private class func valueWithType(object : AnyObject!)->YTKKeyValueType{
if object is String{
return .String
}else if object as? NSNumber != nil{
return .Number
}else{
return .Object
}
}

/**
加入数据

:param: object 数据
:param: objectId 数据索引
:param: tableName 表单名
*/

public func putObject(object : AnyObject! , withId objectId: String! , intoTable tableName: String!){
if !YTKKeyValueStore_Swift.checkTableName(tableName){
return
}
var error : NSError?
var data = NSJSONSerialization.dataWithJSONObject(object, options: NSJSONWritingOptions(0), error: &error)
if error != nil {
println("error, faild to get json data")
return

let type = YTKKeyValueStore_Swift.valueWithType(object)
var jsonString : String?

if type == .Number || type == .Object{
let sqlObject: AnyObject! = type == .Number ? [object] : object
var error : NSError?
let data = NSJSONSerialization.dataWithJSONObject(sqlObject, options: NSJSONWritingOptions(0), error: &error)
if error != nil {
println("error, faild to get json data")
return
}
jsonString = NSString(data: data!, encoding: NSUTF8StringEncoding)
}else{
let jsonString = NSString(data: data!, encoding: NSUTF8StringEncoding)
let createTime = NSDate()
let sql = NSString(format: UPDATE_ITEM_SQL, tableName)
var result : Bool?
dbQueue?.inDatabase({ (db) -> Void in
result = db.executeUpdate(sql, withArgumentsInArray:[objectId,jsonString!,createTime])
})
jsonString = object as? String
}

let createTime = NSDate()
let sql = NSString(format: UPDATE_ITEM_SQL, tableName)
var result : Bool?
dbQueue?.inDatabase({ (db) -> Void in
result = db.executeUpdate(sql, withArgumentsInArray:[objectId,jsonString!,createTime])
})

}


Expand All @@ -152,7 +177,7 @@ public class YTKKeyValueStore_Swift: NSObject {

:returns: 对象数据
*/
public func getObjectById(objectId : String! , fromTable tableName : String! )->AnyObject?{
public func getObjectById(objectId : String! , fromTable tableName : String! )->YTKObject?{
let item = self.getYTKKeyValueItemById(objectId, fromTable: tableName)
if item != nil {
return item!.itemObject
Expand Down Expand Up @@ -184,81 +209,16 @@ public class YTKKeyValueStore_Swift: NSObject {
rs.close()
})
if json != nil{
var error : NSError?
var result: AnyObject? = NSJSONSerialization.JSONObjectWithData(json!.dataUsingEncoding(NSUTF8StringEncoding)!, options: NSJSONReadingOptions.AllowFragments, error: &error)
if error != nil{
println("error, faild to prase to json")
return nil
}
var item = YTKKeyValueItem_Swift()
item.itemId = objectId
item.itemObject = result!
item.itemObject = YTKObject(value: json! )
item.createdTime = createdTime
return item
}else{
return nil
}
}

//MARK: 字符串

/**
插入字符串

:param: string 字符串
:param: stringId 索引
:param: tableName 表单名
*/
public func putString(string : String! , withId stringId : String! , intoTable tableName:String!){
self.putObject([string], withId: stringId, intoTable: tableName)
}

/**
获取字符串

:param: stringId 索引
:param: tableName 表单名

:returns: 字符串
*/
public func getStringById(stringId : String! , fromTable tableName : String!)->String?{
let array : AnyObject? = self.getObjectById(stringId, fromTable: tableName)
if let result = array as? [String]{
return result[0]
}else{
return nil
}
}

//MARK: 数组

/**
插入数字

:param: number 数字
:param: numberId 索引
:param: tableName 表单名
*/
public func putNumber(number : CGFloat! , withId numberId : String! , intoTable tableName : String!){
self.putObject([number], withId: numberId, intoTable: tableName)
}

/**
获取数字

:param: numberId 索引
:param: tableName 表单名

:returns: 数字
*/
public func getNumberById(numberId : String! , fromTable tableName : String!)->CGFloat?{
let array : AnyObject? = self.getObjectById(numberId, fromTable: tableName)
if let result = array as? [CGFloat] {
return result[0]
}else{
return nil
}
}

//MARK: 其他

Expand All @@ -269,38 +229,25 @@ public class YTKKeyValueStore_Swift: NSObject {

:returns: 所有数据
*/
public func getAllItemsFromTable(tableName : String!)->[AnyObject]?{
public func getAllItemsFromTable(tableName : String!)->[YTKKeyValueItem_Swift]?{
if !YTKKeyValueStore_Swift.checkTableName(tableName){
return nil
}
let sql = NSString(format: SELECT_ALL_SQL, tableName)
var result : [AnyObject] = []
var result : [YTKKeyValueItem_Swift] = []
dbQueue?.inDatabase({ (db) -> Void in
var rs : FMResultSet = db.executeQuery(sql, withArgumentsInArray: nil)
while(rs.next()){
var item = YTKKeyValueItem_Swift()
item.itemId = rs.stringForColumn("id")
item.itemObject = rs.stringForColumn("json")
item.itemObject = YTKObject(value:rs.stringForColumn("json"))
item.createdTime = rs.dateForColumn("createdTime")
result.append(item)
}
rs.close()
})
var error : NSError?

for i in 0..<result.count {
var item: YTKKeyValueItem_Swift = result[i] as YTKKeyValueItem_Swift
error = nil
var object: AnyObject? = NSJSONSerialization.JSONObjectWithData(item.itemObject!.dataUsingEncoding(NSUTF8StringEncoding)!, options: NSJSONReadingOptions.AllowFragments, error: &error)
if error != nil {
println("error, faild to prase to json.")
}else{
item.itemObject = object!
result[i] = item
}
}

return result

return result == [] ? nil : result
}

/**
Expand Down Expand Up @@ -383,3 +330,86 @@ public class YTKKeyValueStore_Swift: NSObject {

}

//MARK: - 数据库的对象类型

public struct YTKObject{
private var value : AnyObject?

public var objectValue : AnyObject?{
get{
return self.value
}
}

public var stringValue : String? {
get{
return self.value as? String
}
}

public var numberValue : NSNumber?{

get{
if self.value == nil { return nil}

if let num = self.value as? NSNumber{
return num
}else{

var error : NSError?
let result: AnyObject? = NSJSONSerialization.JSONObjectWithData(self.value!.dataUsingEncoding(NSUTF8StringEncoding)!, options: NSJSONReadingOptions.AllowFragments, error: &error)
if error != nil{
return nil
}else{
if let num = result as? [NSNumber]{
return num[0]
}else{
return nil
}
}
}
}

}

public var dictionaryValue : Dictionary<String , AnyObject>?{
get{
if self.value == nil { return nil}

var error : NSError?
let result: AnyObject? = NSJSONSerialization.JSONObjectWithData(self.value!.dataUsingEncoding(NSUTF8StringEncoding)!, options: NSJSONReadingOptions.AllowFragments, error: &error)
if error != nil{
return nil
}else{
if let dic = result as? Dictionary<String , AnyObject>{
return dic
}else{
return nil
}
}

}
}


public var arrayValue : Array<AnyObject>?{
get{
if self.value == nil { return nil}

var error : NSError?
let result: AnyObject? = NSJSONSerialization.JSONObjectWithData(self.value!.dataUsingEncoding(NSUTF8StringEncoding)!, options: NSJSONReadingOptions.AllowFragments, error: &error)
if error != nil{
return nil
}else{
if let dic = result as? Array<AnyObject>{
return dic
}else{
return nil
}
}
}
}


}

Loading

0 comments on commit 07e5e38

Please sign in to comment.