-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMDDQuery.m
More file actions
105 lines (82 loc) · 3.32 KB
/
MDDQuery.m
File metadata and controls
105 lines (82 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// MDDQuery.m
// MDDatabase
//
// Created by xulinfeng on 2018/3/23.
// Copyright © 2018年 markejave. All rights reserved.
//
#import "MDDQuery.h"
#import "MDDQuery+Private.h"
#import "MDDColumn.h"
#import "MDDTableInfo.h"
#import "MDDIndex.h"
#import "MDDItem.h"
#import "MDDDescription.h"
#import "MDDRange.h"
#import "MDDSort.h"
#import "MDDConditionSet+Private.h"
@implementation MDDQuery
@dynamic tableInfo;
+ (instancetype)queryWithTableInfo:(id<MDDTableInfo>)tableInfo objectClass:(Class<MDDObject>)objectClass;{
MDDQuery *query = [self descriptorWithTableInfo:tableInfo];
query.transform = ^id(NSDictionary *result) {
return [objectClass objectWithDictionary:result];
};
return query;
}
- (id)transformValue:(NSDictionary *)value;{
if (_transform) return _transform(value);
return value;
}
- (id<MDDTableInfo>)tableInfo{
return super.tableInfo ?: _conditionSet.tableInfo;
}
- (NSString *)description{
return [[self dictionaryWithValuesForKeys:@[@"tableInfo", @"set", @"conditionSet", @"range", @"property", @"sorts"]] description];
}
#pragma mark - accessor
- (MDDDescription *)SQLDescription{
NSMutableArray *values = [NSMutableArray array];
NSMutableArray<NSString *> *columns = [NSMutableArray<NSString *> array];
NSMutableSet<MDDTableInfo> *tableInfos = [NSMutableSet<MDDTableInfo> setWithObject:self.tableInfo];
[tableInfos unionSet:_conditionSet.mutableTableInfos];
for (MDDItem *property in _properties) {
MDDDescription *description = property.SQLDescription;
if (property.tableInfo) [tableInfos addObject:property.tableInfo];
[columns addObject:description.SQL];
[values addObjectsFromArray:description.values];
}
for (MDDSort *sort in _sorts) {
if (sort.tableInfo) [tableInfos addObject:sort.tableInfo];
}
NSString *property = [columns componentsJoinedByString:@", "];
property = [property length] ? property : @" * ";
MDDIndex *index = _conditionSet.index;
NSString *indexString = index ? [NSString stringWithFormat:@" INDEXED BY %@ ", index.name] : @"";
MDDDescription *description = _set.SQLDescription;
NSString *tableSet = nil;
if (description) {
tableSet = description.SQL;
[values addObjectsFromArray:description.values];
} else {
tableSet = [[tableInfos.allObjects valueForKey:@MDDKeyPath(MDDTableInfo, name)] componentsJoinedByString:@" , "];
}
NSMutableString *SQL = [NSMutableString stringWithFormat:@" SELECT %@ FROM %@ %@ ", property, tableSet, indexString];
if (_set) description = [_conditionSet SQLDescriptionInSet:_set];
else description = _conditionSet.SQLDescription;
if (description.SQL) {
[SQL appendFormat:@" WHERE %@ ", description.SQL];
[values addObjectsFromArray:description.values];
}
description = [MDDSort descriptionWithSorts:_sorts];
if (description.SQL) {
[SQL appendFormat:@" ORDER BY %@ ", description.SQL ?: @""];
}
NSRange range = _range;
if (range.location || range.length) {
range.length = range.length ?: INT_MAX;
[SQL appendFormat:@" LIMIT %lu OFFSET %ld ", (unsigned long)range.length, (unsigned long)range.location];
}
return [MDDDescription descriptionWithSQL:SQL values:values];
}
@end