Added manual sequencers

This commit is contained in:
Marcin Kurczewski 2014-11-09 14:04:44 +01:00
parent fadd4bf447
commit 61317d8b4d
17 changed files with 88 additions and 23 deletions

View file

@ -131,9 +131,14 @@ abstract class AbstractDao implements ICrudDao, IBatchDao
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);
$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;
}

View file

@ -1,6 +1,8 @@
<?php
namespace Szurubooru\Dao\EntityConverters;
use Szurubooru\Entities\Entity;
abstract class AbstractEntityConverter implements IEntityConverter
{
private $entityDecorator = null;
@ -19,8 +21,18 @@ abstract class AbstractEntityConverter implements IEntityConverter
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 toBasicArray(Entity $entity);
protected function dbTimeToEntityTime($time)
{
if ($time === null)

View file

@ -5,11 +5,10 @@ use Szurubooru\Entities\Entity;
class CommentEntityConverter extends AbstractEntityConverter implements IEntityConverter
{
public function toArray(Entity $entity)
public function toBasicArray(Entity $entity)
{
return
[
'id' => $entity->getId(),
'userId' => $entity->getUserId(),
'postId' => $entity->getPostId(),
'text' => $entity->getText(),

View file

@ -5,11 +5,10 @@ use Szurubooru\Entities\Favorite;
class FavoriteEntityConverter extends AbstractEntityConverter implements IEntityConverter
{
public function toArray(Entity $entity)
public function toBasicArray(Entity $entity)
{
return
[
'id' => $entity->getId(),
'userId' => $entity->getUserId(),
'postId' => $entity->getPostId(),
'time' => $this->entityTimeToDbTime($entity->getTime()),

View file

@ -5,11 +5,10 @@ use Szurubooru\Entities\GlobalParam;
class GlobalParamEntityConverter extends AbstractEntityConverter implements IEntityConverter
{
public function toArray(Entity $entity)
public function toBasicArray(Entity $entity)
{
return
[
'id' => $entity->getId(),
'dataKey' => $entity->getKey(),
'dataValue' => $entity->getValue(),
];

View file

@ -5,11 +5,10 @@ use Szurubooru\Entities\Post;
class PostEntityConverter extends AbstractEntityConverter implements IEntityConverter
{
public function toArray(Entity $entity)
public function toBasicArray(Entity $entity)
{
return
[
'id' => $entity->getId(),
'name' => $entity->getName(),
'userId' => $entity->getUserId(),
'uploadTime' => $this->entityTimeToDbTime($entity->getUploadTime()),

View file

@ -5,11 +5,10 @@ use Szurubooru\Entities\PostNote;
class PostNoteEntityConverter extends AbstractEntityConverter implements IEntityConverter
{
public function toArray(Entity $entity)
public function toBasicArray(Entity $entity)
{
return
[
'id' => $entity->getId(),
'postId' => $entity->getPostId(),
'x' => $entity->getLeft(),
'y' => $entity->getTop(),

View file

@ -5,11 +5,10 @@ use Szurubooru\Entities\Score;
class ScoreEntityConverter extends AbstractEntityConverter implements IEntityConverter
{
public function toArray(Entity $entity)
public function toBasicArray(Entity $entity)
{
return
[
'id' => $entity->getId(),
'userId' => $entity->getUserId(),
'postId' => $entity->getPostId(),
'commentId' => $entity->getCommentId(),

View file

@ -5,11 +5,10 @@ use Szurubooru\Entities\Snapshot;
class SnapshotEntityConverter extends AbstractEntityConverter implements IEntityConverter
{
public function toArray(Entity $entity)
public function toBasicArray(Entity $entity)
{
return
[
'id' => $entity->getId(),
'time' => $this->entityTimeToDbTime($entity->getTime()),
'type' => $entity->getType(),
'primaryKey' => $entity->getPrimaryKey(),

View file

@ -5,11 +5,10 @@ use Szurubooru\Entities\Tag;
class TagEntityConverter extends AbstractEntityConverter implements IEntityConverter
{
public function toArray(Entity $entity)
public function toBasicArray(Entity $entity)
{
return
[
'id' => $entity->getId(),
'name' => $entity->getName(),
'creationTime' => $this->entityTimeToDbTime($entity->getCreationTime()),
'banned' => $entity->isBanned(),

View file

@ -5,11 +5,10 @@ use Szurubooru\Entities\Token;
class TokenEntityConverter extends AbstractEntityConverter implements IEntityConverter
{
public function toArray(Entity $entity)
public function toBasicArray(Entity $entity)
{
return
[
'id' => $entity->getId(),
'name' => $entity->getName(),
'purpose' => $entity->getPurpose(),
'additionalData' => $entity->getAdditionalData(),

View file

@ -5,11 +5,10 @@ use Szurubooru\Entities\User;
class UserEntityConverter extends AbstractEntityConverter implements IEntityConverter
{
public function toArray(Entity $entity)
public function toBasicArray(Entity $entity)
{
return
[
'id' => $entity->getId(),
'name' => $entity->getName(),
'email' => $entity->getEmail(),
'emailUnconfirmed' => $entity->getEmailUnconfirmed(),

View file

@ -37,8 +37,12 @@ class DatabaseConnection
$cwd = getcwd();
if ($this->config->getDataDirectory())
chdir($this->config->getDataDirectory());
$this->pdo = new PDOEx($this->config->database->dsn, $this->config->database->user,
$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);
chdir($cwd);
}

View 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));
}
}

View file

@ -46,6 +46,7 @@ return [
$container->get(\Szurubooru\Upgrades\Upgrade27::class),
$container->get(\Szurubooru\Upgrades\Upgrade28::class),
$container->get(\Szurubooru\Upgrades\Upgrade29::class),
$container->get(\Szurubooru\Upgrades\Upgrade30::class),
];
}),

View file

@ -61,6 +61,9 @@ final class PostDaoTest extends AbstractDatabaseTestCase
$post2 = self::getTestPost();
$postDao->save($post1);
$postDao->save($post2);
$this->assertNotNull($post1->getId());
$this->assertNotNull($post2->getId());
$this->assertNotEquals($post1->getId(), $post2->getId());
$actual = $postDao->findAll();

View file

@ -34,6 +34,7 @@ final class TagServiceTest extends AbstractDatabaseTestCase
$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 (2, \'test2\', \'2014-10-01 00:00:00\')');
$pdo->exec('UPDATE sequencer SET lastUsedId = 2 WHERE tableName = \'tags\'');
$tag1 = new Tag();
$tag1->setName('test1');
@ -54,6 +55,7 @@ final class TagServiceTest extends AbstractDatabaseTestCase
$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 (2, \'test2\', \'2014-10-01 00:00:00\')');
$pdo->exec('UPDATE sequencer SET lastUsedId = 2 WHERE tableName = \'tags\'');
$tag1 = new Tag();
$tag1->setName('test1');