@@ -942,29 +942,36 @@ - (NSMutableArray*)searchBase:(Class)modelClass columns:(id)columns where:(id)wh
942
942
return [self searchBaseWithParams: params];
943
943
}
944
944
945
+ - (NSString *)replaceTableNameIfNeeded : (NSString *)sql withModelClass : (Class )modelClass
946
+ {
947
+ // replace @t to model table name
948
+ NSString * replaceString = [[modelClass getTableName ] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet ]];
949
+ if ([sql hasSuffix: @" @t" ]) {
950
+ sql = [sql stringByAppendingString: @" " ];
951
+ }
952
+ if ([sql componentsSeparatedByString: @" from " ].count == 2 && [sql rangeOfString: @" join " ].length == 0 ) {
953
+ sql = [sql stringByReplacingOccurrencesOfString: @" from " withString: [NSString stringWithFormat: @" ,%@ .rowid from " , replaceString]];
954
+ }
955
+
956
+ sql = [sql stringByReplacingOccurrencesOfString: @" @t "
957
+ withString:
958
+ [NSString stringWithFormat: @" %@ " , replaceString]];
959
+ sql = [sql stringByReplacingOccurrencesOfString: @" @t,"
960
+ withString:
961
+ [NSString stringWithFormat: @" %@ ," , replaceString]];
962
+ sql = [sql stringByReplacingOccurrencesOfString: @" ,@t "
963
+ withString:
964
+ [NSString stringWithFormat: @" ,%@ " , replaceString]];
965
+
966
+ return sql;
967
+ }
968
+
945
969
- (NSMutableArray *)searchWithSQL : (NSString *)sql toClass : (Class )modelClass
946
970
{
947
- // replace @t to model table name
948
- NSString * replaceString = [[modelClass getTableName ] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet ]];
949
- if ([sql hasSuffix: @" @t" ]) {
950
- sql = [sql stringByAppendingString: @" " ];
951
- }
952
- if ([sql componentsSeparatedByString: @" from " ].count == 2 && [sql rangeOfString: @" join " ].length == 0 ) {
953
- sql = [sql stringByReplacingOccurrencesOfString: @" from " withString: [NSString stringWithFormat: @" ,%@ .rowid from " , replaceString]];
954
- }
955
-
956
- sql = [sql stringByReplacingOccurrencesOfString: @" @t "
957
- withString:
958
- [NSString stringWithFormat: @" %@ " , replaceString]];
959
- sql = [sql stringByReplacingOccurrencesOfString: @" @t,"
960
- withString:
961
- [NSString stringWithFormat: @" %@ ," , replaceString]];
962
- sql = [sql stringByReplacingOccurrencesOfString: @" ,@t "
963
- withString:
964
- [NSString stringWithFormat: @" ,%@ " , replaceString]];
965
-
971
+ sql = [self replaceTableNameIfNeeded: sql withModelClass: modelClass];
966
972
return [self searchWithRAWSQL: sql toClass: modelClass];
967
973
}
974
+
968
975
- (NSMutableArray *)searchWithRAWSQL : (NSString *)sql toClass : (Class )modelClass
969
976
{
970
977
__block NSMutableArray * results = nil ;
@@ -976,6 +983,25 @@ - (NSMutableArray*)searchWithRAWSQL:(NSString*)sql toClass:(Class)modelClass
976
983
return results;
977
984
}
978
985
986
+ - (NSMutableArray *)search : (Class )modelClass withSQL : (NSString *)sql , ...
987
+ {
988
+ va_list args;
989
+ va_start (args, sql);
990
+
991
+ sql = [self replaceTableNameIfNeeded: sql withModelClass: modelClass];
992
+
993
+ va_list *argsPoint = &args;
994
+ __block NSMutableArray *results = nil ;
995
+ [self executeDB: ^(FMDatabase *db) {
996
+ FMResultSet *set = [db executeQuery: sql withVAList: *argsPoint];
997
+ results = [self executeResult: set Class: modelClass tableName: nil ];
998
+ [set close ];
999
+ }];
1000
+
1001
+ va_end (args);
1002
+ return results;
1003
+ }
1004
+
979
1005
- (void )sqlString : (NSMutableString *)sql groupBy : (NSString *)groupBy orderBy : (NSString *)orderby offset : (NSInteger )offset count : (NSInteger )count
980
1006
{
981
1007
if ([LKDBUtils checkStringIsEmpty: groupBy] == NO ) {
0 commit comments