]> git.saurik.com Git - apple/security.git/blob - OSX/utilities/src/SecDb.h
Security-57337.40.85.tar.gz
[apple/security.git] / OSX / utilities / src / SecDb.h
1 /*
2 * Copyright (c) 2012-2014 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
25 #ifndef _UTILITIES_SECDB_H_
26 #define _UTILITIES_SECDB_H_
27
28 #include <CoreFoundation/CoreFoundation.h>
29 #include <sqlite3.h>
30
31 __BEGIN_DECLS
32
33 // MARK: SecDbRef and SecDbConnectionRef forward declarations
34 typedef struct __OpaqueSecDb *SecDbRef;
35 typedef struct __OpaqueSecDbConnection *SecDbConnectionRef;
36 typedef struct __OpaqueSecDbStatement *SecDbStatementRef;
37 struct SOSDigestVector;
38
39 // MARK: Configuration values, not used by clients directly.
40 // TODO: Move this section to a private header
41 enum {
42 kSecDbMaxReaders = 4,
43 kSecDbMaxWriters = 1,
44 kSecDbMaxIdleHandles = 3,
45 };
46
47 // MARK: SecDbTransactionType
48 enum {
49 kSecDbNoneTransactionType = 0,
50 kSecDbImmediateTransactionType,
51 kSecDbExclusiveTransactionType,
52 kSecDbNormalTransactionType,
53 kSecDbExclusiveRemoteTransactionType,
54 };
55 typedef CFOptionFlags SecDbTransactionType;
56
57 enum SecDbTransactionPhase {
58 kSecDbTransactionDidRollback = 0, // A transaction just got rolled back
59 kSecDbTransactionWillCommit, // A transaction is about to commit.
60 kSecDbTransactionDidCommit, // A transnaction sucessfully committed.
61 };
62 typedef CFOptionFlags SecDbTransactionPhase;
63
64 enum SecDbTransactionSource {
65 kSecDbSOSTransaction, // A remotely initated transaction.
66 kSecDbAPITransaction, // A user initated transaction.
67 };
68 typedef CFOptionFlags SecDbTransactionSource;
69
70 // MARK: --
71 // MARK: Error creation helpers.
72
73 // SQLITE3 errors are in this domain
74 extern CFStringRef kSecDbErrorDomain;
75
76 typedef CFTypeRef SecDbEntryRef;
77
78 bool SecDbError(int sql_code, CFErrorRef *error, CFStringRef format, ...);
79 bool SecDbErrorWithDb(int sql_code, sqlite3 *db, CFErrorRef *error, CFStringRef format, ...);
80 bool SecDbErrorWithStmt(int sql_code, sqlite3_stmt *stmt, CFErrorRef *error, CFStringRef format, ...);
81
82 // MARK: mark -
83 // MARK: mark SecDbRef
84
85 typedef CFTypeRef SecDbEventRef;
86
87 SecDbEventRef SecDbEventCreateWithComponents(CFTypeRef deleted, CFTypeRef inserted);
88
89 // Return deleted and inserted for a given changes entry, both are optional
90 bool SecDbEventGetComponents(SecDbEventRef event, CFTypeRef *deleted, CFTypeRef *inserted, CFErrorRef *error);
91
92 // changes is an array of SecDbEventRef
93 typedef void (^SecDBNotifyBlock)(SecDbConnectionRef dbconn, SecDbTransactionPhase phase, SecDbTransactionSource source, CFArrayRef changes);
94
95 CFTypeID SecDbGetTypeID(void);
96
97 SecDbRef SecDbCreate(CFStringRef dbName, bool (^opened)(SecDbConnectionRef dbconn, bool didCreate, bool *callMeAgainForNextConnection, CFErrorRef *error));
98
99 void SecDbSetNotifyPhaseBlock(SecDbRef db, dispatch_queue_t queue, SecDBNotifyBlock notifyPhase);
100
101 // Read only connections go to the end of the queue, writeable
102 // connections go to the start of the queue. Use SecDbPerformRead() and SecDbPerformWrite() if you
103 // can to avoid leaks.
104 SecDbConnectionRef SecDbConnectionAquire(SecDbRef db, bool readOnly, CFErrorRef *error);
105 void SecDbConnectionRelease(SecDbConnectionRef dbconn);
106
107 // Perform a database read operation,
108 bool SecDbPerformRead(SecDbRef db, CFErrorRef *error, void (^perform)(SecDbConnectionRef dbconn));
109 bool SecDbPerformWrite(SecDbRef db, CFErrorRef *error, void (^perform)(SecDbConnectionRef dbconn));
110
111 // TODO: DEBUG only -> Private header
112 CFIndex SecDbIdleConnectionCount(SecDbRef db);
113
114 CFStringRef SecDbGetPath(SecDbRef db);
115
116 // MARK: -
117 // MARK: SecDbConectionRef
118
119 CFTypeID SecDbConnectionGetTypeID(void);
120
121 bool SecDbPrepare(SecDbConnectionRef dbconn, CFStringRef sql, CFErrorRef *error, void(^exec)(sqlite3_stmt *stmt));
122
123 bool SecDbStep(SecDbConnectionRef dbconn, sqlite3_stmt *stmt, CFErrorRef *error, void (^row)(bool *stop));
124
125 bool SecDbExec(SecDbConnectionRef dbconn, CFStringRef sql, CFErrorRef *error);
126
127 bool SecDbCheckpoint(SecDbConnectionRef dbconn, CFErrorRef *error);
128
129 bool SecDbTransaction(SecDbConnectionRef dbconn, SecDbTransactionType ttype, CFErrorRef *error,
130 void (^transaction)(bool *commit));
131
132 sqlite3 *SecDbHandle(SecDbConnectionRef dbconn);
133
134 // Do not call this unless you are SecDbItem!
135 void SecDbRecordChange(SecDbConnectionRef dbconn, CFTypeRef deleted, CFTypeRef inserted);
136
137 // MARK: -
138 // MARK: Bind helpers
139
140 #if 0
141 bool SecDbBindNull(sqlite3_stmt *stmt, int param, CFErrorRef *error);
142 #endif
143 bool SecDbBindBlob(sqlite3_stmt *stmt, int param, const void *zData, size_t n, void(*xDel)(void*), CFErrorRef *error);
144 bool SecDbBindText(sqlite3_stmt *stmt, int param, const char *zData, size_t n, void(*xDel)(void*), CFErrorRef *error);
145 bool SecDbBindDouble(sqlite3_stmt *stmt, int param, double value, CFErrorRef *error);
146 bool SecDbBindInt(sqlite3_stmt *stmt, int param, int value, CFErrorRef *error);
147 bool SecDbBindInt64(sqlite3_stmt *stmt, int param, sqlite3_int64 value, CFErrorRef *error);
148 bool SecDbBindObject(sqlite3_stmt *stmt, int param, CFTypeRef value, CFErrorRef *error);
149
150 // MARK: -
151 // MARK: SecDbStatementRef
152
153 bool SecDbReset(sqlite3_stmt *stmt, CFErrorRef *error);
154 bool SecDbClearBindings(sqlite3_stmt *stmt, CFErrorRef *error);
155 bool SecDbFinalize(sqlite3_stmt *stmt, CFErrorRef *error);
156 sqlite3_stmt *SecDbPrepareV2(SecDbConnectionRef dbconn, const char *sql, size_t sqlLen, const char **sqlTail, CFErrorRef *error);
157 sqlite3_stmt *SecDbCopyStmt(SecDbConnectionRef dbconn, CFStringRef sql, CFStringRef *tail, CFErrorRef *error);
158 bool SecDbReleaseCachedStmt(SecDbConnectionRef dbconn, CFStringRef sql, sqlite3_stmt *stmt, CFErrorRef *error);
159 bool SecDbWithSQL(SecDbConnectionRef dbconn, CFStringRef sql, CFErrorRef *error, bool(^perform)(sqlite3_stmt *stmt));
160 bool SecDbForEach(sqlite3_stmt *stmt, CFErrorRef *error, bool(^row)(int row_index));
161
162 // Mark the database as corrupted.
163 void SecDbCorrupt(SecDbConnectionRef dbconn, CFErrorRef error);
164
165 __END_DECLS
166
167 #endif /* !_UTILITIES_SECDB_H_ */