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