Use insertIfNotExists() when creating a group

This commit is contained in:
Joas Schilling 2016-01-14 10:08:41 +01:00 committed by Thomas Müller
parent 8f89e3520d
commit 436ce1f4a6
1 changed files with 5 additions and 33 deletions

View File

@ -85,44 +85,16 @@ class OC_Group_Database extends OC_Group_Backend {
public function createGroup( $gid ) { public function createGroup( $gid ) {
$this->fixDI(); $this->fixDI();
// Check cache first // Add group
if (isset($this->groupCache[$gid])) {
return false;
} else {
// Check for existence in DB
$qb = $this->dbConn->getQueryBuilder();
$cursor = $qb->select('gid')
->from('groups')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
->execute();
$result = $cursor->fetch();
$cursor->closeCursor();
if($result) {
// Can not add an existing group
// Add to cache
$this->groupCache[$gid] = $gid;
return false;
}
}
// Add group and exit
$qb = $this->dbConn->getQueryBuilder(); $qb = $this->dbConn->getQueryBuilder();
$result = $qb->insert('groups') $result = $qb->insertIfNotExists('groups', [
->setValue('gid', $qb->createNamedParameter($gid)) 'gid' => $gid,
->execute(); ]);
if (!$result) {
return false;
}
// Add to cache // Add to cache
$this->groupCache[$gid] = $gid; $this->groupCache[$gid] = $gid;
return true; return $result === 1;
} }
/** /**