Use RATs from HHBBC in init_use_vars
[hiphop-php.git] / hphp / runtime / vm / repo-helpers.h
blob46d53b09d4c1497d7899c7e33f0f42b25a849552
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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>
27 #include <sqlite3.h>
29 namespace HPHP {
31 // Forward declaration.
32 class BlobDecoder;
33 class BlobEncoder;
34 struct StringData;
35 struct TypedValue;
36 class Repo;
38 enum RepoId {
39 RepoIdInvalid = -1,
40 RepoIdCentral = 0,
41 RepoIdLocal = 1,
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) {
49 va_list(ap);
50 va_start(ap, fmt);
51 char* msg;
52 if (vasprintf(&msg, fmt, ap) == -1) {
53 m_msg = "";
54 } else {
55 m_msg = msg;
56 free(msg);
58 va_end(ap);
60 const std::string& msg() const { return m_msg; }
61 const char* what() const noexcept override { return m_msg.c_str(); }
63 private:
64 std::string m_msg;
67 class RepoStmt {
68 public:
69 explicit RepoStmt(Repo& repo);
70 ~RepoStmt();
71 private:
72 void finalize();
73 public:
74 bool prepared() const { return (m_stmt != nullptr); }
75 void prepare(const std::string& sql);
76 void reset();
77 Repo& repo() const { return m_repo; }
78 sqlite3_stmt*& get() { return m_stmt; }
79 const std::string& sql() const { return m_sql; }
81 protected:
82 Repo& m_repo;
83 std::string m_sql;
84 sqlite3_stmt* m_stmt;
87 // Under most circumstances RepoTxnQuery should be used instead of RepoQuery;
88 // queries outside of transactions are fraught with peril.
89 class RepoQuery {
90 public:
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,
98 bool isStatic=false);
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);
116 void step();
117 bool row() const { return m_row; }
118 bool done() const { return m_done; }
119 void reset();
120 void exec();
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);
146 protected:
147 RepoStmt& m_stmt;
148 bool m_row;
149 bool m_done;
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 {
159 public:
160 explicit RepoTxn(Repo& repo);
161 ~RepoTxn();
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);
172 void commit();
174 bool error() const { return m_error; }
176 private:
177 friend class RepoTxnQuery;
178 void step(RepoQuery& query);
179 void exec(RepoQuery& query);
180 void rollback(); // nothrow
182 Repo& m_repo;
183 bool m_pending;
184 bool m_error;
187 class RepoTxnQuery : public RepoQuery {
188 public:
189 RepoTxnQuery(RepoTxn& txn, RepoStmt& stmt)
190 : RepoQuery(stmt), m_txn(txn) {
193 void step();
194 void exec();
196 private:
197 RepoTxn& m_txn;
200 class RepoProxy {
201 public:
202 explicit RepoProxy(Repo& repo) : m_repo(repo) {}
203 ~RepoProxy() {}
205 protected:
206 class Stmt : public RepoStmt {
207 public:
208 Stmt(Repo& repo, int repoId) : RepoStmt(repo), m_repoId(repoId) {}
209 protected:
210 int m_repoId;
213 Repo& m_repo;
218 #endif