/database/mysql/MysqlQuery.php

https://github.com/regality/zombie-core · PHP · 253 lines · 133 code · 23 blank · 97 comment · 32 complexity · a631460298c9a912c8a9ed96c0e5b508 MD5 · raw file

  1. <?php
  2. # Copyright (c) 2011, Regaltic LLC. This file is
  3. # licensed under the General Public License version 3.
  4. # See the LICENSE file.
  5. /**
  6. * @package Database
  7. * @subpackage mysql
  8. */
  9. require_once(__DIR__ . "/MysqlException.php");
  10. /**
  11. * A generic mysql query.
  12. */
  13. class MysqlQuery extends SqlQuery {
  14. /**
  15. * The query to be executed.
  16. * @ignore
  17. */
  18. protected $query;
  19. /**
  20. * The parameters for the query.
  21. * @ignore
  22. */
  23. protected $params = array();
  24. /**
  25. * Number of parameters.
  26. * @ignore
  27. */
  28. protected $param_count = 0;
  29. /**
  30. * Check for magic quotes.
  31. * @ignore
  32. */
  33. protected $magic_quotes_on = false;
  34. /**
  35. * Database connection
  36. * @ignore
  37. */
  38. protected static $db = null;
  39. /**
  40. * Construct a new query.
  41. *
  42. * @param string $query the query to be executed.
  43. * @param string $connector the connector from the config file.
  44. */
  45. public function __construct($query = '', $connector = 'mysql') {
  46. if (MysqlQuery::$db == null) {
  47. $config = getZombieConfig();
  48. MysqlQuery::$db = mysql_connect($config[$connector]['host'],
  49. $config[$connector]['user'],
  50. $config[$connector]['pass']);
  51. mysql_select_db($config[$connector]['database'], MysqlQuery::$db);
  52. }
  53. if (get_magic_quotes_gpc()) {
  54. $this->magic_quotes_on = true;
  55. }
  56. $this->query = $query;
  57. }
  58. /**
  59. * Convert the query to a string.
  60. */
  61. public function __toString() {
  62. return $this->getQueryString();
  63. }
  64. /**
  65. * Retrieve the result from mysql.
  66. *
  67. * @param string $query the sql query
  68. * @param boolean $debug true will print debug info
  69. * @return MysqlResult
  70. */
  71. public function getMysqlResult($query, $debug = false) {
  72. if ($debug) {
  73. trigger_error("Query debug:" . $query, E_USER_NOTICE);
  74. }
  75. $result = mysql_query($query, MysqlQuery::$db);
  76. $errno = mysql_errno();
  77. if ($errno != 0) {
  78. $error = "Mysql Error: " . mysql_error();
  79. $exception_class = getMysqlExceptionClassName($errno);
  80. throw new $exception_class($error);
  81. }
  82. return $result;
  83. }
  84. /**
  85. * Execute a query and return the number of rows effected.
  86. * @param boolean $debug prints the executed query if true
  87. * @return integer|boolean
  88. */
  89. public function exec($debug = false) {
  90. $bound_query = $this->getBoundQuery();
  91. $result = $this->getMysqlResult($bound_query, $debug);
  92. return mysql_affected_rows(MysqlQuery::$db);
  93. }
  94. /**
  95. * Execute a query and return the results.
  96. * @param boolean $debug prints the executed query if true
  97. * @return SqlResult|boolean
  98. */
  99. public function query($debug = false) {
  100. $bound_query = $this->getBoundQuery();
  101. $result = $this->getMysqlResult($bound_query, $debug);
  102. return new MysqlResult($result);
  103. }
  104. /**
  105. * Return the query string.
  106. * @return string
  107. * @ignore
  108. */
  109. public function getQueryString() {
  110. return $this->query;
  111. }
  112. /**
  113. * Bind the parameters into the query and return it.
  114. * @return string
  115. * @ignore
  116. */
  117. protected function getBoundQuery() {
  118. $query = $this->getQueryString();
  119. $qlen = strlen($query);
  120. $bound_query = '';
  121. for ($i = 0; $i < $qlen; ++$i) {
  122. $char = $query[$i];
  123. if ($char == '$') {
  124. $key = '';
  125. while ($i < ($qlen - 1) && is_numeric($query[$i + 1])) {
  126. ++$i;
  127. $key .= $query[$i];
  128. }
  129. $key = intval($key) - 1;
  130. if (isset($this->params[$key])) {
  131. $bound_query .= $this->params[$key];
  132. } else {
  133. $error = "Wrong number of params in query: " . $query;
  134. throw new MysqlParamCountException($error);
  135. }
  136. } else {
  137. $bound_query .= $query[$i];
  138. }
  139. }
  140. return $bound_query;
  141. }
  142. /**
  143. * Add multiple parameters.
  144. * @param array $params
  145. */
  146. public function addParams($params) {
  147. foreach ($params as $param) {
  148. $this->addParam($param);
  149. }
  150. return $this;
  151. }
  152. /**
  153. * Add a parameter. If the type parameter is used
  154. * valid options are 'html', 'secure', and 'raw'.
  155. * @param mixed $value the value of the parameter
  156. * @param string $type
  157. */
  158. public function addParam($value, $type = null) {
  159. $this->params[$this->param_count] = $this->sanitize($value, $type);
  160. $this->param_count += 1;
  161. return $this;
  162. }
  163. /**
  164. * Sanitze incoming data.
  165. * @ignore
  166. */
  167. public function sanitize($value, $type = null) {
  168. if (is_null($type)) {
  169. $type = '';
  170. }
  171. if (is_string($value)) {
  172. if ($this->magic_quotes_on) {
  173. $value = stripslashes($value);
  174. }
  175. if ($type == "html") {
  176. $value = purifyHtml($value);
  177. } else if ($type == "secure") {
  178. $value = encrypt($value);
  179. } else if ($type != "raw") {
  180. $value = htmlentities($value);
  181. }
  182. $value = "'" . mysql_real_escape_string($value) . "'";
  183. } else if (is_numeric($value)) {
  184. $value = (string)$value;
  185. } else if (is_bool($value)) {
  186. $value = (string)(int)$value;
  187. } else if (is_null($value)) {
  188. $value = "NULL";
  189. } else /* array, object, or unknown */ {
  190. $value = "'" . mysql_real_escape_string(serialize($value)) . "'";
  191. }
  192. return $value;
  193. }
  194. /**
  195. * Begin a transaction.
  196. */
  197. public function begin() {
  198. $this->getMysqlResult("SET autocommit = 0");
  199. $this->getMysqlResult("START TRANSACTION");
  200. }
  201. /**
  202. * Begin a transaction.
  203. */
  204. public function rollback() {
  205. $this->getMysqlResult("ROLLBACK");
  206. }
  207. /**
  208. * Commit a transaction.
  209. */
  210. public function commit() {
  211. $this->getMysqlResult("COMMIT");
  212. }
  213. /**
  214. * Get the last insert id.
  215. * @return int
  216. */
  217. public function lastInsertId() {
  218. return mysql_insert_id();
  219. }
  220. /**
  221. * Describe a table.
  222. * @param string $table the name of the table
  223. */
  224. public function describe($table) {
  225. $query = "DESCRIBE $table";
  226. $result = $this->getMysqlResult($query);
  227. return new MysqlResult($result);
  228. }
  229. }
  230. ?>