1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16:
17: class COciCommandBuilder extends CDbCommandBuilder
18: {
19: 20: 21:
22: public $returnID;
23:
24: 25: 26: 27: 28:
29: public function getLastInsertID($table)
30: {
31: return $this->returnID;
32: }
33:
34: 35: 36: 37: 38: 39: 40:
41: public function applyLimit($sql,$limit,$offset)
42: {
43: if (($limit < 0) and ($offset < 0)) return $sql;
44:
45: $filters = array();
46: if($offset>0){
47: $filters[] = 'rowNumId > '.(int)$offset;
48: }
49:
50: if($limit>=0){
51: $filters[]= 'rownum <= '.(int)$limit;
52: }
53:
54: if (count($filters) > 0){
55: $filter = implode(' and ', $filters);
56: $filter= " WHERE ".$filter;
57: }else{
58: $filter = '';
59: }
60:
61:
62: $sql = <<<EOD
63: WITH USER_SQL AS ({$sql}),
64: PAGINATION AS (SELECT USER_SQL.*, rownum as rowNumId FROM USER_SQL)
65: SELECT *
66: FROM PAGINATION
67: {$filter}
68: EOD;
69:
70: return $sql;
71: }
72:
73: 74: 75: 76: 77: 78:
79: public function createInsertCommand($table,$data)
80: {
81: $this->ensureTable($table);
82: $fields=array();
83: $values=array();
84: $placeholders=array();
85: $i=0;
86: foreach($data as $name=>$value)
87: {
88: if(($column=$table->getColumn($name))!==null && ($value!==null || $column->allowNull))
89: {
90: $fields[]=$column->rawName;
91: if($value instanceof CDbExpression)
92: {
93: $placeholders[]=$value->expression;
94: foreach($value->params as $n=>$v)
95: $values[$n]=$v;
96: }
97: else
98: {
99: $placeholders[]=self::PARAM_PREFIX.$i;
100: $values[self::PARAM_PREFIX.$i]=$column->typecast($value);
101: $i++;
102: }
103: }
104: }
105:
106: $sql="INSERT INTO {$table->rawName} (".implode(', ',$fields).') VALUES ('.implode(', ',$placeholders).')';
107:
108: if(is_string($table->primaryKey) && ($column=$table->getColumn($table->primaryKey))!==null && $column->type!=='string')
109: {
110: $sql.=' RETURNING '.$column->rawName.' INTO :RETURN_ID';
111: $command=$this->getDbConnection()->createCommand($sql);
112: $command->bindParam(':RETURN_ID', $this->returnID, PDO::PARAM_INT, 12);
113: $table->sequenceName='RETURN_ID';
114: }
115: else
116: $command=$this->getDbConnection()->createCommand($sql);
117:
118: foreach($values as $name=>$value)
119: $command->bindValue($name,$value);
120:
121: return $command;
122: }
123:
124: 125: 126: 127: 128: 129: 130: 131: 132: 133:
134: public function createMultipleInsertCommand($table,array $data)
135: {
136: $templates=array(
137: 'main'=>'INSERT ALL {{rowInsertValues}} SELECT * FROM dual',
138: 'columnInsertValue'=>'{{value}}',
139: 'columnInsertValueGlue'=>', ',
140: 'rowInsertValue'=>'INTO {{tableName}} ({{columnInsertNames}}) VALUES ({{columnInsertValues}})',
141: 'rowInsertValueGlue'=>' ',
142: 'columnInsertNameGlue'=>', ',
143: );
144: return $this->composeMultipleInsertCommand($table,$data,$templates);
145: }
146: }