diff --git a/src/Controllers/IndexController.php b/src/Controllers/IndexController.php index cefeee2b..e2b7aa21 100644 --- a/src/Controllers/IndexController.php +++ b/src/Controllers/IndexController.php @@ -57,10 +57,10 @@ class IndexController $stmt = (new SqlSelectStatement) ->setColumn('id') ->setTable('post') - ->setCriterion((new SqlConjunction) - ->add(new SqlEqualsOperator('type', new SqlBinding(PostType::Image))) - ->add(new SqlEqualsOperator('safety', new SqlBinding(PostSafety::Safe)))) - ->setOrderBy(new SqlRandomOperator(), SqlSelectStatement::ORDER_DESC); + ->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; diff --git a/src/Models/AbstractCrudModel.php b/src/Models/AbstractCrudModel.php index 53ea77d4..958965f7 100644 --- a/src/Models/AbstractCrudModel.php +++ b/src/Models/AbstractCrudModel.php @@ -24,7 +24,7 @@ abstract class AbstractCrudModel implements IModel $stmt = new SqlSelectStatement(); $stmt->setColumn('*'); $stmt->setTable(static::getTableName()); - $stmt->setCriterion(new SqlEqualsOperator('id', new SqlBinding($key))); + $stmt->setCriterion(new SqlEqualsFunctor('id', new SqlBinding($key))); $row = Database::fetchOne($stmt); if ($row) @@ -40,7 +40,7 @@ abstract class AbstractCrudModel implements IModel $stmt = new SqlSelectStatement(); $stmt->setColumn('*'); $stmt->setTable(static::getTableName()); - $stmt->setCriterion(SqlInOperator::fromArray('id', SqlBinding::fromArray(array_unique($ids)))); + $stmt->setCriterion(SqlInFunctor::fromArray('id', SqlBinding::fromArray(array_unique($ids)))); $rows = Database::fetchAll($stmt); if ($rows) @@ -52,7 +52,7 @@ abstract class AbstractCrudModel implements IModel public static function getCount() { $stmt = new SqlSelectStatement(); - $stmt->setColumn(new SqlAliasOperator(new SqlCountOperator('1'), 'count')); + $stmt->setColumn(new SqlAliasFunctor(new SqlCountFunctor('1'), 'count')); $stmt->setTable(static::getTableName()); return Database::fetchOne($stmt)['count']; } diff --git a/src/Models/CommentModel.php b/src/Models/CommentModel.php index 1bd8a40f..f1cb8782 100644 --- a/src/Models/CommentModel.php +++ b/src/Models/CommentModel.php @@ -27,7 +27,7 @@ class CommentModel extends AbstractCrudModel $stmt = new SqlUpdateStatement(); $stmt->setTable('comment'); - $stmt->setCriterion(new SqlEqualsOperator('id', new SqlBinding($comment->id))); + $stmt->setCriterion(new SqlEqualsFunctor('id', new SqlBinding($comment->id))); foreach ($bindings as $key => $val) $stmt->setColumn($key, new SqlBinding($val)); @@ -42,7 +42,7 @@ class CommentModel extends AbstractCrudModel { $stmt = new SqlDeleteStatement(); $stmt->setTable('comment'); - $stmt->setCriterion(new SqlEqualsOperator('id', new SqlBinding($comment->id))); + $stmt->setCriterion(new SqlEqualsFunctor('id', new SqlBinding($comment->id))); Database::exec($stmt); }); } @@ -54,7 +54,7 @@ class CommentModel extends AbstractCrudModel $stmt = new SqlSelectStatement(); $stmt->setColumn('comment.*'); $stmt->setTable('comment'); - $stmt->setCriterion(new SqlEqualsOperator('post_id', new SqlBinding($key))); + $stmt->setCriterion(new SqlEqualsFunctor('post_id', new SqlBinding($key))); $rows = Database::fetchAll($stmt); if ($rows) diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index 8ff6d5b9..a85d37a8 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -54,8 +54,8 @@ class PostEntity extends AbstractEntity $stmt = new SqlSelectStatement(); $stmt->setColumn('user.*'); $stmt->setTable('user'); - $stmt->addInnerJoin('favoritee', new SqlEqualsOperator('favoritee.user_id', 'user.id')); - $stmt->setCriterion(new SqlEqualsOperator('favoritee.post_id', new SqlBinding($this->id))); + $stmt->addInnerJoin('favoritee', new SqlEqualsFunctor('favoritee.user_id', 'user.id')); + $stmt->setCriterion(new SqlEqualsFunctor('favoritee.post_id', new SqlBinding($this->id))); $rows = Database::fetchAll($stmt); $favorites = UserModel::convertRows($rows); $this->setCache('favoritee', $favorites); @@ -73,15 +73,15 @@ class PostEntity extends AbstractEntity $stmt->setColumn('post.*'); $stmt->setTable('post'); $binding = new SqlBinding($this->id); - $stmt->addInnerJoin('crossref', (new SqlDisjunction) + $stmt->addInnerJoin('crossref', (new SqlDisjunctionFunctor) ->add( - (new SqlConjunction) - ->add(new SqlEqualsOperator('post.id', 'crossref.post2_id')) - ->add(new SqlEqualsOperator('crossref.post_id', $binding))) + (new SqlConjunctionFunctor) + ->add(new SqlEqualsFunctor('post.id', 'crossref.post2_id')) + ->add(new SqlEqualsFunctor('crossref.post_id', $binding))) ->add( - (new SqlConjunction) - ->add(new SqlEqualsOperator('post.id', 'crossref.post_id')) - ->add(new SqlEqualsOperator('crossref.post2_id', $binding)))); + (new SqlConjunctionFunctor) + ->add(new SqlEqualsFunctor('post.id', 'crossref.post_id')) + ->add(new SqlEqualsFunctor('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 07d93203..e1f21492 100644 --- a/src/Models/Entities/TagEntity.php +++ b/src/Models/Entities/TagEntity.php @@ -6,9 +6,9 @@ class TagEntity extends AbstractEntity public function getPostCount() { $stmt = new SqlSelectStatement(); - $stmt->setColumn(new SqlAliasOperator(new SqlCountOperator('1'), 'count')); + $stmt->setColumn(new SqlAliasFunctor(new SqlCountFunctor('1'), 'count')); $stmt->setTable('post_tag'); - $stmt->setCriterion(new SqlEqualsOperator('tag_id', new SqlBinding($this->id))); + $stmt->setCriterion(new SqlEqualsFunctor('tag_id', new SqlBinding($this->id))); return Database::fetchOne($stmt)['count']; } } diff --git a/src/Models/Entities/UserEntity.php b/src/Models/Entities/UserEntity.php index 21870795..b79b8f5e 100644 --- a/src/Models/Entities/UserEntity.php +++ b/src/Models/Entities/UserEntity.php @@ -112,11 +112,11 @@ class UserEntity extends AbstractEntity public function hasFavorited($post) { $stmt = new SqlSelectStatement(); - $stmt->setColumn(new SqlAliasOperator(new SqlCountOperator('1'), 'count')); + $stmt->setColumn(new SqlAliasFunctor(new SqlCountFunctor('1'), 'count')); $stmt->setTable('favoritee'); - $stmt->setCriterion((new SqlConjunction) - ->add(new SqlEqualsOperator('user_id', new SqlBinding($this->id))) - ->add(new SqlEqualsOperator('post_id', new SqlBinding($post->id)))); + $stmt->setCriterion((new SqlConjunctionFunctor) + ->add(new SqlEqualsFunctor('user_id', new SqlBinding($this->id))) + ->add(new SqlEqualsFunctor('post_id', new SqlBinding($post->id)))); return Database::fetchOne($stmt)['count'] == 1; } @@ -125,9 +125,9 @@ class UserEntity extends AbstractEntity $stmt = new SqlSelectStatement(); $stmt->setColumn('score'); $stmt->setTable('post_score'); - $stmt->setCriterion((new SqlConjunction) - ->add(new SqlEqualsOperator('user_id', new SqlBinding($this->id))) - ->add(new SqlEqualsOperator('post_id', new SqlBinding($post->id)))); + $stmt->setCriterion((new SqlConjunctionFunctor) + ->add(new SqlEqualsFunctor('user_id', new SqlBinding($this->id))) + ->add(new SqlEqualsFunctor('post_id', new SqlBinding($post->id)))); $row = Database::fetchOne($stmt); if ($row) return intval($row['score']); @@ -137,27 +137,27 @@ class UserEntity extends AbstractEntity public function getFavoriteCount() { $stmt = new SqlSelectStatement(); - $stmt->setColumn(new SqlAliasOperator(new SqlCountOperator('1'), 'count')); + $stmt->setColumn(new SqlAliasFunctor(new SqlCountFunctor('1'), 'count')); $stmt->setTable('favoritee'); - $stmt->setCriterion(new SqlEqualsOperator('user_id', new SqlBinding($this->id))); + $stmt->setCriterion(new SqlEqualsFunctor('user_id', new SqlBinding($this->id))); return Database::fetchOne($stmt)['count']; } public function getCommentCount() { $stmt = new SqlSelectStatement(); - $stmt->setColumn(new SqlAliasOperator(new SqlCountOperator('1'), 'count')); + $stmt->setColumn(new SqlAliasFunctor(new SqlCountFunctor('1'), 'count')); $stmt->setTable('comment'); - $stmt->setCriterion(new SqlEqualsOperator('commenter_id', new SqlBinding($this->id))); + $stmt->setCriterion(new SqlEqualsFunctor('commenter_id', new SqlBinding($this->id))); return Database::fetchOne($stmt)['count']; } public function getPostCount() { $stmt = new SqlSelectStatement(); - $stmt->setColumn(new SqlAliasOperator(new SqlCountOperator('1'), 'count')); + $stmt->setColumn(new SqlAliasFunctor(new SqlCountFunctor('1'), 'count')); $stmt->setTable('post'); - $stmt->setCriterion(new SqlEqualsOperator('uploader_id', new SqlBinding($this->id))); + $stmt->setCriterion(new SqlEqualsFunctor('uploader_id', new SqlBinding($this->id))); return Database::fetchOne($stmt)['count']; } } diff --git a/src/Models/PostModel.php b/src/Models/PostModel.php index 2df06196..71261ddf 100644 --- a/src/Models/PostModel.php +++ b/src/Models/PostModel.php @@ -54,7 +54,7 @@ class PostModel extends AbstractCrudModel foreach ($bindings as $key => $value) $stmt->setColumn($key, new SqlBinding($value)); - $stmt->setCriterion(new SqlEqualsOperator('id', new SqlBinding($post->id))); + $stmt->setCriterion(new SqlEqualsFunctor('id', new SqlBinding($post->id))); Database::exec($stmt); //tags @@ -62,7 +62,7 @@ class PostModel extends AbstractCrudModel $stmt = new SqlDeleteStatement(); $stmt->setTable('post_tag'); - $stmt->setCriterion(new SqlEqualsOperator('post_id', new SqlBinding($post->id))); + $stmt->setCriterion(new SqlEqualsFunctor('post_id', new SqlBinding($post->id))); Database::exec($stmt); foreach ($tags as $postTag) @@ -80,9 +80,9 @@ class PostModel extends AbstractCrudModel $stmt = new SqlDeleteStatement(); $stmt->setTable('crossref'); $binding = new SqlBinding($post->id); - $stmt->setCriterion((new SqlDisjunction) - ->add(new SqlEqualsOperator('post_id', $binding)) - ->add(new SqlEqualsOperator('post2_id', $binding))); + $stmt->setCriterion((new SqlDisjunctionFunctor) + ->add(new SqlEqualsFunctor('post_id', $binding)) + ->add(new SqlEqualsFunctor('post2_id', $binding))); Database::exec($stmt); foreach ($relations as $relatedPost) @@ -104,7 +104,7 @@ class PostModel extends AbstractCrudModel $stmt = new SqlDeleteStatement(); $stmt->setTable('post_score'); - $stmt->setCriterion(new SqlEqualsOperator('post_id', $binding)); + $stmt->setCriterion(new SqlEqualsFunctor('post_id', $binding)); Database::exec($stmt); $stmt->setTable('post_tag'); @@ -117,13 +117,13 @@ class PostModel extends AbstractCrudModel Database::exec($stmt); $stmt->setTable('crossref'); - $stmt->setCriterion((new SqlDisjunction) - ->add(new SqlEqualsOperator('post_id', $binding)) - ->add(new SqlEqualsOperator('post_id', $binding))); + $stmt->setCriterion((new SqlDisjunctionFunctor) + ->add(new SqlEqualsFunctor('post_id', $binding)) + ->add(new SqlEqualsFunctor('post_id', $binding))); Database::exec($stmt); $stmt->setTable('post'); - $stmt->setCriterion(new SqlEqualsOperator('id', $binding)); + $stmt->setCriterion(new SqlEqualsFunctor('id', $binding)); Database::exec($stmt); }); } @@ -136,7 +136,7 @@ class PostModel extends AbstractCrudModel $stmt = new SqlSelectStatement(); $stmt->setColumn('*'); $stmt->setTable('post'); - $stmt->setCriterion(new SqlEqualsOperator('name', new SqlBinding($key))); + $stmt->setCriterion(new SqlEqualsFunctor('name', new SqlBinding($key))); $row = Database::fetchOne($stmt); if ($row) @@ -161,7 +161,7 @@ class PostModel extends AbstractCrudModel $stmt = new SqlSelectStatement(); $stmt->setColumn('*'); $stmt->setTable('post'); - $stmt->setCriterion(new SqlEqualsOperator('file_hash', new SqlBinding($key))); + $stmt->setCriterion(new SqlEqualsFunctor('file_hash', new SqlBinding($key))); $row = Database::fetchOne($stmt); if ($row) @@ -193,7 +193,7 @@ class PostModel extends AbstractCrudModel $stmt->setTable('comment'); $stmt->addColumn('comment.*'); $stmt->addColumn('post_id'); - $stmt->setCriterion(SqlInOperator::fromArray('post_id', SqlBinding::fromArray($postIds))); + $stmt->setCriterion(SqlInFunctor::fromArray('post_id', SqlBinding::fromArray($postIds))); $rows = Database::fetchAll($stmt); foreach ($rows as $row) @@ -234,8 +234,8 @@ class PostModel extends AbstractCrudModel $stmt->setTable('tag'); $stmt->addColumn('tag.*'); $stmt->addColumn('post_id'); - $stmt->addInnerJoin('post_tag', new SqlEqualsOperator('post_tag.tag_id', 'tag.id')); - $stmt->setCriterion(SqlInOperator::fromArray('post_id', SqlBinding::fromArray($postIds))); + $stmt->addInnerJoin('post_tag', new SqlEqualsFunctor('post_tag.tag_id', 'tag.id')); + $stmt->setCriterion(SqlInFunctor::fromArray('post_id', SqlBinding::fromArray($postIds))); $rows = Database::fetchAll($stmt); foreach ($rows as $row) diff --git a/src/Models/PropertyModel.php b/src/Models/PropertyModel.php index 67e9754a..5a3978c6 100644 --- a/src/Models/PropertyModel.php +++ b/src/Models/PropertyModel.php @@ -44,13 +44,13 @@ class PropertyModel implements IModel $stmt = new SqlSelectStatement(); $stmt->setColumn('id'); $stmt->setTable('property'); - $stmt->setCriterion(new SqlEqualsOperator('prop_id', new SqlBinding($propertyId))); + $stmt->setCriterion(new SqlEqualsFunctor('prop_id', new SqlBinding($propertyId))); $row = Database::fetchOne($stmt); if ($row) { $stmt = new SqlUpdateStatement(); - $stmt->setCriterion(new SqlEqualsOperator('prop_id', new SqlBinding($propertyId))); + $stmt->setCriterion(new SqlEqualsFunctor('prop_id', new SqlBinding($propertyId))); } else { diff --git a/src/Models/SearchParsers/CommentSearchParser.php b/src/Models/SearchParsers/CommentSearchParser.php index 7eb5892b..c987a3a4 100644 --- a/src/Models/SearchParsers/CommentSearchParser.php +++ b/src/Models/SearchParsers/CommentSearchParser.php @@ -3,13 +3,13 @@ class CommentSearchParser extends AbstractSearchParser { protected function processSetup(&$tokens) { - $this->statement->addInnerJoin('post', new SqlEqualsOperator('post_id', 'post.id')); + $this->statement->addInnerJoin('post', new SqlEqualsFunctor('post_id', 'post.id')); $allowedSafety = PrivilegesHelper::getAllowedSafety(); - $this->statement->setCriterion(new SqlConjunction()); - $this->statement->getCriterion()->add(SqlInOperator::fromArray('post.safety', SqlBinding::fromArray($allowedSafety))); + $this->statement->setCriterion(new SqlConjunctionFunctor()); + $this->statement->getCriterion()->add(SqlInFunctor::fromArray('post.safety', SqlBinding::fromArray($allowedSafety))); if (!PrivilegesHelper::confirm(Privilege::ListPosts, 'hidden')) - $this->statement->getCriterion()->add(new SqlNegationOperator(new SqlStringExpression('hidden'))); + $this->statement->getCriterion()->add(new SqlNegationFunctor(new SqlStringExpression('hidden'))); $this->statement->addOrderBy('comment.id', SqlSelectStatement::ORDER_DESC); } diff --git a/src/Models/SearchParsers/PostSearchParser.php b/src/Models/SearchParsers/PostSearchParser.php index 38e59a31..9586f92e 100644 --- a/src/Models/SearchParsers/PostSearchParser.php +++ b/src/Models/SearchParsers/PostSearchParser.php @@ -8,10 +8,10 @@ class PostSearchParser extends AbstractSearchParser $config = \Chibi\Registry::getConfig(); $this->tags = []; - $this->statement->setCriterion(new SqlConjunction()); + $this->statement->setCriterion(new SqlConjunctionFunctor()); $allowedSafety = PrivilegesHelper::getAllowedSafety(); - $this->statement->getCriterion()->add(SqlInOperator::fromArray('safety', SqlBinding::fromArray($allowedSafety))); + $this->statement->getCriterion()->add(SqlInFunctor::fromArray('safety', SqlBinding::fromArray($allowedSafety))); if (\Chibi\Registry::getContext()->user->hasEnabledHidingDislikedPosts() and !in_array('special:disliked', array_map('strtolower', $tokens))) $this->processComplexToken('special', 'disliked', true); @@ -31,12 +31,12 @@ class PostSearchParser extends AbstractSearchParser $tag = TagModel::findByName($tagName); $innerStmt = new SqlSelectStatement(); $innerStmt->setTable('post_tag'); - $innerStmt->setCriterion((new SqlConjunction) - ->add(new SqlEqualsOperator('post_tag.post_id', 'post.id')) - ->add(new SqlEqualsOperator('post_tag.tag_id', new SqlBinding($tag->id)))); - $operator = new SqlExistsOperator($innerStmt); + $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); if ($neg) - $operator = new SqlNegationOperator($operator); + $operator = new SqlNegationFunctor($operator); $this->statement->getCriterion()->add($operator); } @@ -58,7 +58,7 @@ class PostSearchParser extends AbstractSearchParser { $ids = preg_split('/[;,]/', $value); $ids = array_map('intval', $ids); - return SqlInOperator::fromArray('post.id', SqlBinding::fromArray($ids)); + return SqlInFunctor::fromArray('post.id', SqlBinding::fromArray($ids)); } elseif (in_array($key, ['fav', 'favs'])) @@ -66,10 +66,10 @@ class PostSearchParser extends AbstractSearchParser $user = UserModel::findByNameOrEmail($value); $innerStmt = (new SqlSelectStatement) ->setTable('favoritee') - ->setCriterion((new SqlConjunction) - ->add(new SqlEqualsOperator('favoritee.post_id', 'post.id')) - ->add(new SqlEqualsOperator('favoritee.user_id', new SqlBinding($user->id)))); - return new SqlExistsOperator($innerStmt); + ->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); } elseif (in_array($key, ['comment', 'commenter'])) @@ -77,66 +77,66 @@ class PostSearchParser extends AbstractSearchParser $user = UserModel::findByNameOrEmail($value); $innerStmt = (new SqlSelectStatement) ->setTable('comment') - ->setCriterion((new SqlConjunction) - ->add(new SqlEqualsOperator('comment.post_id', 'post.id')) - ->add(new SqlEqualsOperator('comment.commenter_id', new SqlBinding($user->id)))); - return new SqlExistsOperator($innerStmt); + ->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); } elseif (in_array($key, ['submit', 'upload', 'uploader', 'uploaded'])) { $user = UserModel::findByNameOrEmail($value); - return new SqlEqualsOperator('uploader_id', new SqlBinding($user->id)); + return new SqlEqualsFunctor('uploader_id', new SqlBinding($user->id)); } elseif (in_array($key, ['idmin', 'id_min'])) - return new SqlEqualsOrGreaterOperator('post.id', new SqlBinding(intval($value))); + return new SqlEqualsOrGreaterFunctor('post.id', new SqlBinding(intval($value))); elseif (in_array($key, ['idmax', 'id_max'])) - return new SqlEqualsOrLesserOperator('post.id', new SqlBinding(intval($value))); + return new SqlEqualsOrLesserFunctor('post.id', new SqlBinding(intval($value))); elseif (in_array($key, ['scoremin', 'score_min'])) - return new SqlEqualsOrGreaterOperator('score', new SqlBinding(intval($value))); + return new SqlEqualsOrGreaterFunctor('score', new SqlBinding(intval($value))); elseif (in_array($key, ['scoremax', 'score_max'])) - return new SqlEqualsOrLesserOperator('score', new SqlBinding(intval($value))); + return new SqlEqualsOrLesserFunctor('score', new SqlBinding(intval($value))); elseif (in_array($key, ['tagmin', 'tag_min'])) - return new SqlEqualsOrGreaterOperator('tag_count', new SqlBinding(intval($value))); + return new SqlEqualsOrGreaterFunctor('tag_count', new SqlBinding(intval($value))); elseif (in_array($key, ['tagmax', 'tag_max'])) - return new SqlEqualsOrLesserOperator('tag_count', new SqlBinding(intval($value))); + return new SqlEqualsOrLesserFunctor('tag_count', new SqlBinding(intval($value))); elseif (in_array($key, ['favmin', 'fav_min'])) - return new SqlEqualsOrGreaterOperator('fav_count', new SqlBinding(intval($value))); + return new SqlEqualsOrGreaterFunctor('fav_count', new SqlBinding(intval($value))); elseif (in_array($key, ['favmax', 'fav_max'])) - return new SqlEqualsOrLesserOperator('fav_count', new SqlBinding(intval($value))); + return new SqlEqualsOrLesserFunctor('fav_count', new SqlBinding(intval($value))); elseif (in_array($key, ['commentmin', 'comment_min'])) - return new SqlEqualsOrGreaterOperator('comment_count', new SqlBinding(intval($value))); + return new SqlEqualsOrGreaterFunctor('comment_count', new SqlBinding(intval($value))); elseif (in_array($key, ['commentmax', 'comment_max'])) - return new SqlEqualsOrLesserOperator('comment_count', new SqlBinding(intval($value))); + return new SqlEqualsOrLesserFunctor('comment_count', new SqlBinding(intval($value))); elseif (in_array($key, ['date'])) { list ($dateMin, $dateMax) = self::parseDate($value); - return (new SqlConjunction) - ->add(new SqlEqualsOrLesserOperator('upload_date', new SqlBinding($dateMax))) - ->add(new SqlEqualsOrGreaterOperator('upload_date', new SqlBinding($dateMin))); + return (new SqlConjunctionFunctor) + ->add(new SqlEqualsOrLesserFunctor('upload_date', new SqlBinding($dateMax))) + ->add(new SqlEqualsOrGreaterFunctor('upload_date', new SqlBinding($dateMin))); } elseif (in_array($key, ['datemin', 'date_min'])) { list ($dateMin, $dateMax) = self::parseDate($value); - return new SqlEqualsOrGreaterOperator('upload_date', new SqlBinding($dateMin)); + return new SqlEqualsOrGreaterFunctor('upload_date', new SqlBinding($dateMin)); } elseif (in_array($key, ['datemax', 'date_max'])) { list ($dateMin, $dateMax) = self::parseDate($value); - return new SqlEqualsOrLesserOperator('upload_date', new SqlBinding($dateMax)); + return new SqlEqualsOrLesserFunctor('upload_date', new SqlBinding($dateMax)); } elseif ($key == 'special') @@ -147,22 +147,22 @@ class PostSearchParser extends AbstractSearchParser { if (!$this->statement->isTableJoined('post_score')) { - $this->statement->addLeftOuterJoin('post_score', (new SqlConjunction) - ->add(new SqlEqualsOperator('post_score.post_id', 'post.id')) - ->add(new SqlEqualsOperator('post_score.user_id', new SqlBinding($context->user->id)))); + $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)))); } - return new SqlEqualsOperator(new SqlIfNullOperator('post_score.score', '0'), '1'); + return new SqlEqualsFunctor(new SqlIfNullFunctor('post_score.score', '0'), '1'); } elseif (in_array($value, ['disliked', 'dislikes'])) { if (!$this->statement->isTableJoined('post_score')) { - $this->statement->addLeftOuterJoin('post_score', (new SqlConjunction) - ->add(new SqlEqualsOperator('post_score.post_id', 'post.id')) - ->add(new SqlEqualsOperator('post_score.user_id', new SqlBinding($context->user->id)))); + $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)))); } - return new SqlEqualsOperator(new SqlIfNullOperator('post_score.score', '0'), '-1'); + return new SqlEqualsFunctor(new SqlIfNullFunctor('post_score.score', '0'), '-1'); } elseif ($value == 'hidden') @@ -184,7 +184,7 @@ class PostSearchParser extends AbstractSearchParser else throw new SimpleException('Invalid post type: ' . $value); - return new SqlEqualsOperator('type', new SqlBinding($type)); + return new SqlEqualsFunctor('type', new SqlBinding($type)); } return null; @@ -197,7 +197,7 @@ class PostSearchParser extends AbstractSearchParser return false; if ($neg) - $criterion = new SqlNegationOperator($criterion); + $criterion = new SqlNegationFunctor($criterion); $this->statement->getCriterion()->add($criterion); return true; @@ -239,7 +239,9 @@ class PostSearchParser extends AbstractSearchParser if (!isset($_SESSION['browsing-seed'])) $_SESSION['browsing-seed'] = mt_rand(); $seed = $_SESSION['browsing-seed']; - $orderColumn = 'SUBSTR(post.id * ' . $seed .', LENGTH(post.id) + 2)'; + $orderColumn = new SqlSubstrFunctor( + new SqlMultiplicationFunctor('post.id', $seed), + new SqlAdditionFunctor(new SqlLengthFunctor('post.id'), '2')); } else diff --git a/src/Models/SearchParsers/TagSearchParser.php b/src/Models/SearchParsers/TagSearchParser.php index 34b984e0..4998a821 100644 --- a/src/Models/SearchParsers/TagSearchParser.php +++ b/src/Models/SearchParsers/TagSearchParser.php @@ -5,9 +5,9 @@ class TagSearchParser extends AbstractSearchParser { $allowedSafety = PrivilegesHelper::getAllowedSafety(); $this->statement - ->addInnerJoin('post_tag', new SqlEqualsOperator('tag.id', 'post_tag.tag_id')) - ->addInnerJoin('post', new SqlEqualsOperator('post.id', 'post_tag.post_id')) - ->setCriterion((new SqlConjunction)->add(SqlInOperator::fromArray('safety', SqlBinding::fromArray($allowedSafety)))) + ->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)))) ->setGroupBy('tag.id'); } @@ -20,7 +20,7 @@ class TagSearchParser extends AbstractSearchParser $value = '%' . $value; $value .= '%'; - $this->statement->getCriterion()->add(new SqlNoCaseOperator(new SqlLikeOperator('tag.name', new SqlBinding($value)))); + $this->statement->getCriterion()->add(new SqlNoCaseFunctor(new SqlLikeFunctor('tag.name', new SqlBinding($value)))); return true; } diff --git a/src/Models/SearchParsers/UserSearchParser.php b/src/Models/SearchParsers/UserSearchParser.php index cc5bc08d..73cae6dd 100644 --- a/src/Models/SearchParsers/UserSearchParser.php +++ b/src/Models/SearchParsers/UserSearchParser.php @@ -8,9 +8,9 @@ class UserSearchParser extends AbstractSearchParser if ($value == 'pending') { - $this->statement->setCriterion((new SqlDisjunction) - ->add(new SqlIsNullOperator('staff_confirmed')) - ->add(new SqlEqualsOperator('staff_confirmed', '0'))); + $this->statement->setCriterion((new SqlDisjunctionFunctor) + ->add(new SqlIsFunctor('staff_confirmed', new SqlNullFunctor())) + ->add(new SqlEqualsFunctor('staff_confirmed', '0'))); return true; } return false; @@ -19,7 +19,7 @@ class UserSearchParser extends AbstractSearchParser protected function processOrderToken($orderByString, $orderDir) { if ($orderByString == 'alpha') - $this->statement->setOrderBy(new SqlNoCaseOperator('name'), $orderDir); + $this->statement->setOrderBy(new SqlNoCaseFunctor('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 9a46b52a..303af09a 100644 --- a/src/Models/SearchServices/AbstractSearchService.php +++ b/src/Models/SearchServices/AbstractSearchService.php @@ -68,7 +68,7 @@ abstract class AbstractSearchService $innerStmt->resetOrderBy(); $stmt = new SqlSelectStatement(); - $stmt->setColumn(new SqlAliasOperator(new SqlCountOperator('1'), 'count')); + $stmt->setColumn(new SqlAliasFunctor(new SqlCountFunctor('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 098f17eb..39b5787b 100644 --- a/src/Models/SearchServices/PostSearchService.php +++ b/src/Models/SearchServices/PostSearchService.php @@ -24,7 +24,7 @@ class PostSearchService extends AbstractSearchService $stmt = new SqlSelectStatement(); $stmt->setTable('post_search'); $stmt->setColumn('id'); - $stmt->setCriterion(new SqlEqualsOperator('post_id', new SqlBinding($postId))); + $stmt->setCriterion(new SqlEqualsFunctor('post_id', new SqlBinding($postId))); $rowId = Database::fetchOne($stmt)['id']; //it's possible that given post won't show in search results: @@ -35,10 +35,10 @@ class PostSearchService extends AbstractSearchService $rowId = intval($rowId); $stmt->setColumn('post_id'); - $stmt->setCriterion(new SqlEqualsOperator('id', new SqlBinding($rowId - 1))); + $stmt->setCriterion(new SqlEqualsFunctor('id', new SqlBinding($rowId - 1))); $nextPostId = Database::fetchOne($stmt)['post_id']; - $stmt->setCriterion(new SqlEqualsOperator('id', new SqlBinding($rowId + 1))); + $stmt->setCriterion(new SqlEqualsFunctor('id', new SqlBinding($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 0f79971a..8dd01040 100644 --- a/src/Models/SearchServices/TagSearchService.php +++ b/src/Models/SearchServices/TagSearchService.php @@ -3,6 +3,6 @@ class TagSearchService extends AbstractSearchService { public static function decorateCustom(SqlSelectStatement $stmt) { - $stmt->addColumn(new SqlAliasOperator(new SqlCountOperator('post_tag.post_id'), 'post_count')); + $stmt->addColumn(new SqlAliasFunctor(new SqlCountFunctor('post_tag.post_id'), 'post_count')); } } diff --git a/src/Models/TagModel.php b/src/Models/TagModel.php index e9972395..32dba6f3 100644 --- a/src/Models/TagModel.php +++ b/src/Models/TagModel.php @@ -15,7 +15,7 @@ class TagModel extends AbstractCrudModel $stmt = new SqlUpdateStatement(); $stmt->setTable('tag'); $stmt->setColumn('name', new SqlBinding($tag->name)); - $stmt->setCriterion(new SqlEqualsOperator('id', new SqlBinding($tag->id))); + $stmt->setCriterion(new SqlEqualsFunctor('id', new SqlBinding($tag->id))); Database::exec($stmt); }); @@ -28,12 +28,12 @@ class TagModel extends AbstractCrudModel $stmt = new SqlDeleteStatement(); $stmt->setTable('post_tag'); - $stmt->setCriterion(new SqlEqualsOperator('tag_id', $binding)); + $stmt->setCriterion(new SqlEqualsFunctor('tag_id', $binding)); Database::exec($stmt); $stmt = new SqlDeleteStatement(); $stmt->setTable('tag'); - $stmt->setCriterion(new SqlEqualsOperator('id', $binding)); + $stmt->setCriterion(new SqlEqualsFunctor('id', $binding)); Database::exec($stmt); } @@ -66,24 +66,24 @@ class TagModel extends AbstractCrudModel $stmt->setColumn('post.id'); $stmt->setTable('post'); $stmt->setCriterion( - (new SqlConjunction) + (new SqlConjunctionFunctor) ->add( - new SqlExistsOperator( + new SqlExistsFunctor( (new SqlSelectStatement) ->setTable('post_tag') ->setCriterion( - (new SqlConjunction) - ->add(new SqlEqualsOperator('post_tag.post_id', 'post.id')) - ->add(new SqlEqualsOperator('post_tag.tag_id', new SqlBinding($sourceTag->id)))))) + (new SqlConjunctionFunctor) + ->add(new SqlEqualsFunctor('post_tag.post_id', 'post.id')) + ->add(new SqlEqualsFunctor('post_tag.tag_id', new SqlBinding($sourceTag->id)))))) ->add( - new SqlNegationOperator( - new SqlExistsOperator( + new SqlNegationFunctor( + new SqlExistsFunctor( (new SqlSelectStatement) ->setTable('post_tag') ->setCriterion( - (new SqlConjunction) - ->add(new SqlEqualsOperator('post_tag.post_id', 'post.id')) - ->add(new SqlEqualsOperator('post_tag.tag_id', new SqlBinding($targetTag->id)))))))); + (new SqlConjunctionFunctor) + ->add(new SqlEqualsFunctor('post_tag.post_id', 'post.id')) + ->add(new SqlEqualsFunctor('post_tag.tag_id', new SqlBinding($targetTag->id)))))))); $rows = Database::fetchAll($stmt); $postIds = array_map(function($row) { return $row['id']; }, $rows); @@ -106,8 +106,8 @@ class TagModel extends AbstractCrudModel $stmt = new SqlSelectStatement(); $stmt->setColumn('tag.*'); $stmt->setTable('tag'); - $stmt->addInnerJoin('post_tag', new SqlEqualsOperator('post_tag.tag_id', 'tag.id')); - $stmt->setCriterion(new SqlEqualsOperator('post_tag.post_id', new SqlBinding($key))); + $stmt->addInnerJoin('post_tag', new SqlEqualsFunctor('post_tag.tag_id', 'tag.id')); + $stmt->setCriterion(new SqlEqualsFunctor('post_tag.post_id', new SqlBinding($key))); $rows = Database::fetchAll($stmt); if ($rows) @@ -120,7 +120,7 @@ class TagModel extends AbstractCrudModel $stmt = new SqlSelectStatement(); $stmt->setColumn('tag.*'); $stmt->setTable('tag'); - $stmt->setCriterion(new SqlNoCaseOperator(new SqlEqualsOperator('name', new SqlBinding($key)))); + $stmt->setCriterion(new SqlNoCaseFunctor(new SqlEqualsFunctor('name', new SqlBinding($key)))); $row = Database::fetchOne($stmt); if ($row) @@ -138,11 +138,11 @@ class TagModel extends AbstractCrudModel $stmt = (new SqlDeleteStatement) ->setTable('tag') ->setCriterion( - new SqlNegationOperator( - new SqlExistsOperator( + new SqlNegationFunctor( + new SqlExistsFunctor( (new SqlSelectStatement) ->setTable('post_tag') - ->setCriterion(new SqlEqualsOperator('post_tag.tag_id', 'tag.id'))))); + ->setCriterion(new SqlEqualsFunctor('post_tag.tag_id', 'tag.id'))))); Database::exec($stmt); } diff --git a/src/Models/TokenModel.php b/src/Models/TokenModel.php index 26434576..e086af30 100644 --- a/src/Models/TokenModel.php +++ b/src/Models/TokenModel.php @@ -22,7 +22,7 @@ implements IModel $stmt = new SqlUpdateStatement(); $stmt->setTable('user_token'); - $stmt->setCriterion(new SqlEqualsOperator('id', new SqlBinding($token->id))); + $stmt->setCriterion(new SqlEqualsFunctor('id', new SqlBinding($token->id))); foreach ($bindings as $key => $val) $stmt->setColumn($key, new SqlBinding($val)); @@ -42,7 +42,7 @@ implements IModel $stmt = new SqlSelectStatement(); $stmt->setTable('user_token'); $stmt->setColumn('*'); - $stmt->setCriterion(new SqlEqualsOperator('token', new SqlBinding($key))); + $stmt->setCriterion(new SqlEqualsFunctor('token', new SqlBinding($key))); $row = Database::fetchOne($stmt); if ($row) diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index 31e5ad46..10599c33 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -42,7 +42,7 @@ class UserModel extends AbstractCrudModel $stmt = (new SqlUpdateStatement) ->setTable('user') - ->setCriterion(new SqlEqualsOperator('id', new SqlBinding($user->id))); + ->setCriterion(new SqlEqualsFunctor('id', new SqlBinding($user->id))); foreach ($bindings as $key => $val) $stmt->setColumn($key, new SqlBinding($val)); @@ -59,26 +59,26 @@ class UserModel extends AbstractCrudModel $stmt = new SqlDeleteStatement(); $stmt->setTable('post_score'); - $stmt->setCriterion(new SqlEqualsOperator('user_id', $binding)); + $stmt->setCriterion(new SqlEqualsFunctor('user_id', $binding)); Database::exec($stmt); $stmt->setTable('favoritee'); Database::exec($stmt); $stmt->setTable('user'); - $stmt->setCriterion(new SqlEqualsOperator('id', $binding)); + $stmt->setCriterion(new SqlEqualsFunctor('id', $binding)); Database::exec($stmt); $stmt = new SqlUpdateStatement(); $stmt->setTable('comment'); - $stmt->setCriterion(new SqlEqualsOperator('commenter_id', $binding)); - $stmt->setColumn('commenter_id', new SqlNullOperator()); + $stmt->setCriterion(new SqlEqualsFunctor('commenter_id', $binding)); + $stmt->setColumn('commenter_id', new SqlNullFunctor()); Database::exec($stmt); $stmt = new SqlUpdateStatement(); $stmt->setTable('post'); - $stmt->setCriterion(new SqlEqualsOperator('uploader_id', $binding)); - $stmt->setColumn('uploader_id', new SqlNullOperator()); + $stmt->setCriterion(new SqlEqualsFunctor('uploader_id', $binding)); + $stmt->setColumn('uploader_id', new SqlNullFunctor()); Database::exec($stmt); }); } @@ -90,7 +90,7 @@ class UserModel extends AbstractCrudModel $stmt = new SqlSelectStatement(); $stmt->setColumn('*'); $stmt->setTable('user'); - $stmt->setCriterion(new SqlNoCaseOperator(new SqlEqualsOperator('name', new SqlBinding(trim($key))))); + $stmt->setCriterion(new SqlNoCaseFunctor(new SqlEqualsFunctor('name', new SqlBinding(trim($key))))); $row = Database::fetchOne($stmt); if ($row) @@ -106,9 +106,9 @@ class UserModel extends AbstractCrudModel $stmt = new SqlSelectStatement(); $stmt->setColumn('*'); $stmt->setTable('user'); - $stmt->setCriterion((new SqlDisjunction) - ->add(new SqlNoCaseOperator(new SqlEqualsOperator('name', new SqlBinding(trim($key))))) - ->add(new SqlNoCaseOperator(new SqlEqualsOperator('email_confirmed', new SqlBinding(trim($key)))))); + $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)))))); $row = Database::fetchOne($stmt); if ($row) @@ -127,9 +127,9 @@ class UserModel extends AbstractCrudModel { $stmt = new SqlDeleteStatement(); $stmt->setTable('post_score'); - $stmt->setCriterion((new SqlConjunction) - ->add(new SqlEqualsOperator('post_id', new SqlBinding($post->id))) - ->add(new SqlEqualsOperator('user_id', new SqlBinding($user->id)))); + $stmt->setCriterion((new SqlConjunctionFunctor) + ->add(new SqlEqualsFunctor('post_id', new SqlBinding($post->id))) + ->add(new SqlEqualsFunctor('user_id', new SqlBinding($user->id)))); Database::exec($stmt); $score = intval($score); if ($score != 0) @@ -163,9 +163,9 @@ class UserModel extends AbstractCrudModel { $stmt = new SqlDeleteStatement(); $stmt->setTable('favoritee'); - $stmt->setCriterion((new SqlConjunction) - ->add(new SqlEqualsOperator('post_id', new SqlBinding($post->id))) - ->add(new SqlEqualsOperator('user_id', new SqlBinding($user->id)))); + $stmt->setCriterion((new SqlConjunctionFunctor) + ->add(new SqlEqualsFunctor('post_id', new SqlBinding($post->id))) + ->add(new SqlEqualsFunctor('user_id', new SqlBinding($user->id)))); Database::exec($stmt); }); } diff --git a/src/Sql/Functors/BinaryOperatorFunctors/SqlAdditionFunctor.php b/src/Sql/Functors/BinaryOperatorFunctors/SqlAdditionFunctor.php new file mode 100644 index 00000000..25db722c --- /dev/null +++ b/src/Sql/Functors/BinaryOperatorFunctors/SqlAdditionFunctor.php @@ -0,0 +1,8 @@ +subjects[0]) + . ' ' . $this->getOperator() + . ' ' . $this->subjects[1]->getAsString(); + } +} diff --git a/src/Sql/Functors/BinaryOperatorFunctors/SqlDivisionFunctor.php b/src/Sql/Functors/BinaryOperatorFunctors/SqlDivisionFunctor.php new file mode 100644 index 00000000..85bf0fa3 --- /dev/null +++ b/src/Sql/Functors/BinaryOperatorFunctors/SqlDivisionFunctor.php @@ -0,0 +1,8 @@ +='; + } +} diff --git a/src/Sql/Functors/BinaryOperatorFunctors/SqlEqualsOrLesserFunctor.php b/src/Sql/Functors/BinaryOperatorFunctors/SqlEqualsOrLesserFunctor.php new file mode 100644 index 00000000..8be695e5 --- /dev/null +++ b/src/Sql/Functors/BinaryOperatorFunctors/SqlEqualsOrLesserFunctor.php @@ -0,0 +1,8 @@ +'; + } +} diff --git a/src/Sql/Functors/BinaryOperatorFunctors/SqlIsFunctor.php b/src/Sql/Functors/BinaryOperatorFunctors/SqlIsFunctor.php new file mode 100644 index 00000000..ee0174ad --- /dev/null +++ b/src/Sql/Functors/BinaryOperatorFunctors/SqlIsFunctor.php @@ -0,0 +1,8 @@ +subjects[0]->getAsString() . ' COLLATE NOCASE'; + } +} diff --git a/src/Sql/Functors/FunctionFunctors/SqlRandomFunctor.php b/src/Sql/Functors/FunctionFunctors/SqlRandomFunctor.php new file mode 100644 index 00000000..7b8416d2 --- /dev/null +++ b/src/Sql/Functors/FunctionFunctors/SqlRandomFunctor.php @@ -0,0 +1,16 @@ +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 new file mode 100644 index 00000000..da3ac0c7 --- /dev/null +++ b/src/Sql/Functors/FunctionFunctors/SqlSubstrFunctor.php @@ -0,0 +1,13 @@ +getAsString(); + return self::surroundBraces($subject); }, $this->subjects)) . ')'; } } diff --git a/src/Sql/Operators/VariableOperators/SqlDisjunction.php b/src/Sql/Functors/MiscOperators/SqlDisjunctionFunctor.php similarity index 67% rename from src/Sql/Operators/VariableOperators/SqlDisjunction.php rename to src/Sql/Functors/MiscOperators/SqlDisjunctionFunctor.php index e7c519b2..bcab3055 100644 --- a/src/Sql/Operators/VariableOperators/SqlDisjunction.php +++ b/src/Sql/Functors/MiscOperators/SqlDisjunctionFunctor.php @@ -1,5 +1,5 @@ getAsString(); + return self::surroundBraces($subject); }, $this->subjects)) . ')'; } } diff --git a/src/Sql/Operators/VariableOperators/SqlInOperator.php b/src/Sql/Functors/MiscOperators/SqlInFunctor.php similarity index 66% rename from src/Sql/Operators/VariableOperators/SqlInOperator.php rename to src/Sql/Functors/MiscOperators/SqlInFunctor.php index 95c576f6..a34cabc7 100644 --- a/src/Sql/Operators/VariableOperators/SqlInOperator.php +++ b/src/Sql/Functors/MiscOperators/SqlInFunctor.php @@ -1,5 +1,5 @@ subject->getAsString() . ') IN (' . join(', ', array_map(function($subject) + return self::surroundBraces($this->subject) . ' IN (' . join(', ', array_map(function($subject) { - return $subject->getAsString(); + return self::surroundBraces($subject); }, $this->subjects)) . ')'; } } diff --git a/src/Sql/Operators/NullaryOperators/SqlNullOperator.php b/src/Sql/Functors/MiscOperators/SqlNullFunctor.php similarity index 56% rename from src/Sql/Operators/NullaryOperators/SqlNullOperator.php rename to src/Sql/Functors/MiscOperators/SqlNullFunctor.php index 9c6ca728..1eed354b 100644 --- a/src/Sql/Operators/NullaryOperators/SqlNullOperator.php +++ b/src/Sql/Functors/MiscOperators/SqlNullFunctor.php @@ -1,5 +1,5 @@ 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 new file mode 100644 index 00000000..73df8dfa --- /dev/null +++ b/src/Sql/Functors/SqlFunctionFunctor.php @@ -0,0 +1,30 @@ +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 new file mode 100644 index 00000000..26dba8fc --- /dev/null +++ b/src/Sql/Functors/SqlFunctor.php @@ -0,0 +1,4 @@ +subject->getAsString() . ') ' . $this->operator . ' ' . $this->target->getAsString(); - } -} diff --git a/src/Sql/Operators/BinaryOperators/SqlEqualsOperator.php b/src/Sql/Operators/BinaryOperators/SqlEqualsOperator.php deleted file mode 100644 index 19f3c3e0..00000000 --- a/src/Sql/Operators/BinaryOperators/SqlEqualsOperator.php +++ /dev/null @@ -1,8 +0,0 @@ -='); - } -} diff --git a/src/Sql/Operators/BinaryOperators/SqlEqualsOrLesserOperator.php b/src/Sql/Operators/BinaryOperators/SqlEqualsOrLesserOperator.php deleted file mode 100644 index c98b6b03..00000000 --- a/src/Sql/Operators/BinaryOperators/SqlEqualsOrLesserOperator.php +++ /dev/null @@ -1,8 +0,0 @@ -'); - } -} diff --git a/src/Sql/Operators/BinaryOperators/SqlLesserOperator.php b/src/Sql/Operators/BinaryOperators/SqlLesserOperator.php deleted file mode 100644 index eda21662..00000000 --- a/src/Sql/Operators/BinaryOperators/SqlLesserOperator.php +++ /dev/null @@ -1,8 +0,0 @@ -subject = $this->attachExpression($subject); - $this->target = $this->attachExpression($target); - } - - public function getAsString() - { - return 'IFNULL (' . $this->subject->getAsString() . ', ' . $this->target->getAsString() . ')'; - } -} diff --git a/src/Sql/Operators/NullaryOperators/SqlRandomOperator.php b/src/Sql/Operators/NullaryOperators/SqlRandomOperator.php deleted file mode 100644 index ad70eba2..00000000 --- a/src/Sql/Operators/NullaryOperators/SqlRandomOperator.php +++ /dev/null @@ -1,11 +0,0 @@ -main->dbDriver == 'sqlite' - ? 'RANDOM()' - : 'RAND()'; - } -} diff --git a/src/Sql/Operators/SqlBinaryOperator.php b/src/Sql/Operators/SqlBinaryOperator.php deleted file mode 100644 index ae411392..00000000 --- a/src/Sql/Operators/SqlBinaryOperator.php +++ /dev/null @@ -1,19 +0,0 @@ -subject = $this->attachExpression($subject); - $this->target = $this->attachExpression($target); - $this->operator = $operator; - } - - public function getAsString() - { - return '(' . $this->subject->getAsString() . ') ' . $this->operator . ' (' . $this->target->getAsString() . ')'; - } -} diff --git a/src/Sql/Operators/SqlNullaryOperator.php b/src/Sql/Operators/SqlNullaryOperator.php deleted file mode 100644 index 5b770acc..00000000 --- a/src/Sql/Operators/SqlNullaryOperator.php +++ /dev/null @@ -1,4 +0,0 @@ -subject = $this->attachExpression($subject); - } - - public function getAsString() - { - if (empty($this->subject->getAsString())) - return $this->getAsStringEmpty(); - - return $this->getAsStringNonEmpty(); - } - - public function getAsStringEmpty() - { - return ''; - } - - public abstract function getAsStringNonEmpty(); -} diff --git a/src/Sql/Operators/UnaryOperators/SqlAbsOperator.php b/src/Sql/Operators/UnaryOperators/SqlAbsOperator.php deleted file mode 100644 index 09f60e34..00000000 --- a/src/Sql/Operators/UnaryOperators/SqlAbsOperator.php +++ /dev/null @@ -1,8 +0,0 @@ -subject->getAsString() . ')'; - } -} diff --git a/src/Sql/Operators/UnaryOperators/SqlCountOperator.php b/src/Sql/Operators/UnaryOperators/SqlCountOperator.php deleted file mode 100644 index 3a6ad805..00000000 --- a/src/Sql/Operators/UnaryOperators/SqlCountOperator.php +++ /dev/null @@ -1,8 +0,0 @@ -subject->getAsString() . ')'; - } -} diff --git a/src/Sql/Operators/UnaryOperators/SqlExistsOperator.php b/src/Sql/Operators/UnaryOperators/SqlExistsOperator.php deleted file mode 100644 index e2e05739..00000000 --- a/src/Sql/Operators/UnaryOperators/SqlExistsOperator.php +++ /dev/null @@ -1,8 +0,0 @@ -subject->getAsString() . ')'; - } -} diff --git a/src/Sql/Operators/UnaryOperators/SqlIsNullOperator.php b/src/Sql/Operators/UnaryOperators/SqlIsNullOperator.php deleted file mode 100644 index 6576f1a0..00000000 --- a/src/Sql/Operators/UnaryOperators/SqlIsNullOperator.php +++ /dev/null @@ -1,8 +0,0 @@ -subject->getAsString() . ') IS NULL'; - } -} diff --git a/src/Sql/Operators/UnaryOperators/SqlMaxOperator.php b/src/Sql/Operators/UnaryOperators/SqlMaxOperator.php deleted file mode 100644 index 0b9a12b3..00000000 --- a/src/Sql/Operators/UnaryOperators/SqlMaxOperator.php +++ /dev/null @@ -1,8 +0,0 @@ -subject->getAsString() . ')'; - } -} diff --git a/src/Sql/Operators/UnaryOperators/SqlNegationOperator.php b/src/Sql/Operators/UnaryOperators/SqlNegationOperator.php deleted file mode 100644 index 027bd800..00000000 --- a/src/Sql/Operators/UnaryOperators/SqlNegationOperator.php +++ /dev/null @@ -1,8 +0,0 @@ -subject->getAsString() . ')'; - } -} diff --git a/src/Sql/Operators/UnaryOperators/SqlNoCaseOperator.php b/src/Sql/Operators/UnaryOperators/SqlNoCaseOperator.php deleted file mode 100644 index 724a55f4..00000000 --- a/src/Sql/Operators/UnaryOperators/SqlNoCaseOperator.php +++ /dev/null @@ -1,8 +0,0 @@ -subject->getAsString() . ' COLLATE NOCASE'; - } -} diff --git a/src/Sql/SqlExpression.php b/src/Sql/SqlExpression.php index 8f5b2adc..7eec46d0 100644 --- a/src/Sql/SqlExpression.php +++ b/src/Sql/SqlExpression.php @@ -12,6 +12,13 @@ abstract class SqlExpression return $this; } + protected static function surroundBraces(SqlExpression $object) + { + if ($object instanceof SqlStatement) + return '(' . $object->getAsString() . ')'; + return $object->getAsString(); + } + public function getBindings() { $bindings = $this->bindings; diff --git a/src/Sql/Statements/SqlSelectStatement.php b/src/Sql/Statements/SqlSelectStatement.php index 96b68ba7..8c06b95f 100644 --- a/src/Sql/Statements/SqlSelectStatement.php +++ b/src/Sql/Statements/SqlSelectStatement.php @@ -154,7 +154,7 @@ class SqlSelectStatement extends SqlStatement }, $this->columns)); else $sql .= '1'; - $sql .= ' FROM (' . $this->source->getAsString() . ')'; + $sql .= ' FROM ' . self::surroundBraces($this->source); foreach ($this->innerJoins as $join) {