Added manual sequencers
This commit is contained in:
parent
fadd4bf447
commit
61317d8b4d
17 changed files with 88 additions and 23 deletions
|
@ -131,9 +131,14 @@ abstract class AbstractDao implements ICrudDao, IBatchDao
|
||||||
|
|
||||||
public function create(Entity $entity)
|
public function create(Entity $entity)
|
||||||
{
|
{
|
||||||
|
$sequencerQuery = $this->pdo->from('sequencer')->where('tableName', $this->tableName);
|
||||||
|
$lastUsedId = intval(iterator_to_array($sequencerQuery)[0]['lastUsedId']);
|
||||||
|
$lastUsedId ++;
|
||||||
|
|
||||||
|
$entity->setId($lastUsedId);
|
||||||
$arrayEntity = $this->entityConverter->toArray($entity);
|
$arrayEntity = $this->entityConverter->toArray($entity);
|
||||||
$this->pdo->insertInto($this->tableName)->values($arrayEntity)->execute();
|
$this->pdo->insertInto($this->tableName)->values($arrayEntity)->execute();
|
||||||
$entity->setId(intval($this->pdo->lastInsertId()));
|
$this->pdo->update('sequencer')->set(['lastUsedId' => $lastUsedId])->where('tableName', $this->tableName)->execute();
|
||||||
return $entity;
|
return $entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Szurubooru\Dao\EntityConverters;
|
namespace Szurubooru\Dao\EntityConverters;
|
||||||
|
|
||||||
|
use Szurubooru\Entities\Entity;
|
||||||
|
|
||||||
abstract class AbstractEntityConverter implements IEntityConverter
|
abstract class AbstractEntityConverter implements IEntityConverter
|
||||||
{
|
{
|
||||||
private $entityDecorator = null;
|
private $entityDecorator = null;
|
||||||
|
@ -19,8 +21,18 @@ abstract class AbstractEntityConverter implements IEntityConverter
|
||||||
return $entity;
|
return $entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function toArray(Entity $entity)
|
||||||
|
{
|
||||||
|
$array = $this->toBasicArray($entity);
|
||||||
|
if ($entity->getId() !== null)
|
||||||
|
$array['id'] = $entity->getId();
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract function toBasicEntity(array $array);
|
protected abstract function toBasicEntity(array $array);
|
||||||
|
|
||||||
|
protected abstract function toBasicArray(Entity $entity);
|
||||||
|
|
||||||
protected function dbTimeToEntityTime($time)
|
protected function dbTimeToEntityTime($time)
|
||||||
{
|
{
|
||||||
if ($time === null)
|
if ($time === null)
|
||||||
|
|
|
@ -5,11 +5,10 @@ use Szurubooru\Entities\Entity;
|
||||||
|
|
||||||
class CommentEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
class CommentEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
||||||
{
|
{
|
||||||
public function toArray(Entity $entity)
|
public function toBasicArray(Entity $entity)
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
[
|
[
|
||||||
'id' => $entity->getId(),
|
|
||||||
'userId' => $entity->getUserId(),
|
'userId' => $entity->getUserId(),
|
||||||
'postId' => $entity->getPostId(),
|
'postId' => $entity->getPostId(),
|
||||||
'text' => $entity->getText(),
|
'text' => $entity->getText(),
|
||||||
|
|
|
@ -5,11 +5,10 @@ use Szurubooru\Entities\Favorite;
|
||||||
|
|
||||||
class FavoriteEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
class FavoriteEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
||||||
{
|
{
|
||||||
public function toArray(Entity $entity)
|
public function toBasicArray(Entity $entity)
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
[
|
[
|
||||||
'id' => $entity->getId(),
|
|
||||||
'userId' => $entity->getUserId(),
|
'userId' => $entity->getUserId(),
|
||||||
'postId' => $entity->getPostId(),
|
'postId' => $entity->getPostId(),
|
||||||
'time' => $this->entityTimeToDbTime($entity->getTime()),
|
'time' => $this->entityTimeToDbTime($entity->getTime()),
|
||||||
|
|
|
@ -5,11 +5,10 @@ use Szurubooru\Entities\GlobalParam;
|
||||||
|
|
||||||
class GlobalParamEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
class GlobalParamEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
||||||
{
|
{
|
||||||
public function toArray(Entity $entity)
|
public function toBasicArray(Entity $entity)
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
[
|
[
|
||||||
'id' => $entity->getId(),
|
|
||||||
'dataKey' => $entity->getKey(),
|
'dataKey' => $entity->getKey(),
|
||||||
'dataValue' => $entity->getValue(),
|
'dataValue' => $entity->getValue(),
|
||||||
];
|
];
|
||||||
|
|
|
@ -5,11 +5,10 @@ use Szurubooru\Entities\Post;
|
||||||
|
|
||||||
class PostEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
class PostEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
||||||
{
|
{
|
||||||
public function toArray(Entity $entity)
|
public function toBasicArray(Entity $entity)
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
[
|
[
|
||||||
'id' => $entity->getId(),
|
|
||||||
'name' => $entity->getName(),
|
'name' => $entity->getName(),
|
||||||
'userId' => $entity->getUserId(),
|
'userId' => $entity->getUserId(),
|
||||||
'uploadTime' => $this->entityTimeToDbTime($entity->getUploadTime()),
|
'uploadTime' => $this->entityTimeToDbTime($entity->getUploadTime()),
|
||||||
|
|
|
@ -5,11 +5,10 @@ use Szurubooru\Entities\PostNote;
|
||||||
|
|
||||||
class PostNoteEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
class PostNoteEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
||||||
{
|
{
|
||||||
public function toArray(Entity $entity)
|
public function toBasicArray(Entity $entity)
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
[
|
[
|
||||||
'id' => $entity->getId(),
|
|
||||||
'postId' => $entity->getPostId(),
|
'postId' => $entity->getPostId(),
|
||||||
'x' => $entity->getLeft(),
|
'x' => $entity->getLeft(),
|
||||||
'y' => $entity->getTop(),
|
'y' => $entity->getTop(),
|
||||||
|
|
|
@ -5,11 +5,10 @@ use Szurubooru\Entities\Score;
|
||||||
|
|
||||||
class ScoreEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
class ScoreEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
||||||
{
|
{
|
||||||
public function toArray(Entity $entity)
|
public function toBasicArray(Entity $entity)
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
[
|
[
|
||||||
'id' => $entity->getId(),
|
|
||||||
'userId' => $entity->getUserId(),
|
'userId' => $entity->getUserId(),
|
||||||
'postId' => $entity->getPostId(),
|
'postId' => $entity->getPostId(),
|
||||||
'commentId' => $entity->getCommentId(),
|
'commentId' => $entity->getCommentId(),
|
||||||
|
|
|
@ -5,11 +5,10 @@ use Szurubooru\Entities\Snapshot;
|
||||||
|
|
||||||
class SnapshotEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
class SnapshotEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
||||||
{
|
{
|
||||||
public function toArray(Entity $entity)
|
public function toBasicArray(Entity $entity)
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
[
|
[
|
||||||
'id' => $entity->getId(),
|
|
||||||
'time' => $this->entityTimeToDbTime($entity->getTime()),
|
'time' => $this->entityTimeToDbTime($entity->getTime()),
|
||||||
'type' => $entity->getType(),
|
'type' => $entity->getType(),
|
||||||
'primaryKey' => $entity->getPrimaryKey(),
|
'primaryKey' => $entity->getPrimaryKey(),
|
||||||
|
|
|
@ -5,11 +5,10 @@ use Szurubooru\Entities\Tag;
|
||||||
|
|
||||||
class TagEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
class TagEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
||||||
{
|
{
|
||||||
public function toArray(Entity $entity)
|
public function toBasicArray(Entity $entity)
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
[
|
[
|
||||||
'id' => $entity->getId(),
|
|
||||||
'name' => $entity->getName(),
|
'name' => $entity->getName(),
|
||||||
'creationTime' => $this->entityTimeToDbTime($entity->getCreationTime()),
|
'creationTime' => $this->entityTimeToDbTime($entity->getCreationTime()),
|
||||||
'banned' => $entity->isBanned(),
|
'banned' => $entity->isBanned(),
|
||||||
|
|
|
@ -5,11 +5,10 @@ use Szurubooru\Entities\Token;
|
||||||
|
|
||||||
class TokenEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
class TokenEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
||||||
{
|
{
|
||||||
public function toArray(Entity $entity)
|
public function toBasicArray(Entity $entity)
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
[
|
[
|
||||||
'id' => $entity->getId(),
|
|
||||||
'name' => $entity->getName(),
|
'name' => $entity->getName(),
|
||||||
'purpose' => $entity->getPurpose(),
|
'purpose' => $entity->getPurpose(),
|
||||||
'additionalData' => $entity->getAdditionalData(),
|
'additionalData' => $entity->getAdditionalData(),
|
||||||
|
|
|
@ -5,11 +5,10 @@ use Szurubooru\Entities\User;
|
||||||
|
|
||||||
class UserEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
class UserEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
||||||
{
|
{
|
||||||
public function toArray(Entity $entity)
|
public function toBasicArray(Entity $entity)
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
[
|
[
|
||||||
'id' => $entity->getId(),
|
|
||||||
'name' => $entity->getName(),
|
'name' => $entity->getName(),
|
||||||
'email' => $entity->getEmail(),
|
'email' => $entity->getEmail(),
|
||||||
'emailUnconfirmed' => $entity->getEmailUnconfirmed(),
|
'emailUnconfirmed' => $entity->getEmailUnconfirmed(),
|
||||||
|
|
|
@ -37,8 +37,12 @@ class DatabaseConnection
|
||||||
$cwd = getcwd();
|
$cwd = getcwd();
|
||||||
if ($this->config->getDataDirectory())
|
if ($this->config->getDataDirectory())
|
||||||
chdir($this->config->getDataDirectory());
|
chdir($this->config->getDataDirectory());
|
||||||
$this->pdo = new PDOEx($this->config->database->dsn, $this->config->database->user,
|
|
||||||
$this->config->database->password);
|
$this->pdo = new PDOEx(
|
||||||
|
$this->config->database->dsn,
|
||||||
|
$this->config->database->user,
|
||||||
|
$this->config->database->password);
|
||||||
|
|
||||||
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||||
chdir($cwd);
|
chdir($cwd);
|
||||||
}
|
}
|
||||||
|
|
48
src/Upgrades/Upgrade30.php
Normal file
48
src/Upgrades/Upgrade30.php
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
<?php
|
||||||
|
namespace Szurubooru\Upgrades;
|
||||||
|
use Szurubooru\DatabaseConnection;
|
||||||
|
|
||||||
|
class Upgrade30 implements IUpgrade
|
||||||
|
{
|
||||||
|
public function run(DatabaseConnection $databaseConnection)
|
||||||
|
{
|
||||||
|
$pdo = $databaseConnection->getPDO();
|
||||||
|
|
||||||
|
$pdo->exec('
|
||||||
|
CREATE TABLE sequencer (
|
||||||
|
tableName VARCHAR(32) NOT NULL,
|
||||||
|
lastUsedId INT(11) NOT NULL DEFAULT 0
|
||||||
|
)');
|
||||||
|
|
||||||
|
$tables = [
|
||||||
|
'favorites',
|
||||||
|
'tags',
|
||||||
|
'comments',
|
||||||
|
'globals',
|
||||||
|
'postNotes',
|
||||||
|
'posts',
|
||||||
|
'scores',
|
||||||
|
'snapshots',
|
||||||
|
'tags',
|
||||||
|
'tokens',
|
||||||
|
'users'];
|
||||||
|
|
||||||
|
foreach ($tables as $table)
|
||||||
|
{
|
||||||
|
$this->removeAutoIncrement($pdo, $table);
|
||||||
|
$this->createSequencer($pdo, $table);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function removeAutoIncrement($pdo, $table)
|
||||||
|
{
|
||||||
|
$pdo->exec('ALTER TABLE ' . $table . ' CHANGE id id INT(11) UNSIGNED NOT NULL');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createSequencer($pdo, $table)
|
||||||
|
{
|
||||||
|
$pdo->exec(sprintf('
|
||||||
|
INSERT INTO sequencer (tableName, lastUsedId)
|
||||||
|
VALUES (\'%s\', IFNULL((SELECT MAX(id) FROM %s), 0))', $table, $table));
|
||||||
|
}
|
||||||
|
}
|
|
@ -46,6 +46,7 @@ return [
|
||||||
$container->get(\Szurubooru\Upgrades\Upgrade27::class),
|
$container->get(\Szurubooru\Upgrades\Upgrade27::class),
|
||||||
$container->get(\Szurubooru\Upgrades\Upgrade28::class),
|
$container->get(\Szurubooru\Upgrades\Upgrade28::class),
|
||||||
$container->get(\Szurubooru\Upgrades\Upgrade29::class),
|
$container->get(\Szurubooru\Upgrades\Upgrade29::class),
|
||||||
|
$container->get(\Szurubooru\Upgrades\Upgrade30::class),
|
||||||
];
|
];
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
|
|
@ -61,6 +61,9 @@ final class PostDaoTest extends AbstractDatabaseTestCase
|
||||||
$post2 = self::getTestPost();
|
$post2 = self::getTestPost();
|
||||||
$postDao->save($post1);
|
$postDao->save($post1);
|
||||||
$postDao->save($post2);
|
$postDao->save($post2);
|
||||||
|
$this->assertNotNull($post1->getId());
|
||||||
|
$this->assertNotNull($post2->getId());
|
||||||
|
$this->assertNotEquals($post1->getId(), $post2->getId());
|
||||||
|
|
||||||
$actual = $postDao->findAll();
|
$actual = $postDao->findAll();
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@ final class TagServiceTest extends AbstractDatabaseTestCase
|
||||||
$pdo = $this->databaseConnection->getPDO();
|
$pdo = $this->databaseConnection->getPDO();
|
||||||
$pdo->exec('INSERT INTO tags(id, name, creationTime) VALUES (1, \'test1\', \'2014-10-01 00:00:00\')');
|
$pdo->exec('INSERT INTO tags(id, name, creationTime) VALUES (1, \'test1\', \'2014-10-01 00:00:00\')');
|
||||||
$pdo->exec('INSERT INTO tags(id, name, creationTime) VALUES (2, \'test2\', \'2014-10-01 00:00:00\')');
|
$pdo->exec('INSERT INTO tags(id, name, creationTime) VALUES (2, \'test2\', \'2014-10-01 00:00:00\')');
|
||||||
|
$pdo->exec('UPDATE sequencer SET lastUsedId = 2 WHERE tableName = \'tags\'');
|
||||||
|
|
||||||
$tag1 = new Tag();
|
$tag1 = new Tag();
|
||||||
$tag1->setName('test1');
|
$tag1->setName('test1');
|
||||||
|
@ -54,6 +55,7 @@ final class TagServiceTest extends AbstractDatabaseTestCase
|
||||||
$pdo = $this->databaseConnection->getPDO();
|
$pdo = $this->databaseConnection->getPDO();
|
||||||
$pdo->exec('INSERT INTO tags(id, name, creationTime) VALUES (1, \'test1\', \'2014-10-01 00:00:00\')');
|
$pdo->exec('INSERT INTO tags(id, name, creationTime) VALUES (1, \'test1\', \'2014-10-01 00:00:00\')');
|
||||||
$pdo->exec('INSERT INTO tags(id, name, creationTime) VALUES (2, \'test2\', \'2014-10-01 00:00:00\')');
|
$pdo->exec('INSERT INTO tags(id, name, creationTime) VALUES (2, \'test2\', \'2014-10-01 00:00:00\')');
|
||||||
|
$pdo->exec('UPDATE sequencer SET lastUsedId = 2 WHERE tableName = \'tags\'');
|
||||||
|
|
||||||
$tag1 = new Tag();
|
$tag1 = new Tag();
|
||||||
$tag1->setName('test1');
|
$tag1->setName('test1');
|
||||||
|
|
Loading…
Reference in a new issue