Source for file PostgreSQL_SQLGenerator.class.php

Documentation is available at PostgreSQL_SQLGenerator.class.php

  1. <?php
  2. /**
  3. * @package harmoni.dbc.postgre
  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: PostgreSQL_SQLGenerator.class.php,v 1.1 2007/09/14 13:57:08 adamfranco Exp $
  9. */
  10. require_once(HARMONI."DBHandler/SQLGenerator.interface.php");
  11.  
  12. /**
  13. * A PostgreSQLQueryGenerator class provides the tools to build a PostgreSQL query from a Query object.
  14. *
  15. *
  16. * @package harmoni.dbc.postgre
  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: PostgreSQL_SQLGenerator.class.php,v 1.1 2007/09/14 13:57:08 adamfranco Exp $
  22. */
  23.  
  24. class PostgreSQL_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. return PostgreSQL_SQLGenerator::generateInsertSQLQuery($query);
  38. break;
  39. case UPDATE :
  40. return PostgreSQL_SQLGenerator::generateUpdateSQLQuery($query);
  41. break;
  42. case DELETE :
  43. return PostgreSQL_SQLGenerator::generateDeleteSQLQuery($query);
  44. break;
  45. case SELECT :
  46. return PostgreSQL_SQLGenerator::generateSelectSQLQuery($query);
  47. break;
  48. case GENERIC :
  49. return MySQL_SQLGenerator::generateGenericSQLQuery($query);
  50. break;
  51. default:
  52. throw new DatabaseException("Unsupported query type.");
  53. } // switch
  54. }
  55.  
  56.  
  57.  
  58. /**
  59. * Returns a string representing the SQL query corresonding to this Query object.
  60. * @return string A string representing the SQL query corresonding to this Query object.
  61. * @access public
  62. * @static
  63. */
  64. function generateGenericSQLQuery(GenericSQLQueryInterface $query) {
  65.  
  66. $queries = $query->_sql;
  67.  
  68. if (!is_array($queries) || count($queries) == 0) {
  69. $description = "Cannot generate SQL string for this Query object due to invalid query setup.";
  70. throw new DatabaseException($description);
  71. return null;
  72. }
  73. else if (count($queries) == 1)
  74. return $queries[0];
  75. else
  76. return $queries;
  77. }
  78.  
  79.  
  80.  
  81.  
  82. /**
  83. * Returns a string representing the SQL query corresonding to this Query object.
  84. * @return string A string representing the SQL query corresonding to this Query object.
  85. * @access public
  86. * @static
  87. */
  88. function generateInsertSQLQuery(InsertQueryInterface $query) {
  89. if (!$query->_table || count($query->_values) == 0) {
  90. $description = "Cannot generate SQL string for this Query object due to invalid query setup.";
  91. throw new DatabaseException($description);
  92. return null;
  93. }
  94. $count = count($query->_values);
  95. $queries = array();
  96. for ($row = 0; $row < $count; $row++) {
  97. $sql = "";
  98. $sql .= "INSERT INTO ";
  99. $sql .= $query->_table;
  100. if ($query->_columns || $query->_autoIncrementColumn) {
  101. $sql .= "\n\t(";
  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 .= "\n\tVALUES";
  111. $rowOfValues = $query->_values[$row];
  112. // make sure that the number of fields matches the number of columns
  113. if (count($rowOfValues) != count($query->_columns)) {
  114. $description = "Cannot generate SQL string for this Query object due to invalid query setup.";
  115. throw new DatabaseException($description);
  116. return null;
  117. }
  118. // include autoincrement column if necessary
  119. if ($query->_autoIncrementColumn)
  120. $rowOfValues[] = "NEXTVAL('".$query->_sequence."')";
  121. $values = implode(", ", $rowOfValues);
  122. $sql .= "(";
  123. $sql .= $values;
  124. $sql .= ")\n";
  125. $queries[] = $sql;
  126. }
  127. if (count($queries) == 1)
  128. return $queries[0];
  129. else
  130. return $queries;
  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. $sql = "";
  142. if (!$query->_table || count($query->_columns) == 0 || count($query->_values) == 0) {
  143. $description = "Cannot generate SQL string for this Query object due to invalid query setup.";
  144. throw new DatabaseException($description);
  145. return null;
  146. }
  147. $sql .= "UPDATE ";
  148. $sql .= $query->_table;
  149. $sql .= "\nSET\n\t";
  150. // make sure that the number of fields matches the number of columns
  151. if (count($query->_values) != count($query->_columns)) {
  152. $description = "Cannot generate SQL string for this Query object due to invalid query setup.";
  153. throw new DatabaseException($description);
  154. return null;
  155. }
  156.  
  157. $updateExpressions = array(); // will store things like "id = 5" where
  158. // "id" was in the _columns array
  159. // and "5" was in the _values array
  160. // this loop sticks together _columns and _values
  161. foreach ($query->_columns as $key => $column)
  162. $updateExpressions[] = $column." = ".$query->_values[$key];
  163. $sql .= implode(",\n\t", $updateExpressions);
  164. // include the WHERE clause, if necessary
  165. if ($query->_condition) {
  166. $sql .= "\nWHERE";
  167.  
  168. // include join
  169. foreach($query->_condition as $key => $condition) {
  170. // we don't append anything for the first element
  171. if ($key != 0) {
  172. switch ($condition[1]) {
  173. case _AND :
  174. $sql .= "\n\t\tAND";
  175. break;
  176. case _OR :
  177. $sql .= "\n\t\tOR";
  178. break;
  179. default:
  180. throw(new Error("Unsupported logical operator!", "DBHandler", true)); ;
  181. } // switch
  182. }
  183. $sql .= "\n\t";
  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\nFROM\n\t";
  207. $sql .= $query->_table;
  208. // include the WHERE clause, if necessary
  209. if ($query->_condition) {
  210. $sql .= "\nWHERE";
  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\t\tAND";
  219. break;
  220. case _OR :
  221. $sql .= "\n\t\tOR";
  222. break;
  223. default:
  224. throw(new Error("Unsupported logical operator!", "DBHandler", true)); ;
  225. } // switch
  226. }
  227. $sql .= "\n\t";
  228. $sql .= $condition[0];
  229. }
  230. }
  231.  
  232. $sql .= "\n";
  233.  
  234. return $sql;
  235. }
  236.  
  237.  
  238.  
  239. /**
  240. * Returns a string representing the SELECT SQL query corresonding to the Query object.
  241. * @return string A string representing the SELECT SQL query corresonding to the Query object.
  242. * @access public
  243. */
  244. function generateSelectSQLQuery(SelectQueryInterface $query) {
  245.  
  246. $sql = "";
  247. if (count($query->_columns) == 0) {
  248. $description = "Cannot generate SQL string for this Query object due to invalid query setup; No columns added";
  249. throw new DatabaseException($description);
  250. return null;
  251. }
  252. $sql .= "SELECT";
  253. // include the DISTINCT keyword, if necessary
  254. if ($query->_distinct) {
  255. $sql .= " DISTINCT";
  256. }
  257. $sql .= "\n\t";
  258. // process any aliases
  259. $columns = array();
  260. foreach ($query->_columns AS $column) {
  261. $str = "";
  262. if ($column[2])
  263. $str .= $column[2].".";
  264. $str .= $column[0];
  265. if ($column[1]) {
  266. $str .= " AS ";
  267. $str .= $column[1];
  268. }
  269. $columns[] = $str;
  270. }
  271. // include columns to select
  272. $sql .= implode(",\n\t", $columns);
  273. // include FROM clause if necessary
  274. if ($query->_tables) {
  275. $sql .= "\nFROM";
  276.  
  277. // include join
  278. foreach($query->_tables as $key => $table) {
  279. // depending on join type we are appending:
  280. // NO_JOIN: ","
  281. // LEFT_JOIN: "LEFT JOIN"
  282. // INNER_JOIN: "INNER JOIN"
  283. // RIGHT_JOIN: "RIGHT JOIN"
  284. // however, we don't append anything for the first element
  285. if ($key != 0) {
  286. switch ($table[1]) {
  287. case NO_JOIN :
  288. $sql .= ",";
  289. break;
  290. case LEFT_JOIN :
  291. $sql .= "\n\t\tLEFT JOIN";
  292. break;
  293. case INNER_JOIN :
  294. $sql .= "\n\t\tINNER JOIN";
  295. break;
  296. case RIGHT_JOIN :
  297. $sql .= "\n\t\tRIGHT JOIN";
  298. break;
  299. default:
  300. throw new DatabaseException("Unsupported JOIN type!"); ;
  301. } // switch
  302. }
  303.  
  304. $sql .= "\n\t";
  305. // append table name
  306. $sql .= $table[0];
  307. // insert the alias if present
  308. if ($table[3])
  309. $sql .= " AS ".$table[3];
  310. // now append join condition
  311. if ($key != 0 && $table[1] != NO_JOIN && $table[2]) {
  312. $sql .= "\n\t\tON ";
  313. $sql .= $table[2];
  314. }
  315. }
  316. }
  317. // include the WHERE clause, if necessary
  318. if ($query->_condition) {
  319. $sql .= "\nWHERE";
  320.  
  321. // include join
  322. foreach($query->_condition as $key => $condition) {
  323. // we don't append anything for the first element
  324. if ($key != 0) {
  325. switch ($condition[1]) {
  326. case _AND :
  327. $sql .= "\n\t\tAND";
  328. break;
  329. case _OR :
  330. $sql .= "\n\t\tOR";
  331. break;
  332. default:
  333. throw new DatabaseException("Unsupported logical operator!"); ;
  334. } // switch
  335. }
  336. $sql .= "\n\t";
  337. $sql .= $condition[0];
  338. }
  339. }
  340. // include the GROUP BY and HAVING clauses, if necessary
  341. if ($query->_groupBy) {
  342. $sql .= "\nGROUP BY\n\t";
  343. $sql .= implode(",\n\t", $query->_groupBy);
  344. if ($query->_having) {
  345. $sql .= "\nHAVING\n\t";
  346. $sql .= $query->_having;
  347. }
  348. }
  349.  
  350. // include the ORDER BY clause, if necessary
  351. if ($query->_orderBy) {
  352. $sql .= "\nORDER BY\n\t";
  353. // generate an array for all the columns
  354. $columns = array();
  355. foreach($query->_orderBy as $orderBy) {
  356. $column = $orderBy[0];
  357. $column .= " ";
  358. $column .= ($orderBy[1] === ASCENDING) ? "ASC" : "DESC";
  359. $columns[] = $column;
  360. }
  361. $sql .= implode(",\n\t", $columns);
  362. }
  363. if ($query->_numberOfRows) {
  364. $sql .= "\nLIMIT ";
  365. $sql .= $query->_numberOfRows;
  366. }
  367.  
  368. if ($query->_startFromRow) {
  369. $sql .= "\nOFFSET ";
  370. $sql .= $query->_startFromRow - 1;
  371. }
  372. $sql .= "\n";
  373. return $sql;
  374. }
  375.  
  376. }
  377. ?>

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