CMysqlCommandBuilder.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. }