Skip to content

Commit 0461d28

Browse files
committed
Fuzzy query
1 parent baa9103 commit 0461d28

File tree

6 files changed

+143
-4
lines changed

6 files changed

+143
-4
lines changed

SJDBMap/SJDatabaseMap+Server.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ extern char *_sjmystrcat(char *dst, const char *src);
5757
*/
5858
- (NSArray<id<SJDBMapUseProtocol>> *)sjQueryConversionMolding:(Class)cls dict:(NSDictionary *)dict;
5959

60+
/*!
61+
* 根据条件模糊查询
62+
*/
63+
- (NSArray<id<SJDBMapUseProtocol>> *)sjFuzzyQueryConversionMolding:(Class)cls match:(SJDatabaseMapFuzzyMatch)match dict:(NSDictionary *)dict;
64+
6065
/*!
6166
* 查询数据库原始存储数据
6267
*/

SJDBMap/SJDatabaseMap+Server.m

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ - (void)sjAutoCreateOrAlterRelevanceTabWithClass:(Class)cls {
175175
NSMutableString *fieldsSqlM = [NSMutableString new];
176176
[fieldsSqlM appendFormat:@"select * from %s where ", tabName];
177177
[dict enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
178-
[fieldsSqlM appendFormat:@"%@ = '%@'", key, obj];
178+
if ( [obj isKindOfClass:[NSString class]] && [(NSString *)obj containsString:@"'"] )
179+
[fieldsSqlM appendFormat:@"%@ = \"%@\"", key, obj];
180+
else
181+
[fieldsSqlM appendFormat:@"%@ = '%@'", key, obj];
179182
[fieldsSqlM appendString:@" and "];
180183
}];
181184
[fieldsSqlM deleteCharactersInRange:NSMakeRange(fieldsSqlM.length - 5, 5)];
@@ -189,6 +192,66 @@ - (void)sjAutoCreateOrAlterRelevanceTabWithClass:(Class)cls {
189192
return [self _sjConversionMolding:cls rawStorageData:incompleteData];
190193
}
191194

195+
/*!
196+
* 根据条件模糊查询
197+
*/
198+
- (NSArray<id<SJDBMapUseProtocol>> *)sjFuzzyQueryConversionMolding:(Class)cls match:(SJDatabaseMapFuzzyMatch)match dict:(NSDictionary *)dict {
199+
SJDBMapUnderstandingModel *uM = [self sjGetUnderstandingWithClass:cls];
200+
if ( !uM.primaryKey && !uM.autoincrementPrimaryKey ) return nil;
201+
NSAssert(uM.primaryKey || uM.autoincrementPrimaryKey, @"[%@] 该类没有设置主键", cls);
202+
203+
const char *tabName = [self sjGetTabName:cls];
204+
205+
NSMutableString *fieldsSqlM = [NSMutableString new];
206+
[fieldsSqlM appendFormat:@"select * from %s where ", tabName];
207+
[dict enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
208+
209+
switch (match) {
210+
// * ...A...
211+
case SJDatabaseMapFuzzyMatchAll:
212+
{
213+
if ( [obj isKindOfClass:[NSString class]] && [(NSString *)obj containsString:@"'"] )
214+
[fieldsSqlM appendFormat:@"%@ like \"%%%@%%\"", key, obj];
215+
else
216+
[fieldsSqlM appendFormat:@"%@ like '%%%@%%'", key, obj];
217+
}
218+
break;
219+
// * ABC.....
220+
case SJDatabaseMapFuzzyMatchFront:
221+
{
222+
if ( [obj isKindOfClass:[NSString class]] && [(NSString *)obj containsString:@"'"] )
223+
[fieldsSqlM appendFormat:@"%@ like \"%@%%\"", key, obj];
224+
else
225+
[fieldsSqlM appendFormat:@"%@ like '%@%%'", key, obj];
226+
}
227+
break;
228+
// * ...DEF
229+
case SJDatabaseMapFuzzyMatchLater:
230+
{
231+
if ( [obj isKindOfClass:[NSString class]] && [(NSString *)obj containsString:@"'"] )
232+
[fieldsSqlM appendFormat:@"%@ like \"%%%@\"", key, obj];
233+
else
234+
[fieldsSqlM appendFormat:@"%@ like '%%%@'", key, obj];
235+
}
236+
break;
237+
default:
238+
break;
239+
}
240+
241+
[fieldsSqlM appendString:@" and "];
242+
}];
243+
[fieldsSqlM deleteCharactersInRange:NSMakeRange(fieldsSqlM.length - 5, 5)];
244+
[fieldsSqlM appendString:@";"];
245+
246+
NSMutableArray<NSMutableDictionary *> *incompleteData = [NSMutableArray new];
247+
[[self sjQueryWithSQLStr:fieldsSqlM] enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
248+
[incompleteData addObject:obj.mutableCopy];
249+
}];
250+
251+
return [self _sjConversionMolding:cls rawStorageData:incompleteData];
252+
}
253+
254+
192255
- (NSArray<id> *)_sjConversionMolding:(Class)cls rawStorageData:(NSArray<NSDictionary *> *)rawStorageData {
193256
NSMutableArray<id> *allDataModel = [NSMutableArray new];
194257
NSArray<SJDBMapCorrespondingKeyModel *>*cKr = [self sjGetCorrespondingKeys:cls];

SJDBMap/SJDatabaseMap.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ NS_ASSUME_NONNULL_BEGIN
103103

104104
// MARK: Query
105105

106+
106107
@interface SJDatabaseMap (Query)
107108

108109
/*!
@@ -122,6 +123,37 @@ NS_ASSUME_NONNULL_BEGIN
122123
*/
123124
- (void)queryDataWithClass:(Class)cls queryDict:(NSDictionary *)dict completeCallBlock:(void (^ __nullable)(NSArray<id<SJDBMapUseProtocol>> * _Nullable data))block;
124125

126+
127+
/*!
128+
* 模糊查询
129+
* default SJDatabaseMapFuzzyMatchAll
130+
*/
131+
- (void)fuzzyQueryDataWithClass:(Class)cls queryDict:(NSDictionary *)dict completeCallBlock:(void (^ __nullable)(NSArray<id<SJDBMapUseProtocol>> * _Nullable data))block;
132+
133+
134+
typedef NS_ENUM(NSUInteger, SJDatabaseMapFuzzyMatch) {
135+
/*!
136+
* 匹配左右两边
137+
* ...A...
138+
*/
139+
SJDatabaseMapFuzzyMatchAll = 0,
140+
/*!
141+
* 匹配以什么开头
142+
* ABC.....
143+
*/
144+
SJDatabaseMapFuzzyMatchFront,
145+
/*!
146+
* 匹配以什么结尾
147+
* ...DEF
148+
*/
149+
SJDatabaseMapFuzzyMatchLater,
150+
};
151+
152+
/*!
153+
* 模糊查询
154+
*/
155+
- (void)fuzzyQueryDataWithClass:(Class)cls queryDict:(NSDictionary *)dict match:(SJDatabaseMapFuzzyMatch)match completeCallBlock:(void (^ __nullable)(NSArray<id<SJDBMapUseProtocol>> * _Nullable data))block;
156+
125157
@end
126158

127159

SJDBMap/SJDatabaseMap.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,25 @@ - (void)queryDataWithClass:(Class)cls queryDict:(NSDictionary *)dict completeCal
307307
}];
308308
}
309309

310+
/*!
311+
* 模糊查询
312+
*/
313+
- (void)fuzzyQueryDataWithClass:(Class)cls queryDict:(NSDictionary *)dict completeCallBlock:(void (^ __nullable)(NSArray<id<SJDBMapUseProtocol>> * _Nullable data))block {
314+
[self fuzzyQueryDataWithClass:cls queryDict:dict match:SJDatabaseMapFuzzyMatchAll completeCallBlock:block];
315+
}
316+
317+
/*!
318+
* 模糊查询
319+
*/
320+
- (void)fuzzyQueryDataWithClass:(Class)cls queryDict:(NSDictionary *)dict match:(SJDatabaseMapFuzzyMatch)match completeCallBlock:(void (^ __nullable)(NSArray<id<SJDBMapUseProtocol>> * _Nullable data))block {
321+
[self.operationQueue addOperationWithBlock:^{
322+
NSArray *models = [self sjFuzzyQueryConversionMolding:cls match:match dict:dict];
323+
dispatch_async(dispatch_get_main_queue(), ^{
324+
if ( block ) block(models);
325+
});
326+
}];
327+
}
328+
310329
@end
311330

312331

SJDBMapProject/Model/Person.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@
3131

3232
@property (nonatomic, strong) NSArray<Goods *> *goods;
3333

34+
@property (nonatomic, assign) NSInteger age;
35+
3436
@end

SJDBMapProject/ViewController.m

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ - (void)viewDidLoad {
5050

5151

5252
[self insertOrUpdate];
53-
// [self query];
54-
53+
54+
[[SJDatabaseMap sharedServer] fuzzyQueryDataWithClass:[Person class] queryDict:@{@"name":@"j"} match:SJDatabaseMapFuzzyMatchLater completeCallBlock:^(NSArray<id<SJDBMapUseProtocol>> * _Nullable data) {
55+
NSLog(@"count = %zd", data.count);
56+
NSLog(@"%@", data);
57+
}];
5558
// Do any additional setup after loading the view, typically from a nib.
5659
}
5760

@@ -74,6 +77,7 @@ - (void)insertOrUpdate {
7477
[PersonTag tagWithID:4 des:@"E"],];
7578

7679
sj.aBook = [Book bookWithID:123 name:@"How Are You?"];
80+
sj.age = 20;
7781

7882
Goods *g = [Goods new];
7983
g.name = @"G1";
@@ -82,7 +86,21 @@ - (void)insertOrUpdate {
8286

8387
sj.goods = @[g, g2];
8488

85-
[[SJDatabaseMap sharedServer] insertOrUpdateDataWithModels:@[sj] callBlock:^(BOOL r) {
89+
90+
Person *sj1 = [Person new];
91+
sj1.personID = 1;
92+
sj1.name = @"'sj'";
93+
sj1.tags = @[[PersonTag tagWithID:0 des:@"A"],
94+
[PersonTag tagWithID:1 des:@"B"],
95+
[PersonTag tagWithID:2 des:@"C"],
96+
[PersonTag tagWithID:3 des:@"'D'"],
97+
[PersonTag tagWithID:4 des:@"E"],];
98+
99+
sj1.aBook = [Book bookWithID:123 name:@"How Are You?"];
100+
sj1.age = 20;
101+
sj1.goods = @[g, g2];
102+
103+
[[SJDatabaseMap sharedServer] insertOrUpdateDataWithModels:@[sj, sj1] callBlock:^(BOOL r) {
86104

87105
}];
88106
}

0 commit comments

Comments
 (0)