/web/fuel/core/classes/database/query.php

https://github.com/leonardteo/INSE6530 · PHP · 236 lines · 90 code · 35 blank · 111 comment · 2 complexity · 9665b8aa3b6ee6fe11db8bcbfca020e9 MD5 · raw file

  1. <?php
  2. /**
  3. * Database query wrapper.
  4. *
  5. * @package Fuel/Database
  6. * @category Query
  7. * @author Kohana Team
  8. * @copyright (c) 2008-2009 Kohana Team
  9. * @license http://kohanaphp.com/license
  10. */
  11. namespace Fuel\Core;
  12. class Database_Query {
  13. // Query type
  14. protected $_type;
  15. // Cache lifetime
  16. protected $_lifetime;
  17. // SQL statement
  18. protected $_sql;
  19. // Quoted query parameters
  20. protected $_parameters = array();
  21. // Return results as associative arrays or objects
  22. protected $_as_object = FALSE;
  23. /**
  24. * Creates a new SQL query of the specified type.
  25. *
  26. * @param integer query type: DB::SELECT, DB::INSERT, etc
  27. * @param string query string
  28. * @return void
  29. */
  30. public function __construct($sql, $type = null)
  31. {
  32. $this->_type = $type;
  33. $this->_sql = $sql;
  34. }
  35. /**
  36. * Return the SQL query string.
  37. *
  38. * @return string
  39. */
  40. final public function __toString()
  41. {
  42. try
  43. {
  44. // Return the SQL string
  45. return $this->compile(\Database_Connection::instance());
  46. }
  47. catch (Exception $e)
  48. {
  49. return $e->getMessage();
  50. }
  51. }
  52. /**
  53. * Get the type of the query.
  54. *
  55. * @return integer
  56. */
  57. public function type()
  58. {
  59. return $this->_type;
  60. }
  61. /**
  62. * Enables the query to be cached for a specified amount of time.
  63. *
  64. * @param integer number of seconds to cache or null for default
  65. * @return $this
  66. */
  67. public function cached($lifetime = NULL)
  68. {
  69. $this->_lifetime = $lifetime;
  70. return $this;
  71. }
  72. /**
  73. * Returns results as associative arrays
  74. *
  75. * @return $this
  76. */
  77. public function as_assoc()
  78. {
  79. $this->_as_object = FALSE;
  80. return $this;
  81. }
  82. /**
  83. * Returns results as objects
  84. *
  85. * @param string classname or TRUE for stdClass
  86. * @return $this
  87. */
  88. public function as_object($class = TRUE)
  89. {
  90. $this->_as_object = $class;
  91. return $this;
  92. }
  93. /**
  94. * Set the value of a parameter in the query.
  95. *
  96. * @param string parameter key to replace
  97. * @param mixed value to use
  98. * @return $this
  99. */
  100. public function param($param, $value)
  101. {
  102. // Add or overload a new parameter
  103. $this->_parameters[$param] = $value;
  104. return $this;
  105. }
  106. /**
  107. * Bind a variable to a parameter in the query.
  108. *
  109. * @param string parameter key to replace
  110. * @param mixed variable to use
  111. * @return $this
  112. */
  113. public function bind($param, & $var)
  114. {
  115. // Bind a value to a variable
  116. $this->_parameters[$param] =& $var;
  117. return $this;
  118. }
  119. /**
  120. * Add multiple parameters to the query.
  121. *
  122. * @param array list of parameters
  123. * @return $this
  124. */
  125. public function parameters(array $params)
  126. {
  127. // Merge the new parameters in
  128. $this->_parameters = $params + $this->_parameters;
  129. return $this;
  130. }
  131. /**
  132. * Compile the SQL query and return it. Replaces any parameters with their
  133. * given values.
  134. *
  135. * @param object Database instance
  136. * @return string
  137. */
  138. public function compile(\Database_Connection$db)
  139. {
  140. // Import the SQL locally
  141. $sql = $this->_sql;
  142. if ( ! empty($this->_parameters))
  143. {
  144. // Quote all of the values
  145. $values = array_map(array($db, 'quote'), $this->_parameters);
  146. // Replace the values in the SQL
  147. $sql = strtr($sql, $values);
  148. }
  149. return $sql;
  150. }
  151. /**
  152. * Execute the current query on the given database.
  153. *
  154. * @param mixed Database instance or name of instance
  155. * @return object Database_Result for SELECT queries
  156. * @return mixed the insert id for INSERT queries
  157. * @return integer number of affected rows for all other queries
  158. */
  159. public function execute($db = NULL)
  160. {
  161. if ( ! is_object($db))
  162. {
  163. // Get the database instance
  164. $db = \Database_Connection::instance($db);
  165. }
  166. // Compile the SQL query
  167. $sql = $this->compile($db);
  168. /* if ( ! empty($this->_lifetime) AND $this->_type === DB::SELECT)
  169. {
  170. // Set the cache key based on the database instance name and SQL
  171. $cache_key = 'Database_Connection::query("'.$db.'", "'.$sql.'")';
  172. if ($result = Kohana::cache($cache_key, NULL, $this->_lifetime))
  173. {
  174. // Return a cached result
  175. return new Database_Result_Cached($result, $sql, $this->_as_object);
  176. }
  177. }
  178. */
  179. switch(strtoupper(substr($sql, 0, 6)))
  180. {
  181. case 'SELECT':
  182. $this->_type = \DB::SELECT;
  183. break;
  184. case 'INSERT':
  185. case 'CREATE':
  186. $this->_type = \DB::INSERT;
  187. break;
  188. }
  189. \DB::$query_count++;
  190. // Execute the query
  191. $result = $db->query($this->_type, $sql, $this->_as_object);
  192. /* if (isset($cache_key))
  193. {
  194. // Cache the result array
  195. Kohana::cache($cache_key, $result->as_array(), $this->_lifetime);
  196. }
  197. */
  198. return $result;
  199. }
  200. } // End Database_Query