3 use wcf\system\database\exception\DatabaseQueryExecutionException
;
4 use wcf\system\database\util\PreparedStatementConditionBuilder
;
8 * Basic implementation for object editors following the decorator pattern.
11 * @copyright 2001-2019 WoltLab GmbH
12 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
13 * @package WoltLabSuite\Core\Data
15 abstract class DatabaseObjectEditor
extends DatabaseObjectDecorator
implements IEditableObject
{
19 public static function create(array $parameters = []) {
21 $statementParameters = [];
22 foreach ($parameters as $key => $value) {
30 $statementParameters[] = $value;
34 $sql = "INSERT INTO ".static::getDatabaseTableName()."
36 VALUES (".$values.")";
37 $statement = WCF
::getDB()->prepareStatement($sql);
38 $statement->execute($statementParameters);
41 if (static::getDatabaseTableIndexIsIdentity()) {
42 $id = WCF
::getDB()->getInsertID(static::getDatabaseTableName(), static::getDatabaseTableIndexName());
45 $id = $parameters[static::getDatabaseTableIndexName()];
47 return new static::$baseClass($id);
53 public function update(array $parameters = []) {
54 if (empty($parameters)) return;
57 $statementParameters = [];
58 foreach ($parameters as $key => $value) {
59 if (!empty($updateSQL)) $updateSQL .= ', ';
60 $updateSQL .= $key . ' = ?';
61 $statementParameters[] = $value;
63 $statementParameters[] = $this->getObjectID();
65 $sql = "UPDATE ".static::getDatabaseTableName()."
67 WHERE ".static::getDatabaseTableIndexName()." = ?";
68 $statement = WCF
::getDB()->prepareStatement($sql);
69 $statement->execute($statementParameters);
75 public function updateCounters(array $counters = []) {
76 if (empty($counters)) return;
79 $statementParameters = [];
80 foreach ($counters as $key => $value) {
81 if (!empty($updateSQL)) $updateSQL .= ', ';
82 $updateSQL .= $key . ' = ' . $key . ' + ?';
83 $statementParameters[] = $value;
85 $statementParameters[] = $this->getObjectID();
87 $sql = "UPDATE ".static::getDatabaseTableName()."
89 WHERE ".static::getDatabaseTableIndexName()." = ?";
90 $statement = WCF
::getDB()->prepareStatement($sql);
91 $statement->execute($statementParameters);
97 public function delete() {
98 static::deleteAll([$this->getObjectID()]);
104 public static function deleteAll(array $objectIDs = []) {
107 $itemsPerLoop = 1000;
108 $loopCount = ceil(count($objectIDs) / $itemsPerLoop);
110 WCF
::getDB()->beginTransaction();
111 for ($i = 0; $i < $loopCount; $i++
) {
112 $batchObjectIDs = array_slice($objectIDs, $i * $itemsPerLoop, $itemsPerLoop);
114 $conditionBuilder = new PreparedStatementConditionBuilder();
115 $conditionBuilder->add(static::getDatabaseTableIndexName() . ' IN (?)', [$batchObjectIDs]);
117 $sql = "DELETE FROM " . static::getDatabaseTableName() . "
118 " . $conditionBuilder;
119 $statement = WCF
::getDB()->prepareStatement($sql);
120 $statement->execute($conditionBuilder->getParameters());
121 $affectedCount +
= $statement->getAffectedRows();
123 WCF
::getDB()->commitTransaction();
125 return $affectedCount;
129 * Creates a new object, returns null if the row already exists.
131 * @param array $parameters
132 * @return IStorableObject|null
135 public static function createOrIgnore(array $parameters = []) {
137 return static::create($parameters);
139 catch (DatabaseQueryExecutionException
$e) {
140 // Error code 23000 = duplicate key
141 if ($e->getCode() == '23000' && $e->getDriverCode() == '1062') {