diff --git a/framework/db/schema/oci/COciCommandBuilder.php b/framework/db/schema/oci/COciCommandBuilder.php index 56066fbbeb..2ef0cfbcc8 100644 --- a/framework/db/schema/oci/COciCommandBuilder.php +++ b/framework/db/schema/oci/COciCommandBuilder.php @@ -28,7 +28,15 @@ class COciCommandBuilder extends CDbCommandBuilder */ public function getLastInsertID($table) { - return $this->returnID; + if($table !== null && $table->sequenceName !== null) { + $sql = "SELECT " . $table->sequenceName . ".CURRVAL FROM DUAL"; + try { + return $this->getDbConnection()->createCommand($sql)->queryScalar(); + } catch(Exception $e) { + return $this->returnID; + } + } + return $this->returnID; } /** @@ -105,15 +113,7 @@ public function createInsertCommand($table,$data) $sql="INSERT INTO {$table->rawName} (".implode(', ',$fields).') VALUES ('.implode(', ',$placeholders).')'; - if(is_string($table->primaryKey) && ($column=$table->getColumn($table->primaryKey))!==null && $column->type!=='string') - { - $sql.=' RETURNING '.$column->rawName.' INTO :RETURN_ID'; - $command=$this->getDbConnection()->createCommand($sql); - $command->bindParam(':RETURN_ID', $this->returnID, PDO::PARAM_INT, 12); - $table->sequenceName='RETURN_ID'; - } - else - $command=$this->getDbConnection()->createCommand($sql); + $command=$this->getDbConnection()->createCommand($sql); foreach($values as $name=>$value) $command->bindValue($name,$value); @@ -143,4 +143,4 @@ public function createMultipleInsertCommand($table,array $data) ); return $this->composeMultipleInsertCommand($table,$data,$templates); } -} \ No newline at end of file +} diff --git a/framework/db/schema/oci/COciSchema.php b/framework/db/schema/oci/COciSchema.php index 1534764c09..9b3fe467dc 100644 --- a/framework/db/schema/oci/COciSchema.php +++ b/framework/db/schema/oci/COciSchema.php @@ -213,13 +213,35 @@ protected function findColumns($table) $table->primaryKey=array($table->primaryKey,$c->name); else $table->primaryKey[]=$c->name; - $table->sequenceName=''; + $table->sequenceName=$this->getTableSequenceName($table->name); $c->autoIncrement=true; } } return true; } + /** + * Sequence name of table. + * @param string $tableName table name + * @return string Whether the sequence exists. + */ + protected function getTableSequenceName($tableName) + { + $sequenceNameSql = <<getDbConnection()->createCommand($sequenceNameSql)->queryScalar(); + return $sequenceName === false ? '' : $sequenceName; + } + /** * Creates a table column. * @param array $column column metadata