                 bridging_sqlite3_finalize(statement)
             }
         })
-        
+
         return result
     }
-    
+
     private func removeRecordsForRecordId(recordId: AnyObject, fileLink: String) {
-        
+
         fileLink.cacheDBFileLinkRemoveFileWithFolder(self.db.folder)
-        
+
         let removeQuery = "DELETE FROM records WHERE record_id='\(recordId)';"
-        
-        dispatch_barrier_async(db.dispatchQueue, {
-            
+
+        dispatch_barrier_async(db.dispatchQueue, {
+
             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)
-            }
-        })
-    }
-    
+
+                bridging_sqlite3_finalize(statement)
+            }
+        })
+    }
+
     private func addData(data: NSData, forRecord recordId: String) {
-        
+
         let fileLink = NSUUID().UUIDString
-        
+
         let addQuery = "INSERT INTO records (record_id, file_link, update_time, access_time) VALUES ('\(recordId)', '\(fileLink)', '\(currentTime)', '\(currentTime)');"
-        
-        dispatch_barrier_async(db.dispatchQueue, {
-            
+
+        dispatch_barrier_async(db.dispatchQueue, {
+
             var statement: COpaquePointer = nil
             if self.prepareQuery(addQuery, statement:&statement) {
                 if bridging_sqlite3_step(statement) == BRIDGING_SQLITE_DONE {

            }
        })
    }
    
    func prepareQuery(sql: String, statement: UnsafeMutablePointer<COpaquePointer>) -> Bool {
        
        return sql.withCString { (cStr: UnsafePointer<Int8>) -> Bool in
            
            return bridging_sqlite3_prepare_v2(self.db, cStr, -1, statement, nil) == BRIDGING_SQLITE_OK
        }
    }
    
    func execQuery(sql: String) -> Bool {
        
        return sql.withCString { (cStr: UnsafePointer<Int8>) -> Bool in
            
            var errorMessage: UnsafeMutablePointer<Int8> = nil
            
            if bridging_sqlite3_exec(self.db, cStr, nil, nil, &errorMessage) != BRIDGING_SQLITE_OK {
                
                let logStr = "\(sql) error: \(errorMessage)"
                NSLog(logStr)
                bridging_sqlite3_free(errorMessage)
                return false
            }
            
            return true
        }
    }
    
    var errorMessage: String? {
        return String.fromCString(bridging_sqlite3_errmsg(db))
    }

    }

    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)
