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