3 use wcf\system\database\util\PreparedStatementConditionBuilder
;
7 * Basic implementation for object editors following the decorator pattern.
10 * @copyright 2001-2019 WoltLab GmbH
11 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
12 * @package WoltLabSuite\Core\Data
14 abstract class DatabaseObjectEditor
extends DatabaseObjectDecorator
implements IEditableObject
{
18 public static function create(array $parameters = []) {
20 $statementParameters = [];
21 foreach ($parameters as $key => $value) {
29 $statementParameters[] = $value;
33 $sql = "INSERT INTO ".static::getDatabaseTableName()."
35 VALUES (".$values.")";
36 $statement = WCF
::getDB()->prepareStatement($sql);
37 $statement->execute($statementParameters);
40 if (static::getDatabaseTableIndexIsIdentity()) {
41 $id = WCF
::getDB()->getInsertID(static::getDatabaseTableName(), static::getDatabaseTableIndexName());
44 $id = $parameters[static::getDatabaseTableIndexName()];
46 return new static::$baseClass($id);
52 public function update(array $parameters = []) {
53 if (empty($parameters)) return;
56 $statementParameters = [];
57 foreach ($parameters as $key => $value) {
58 if (!empty($updateSQL)) $updateSQL .= ', ';
59 $updateSQL .= $key . ' = ?';
60 $statementParameters[] = $value;
62 $statementParameters[] = $this->getObjectID();
64 $sql = "UPDATE ".static::getDatabaseTableName()."
66 WHERE ".static::getDatabaseTableIndexName()." = ?";
67 $statement = WCF
::getDB()->prepareStatement($sql);
68 $statement->execute($statementParameters);
74 public function updateCounters(array $counters = []) {
75 if (empty($counters)) return;
78 $statementParameters = [];
79 foreach ($counters as $key => $value) {
80 if (!empty($updateSQL)) $updateSQL .= ', ';
81 $updateSQL .= $key . ' = ' . $key . ' + ?';
82 $statementParameters[] = $value;
84 $statementParameters[] = $this->getObjectID();
86 $sql = "UPDATE ".static::getDatabaseTableName()."
88 WHERE ".static::getDatabaseTableIndexName()." = ?";
89 $statement = WCF
::getDB()->prepareStatement($sql);
90 $statement->execute($statementParameters);
96 public function delete() {
97 static::deleteAll([$this->getObjectID()]);
103 public static function deleteAll(array $objectIDs = []) {
106 $itemsPerLoop = 1000;
107 $loopCount = ceil(count($objectIDs) / $itemsPerLoop);
109 WCF
::getDB()->beginTransaction();
110 for ($i = 0; $i < $loopCount; $i++
) {
111 $batchObjectIDs = array_slice($objectIDs, $i * $itemsPerLoop, $itemsPerLoop);
113 $conditionBuilder = new PreparedStatementConditionBuilder();
114 $conditionBuilder->add(static::getDatabaseTableIndexName() . ' IN (?)', [$batchObjectIDs]);
116 $sql = "DELETE FROM " . static::getDatabaseTableName() . "
117 " . $conditionBuilder;
118 $statement = WCF
::getDB()->prepareStatement($sql);
119 $statement->execute($conditionBuilder->getParameters());
120 $affectedCount +
= $statement->getAffectedRows();
122 WCF
::getDB()->commitTransaction();
124 return $affectedCount;