Fix crash when trying to change staticness of a property
[hiphop-php.git] / hphp / util / db-filter.cpp
bloba22a70df0baea64d2205132218f8b4016c519a2e
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2014 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 +----------------------------------------------------------------------+
16 #include "hphp/util/db-filter.h"
18 #include "hphp/util/db-conn.h"
20 namespace HPHP {
22 int DBQueryFilter::MAX_COUNT = 500;
24 ///////////////////////////////////////////////////////////////////////////////
25 // DBInNumberFilter
27 DBInNumberFilter::DBInNumberFilter() {
30 const char *DBInNumberFilter::getFirst(const std::string &where) {
31 assert(!m_values.empty());
32 m_iter = m_values.begin();
33 return getFilter(where);
36 const char *DBInNumberFilter::getNext(const std::string &where) {
37 if (m_iter == m_values.end()) return nullptr;
38 return getFilter(where);
41 const char *DBInNumberFilter::getFilter(const std::string &where) {
42 std::string values;
43 for (int i = 0; i < MAX_COUNT; i++) {
44 if (i > 0) values += ",";
46 char buf[12];
47 sprintf(buf, "%d", *m_iter);
48 values += buf;
50 ++m_iter;
51 if (m_iter == m_values.end()) break;
54 auto pos = where.find("%s");
55 assert(pos != std::string::npos);
57 m_filter = where;
58 m_filter.replace(pos, 2, values.c_str());
59 return m_filter.c_str();
62 ///////////////////////////////////////////////////////////////////////////////
63 // DBInStringFilter
65 DBInStringFilter::DBInStringFilter(DBConn *conn) : m_conn(conn) {
66 //assert(m_conn);
69 const char *DBInStringFilter::getFirst(const std::string &where) {
70 assert(!m_values.empty());
71 m_iter = m_values.begin();
72 return getFilter(where);
75 const char *DBInStringFilter::getNext(const std::string &where) {
76 if (m_iter == m_values.end()) return nullptr;
77 return getFilter(where);
80 const char *DBInStringFilter::getFilter(const std::string &where) {
81 std::string values;
82 for (int i = 0; i < MAX_COUNT; i++) {
83 if (i > 0) values += ",";
85 values += "'";
86 if (m_conn) {
87 std::string escaped;
88 m_conn->escapeString(m_iter->c_str(), escaped);
89 values += escaped;
90 } else {
91 values += m_iter->c_str();
93 values += "'";
95 ++m_iter;
96 if (m_iter == m_values.end()) break;
99 auto pos = where.find("%s");
100 assert(pos != std::string::npos);
102 m_filter = where;
103 m_filter.replace(pos, 2, values.c_str());
104 return m_filter.c_str();
107 ///////////////////////////////////////////////////////////////////////////////
108 // DBOrStringFilter
110 DBOrStringFilter::DBOrStringFilter() {
113 const char *DBOrStringFilter::getFirst(const std::string &where) {
114 assert(!m_values.empty());
115 m_iter = m_values.begin();
116 return getFilter(where);
119 const char *DBOrStringFilter::getNext(const std::string &where) {
120 if (m_iter == m_values.end()) return nullptr;
121 return getFilter(where);
124 const char *DBOrStringFilter::getFilter(const std::string &where) {
125 std::string values = "(";
126 for (int i = 0; i < MAX_COUNT; i++) {
127 if (i > 0) values += ") or (";
129 values += *m_iter;
131 ++m_iter;
132 if (m_iter == m_values.end()) break;
134 values += ")";
136 auto pos = where.find("%s");
137 assert(pos != std::string::npos);
139 m_filter = where;
140 m_filter.replace(pos, 2, values.c_str());
141 return m_filter.c_str();
144 ///////////////////////////////////////////////////////////////////////////////