Permalink
Browse files

Add SQL selectMaybe

  • Loading branch information...
1 parent 7d73a76 commit fd661537556c538789afa0bcb98f2e75b564c7ea @marcuswestin committed Feb 8, 2014
Showing with 26 additions and 2 deletions.
  1. +2 −0 Classes/FunSQL/SQL.h
  2. +24 −2 Classes/FunSQL/SQL.m
View
@@ -13,6 +13,7 @@
@property FMDatabase* db;
- (NSArray*)select:(NSString *)sql args:(NSArray *)args error:(NSError**)outError;
- (NSDictionary*)selectOne:(NSString *)sql args:(NSArray *)args error:(NSError**)outError;
+- (NSDictionary*)selectMaybe:(NSString*)sql args:(NSArray*)args error:(NSError**)outError;
- (void)execute:(NSString*)sql args:(NSArray*)args error:(NSError**)outError;
- (void)updateOne:(NSString*)sql args:(NSArray*)args error:(NSError**)outError;
- (void)insertInto:(NSString*)table item:(id)item error:(NSError**)outError;
@@ -40,6 +41,7 @@ typedef void (^SQLTransactionBlock)(SQLConn *conn, SQLRollbackBlock rollback);
+ (void)transact:(SQLTransactionBlock)block;
+ (NSArray*)select:(NSString*)sql args:(NSArray*)args error:(NSError**)outError;
+ (NSDictionary*)selectOne:(NSString*)sql args:(NSArray*)args error:(NSError**)outError;
++ (NSDictionary*)selectMaybe:(NSString*)sql args:(NSArray*)args error:(NSError**)outError;
+ (void)openDocument:(NSString*)name withMigrations:(SQLRegisterMigrations)migrationsFn;
+ (NSString*) joinSelect:(NSDictionary*)tableColumns;
@end
View
@@ -156,6 +156,14 @@ + (NSArray *)select:(NSString *)sql args:(NSArray *)args error:(NSError *__autor
return result;
}
++ (NSDictionary *)selectMaybe:(NSString *)sql args:(NSArray *)args error:(NSError *__autoreleasing *)outError {
+ __block NSDictionary* result;
+ [SQL autocommit:^(SQLConn *conn) {
+ result = [conn selectMaybe:sql args:args error:outError];
+ }];
+ return result;
+}
+
+ (NSDictionary *)selectOne:(NSString *)sql args:(NSArray *)args error:(NSError *__autoreleasing *)outError {
__block NSDictionary* result;
[SQL autocommit:^(SQLConn *conn) {
@@ -196,18 +204,32 @@ - (NSArray *)select:(NSString *)sql args:(NSArray *)args error:(NSError *__autor
}
- (NSDictionary *)selectOne:(NSString *)sql args:(NSArray *)args error:(NSError *__autoreleasing *)outError {
+ NSDictionary* row = [self selectMaybe:sql args:args error:outError];
+ if (*outError) {
+ return nil;
+ }
+
+ if (!row) {
+ *outError = makeError([NSString stringWithFormat:@"SelectOne returned no rows.\nQuery: %@", sql]);
+ return nil;
+ }
+
+ return row;
+}
+
+- (NSDictionary *)selectMaybe:(NSString *)sql args:(NSArray *)args error:(NSError *__autoreleasing *)outError {
NSArray* rows = [self select:sql args:args error:outError];
if (*outError) {
return nil;
}
if (rows.count > 1) {
- *outError = makeError(@"Bad number of rows");
+ *outError = makeError([NSString stringWithFormat:@"SelectOne/SelectMaybe got more than 1 rows.\nQuery: %@", sql]);
return nil;
}
- return (rows.count == 1 ? rows[0] : nil);
+ return rows.firstObject;
}
- (void)insertMultiple:(NSString *)sql argsList:(NSArray *)argsList error:(NSError *__autoreleasing *)outError {

0 comments on commit fd66153

Please sign in to comment.