<commit_before>//
//  YTKTable.swift
//  YTKKeyValueStore
//
//  Created by ysq on 15/2/11.
//  Copyright (c) 2015年 sgxiang. All rights reserved.
//

import UIKit
import SQLite


public struct YTKTable{
    
    public let name : String?
    
    public var isExists : Bool {
        get{
            return self.tableHandle != nil
        }
    }
    
    internal let db : Connection?
    internal let tableHandle : Table?
    
    internal init(db : Connection?, _ tableName : String!){
        if YTKTable.checkTableName(tableName){
            self.db = db
            self.name = tableName
            self.tableHandle = Table(tableName)
        }else{
            self.db = nil
            self.name = nil
            self.tableHandle = nil
        }
    }
    
    internal static func checkTableName(tableName : String!)->Bool{
        if tableName.containsString(" "){
            print("table name : \(tableName) format error")
            return false
        }
        return true
    }
    
    public func clear() throws ->Int{
        
        do{
            let changes = try db?.run(tableHandle!.delete()) ?? 0
            print("table : \(self.name)  number of deleted rows : \(changes)")
            return changes
        }catch let error{
            throw error
        }
        
    }
    
    public func delete(objectIds : String... ) throws -> Int{
        
        do{
            let changes = try db?.run(tableHandle!.filter(objectIds.contains(ID)).delete()) ?? 0
            print("table : \(self.name)  number of deleted rows : \(changes)")
            return changes
        }catch let error{
            throw error
        }

    }
    
    public func deletePreLike(objectId : String!) throws -> Int{
        
        do{
            let changes = try db?.run(tableHandle!.filter(ID.like("\(objectId)%")).delete()) ?? 0
            print("table : \(self.name)  number of deleted rows : \(changes)")
            return changes
        }catch let error{
            throw error
        }
    }
    
    private enum YTKKeyValueType{
        case String,Number,Object
    }
    
    private static func valueWithType(object : AnyObject!)->YTKKeyValueType{
        if object is String{
            return .String
        }else if (object as? NSNumber) != nil{
            return .Number
        }else{
            return .Object
        }
    }
    
    public func put( set :  YTKSetter ) throws{
        
        guard let jsonString : String = set.jsonString else{
            throw YTKError.ValueNoSupport
        }
        
        let query = tableHandle!.filter(ID == set.objectId).limit(1)
        
        if let filter = db?.prepare(query){
            if filter.underestimateCount() == 0{
                do{
                    try db?.run( tableHandle!.insert(ID <- set.objectId,JSON <- jsonString,CREATEDTIME <- NSDate()) )
                    print("[insert] id : \(set.objectId)  jsonString : \(set.jsonString!)")
                }catch let error{
                    throw error
                }
            }else{
                do{
                    try db?.run(query.update(JSON <- jsonString,CREATEDTIME <- NSDate()))
                    print("[update] id : \(set.objectId)  jsonString : \(set.jsonString!)")
                }catch let error{
                    throw error
                }
            }
        }
        
    }
    
    public func get( objectId : String! ) -> YTKObject?{
        if let item = self.getItem(objectId){
            return item.itemObject
        }
        return nil
    }
    
    public func getItem(objectId :String!)->YTKItem?{
        
        if let filter = db?.prepare( tableHandle!.filter(ID == objectId).limit(1) ){
            for v in filter{
                var item = YTKItem()
                item.itemId = objectId<commit_msg>Update YTKTable.swift<commit_after>//
//  YTKTable.swift
//  YTKKeyValueStore
//
//  Created by ysq on 15/2/11.
//  Copyright (c) 2015年 sgxiang. All rights reserved.
//

import UIKit
import SQLite


public struct YTKTable{
    
    public let name : String?
    
    public var isExists : Bool {
        get{
            return self.tableHandle != nil
        }
    }
    
    internal let db : Connection?
    internal let tableHandle : Table?
    
    internal init(db : Connection?, _ tableName : String!){
        if YTKTable.checkTableName(tableName){
            self.db = db
            self.name = tableName
            self.tableHandle = Table(tableName)
        }else{
            self.db = nil
            self.name = nil
            self.tableHandle = nil
        }
    }
    
    internal static func checkTableName(tableName : String!)->Bool{
        if tableName.containsString(" "){
            print("table name : \(tableName) format error")
            return false
        }
        return true
    }
    
    public func clear() throws ->Int{
        
        do{
            let changes = try db?.run(tableHandle!.delete()) ?? 0
            print("table : \(self.name)  number of deleted rows : \(changes)")
            return changes
        }catch let error{
            throw error
        }
        
    }
    
    public func delete(objectIds : String... ) throws -> Int{
        
        do{
            let changes = try db?.run(tableHandle!.filter(objectIds.contains(ID)).delete()) ?? 0
            print("table : \(self.name)  number of deleted rows : \(changes)")
            return changes
        }catch let error{
            throw error
        }

    }
    
    public func deletePreLike(objectId : String!) throws -> Int{
        
        do{
            let changes = try db?.run(tableHandle!.filter(ID.like("\(objectId)%")).delete()) ?? 0
            print("table : \(self.name)  number of deleted rows : \(changes)")
            return changes
        }catch let error{
            throw error
        }
    }
    
    private enum YTKKeyValueType{
        case String,Number,Object
    }
    
    private static func valueWithType(object : AnyObject!)->YTKKeyValueType{
        if object is String{
            return .String
        }else if (object as? NSNumber) != nil{
            return .Number
        }else{
            return .Object
        }
    }
    
    public func put( set :  YTKSetter ) throws{
        
        guard let jsonString : String = set.jsonString else{
            throw YTKError.ValueNoSupport
        }
        
        let query = tableHandle!.filter(ID == set.objectId).limit(1)
        
        if let filter = db?.prepare(query){
            if filter.generate().next() == nil{
                do{
                    try db?.run( tableHandle!.insert(ID <- set.objectId,JSON <- jsonString,CREATEDTIME <- NSDate()) )
                    print("[insert] id : \(set.objectId)  jsonString : \(set.jsonString!)")
                }catch let error{
                    throw error
                }
            }else{
                do{
                    try db?.run(query.update(JSON <- jsonString,CREATEDTIME <- NSDate()))
                    print("[update] id : \(set.objectId)  jsonString : \(set.jsonString!)")
                }catch let error{
                    throw error
                }
            }
        }
        
    }
    
    public func get( objectId : String! ) -> YTKObject?{
        if let item = self.getItem(objectId){
            return item.itemObject
        }
        return nil
    }
    
    public func getItem(objectId :String!)->YTKItem?{
        
        if let filter = db?.prepare( tableHandle!.filter(ID == objectId).limit(1) ){
            for v in filter{
                var item = YTKItem()
                item.itemId = objectId
{'fraction_non_alphanumeric': 0.10540013012361743, 'fraction_numerical': 0.0037735849056603774, 'mean_word_length': 1.4431023521932613, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4732595', 'n_tokens_mistral': 2160, 'n_tokens_neox': 2062, 'n_words': 546}