2 * Copyright (c) 2016 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <securityd/SecDbItem.h>
25 #include <utilities/SecDb.h>
27 #define CKKSNilToNSNull(obj) \
30 o ? o : [NSNull null]; \
32 #define CKKSNSNullToNil(obj) \
35 ([o isEqual:[NSNull null]]) ? nil : o; \
38 #define CKKSIsNull(x) \
41 ((y == nil) || ([y isEqual:[NSNull null]])); \
43 #define CKKSUnbase64NullableString(x) (!CKKSIsNull(x) ? [[NSData alloc] initWithBase64EncodedString:x options:0] : nil)
45 NS_ASSUME_NONNULL_BEGIN
47 @interface CKKSSQLDatabaseObject
: NSObject
<NSCopying
>
49 @
property (copy
) NSDictionary
<NSString
*, NSString
*>* originalSelfWhereClause
;
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
;
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
;
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
;
64 + (NSArray
*)all
:(NSError
* _Nullable __autoreleasing
* _Nullable
)error
;
65 + (NSArray
*)allWhere
:(NSDictionary
* _Nullable
)whereDict error
:(NSError
* _Nullable __autoreleasing
* _Nullable
)error
;
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
;
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
;
87 + (bool)queryDatabaseTable
:(NSString
*)table
88 where
:(NSDictionary
* _Nullable
)whereDict
89 columns
:(NSArray
*)names
90 groupBy
:(NSArray
* _Nullable
)groupColumns
91 orderBy
:(NSArray
* _Nullable
)orderColumns
93 processRow
:(void (^)(NSDictionary
*))processRow
94 error
:(NSError
* _Nullable __autoreleasing
* _Nullable
)error
;
96 + (bool)queryMaxValueForField
:(NSString
*)maxField
97 inTable
:(NSString
*)table
98 where
:(NSDictionary
* _Nullable
)whereDict
99 columns
:(NSArray
*)names
100 processRow
:(void (^)(NSDictionary
*))processRow
;
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
;
106 #pragma mark - Subclasses must implement the following:
108 // Given a row from the database, make this object
109 + (instancetype _Nullable
)fromDatabaseRow
:(NSDictionary
*)row
;
111 // Return the columns, in order, that this row wants to fetch
112 + (NSArray
<NSString
*>*)sqlColumns
;
114 // Return the table name for objects of this class
115 + (NSString
*)sqlTable
;
117 // Return the columns and values, in order, that this row wants to save
118 - (NSDictionary
<NSString
*, NSString
*>*)sqlValues
;
120 // Return a set of key-value pairs that will uniquely find This Row in the table
121 - (NSDictionary
<NSString
*, NSString
*>*)whereClauseToFindSelf
;
123 //- (instancetype)copyWithZone:(NSZone* _Nullable)zone;
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.
136 NS_ASSUME_NONNULL_END