diff --git a/.gitmodules b/.gitmodules index 9be025bc..2a7690ae 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "php-markdown"] path = lib/php-markdown url = https://github.com/michelf/php-markdown.git +[submodule "lib/chibi-sql"] + path = lib/chibi-sql + url = https://github.com/rr-/chibi-sql.git diff --git a/lib/chibi-sql b/lib/chibi-sql new file mode 160000 index 00000000..a5d7a039 --- /dev/null +++ b/lib/chibi-sql @@ -0,0 +1 @@ +Subproject commit a5d7a03965e7089c070defa5907798a1df66c847 diff --git a/src/Bootstrap.php b/src/Bootstrap.php index 6b581cfc..d23c86b3 100644 --- a/src/Bootstrap.php +++ b/src/Bootstrap.php @@ -1,4 +1,6 @@ featureNewPost(); + return PropertyModel::featureNewPost(); return $featuredPost; } - - private function featureNewPost() - { - $stmt = (new SqlSelectStatement) - ->setColumn('id') - ->setTable('post') - ->setCriterion((new SqlConjunctionFunctor) - ->add(new SqlEqualsFunctor('type', new SqlBinding(PostType::Image))) - ->add(new SqlEqualsFunctor('safety', new SqlBinding(PostSafety::Safe)))) - ->setOrderBy(new SqlRandomFunctor(), SqlSelectStatement::ORDER_DESC); - $featuredPostId = Database::fetchOne($stmt)['id']; - if (!$featuredPostId) - return null; - - PropertyModel::set(PropertyModel::FeaturedPostId, $featuredPostId); - PropertyModel::set(PropertyModel::FeaturedPostDate, time()); - PropertyModel::set(PropertyModel::FeaturedPostUserName, null); - return PostModel::findById($featuredPostId); - } } diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 9f505bbd..fbe750a5 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -176,7 +176,7 @@ class PostController if (InputHelper::get('submit')) { - Database::transaction(function() + \Chibi\Database::transaction(function() { $post = PostModel::spawn(); LogHelper::bufferChanges(); diff --git a/src/Database.php b/src/Database.php deleted file mode 100644 index 0a4a9cf4..00000000 --- a/src/Database.php +++ /dev/null @@ -1,134 +0,0 @@ -setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - self::$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); - self::$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); - } - catch (Exception $e) - { - self::$pdo = null; - throw $e; - } - } - - protected static function convertStatement(SqlStatement $stmt) - { - try - { - $stmtText = $stmt->getAsString(); - $stmtPdo = self::$pdo->prepare($stmtText); - foreach ($stmt->getBindings() as $key => $value) - if (strpos($stmtText, $key) !== false) - $stmtPdo->bindValue($key, $value); - } - catch (Exception $e) - { - throw new Exception('Problem with ' . $stmt->getAsString() . ' creation (' . $e->getMessage() . ')'); - } - return $stmtPdo; - } - - public static function disconnect() - { - self::$pdo = null; - } - - public static function connected() - { - return self::$pdo !== null; - } - - private static function execInternal(SqlStatement $stmt, $callback) - { - if (!self::connected()) - throw new Exception('Database is not connected'); - - $stmtPdo = self::convertStatement($stmt); - try - { - $timeStart = microtime(true); - $stmtPdo->execute(); - $timeExec = microtime(true) - $timeStart; - - $timeStart = microtime(true); - $ret = $callback($stmtPdo); - $timeFetch = microtime(true) - $timeStart; - } - catch (Exception $e) - { - throw new Exception('Problem with ' . $stmt->getAsString() . ' execution (' . $e->getMessage() . ')'); - } - $queryLog = new StdClass(); - $queryLog->statement = $stmt; - $queryLog->timeExec = $timeExec; - $queryLog->timeFetch = $timeFetch; - self::$queryLogs []= $queryLog; - return $ret; - } - - public static function exec(SqlStatement $stmt) - { - return self::execInternal($stmt, function($stmtPdo) { }); - } - - public static function fetchOne(SqlStatement $stmt) - { - return self::execInternal($stmt, function($stmtPdo) { return $stmtPdo->fetch(); }); - } - - public static function fetchAll(SqlStatement $stmt) - { - return self::execInternal($stmt, function($stmtPdo) { return $stmtPdo->fetchAll(); }); - } - - public static function getLogs() - { - return self::$queryLogs; - } - - public static function inTransaction() - { - return self::$pdo->inTransaction(); - } - - public static function lastInsertId() - { - return self::$pdo->lastInsertId(); - } - - public static function transaction($func) - { - if (self::inTransaction()) - { - return $func(); - } - else - { - try - { - self::$pdo->beginTransaction(); - $ret = $func(); - self::$pdo->commit(); - return $ret; - } - catch (Exception $e) - { - self::$pdo->rollBack(); - throw $e; - } - } - } -} diff --git a/src/Models/AbstractCrudModel.php b/src/Models/AbstractCrudModel.php index 958965f7..1e8d779f 100644 --- a/src/Models/AbstractCrudModel.php +++ b/src/Models/AbstractCrudModel.php @@ -1,4 +1,7 @@ setColumn('*'); $stmt->setTable(static::getTableName()); - $stmt->setCriterion(new SqlEqualsFunctor('id', new SqlBinding($key))); + $stmt->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($key))); $row = Database::fetchOne($stmt); if ($row) @@ -37,10 +40,10 @@ abstract class AbstractCrudModel implements IModel public static function findByIds(array $ids) { - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setColumn('*'); $stmt->setTable(static::getTableName()); - $stmt->setCriterion(SqlInFunctor::fromArray('id', SqlBinding::fromArray(array_unique($ids)))); + $stmt->setCriterion(Sql\InFunctor::fromArray('id', Sql\Binding::fromArray(array_unique($ids)))); $rows = Database::fetchAll($stmt); if ($rows) @@ -51,8 +54,8 @@ abstract class AbstractCrudModel implements IModel public static function getCount() { - $stmt = new SqlSelectStatement(); - $stmt->setColumn(new SqlAliasFunctor(new SqlCountFunctor('1'), 'count')); + $stmt = new Sql\SelectStatement(); + $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count')); $stmt->setTable(static::getTableName()); return Database::fetchOne($stmt)['count']; } @@ -107,7 +110,7 @@ abstract class AbstractCrudModel implements IModel throw new Exception('Can be run only within transaction'); if (!$entity->id) { - $stmt = new SqlInsertStatement(); + $stmt = new Sql\InsertStatement(); $stmt->setTable($table); Database::exec($stmt); $entity->id = Database::lastInsertId(); diff --git a/src/Models/CommentModel.php b/src/Models/CommentModel.php index f1cb8782..3ab5b269 100644 --- a/src/Models/CommentModel.php +++ b/src/Models/CommentModel.php @@ -1,4 +1,7 @@ $comment->commentDate, 'commenter_id' => $comment->commenterId]; - $stmt = new SqlUpdateStatement(); + $stmt = new Sql\UpdateStatement(); $stmt->setTable('comment'); - $stmt->setCriterion(new SqlEqualsFunctor('id', new SqlBinding($comment->id))); + $stmt->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($comment->id))); foreach ($bindings as $key => $val) - $stmt->setColumn($key, new SqlBinding($val)); + $stmt->setColumn($key, new Sql\Binding($val)); Database::exec($stmt); }); @@ -40,9 +43,9 @@ class CommentModel extends AbstractCrudModel { Database::transaction(function() use ($comment) { - $stmt = new SqlDeleteStatement(); + $stmt = new Sql\DeleteStatement(); $stmt->setTable('comment'); - $stmt->setCriterion(new SqlEqualsFunctor('id', new SqlBinding($comment->id))); + $stmt->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($comment->id))); Database::exec($stmt); }); } @@ -51,10 +54,10 @@ class CommentModel extends AbstractCrudModel public static function findAllByPostId($key) { - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setColumn('comment.*'); $stmt->setTable('comment'); - $stmt->setCriterion(new SqlEqualsFunctor('post_id', new SqlBinding($key))); + $stmt->setCriterion(new Sql\EqualsFunctor('post_id', new Sql\Binding($key))); $rows = Database::fetchAll($stmt); if ($rows) diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index a85d37a8..b0d1d8c9 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -1,4 +1,7 @@ hasCache('favoritee')) return $this->getCache('favoritee'); - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setColumn('user.*'); $stmt->setTable('user'); - $stmt->addInnerJoin('favoritee', new SqlEqualsFunctor('favoritee.user_id', 'user.id')); - $stmt->setCriterion(new SqlEqualsFunctor('favoritee.post_id', new SqlBinding($this->id))); + $stmt->addInnerJoin('favoritee', new Sql\EqualsFunctor('favoritee.user_id', 'user.id')); + $stmt->setCriterion(new Sql\EqualsFunctor('favoritee.post_id', new Sql\Binding($this->id))); $rows = Database::fetchAll($stmt); $favorites = UserModel::convertRows($rows); $this->setCache('favoritee', $favorites); @@ -69,19 +72,19 @@ class PostEntity extends AbstractEntity if ($this->hasCache('relations')) return $this->getCache('relations'); - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setColumn('post.*'); $stmt->setTable('post'); - $binding = new SqlBinding($this->id); - $stmt->addInnerJoin('crossref', (new SqlDisjunctionFunctor) + $binding = new Sql\Binding($this->id); + $stmt->addInnerJoin('crossref', (new Sql\DisjunctionFunctor) ->add( - (new SqlConjunctionFunctor) - ->add(new SqlEqualsFunctor('post.id', 'crossref.post2_id')) - ->add(new SqlEqualsFunctor('crossref.post_id', $binding))) + (new Sql\ConjunctionFunctor) + ->add(new Sql\EqualsFunctor('post.id', 'crossref.post2_id')) + ->add(new Sql\EqualsFunctor('crossref.post_id', $binding))) ->add( - (new SqlConjunctionFunctor) - ->add(new SqlEqualsFunctor('post.id', 'crossref.post_id')) - ->add(new SqlEqualsFunctor('crossref.post2_id', $binding)))); + (new Sql\ConjunctionFunctor) + ->add(new Sql\EqualsFunctor('post.id', 'crossref.post_id')) + ->add(new Sql\EqualsFunctor('crossref.post2_id', $binding)))); $rows = Database::fetchAll($stmt); $posts = PostModel::convertRows($rows); $this->setCache('relations', $posts); diff --git a/src/Models/Entities/TagEntity.php b/src/Models/Entities/TagEntity.php index e1f21492..6274c81e 100644 --- a/src/Models/Entities/TagEntity.php +++ b/src/Models/Entities/TagEntity.php @@ -1,14 +1,17 @@ setColumn(new SqlAliasFunctor(new SqlCountFunctor('1'), 'count')); + $stmt = new Sql\SelectStatement(); + $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count')); $stmt->setTable('post_tag'); - $stmt->setCriterion(new SqlEqualsFunctor('tag_id', new SqlBinding($this->id))); + $stmt->setCriterion(new Sql\EqualsFunctor('tag_id', new Sql\Binding($this->id))); return Database::fetchOne($stmt)['count']; } } diff --git a/src/Models/Entities/UserEntity.php b/src/Models/Entities/UserEntity.php index b79b8f5e..b23998c0 100644 --- a/src/Models/Entities/UserEntity.php +++ b/src/Models/Entities/UserEntity.php @@ -1,4 +1,7 @@ setColumn(new SqlAliasFunctor(new SqlCountFunctor('1'), 'count')); + $stmt = new Sql\SelectStatement(); + $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count')); $stmt->setTable('favoritee'); - $stmt->setCriterion((new SqlConjunctionFunctor) - ->add(new SqlEqualsFunctor('user_id', new SqlBinding($this->id))) - ->add(new SqlEqualsFunctor('post_id', new SqlBinding($post->id)))); + $stmt->setCriterion((new Sql\ConjunctionFunctor) + ->add(new Sql\EqualsFunctor('user_id', new Sql\Binding($this->id))) + ->add(new Sql\EqualsFunctor('post_id', new Sql\Binding($post->id)))); return Database::fetchOne($stmt)['count'] == 1; } public function getScore($post) { - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setColumn('score'); $stmt->setTable('post_score'); - $stmt->setCriterion((new SqlConjunctionFunctor) - ->add(new SqlEqualsFunctor('user_id', new SqlBinding($this->id))) - ->add(new SqlEqualsFunctor('post_id', new SqlBinding($post->id)))); + $stmt->setCriterion((new Sql\ConjunctionFunctor) + ->add(new Sql\EqualsFunctor('user_id', new Sql\Binding($this->id))) + ->add(new Sql\EqualsFunctor('post_id', new Sql\Binding($post->id)))); $row = Database::fetchOne($stmt); if ($row) return intval($row['score']); @@ -136,28 +139,28 @@ class UserEntity extends AbstractEntity public function getFavoriteCount() { - $stmt = new SqlSelectStatement(); - $stmt->setColumn(new SqlAliasFunctor(new SqlCountFunctor('1'), 'count')); + $stmt = new Sql\SelectStatement(); + $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count')); $stmt->setTable('favoritee'); - $stmt->setCriterion(new SqlEqualsFunctor('user_id', new SqlBinding($this->id))); + $stmt->setCriterion(new Sql\EqualsFunctor('user_id', new Sql\Binding($this->id))); return Database::fetchOne($stmt)['count']; } public function getCommentCount() { - $stmt = new SqlSelectStatement(); - $stmt->setColumn(new SqlAliasFunctor(new SqlCountFunctor('1'), 'count')); + $stmt = new Sql\SelectStatement(); + $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count')); $stmt->setTable('comment'); - $stmt->setCriterion(new SqlEqualsFunctor('commenter_id', new SqlBinding($this->id))); + $stmt->setCriterion(new Sql\EqualsFunctor('commenter_id', new Sql\Binding($this->id))); return Database::fetchOne($stmt)['count']; } public function getPostCount() { - $stmt = new SqlSelectStatement(); - $stmt->setColumn(new SqlAliasFunctor(new SqlCountFunctor('1'), 'count')); + $stmt = new Sql\SelectStatement(); + $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count')); $stmt->setTable('post'); - $stmt->setCriterion(new SqlEqualsFunctor('uploader_id', new SqlBinding($this->id))); + $stmt->setCriterion(new Sql\EqualsFunctor('uploader_id', new Sql\Binding($this->id))); return Database::fetchOne($stmt)['count']; } } diff --git a/src/Models/PostModel.php b/src/Models/PostModel.php index 71261ddf..0717474b 100644 --- a/src/Models/PostModel.php +++ b/src/Models/PostModel.php @@ -1,4 +1,7 @@ $post->source, ]; - $stmt = new SqlUpdateStatement(); + $stmt = new Sql\UpdateStatement(); $stmt->setTable('post'); foreach ($bindings as $key => $value) - $stmt->setColumn($key, new SqlBinding($value)); + $stmt->setColumn($key, new Sql\Binding($value)); - $stmt->setCriterion(new SqlEqualsFunctor('id', new SqlBinding($post->id))); + $stmt->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($post->id))); Database::exec($stmt); //tags $tags = $post->getTags(); - $stmt = new SqlDeleteStatement(); + $stmt = new Sql\DeleteStatement(); $stmt->setTable('post_tag'); - $stmt->setCriterion(new SqlEqualsFunctor('post_id', new SqlBinding($post->id))); + $stmt->setCriterion(new Sql\EqualsFunctor('post_id', new Sql\Binding($post->id))); Database::exec($stmt); foreach ($tags as $postTag) { - $stmt = new SqlInsertStatement(); + $stmt = new Sql\InsertStatement(); $stmt->setTable('post_tag'); - $stmt->setColumn('post_id', new SqlBinding($post->id)); - $stmt->setColumn('tag_id', new SqlBinding($postTag->id)); + $stmt->setColumn('post_id', new Sql\Binding($post->id)); + $stmt->setColumn('tag_id', new Sql\Binding($postTag->id)); Database::exec($stmt); } //relations $relations = $post->getRelations(); - $stmt = new SqlDeleteStatement(); + $stmt = new Sql\DeleteStatement(); $stmt->setTable('crossref'); - $binding = new SqlBinding($post->id); - $stmt->setCriterion((new SqlDisjunctionFunctor) - ->add(new SqlEqualsFunctor('post_id', $binding)) - ->add(new SqlEqualsFunctor('post2_id', $binding))); + $binding = new Sql\Binding($post->id); + $stmt->setCriterion((new Sql\DisjunctionFunctor) + ->add(new Sql\EqualsFunctor('post_id', $binding)) + ->add(new Sql\EqualsFunctor('post2_id', $binding))); Database::exec($stmt); foreach ($relations as $relatedPost) { - $stmt = new SqlInsertStatement(); + $stmt = new Sql\InsertStatement(); $stmt->setTable('crossref'); - $stmt->setColumn('post_id', new SqlBinding($post->id)); - $stmt->setColumn('post2_id', new SqlBinding($relatedPost->id)); + $stmt->setColumn('post_id', new Sql\Binding($post->id)); + $stmt->setColumn('post2_id', new Sql\Binding($relatedPost->id)); Database::exec($stmt); } }); @@ -100,11 +103,11 @@ class PostModel extends AbstractCrudModel { Database::transaction(function() use ($post) { - $binding = new SqlBinding($post->id); + $binding = new Sql\Binding($post->id); - $stmt = new SqlDeleteStatement(); + $stmt = new Sql\DeleteStatement(); $stmt->setTable('post_score'); - $stmt->setCriterion(new SqlEqualsFunctor('post_id', $binding)); + $stmt->setCriterion(new Sql\EqualsFunctor('post_id', $binding)); Database::exec($stmt); $stmt->setTable('post_tag'); @@ -117,13 +120,13 @@ class PostModel extends AbstractCrudModel Database::exec($stmt); $stmt->setTable('crossref'); - $stmt->setCriterion((new SqlDisjunctionFunctor) - ->add(new SqlEqualsFunctor('post_id', $binding)) - ->add(new SqlEqualsFunctor('post_id', $binding))); + $stmt->setCriterion((new Sql\DisjunctionFunctor) + ->add(new Sql\EqualsFunctor('post_id', $binding)) + ->add(new Sql\EqualsFunctor('post_id', $binding))); Database::exec($stmt); $stmt->setTable('post'); - $stmt->setCriterion(new SqlEqualsFunctor('id', $binding)); + $stmt->setCriterion(new Sql\EqualsFunctor('id', $binding)); Database::exec($stmt); }); } @@ -133,10 +136,10 @@ class PostModel extends AbstractCrudModel public static function findByName($key, $throw = true) { - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setColumn('*'); $stmt->setTable('post'); - $stmt->setCriterion(new SqlEqualsFunctor('name', new SqlBinding($key))); + $stmt->setCriterion(new Sql\EqualsFunctor('name', new Sql\Binding($key))); $row = Database::fetchOne($stmt); if ($row) @@ -158,10 +161,10 @@ class PostModel extends AbstractCrudModel public static function findByHash($key, $throw = true) { - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setColumn('*'); $stmt->setTable('post'); - $stmt->setCriterion(new SqlEqualsFunctor('file_hash', new SqlBinding($key))); + $stmt->setCriterion(new Sql\EqualsFunctor('file_hash', new Sql\Binding($key))); $row = Database::fetchOne($stmt); if ($row) @@ -189,11 +192,11 @@ class PostModel extends AbstractCrudModel } $postIds = array_unique(array_keys($postMap)); - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setTable('comment'); $stmt->addColumn('comment.*'); $stmt->addColumn('post_id'); - $stmt->setCriterion(SqlInFunctor::fromArray('post_id', SqlBinding::fromArray($postIds))); + $stmt->setCriterion(Sql\InFunctor::fromArray('post_id', Sql\Binding::fromArray($postIds))); $rows = Database::fetchAll($stmt); foreach ($rows as $row) @@ -230,12 +233,12 @@ class PostModel extends AbstractCrudModel } $postIds = array_unique(array_keys($postMap)); - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setTable('tag'); $stmt->addColumn('tag.*'); $stmt->addColumn('post_id'); - $stmt->addInnerJoin('post_tag', new SqlEqualsFunctor('post_tag.tag_id', 'tag.id')); - $stmt->setCriterion(SqlInFunctor::fromArray('post_id', SqlBinding::fromArray($postIds))); + $stmt->addInnerJoin('post_tag', new Sql\EqualsFunctor('post_tag.tag_id', 'tag.id')); + $stmt->setCriterion(Sql\InFunctor::fromArray('post_id', Sql\Binding::fromArray($postIds))); $rows = Database::fetchAll($stmt); foreach ($rows as $row) diff --git a/src/Models/PropertyModel.php b/src/Models/PropertyModel.php index 5a3978c6..9c6319c5 100644 --- a/src/Models/PropertyModel.php +++ b/src/Models/PropertyModel.php @@ -1,4 +1,7 @@ setColumn('*'); $stmt ->setTable('property'); foreach (Database::fetchAll($stmt) as $row) @@ -41,28 +44,47 @@ class PropertyModel implements IModel self::loadIfNecessary(); Database::transaction(function() use ($propertyId, $value) { - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setColumn('id'); $stmt->setTable('property'); - $stmt->setCriterion(new SqlEqualsFunctor('prop_id', new SqlBinding($propertyId))); + $stmt->setCriterion(new Sql\EqualsFunctor('prop_id', new Sql\Binding($propertyId))); $row = Database::fetchOne($stmt); if ($row) { - $stmt = new SqlUpdateStatement(); - $stmt->setCriterion(new SqlEqualsFunctor('prop_id', new SqlBinding($propertyId))); + $stmt = new Sql\UpdateStatement(); + $stmt->setCriterion(new Sql\EqualsFunctor('prop_id', new Sql\Binding($propertyId))); } else { - $stmt = new SqlInsertStatement(); - $stmt->setColumn('prop_id', new SqlBinding($propertyId)); + $stmt = new Sql\InsertStatement(); + $stmt->setColumn('prop_id', new Sql\Binding($propertyId)); } $stmt->setTable('property'); - $stmt->setColumn('value', new SqlBinding($value)); + $stmt->setColumn('value', new Sql\Binding($value)); Database::exec($stmt); self::$allProperties[$propertyId] = $value; }); } + + public static function featureNewPost() + { + $stmt = (new Sql\SelectStatement) + ->setColumn('id') + ->setTable('post') + ->setCriterion((new Sql\ConjunctionFunctor) + ->add(new Sql\EqualsFunctor('type', new Sql\Binding(PostType::Image))) + ->add(new Sql\EqualsFunctor('safety', new Sql\Binding(PostSafety::Safe)))) + ->setOrderBy(new Sql\RandomFunctor(), Sql\SelectStatement::ORDER_DESC); + $featuredPostId = Database::fetchOne($stmt)['id']; + if (!$featuredPostId) + return null; + + self::set(self::FeaturedPostId, $featuredPostId); + self::set(self::FeaturedPostDate, time()); + self::set(self::FeaturedPostUserName, null); + return PostModel::findById($featuredPostId); + } } diff --git a/src/Models/SearchParsers/AbstractSearchParser.php b/src/Models/SearchParsers/AbstractSearchParser.php index 342872c8..73d79ee5 100644 --- a/src/Models/SearchParsers/AbstractSearchParser.php +++ b/src/Models/SearchParsers/AbstractSearchParser.php @@ -1,9 +1,11 @@ statement = $statement; @@ -65,17 +67,17 @@ abstract class AbstractSearchParser $orderByString = strtolower(array_shift($arr)); $orderDirString = strtolower(array_shift($arr)); if ($orderDirString == 'asc') - $orderDir = SqlSelectStatement::ORDER_ASC; + $orderDir = Sql\SelectStatement::ORDER_ASC; elseif ($orderDirString == 'desc') - $orderDir = SqlSelectStatement::ORDER_DESC; + $orderDir = Sql\SelectStatement::ORDER_DESC; else throw new SimpleException('Invalid search order direction: ' . $searchOrderDir); if ($neg) { - $orderDir = $orderDir == SqlSelectStatement::ORDER_ASC - ? SqlSelectStatement::ORDER_DESC - : SqlSelectStatement::ORDER_ASC; + $orderDir = $orderDir == Sql\SelectStatement::ORDER_ASC + ? Sql\SelectStatement::ORDER_DESC + : Sql\SelectStatement::ORDER_ASC; } if (!$this->processOrderToken($orderByString, $orderDir)) diff --git a/src/Models/SearchParsers/CommentSearchParser.php b/src/Models/SearchParsers/CommentSearchParser.php index c987a3a4..7350a734 100644 --- a/src/Models/SearchParsers/CommentSearchParser.php +++ b/src/Models/SearchParsers/CommentSearchParser.php @@ -1,16 +1,18 @@ statement->addInnerJoin('post', new SqlEqualsFunctor('post_id', 'post.id')); + $this->statement->addInnerJoin('post', new Sql\EqualsFunctor('post_id', 'post.id')); $allowedSafety = PrivilegesHelper::getAllowedSafety(); - $this->statement->setCriterion(new SqlConjunctionFunctor()); - $this->statement->getCriterion()->add(SqlInFunctor::fromArray('post.safety', SqlBinding::fromArray($allowedSafety))); + $this->statement->setCriterion(new Sql\ConjunctionFunctor()); + $this->statement->getCriterion()->add(Sql\InFunctor::fromArray('post.safety', Sql\Binding::fromArray($allowedSafety))); if (!PrivilegesHelper::confirm(Privilege::ListPosts, 'hidden')) - $this->statement->getCriterion()->add(new SqlNegationFunctor(new SqlStringExpression('hidden'))); + $this->statement->getCriterion()->add(new Sql\NegationFunctor(new Sql\StringExpression('hidden'))); - $this->statement->addOrderBy('comment.id', SqlSelectStatement::ORDER_DESC); + $this->statement->addOrderBy('comment.id', Sql\SelectStatement::ORDER_DESC); } } diff --git a/src/Models/SearchParsers/PostSearchParser.php b/src/Models/SearchParsers/PostSearchParser.php index 9586f92e..cf8960b0 100644 --- a/src/Models/SearchParsers/PostSearchParser.php +++ b/src/Models/SearchParsers/PostSearchParser.php @@ -1,4 +1,6 @@ tags = []; - $this->statement->setCriterion(new SqlConjunctionFunctor()); + $this->statement->setCriterion(new Sql\ConjunctionFunctor()); $allowedSafety = PrivilegesHelper::getAllowedSafety(); - $this->statement->getCriterion()->add(SqlInFunctor::fromArray('safety', SqlBinding::fromArray($allowedSafety))); + $this->statement->getCriterion()->add(Sql\InFunctor::fromArray('safety', Sql\Binding::fromArray($allowedSafety))); if (\Chibi\Registry::getContext()->user->hasEnabledHidingDislikedPosts() and !in_array('special:disliked', array_map('strtolower', $tokens))) $this->processComplexToken('special', 'disliked', true); @@ -29,20 +31,20 @@ class PostSearchParser extends AbstractSearchParser { list ($tagName, $neg) = $item; $tag = TagModel::findByName($tagName); - $innerStmt = new SqlSelectStatement(); + $innerStmt = new Sql\SelectStatement(); $innerStmt->setTable('post_tag'); - $innerStmt->setCriterion((new SqlConjunctionFunctor) - ->add(new SqlEqualsFunctor('post_tag.post_id', 'post.id')) - ->add(new SqlEqualsFunctor('post_tag.tag_id', new SqlBinding($tag->id)))); - $operator = new SqlExistsFunctor($innerStmt); + $innerStmt->setCriterion((new Sql\ConjunctionFunctor) + ->add(new Sql\EqualsFunctor('post_tag.post_id', 'post.id')) + ->add(new Sql\EqualsFunctor('post_tag.tag_id', new Sql\Binding($tag->id)))); + $operator = new Sql\ExistsFunctor($innerStmt); if ($neg) - $operator = new SqlNegationFunctor($operator); + $operator = new Sql\NegationFunctor($operator); $this->statement->getCriterion()->add($operator); } $this->statement->addOrderBy('post.id', empty($this->statement->getOrderBy()) - ? SqlSelectStatement::ORDER_DESC + ? Sql\SelectStatement::ORDER_DESC : $this->statement->getOrderBy()[0][1]); } @@ -58,85 +60,85 @@ class PostSearchParser extends AbstractSearchParser { $ids = preg_split('/[;,]/', $value); $ids = array_map('intval', $ids); - return SqlInFunctor::fromArray('post.id', SqlBinding::fromArray($ids)); + return Sql\InFunctor::fromArray('post.id', Sql\Binding::fromArray($ids)); } elseif (in_array($key, ['fav', 'favs'])) { $user = UserModel::findByNameOrEmail($value); - $innerStmt = (new SqlSelectStatement) + $innerStmt = (new Sql\SelectStatement) ->setTable('favoritee') - ->setCriterion((new SqlConjunctionFunctor) - ->add(new SqlEqualsFunctor('favoritee.post_id', 'post.id')) - ->add(new SqlEqualsFunctor('favoritee.user_id', new SqlBinding($user->id)))); - return new SqlExistsFunctor($innerStmt); + ->setCriterion((new Sql\ConjunctionFunctor) + ->add(new Sql\EqualsFunctor('favoritee.post_id', 'post.id')) + ->add(new Sql\EqualsFunctor('favoritee.user_id', new Sql\Binding($user->id)))); + return new Sql\ExistsFunctor($innerStmt); } elseif (in_array($key, ['comment', 'commenter'])) { $user = UserModel::findByNameOrEmail($value); - $innerStmt = (new SqlSelectStatement) + $innerStmt = (new Sql\SelectStatement) ->setTable('comment') - ->setCriterion((new SqlConjunctionFunctor) - ->add(new SqlEqualsFunctor('comment.post_id', 'post.id')) - ->add(new SqlEqualsFunctor('comment.commenter_id', new SqlBinding($user->id)))); - return new SqlExistsFunctor($innerStmt); + ->setCriterion((new Sql\ConjunctionFunctor) + ->add(new Sql\EqualsFunctor('comment.post_id', 'post.id')) + ->add(new Sql\EqualsFunctor('comment.commenter_id', new Sql\Binding($user->id)))); + return new Sql\ExistsFunctor($innerStmt); } elseif (in_array($key, ['submit', 'upload', 'uploader', 'uploaded'])) { $user = UserModel::findByNameOrEmail($value); - return new SqlEqualsFunctor('uploader_id', new SqlBinding($user->id)); + return new Sql\EqualsFunctor('uploader_id', new Sql\Binding($user->id)); } elseif (in_array($key, ['idmin', 'id_min'])) - return new SqlEqualsOrGreaterFunctor('post.id', new SqlBinding(intval($value))); + return new Sql\EqualsOrGreaterFunctor('post.id', new Sql\Binding(intval($value))); elseif (in_array($key, ['idmax', 'id_max'])) - return new SqlEqualsOrLesserFunctor('post.id', new SqlBinding(intval($value))); + return new Sql\EqualsOrLesserFunctor('post.id', new Sql\Binding(intval($value))); elseif (in_array($key, ['scoremin', 'score_min'])) - return new SqlEqualsOrGreaterFunctor('score', new SqlBinding(intval($value))); + return new Sql\EqualsOrGreaterFunctor('score', new Sql\Binding(intval($value))); elseif (in_array($key, ['scoremax', 'score_max'])) - return new SqlEqualsOrLesserFunctor('score', new SqlBinding(intval($value))); + return new Sql\EqualsOrLesserFunctor('score', new Sql\Binding(intval($value))); elseif (in_array($key, ['tagmin', 'tag_min'])) - return new SqlEqualsOrGreaterFunctor('tag_count', new SqlBinding(intval($value))); + return new Sql\EqualsOrGreaterFunctor('tag_count', new Sql\Binding(intval($value))); elseif (in_array($key, ['tagmax', 'tag_max'])) - return new SqlEqualsOrLesserFunctor('tag_count', new SqlBinding(intval($value))); + return new Sql\EqualsOrLesserFunctor('tag_count', new Sql\Binding(intval($value))); elseif (in_array($key, ['favmin', 'fav_min'])) - return new SqlEqualsOrGreaterFunctor('fav_count', new SqlBinding(intval($value))); + return new Sql\EqualsOrGreaterFunctor('fav_count', new Sql\Binding(intval($value))); elseif (in_array($key, ['favmax', 'fav_max'])) - return new SqlEqualsOrLesserFunctor('fav_count', new SqlBinding(intval($value))); + return new Sql\EqualsOrLesserFunctor('fav_count', new Sql\Binding(intval($value))); elseif (in_array($key, ['commentmin', 'comment_min'])) - return new SqlEqualsOrGreaterFunctor('comment_count', new SqlBinding(intval($value))); + return new Sql\EqualsOrGreaterFunctor('comment_count', new Sql\Binding(intval($value))); elseif (in_array($key, ['commentmax', 'comment_max'])) - return new SqlEqualsOrLesserFunctor('comment_count', new SqlBinding(intval($value))); + return new Sql\EqualsOrLesserFunctor('comment_count', new Sql\Binding(intval($value))); elseif (in_array($key, ['date'])) { list ($dateMin, $dateMax) = self::parseDate($value); - return (new SqlConjunctionFunctor) - ->add(new SqlEqualsOrLesserFunctor('upload_date', new SqlBinding($dateMax))) - ->add(new SqlEqualsOrGreaterFunctor('upload_date', new SqlBinding($dateMin))); + return (new Sql\ConjunctionFunctor) + ->add(new Sql\EqualsOrLesserFunctor('upload_date', new Sql\Binding($dateMax))) + ->add(new Sql\EqualsOrGreaterFunctor('upload_date', new Sql\Binding($dateMin))); } elseif (in_array($key, ['datemin', 'date_min'])) { list ($dateMin, $dateMax) = self::parseDate($value); - return new SqlEqualsOrGreaterFunctor('upload_date', new SqlBinding($dateMin)); + return new Sql\EqualsOrGreaterFunctor('upload_date', new Sql\Binding($dateMin)); } elseif (in_array($key, ['datemax', 'date_max'])) { list ($dateMin, $dateMax) = self::parseDate($value); - return new SqlEqualsOrLesserFunctor('upload_date', new SqlBinding($dateMax)); + return new Sql\EqualsOrLesserFunctor('upload_date', new Sql\Binding($dateMax)); } elseif ($key == 'special') @@ -147,26 +149,26 @@ class PostSearchParser extends AbstractSearchParser { if (!$this->statement->isTableJoined('post_score')) { - $this->statement->addLeftOuterJoin('post_score', (new SqlConjunctionFunctor) - ->add(new SqlEqualsFunctor('post_score.post_id', 'post.id')) - ->add(new SqlEqualsFunctor('post_score.user_id', new SqlBinding($context->user->id)))); + $this->statement->addLeftOuterJoin('post_score', (new Sql\ConjunctionFunctor) + ->add(new Sql\EqualsFunctor('post_score.post_id', 'post.id')) + ->add(new Sql\EqualsFunctor('post_score.user_id', new Sql\Binding($context->user->id)))); } - return new SqlEqualsFunctor(new SqlIfNullFunctor('post_score.score', '0'), '1'); + return new Sql\EqualsFunctor(new Sql\IfNullFunctor('post_score.score', '0'), '1'); } elseif (in_array($value, ['disliked', 'dislikes'])) { if (!$this->statement->isTableJoined('post_score')) { - $this->statement->addLeftOuterJoin('post_score', (new SqlConjunctionFunctor) - ->add(new SqlEqualsFunctor('post_score.post_id', 'post.id')) - ->add(new SqlEqualsFunctor('post_score.user_id', new SqlBinding($context->user->id)))); + $this->statement->addLeftOuterJoin('post_score', (new Sql\ConjunctionFunctor) + ->add(new Sql\EqualsFunctor('post_score.post_id', 'post.id')) + ->add(new Sql\EqualsFunctor('post_score.user_id', new Sql\Binding($context->user->id)))); } - return new SqlEqualsFunctor(new SqlIfNullFunctor('post_score.score', '0'), '-1'); + return new Sql\EqualsFunctor(new Sql\IfNullFunctor('post_score.score', '0'), '-1'); } elseif ($value == 'hidden') - return new SqlStringExpression('hidden'); + return new Sql\StringExpression('hidden'); else throw new SimpleException('Invalid special token: ' . $value); @@ -184,7 +186,7 @@ class PostSearchParser extends AbstractSearchParser else throw new SimpleException('Invalid post type: ' . $value); - return new SqlEqualsFunctor('type', new SqlBinding($type)); + return new Sql\EqualsFunctor('type', new Sql\Binding($type)); } return null; @@ -197,7 +199,7 @@ class PostSearchParser extends AbstractSearchParser return false; if ($neg) - $criterion = new SqlNegationFunctor($criterion); + $criterion = new Sql\NegationFunctor($criterion); $this->statement->getCriterion()->add($criterion); return true; @@ -239,9 +241,9 @@ class PostSearchParser extends AbstractSearchParser if (!isset($_SESSION['browsing-seed'])) $_SESSION['browsing-seed'] = mt_rand(); $seed = $_SESSION['browsing-seed']; - $orderColumn = new SqlSubstrFunctor( - new SqlMultiplicationFunctor('post.id', $seed), - new SqlAdditionFunctor(new SqlLengthFunctor('post.id'), '2')); + $orderColumn = new Sql\SubstrFunctor( + new Sql\MultiplicationFunctor('post.id', $seed), + new Sql\AdditionFunctor(new Sql\LengthFunctor('post.id'), '2')); } else diff --git a/src/Models/SearchParsers/TagSearchParser.php b/src/Models/SearchParsers/TagSearchParser.php index 4998a821..ff6cd484 100644 --- a/src/Models/SearchParsers/TagSearchParser.php +++ b/src/Models/SearchParsers/TagSearchParser.php @@ -1,13 +1,15 @@ statement - ->addInnerJoin('post_tag', new SqlEqualsFunctor('tag.id', 'post_tag.tag_id')) - ->addInnerJoin('post', new SqlEqualsFunctor('post.id', 'post_tag.post_id')) - ->setCriterion((new SqlConjunctionFunctor)->add(SqlInFunctor::fromArray('safety', SqlBinding::fromArray($allowedSafety)))) + ->addInnerJoin('post_tag', new Sql\EqualsFunctor('tag.id', 'post_tag.tag_id')) + ->addInnerJoin('post', new Sql\EqualsFunctor('post.id', 'post_tag.post_id')) + ->setCriterion((new Sql\ConjunctionFunctor)->add(Sql\InFunctor::fromArray('safety', Sql\Binding::fromArray($allowedSafety)))) ->setGroupBy('tag.id'); } @@ -20,7 +22,7 @@ class TagSearchParser extends AbstractSearchParser $value = '%' . $value; $value .= '%'; - $this->statement->getCriterion()->add(new SqlNoCaseFunctor(new SqlLikeFunctor('tag.name', new SqlBinding($value)))); + $this->statement->getCriterion()->add(new Sql\NoCaseFunctor(new Sql\LikeFunctor('tag.name', new Sql\Binding($value)))); return true; } diff --git a/src/Models/SearchParsers/UserSearchParser.php b/src/Models/SearchParsers/UserSearchParser.php index 73cae6dd..1b187dfc 100644 --- a/src/Models/SearchParsers/UserSearchParser.php +++ b/src/Models/SearchParsers/UserSearchParser.php @@ -1,4 +1,6 @@ statement->setCriterion((new SqlDisjunctionFunctor) - ->add(new SqlIsFunctor('staff_confirmed', new SqlNullFunctor())) - ->add(new SqlEqualsFunctor('staff_confirmed', '0'))); + $this->statement->setCriterion((new Sql\DisjunctionFunctor) + ->add(new Sql\IsFunctor('staff_confirmed', new Sql\NullFunctor())) + ->add(new Sql\EqualsFunctor('staff_confirmed', '0'))); return true; } return false; @@ -19,7 +21,7 @@ class UserSearchParser extends AbstractSearchParser protected function processOrderToken($orderByString, $orderDir) { if ($orderByString == 'alpha') - $this->statement->setOrderBy(new SqlNoCaseFunctor('name'), $orderDir); + $this->statement->setOrderBy(new Sql\NoCaseFunctor('name'), $orderDir); elseif ($orderByString == 'date') $this->statement->setOrderBy('join_date', $orderDir); else diff --git a/src/Models/SearchServices/AbstractSearchService.php b/src/Models/SearchServices/AbstractSearchService.php index 303af09a..ebd34de4 100644 --- a/src/Models/SearchServices/AbstractSearchService.php +++ b/src/Models/SearchServices/AbstractSearchService.php @@ -1,4 +1,7 @@ decorate($stmt, $searchQuery); } - protected static function decorateCustom(SqlSelectStatement $stmt) + protected static function decorateCustom(Sql\SelectStatement $stmt) { } - protected static function decoratePager(SqlSelectStatement $stmt, $perPage, $page) + protected static function decoratePager(Sql\SelectStatement $stmt, $perPage, $page) { if ($perPage === null) return; $stmt->setLimit( - new SqlBinding($perPage), - new SqlBinding(($page - 1) * $perPage)); + new Sql\Binding($perPage), + new Sql\Binding(($page - 1) * $perPage)); } public static function getEntitiesRows($searchQuery, $perPage = null, $page = 1) @@ -39,7 +42,7 @@ abstract class AbstractSearchService $modelClassName = self::getModelClassName(); $table = $modelClassName::getTableName(); - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setColumn($table . '.*'); $stmt->setTable($table); static::decorateParser($stmt, $searchQuery); @@ -61,14 +64,14 @@ abstract class AbstractSearchService $modelClassName = self::getModelClassName(); $table = $modelClassName::getTableName(); - $innerStmt = new SqlSelectStatement(); + $innerStmt = new Sql\SelectStatement(); $innerStmt->setTable($table); static::decorateParser($innerStmt, $searchQuery); static::decorateCustom($innerStmt); $innerStmt->resetOrderBy(); - $stmt = new SqlSelectStatement(); - $stmt->setColumn(new SqlAliasFunctor(new SqlCountFunctor('1'), 'count')); + $stmt = new Sql\SelectStatement(); + $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count')); $stmt->setSource($innerStmt); return Database::fetchOne($stmt)['count']; diff --git a/src/Models/SearchServices/PostSearchService.php b/src/Models/SearchServices/PostSearchService.php index 39b5787b..638d753f 100644 --- a/src/Models/SearchServices/PostSearchService.php +++ b/src/Models/SearchServices/PostSearchService.php @@ -1,30 +1,33 @@ setTable('post_search'); Database::exec($stmt); - $innerStmt = new SqlSelectStatement($searchQuery); + $innerStmt = new Sql\SelectStatement($searchQuery); $innerStmt->setColumn('post.id'); $innerStmt->setTable('post'); self::decorateParser($innerStmt, $searchQuery); - $stmt = new SqlInsertStatement(); + $stmt = new Sql\InsertStatement(); $stmt->setTable('post_search'); $stmt->setSource(['post_id'], $innerStmt); Database::exec($stmt); - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setTable('post_search'); $stmt->setColumn('id'); - $stmt->setCriterion(new SqlEqualsFunctor('post_id', new SqlBinding($postId))); + $stmt->setCriterion(new Sql\EqualsFunctor('post_id', new Sql\Binding($postId))); $rowId = Database::fetchOne($stmt)['id']; //it's possible that given post won't show in search results: @@ -35,10 +38,10 @@ class PostSearchService extends AbstractSearchService $rowId = intval($rowId); $stmt->setColumn('post_id'); - $stmt->setCriterion(new SqlEqualsFunctor('id', new SqlBinding($rowId - 1))); + $stmt->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($rowId - 1))); $nextPostId = Database::fetchOne($stmt)['post_id']; - $stmt->setCriterion(new SqlEqualsFunctor('id', new SqlBinding($rowId + 1))); + $stmt->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($rowId + 1))); $prevPostId = Database::fetchOne($stmt)['post_id']; return [$prevPostId, $nextPostId]; diff --git a/src/Models/SearchServices/TagSearchService.php b/src/Models/SearchServices/TagSearchService.php index 77ed578d..8f2f6a7d 100644 --- a/src/Models/SearchServices/TagSearchService.php +++ b/src/Models/SearchServices/TagSearchService.php @@ -1,19 +1,22 @@ addColumn(new SqlAliasFunctor(new SqlCountFunctor('post_tag.post_id'), 'post_count')); + $stmt->addColumn(new Sql\AliasFunctor(new Sql\CountFunctor('post_tag.post_id'), 'post_count')); } public static function getMostUsedTag() { - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setTable('post_tag'); $stmt->addColumn('tag_id'); - $stmt->addColumn(new SqlAliasFunctor(new SqlCountFunctor('post_tag.post_id'), 'post_count')); + $stmt->addColumn(new Sql\AliasFunctor(new Sql\CountFunctor('post_tag.post_id'), 'post_count')); $stmt->setGroupBy('post_tag.tag_id'); - $stmt->setOrderBy('post_count', SqlSelectStatement::ORDER_DESC); + $stmt->setOrderBy('post_count', Sql\SelectStatement::ORDER_DESC); $stmt->setLimit(1, 0); return Database::fetchOne($stmt); } diff --git a/src/Models/TagModel.php b/src/Models/TagModel.php index 32dba6f3..a25ac9d2 100644 --- a/src/Models/TagModel.php +++ b/src/Models/TagModel.php @@ -1,4 +1,7 @@ setTable('tag'); - $stmt->setColumn('name', new SqlBinding($tag->name)); - $stmt->setCriterion(new SqlEqualsFunctor('id', new SqlBinding($tag->id))); + $stmt->setColumn('name', new Sql\Binding($tag->name)); + $stmt->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($tag->id))); Database::exec($stmt); }); @@ -24,16 +27,16 @@ class TagModel extends AbstractCrudModel public static function remove($tag) { - $binding = new SqlBinding($tag->id); + $binding = new Sql\Binding($tag->id); - $stmt = new SqlDeleteStatement(); + $stmt = new Sql\DeleteStatement(); $stmt->setTable('post_tag'); - $stmt->setCriterion(new SqlEqualsFunctor('tag_id', $binding)); + $stmt->setCriterion(new Sql\EqualsFunctor('tag_id', $binding)); Database::exec($stmt); - $stmt = new SqlDeleteStatement(); + $stmt = new Sql\DeleteStatement(); $stmt->setTable('tag'); - $stmt->setCriterion(new SqlEqualsFunctor('id', $binding)); + $stmt->setCriterion(new Sql\EqualsFunctor('id', $binding)); Database::exec($stmt); } @@ -62,28 +65,28 @@ class TagModel extends AbstractCrudModel if ($sourceTag->id == $targetTag->id) throw new SimpleException('Source and target tag are the same'); - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setColumn('post.id'); $stmt->setTable('post'); $stmt->setCriterion( - (new SqlConjunctionFunctor) + (new Sql\ConjunctionFunctor) ->add( - new SqlExistsFunctor( - (new SqlSelectStatement) + new Sql\ExistsFunctor( + (new Sql\SelectStatement) ->setTable('post_tag') ->setCriterion( - (new SqlConjunctionFunctor) - ->add(new SqlEqualsFunctor('post_tag.post_id', 'post.id')) - ->add(new SqlEqualsFunctor('post_tag.tag_id', new SqlBinding($sourceTag->id)))))) + (new Sql\ConjunctionFunctor) + ->add(new Sql\EqualsFunctor('post_tag.post_id', 'post.id')) + ->add(new Sql\EqualsFunctor('post_tag.tag_id', new Sql\Binding($sourceTag->id)))))) ->add( - new SqlNegationFunctor( - new SqlExistsFunctor( - (new SqlSelectStatement) + new Sql\NegationFunctor( + new Sql\ExistsFunctor( + (new Sql\SelectStatement) ->setTable('post_tag') ->setCriterion( - (new SqlConjunctionFunctor) - ->add(new SqlEqualsFunctor('post_tag.post_id', 'post.id')) - ->add(new SqlEqualsFunctor('post_tag.tag_id', new SqlBinding($targetTag->id)))))))); + (new Sql\ConjunctionFunctor) + ->add(new Sql\EqualsFunctor('post_tag.post_id', 'post.id')) + ->add(new Sql\EqualsFunctor('post_tag.tag_id', new Sql\Binding($targetTag->id)))))))); $rows = Database::fetchAll($stmt); $postIds = array_map(function($row) { return $row['id']; }, $rows); @@ -91,10 +94,10 @@ class TagModel extends AbstractCrudModel foreach ($postIds as $postId) { - $stmt = new SqlInsertStatement(); + $stmt = new Sql\InsertStatement(); $stmt->setTable('post_tag'); - $stmt->setColumn('post_id', new SqlBinding($postId)); - $stmt->setColumn('tag_id', new SqlBinding($targetTag->id)); + $stmt->setColumn('post_id', new Sql\Binding($postId)); + $stmt->setColumn('tag_id', new Sql\Binding($targetTag->id)); Database::exec($stmt); } }); @@ -103,11 +106,11 @@ class TagModel extends AbstractCrudModel public static function findAllByPostId($key) { - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setColumn('tag.*'); $stmt->setTable('tag'); - $stmt->addInnerJoin('post_tag', new SqlEqualsFunctor('post_tag.tag_id', 'tag.id')); - $stmt->setCriterion(new SqlEqualsFunctor('post_tag.post_id', new SqlBinding($key))); + $stmt->addInnerJoin('post_tag', new Sql\EqualsFunctor('post_tag.tag_id', 'tag.id')); + $stmt->setCriterion(new Sql\EqualsFunctor('post_tag.post_id', new Sql\Binding($key))); $rows = Database::fetchAll($stmt); if ($rows) @@ -117,10 +120,10 @@ class TagModel extends AbstractCrudModel public static function findByName($key, $throw = true) { - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setColumn('tag.*'); $stmt->setTable('tag'); - $stmt->setCriterion(new SqlNoCaseFunctor(new SqlEqualsFunctor('name', new SqlBinding($key)))); + $stmt->setCriterion(new Sql\NoCaseFunctor(new Sql\EqualsFunctor('name', new Sql\Binding($key)))); $row = Database::fetchOne($stmt); if ($row) @@ -135,14 +138,14 @@ class TagModel extends AbstractCrudModel public static function removeUnused() { - $stmt = (new SqlDeleteStatement) + $stmt = (new Sql\DeleteStatement) ->setTable('tag') ->setCriterion( - new SqlNegationFunctor( - new SqlExistsFunctor( - (new SqlSelectStatement) + new Sql\NegationFunctor( + new Sql\ExistsFunctor( + (new Sql\SelectStatement) ->setTable('post_tag') - ->setCriterion(new SqlEqualsFunctor('post_tag.tag_id', 'tag.id'))))); + ->setCriterion(new Sql\EqualsFunctor('post_tag.tag_id', 'tag.id'))))); Database::exec($stmt); } diff --git a/src/Models/TokenModel.php b/src/Models/TokenModel.php index e086af30..fe4167ec 100644 --- a/src/Models/TokenModel.php +++ b/src/Models/TokenModel.php @@ -1,6 +1,8 @@ $token->expires, ]; - $stmt = new SqlUpdateStatement(); + $stmt = new Sql\UpdateStatement(); $stmt->setTable('user_token'); - $stmt->setCriterion(new SqlEqualsFunctor('id', new SqlBinding($token->id))); + $stmt->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($token->id))); foreach ($bindings as $key => $val) - $stmt->setColumn($key, new SqlBinding($val)); + $stmt->setColumn($key, new Sql\Binding($val)); Database::exec($stmt); }); } - - public static function findByToken($key, $throw = true) { if (empty($key)) throw new SimpleNotFoundException('Invalid security token'); - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setTable('user_token'); $stmt->setColumn('*'); - $stmt->setCriterion(new SqlEqualsFunctor('token', new SqlBinding($key))); + $stmt->setCriterion(new Sql\EqualsFunctor('token', new Sql\Binding($key))); $row = Database::fetchOne($stmt); if ($row) @@ -53,8 +53,6 @@ implements IModel return null; } - - public static function checkValidity($token) { if (empty($token)) @@ -67,8 +65,6 @@ implements IModel throw new SimpleException('This token has expired'); } - - public static function forgeUnusedToken() { $tokenText = ''; diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index 10599c33..51d31abd 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -1,4 +1,7 @@ $user->banned ]; - $stmt = (new SqlUpdateStatement) + $stmt = (new Sql\UpdateStatement) ->setTable('user') - ->setCriterion(new SqlEqualsFunctor('id', new SqlBinding($user->id))); + ->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($user->id))); foreach ($bindings as $key => $val) - $stmt->setColumn($key, new SqlBinding($val)); + $stmt->setColumn($key, new Sql\Binding($val)); Database::exec($stmt); }); @@ -55,30 +58,30 @@ class UserModel extends AbstractCrudModel { Database::transaction(function() use ($user) { - $binding = new SqlBinding($user->id); + $binding = new Sql\Binding($user->id); - $stmt = new SqlDeleteStatement(); + $stmt = new Sql\DeleteStatement(); $stmt->setTable('post_score'); - $stmt->setCriterion(new SqlEqualsFunctor('user_id', $binding)); + $stmt->setCriterion(new Sql\EqualsFunctor('user_id', $binding)); Database::exec($stmt); $stmt->setTable('favoritee'); Database::exec($stmt); $stmt->setTable('user'); - $stmt->setCriterion(new SqlEqualsFunctor('id', $binding)); + $stmt->setCriterion(new Sql\EqualsFunctor('id', $binding)); Database::exec($stmt); - $stmt = new SqlUpdateStatement(); + $stmt = new Sql\UpdateStatement(); $stmt->setTable('comment'); - $stmt->setCriterion(new SqlEqualsFunctor('commenter_id', $binding)); - $stmt->setColumn('commenter_id', new SqlNullFunctor()); + $stmt->setCriterion(new Sql\EqualsFunctor('commenter_id', $binding)); + $stmt->setColumn('commenter_id', new Sql\NullFunctor()); Database::exec($stmt); - $stmt = new SqlUpdateStatement(); + $stmt = new Sql\UpdateStatement(); $stmt->setTable('post'); - $stmt->setCriterion(new SqlEqualsFunctor('uploader_id', $binding)); - $stmt->setColumn('uploader_id', new SqlNullFunctor()); + $stmt->setCriterion(new Sql\EqualsFunctor('uploader_id', $binding)); + $stmt->setColumn('uploader_id', new Sql\NullFunctor()); Database::exec($stmt); }); } @@ -87,10 +90,10 @@ class UserModel extends AbstractCrudModel public static function findByName($key, $throw = true) { - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setColumn('*'); $stmt->setTable('user'); - $stmt->setCriterion(new SqlNoCaseFunctor(new SqlEqualsFunctor('name', new SqlBinding(trim($key))))); + $stmt->setCriterion(new Sql\NoCaseFunctor(new Sql\EqualsFunctor('name', new Sql\Binding(trim($key))))); $row = Database::fetchOne($stmt); if ($row) @@ -103,12 +106,12 @@ class UserModel extends AbstractCrudModel public static function findByNameOrEmail($key, $throw = true) { - $stmt = new SqlSelectStatement(); + $stmt = new Sql\SelectStatement(); $stmt->setColumn('*'); $stmt->setTable('user'); - $stmt->setCriterion((new SqlDisjunctionFunctor) - ->add(new SqlNoCaseFunctor(new SqlEqualsFunctor('name', new SqlBinding(trim($key))))) - ->add(new SqlNoCaseFunctor(new SqlEqualsFunctor('email_confirmed', new SqlBinding(trim($key)))))); + $stmt->setCriterion((new Sql\DisjunctionFunctor) + ->add(new Sql\NoCaseFunctor(new Sql\EqualsFunctor('name', new Sql\Binding(trim($key))))) + ->add(new Sql\NoCaseFunctor(new Sql\EqualsFunctor('email_confirmed', new Sql\Binding(trim($key)))))); $row = Database::fetchOne($stmt); if ($row) @@ -125,20 +128,20 @@ class UserModel extends AbstractCrudModel { Database::transaction(function() use ($user, $post, $score) { - $stmt = new SqlDeleteStatement(); + $stmt = new Sql\DeleteStatement(); $stmt->setTable('post_score'); - $stmt->setCriterion((new SqlConjunctionFunctor) - ->add(new SqlEqualsFunctor('post_id', new SqlBinding($post->id))) - ->add(new SqlEqualsFunctor('user_id', new SqlBinding($user->id)))); + $stmt->setCriterion((new Sql\ConjunctionFunctor) + ->add(new Sql\EqualsFunctor('post_id', new Sql\Binding($post->id))) + ->add(new Sql\EqualsFunctor('user_id', new Sql\Binding($user->id)))); Database::exec($stmt); $score = intval($score); if ($score != 0) { - $stmt = new SqlInsertStatement(); + $stmt = new Sql\InsertStatement(); $stmt->setTable('post_score'); - $stmt->setColumn('post_id', new SqlBinding($post->id)); - $stmt->setColumn('user_id', new SqlBinding($user->id)); - $stmt->setColumn('score', new SqlBinding($score)); + $stmt->setColumn('post_id', new Sql\Binding($post->id)); + $stmt->setColumn('user_id', new Sql\Binding($user->id)); + $stmt->setColumn('score', new Sql\Binding($score)); Database::exec($stmt); } }); @@ -149,10 +152,10 @@ class UserModel extends AbstractCrudModel Database::transaction(function() use ($user, $post) { self::removeFromUserFavorites($user, $post); - $stmt = new SqlInsertStatement(); + $stmt = new Sql\InsertStatement(); $stmt->setTable('favoritee'); - $stmt->setColumn('post_id', new SqlBinding($post->id)); - $stmt->setColumn('user_id', new SqlBinding($user->id)); + $stmt->setColumn('post_id', new Sql\Binding($post->id)); + $stmt->setColumn('user_id', new Sql\Binding($user->id)); Database::exec($stmt); }); } @@ -161,11 +164,11 @@ class UserModel extends AbstractCrudModel { Database::transaction(function() use ($user, $post) { - $stmt = new SqlDeleteStatement(); + $stmt = new Sql\DeleteStatement(); $stmt->setTable('favoritee'); - $stmt->setCriterion((new SqlConjunctionFunctor) - ->add(new SqlEqualsFunctor('post_id', new SqlBinding($post->id))) - ->add(new SqlEqualsFunctor('user_id', new SqlBinding($user->id)))); + $stmt->setCriterion((new Sql\ConjunctionFunctor) + ->add(new Sql\EqualsFunctor('post_id', new Sql\Binding($post->id))) + ->add(new Sql\EqualsFunctor('user_id', new Sql\Binding($user->id)))); Database::exec($stmt); }); } diff --git a/src/Sql/Functors/BinaryOperatorFunctors/SqlAdditionFunctor.php b/src/Sql/Functors/BinaryOperatorFunctors/SqlAdditionFunctor.php deleted file mode 100644 index 25db722c..00000000 --- a/src/Sql/Functors/BinaryOperatorFunctors/SqlAdditionFunctor.php +++ /dev/null @@ -1,8 +0,0 @@ -subjects[0]) - . ' ' . $this->getOperator() - . ' ' . $this->subjects[1]->getAsString(); - } -} diff --git a/src/Sql/Functors/BinaryOperatorFunctors/SqlDivisionFunctor.php b/src/Sql/Functors/BinaryOperatorFunctors/SqlDivisionFunctor.php deleted file mode 100644 index 85bf0fa3..00000000 --- a/src/Sql/Functors/BinaryOperatorFunctors/SqlDivisionFunctor.php +++ /dev/null @@ -1,8 +0,0 @@ -='; - } -} diff --git a/src/Sql/Functors/BinaryOperatorFunctors/SqlEqualsOrLesserFunctor.php b/src/Sql/Functors/BinaryOperatorFunctors/SqlEqualsOrLesserFunctor.php deleted file mode 100644 index 8be695e5..00000000 --- a/src/Sql/Functors/BinaryOperatorFunctors/SqlEqualsOrLesserFunctor.php +++ /dev/null @@ -1,8 +0,0 @@ -'; - } -} diff --git a/src/Sql/Functors/BinaryOperatorFunctors/SqlIsFunctor.php b/src/Sql/Functors/BinaryOperatorFunctors/SqlIsFunctor.php deleted file mode 100644 index ee0174ad..00000000 --- a/src/Sql/Functors/BinaryOperatorFunctors/SqlIsFunctor.php +++ /dev/null @@ -1,8 +0,0 @@ -getFunctionName() . ' (' . $this->subjects[0]->getAsString() . ')'; - } -} diff --git a/src/Sql/Functors/FunctionFunctors/SqlIfNullFunctor.php b/src/Sql/Functors/FunctionFunctors/SqlIfNullFunctor.php deleted file mode 100644 index 7fc26647..00000000 --- a/src/Sql/Functors/FunctionFunctors/SqlIfNullFunctor.php +++ /dev/null @@ -1,13 +0,0 @@ -subjects[0]->getAsString() . ' COLLATE NOCASE'; - } -} diff --git a/src/Sql/Functors/FunctionFunctors/SqlRandomFunctor.php b/src/Sql/Functors/FunctionFunctors/SqlRandomFunctor.php deleted file mode 100644 index 7b8416d2..00000000 --- a/src/Sql/Functors/FunctionFunctors/SqlRandomFunctor.php +++ /dev/null @@ -1,16 +0,0 @@ -main->dbDriver == 'sqlite' - ? 'RANDOM' - : 'RAND'; - } - - public function getArgumentCount() - { - return 2; - } -} diff --git a/src/Sql/Functors/FunctionFunctors/SqlSubstrFunctor.php b/src/Sql/Functors/FunctionFunctors/SqlSubstrFunctor.php deleted file mode 100644 index da3ac0c7..00000000 --- a/src/Sql/Functors/FunctionFunctors/SqlSubstrFunctor.php +++ /dev/null @@ -1,13 +0,0 @@ -subjects)) . ')'; - } -} diff --git a/src/Sql/Functors/MiscOperators/SqlDisjunctionFunctor.php b/src/Sql/Functors/MiscOperators/SqlDisjunctionFunctor.php deleted file mode 100644 index bcab3055..00000000 --- a/src/Sql/Functors/MiscOperators/SqlDisjunctionFunctor.php +++ /dev/null @@ -1,16 +0,0 @@ -subjects)) . ')'; - } -} diff --git a/src/Sql/Functors/MiscOperators/SqlInFunctor.php b/src/Sql/Functors/MiscOperators/SqlInFunctor.php deleted file mode 100644 index a34cabc7..00000000 --- a/src/Sql/Functors/MiscOperators/SqlInFunctor.php +++ /dev/null @@ -1,23 +0,0 @@ -subject = $this->attachExpression($subject); - } - - public function getAsStringEmpty() - { - return '0'; - } - - public function getAsStringNonEmpty() - { - return self::surroundBraces($this->subject) . ' IN (' . join(', ', array_map(function($subject) - { - return self::surroundBraces($subject); - }, $this->subjects)) . ')'; - } -} diff --git a/src/Sql/Functors/MiscOperators/SqlNullFunctor.php b/src/Sql/Functors/MiscOperators/SqlNullFunctor.php deleted file mode 100644 index 1eed354b..00000000 --- a/src/Sql/Functors/MiscOperators/SqlNullFunctor.php +++ /dev/null @@ -1,8 +0,0 @@ -subjects[0]) - . ' ' . $this->getOperator() - . ' ' . self::surroundBraces($this->subjects[1]); - } - - protected abstract function getOperator(); -} diff --git a/src/Sql/Functors/SqlFunctionFunctor.php b/src/Sql/Functors/SqlFunctionFunctor.php deleted file mode 100644 index 73df8dfa..00000000 --- a/src/Sql/Functors/SqlFunctionFunctor.php +++ /dev/null @@ -1,30 +0,0 @@ -getArgumentCount(); - if (!in_array(count($subjects), $expectedArgumentCount)) - throw new Exception('Unepxected argument count for ' . get_called_class()); - - foreach ($subjects as $subject) - $this->subjects []= $this->attachExpression($subject); - } - - protected abstract function getFunctionName(); - protected abstract function getArgumentCount(); - - public function getAsString() - { - return $this->getFunctionName() - . ' (' - . join(', ', array_map(function($subject) - { - return self::surroundBraces($subject); - }, $this->subjects)) - . ')'; - } -} diff --git a/src/Sql/Functors/SqlFunctor.php b/src/Sql/Functors/SqlFunctor.php deleted file mode 100644 index 26dba8fc..00000000 --- a/src/Sql/Functors/SqlFunctor.php +++ /dev/null @@ -1,4 +0,0 @@ -subjects = []; - } - - public function add($subject) - { - $this->subjects []= $this->attachExpression($subject); - return $this; - } - - public abstract function getAsStringNonEmpty(); - public abstract function getAsStringEmpty(); - - public function getAsString() - { - if (empty(array_filter($this->subjects, function($x) { return !empty($x->getAsString()); }))) - return $this->getAsStringEmpty(); - - return $this->getAsStringNonEmpty(); - } - - //variable arguments - public static function fromArray() - { - $args = func_get_args(); - $subjects = array_pop($args); - $instance = (new ReflectionClass(get_called_class()))->newInstanceArgs($args); - foreach ($subjects as $subject) - $instance->add($subject); - return $instance; - } -} diff --git a/src/Sql/SqlBinding.php b/src/Sql/SqlBinding.php deleted file mode 100644 index a284b21c..00000000 --- a/src/Sql/SqlBinding.php +++ /dev/null @@ -1,31 +0,0 @@ -content = $content; - $this->name = ':p' . (self::$bindingCount ++); - } - - public function getName() - { - return $this->name; - } - - public function getValue() - { - return $this->content; - } - - public static function fromArray(array $contents) - { - return array_map(function($content) - { - return new SqlBinding($content); - }, $contents); - } -} diff --git a/src/Sql/SqlExpression.php b/src/Sql/SqlExpression.php deleted file mode 100644 index 7eec46d0..00000000 --- a/src/Sql/SqlExpression.php +++ /dev/null @@ -1,49 +0,0 @@ -bindings[$key] = $val; - return $this; - } - - protected static function surroundBraces(SqlExpression $object) - { - if ($object instanceof SqlStatement) - return '(' . $object->getAsString() . ')'; - return $object->getAsString(); - } - - public function getBindings() - { - $bindings = $this->bindings; - foreach ($this->subExpressions as $subExpression) - $bindings = array_merge($bindings, $subExpression->getBindings()); - return $bindings; - } - - public function attachExpression($object) - { - if ($object instanceof SqlBinding) - { - $expr = new SqlStringExpression($object->getName()); - $expr->bind($object->getName(), $object->getValue()); - $this->subExpressions []= $expr; - return $expr; - } - else if ($object instanceof SqlExpression) - { - $this->subExpressions []= $object; - return $object; - } - else - { - return new SqlStringExpression((string) $object); - } - } -} diff --git a/src/Sql/SqlStringExpression.php b/src/Sql/SqlStringExpression.php deleted file mode 100644 index 977e15d3..00000000 --- a/src/Sql/SqlStringExpression.php +++ /dev/null @@ -1,15 +0,0 @@ -text = $text; - } - - public function getAsString() - { - return $this->text; - } -} diff --git a/src/Sql/Statements/SqlDeleteStatement.php b/src/Sql/Statements/SqlDeleteStatement.php deleted file mode 100644 index d297ef87..00000000 --- a/src/Sql/Statements/SqlDeleteStatement.php +++ /dev/null @@ -1,39 +0,0 @@ -table; - } - - public function setTable($table) - { - $this->table = new SqlStringExpression($table); - return $this; - } - - public function getCriterion() - { - return $this->criterion; - } - - public function setCriterion($criterion) - { - $this->criterion = $this->attachExpression($criterion); - return $this; - } - - public function getAsString() - { - $sql = 'DELETE FROM ' . $this->table->getAsString() . ' '; - - if (!empty($this->criterion) and !empty($this->criterion->getAsString())) - $sql .= ' WHERE ' . $this->criterion->getAsString(); - - return $sql; - } -} - diff --git a/src/Sql/Statements/SqlInsertStatement.php b/src/Sql/Statements/SqlInsertStatement.php deleted file mode 100644 index bc151869..00000000 --- a/src/Sql/Statements/SqlInsertStatement.php +++ /dev/null @@ -1,62 +0,0 @@ -table; - } - - public function setTable($table) - { - $this->table = new SqlStringExpression($table); - return $this; - } - - public function setColumn($column, $what) - { - $this->columns[$column] = $this->attachExpression($what); - $this->source = null; - return $this; - } - - public function setSource($columns, $what) - { - $this->source = $this->attachExpression($what); - $this->columns = $columns; - } - - public function getAsString() - { - $sql = 'INSERT INTO ' . $this->table->getAsString() . ' '; - if (!empty($this->source)) - { - $sql .= ' (' . join(', ', $this->columns) . ')'; - $sql .= ' ' . $this->source->getAsString(); - } - else - { - if (empty($this->columns)) - { - $config = \Chibi\Registry::getConfig(); - if ($config->main->dbDriver == 'sqlite') - $sql .= ' DEFAULT VALUES'; - else - $sql .= ' VALUES()'; - } - else - { - $sql .= ' (' . join(', ', array_keys($this->columns)) . ')'; - - $sql .= ' VALUES (' . join(', ', array_map(function($val) - { - return '(' . $val->getAsString() . ')'; - }, array_values($this->columns))) . ')'; - } - } - return $sql; - } -} diff --git a/src/Sql/Statements/SqlRawStatement.php b/src/Sql/Statements/SqlRawStatement.php deleted file mode 100644 index cfea4250..00000000 --- a/src/Sql/Statements/SqlRawStatement.php +++ /dev/null @@ -1,15 +0,0 @@ -text = $text; - } - - public function getAsString() - { - return $this->text; - } -} diff --git a/src/Sql/Statements/SqlSelectStatement.php b/src/Sql/Statements/SqlSelectStatement.php deleted file mode 100644 index 8c06b95f..00000000 --- a/src/Sql/Statements/SqlSelectStatement.php +++ /dev/null @@ -1,213 +0,0 @@ -columns; - } - - public function resetColumns() - { - $this->columns = []; - return $this; - } - - public function setColumn($what) - { - $this->setColumns([$what]); - return $this; - } - - public function addColumn($what) - { - $this->columns []= $this->attachExpression($what); - return $this; - } - - public function setColumns($what) - { - $this->resetColumns(); - foreach ($what as $item) - $this->addColumn($item); - return $this; - } - - public function getTable() - { - return $this->source; - } - - public function setTable($table) - { - $this->source = new SqlStringExpression($table); - return $this; - } - - public function setSource(SqlExpression $source) - { - $this->source = $this->attachExpression($source); - return $this; - } - - public function addInnerJoin($table, SqlExpression $expression) - { - $this->innerJoins []= [$table, $this->attachExpression($expression)]; - return $this; - } - - public function addLeftOuterJoin($table, $expression) - { - $this->leftOuterJoins []= [$table, $this->attachExpression($expression)]; - return $this; - } - - public function getJoinedTables() - { - $tables = []; - foreach (array_merge($this->innerJoins, $this->leftOuterJoins) as $join) - { - list ($table, $joinExpression) = $join; - $tables []= $table; - } - return array_unique($tables); - } - - public function isTableJoined($table) - { - return in_array($table, $this->getJoinedTables()); - } - - public function getCriterion() - { - return $this->criterion; - } - - public function setCriterion($criterion) - { - $this->criterion = $this->attachExpression($criterion); - return $this; - } - - public function resetOrderBy() - { - $this->orderBy = []; - return $this; - } - - public function setOrderBy($what, $dir = self::ORDER_ASC) - { - $this->resetOrderBy(); - $this->addOrderBy($this->attachExpression($what), $dir); - return $this; - } - - public function addOrderBy($what, $dir = self::ORDER_ASC) - { - $this->orderBy []= [$this->attachExpression($what), $dir]; - return $this; - } - - public function getOrderBy() - { - return $this->orderBy; - } - - public function resetLimit() - { - $this->limit = null; - $this->offset = null; - return $this; - } - - public function setLimit($limit, $offset = null) - { - $this->limit = $this->attachExpression($limit); - $this->offset = $this->attachExpression($offset); - return $this; - } - - public function setGroupBy($groupBy) - { - $this->groupBy = $this->attachExpression($groupBy); - } - - public function getAsString() - { - $sql = 'SELECT '; - if (!empty($this->columns)) - $sql .= join(', ', array_map(function($column) - { - return $column->getAsString(); - }, $this->columns)); - else - $sql .= '1'; - $sql .= ' FROM ' . self::surroundBraces($this->source); - - foreach ($this->innerJoins as $join) - { - list ($table, $criterion) = $join; - $sql .= ' INNER JOIN ' . $table . ' ON ' . $criterion->getAsString(); - } - - foreach ($this->leftOuterJoins as $outerJoin) - { - list ($table, $criterion) = $outerJoin; - $sql .= ' LEFT OUTER JOIN ' . $table . ' ON ' . $criterion->getAsString(); - } - - if (!empty($this->criterion) and !empty($this->criterion->getAsString())) - $sql .= ' WHERE ' . $this->criterion->getAsString(); - - if (!empty($this->groupBy) and !empty($this->groupBy->getAsString())) - { - $sql .= ' GROUP BY ' . $this->groupBy->getAsString(); - } - - if (!empty($this->orderBy)) - { - $f = true; - foreach ($this->orderBy as $orderBy) - { - $sql .= $f ? ' ORDER BY' : ', '; - $f = false; - list ($orderColumn, $orderDir) = $orderBy; - $sql .= ' ' . $orderColumn->getAsString(); - switch ($orderDir) - { - case self::ORDER_DESC: - $sql .= ' DESC'; - break; - case self::ORDER_ASC: - $sql .= ' ASC'; - break; - } - } - } - - if (!empty($this->limit) and !empty($this->limit->getAsString())) - { - $sql .= ' LIMIT '; - $sql .= $this->limit->getAsString(); - if (!empty($this->offset)) - { - $sql .= ' OFFSET '; - $sql .= $this->offset->getAsString(); - } - } - - return $sql; - } -} diff --git a/src/Sql/Statements/SqlStatement.php b/src/Sql/Statements/SqlStatement.php deleted file mode 100644 index 9b047d90..00000000 --- a/src/Sql/Statements/SqlStatement.php +++ /dev/null @@ -1,4 +0,0 @@ -table; - } - - public function setTable($table) - { - $this->table = new SqlStringExpression($table); - return $this; - } - - public function getCriterion() - { - return $this->criterion; - } - - public function setCriterion($criterion) - { - $this->criterion = $this->attachExpression($criterion); - return $this; - } - - public function setColumn($column, $what) - { - $this->columns[$column] = $this->attachExpression($what); - return $this; - } - - public function getAsString() - { - $sql = 'UPDATE ' . $this->table->getAsString(); - - if (!empty($this->columns)) - { - $sql .= ' SET ' . join(', ', array_map(function($key) - { - return $key . ' = (' . $this->columns[$key]->getAsString() . ')'; - }, array_keys($this->columns))); - } - - if (!empty($this->criterion) and !empty($this->criterion->getAsString())) - $sql .= ' WHERE ' . $this->criterion->getAsString(); - - return $sql; - } -} diff --git a/src/Views/debug.phtml b/src/Views/debug.phtml index 80c3ac62..a71ce8c0 100644 --- a/src/Views/debug.phtml +++ b/src/Views/debug.phtml @@ -1,9 +1,9 @@
- +
statement->getAsString(); + $query = $log->getStatement()->getAsString(); $query = str_replace('(', '(', $query); $query = str_replace(')', ')', $query); ?> @@ -11,13 +11,13 @@
' . $log->statement->getBindings()[$key] . '';
+					return $key . '=' . $log->getStatement()->getBindings()[$key] . '';
 				},
-				array_keys($log->statement->getBindings()))) ?>
+ array_keys($log->getStatement()->getBindings()))) ?> - - + +
Execution:timeExec) ?>
Retrieval:timeFetch) ?>
Execution:getExecutionTime()) ?>
Retrieval:getRetrievalTime()) ?>
diff --git a/src/Views/layout-normal.phtml b/src/Views/layout-normal.phtml index ace048bf..9724b443 100644 --- a/src/Views/layout-normal.phtml +++ b/src/Views/layout-normal.phtml @@ -37,7 +37,7 @@ CustomAssetViewDecorator::addScript('core.js');

Load: context->startTime) ?>s - Queries: + Queries: szurubooru v Logs diff --git a/src/core.php b/src/core.php index 8e23e7c2..56253da7 100644 --- a/src/core.php +++ b/src/core.php @@ -13,7 +13,7 @@ ini_set('memory_limit', '128M'); //basic include calls, autoloader init require_once $rootDir . 'lib' . DS . 'php-markdown' . DS . 'Michelf' . DS . 'Markdown.php'; require_once $rootDir . 'lib' . DS . 'chibi-core' . DS . 'Facade.php'; -\Chibi\AutoLoader::init(__DIR__); +\Chibi\AutoLoader::init([__DIR__, $rootDir . 'lib' . DS . 'chibi-sql']); //load config manually $configPaths = @@ -39,7 +39,11 @@ $context = \Chibi\Registry::getContext(); $context->startTime = $startTime; $context->rootDir = $rootDir; -Database::connect($config->main->dbDriver, TextHelper::absolutePath($config->main->dbLocation), $config->main->dbUser, $config->main->dbPass); +\Chibi\Database::connect( + $config->main->dbDriver, + TextHelper::absolutePath($config->main->dbLocation), + $config->main->dbUser, + $config->main->dbPass); //wire models foreach (\Chibi\AutoLoader::getAllIncludablePaths() as $path) diff --git a/upgrade.php b/upgrade.php index 18460887..65deffff 100644 --- a/upgrade.php +++ b/upgrade.php @@ -57,7 +57,7 @@ foreach ($upgrades as $upgradePath) { try { - Database::exec(new SqlRawStatement($query)); + \Chibi\Database::exec(new \Chibi\Sql\RawStatement($query)); } catch (Exception $e) {