allow to set pre-condition for setValue
This commit is contained in:
parent
4b650a20a4
commit
2d83424a29
|
@ -84,14 +84,14 @@ class OC_Preferences{
|
||||||
* @param string $app app
|
* @param string $app app
|
||||||
* @param string $key key
|
* @param string $key key
|
||||||
* @param string $value value
|
* @param string $value value
|
||||||
* @return bool
|
* @param string $preCondition only set value if the key had a specific value before
|
||||||
|
* @return bool true if value was set, otherwise false
|
||||||
*
|
*
|
||||||
* Adds a value to the preferences. If the key did not exist before, it
|
* Adds a value to the preferences. If the key did not exist before, it
|
||||||
* will be added automagically.
|
* will be added automagically.
|
||||||
*/
|
*/
|
||||||
public static function setValue( $user, $app, $key, $value ) {
|
public static function setValue( $user, $app, $key, $value, $preCondition = null ) {
|
||||||
self::$object->setValue( $user, $app, $key, $value );
|
return self::$object->setValue( $user, $app, $key, $value, $preCondition );
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -165,44 +165,56 @@ class Preferences {
|
||||||
* @param string $app app
|
* @param string $app app
|
||||||
* @param string $key key
|
* @param string $key key
|
||||||
* @param string $value value
|
* @param string $value value
|
||||||
|
* @param string $preCondition only set value if the key had a specific value before
|
||||||
|
* @return bool true if value was set, otherwise false
|
||||||
*
|
*
|
||||||
* Adds a value to the preferences. If the key did not exist before, it
|
* Adds a value to the preferences. If the key did not exist before, it
|
||||||
* will be added automagically.
|
* will be added automagically.
|
||||||
*/
|
*/
|
||||||
public function setValue($user, $app, $key, $value) {
|
public function setValue($user, $app, $key, $value, $preCondition = null) {
|
||||||
// Check if the key does exist
|
// Check if the key does exist
|
||||||
$query = 'SELECT COUNT(*) FROM `*PREFIX*preferences`'
|
$query = 'SELECT COUNT(*) FROM `*PREFIX*preferences`'
|
||||||
. ' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?';
|
. ' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?';
|
||||||
$count = $this->conn->fetchColumn($query, array($user, $app, $key));
|
$count = $this->conn->fetchColumn($query, array($user, $app, $key));
|
||||||
$exists = $count > 0;
|
$exists = $count > 0;
|
||||||
|
|
||||||
if (!$exists) {
|
$affectedRows = 0;
|
||||||
|
|
||||||
|
if (!$exists && $preCondition === null) {
|
||||||
$data = array(
|
$data = array(
|
||||||
'userid' => $user,
|
'userid' => $user,
|
||||||
'appid' => $app,
|
'appid' => $app,
|
||||||
'configkey' => $key,
|
'configkey' => $key,
|
||||||
'configvalue' => $value,
|
'configvalue' => $value,
|
||||||
);
|
);
|
||||||
$this->conn->insert('*PREFIX*preferences', $data);
|
$affectedRows = $this->conn->insert('*PREFIX*preferences', $data);
|
||||||
|
} elseif ($exists) {
|
||||||
|
$data = array($value, $user, $app, $key);
|
||||||
|
$sql = "UPDATE `*PREFIX*preferences` SET `configvalue` = ?"
|
||||||
|
. " WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?";
|
||||||
|
|
||||||
|
if ($preCondition !== null) {
|
||||||
|
if (\OC_Config::getValue( 'dbtype', 'sqlite' ) === 'oci') {
|
||||||
|
//oracle hack: need to explicitly cast CLOB to CHAR for comparison
|
||||||
|
$sql .= " AND to_char(`configvalue`) = ?";
|
||||||
} else {
|
} else {
|
||||||
$data = array(
|
$sql .= " AND `configvalue` = ?";
|
||||||
'configvalue' => $value,
|
}
|
||||||
);
|
$data[] = $preCondition;
|
||||||
$where = array(
|
}
|
||||||
'userid' => $user,
|
$affectedRows = $this->conn->executeUpdate($sql, $data);
|
||||||
'appid' => $app,
|
|
||||||
'configkey' => $key,
|
|
||||||
);
|
|
||||||
$this->conn->update('*PREFIX*preferences', $data, $where);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// only add to the cache if we already loaded data for the user
|
// only add to the cache if we already loaded data for the user
|
||||||
if (isset($this->cache[$user])) {
|
if ($affectedRows > 0 && isset($this->cache[$user])) {
|
||||||
if (!isset($this->cache[$user][$app])) {
|
if (!isset($this->cache[$user][$app])) {
|
||||||
$this->cache[$user][$app] = array();
|
$this->cache[$user][$app] = array();
|
||||||
}
|
}
|
||||||
$this->cache[$user][$app][$key] = $value;
|
$this->cache[$user][$app][$key] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ($affectedRows > 0) ? true : false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -97,6 +97,42 @@ class Test_Preferences extends PHPUnit_Framework_TestCase {
|
||||||
$this->assertEquals('othervalue', $value);
|
$this->assertEquals('othervalue', $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSetValueWithPreCondition() {
|
||||||
|
// remove existing key
|
||||||
|
$this->assertTrue(\OC_Preferences::deleteKey('Someuser', 'setvalueapp', 'newkey'));
|
||||||
|
|
||||||
|
// add new preference with pre-condition should fails
|
||||||
|
$this->assertFalse(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'newvalue', 'preCondition'));
|
||||||
|
$query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?');
|
||||||
|
$result = $query->execute(array('Someuser', 'setvalueapp', 'newkey'));
|
||||||
|
$row = $result->fetchRow();
|
||||||
|
$this->assertFalse($row);
|
||||||
|
|
||||||
|
// add new preference without pre-condition should insert the new value
|
||||||
|
$this->assertTrue(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'newvalue'));
|
||||||
|
$query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?');
|
||||||
|
$result = $query->execute(array('Someuser', 'setvalueapp', 'newkey'));
|
||||||
|
$row = $result->fetchRow();
|
||||||
|
$value = $row['configvalue'];
|
||||||
|
$this->assertEquals('newvalue', $value);
|
||||||
|
|
||||||
|
// wrong pre-condition, value should stay the same
|
||||||
|
$this->assertFalse(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'othervalue', 'preCondition'));
|
||||||
|
$query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?');
|
||||||
|
$result = $query->execute(array('Someuser', 'setvalueapp', 'newkey'));
|
||||||
|
$row = $result->fetchRow();
|
||||||
|
$value = $row['configvalue'];
|
||||||
|
$this->assertEquals('newvalue', $value);
|
||||||
|
|
||||||
|
// correct pre-condition, value should change
|
||||||
|
$this->assertTrue(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'othervalue', 'newvalue'));
|
||||||
|
$query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?');
|
||||||
|
$result = $query->execute(array('Someuser', 'setvalueapp', 'newkey'));
|
||||||
|
$row = $result->fetchRow();
|
||||||
|
$value = $row['configvalue'];
|
||||||
|
$this->assertEquals('othervalue', $value);
|
||||||
|
}
|
||||||
|
|
||||||
public function testDeleteKey() {
|
public function testDeleteKey() {
|
||||||
$this->assertTrue(\OC_Preferences::deleteKey('Deleteuser', 'deleteapp', 'deletekey'));
|
$this->assertTrue(\OC_Preferences::deleteKey('Deleteuser', 'deleteapp', 'deletekey'));
|
||||||
$query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?');
|
$query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?');
|
||||||
|
@ -165,19 +201,11 @@ class Test_Preferences_Object extends PHPUnit_Framework_TestCase {
|
||||||
)
|
)
|
||||||
));
|
));
|
||||||
$connectionMock->expects($this->once())
|
$connectionMock->expects($this->once())
|
||||||
->method('update')
|
->method('executeUpdate')
|
||||||
->with($this->equalTo('*PREFIX*preferences'),
|
->with($this->equalTo("UPDATE `*PREFIX*preferences` SET `configvalue` = ?"
|
||||||
$this->equalTo(
|
. " WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?"),
|
||||||
array(
|
$this->equalTo(array('v2', 'grg', 'bar', 'foo'))
|
||||||
'configvalue' => 'v2',
|
);
|
||||||
)),
|
|
||||||
$this->equalTo(
|
|
||||||
array(
|
|
||||||
'userid' => 'grg',
|
|
||||||
'appid' => 'bar',
|
|
||||||
'configkey' => 'foo',
|
|
||||||
)
|
|
||||||
));
|
|
||||||
|
|
||||||
$preferences = new OC\Preferences($connectionMock);
|
$preferences = new OC\Preferences($connectionMock);
|
||||||
$preferences->setValue('grg', 'bar', 'foo', 'v1');
|
$preferences->setValue('grg', 'bar', 'foo', 'v1');
|
||||||
|
|
Loading…
Reference in New Issue