]>
git.saurik.com Git - apple/security.git/blob - libsecurity_utilities/lib/sqlite++.h
1db59aaa7e434efa3c48e6fb74915a8b7f31dc0a
2 * Copyright (c) 2008 Apple Computer, 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 // sqlite++ - C++ interface to SQLite3
30 #include <security_utilities/errors.h>
31 #include <security_utilities/threading.h>
32 #include <CoreFoundation/CFData.h>
41 typedef sqlite3_int64 int64
;
42 typedef sqlite3_uint64 uint64
;
48 class Error
: public CommonError
{
51 Error(int err
) : error(err
) { }
52 Error(int err
, const char *msg
) : error(err
), message(msg
) { }
55 const std::string message
;
57 const char *what() const throw () { return message
.c_str(); }
58 OSStatus
osStatus() const;
59 int unixError() const;
61 static void check(int err
);
62 static void throwMe(int err
) __attribute__((noreturn
));
67 // An sqlite3 database "connection"
70 friend class Statement
;
72 Database(const char *path
, int flags
= SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
);
78 int openFlags() const { return mOpenFlags
; }
80 // last error condition encountered
90 int execute(const char *text
, bool strict
= true);
91 int execute(const std::string
&text
, bool strict
= true)
92 { return execute(text
.c_str(), strict
); }
96 template <class RType
> RType
value(const char *text
, RType defaultResult
= RType());
97 template <class RType
> RType
value(const std::string
&text
, RType defaultResult
= RType())
98 { return value(text
.c_str(), defaultResult
); }
101 { return this->value
<double>("SELECT JULIANDAY('now');"); }
103 void busyDelay(int ms
);
107 sqlite3
*sql() const { return mDb
; }
117 // An sqlite column value.
118 // These are definitely not first-class API objects; in particular,
119 // there doesn't seem to be API to actually *make* one - you can only
120 // get them out of sqlite.
124 Value(sqlite3_value
*v
) : mValue(v
) { }
126 operator int () const { return ::sqlite3_value_int(mValue
); }
127 operator sqlite3_int64 () const { return ::sqlite3_value_int64(mValue
); }
128 operator const char * () const { return (const char *)::sqlite3_value_text(mValue
); }
129 operator double () const { return ::sqlite3_value_double(mValue
); }
131 int type() const { return ::sqlite3_value_type(mValue
); }
132 int numericType() const { return ::sqlite3_value_numeric_type(mValue
); }
134 operator bool () const { return type() != SQLITE_NULL
; }
135 bool operator ! () const { return type() == SQLITE_NULL
; }
137 sqlite3_value
*sql() const { return mValue
; }
140 sqlite3_value
*mValue
;
145 // A Transaction proxy.
156 Transaction(Database
&db
, Type type
= deferred
, const char *name
= NULL
);
157 virtual ~Transaction();
161 void rollback() { this->abort(); }
166 void xactCommand(const std::string
&s
);
174 // A (prepared) statement.
176 class Statement
: private StLock
<Mutex
> {
180 Statement(Database
&db
, const char *text
); // ready to serve
181 Statement(Database
&db
); // quiescent; call query(text) to activate it
182 virtual ~Statement();
186 operator bool () const { return mStmt
!= NULL
; } // active
188 void query(const char *text
); // activate statement with query text
189 void query(const std::string
&text
)
190 { query(text
.c_str()); }
191 void close(); // close up active statement
193 Binding
bind(int ix
) const { return Binding(*this, ix
); }
194 Binding
bind(const char *name
) const;
195 unsigned int bindings() const { return ::sqlite3_bind_parameter_count(mStmt
); }
201 bool operator () () { return nextRow(); }
206 Result
operator [] (int ix
) { return Result(*this, ix
); }
207 unsigned int count() const { return ::sqlite3_column_count(mStmt
); }
209 void check(int err
) const { database
.check(err
); }
210 sqlite3_stmt
*sql() const { return mStmt
; }
215 Column(const Statement
&st
, int ix
) : statement(st
), index(ix
) { }
217 const Statement
&statement
;
221 class Binding
: public Column
{
223 Binding(const Statement
&st
, int ix
) : Column(st
, ix
) { }
225 const char *name() const;
228 void operator = (int value
);
229 void operator = (sqlite3_int64 value
);
230 void operator = (double value
);
231 void operator = (const char *value
);
232 void operator = (const std::string
&value
);
233 void operator = (const Value
&value
);
234 void integer(sqlite3_int64 value
);
235 void blob(const void *data
, size_t length
, bool shared
= false);
236 void operator = (CFDataRef data
);
237 void operator = (CFStringRef value
);
241 class Result
: public Column
{
243 Result(const Statement
&st
, int ix
) : Column(st
, ix
) { }
245 const char *name() const;
247 operator int () const { return ::sqlite3_column_int(statement
.sql(), index
); }
248 operator sqlite3_int64 () const { return ::sqlite3_column_int64(statement
.sql(), index
); }
249 operator double () const { return ::sqlite3_column_double(statement
.sql(), index
); }
250 const char *string() const { return (const char *)::sqlite3_column_text(statement
.sql(), index
); }
251 operator const char *() const { return this->string(); }
252 const void *blob() const { return ::sqlite3_column_blob(statement
.sql(), index
); }
253 int length() const { return ::sqlite3_column_bytes(statement
.sql(), index
); }
254 CFDataRef
data() const;
256 int type() const { return ::sqlite3_column_type(statement
.sql(), index
); }
257 const char *declType() const { return ::sqlite3_column_decltype(statement
.sql(), index
); }
259 operator bool () const { return type() != SQLITE_NULL
; }
260 bool operator ! () const { return type() == SQLITE_NULL
; }
268 template <class RType
>
269 RType
Database::value(const char *text
, RType defaultResult
)
271 Statement
stmt(*this, text
);
273 return RType(stmt
[0]);
275 return defaultResult
;