]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/CKKSSQLDatabaseObject.h
Security-59306.11.20.tar.gz
[apple/security.git] / keychain / ckks / CKKSSQLDatabaseObject.h
1 /*
2 * Copyright (c) 2016 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <securityd/SecDbItem.h>
25 #include <utilities/SecDb.h>
26
27 #define CKKSNilToNSNull(obj) \
28 ({ \
29 id o = (obj); \
30 o ? o : [NSNull null]; \
31 })
32
33 #define CKKSIsNull(x) \
34 ({ \
35 id y = (x); \
36 ((y == nil) || ([y isEqual:[NSNull null]])); \
37 })
38 #define CKKSUnbase64NullableString(x) (!CKKSIsNull(x) ? [[NSData alloc] initWithBase64EncodedString:x options:0] : nil)
39
40 NS_ASSUME_NONNULL_BEGIN
41
42 // A holder of a possibly-present string value.
43 // Also includes some convenience methods for parsing the string in different manners.
44 // If the value is nil, then the convenience methods return nil (or false/0).
45 @interface CKKSSQLResult : NSObject
46 - (instancetype)init:(NSString* _Nullable)value;
47
48 - (BOOL)asBOOL;
49 - (NSInteger)asNSInteger;
50 - (NSString* _Nullable)asString;
51 - (NSNumber* _Nullable)asNSNumberInteger;
52 - (NSDate* _Nullable)asISO8601Date;
53 - (NSData* _Nullable)asBase64DecodedData;
54 @end
55
56 @interface CKKSSQLDatabaseObject : NSObject <NSCopying>
57
58 @property (copy) NSDictionary<NSString*, NSString*>* originalSelfWhereClause;
59
60 - (bool)saveToDatabase:(NSError* _Nullable __autoreleasing* _Nullable)error;
61 - (bool)saveToDatabaseWithConnection:(SecDbConnectionRef _Nullable)conn
62 error:(NSError* _Nullable __autoreleasing* _Nullable)error;
63 - (bool)deleteFromDatabase:(NSError* _Nullable __autoreleasing* _Nullable)error;
64 + (bool)deleteAll:(NSError* _Nullable __autoreleasing* _Nullable)error;
65
66 // Load the object from the database, and error if it doesn't exist
67 + (instancetype _Nullable)fromDatabaseWhere:(NSDictionary*)whereDict error:(NSError* _Nullable __autoreleasing* _Nullable)error;
68
69 // Load the object from the database, and return nil if it doesn't exist
70 + (instancetype _Nullable)tryFromDatabaseWhere:(NSDictionary*)whereDict
71 error:(NSError* _Nullable __autoreleasing* _Nullable)error;
72
73 + (NSArray*)all:(NSError* _Nullable __autoreleasing* _Nullable)error;
74 + (NSArray*)allWhere:(NSDictionary* _Nullable)whereDict error:(NSError* _Nullable __autoreleasing* _Nullable)error;
75
76 // Like all() above, but with limits on how many will return
77 + (NSArray*)fetch:(size_t)count error:(NSError* _Nullable __autoreleasing* _Nullable)error;
78 + (NSArray*)fetch:(size_t)count
79 where:(NSDictionary* _Nullable)whereDict
80 error:(NSError* _Nullable __autoreleasing* _Nullable)error;
81 + (NSArray*)fetch:(size_t)count
82 where:(NSDictionary* _Nullable)whereDict
83 orderBy:(NSArray* _Nullable)orderColumns
84 error:(NSError* _Nullable __autoreleasing* _Nullable)error;
85
86
87 + (bool)saveToDatabaseTable:(NSString*)table
88 row:(NSDictionary*)row
89 connection:(SecDbConnectionRef _Nullable)dbconn
90 error:(NSError* _Nullable __autoreleasing* _Nullable)error;
91 + (bool)deleteFromTable:(NSString*)table
92 where:(NSDictionary* _Nullable)whereDict
93 connection:(SecDbConnectionRef _Nullable)dbconn
94 error:(NSError* _Nullable __autoreleasing* _Nullable)error;
95
96 + (bool)queryDatabaseTable:(NSString*)table
97 where:(NSDictionary* _Nullable)whereDict
98 columns:(NSArray*)names
99 groupBy:(NSArray* _Nullable)groupColumns
100 orderBy:(NSArray* _Nullable)orderColumns
101 limit:(ssize_t)limit
102 processRow:(void (^)(NSDictionary<NSString*, CKKSSQLResult*>*))processRow
103 error:(NSError* _Nullable __autoreleasing* _Nullable)error;
104
105 + (bool)queryMaxValueForField:(NSString*)maxField
106 inTable:(NSString*)table
107 where:(NSDictionary* _Nullable)whereDict
108 columns:(NSArray*)names
109 processRow:(void (^)(NSDictionary<NSString*, CKKSSQLResult*>*))processRow;
110
111 // Note: if you don't use the SQLDatabase methods of loading yourself,
112 // make sure you call this directly after loading.
113 - (instancetype)memoizeOriginalSelfWhereClause;
114
115 + (NSString *)quotedString:(NSString *)string;
116
117 #pragma mark - Subclasses must implement the following:
118
119 // Given a row from the database, make this object
120 + (instancetype _Nullable)fromDatabaseRow:(NSDictionary<NSString *, CKKSSQLResult *>*)row;
121
122 // Return the columns, in order, that this row wants to fetch
123 + (NSArray<NSString*>*)sqlColumns;
124
125 // Return the table name for objects of this class
126 + (NSString*)sqlTable;
127
128 // Return the columns and values, in order, that this row wants to save
129 - (NSDictionary<NSString*, NSString*>*)sqlValues;
130
131 // Return a set of key-value pairs that will uniquely find This Row in the table
132 - (NSDictionary<NSString*, NSString*>*)whereClauseToFindSelf;
133
134 //- (instancetype)copyWithZone:(NSZone* _Nullable)zone;
135 @end
136
137 // Helper class to use with where clauses
138 // If you pass in one of these instead of a concrete value, its substring will be used directly, instead of binding the value as a named parameter
139 @interface CKKSSQLWhereObject : NSObject
140 @property NSString* sqlOp;
141 @property NSString* contents;
142 - (instancetype)initWithOperation:(NSString*)op string:(NSString*)str;
143 + (instancetype)op:(NSString*)op string:(NSString*)str;
144 + (instancetype)op:(NSString*)op stringValue:(NSString*)str; // Will add single quotes around your value.
145 @end
146
147 NS_ASSUME_NONNULL_END