Source for file MySQL_SQLGenerator.class.php

Documentation is available at MySQL_SQLGenerator.class.php

  1. <?php
  2. /**
  3. * @package harmoni.dbc.mysql
  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: MySQL_SQLGenerator.class.php,v 1.14 2007/09/05 21:39:00 adamfranco Exp $
  9. */
  10. require_once(HARMONI."DBHandler/SQLGenerator.interface.php");
  11.  
  12. /**
  13. * A MySQLSelectQueryGenerator class provides the tools to build a MySQL query from a Query object.
  14. *
  15. *
  16. * @package harmoni.dbc.mysql
  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: MySQL_SQLGenerator.class.php,v 1.14 2007/09/05 21:39:00 adamfranco Exp $
  22. */
  23.  
  24. class MySQL_SQLGenerator extends SQLGeneratorInterface {
  25.  
  26. /**
  27. * Returns a string representing the SQL query corresonding to the specified Query object.
  28. * @param object QueryInterface $query The object from which to generate the SQL string.
  29. * @return mixed Either a string (this would be the case, normally) or an array of strings.
  30. * Each string is corresponding to an SQL query.
  31. * @static
  32. * @access public
  33. */
  34. function generateSQLQuery(Query $query) {
  35. switch($query->getType()) {
  36. case INSERT :
  37. $result = MySQL_SQLGenerator::generateInsertSQLQuery($query);
  38. break;
  39. case UPDATE :
  40. $result = MySQL_SQLGenerator::generateUpdateSQLQuery($query);
  41. break;
  42. case DELETE :
  43. $result = MySQL_SQLGenerator::generateDeleteSQLQuery($query);
  44. break;
  45. case SELECT :
  46. $result = MySQL_SQLGenerator::generateSelectSQLQuery($query);
  47. break;
  48. case GENERIC :
  49. $result = MySQL_SQLGenerator::generateGenericSQLQuery($query);
  50. break;
  51. default:
  52. throw new DatabaseException("Unsupported query type.");
  53. } // switch
  54. // echo "<pre>\n";
  55. // echo $result;
  56. // echo "</pre>\n";
  57. return $result;
  58. }
  59.  
  60.  
  61. /**
  62. * Returns a string representing the SQL query corresonding to this Query object.
  63. * @return string A string representing the SQL query corresonding to this Query object.
  64. * @access public
  65. * @static
  66. */
  67. function generateGenericSQLQuery(GenericSQLQueryInterface $query) {
  68.  
  69. $queries = $query->_sql;
  70.  
  71. if (!is_array($queries) || count($queries) == 0) {
  72. $description = "Cannot generate SQL string for this Query object due to invalid query setup.";
  73. throw new DatabaseException($description);
  74. return null;
  75. }
  76. else if (count($queries) == 1)
  77. return $queries[0];
  78. else
  79. return $queries;
  80. }
  81.  
  82.  
  83.  
  84. /**
  85. * Returns a string representing the SQL query corresonding to this Query object.
  86. * @return string A string representing the SQL query corresonding to this Query object.
  87. * @access public
  88. * @static
  89. */
  90. function generateInsertSQLQuery(InsertQueryInterface $query) {
  91.  
  92. $sql = "";
  93. if (!$query->_table || count($query->_values) == 0) {
  94. $description = "Cannot generate SQL string for this Query object due to invalid query setup.";
  95. throw new DatabaseException($description);
  96. return null;
  97. }
  98. $sql .= "INSERT\n INTO ";
  99. $sql .= $query->_table;
  100. if ($query->_columns || $query->_autoIncrementColumn) {
  101. $sql .= " (";
  102. $columns = $query->_columns;
  103. // include autoincrement column if necessary
  104. if ($query->_autoIncrementColumn)
  105. $columns[] = $query->_autoIncrementColumn;
  106. $sql .= implode(", ", $columns);
  107.  
  108. $sql .= ")";
  109. }
  110. $sql .= "\nVALUES ";
  111.  
  112. // process all rows in the $query->_values array
  113. $allRows = array();
  114. foreach ($query->_values as $rowOfValues) {
  115. // make sure that the number of fields matches the number of columns
  116. if (count($rowOfValues) != count($query->_columns)) {
  117. $description = "Cannot generate SQL string for this Query object due to invalid query setup.";
  118. throw new DatabaseException($description);
  119. return null;
  120. }
  121. // include autoincrement column if necessary
  122. if ($query->_autoIncrementColumn)
  123. $rowOfValues[] = "''";
  124. $allRows[] = implode(", ", $rowOfValues);
  125. }
  126.  
  127. $sql .= "(";
  128. $sql .= implode("),\n (", $allRows);
  129. $sql .= ")\n";
  130. return $sql;
  131. }
  132.  
  133.  
  134. /**
  135. * Returns a string representing the SQL query corresonding to this Query object.
  136. * @return string A string representing the SQL query corresonding to this Query object.
  137. * @static
  138. * @access public
  139. */
  140. function generateUpdateSQLQuery(UpdateQueryInterface $query) {
  141.  
  142. $sql = "";
  143. if (!$query->_table || count($query->_columns) == 0 || count($query->_values) == 0) {
  144. $description = "Cannot generate SQL string for this Query object due to invalid query setup.";
  145. throw new DatabaseException($description);
  146. return null;
  147. }
  148. $sql .= "UPDATE ";
  149. $sql .= $query->_table;
  150. $sql .= "\n SET ";
  151. // make sure that the number of fields matches the number of columns
  152. if (count($query->_values) != count($query->_columns)) {
  153. $description = "Cannot generate SQL string for this Query object due to invalid query setup: Num values is not equal to Num columns: ".print_r($query->_values, TRUE)." ".print_r($query->_columns, TRUE);
  154. throw new DatabaseException($description);
  155. return null;
  156. }
  157.  
  158. $updateExpressions = array(); // will store things like "id = 5" where
  159. // "id" was in the _columns array
  160. // and "5" was in the _values array
  161. // this loop sticks together _columns and _values
  162. foreach ($query->_columns as $key => $column)
  163. $updateExpressions[] = $column." = ".$query->_values[$key];
  164. $sql .= implode(",\n ", $updateExpressions);
  165. // include the WHERE clause, if necessary
  166. if ($query->_condition) {
  167. $sql .= "\n WHERE ";
  168.  
  169. // include join
  170. foreach($query->_condition as $key => $condition) {
  171. // we don't append anything for the first element
  172. if ($key != 0) {
  173. switch ($condition[1]) {
  174. case _AND :
  175. $sql .= "\n AND ";
  176. break;
  177. case _OR :
  178. $sql .= "\n OR ";
  179. break;
  180. default:
  181. throw(new Error("Unsupported logical operator!", "DBHandler", true));
  182. } // switch
  183. }
  184. $sql .= $condition[0];
  185. }
  186. }
  187. $sql .= "\n";
  188. return $sql;
  189. }
  190.  
  191.  
  192. /**
  193. * Returns a string representing the DELETE SQL query corresonding to the Query object.
  194. * @return string A string representing the DELETE SQL query corresonding to the Query object.
  195. * @static
  196. * @access public
  197. */
  198. function generateDeleteSQLQuery(DeleteQueryInterface $query) {
  199.  
  200. $sql = "";
  201. if (!$query->_table) {
  202. $description = "Cannot generate SQL string for this Query object due to invalid query setup.";
  203. throw new DatabaseException($description);
  204. return null;
  205. }
  206. $sql .= "DELETE\n FROM ";
  207. $sql .= $query->_table;
  208. // include the WHERE clause, if necessary
  209. if ($query->_condition) {
  210. $sql .= "\n WHERE ";
  211.  
  212. // include join
  213. foreach($query->_condition as $key => $condition) {
  214. // we don't append anything for the first element
  215. if ($key != 0) {
  216. switch ($condition[1]) {
  217. case _AND :
  218. $sql .= "\n AND ";
  219. break;
  220. case _OR :
  221. $sql .= "\n OR ";
  222. break;
  223. default:
  224. throw(new Error("Unsupported logical operator!", "DBHandler", true));
  225. } // switch
  226. }
  227. $sql .= $condition[0];
  228. }
  229. }
  230.  
  231. $sql .= "\n";
  232.  
  233. return $sql;
  234. }
  235.  
  236.  
  237.  
  238. /**
  239. * Returns a string representing the SELECT SQL query corresonding to the Query object.
  240. * @return string A string representing the SELECT SQL query corresonding to the Query object.
  241. * @access public
  242. */
  243. function generateSelectSQLQuery(SelectQueryInterface $query) {
  244.  
  245. $sql = "";
  246. if (count($query->_columns) == 0) {
  247. $description = "Cannot generate SQL string for this Query object due to invalid query setup.";
  248. throw new DatabaseException($description);
  249. return null;
  250. }
  251. $sql .= "SELECT ";
  252. // include the DISTINCT keyword, if necessary
  253. if ($query->_distinct) {
  254. $sql .= "DISTINCT\n ";
  255. }
  256. // process any aliases
  257. $columns = array();
  258. foreach ($query->_columns AS $column) {
  259. $str = "";
  260. if ($column[2])
  261. $str .= $column[2].".";
  262. $str .= $column[0];
  263. if ($column[1]) {
  264. $str .= " AS ";
  265. $str .= $column[1];
  266. }
  267. $columns[] = $str;
  268. }
  269. // include columns to select
  270. $sql .= implode(",\n ", $columns);
  271. // include FROM clause if necessary
  272. if ($query->_tables) {
  273. $sql .= "\n FROM ";
  274.  
  275. // include join
  276. foreach($query->_tables as $key => $table) {
  277. // depending on join type we are appending:
  278. // NO_JOIN: ","
  279. // LEFT_JOIN: "LEFT JOIN"
  280. // INNER_JOIN: "INNER JOIN"
  281. // RIGHT_JOIN: "RIGHT JOIN"
  282. // however, we don't append anything for the first element
  283. if ($key != 0) {
  284. switch ($table[1]) {
  285. case NO_JOIN :
  286. $sql .= ",\n ";
  287. break;
  288. case LEFT_JOIN :
  289. $sql .= "\n LEFT JOIN ";
  290. break;
  291. case INNER_JOIN :
  292. $sql .= "\n INNER JOIN ";
  293. break;
  294. case RIGHT_JOIN :
  295. $sql .= "\n RIGHT JOIN ";
  296. break;
  297. default:
  298. throw new DatabaseException("Unsupported JOIN type!"); ;
  299. } // switch
  300. }
  301.  
  302. // append table name
  303. $sql .= $table[0];
  304. // insert the alias if present
  305. if ($table[3])
  306. $sql .= " AS ".$table[3];
  307. // now append join condition
  308. if ($key != 0 && $table[1] != NO_JOIN && $table[2]) {
  309. $sql .= " ON ";
  310. $sql .= $table[2];
  311. }
  312. }
  313. }
  314. // include the WHERE clause, if necessary
  315. if ($query->_condition) {
  316. $sql .= "\n WHERE ";
  317.  
  318. // include join
  319. foreach($query->_condition as $key => $condition) {
  320. // we don't append anything for the first element
  321. if ($key != 0) {
  322. switch ($condition[1]) {
  323. case _AND :
  324. $sql .= "\n AND ";
  325. break;
  326. case _OR :
  327. $sql .= "\n OR ";
  328. break;
  329. default:
  330. throw new DatabaseException("Unsupported logical operator!");
  331. } // switch
  332. }
  333. $sql .= $condition[0];
  334. }
  335. }
  336.  
  337. // include the GROUP BY and HAVING clauses, if necessary
  338. if ($query->_groupBy) {
  339. $sql .= "\n GROUP BY\n ";
  340. $sql .= implode(",\n ", $query->_groupBy);
  341. if ($query->_having) {
  342. $sql .= "\nHAVING ";
  343. $sql .= $query->_having;
  344. }
  345. }
  346.  
  347. // include the ORDER BY clause, if necessary
  348. if ($query->_orderBy) {
  349. $sql .= "\n ORDER BY\n ";
  350. // generate an array for all the columns
  351. $columns = array();
  352. foreach($query->_orderBy as $orderBy) {
  353. $column = $orderBy[0];
  354. $column .= " ";
  355. $column .= ($orderBy[1] === ASCENDING) ? "ASC" : "DESC";
  356. $columns[] = $column;
  357. }
  358. $sql .= implode(",\n ", $columns);
  359. }
  360. // include the LIMIT clause, if necessary
  361. if ($query->_startFromRow || $query->_numberOfRows) {
  362. $sql .= "\n LIMIT ";
  363.  
  364. if ($query->_startFromRow) {
  365. $sql .= $query->_startFromRow - 1;
  366. $sql .= ", ";
  367. }
  368.  
  369. if ($query->_numberOfRows)
  370. $sql .= $query->_numberOfRows;
  371. else
  372. $sql .= "-1";
  373. }
  374. $sql .= "\n";
  375. return $sql;
  376. }
  377.  
  378. }
  379. ?>

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