Pārlūkot izejas kodu

Revert "Revert "Framework branch""

This reverts commit 5eeda4f0b57b407c5d0eaebba0192ca45ddc1e1e.
August Mueller 10 gadi atpakaļ
vecāks
revīzija
43881cfcff

+ 1 - 0
FMDB.podspec

@@ -12,6 +12,7 @@ Pod::Spec.new do |s|
 
   s.subspec 'common' do |ss|
     ss.source_files = 'src/fmdb/FM*.{h,m}'
+    ss.private_header_files = 'src/fmdb/*Private.h'
     ss.exclude_files = 'src/fmdb.m'
   end
 

+ 72 - 1
README.markdown

@@ -319,6 +319,78 @@ if let rs = database.executeQuery("select x, y, z from test", withArgumentsInArr
 database.close()
 ```
 
+Then you can bring it up on the FMDB mailing list by showing your nice and compact FMDBReportABugFunction, or you can report the bug via the github FMDB bug reporter.
+
+## Using FMDB in a framework
+
+1. Clone FMDB repo.
+
+2. Switch to “framework” branch.
+
+3. In your project create a new target (“File” - “New” - “Target…”) and choose “Framework". Call this new target “FMDB” (and make sure to specify language of Objective-C, regardless of the base language for your main project)
+
+4. Copy the contents of the `src/fmdb` directory into your new framework target. **Do not copy the `FMDB.h` file, though.**
+
+5. Edit the `FMDB.h` file in the framework to include whatever headers you want, e.g. 
+
+ ```//
+//  FMDB.h
+//  FMDB
+//
+//  Created by You on 8/2/15.
+//  Copyright (c) 2015 You. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+//! Project version number for FMDB.
+FOUNDATION_EXPORT double FMDBVersionNumber;
+
+//! Project version string for FMDB.
+FOUNDATION_EXPORT const unsigned char FMDBVersionString[];
+
+// In this header, you should import all the public headers of your framework using statements like #import <FMDB/PublicHeader.h>
+
+#import "FMDatabase.h"
+#import "FMResultSet.h"
+#import "FMDatabaseAdditions.h"
+#import "FMDatabaseQueue.h"
+```
+
+6. Whichever headers you include in this `FMDB.h` must be changed to “public” headers as discussed here: http://stackoverflow.com/a/24317456/1271826
+
+7. Remember to link the `libsqlite3.dylib` with your new FMDB framework target.
+
+8. You can now use this new FMDB framework from your main target. For example, in Swift:
+
+        import UIKit
+        import FMDB
+
+        class ViewController: UIViewController {
+
+            override func viewDidLoad() {
+                super.viewDidLoad()
+
+                let documents = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
+                let path = documents.stringByAppendingPathComponent("test.sqlite")
+                NSFileManager.defaultManager().removeItemAtPath(path, error: nil)
+
+                let queue = FMDatabaseQueue(path: path)
+
+                queue.inDatabase { db in
+                    db.executeUpdate("create table foo (bar text)", withArgumentsInArray: nil)
+                    db.executeUpdate("insert into foo (bar) values (?)", withArgumentsInArray: ["baz"])
+
+                    if let rs = db.executeQuery("select * from foo", withArgumentsInArray: nil) {
+                        while rs.next() {
+                            println(rs.resultDictionary())
+                        }
+                    }
+                }
+            }
+        }
+
+
 ## History
 
 The history and changes are availbe on its [GitHub page](https://github.com/ccgus/fmdb) and are summarized in the "CHANGES_AND_TODO_LIST.txt" file.
@@ -348,7 +420,6 @@ And we've even added a template function to main.m (FMDBReportABugFunction) in t
     * Make your query or update(s).
     * Add some assertions which demonstrate the bug.
 
-Then you can bring it up on the FMDB mailing list by showing your nice and compact FMDBReportABugFunction, or you can report the bug via the github FMDB bug reporter.
 
 **Optional:**
 

+ 6 - 0
fmdb.xcodeproj/project.pbxproj

@@ -16,6 +16,7 @@
 		6290CBB7188FE836009790F8 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6290CBB6188FE836009790F8 /* Foundation.framework */; };
 		67CB1E3019AD27D000A3CA7F /* FMDatabaseFTS3Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = 67CB1E2F19AD27D000A3CA7F /* FMDatabaseFTS3Tests.m */; };
 		8314AF3318CD73D600EC0E25 /* FMDB.h in Headers */ = {isa = PBXBuildFile; fileRef = 8314AF3218CD73D600EC0E25 /* FMDB.h */; };
+		83D9D8CF1B6E7DC50083E17F /* FMDatabase+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 83D9D8CE1B6E7DC50083E17F /* FMDatabase+Private.h */; };
 		8DD76F9C0486AA7600D96B5E /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 08FB779EFE84155DC02AAC07 /* Foundation.framework */; };
 		8DD76F9F0486AA7600D96B5E /* fmdb.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = C6859EA3029092ED04C91782 /* fmdb.1 */; };
 		BF5D041918416BB2008C5AA9 /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BF5D041818416BB2008C5AA9 /* XCTest.framework */; };
@@ -99,6 +100,8 @@
 		831DE6FD175B7C9C001F7317 /* README.markdown */ = {isa = PBXFileReference; lastKnownFileType = text; path = README.markdown; sourceTree = "<group>"; };
 		832F502419EC4C6B0087DCBF /* FMDatabaseVariadic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = FMDatabaseVariadic.swift; path = "src/extra/Swift extensions/FMDatabaseVariadic.swift"; sourceTree = "<group>"; };
 		8352D5AC1A73DCEA003A8E09 /* FMDatabaseAdditionsVariadic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = FMDatabaseAdditionsVariadic.swift; path = "src/extra/Swift extensions/FMDatabaseAdditionsVariadic.swift"; sourceTree = "<group>"; };
+		83D9D8CE1B6E7DC50083E17F /* FMDatabase+Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "FMDatabase+Private.h"; path = "src/fmdb/FMDatabase+Private.h"; sourceTree = "<group>"; };
+		83D9D8D01B6E7F6D0083E17F /* FMDB.podspec */ = {isa = PBXFileReference; lastKnownFileType = text; path = FMDB.podspec; sourceTree = "<group>"; };
 		8DD76FA10486AA7600D96B5E /* fmdb */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = fmdb; sourceTree = BUILT_PRODUCTS_DIR; };
 		BF5D041618416BB2008C5AA9 /* Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
 		BF5D041818416BB2008C5AA9 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
@@ -187,6 +190,7 @@
 				CC8C138B0E3135C400FBE1E7 /* LICENSE.txt */,
 				CC8C138A0E3135C400FBE1E7 /* CHANGES_AND_TODO_LIST.txt */,
 				CC8C138C0E3135C400FBE1E7 /* CONTRIBUTORS.txt */,
+				83D9D8D01B6E7F6D0083E17F /* FMDB.podspec */,
 				08FB7795FE84155DC02AAC07 /* Source */,
 				C6859EA2029092E104C91782 /* Documentation */,
 				08FB779DFE84155DC02AAC07 /* External Frameworks and Libraries */,
@@ -247,6 +251,7 @@
 				8314AF3218CD73D600EC0E25 /* FMDB.h */,
 				CCC24EBA0A13E34D00A6D3E3 /* FMDatabase.h */,
 				CCC24EBB0A13E34D00A6D3E3 /* FMDatabase.m */,
+				83D9D8CE1B6E7DC50083E17F /* FMDatabase+Private.h */,
 				CCC24EBF0A13E34D00A6D3E3 /* FMResultSet.h */,
 				CCC24EC00A13E34D00A6D3E3 /* FMResultSet.m */,
 				CC47A00D148581E9002CCDAB /* FMDatabaseQueue.h */,
@@ -351,6 +356,7 @@
 				EE42910712B42FC90088BD94 /* FMDatabase.h in Headers */,
 				EE42910612B42FC30088BD94 /* FMDatabaseAdditions.h in Headers */,
 				EE42910912B42FD00088BD94 /* FMResultSet.h in Headers */,
+				83D9D8CF1B6E7DC50083E17F /* FMDatabase+Private.h in Headers */,
 				8314AF3318CD73D600EC0E25 /* FMDB.h in Headers */,
 				CC9E4EBA13B31188005F9210 /* FMDatabasePool.h in Headers */,
 				CC47A00F148581E9002CCDAB /* FMDatabaseQueue.h in Headers */,

+ 39 - 0
src/fmdb/FMDatabase+Private.h

@@ -0,0 +1,39 @@
+//
+//  FMDatabase+Private.h
+//  deleteme2
+//
+//  Created by Robert Ryan on 8/2/15.
+//  Copyright (c) 2015 Robert Ryan. All rights reserved.
+//
+
+#ifndef deleteme2_FMDatabase_Private_h
+#define deleteme2_FMDatabase_Private_h
+
+#import <sqlite3.h>
+
+@class FMDatabase;
+@class FMStatement;
+
+@interface FMDatabase (Private)
+
+/** SQLite sqlite3
+ 
+ @see [`sqlite3`](http://www.sqlite.org/c3ref/sqlite3.html)
+ */
+
+@property (nonatomic, assign, readonly) sqlite3 *db;
+
+@end
+
+@interface FMStatement (Private)
+
+/** SQLite sqlite3_stmt
+ 
+ @see [`sqlite3_stmt`](http://www.sqlite.org/c3ref/stmt.html)
+ */
+
+@property (nonatomic, assign) sqlite3_stmt *statement;
+
+@end
+
+#endif

+ 6 - 12
src/fmdb/FMDatabase.h

@@ -1,5 +1,4 @@
 #import <Foundation/Foundation.h>
-#import "sqlite3.h"
 #import "FMResultSet.h"
 #import "FMDatabasePool.h"
 
@@ -73,7 +72,6 @@ typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary
 
 @interface FMDatabase : NSObject  {
     
-    sqlite3*            _db;
     NSString*           _databasePath;
     BOOL                _logsErrors;
     BOOL                _crashOnErrors;
@@ -217,12 +215,12 @@ typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary
  @see [sqlite3_open_v2()](http://sqlite.org/c3ref/open.html)
  @see open
  @see close
+ 
+ @warning Requires SQLite 3.5
  */
 
-#if SQLITE_VERSION_NUMBER >= 3005000
 - (BOOL)openWithFlags:(int)flags;
 - (BOOL)openWithFlags:(int)flags vfs:(NSString *)vfsName;
-#endif
 
 /** Closing a database connection
  
@@ -440,7 +438,7 @@ typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary
 
  */
 
-- (sqlite_int64)lastInsertRowId;
+- (long long int)lastInsertRowId;
 
 /** The number of rows changed by prior SQL statement.
  
@@ -730,7 +728,7 @@ typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary
  
  */
 
-- (sqlite3*)sqliteHandle;
+- (void*)sqliteHandle;
 
 
 ///-----------------------------
@@ -794,8 +792,6 @@ typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary
 - (NSTimeInterval)maxBusyRetryTimeInterval;
 
 
-#if SQLITE_VERSION_NUMBER >= 3007000
-
 ///------------------
 /// @name Save points
 ///------------------
@@ -857,7 +853,6 @@ typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary
 
 - (NSError*)inSavePoint:(void (^)(BOOL *rollback))block;
 
-#endif
 
 ///----------------------------
 /// @name SQLite library status
@@ -935,7 +930,7 @@ typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary
  @see [sqlite3_create_function()](http://sqlite.org/c3ref/create_function.html)
  */
 
-- (void)makeFunctionNamed:(NSString*)name maximumArguments:(int)count withBlock:(void (^)(sqlite3_context *context, int argc, sqlite3_value **argv))block;
+- (void)makeFunctionNamed:(NSString*)name maximumArguments:(int)count withBlock:(void (^)(void *context, int argc, void **argv))block;
 
 
 ///---------------------
@@ -1039,7 +1034,6 @@ typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary
  */
 
 @interface FMStatement : NSObject {
-    sqlite3_stmt *_statement;
     NSString *_query;
     long _useCount;
     BOOL _inUse;
@@ -1062,7 +1056,7 @@ typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary
  @see [`sqlite3_stmt`](http://www.sqlite.org/c3ref/stmt.html)
  */
 
-@property (atomic, assign) sqlite3_stmt *statement;
+@property (atomic, assign) void *statement;
 
 /** Indication of whether the statement is in use */
 

+ 11 - 4
src/fmdb/FMDatabase.m

@@ -1,9 +1,12 @@
 #import "FMDatabase.h"
 #import "unistd.h"
 #import <objc/runtime.h>
+#import "FMDatabase+Private.h"
 
 @interface FMDatabase ()
 
+@property (nonatomic, assign) sqlite3 *db;
+
 - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arrayArgs orDictionary:(NSDictionary *)dictionaryArgs orVAList:(va_list)args;
 - (BOOL)executeUpdate:(NSString*)sql error:(NSError**)outErr withArgumentsInArray:(NSArray*)arrayArgs orDictionary:(NSDictionary *)dictionaryArgs orVAList:(va_list)args;
 
@@ -109,7 +112,7 @@ + (BOOL)isSQLiteThreadSafe {
     return sqlite3_threadsafe() != 0;
 }
 
-- (sqlite3*)sqliteHandle {
+- (void*)sqliteHandle {
     return _db;
 }
 
@@ -149,11 +152,11 @@ - (BOOL)open {
     return YES;
 }
 
-#if SQLITE_VERSION_NUMBER >= 3005000
 - (BOOL)openWithFlags:(int)flags {
     return [self openWithFlags:flags vfs:nil];
 }
 - (BOOL)openWithFlags:(int)flags vfs:(NSString *)vfsName; {
+#if SQLITE_VERSION_NUMBER >= 3005000
     if (_db) {
         return YES;
     }
@@ -170,9 +173,13 @@ - (BOOL)openWithFlags:(int)flags vfs:(NSString *)vfsName; {
     }
     
     return YES;
-}
+#else 
+    NSLog(@"Requires SQLite 3.5; will just open");
+    return [self open];
 #endif
 
+}
+
 
 - (BOOL)close {
     
@@ -1364,7 +1371,7 @@ void FMDBBlockSQLiteCallBackFunction(sqlite3_context *context, int argc, sqlite3
 }
 
 
-- (void)makeFunctionNamed:(NSString*)name maximumArguments:(int)count withBlock:(void (^)(sqlite3_context *context, int argc, sqlite3_value **argv))block {
+- (void)makeFunctionNamed:(NSString*)name maximumArguments:(int)count withBlock:(void (^)(void *context, int argc, void **argv))block {
     
     if (!_openFunctions) {
         _openFunctions = [NSMutableSet new];

+ 0 - 4
src/fmdb/FMDatabaseAdditions.h

@@ -209,8 +209,6 @@
 - (BOOL)validateSQL:(NSString*)sql error:(NSError**)error;
 
 
-#if SQLITE_VERSION_NUMBER >= 3007017
-
 ///-----------------------------------
 /// @name Application identifier tasks
 ///-----------------------------------
@@ -254,8 +252,6 @@
 - (void)setApplicationIDString:(NSString*)string;
 #endif
 
-#endif
-
 ///-----------------------------------
 /// @name user version identifier tasks
 ///-----------------------------------

+ 6 - 5
src/fmdb/FMDatabaseAdditions.m

@@ -9,6 +9,7 @@
 #import "FMDatabase.h"
 #import "FMDatabaseAdditions.h"
 #import "TargetConditionals.h"
+#import "FMDatabase+Private.h"
 
 @interface FMDatabase (PrivateStuff)
 - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arrayArgs orDictionary:(NSDictionary *)dictionaryArgs orVAList:(va_list)args;
@@ -119,10 +120,9 @@ - (BOOL)columnExists:(NSString*)columnName inTableWithName:(NSString*)tableName
 }
 
 
-#if SQLITE_VERSION_NUMBER >= 3007017
-
 - (uint32_t)applicationID {
     
+#if SQLITE_VERSION_NUMBER >= 3007017
     uint32_t r = 0;
     
     FMResultSet *rs = [self executeQuery:@"pragma application_id"];
@@ -132,15 +132,18 @@ - (uint32_t)applicationID {
     }
     
     [rs close];
+#endif
     
     return r;
 }
 
 - (void)setApplicationID:(uint32_t)appID {
+#if SQLITE_VERSION_NUMBER >= 3007017
     NSString *query = [NSString stringWithFormat:@"pragma application_id=%d", appID];
     FMResultSet *rs = [self executeQuery:query];
     [rs next];
     [rs close];
+#endif
 }
 
 
@@ -166,10 +169,8 @@ - (void)setApplicationIDString:(NSString*)s {
     [self setApplicationID:NSHFSTypeCodeFromFileType([NSString stringWithFormat:@"'%@'", s])];
 }
 
-
 #endif
 
-#endif
 
 - (uint32_t)userVersion {
     uint32_t r = 0;
@@ -205,7 +206,7 @@ - (BOOL)validateSQL:(NSString*)sql error:(NSError**)error {
     sqlite3_stmt *pStmt = NULL;
     BOOL validationSucceeded = YES;
     
-    int rc = sqlite3_prepare_v2(_db, [sql UTF8String], -1, &pStmt, 0);
+    int rc = sqlite3_prepare_v2(self.db, [sql UTF8String], -1, &pStmt, 0);
     if (rc != SQLITE_OK) {
         validationSucceeded = NO;
         if (error) {

+ 0 - 4
src/fmdb/FMDatabasePool.h

@@ -7,7 +7,6 @@
 //
 
 #import <Foundation/Foundation.h>
-#import "sqlite3.h"
 
 @class FMDatabase;
 
@@ -156,8 +155,6 @@
 
 - (void)inDeferredTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block;
 
-#if SQLITE_VERSION_NUMBER >= 3007000
-
 /** Synchronously perform database operations in pool using save point.
 
  @param block The code to be run on the `FMDatabasePool` pool.
@@ -168,7 +165,6 @@
 */
 
 - (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block;
-#endif
 
 @end
 

+ 8 - 4
src/fmdb/FMDatabasePool.m

@@ -8,6 +8,7 @@
 
 #import "FMDatabasePool.h"
 #import "FMDatabase.h"
+#import "FMDatabase+Private.h"
 
 @interface FMDatabasePool()
 
@@ -238,9 +239,13 @@ - (void)inDeferredTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block {
 - (void)inTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block {
     [self beginTransaction:NO withBlock:block];
 }
-#if SQLITE_VERSION_NUMBER >= 3007000
+
 - (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block {
     
+    NSError *err = 0x00;
+
+#if SQLITE_VERSION_NUMBER >= 3007000
+
     static unsigned long savePointIdx = 0;
     
     NSString *name = [NSString stringWithFormat:@"savePoint%ld", savePointIdx++];
@@ -249,8 +254,6 @@ - (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block {
     
     FMDatabase *db = [self db];
     
-    NSError *err = 0x00;
-    
     if (![db startSavePointWithName:name error:&err]) {
         [self pushDatabaseBackInPool:db];
         return err;
@@ -266,8 +269,9 @@ - (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block {
     
     [self pushDatabaseBackInPool:db];
     
+#endif
+    
     return err;
 }
-#endif
 
 @end

+ 0 - 3
src/fmdb/FMDatabaseQueue.h

@@ -7,7 +7,6 @@
 //
 
 #import <Foundation/Foundation.h>
-#import "sqlite3.h"
 
 @class FMDatabase;
 
@@ -175,11 +174,9 @@
  @param block The code to be run on the queue of `FMDatabaseQueue`
  */
 
-#if SQLITE_VERSION_NUMBER >= 3007000
 // NOTE: you can not nest these, since calling it will pull another database out of the pool and you'll get a deadlock.
 // If you need to nest, use FMDatabase's startSavePointWithName:error: instead.
 - (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block;
-#endif
 
 @end
 

+ 7 - 3
src/fmdb/FMDatabaseQueue.m

@@ -8,6 +8,7 @@
 
 #import "FMDatabaseQueue.h"
 #import "FMDatabase.h"
+#import "FMDatabase+Private.h"
 
 /*
  
@@ -204,11 +205,13 @@ - (void)inTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block {
     [self beginTransaction:NO withBlock:block];
 }
 
-#if SQLITE_VERSION_NUMBER >= 3007000
 - (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block {
     
-    static unsigned long savePointIdx = 0;
     __block NSError *err = 0x00;
+
+#if SQLITE_VERSION_NUMBER >= 3007000
+
+    static unsigned long savePointIdx = 0;
     FMDBRetain(self);
     dispatch_sync(_queue, ^() { 
         
@@ -229,8 +232,9 @@ - (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block {
         }
     });
     FMDBRelease(self);
+
+#endif
     return err;
 }
-#endif
 
 @end

+ 0 - 1
src/fmdb/FMResultSet.h

@@ -1,5 +1,4 @@
 #import <Foundation/Foundation.h>
-#import "sqlite3.h"
 
 #ifndef __has_feature      // Optional.
 #define __has_feature(x) 0 // Compatibility with non-clang compilers.

+ 1 - 0
src/fmdb/FMResultSet.m

@@ -1,6 +1,7 @@
 #import "FMResultSet.h"
 #import "FMDatabase.h"
 #import "unistd.h"
+#import "FMDatabase+Private.h"
 
 @interface FMDatabase ()
 - (void)resultSetDidClose:(FMResultSet *)resultSet;