Skip to content

Commit 982c14c

Browse files
committed
2 parents 6fff75b + c7a5ad6 commit 982c14c

File tree

2 files changed

+55
-24
lines changed

2 files changed

+55
-24
lines changed

LKDBHelper/Helper/LKDBHelper.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@
126126
*/
127127
- (NSMutableArray*)searchWithRAWSQL:(NSString*)sql toClass:(Class)modelClass;
128128

129+
/**
130+
* query sql, query finished result is an array(model instance collection)
131+
* you can use the "@t" replace Model TableName and replace all ? placeholders with the va_list
132+
* example:
133+
NSMutableArray* array = [[LKDBHelper getUsingLKDBHelper] searc:[ModelClass class] withSQL:@"select rowid from name_table where name = ?", @"Swift"];
134+
*
135+
*/
136+
-(NSMutableArray *)search:(Class)modelClass withSQL:(NSString *)sql,...;
137+
129138
/**
130139
columns may NSArray or NSString if query column count == 1 return single column string array
131140
other return models entity array

LKDBHelper/Helper/LKDBHelper.m

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -620,10 +620,6 @@ - (BOOL)_createTableWithModelClass:(Class)modelClass tableName:(NSString*)tableN
620620

621621
NSString* columnType = property.sqlColumnType;
622622

623-
if ([columnType isEqualToString:LKSQL_Type_Double]) {
624-
columnType = LKSQL_Type_Text;
625-
}
626-
627623
[table_pars appendFormat:@"%@ %@", property.sqlColumnName, columnType];
628624

629625
if ([property.sqlColumnType isEqualToString:LKSQL_Type_Text]) {
@@ -937,29 +933,36 @@ - (NSMutableArray*)searchBase:(Class)modelClass columns:(id)columns where:(id)wh
937933
return [self searchBaseWithParams:params];
938934
}
939935

936+
- (NSString *)replaceTableNameIfNeeded:(NSString *)sql withModelClass:(Class)modelClass
937+
{
938+
// replace @t to model table name
939+
NSString* replaceString = [[modelClass getTableName] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
940+
if ([sql hasSuffix:@" @t"]) {
941+
sql = [sql stringByAppendingString:@" "];
942+
}
943+
if ([sql componentsSeparatedByString:@" from "].count == 2 && [sql rangeOfString:@" join "].length == 0) {
944+
sql = [sql stringByReplacingOccurrencesOfString:@" from " withString:[NSString stringWithFormat:@",%@.rowid from ", replaceString]];
945+
}
946+
947+
sql = [sql stringByReplacingOccurrencesOfString:@" @t "
948+
withString:
949+
[NSString stringWithFormat:@" %@ ", replaceString]];
950+
sql = [sql stringByReplacingOccurrencesOfString:@" @t,"
951+
withString:
952+
[NSString stringWithFormat:@" %@,", replaceString]];
953+
sql = [sql stringByReplacingOccurrencesOfString:@",@t "
954+
withString:
955+
[NSString stringWithFormat:@",%@ ", replaceString]];
956+
957+
return sql;
958+
}
959+
940960
- (NSMutableArray*)searchWithSQL:(NSString*)sql toClass:(Class)modelClass
941961
{
942-
// replace @t to model table name
943-
NSString* replaceString = [[modelClass getTableName] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
944-
if ([sql hasSuffix:@" @t"]) {
945-
sql = [sql stringByAppendingString:@" "];
946-
}
947-
if ([sql componentsSeparatedByString:@" from "].count == 2 && [sql rangeOfString:@" join "].length == 0) {
948-
sql = [sql stringByReplacingOccurrencesOfString:@" from " withString:[NSString stringWithFormat:@",%@.rowid from ", replaceString]];
949-
}
950-
951-
sql = [sql stringByReplacingOccurrencesOfString:@" @t "
952-
withString:
953-
[NSString stringWithFormat:@" %@ ", replaceString]];
954-
sql = [sql stringByReplacingOccurrencesOfString:@" @t,"
955-
withString:
956-
[NSString stringWithFormat:@" %@,", replaceString]];
957-
sql = [sql stringByReplacingOccurrencesOfString:@",@t "
958-
withString:
959-
[NSString stringWithFormat:@",%@ ", replaceString]];
960-
962+
sql = [self replaceTableNameIfNeeded:sql withModelClass:modelClass];
961963
return [self searchWithRAWSQL:sql toClass:modelClass];
962964
}
965+
963966
- (NSMutableArray*)searchWithRAWSQL:(NSString*)sql toClass:(Class)modelClass
964967
{
965968
__block NSMutableArray* results = nil;
@@ -971,6 +974,25 @@ - (NSMutableArray*)searchWithRAWSQL:(NSString*)sql toClass:(Class)modelClass
971974
return results;
972975
}
973976

977+
- (NSMutableArray*)search:(Class)modelClass withSQL:(NSString*)sql, ...
978+
{
979+
va_list args;
980+
va_start(args, sql);
981+
982+
sql = [self replaceTableNameIfNeeded:sql withModelClass:modelClass];
983+
984+
va_list *argsPoint = &args;
985+
__block NSMutableArray *results = nil;
986+
[self executeDB:^(FMDatabase *db) {
987+
FMResultSet *set = [db executeQuery:sql withVAList:*argsPoint];
988+
results = [self executeResult:set Class:modelClass tableName:nil];
989+
[set close];
990+
}];
991+
992+
va_end(args);
993+
return results;
994+
}
995+
974996
- (void)sqlString:(NSMutableString*)sql groupBy:(NSString*)groupBy orderBy:(NSString*)orderby offset:(NSInteger)offset count:(NSInteger)count
975997
{
976998
if ([LKDBUtils checkStringIsEmpty:groupBy] == NO) {
@@ -1549,4 +1571,4 @@ + (LKDBHelper*)getUsingLKDBHelper
15491571

15501572
@implementation LKDBWeakObject
15511573

1552-
@end
1574+
@end

0 commit comments

Comments
 (0)