]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/CKKSSQLDatabaseObject.h
Security-58286.270.3.0.1.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 #define CKKSNSNullToNil(obj) \
33 ({ \
34 id o = (obj); \
35 ([o isEqual:[NSNull null]]) ? nil : o; \
36 })
37
38 #define CKKSIsNull(x) \
39 ({ \
40 id y = (x); \
41 ((y == nil) || ([y isEqual:[NSNull null]])); \
42 })
43 #define CKKSUnbase64NullableString(x) (!CKKSIsNull(x) ? [[NSData alloc] initWithBase64EncodedString:x options:0] : nil)
44
45 NS_ASSUME_NONNULL_BEGIN
46
47 @interface CKKSSQLDatabaseObject : NSObject <NSCopying>
48
49 @property (copy) NSDictionary<NSString*, NSString*>* originalSelfWhereClause;
50
51 - (bool)saveToDatabase:(NSError* _Nullable __autoreleasing* _Nullable)error;
52 - (bool)saveToDatabaseWithConnection:(SecDbConnectionRef _Nullable)conn
53 error:(NSError* _Nullable __autoreleasing* _Nullable)error;
54 - (bool)deleteFromDatabase:(NSError* _Nullable __autoreleasing* _Nullable)error;
55 + (bool)deleteAll:(NSError* _Nullable __autoreleasing* _Nullable)error;
56
57 // Load the object from the database, and error if it doesn't exist
58 + (instancetype _Nullable)fromDatabaseWhere:(NSDictionary*)whereDict error:(NSError* _Nullable __autoreleasing* _Nullable)error;
59
60 // Load the object from the database, and return nil if it doesn't exist
61 + (instancetype _Nullable)tryFromDatabaseWhere:(NSDictionary*)whereDict
62 error:(NSError* _Nullable __autoreleasing* _Nullable)error;
63
64 + (NSArray*)all:(NSError* _Nullable __autoreleasing* _Nullable)error;
65 + (NSArray*)allWhere:(NSDictionary* _Nullable)whereDict error:(NSError* _Nullable __autoreleasing* _Nullable)error;
66
67 // Like all() above, but with limits on how many will return
68 + (NSArray*)fetch:(size_t)count error:(NSError* _Nullable __autoreleasing* _Nullable)error;
69 + (NSArray*)fetch:(size_t)count
70 where:(NSDictionary* _Nullable)whereDict
71 error:(NSError* _Nullable __autoreleasing* _Nullable)error;
72 + (NSArray*)fetch:(size_t)count
73 where:(NSDictionary* _Nullable)whereDict
74 orderBy:(NSArray* _Nullable)orderColumns
75 error:(NSError* _Nullable __autoreleasing* _Nullable)error;
76
77
78 + (bool)saveToDatabaseTable:(NSString*)table
79 row:(NSDictionary*)row
80 connection:(SecDbConnectionRef _Nullable)dbconn
81 error:(NSError* _Nullable __autoreleasing* _Nullable)error;
82 + (bool)deleteFromTable:(NSString*)table
83 where:(NSDictionary* _Nullable)whereDict
84 connection:(SecDbConnectionRef _Nullable)dbconn
85 error:(NSError* _Nullable __autoreleasing* _Nullable)error;
86
87 + (bool)queryDatabaseTable:(NSString*)table
88 where:(NSDictionary* _Nullable)whereDict
89 columns:(NSArray*)names
90 groupBy:(NSArray* _Nullable)groupColumns
91 orderBy:(NSArray* _Nullable)orderColumns
92 limit:(ssize_t)limit
93 processRow:(void (^)(NSDictionary*))processRow
94 error:(NSError* _Nullable __autoreleasing* _Nullable)error;
95
96 + (bool)queryMaxValueForField:(NSString*)maxField
97 inTable:(NSString*)table
98 where:(NSDictionary* _Nullable)whereDict
99 columns:(NSArray*)names
100 processRow:(void (^)(NSDictionary*))processRow;
101
102 // Note: if you don't use the SQLDatabase methods of loading yourself,
103 // make sure you call this directly after loading.
104 - (instancetype)memoizeOriginalSelfWhereClause;
105
106 #pragma mark - Subclasses must implement the following:
107
108 // Given a row from the database, make this object
109 + (instancetype _Nullable)fromDatabaseRow:(NSDictionary*)row;
110
111 // Return the columns, in order, that this row wants to fetch
112 + (NSArray<NSString*>*)sqlColumns;
113
114 // Return the table name for objects of this class
115 + (NSString*)sqlTable;
116
117 // Return the columns and values, in order, that this row wants to save
118 - (NSDictionary<NSString*, NSString*>*)sqlValues;
119
120 // Return a set of key-value pairs that will uniquely find This Row in the table
121 - (NSDictionary<NSString*, NSString*>*)whereClauseToFindSelf;
122
123 //- (instancetype)copyWithZone:(NSZone* _Nullable)zone;
124 @end
125
126 // Helper class to use with where clauses
127 // 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
128 @interface CKKSSQLWhereObject : NSObject
129 @property NSString* sqlOp;
130 @property NSString* contents;
131 - (instancetype)initWithOperation:(NSString*)op string:(NSString*)str;
132 + (instancetype)op:(NSString*)op string:(NSString*)str;
133 + (instancetype)op:(NSString*)op stringValue:(NSString*)str; // Will add single quotes around your value.
134 @end
135
136 NS_ASSUME_NONNULL_END