- Change (P)Config::set to use INSERT >>> ON DUPLICATE KEY UPDATE
- Add DB update
This commit is contained in:
Hypolite Petovan 2016-10-01 21:40:41 -04:00
parent 82dcc3fe79
commit 3c85fb8c9a
3 changed files with 25 additions and 44 deletions

View File

@ -126,22 +126,17 @@ class Config {
public static function set($family,$key,$value) { public static function set($family,$key,$value) {
global $a; global $a;
// If $a->config[$family] has been previously set to '!<unset>!', then $a->config[$family][$key] = $value;
// $a->config[$family][$key] will evaluate to $a->config[$family][0], and
// $a->config[$family][$key] = $value will be equivalent to
// $a->config[$family][0] = $value[0] (this causes infuriating bugs),
// so unset the family before assigning a value to a family's key
if($a->config[$family] === '!<unset>!')
unset($a->config[$family]);
// manage array value // manage array value
$dbvalue = (is_array($value)?serialize($value):$value); $dbvalue = (is_array($value)?serialize($value):$value);
$dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue); $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
if(is_null(self::get($family,$key,null,true))) {
$a->config[$family][$key] = $value; $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' )
$ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ", ON DUPLICATE KEY UPDATE `v` = '%s'",
dbesc($family), dbesc($family),
dbesc($key), dbesc($key),
dbesc($dbvalue),
dbesc($dbvalue) dbesc($dbvalue)
); );
if($ret) if($ret)
@ -149,19 +144,6 @@ class Config {
return $ret; return $ret;
} }
$ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
dbesc($dbvalue),
dbesc($family),
dbesc($key)
);
$a->config[$family][$key] = $value;
if($ret)
return $value;
return $ret;
}
/** /**
* @brief Deletes the given key from the system configuration. * @brief Deletes the given key from the system configuration.
* *

View File

@ -126,31 +126,20 @@ class PConfig {
// manage array value // manage array value
$dbvalue = (is_array($value)?serialize($value):$value); $dbvalue = (is_array($value)?serialize($value):$value);
if(is_null(self::get($uid,$family,$key,null, true))) {
$a->config[$uid][$family][$key] = $value; $a->config[$uid][$family][$key] = $value;
$ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
$ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' )
ON DUPLICATE KEY UPDATE `v` = '%s'",
intval($uid), intval($uid),
dbesc($family), dbesc($family),
dbesc($key), dbesc($key),
dbesc($dbvalue),
dbesc($dbvalue) dbesc($dbvalue)
); );
if($ret) if($ret)
return $value; return $value;
return $ret; return $ret;
} }
$ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
dbesc($dbvalue),
intval($uid),
dbesc($family),
dbesc($key)
);
$a->config[$uid][$family][$key] = $value;
if($ret)
return $value;
return $ret;
}
/** /**
* @brief Deletes the given key from the users's configuration. * @brief Deletes the given key from the users's configuration.

View File

@ -1728,3 +1728,13 @@ function update_1202() {
$r = q("UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)", $r = q("UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)",
dbesc(ACCOUNT_TYPE_COMMUNITY), dbesc(PAGE_COMMUNITY), dbesc(PAGE_PRVGROUP)); dbesc(ACCOUNT_TYPE_COMMUNITY), dbesc(PAGE_COMMUNITY), dbesc(PAGE_PRVGROUP));
} }
function update_1210() {
// Convert config indexes to unique, old_alter_table=1 removes duplicates on ALTER IGNORE
$r = q("SET session old_alter_table=1;");
$r = q("ALTER TABLE config DROP INDEX cat_k");
$r = q("ALTER IGNORE TABLE config ADD UNIQUE INDEX cat_k (cat, k)");
$r = q("ALTER TABLE pconfig DROP INDEX uid_cat_k");
$r = q("ALTER IGNORE TABLE pconfig ADD UNIQUE INDEX uid_cat_k (uid, cat, k)");
}