Source for file Query.abstract.php

Documentation is available at Query.abstract.php

  1. <?php
  2. /**
  3. * @package harmoni.dbc
  4. *
  5. * @copyright Copyright &copy; 2005, Middlebury College
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  7. *
  8. * @version $Id: Query.abstract.php,v 1.6 2007/09/05 21:38:59 adamfranco Exp $
  9. */
  10. require_once(HARMONI."DBHandler/Query.interface.php");
  11.  
  12. /**
  13. * A generic Query interface to be implemented by all Query objects.
  14. *
  15. *
  16. * @package harmoni.dbc
  17. *
  18. * @copyright Copyright &copy; 2005, Middlebury College
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  20. *
  21. * @version $Id: Query.abstract.php,v 1.6 2007/09/05 21:38:59 adamfranco Exp $
  22. */
  23.  
  24. abstract class QueryAbstract
  25. extends SObject
  26. implements Query
  27. {
  28.  
  29. /**
  30. * The type of the query.
  31. * The type of the query. Allowed values: SELECT, INSERT, DELETE,
  32. * or UPDATE.
  33. * var integer $_type The type of the query.
  34. */
  35. var $_type;
  36.  
  37. /**
  38. * Resets the query.
  39. * @access public
  40. */
  41. function reset() {
  42. $this->_type = UNKNOWN;
  43. }
  44. /**
  45. * Returns the type of this query.
  46. * Returns the type of this query: SELECT, INSERT, DELETE,
  47. * or UPDATE.
  48. * @access public
  49. * @return integer The type of this query: SELECT, INSERT, DELETE, or UPDATE.
  50. */
  51. function getType() {
  52. return $this->_type;
  53. }
  54. /**
  55. * Answer a safe SQL column string
  56. *
  57. * @param string $column
  58. * @return string
  59. * @access public
  60. * @since 3/9/07
  61. */
  62. function cleanColumn ( $column ) {
  63. if (!preg_match('/^[a-z0-9_\.]+$/i', $column))
  64. throw new DatabaseException("Invalid SQL column, '".$column."'");
  65. return $column;
  66. }
  67. /**
  68. * Add a comparison where the value is NOT quoted or escaped
  69. *
  70. * @param string $column
  71. * @param string $value
  72. * @param string $comparison
  73. * @return void
  74. * @access public
  75. * @since 3/9/07
  76. */
  77. function addWhereRawComparison ( $column, $value, $comparison, $logicalOperation = _AND ) {
  78. if (!preg_match('/[!=><]{1,3}/', $comparison))
  79. throw new DatabaseException("Invalid SQL comparison, '".$comparison."'");
  80. $this->addWhere(
  81. $this->cleanColumn($column)
  82. .$comparison
  83. .$value,
  84. $logicalOperation);
  85. }
  86. /**
  87. * Add a comparison where the value is quoted and escaped.
  88. *
  89. * @param string $column
  90. * @param string $value
  91. * @param string $comparison
  92. * @return void
  93. * @access public
  94. * @since 3/9/07
  95. */
  96. function addWhereComparison ( $column, $value, $comparison, $logicalOperation = _AND ) {
  97. $this->addWhereRawComparison(
  98. $column,
  99. "'".addslashes($value)."'",
  100. $comparison,
  101. $logicalOperation);
  102. }
  103. /**
  104. * Add a where clause of the form column='value'.
  105. *
  106. * @param string $column
  107. * @param string $value
  108. * @return void
  109. * @access public
  110. * @since 3/9/07
  111. */
  112. function addWhereEqual ( $column, $value, $logicalOperation = _AND ) {
  113. $this->addWhereComparison($column, $value, '=', $logicalOperation);
  114. }
  115. /**
  116. * Add a where clause of the form column!='value'.
  117. *
  118. * @param string $column
  119. * @param string $value
  120. * @return void
  121. * @access public
  122. * @since 3/9/07
  123. */
  124. function addWhereNotEqual ( $column, $value, $logicalOperation = _AND ) {
  125. $this->addWhereComparison($column, $value, '!=', $logicalOperation);
  126. }
  127. /**
  128. * Add a where clause of the form column<'value'.
  129. *
  130. * @param string $column
  131. * @param string $value
  132. * @return void
  133. * @access public
  134. * @since 3/9/07
  135. */
  136. function addWhereLessThan ( $column, $value, $logicalOperation = _AND ) {
  137. $this->addWhereComparison($column, $value, '<', $logicalOperation);
  138. }
  139. /**
  140. * Add a where clause of the form column>'value'.
  141. *
  142. * @param string $column
  143. * @param string $value
  144. * @return void
  145. * @access public
  146. * @since 3/9/07
  147. */
  148. function addWhereGreaterThan ( $column, $value, $logicalOperation = _AND ) {
  149. $this->addWhereComparison($column, $value, '>', $logicalOperation);
  150. }
  151. /**
  152. * Add a where clause of the form column<='value'.
  153. *
  154. * @param string $column
  155. * @param string $value
  156. * @return void
  157. * @access public
  158. * @since 3/9/07
  159. */
  160. function addWhereLessThanOrEqual ( $column, $value, $logicalOperation = _AND ) {
  161. $this->addWhereComparison($column, $value, '<=', $logicalOperation);
  162. }
  163. /**
  164. * Add a where clause of the form column>='value'.
  165. *
  166. * @param string $column
  167. * @param string $value
  168. * @return void
  169. * @access public
  170. * @since 3/9/07
  171. */
  172. function addWhereGreaterThanOrEqual ( $column, $value, $logicalOperation = _AND ) {
  173. $this->addWhereComparison($column, $value, '>=', $logicalOperation);
  174. }
  175. /**
  176. * Add a where clause of the form column='value'.
  177. *
  178. * @param string $column
  179. * @param string $value
  180. * @return void
  181. * @access public
  182. * @since 3/9/07
  183. */
  184. function addWhereRawEqual ( $column, $value, $logicalOperation = _AND ) {
  185. $this->addWhereRawComparison($column, $value, '=', $logicalOperation);
  186. }
  187. /**
  188. * Add a where clause of the form column!='value'.
  189. *
  190. * @param string $column
  191. * @param string $value
  192. * @return void
  193. * @access public
  194. * @since 3/9/07
  195. */
  196. function addWhereRawNotEqual ( $column, $value, $logicalOperation = _AND ) {
  197. $this->addWhereRawComparison($column, $value, '!=', $logicalOperation);
  198. }
  199. /**
  200. * Add a where clause of the form column<'value'.
  201. *
  202. * @param string $column
  203. * @param string $value
  204. * @return void
  205. * @access public
  206. * @since 3/9/07
  207. */
  208. function addWhereRawLessThan ( $column, $value, $logicalOperation = _AND ) {
  209. $this->addWhereRawComparison($column, $value, '<', $logicalOperation);
  210. }
  211. /**
  212. * Add a where clause of the form column>'value'.
  213. *
  214. * @param string $column
  215. * @param string $value
  216. * @return void
  217. * @access public
  218. * @since 3/9/07
  219. */
  220. function addWhereRawGreaterThan ( $column, $value, $logicalOperation = _AND ) {
  221. $this->addWhereRawComparison($column, $value, '>', $logicalOperation);
  222. }
  223. /**
  224. * Add a where clause of the form column<='value'.
  225. *
  226. * @param string $column
  227. * @param string $value
  228. * @return void
  229. * @access public
  230. * @since 3/9/07
  231. */
  232. function addWhereRawLessThanOrEqual ( $column, $value, $logicalOperation = _AND ) {
  233. $this->addWhereRawComparison($column, $value, '<=', $logicalOperation);
  234. }
  235. /**
  236. * Add a where clause of the form column>='value'.
  237. *
  238. * @param string $column
  239. * @param string $value
  240. * @return void
  241. * @access public
  242. * @since 3/9/07
  243. */
  244. function addWhereRawGreaterThanOrEqual ( $column, $value, $logicalOperation = _AND ) {
  245. $this->addWhereRawComparison($column, $value, '>=', $logicalOperation);
  246. }
  247. /**
  248. * Add a where clause of the form column IS NULL.
  249. *
  250. * @param string $column
  251. * @param string $value
  252. * @return void
  253. * @access public
  254. * @since 3/9/07
  255. */
  256. function addWhereNull ( $column, $logicalOperation = _AND ) {
  257. $this->addWhere(
  258. $this->cleanColumn($column)." IS NULL",
  259. $logicalOperation);
  260. }
  261. /**
  262. * Add a where clause of the form column IS NOT NULL.
  263. *
  264. * @param string $column
  265. * @param string $value
  266. * @return void
  267. * @access public
  268. * @since 3/9/07
  269. */
  270. function addWhereNotNull ( $column, $logicalOperation = _AND ) {
  271. $this->addWhere(
  272. $this->cleanColumn($column)." IS NOT NULL",
  273. $logicalOperation);
  274. }
  275. /**
  276. * Add a where clause of the form column IN ('xxx', 'yyy').
  277. *
  278. * @param string $column
  279. * @param array $values
  280. * @return void
  281. * @access public
  282. * @since 3/9/07
  283. */
  284. function addWhereIn ( $column, $values, $logicalOperation = _AND ) {
  285. $tmp = array();
  286. foreach ($values as $value) {
  287. $tmp[] = "'".addslashes($value)."'";
  288. }
  289. $string = $this->cleanColumn($column)." IN (";
  290. $string .= implode(", ", $tmp);
  291. $string .= ")";
  292. $this->addWhere($string, $logicalOperation);
  293. }
  294. /**
  295. * Add a where clause of the form column NOT IN ('xxx', 'yyy').
  296. *
  297. * @param string $column
  298. * @param array $values
  299. * @return void
  300. * @access public
  301. * @since 3/9/07
  302. */
  303. function addWhereNotIn ( $column, $values, $logicalOperation = _AND ) {
  304. $tmp = array();
  305. foreach ($values as $value) {
  306. $tmp[] = "'".addslashes($value)."'";
  307. }
  308. $string = $this->cleanColumn($column)." NOT IN (";
  309. $string .= implode(", ", $tmp);
  310. $string .= ")";
  311. $this->addWhere($string, $logicalOperation);
  312. }
  313. /**
  314. * Add a where clause of the form column IN ('xxx', 'yyy').
  315. *
  316. * @param string $column
  317. * @param array $values
  318. * @return void
  319. * @access public
  320. * @since 3/9/07
  321. */
  322. function addWhereRawIn ( $column, $values, $logicalOperation = _AND ) {
  323. $string = $this->cleanColumn($column)." IN (";
  324. $string .= implode(", ", $values);
  325. $string .= ")";
  326. $this->addWhere($string, $logicalOperation);
  327. }
  328. /**
  329. * Add a where clause of the form column NOT IN ('xxx', 'yyy').
  330. *
  331. * @param string $column
  332. * @param array $values
  333. * @return void
  334. * @access public
  335. * @since 3/9/07
  336. */
  337. function addWhereRawNotIn ( $column, $values, $logicalOperation = _AND ) {
  338. $string = $this->cleanColumn($column)." NOT IN (";
  339. $string .= implode(", ", $values);
  340. $string .= ")";
  341. $this->addWhere($string, $logicalOperation);
  342. }
  343. /**
  344. * Answer a string representation of the query.
  345. *
  346. * @param int $dbIndex
  347. * @return string
  348. * @access public
  349. * @since 7/10/07
  350. */
  351. function asString ($dbIndex = 0) {
  352. $dbc = Services::getService('DBHandler');
  353. return $dbc->generateSQL($this, $dbIndex);
  354. }
  355. }
  356.  
  357. ?>

Documentation generated on Wed, 19 Sep 2007 10:25:44 -0400 by phpDocumentor 1.3.0RC3