Browse Source

Merge pull request #311 from robertmryan/master

Added variadic rendition for `executeUpdate` and `executeQuery` for Swift as "extra"
August "Gus" Mueller 11 years ago
parent
commit
ccb994dfab

+ 56 - 0
README.markdown

@@ -230,6 +230,62 @@ FMDatabaseQueue will run the blocks on a serialized queue (hence the name of the
 
 You can do this!  For an example, look for "makeFunctionNamed:" in main.m
 
+## Swift
+
+You can use FMDB in Swift projects too.
+
+To do this, you must:
+
+1. Copy the FMDB `.m` and `.h` files into your project.
+
+2. If prompted to create a "bridging header", you should do so. If not prompted and if you don't already have a bridging header, add one.
+
+ For more information on bridging headers, see [Swift and Objective-C in the Same Project](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_76).
+
+3. In your briding header, add a line that says:
+
+        #import "FMDB.h"
+
+4. Optionally, copy the `FMDatabaseVariadic.swift` from the "src/extra/Swift Extensions" folder into your project. This allows you to use `executeUpdate` and `executeQuery` with variadic parameters, rather than the `withArgumentsInArray` rendition.
+
+If you do the above, you can then write Swift code that uses FMDatabase. For example:
+
+    let documentsFolder = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
+    let path = documentsFolder.stringByAppendingPathComponent("test.sqlite")
+
+    let database = FMDatabase(path: path)
+
+    if !database.open() {
+        println("Unable to open database")
+        return
+    }
+
+    if !database.executeUpdate("create table test(x text, y text, z text)", withArgumentsInArray: nil) {
+        println("create table failed: \(database.lastErrorMessage())")
+    }
+
+    if !database.executeUpdate("insert into test (x, y, z) values (?, ?, ?)", withArgumentsInArray: ["a", "b", "c"]) {
+        println("insert 1 table failed: \(database.lastErrorMessage())")
+    }
+
+    if !database.executeUpdate("insert into test (x, y, z) values (?, ?, ?)", withArgumentsInArray: ["e", "f", "g"]) {
+        println("insert 2 table failed: \(database.lastErrorMessage())")
+    }
+
+    if let rs = database.executeQuery("select x, y, z from test", withArgumentsInArray: nil) {
+        while rs.next() {
+            let x = rs.stringForColumn("x")
+            let y = rs.stringForColumn("y")
+            let z = rs.stringForColumn("z")
+            println("x = \(x); y = \(y); z = \(z)")
+        }
+    } else {
+        println("select failed: \(database.lastErrorMessage())")
+    }
+
+    database.close()
+
+
 ## 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.

+ 22 - 4
fmdb.xcodeproj/project.pbxproj

@@ -94,6 +94,7 @@
 		67CB1E2F19AD27D000A3CA7F /* FMDatabaseFTS3Tests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FMDatabaseFTS3Tests.m; sourceTree = "<group>"; };
 		8314AF3218CD73D600EC0E25 /* FMDB.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FMDB.h; path = src/fmdb/FMDB.h; sourceTree = "<group>"; };
 		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>"; };
 		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; };
@@ -111,8 +112,8 @@
 		CC47A00E148581E9002CCDAB /* FMDatabaseQueue.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FMDatabaseQueue.m; path = src/fmdb/FMDatabaseQueue.m; sourceTree = SOURCE_ROOT; };
 		CC50F2CB0DF9183600E4AAAE /* FMDatabaseAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FMDatabaseAdditions.m; path = src/fmdb/FMDatabaseAdditions.m; sourceTree = SOURCE_ROOT; };
 		CC50F2CC0DF9183600E4AAAE /* FMDatabaseAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FMDatabaseAdditions.h; path = src/fmdb/FMDatabaseAdditions.h; sourceTree = SOURCE_ROOT; };
-		CC7CE42618F5C04600938264 /* FMDatabase+InMemoryOnDiskIO.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "FMDatabase+InMemoryOnDiskIO.h"; path = "src/extra/FMDatabase+InMemoryOnDiskIO.h"; sourceTree = "<group>"; };
-		CC7CE42718F5C04600938264 /* FMDatabase+InMemoryOnDiskIO.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "FMDatabase+InMemoryOnDiskIO.m"; path = "src/extra/FMDatabase+InMemoryOnDiskIO.m"; sourceTree = "<group>"; };
+		CC7CE42618F5C04600938264 /* FMDatabase+InMemoryOnDiskIO.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "FMDatabase+InMemoryOnDiskIO.h"; path = "src/extra/InMemoryOnDiskIO/FMDatabase+InMemoryOnDiskIO.h"; sourceTree = "<group>"; };
+		CC7CE42718F5C04600938264 /* FMDatabase+InMemoryOnDiskIO.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "FMDatabase+InMemoryOnDiskIO.m"; path = "src/extra/InMemoryOnDiskIO/FMDatabase+InMemoryOnDiskIO.m"; sourceTree = "<group>"; };
 		CC8C138A0E3135C400FBE1E7 /* CHANGES_AND_TODO_LIST.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = CHANGES_AND_TODO_LIST.txt; sourceTree = "<group>"; };
 		CC8C138B0E3135C400FBE1E7 /* LICENSE.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE.txt; sourceTree = "<group>"; };
 		CC8C138C0E3135C400FBE1E7 /* CONTRIBUTORS.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = CONTRIBUTORS.txt; sourceTree = "<group>"; };
@@ -262,6 +263,23 @@
 			name = sample;
 			sourceTree = "<group>";
 		};
+		832F502319EC4C4A0087DCBF /* Swift extensions */ = {
+			isa = PBXGroup;
+			children = (
+				832F502419EC4C6B0087DCBF /* FMDatabaseVariadic.swift */,
+			);
+			name = "Swift extensions";
+			sourceTree = "<group>";
+		};
+		832F502519EC4CA20087DCBF /* InMemoryOnDiskIO */ = {
+			isa = PBXGroup;
+			children = (
+				CC7CE42618F5C04600938264 /* FMDatabase+InMemoryOnDiskIO.h */,
+				CC7CE42718F5C04600938264 /* FMDatabase+InMemoryOnDiskIO.m */,
+			);
+			name = InMemoryOnDiskIO;
+			sourceTree = "<group>";
+		};
 		BF5D041718416BB2008C5AA9 /* Frameworks */ = {
 			isa = PBXGroup;
 			children = (
@@ -308,8 +326,8 @@
 		CC7CE42518F5C02E00938264 /* optional extras */ = {
 			isa = PBXGroup;
 			children = (
-				CC7CE42618F5C04600938264 /* FMDatabase+InMemoryOnDiskIO.h */,
-				CC7CE42718F5C04600938264 /* FMDatabase+InMemoryOnDiskIO.m */,
+				832F502319EC4C4A0087DCBF /* Swift extensions */,
+				832F502519EC4CA20087DCBF /* InMemoryOnDiskIO */,
 				6767F81519AD13C300887DBC /* fts3 */,
 			);
 			name = "optional extras";

+ 0 - 0
src/extra/FMDatabase+InMemoryOnDiskIO.h → src/extra/InMemoryOnDiskIO/FMDatabase+InMemoryOnDiskIO.h


+ 0 - 0
src/extra/FMDatabase+InMemoryOnDiskIO.m → src/extra/InMemoryOnDiskIO/FMDatabase+InMemoryOnDiskIO.m


+ 36 - 0
src/extra/Swift extensions/FMDatabaseVariadic.swift

@@ -0,0 +1,36 @@
+//
+//  FMDatabaseVariadic.swift
+//  FMDB
+//
+
+
+//  This extension inspired by http://stackoverflow.com/a/24187932/1271826
+
+import Foundation
+
+extension FMDatabase {
+
+    /// This is a rendition of executeQuery that handles Swift variadic parameters
+    /// for the values to be bound to the ? placeholders in the SQL.
+    ///
+    /// :param: sql The SQL statement to be used.
+    /// :param: values The values to be bound to the ? placeholders
+    ///
+    /// :returns: This returns FMResultSet if successful. Returns nil upon error.
+
+    func executeQuery(sql:String, _ values: AnyObject...) -> FMResultSet? {
+        return executeQuery(sql, withArgumentsInArray: values as NSArray);
+    }
+
+    /// This is a rendition of executeUpdate that handles Swift variadic parameters
+    /// for the values to be bound to the ? placeholders in the SQL.
+    ///
+    /// :param: sql The SQL statement to be used.
+    /// :param: values The values to be bound to the ? placeholders
+    ///
+    /// :returns: This returns true if successful. Returns false upon error.
+
+    func executeUpdate(sql:String, _ values: AnyObject...) -> Bool {
+        return executeUpdate(sql, withArgumentsInArray: values as NSArray);
+    }
+}