CMysqlCommandBuilder.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * CMysqlCommandBuilder class file.
  4. *
  5. * @author Carsten Brandt <mail@cebe.cc>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CMysqlCommandBuilder provides basic methods to create query commands for tables.
  12. *
  13. * @author Carsten Brandt <mail@cebe.cc>
  14. * @package system.db.schema.mysql
  15. * @since 1.1.13
  16. */
  17. class CMysqlCommandBuilder extends CDbCommandBuilder
  18. {
  19. /**
  20. * Alters the SQL to apply JOIN clause.
  21. * This method handles the mysql specific syntax where JOIN has to come before SET in UPDATE statement
  22. * and for DELETE where JOIN has to be after FROM part.
  23. * @param string $sql the SQL statement to be altered
  24. * @param string $join the JOIN clause (starting with join type, such as INNER JOIN)
  25. * @return string the altered SQL statement
  26. */
  27. public function applyJoin($sql,$join)
  28. {
  29. if($join=='')
  30. return $sql;
  31. if(strpos($sql,'UPDATE')===0 && ($pos=strpos($sql,'SET'))!==false)
  32. return substr($sql,0,$pos).$join.' '.substr($sql,$pos);
  33. elseif(strpos($sql,'DELETE FROM ')===0)
  34. {
  35. $tableName=substr($sql,12);
  36. return "DELETE {$tableName} FROM {$tableName} ".$join;
  37. }
  38. else
  39. return $sql.' '.$join;
  40. }
  41. /**
  42. * Alters the SQL to apply LIMIT and OFFSET.
  43. * @param string $sql SQL query string without LIMIT and OFFSET.
  44. * @param integer $limit maximum number of rows, -1 to ignore limit.
  45. * @param integer $offset row offset, -1 to ignore offset.
  46. * @return string SQL with LIMIT and OFFSET
  47. */
  48. public function applyLimit($sql,$limit,$offset)
  49. {
  50. // Ugly, but this is how MySQL recommends doing it: https://dev.mysql.com/doc/refman/5.0/en/select.html
  51. if($limit<=0 && $offset>0)
  52. $limit=PHP_INT_MAX;
  53. return parent::applyLimit($sql,$limit,$offset);
  54. }
  55. }