2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2015 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 "hphp/runtime/base/attr.h"
21 #include "hphp/runtime/base/types.h"
23 #include "hphp/util/md5.h"
24 #include "hphp/util/portability.h"
26 #include <boost/noncopyable.hpp>
31 // Forward declaration.
43 RepoIdCount
= 2 // Number of database IDs.
46 struct RepoExc
: std::exception
{
47 RepoExc(ATTRIBUTE_PRINTF_STRING
const char* fmt
, ...)
48 ATTRIBUTE_PRINTF(2, 3) {
52 if (vasprintf(&msg
, fmt
, ap
) == -1) {
60 const std::string
& msg() const { return m_msg
; }
61 const char* what() const noexcept override
{ return m_msg
.c_str(); }
69 explicit RepoStmt(Repo
& repo
);
74 bool prepared() const { return (m_stmt
!= nullptr); }
75 void prepare(const std::string
& sql
);
77 Repo
& repo() const { return m_repo
; }
78 sqlite3_stmt
*& get() { return m_stmt
; }
79 const std::string
& sql() const { return m_sql
; }
87 // Under most circumstances RepoTxnQuery should be used instead of RepoQuery;
88 // queries outside of transactions are fraught with peril.
91 explicit RepoQuery(RepoStmt
& stmt
)
92 : m_stmt(stmt
), m_row(false), m_done(false) {
93 assert(m_stmt
.prepared());
95 ~RepoQuery() { m_stmt
.reset(); }
97 void bindBlob(const char* paramName
, const void* blob
, size_t size
,
99 void bindBlob(const char* paramName
, const BlobEncoder
& blob
,
100 bool isStatic
=false);
101 void bindMd5(const char* paramName
, const MD5
& md5
);
102 void bindTypedValue(const char* paramName
, const TypedValue
& tv
);
103 void bindText(const char* paramName
, const char* text
, size_t size
,
104 bool isStatic
=false);
105 void bindStaticString(const char* paramName
, const StringData
* sd
);
106 void bindStdString(const char* paramName
, const std::string
& s
);
107 void bindDouble(const char* paramName
, double val
);
108 void bindInt(const char* paramName
, int val
);
109 void bindId(const char* paramName
, Id id
);
110 void bindOffset(const char* paramName
, Offset offset
);
111 void bindAttr(const char* paramName
, Attr attrs
);
112 void bindBool(const char* paramName
, bool b
);
113 void bindInt64(const char* paramName
, int64_t val
);
114 void bindNull(const char* paramName
);
117 bool row() const { return m_row
; }
118 bool done() const { return m_done
; }
122 // rowid() returns the row id associated with the most recent successful
123 // insert. Thus the rowid is irrelevant for non-insert queries.
124 int64_t getInsertedRowid();
125 bool isBlob(int iCol
);
126 bool isText(int iCol
);
127 bool isDouble(int iCol
);
128 bool isInt(int iCol
);
129 bool isNull(int iCol
);
130 void getBlob(int iCol
, const void*& blob
, size_t& size
);
131 BlobDecoder
getBlob(int iCol
);
132 void getMd5(int iCol
, MD5
& md5
);
133 void getTypedValue(int iCol
, TypedValue
& tv
);
134 void getText(int iCol
, const char*& text
);
135 void getText(int iCol
, const char*& text
, size_t& size
);
136 void getStaticString(int iCol
, StringData
*& s
);
137 void getStdString(int iCol
, std::string
& s
);
138 void getDouble(int iCol
, double& val
);
139 void getInt(int iCol
, int& val
);
140 void getId(int iCol
, Id
& id
);
141 void getOffset(int iCol
, Offset
& offset
);
142 void getAttr(int iCol
, Attr
& attrs
);
143 void getBool(int iCol
, bool& b
);
144 void getInt64(int iCol
, int64_t& val
);
153 * Transaction guard object.
155 * Semantics: the guard object will rollback the transaction unless
156 * you tell it not to. Call .commit() when you want things to stay.
158 class RepoTxn
: boost::noncopyable
{
160 explicit RepoTxn(Repo
& repo
);
164 * All these routines may throw if there is an error accessing the
165 * repo. The RepoTxn object will rollback the entire transaction in
166 * any of these cases (which technically means they only provide the
167 * basic exception safety guarantee, even though the whole point is
168 * to behave transactionally. ;)
170 void prepare(RepoStmt
& stmt
, const std::string
& sql
);
171 void exec(const std::string
& sQuery
);
174 bool error() const { return m_error
; }
177 friend class RepoTxnQuery
;
178 void step(RepoQuery
& query
);
179 void exec(RepoQuery
& query
);
180 void rollback(); // nothrow
187 class RepoTxnQuery
: public RepoQuery
{
189 RepoTxnQuery(RepoTxn
& txn
, RepoStmt
& stmt
)
190 : RepoQuery(stmt
), m_txn(txn
) {
202 explicit RepoProxy(Repo
& repo
) : m_repo(repo
) {}
206 class Stmt
: public RepoStmt
{
208 Stmt(Repo
& repo
, int repoId
) : RepoStmt(repo
), m_repoId(repoId
) {}