5 // Created by Michael Brouwer on 11/12/12.
6 // Copyright (c) 2012 Apple Inc. All rights reserved.
9 #ifndef _UTILITIES_SECDB_H_
10 #define _UTILITIES_SECDB_H_
12 #include <CoreFoundation/CoreFoundation.h>
17 // MARK: SecDbRef and SecDbConnectionRef forward declarations
18 typedef struct __OpaqueSecDb
*SecDbRef
;
19 typedef struct __OpaqueSecDbConnection
*SecDbConnectionRef
;
20 typedef struct __OpaqueSecDbStatement
*SecDbStatementRef
;
22 // MARK: Configuration values, not used by clients directly.
23 // TODO: Move this section to a private header
27 kSecDbMaxIdleHandles
= 3,
30 // MARK: SecDbTransactionType
32 kSecDbNoneTransactionType
= 0,
33 kSecDbImmediateTransactionType
,
34 kSecDbExclusiveTransactionType
,
35 kSecDbNormalTransactionType
37 typedef uint32_t SecDbTransactionType
;
40 // MARK: Error creation helpers.
42 // SQLITE3 errors are in this domain
43 extern CFStringRef kSecDbErrorDomain
;
45 bool SecDbError(int sql_code
, CFErrorRef
*error
, CFStringRef format
, ...);
46 bool SecDbErrorWithDb(int sql_code
, sqlite3
*db
, CFErrorRef
*error
, CFStringRef format
, ...);
47 bool SecDbErrorWithStmt(int sql_code
, sqlite3_stmt
*stmt
, CFErrorRef
*error
, CFStringRef format
, ...);
50 // MARK: mark SecDbRef
52 CFTypeID
SecDbGetTypeID(void);
54 SecDbRef
SecDbCreate(CFStringRef dbName
, bool (^opened
)(SecDbConnectionRef dbconn
, bool did_create
, CFErrorRef
*error
));
56 // Read only connections go to the end of the queue, writeable
57 // connections go to the start of the queue. Use SecDbPerformRead() and SecDbPerformWrite() if you
58 // can to avoid leaks.
59 SecDbConnectionRef
SecDbConnectionAquire(SecDbRef db
, bool readOnly
, CFErrorRef
*error
);
60 void SecDbConnectionRelease(SecDbConnectionRef dbconn
);
62 // Perform a database read operation,
63 bool SecDbPerformRead(SecDbRef db
, CFErrorRef
*error
, void (^perform
)(SecDbConnectionRef dbconn
));
64 bool SecDbPerformWrite(SecDbRef db
, CFErrorRef
*error
, void (^perform
)(SecDbConnectionRef dbconn
));
66 // TODO: DEBUG only -> Private header
67 CFIndex
SecDbIdleConnectionCount(SecDbRef db
);
70 // MARK: SecDbConectionRef
72 CFTypeID
SecDbConnectionGetTypeID(void);
74 bool SecDbPrepare(SecDbConnectionRef dbconn
, CFStringRef sql
, CFErrorRef
*error
, void(^exec
)(sqlite3_stmt
*stmt
));
76 bool SecDbStep(SecDbConnectionRef dbconn
, sqlite3_stmt
*stmt
, CFErrorRef
*error
, void (^row
)(bool *stop
));
78 bool SecDbExec(SecDbConnectionRef dbconn
, CFStringRef sql
, CFErrorRef
*error
);
80 bool SecDbCheckpoint(SecDbConnectionRef dbconn
, CFErrorRef
*error
);
82 bool SecDbTransaction(SecDbConnectionRef dbconn
, SecDbTransactionType ttype
, CFErrorRef
*error
,
83 void (^transaction
)(bool *commit
));
85 sqlite3
*SecDbHandle(SecDbConnectionRef dbconn
);
91 bool SecDbBindNull(sqlite3_stmt
*stmt
, int param
, CFErrorRef
*error
);
93 bool SecDbBindBlob(sqlite3_stmt
*stmt
, int param
, const void *zData
, size_t n
, void(*xDel
)(void*), CFErrorRef
*error
);
94 bool SecDbBindText(sqlite3_stmt
*stmt
, int param
, const char *zData
, size_t n
, void(*xDel
)(void*), CFErrorRef
*error
);
95 bool SecDbBindDouble(sqlite3_stmt
*stmt
, int param
, double value
, CFErrorRef
*error
);
96 bool SecDbBindInt(sqlite3_stmt
*stmt
, int param
, int value
, CFErrorRef
*error
);
97 bool SecDbBindInt64(sqlite3_stmt
*stmt
, int param
, sqlite3_int64 value
, CFErrorRef
*error
);
98 bool SecDbBindObject(sqlite3_stmt
*stmt
, int param
, CFTypeRef value
, CFErrorRef
*error
);
101 // MARK: SecDbStatementRef
103 bool SecDbReset(sqlite3_stmt
*stmt
, CFErrorRef
*error
);
104 bool SecDbClearBindings(sqlite3_stmt
*stmt
, CFErrorRef
*error
);
105 bool SecDbFinalize(sqlite3_stmt
*stmt
, CFErrorRef
*error
);
106 sqlite3_stmt
*SecDbPrepareV2(SecDbConnectionRef dbconn
, const char *sql
, size_t sqlLen
, const char **sqlTail
, CFErrorRef
*error
);
107 sqlite3_stmt
*SecDbCopyStmt(SecDbConnectionRef dbconn
, CFStringRef sql
, CFStringRef
*tail
, CFErrorRef
*error
);
108 bool SecDbReleaseCachedStmt(SecDbConnectionRef dbconn
, CFStringRef sql
, sqlite3_stmt
*stmt
, CFErrorRef
*error
);
109 bool SecDbWithSQL(SecDbConnectionRef dbconn
, CFStringRef sql
, CFErrorRef
*error
, bool(^perform
)(sqlite3_stmt
*stmt
));
110 bool SecDbForEach(sqlite3_stmt
*stmt
, CFErrorRef
*error
, bool(^row
)(int row_index
));
112 // Mark the database as corrupted.
113 void SecDbCorrupt(SecDbConnectionRef dbconn
);
117 #endif /* !_UTILITIES_SECDB_H_ */