Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions framework/db/schema/oci/COciCommandBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -143,4 +143,4 @@ public function createMultipleInsertCommand($table,array $data)
);
return $this->composeMultipleInsertCommand($table,$data,$templates);
}
}
}
24 changes: 23 additions & 1 deletion framework/db/schema/oci/COciSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<EOD
SELECT
UD.REFERENCED_NAME AS SEQUENCE_NAME
FROM USER_DEPENDENCIES UD
JOIN USER_TRIGGERS UT ON (UT.TRIGGER_NAME = UD.NAME)
WHERE
UT.TABLE_NAME = '{$tableName}'
AND UD.TYPE = 'TRIGGER'
AND UD.REFERENCED_TYPE = 'SEQUENCE'
EOD;

$sequenceName = $this->getDbConnection()->createCommand($sequenceNameSql)->queryScalar();
return $sequenceName === false ? '' : $sequenceName;
}

/**
* Creates a table column.
* @param array $column column metadata
Expand Down