                 } else {
                     NSLog("\(addQuery) - \(self.errorMessage)")
                 }
-                
+
                 bridging_sqlite3_finalize(statement)
             } else {
                 NSLog("\(addQuery) - \(self.errorMessage)")
             }
         })
     }
-    
+
     //JTODO test !!!!
     private func removeRecordsToDate(date: NSDate, dateFieldName fieldName: String) {
-        
+
         ///First remove all files
         let query = "SELECT file_link FROM records WHERE \(fieldName) < '\(date.timeIntervalSince1970)';"
-        
-        dispatch_barrier_async(db.dispatchQueue, {
-            
-            var statement: COpaquePointer = nil
-            
+
+        dispatch_barrier_async(db.dispatchQueue, {
+
+            var statement: COpaquePointer = nil
+
             if self.db.prepareQuery(query, statement:&statement) {
                 while bridging_sqlite3_step(statement) == BRIDGING_SQLITE_ROW {
-                    
+
                     autoreleasepool {
-                        
+
                         let str = bridging_sqlite3_column_text(statement, 0)
                         let fileLink = String.fromCString(UnsafePointer<CChar>(str))!
-                        
+
                         fileLink.cacheDBFileLinkRemoveFileWithFolder(self.db.folder)
                     }
                 }
                 bridging_sqlite3_finalize(statement)
             }
-            
+
             //////////
-            
+
             let removeQuery = "DELETE FROM records WHERE \(fieldName) < '\(date.timeIntervalSince1970)';"
-            
+
             var queryStatement: COpaquePointer = nil
-            
+
             if self.prepareQuery(removeQuery, statement:&queryStatement) {
                 if bridging_sqlite3_step(queryStatement) != BRIDGING_SQLITE_DONE {
                     NSLog("\(removeQuery) - \(self.errorMessage)")
                 }
-                
+
                 bridging_sqlite3_finalize(queryStatement)
             }
         })
     }
-    
+
     private func folderSize() -> Int64 {
-        
+
         let folderPath  = self.db.folder
         let fileManager = NSFileManager.defaultManager()
         let filesEnumerator = fileManager.enumeratorAtPath(folderPath)
-        
+
         var fileSize: Int64 = 0
-        
+
         if let filesEnumerator = filesEnumerator {
 
             while let fileName = filesEnumerator.nextObject() as? String {

                    bridging_sqlite3_finalize(statement)
                }
            }
            
            //////////
            
            if filesRemoved > 0 {
                
                let removeQuery = "DELETE FROM records WHERE file_link IN (\(selectQuery) LIMIT \(filesRemoved));"
                
                var statement: COpaquePointer = nil
                if self.prepareQuery(removeQuery, statement:&statement) {
                    if bridging_sqlite3_step(statement) != BRIDGING_SQLITE_DONE {
                        NSLog("\(removeQuery) - \(self.errorMessage)")
                    }
                    
                    bridging_sqlite3_finalize(statement)
                }
            }
        })
    }
    
    func removeAllRecordsWithCallback(callback: SimpleBlock?) {
        
        ///First remove all files
        let query = "SELECT file_link FROM records;"
        
        dispatch_barrier_async(db.dispatchQueue, {
            
            var statement: COpaquePointer = nil
            if self.db.prepareQuery(query, statement:&statement) {
                while bridging_sqlite3_step(statement) == BRIDGING_SQLITE_ROW {
                    
                    autoreleasepool {
                        
                        let str = bridging_sqlite3_column_text(statement, 0)
                        let fileLink = String.fromCString(UnsafePointer<CChar>(str))!
                        
                        //JTODO remove files in separate tread, do nont wait it
                        fileLink.cacheDBFileLinkRemoveFileWithFolder(self.db.folder)
                    }
                }
                bridging_sqlite3_finalize(statement)
            }
            
            // remove records in sqlite
            let removeQuery = "DELETE * FROM records;"
            
            if self.prepareQuery(removeQuery, statement:&statement) {
                if bridging_sqlite3_step(statement) != BRIDGING_SQLITE_DONE {
                    NSLog("\(removeQuery) - \(self.errorMessage)")
                }
                
                bridging_sqlite3_finalize(statement)
            }
            
            callback?()
        })
    }
    
    private var currentTime: NSTimeInterval {
        return NSDate().timeIntervalSince1970
    }
    
    private func execQuery(sql: String) -> Bool {
        return db.execQuery(sql)
    }
    
    private func prepareQuery(sql: String, statement: UnsafeMutablePointer<COpaquePointer>) -> Bool {
        return db.prepareQuery(sql, statement: statement)
    }
    
    private var errorMessage: String? {
        return db.errorMessage
    }
    
    private func updateAccessTime(recordID: String) {
        
        dispatch_barrier_async(db.dispatchQueue, {
            self.execQuery("UPDATE records SET access_time='\(self.currentTime)' WHERE record_id='\(recordID)';")
            return ()
        })
    }
    
    private func fileLinkForRecordId(recordId: String) -> String? {
        
        let query = "SELECT file_link FROM records WHERE record_id='\(recordId)';"
        
        var result: String?
        
        dispatch_sync(db.dispatchQueue, {
            
            var statement: COpaquePointer = nil
            if self.db.prepareQuery(query, statement:&statement) {
                if bridging_sqlite3_step(statement) == BRIDGING_SQLITE_ROW {

    }

    func dataForKey(key: String) -> NSData? {

        let result = dataAndLastUpdateDateForKey(key)
        return result?.0
    }

    func dataAndLastUpdateDateForKey(recordId: String) -> (NSData, NSDate)? {

        let linkIndex: Int32 = 0
        let dateIndex: Int32 = 1

        let query = "SELECT file_link, update_time FROM records WHERE record_id='\(recordId)';";

        var result: (NSData, NSDate)?

        dispatch_sync(db.dispatchQueue, {

            //var statement: UnsafeMutablePointer<Void> = nil
            var statement: COpaquePointer = nil

            if self.db.prepareQuery(query, statement:&statement) {
                if bridging_sqlite3_step(statement) == BRIDGING_SQLITE_ROW {

                    let str = bridging_sqlite3_column_text(statement, linkIndex)
                    let fileLink = String.fromCString(UnsafePointer<CChar>(str))!
                    let data = fileLink.cacheDBFileLinkDataWithFolder(self.db.folder)

                    if let data = data {
                        let dateInetrval = bridging_sqlite3_column_double(statement, dateIndex)
                        let updateDate = NSDate(timeIntervalSince1970:dateInetrval)
