SQL operator refactor

* Added few new operators that were left hardcoded
* Changed "Operator" to "Functor"
* Better hierarchy - less mess
* Serialized SQL queries should contain fewer braces
This commit is contained in:
Marcin Kurczewski 2014-02-24 21:31:12 +01:00
parent a1378c98b4
commit cb489d1eca
72 changed files with 465 additions and 372 deletions

View file

@ -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;

View file

@ -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'];
}

View file

@ -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)

View file

@ -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);

View file

@ -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'];
}
}

View file

@ -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'];
}
}

View file

@ -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)

View file

@ -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
{

View file

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

View file

@ -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

View file

@ -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;
}

View file

@ -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

View file

@ -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'];

View file

@ -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];

View file

@ -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'));
}
}

View file

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

View file

@ -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)

View file

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

View file

@ -0,0 +1,8 @@
<?php
class SqlAdditionFunctor extends SqlBinaryOperatorFunctor
{
protected function getOperator()
{
return '+';
}
}

View file

@ -0,0 +1,15 @@
<?php
class SqlAliasFunctor extends SqlBinaryOperatorFunctor
{
protected function getOperator()
{
return 'AS';
}
public function getAsString()
{
return self::surroundBraces($this->subjects[0])
. ' ' . $this->getOperator()
. ' ' . $this->subjects[1]->getAsString();
}
}

View file

@ -0,0 +1,8 @@
<?php
class SqlAdditionFunctor extends SqlBinaryOperatorFunctor
{
protected function getOperator()
{
return '/';
}
}

View file

@ -0,0 +1,8 @@
<?php
class SqlEqualsFunctor extends SqlBinaryOperatorFunctor
{
protected function getOperator()
{
return '=';
}
}

View file

@ -0,0 +1,8 @@
<?php
class SqlEqualsOrGreaterFunctor extends SqlBinaryOperatorFunctor
{
protected function getOperator()
{
return '>=';
}
}

View file

@ -0,0 +1,8 @@
<?php
class SqlEqualsOrLesserFunctor extends SqlBinaryOperatorFunctor
{
protected function getOperator()
{
return '<=';
}
}

View file

@ -0,0 +1,8 @@
<?php
class SqlGreaterFunctor extends SqlBinaryOperatorFunctor
{
protected function getOperator()
{
return '>';
}
}

View file

@ -0,0 +1,8 @@
<?php
class SqlIsFunctor extends SqlBinaryOperatorFunctor
{
protected function getOperator()
{
return 'IS';
}
}

View file

@ -0,0 +1,8 @@
<?php
class SqlLesserFunctor extends SqlBinaryOperatorFunctor
{
protected function getOperator()
{
return '<';
}
}

View file

@ -0,0 +1,8 @@
<?php
class SqlLikeFunctor extends SqlBinaryFunctor
{
protected function getOperator()
{
return 'LIKE';
}
}

View file

@ -0,0 +1,8 @@
<?php
class SqlMultiplicationFunctor extends SqlBinaryOperatorFunctor
{
protected function getOperator()
{
return '*';
}
}

View file

@ -0,0 +1,8 @@
<?php
class SqlSubtractionFunctor extends SqlBinaryOperatorFunctor
{
protected function getOperator()
{
return '-';
}
}

View file

@ -0,0 +1,13 @@
<?php
class SqlAbsFunctor extends SqlUnaryFunctor
{
public function getFunctionName()
{
return 'ABS';
}
public function getArgumentCount()
{
return 1;
}
}

View file

@ -0,0 +1,13 @@
<?php
class SqlCountFunctor extends SqlFunctionFunctor
{
public function getFunctionName()
{
return 'COUNT';
}
public function getArgumentCount()
{
return 1;
}
}

View file

@ -0,0 +1,13 @@
<?php
class SqlExistsFunctor extends SqlUnaryFunctor
{
public function getFunctionName()
{
return 'EXISTS';
}
public function getArgumentCount()
{
return 1;
}
}

View file

@ -0,0 +1,13 @@
<?php
class SqlIfNullFunctor extends SqlFunctionFunctor
{
public function getFunctionName()
{
return 'IFNULL';
}
public function getArgumentCount()
{
return 2;
}
}

View file

@ -0,0 +1,13 @@
<?php
class SqlLengthFunctor extends SqlFunctionFunctor
{
public function getFunctionName()
{
return 'LENGTH';
}
public function getArgumentCount()
{
return 1;
}
}

View file

@ -0,0 +1,13 @@
<?php
class SqlMaxFunctor extends SqlUnaryFunctor
{
public function getFunctionName()
{
return 'MAX';
}
public function getArgumentCount()
{
return 1;
}
}

View file

@ -0,0 +1,13 @@
<?php
class SqlNegationFunctor extends SqlFunctionFunctor
{
public function getFunctionName()
{
return 'NOT';
}
public function getArgumentCount()
{
return 1;
}
}

View file

@ -0,0 +1,18 @@
<?php
class SqlNoCaseFunctor extends SqlFunctionFunctor
{
public function getFunctionName()
{
throw new Exception('Not implemented');
}
public function getArgumentCount()
{
return 1;
}
public function getAsString()
{
return $this->subjects[0]->getAsString() . ' COLLATE NOCASE';
}
}

View file

@ -0,0 +1,16 @@
<?php
class SqlRandomFunctor extends SqlFunctionFunctor
{
public function getFunctionName()
{
$config = \Chibi\Registry::getConfig();
return $config->main->dbDriver == 'sqlite'
? 'RANDOM'
: 'RAND';
}
public function getArgumentCount()
{
return 2;
}
}

View file

@ -0,0 +1,13 @@
<?php
class SqlSubstrFunctor extends SqlFunctionFunctor
{
public function getFunctionName()
{
return 'SUBSTR';
}
public function getArgumentCount()
{
return [2, 3];
}
}

View file

@ -1,5 +1,5 @@
<?php
class SqlConjunction extends SqlVariableOperator
class SqlConjunctionFunctor extends SqlVariableFunctor
{
public function getAsStringEmpty()
{
@ -10,7 +10,7 @@ class SqlConjunction extends SqlVariableOperator
{
return '(' . join(' AND ', array_map(function($subject)
{
return $subject->getAsString();
return self::surroundBraces($subject);
}, $this->subjects)) . ')';
}
}

View file

@ -1,5 +1,5 @@
<?php
class SqlDisjunction extends SqlVariableOperator
class SqlDisjunctionFunctor extends SqlVariableFunctor
{
public function getAsStringEmpty()
{
@ -10,7 +10,7 @@ class SqlDisjunction extends SqlVariableOperator
{
return '(' . join(' OR ', array_map(function($subject)
{
return $subject->getAsString();
return self::surroundBraces($subject);
}, $this->subjects)) . ')';
}
}

View file

@ -1,5 +1,5 @@
<?php
class SqlInOperator extends SqlVariableOperator
class SqlInFunctor extends SqlVariableFunctor
{
protected $subject;
@ -15,9 +15,9 @@ class SqlInOperator extends SqlVariableOperator
public function getAsStringNonEmpty()
{
return '(' . $this->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)) . ')';
}
}

View file

@ -1,5 +1,5 @@
<?php
class SqlNullOperator extends SqlNullaryOperator
class SqlNullFunctor extends SqlFunctor
{
public function getAsString()
{

View file

@ -0,0 +1,22 @@
<?php
abstract class SqlBinaryOperatorFunctor extends SqlFunctionFunctor
{
public function getFunctionName()
{
throw new Exception('Not implemented');
}
public function getArgumentCount()
{
return 2;
}
public function getAsString()
{
return self::surroundBraces($this->subjects[0])
. ' ' . $this->getOperator()
. ' ' . self::surroundBraces($this->subjects[1]);
}
protected abstract function getOperator();
}

View file

@ -0,0 +1,30 @@
<?php
abstract class SqlFunctionFunctor extends SqlFunctor
{
protected $subjects;
public function __construct()
{
$subjects = func_get_args();
$expectedArgumentCount = (array) $this->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))
. ')';
}
}

View file

@ -0,0 +1,4 @@
<?php
abstract class SqlFunctor extends SqlExpression
{
}

View file

@ -1,5 +1,5 @@
<?php
abstract class SqlVariableOperator extends SqlOperator
abstract class SqlVariableFunctor extends SqlFunctor
{
protected $subjects;

View file

@ -1,8 +0,0 @@
<?php
class SqlAdditionOperator extends SqlBinaryOperator
{
public function __construct($subject, $target)
{
parent::__construct($subject, $target, '+');
}
}

View file

@ -1,13 +0,0 @@
<?php
class SqlAliasOperator extends SqlBinaryOperator
{
public function __construct($subject, $target)
{
parent::__construct($subject, $target, 'AS');
}
public function getAsString()
{
return '(' . $this->subject->getAsString() . ') ' . $this->operator . ' ' . $this->target->getAsString();
}
}

View file

@ -1,8 +0,0 @@
<?php
class SqlEqualsOperator extends SqlBinaryOperator
{
public function __construct($subject, $target)
{
parent::__construct($subject, $target, '=');
}
}

View file

@ -1,8 +0,0 @@
<?php
class SqlEqualsOrGreaterOperator extends SqlBinaryOperator
{
public function __construct($subject, $target)
{
parent::__construct($subject, $target, '>=');
}
}

View file

@ -1,8 +0,0 @@
<?php
class SqlEqualsOrLesserOperator extends SqlBinaryOperator
{
public function __construct($subject, $target)
{
parent::__construct($subject, $target, '<=');
}
}

View file

@ -1,8 +0,0 @@
<?php
class SqlGreaterOperator extends SqlBinaryOperator
{
public function __construct($subject, $target)
{
parent::__construct($subject, $target, '>');
}
}

View file

@ -1,8 +0,0 @@
<?php
class SqlLesserOperator extends SqlBinaryOperator
{
public function __construct($subject, $target)
{
parent::__construct($subject, $target, '<');
}
}

View file

@ -1,8 +0,0 @@
<?php
class SqlLikeOperator extends SqlBinaryOperator
{
public function __construct($subject, $target)
{
parent::__construct($subject, $target, 'LIKE');
}
}

View file

@ -1,8 +0,0 @@
<?php
class SqlSubtractionOperator extends SqlBinaryOperator
{
public function __construct($subject, $target)
{
parent::__construct($subject, $target, '-');
}
}

View file

@ -1,17 +0,0 @@
<?php
class SqlIfNullOperator extends SqlOperator
{
protected $subject;
protected $target;
public function __construct($subject, $target)
{
$this->subject = $this->attachExpression($subject);
$this->target = $this->attachExpression($target);
}
public function getAsString()
{
return 'IFNULL (' . $this->subject->getAsString() . ', ' . $this->target->getAsString() . ')';
}
}

View file

@ -1,11 +0,0 @@
<?php
class SqlRandomOperator extends SqlNullaryOperator
{
public function getAsString()
{
$config = \Chibi\Registry::getConfig();
return $config->main->dbDriver == 'sqlite'
? 'RANDOM()'
: 'RAND()';
}
}

View file

@ -1,19 +0,0 @@
<?php
class SqlBinaryOperator extends SqlOperator
{
protected $subject;
protected $target;
protected $operator;
public function __construct($subject, $target, $operator)
{
$this->subject = $this->attachExpression($subject);
$this->target = $this->attachExpression($target);
$this->operator = $operator;
}
public function getAsString()
{
return '(' . $this->subject->getAsString() . ') ' . $this->operator . ' (' . $this->target->getAsString() . ')';
}
}

View file

@ -1,4 +0,0 @@
<?php
abstract class SqlNullaryOperator extends SqlOperator
{
}

View file

@ -1,4 +0,0 @@
<?php
abstract class SqlOperator extends SqlExpression
{
}

View file

@ -1,25 +0,0 @@
<?php
abstract class SqlUnaryOperator extends SqlOperator
{
protected $subject;
public function __construct($subject)
{
$this->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();
}

View file

@ -1,8 +0,0 @@
<?php
class SqlAbsOperator extends SqlUnaryOperator
{
public function getAsStringNonEmpty()
{
return 'ABS (' . $this->subject->getAsString() . ')';
}
}

View file

@ -1,8 +0,0 @@
<?php
class SqlCountOperator extends SqlUnaryOperator
{
public function getAsStringNonEmpty()
{
return 'COUNT (' . $this->subject->getAsString() . ')';
}
}

View file

@ -1,8 +0,0 @@
<?php
class SqlExistsOperator extends SqlUnaryOperator
{
public function getAsStringNonEmpty()
{
return 'EXISTS (' . $this->subject->getAsString() . ')';
}
}

View file

@ -1,8 +0,0 @@
<?php
class SqlIsNullOperator extends SqlUnaryOperator
{
public function getAsStringNonEmpty()
{
return '(' . $this->subject->getAsString() . ') IS NULL';
}
}

View file

@ -1,8 +0,0 @@
<?php
class SqlMaxOperator extends SqlUnaryOperator
{
public function getAsStringNonEmpty()
{
return 'MAX (' . $this->subject->getAsString() . ')';
}
}

View file

@ -1,8 +0,0 @@
<?php
class SqlNegationOperator extends SqlUnaryOperator
{
public function getAsStringNonEmpty()
{
return 'NOT (' . $this->subject->getAsString() . ')';
}
}

View file

@ -1,8 +0,0 @@
<?php
class SqlNoCaseOperator extends SqlUnaryOperator
{
public function getAsStringNonEmpty()
{
return $this->subject->getAsString() . ' COLLATE NOCASE';
}
}

View file

@ -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;

View file

@ -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)
{