public function resetSequence($tableName, $value = null)
{
$table = $this->db->getTableSchema($tableName);
if ($table !== null && $table->sequenceName !== null) {
// c.f. http://www.postgresql.org/docs/8.1/static/functions-sequence.html
$sequence = $this->db->quoteTableName($table->sequenceName);
$tableName = $this->db->quoteTableName($tableName);
if ($value === null) {
$key = reset($table->primaryKey);
$value = "(SELECT COALESCE(MAX(\"{$key}\"),0) FROM {$tableName})+1";
} else {
$value = (int) $value;
}
return "SELECT SETVAL('{$sequence}',{$value},false)";
} elseif ($table === null) {
throw new InvalidParamException("Table not found: {$tableName}");
} else {
throw new InvalidParamException("There is not sequence associated with table '{$tableName}'.");
}
}