Skip to content

Commit 6b91166

Browse files
committed
Improving Swift support within YapDatabaseQuery. Fixes issue #170
1 parent 03a02e8 commit 6b91166

File tree

2 files changed

+199
-123
lines changed

2 files changed

+199
-123
lines changed

YapDatabase/Utilities/YapDatabaseQuery.h

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,63 @@
44
/**
55
* A YapDatabaseQuery is used to pass SQL style queries into various extension classes.
66
* The query that you pass represents everything after the SELECT clause of a query.
7+
*
8+
* For example:
9+
* query = [YapDatabaseQuery queryWithFormat:@"WHERE department = ? AND salary >= ?", deptStr, @(minSalary)];
10+
* [secondaryIndex enumerateKeysAndObjectsMatchingQuery:query
11+
* usingBlock:^(NSString *collection, NSString *key, id object, BOOL *stop){
12+
* ...
13+
* }];
14+
*
15+
* Methods that take YapDatabaseQuery parameters will automatically prefix your query string with
16+
* the proper 'SELECT' clause. So it may get expanded to something like this:
17+
*
18+
* @"SELECT rowid FROM 'database' WHERE department = ? AND salary >= ?"
19+
*
20+
* YapDatabaseQuery supports the following types as query parameters:
21+
* - NSNumber
22+
* - NSDate (automatically converted to double via timeIntervalSinceReferenceDate)
23+
* - NSString
24+
* - NSArray (of any regular type above)
25+
*
26+
* Example 2:
27+
*
28+
* NSArray *departments = [self engineeringDepartments];
29+
* query = [YapDatabaseQuery queryWithFormat:@"WHERE title = ? AND department IN (?)", @"manager", departments];
730
**/
831
@interface YapDatabaseQuery : NSObject
932

1033
/**
1134
* A YapDatabaseQuery is everything after the SELECT clause of a query.
12-
* Methods that take YapDatabaseQuery parameters will prefix your query string similarly to:
13-
*
14-
* fullQuery = @"SELECT rowid FROM 'database' " + [yapDatabaseQuery queryString];
35+
* Thus they generally start with "WHERE ...".
1536
*
16-
* Example 1:
17-
*
18-
* query = [YapDatabaseQuery queryWithFormat:@"WHERE jid = ?", message.jid];
19-
* [secondaryIndex enumerateKeysAndObjectsMatchingQuery:query
20-
* usingBlock:^(NSString *key, id object, BOOL *stop){
21-
* ...
22-
* }];
23-
*
24-
* Please note that you can ONLY pass objective-c objects as parameters.
37+
* Please note that you can ONLY pass objects as parameters.
2538
* Primitive types such as int, float, double, etc are NOT supported.
2639
* You MUST wrap these using NSNumber.
27-
*
28-
* Example 2:
29-
*
30-
* query = [YapDatabaseQuery queryWithFormat:@"WHERE department = ? AND salary >= ?", dept, @(minSalary)];
31-
* [secondaryIndex enumerateKeysAndObjectsMatchingQuery:query
32-
* usingBlock:^(NSString *key, id object, BOOL *stop){
33-
* ...
34-
* }];
3540
**/
3641
+ (instancetype)queryWithFormat:(NSString *)format, ...;
3742

3843
/**
39-
* Shim that allows YapDatabaseQuery to be used from Swift.
44+
* Alternative initializer if you have a prepared va_list.
45+
*
46+
* Swift note:
47+
* You may prefer to use 'queryWithString:(NSString *)queryString parameters:(NSArray *)queryParameters' instead.
4048
*
41-
* Define the following somewhere in your Swift code:
49+
* Alternatively, define the following somewhere in your Swift code:
4250
*
4351
* extension YapDatabaseQuery {
4452
* class func queryWithFormat(format: String, _ arguments: CVarArgType...) -> YapDatabaseQuery? {
4553
* return withVaList(arguments, { YapDatabaseQuery(format: format, arguments: $0) })
4654
* }
4755
* }
48-
**/
56+
**/
4957
+ (instancetype)queryWithFormat:(NSString *)format arguments:(va_list)arguments;
5058

59+
/**
60+
* Alternative initializer - generally preferred for Swift code.
61+
**/
62+
+ (instancetype)queryWithString:(NSString *)queryString parameters:(NSArray *)queryParameters;
63+
5164
/**
5265
* Shorthand for a query with no 'WHERE' clause.
5366
* Equivalent to [YapDatabaseQuery queryWithFormat:@""].

0 commit comments

Comments
 (0)