Clean up VectorEffects::init
[hiphop-php.git] / hphp / runtime / vm / repo_helpers.h
blobe6310a0d5837d94aa5ca0c203671b6925b3cfd91
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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>
21 #include <sqlite3.h>
23 #include "hphp/runtime/base/md5.h"
24 #include "hphp/runtime/base/complex_types.h"
26 namespace HPHP {
28 // Forward declaration.
29 class Repo;
30 class BlobEncoder;
31 class BlobDecoder;
33 enum RepoId {
34 RepoIdInvalid = -1,
35 RepoIdCentral = 0,
36 RepoIdLocal = 1,
38 RepoIdCount = 2 // Number of database IDs.
41 class RepoExc : public std::exception {
42 public:
43 RepoExc(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3) {
44 va_list(ap);
45 va_start(ap, fmt);
46 char* msg;
47 if (vasprintf(&msg, fmt, ap) == -1) {
48 m_msg = "";
49 } else {
50 m_msg = msg;
51 free(msg);
53 va_end(ap);
55 ~RepoExc() throw() {}
56 const std::string& msg() const { return m_msg; }
57 const char* what() const throw() { return m_msg.c_str(); }
59 private:
60 std::string m_msg;
63 class RepoStmt {
64 public:
65 explicit RepoStmt(Repo& repo);
66 ~RepoStmt();
67 private:
68 void finalize();
69 public:
70 bool prepared() const { return (m_stmt != nullptr); }
71 void prepare(const std::string& sql);
72 void reset();
73 Repo& repo() const { return m_repo; }
74 sqlite3_stmt*& get() { return m_stmt; }
75 const std::string& sql() const { return m_sql; }
77 protected:
78 Repo& m_repo;
79 std::string m_sql;
80 sqlite3_stmt* m_stmt;
83 // Under most circumstances RepoTxnQuery should be used instead of RepoQuery;
84 // queries outside of transactions are fraught with peril.
85 class RepoQuery {
86 public:
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,
94 bool isStatic=false);
95 void bindBlob(const char* paramName, const BlobEncoder& blob,
96 bool isStatic=false);
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);
111 void step();
112 bool row() const { return m_row; }
113 bool done() const { return m_done; }
114 void reset();
115 void exec();
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);
140 protected:
141 RepoStmt& m_stmt;
142 bool m_row;
143 bool m_done;
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 {
153 public:
154 explicit RepoTxn(Repo& repo);
155 ~RepoTxn();
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);
166 void commit();
168 bool error() const { return m_error; }
170 private:
171 friend class RepoTxnQuery;
172 void step(RepoQuery& query);
173 void exec(RepoQuery& query);
174 void rollback(); // nothrow
176 Repo& m_repo;
177 bool m_pending;
178 bool m_error;
181 class RepoTxnQuery : public RepoQuery {
182 public:
183 RepoTxnQuery(RepoTxn& txn, RepoStmt& stmt)
184 : RepoQuery(stmt), m_txn(txn) {
187 void step();
188 void exec();
190 private:
191 RepoTxn& m_txn;
194 class RepoProxy {
195 public:
196 explicit RepoProxy(Repo& repo) : m_repo(repo) {}
197 ~RepoProxy() {}
199 protected:
200 class Stmt : public RepoStmt {
201 public:
202 Stmt(Repo& repo, int repoId) : RepoStmt(repo), m_repoId(repoId) {}
203 protected:
204 int m_repoId;
207 Repo& m_repo;
212 #endif