2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_VM_REPO_HELPERS_H_
18 #define incl_HPHP_VM_REPO_HELPERS_H_
20 #include <boost/noncopyable.hpp>
23 #include "hphp/runtime/base/md5.h"
24 #include "hphp/runtime/base/complex_types.h"
28 // Forward declaration.
38 RepoIdCount
= 2 // Number of database IDs.
41 class RepoExc
: public std::exception
{
43 RepoExc(const char* fmt
, ...) ATTRIBUTE_PRINTF(2, 3) {
47 if (vasprintf(&msg
, fmt
, ap
) == -1) {
56 const std::string
& msg() const { return m_msg
; }
57 const char* what() const throw() { return m_msg
.c_str(); }
65 explicit RepoStmt(Repo
& repo
);
70 bool prepared() const { return (m_stmt
!= nullptr); }
71 void prepare(const std::string
& sql
);
73 Repo
& repo() const { return m_repo
; }
74 sqlite3_stmt
*& get() { return m_stmt
; }
75 const std::string
& sql() const { return m_sql
; }
83 // Under most circumstances RepoTxnQuery should be used instead of RepoQuery;
84 // queries outside of transactions are fraught with peril.
87 explicit RepoQuery(RepoStmt
& stmt
)
88 : m_stmt(stmt
), m_row(false), m_done(false) {
89 assert(m_stmt
.prepared());
91 ~RepoQuery() { m_stmt
.reset(); }
93 void bindBlob(const char* paramName
, const void* blob
, size_t size
,
95 void bindBlob(const char* paramName
, const BlobEncoder
& blob
,
97 void bindMd5(const char* paramName
, const MD5
& md5
);
98 void bindTypedValue(const char* paramName
, const TypedValue
& tv
);
99 void bindText(const char* paramName
, const char* text
, size_t size
,
100 bool isStatic
=false);
101 void bindStaticString(const char* paramName
, const StringData
* sd
);
102 void bindDouble(const char* paramName
, double val
);
103 void bindInt(const char* paramName
, int val
);
104 void bindId(const char* paramName
, Id id
);
105 void bindOffset(const char* paramName
, Offset offset
);
106 void bindAttr(const char* paramName
, Attr attrs
);
107 void bindBool(const char* paramName
, bool b
);
108 void bindInt64(const char* paramName
, int64_t val
);
109 void bindNull(const char* paramName
);
112 bool row() const { return m_row
; }
113 bool done() const { return m_done
; }
117 // rowid() returns the row id associated with the most recent successful
118 // insert. Thus the rowid is irrelevant for non-insert queries.
119 int64_t getInsertedRowid();
120 bool isBlob(int iCol
);
121 bool isText(int iCol
);
122 bool isDouble(int iCol
);
123 bool isInt(int iCol
);
124 bool isNull(int iCol
);
125 void getBlob(int iCol
, const void*& blob
, size_t& size
);
126 BlobDecoder
getBlob(int iCol
);
127 void getMd5(int iCol
, MD5
& md5
);
128 void getTypedValue(int iCol
, TypedValue
& tv
);
129 void getText(int iCol
, const char*& text
);
130 void getText(int iCol
, const char*& text
, size_t& size
);
131 void getStaticString(int iCol
, StringData
*& s
);
132 void getDouble(int iCol
, double& val
);
133 void getInt(int iCol
, int& val
);
134 void getId(int iCol
, Id
& id
);
135 void getOffset(int iCol
, Offset
& offset
);
136 void getAttr(int iCol
, Attr
& attrs
);
137 void getBool(int iCol
, bool& b
);
138 void getInt64(int iCol
, int64_t& val
);
147 * Transaction guard object.
149 * Semantics: the guard object will rollback the transaction unless
150 * you tell it not to. Call .commit() when you want things to stay.
152 class RepoTxn
: boost::noncopyable
{
154 explicit RepoTxn(Repo
& repo
);
158 * All these routines may throw if there is an error accessing the
159 * repo. The RepoTxn object will rollback the entire transaction in
160 * any of these cases (which technically means they only provide the
161 * basic exception safety guarantee, even though the whole point is
162 * to behave transactionally. ;)
164 void prepare(RepoStmt
& stmt
, const std::string
& sql
);
165 void exec(const std::string
& sQuery
);
168 bool error() const { return m_error
; }
171 friend class RepoTxnQuery
;
172 void step(RepoQuery
& query
);
173 void exec(RepoQuery
& query
);
174 void rollback(); // nothrow
181 class RepoTxnQuery
: public RepoQuery
{
183 RepoTxnQuery(RepoTxn
& txn
, RepoStmt
& stmt
)
184 : RepoQuery(stmt
), m_txn(txn
) {
196 explicit RepoProxy(Repo
& repo
) : m_repo(repo
) {}
200 class Stmt
: public RepoStmt
{
202 Stmt(Repo
& repo
, int repoId
) : RepoStmt(repo
), m_repoId(repoId
) {}