# Expression.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2004-2007 by Konstantin V. Arkhipov                     *
    00004  *                                                                         *
    00005  *   This program is free software; you can redistribute it and/or modify  *
    00006  *   it under the terms of the GNU Lesser General Public License as        *
    00007  *   published by the Free Software Foundation; either version 3 of the    *
    00008  *   License, or (at your option) any later version.                       *
    00009  *                                                                         *
    00010  ***************************************************************************/
    00011
    00017     final class Expression extends StaticFactory
    00018     {
    00022         public static function expAnd($left, $right)
    00023         {
    00024             return new BinaryExpression(
    00025                 $left, $right, BinaryExpression::EXPRESSION_AND
    00026             );
    00027         }
    00028
    00032         public static function expOr($left, $right)
    00033         {
    00034             return new BinaryExpression(
    00035                 $left, $right, BinaryExpression::EXPRESSION_OR
    00036             );
    00037         }
    00038
    00042         public static function eq($field, $value)
    00043         {
    00044             return new BinaryExpression($field, $value, BinaryExpression::EQUALS);
    00045         }
    00046
    00050         public static function eqId($field, Identifiable $object)
    00051         {
    00052             return self::eq($field, $object->getId());
    00053         }
    00054
    00058         public static function notEq($field, $value)
    00059         {
    00060             return new BinaryExpression(
    00061                 $field, $value, BinaryExpression::NOT_EQUALS
    00062             );
    00063         }
    00064
    00070         public static function gt($field, $value)
    00071         {
    00072             return new BinaryExpression(
    00073                 $field, $value, BinaryExpression::GREATER_THAN
    00074             );
    00075         }
    00076
    00082         public static function gtEq($field, $value)
    00083         {
    00084             return new BinaryExpression(
    00085                 $field, $value, BinaryExpression::GREATER_OR_EQUALS
    00086             );
    00087         }
    00088
    00094         public static function lt($field, $value)
    00095         {
    00096             return new BinaryExpression(
    00097                 $field, $value, BinaryExpression::LOWER_THAN
    00098             );
    00099         }
    00100
    00106         public static function ltEq($field, $value)
    00107         {
    00108             return new BinaryExpression(
    00109                 $field, $value, BinaryExpression::LOWER_OR_EQUALS
    00110             );
    00111         }
    00112
    00116         public static function notNull($field)
    00117         {
    00118             return new PostfixUnaryExpression($field, PostfixUnaryExpression::IS_NOT_NULL);
    00119         }
    00120
    00124         public static function isNull($field)
    00125         {
    00126             return new PostfixUnaryExpression($field, PostfixUnaryExpression::IS_NULL);
    00127         }
    00128
    00132         public static function isTrue($field)
    00133         {
    00134             return new PostfixUnaryExpression($field, PostfixUnaryExpression::IS_TRUE);
    00135         }
    00136
    00140         public static function isFalse($field)
    00141         {
    00142             return new PostfixUnaryExpression($field, PostfixUnaryExpression::IS_FALSE);
    00143         }
    00144
    00148         public static function like($field, $value)
    00149         {
    00150             return new BinaryExpression($field, $value, BinaryExpression::LIKE);
    00151         }
    00152
    00156         public static function notLike($field, $value)
    00157         {
    00158             return new BinaryExpression($field, $value, BinaryExpression::NOT_LIKE);
    00159         }
    00160
    00164         public static function ilike($field, $value)
    00165         {
    00166             return new BinaryExpression($field, $value, BinaryExpression::ILIKE);
    00167         }
    00168
    00172         public static function notIlike($field, $value)
    00173         {
    00174             return new BinaryExpression($field, $value, BinaryExpression::NOT_ILIKE);
    00175         }
    00176
    00180         public static function similar($field, $value)
    00181         {
    00182             return new BinaryExpression($field, $value, BinaryExpression::SIMILAR_TO);
    00183         }
    00184
    00188         public static function notSimilar($field, $value)
    00189         {
    00190             return new BinaryExpression($field, $value, BinaryExpression::NOT_SIMILAR_TO);
    00191         }
    00192
    00196         public static function eqLower($field, $value)
    00197         {
    00198             return new EqualsLowerExpression($field, $value);
    00199         }
    00200
    00204         public static function between($field, $left, $right)
    00205         {
    00206             return new LogicalBetween($field, $left, $right);
    00207         }
    00208
    00214         public static function in($field, $value)
    00215         {
    00216             if (is_numeric($value) && $value == (int) $value)
    00217                 return self::eq($field, $value);
    00218             elseif (is_array($value) && count($value) == 1)
    00219                 return self::eq($field, current($value));
    00220             else {
    00221                 return new InExpression(
    00222                     $field, $value, InExpression::IN
    00223                 );
    00224             }
    00225         }
    00226
    00230         public static function notIn($field, $value)
    00231         {
    00232             if (is_numeric($value) && $value == (int) $value)
    00233                 return self::notEq($field, $value);
    00234             elseif (is_array($value) && count($value) == 1)
    00235                 return self::notEq($field, current($value));
    00236             else {
    00237                 return new InExpression(
    00238                     $field, $value, InExpression::NOT_IN
    00239                 );
    00240             }
    00241         }
    00242
    00248         public static function add($field, $value)
    00249         {
    00250             return new BinaryExpression($field, $value, BinaryExpression::ADD);
    00251         }
    00252
    00258         public static function sub($field, $value)
    00259         {
    00260             return new BinaryExpression($field, $value, BinaryExpression::SUBSTRACT);
    00261         }
    00262
    00268         public static function mul($field, $value)
    00269         {
    00270             return new BinaryExpression($field, $value, BinaryExpression::MULTIPLY);
    00271         }
    00272
    00278         public static function div($field, $value)
    00279         {
    00280             return new BinaryExpression($field, $value, BinaryExpression::DIVIDE);
    00281         }
    00282
    00288         public static function mod($field, $value)
    00289         {
    00290             return new BinaryExpression($field, $value, BinaryExpression::MOD);
    00291         }
    00292
    00296         public static function fullTextAnd($field, $wordsList)
    00297         {
    00298             return new FullTextSearch($field, $wordsList, DB::FULL_TEXT_AND);
    00299         }
    00300
    00304         public static function fullTextOr($field, $wordsList)
    00305         {
    00306             return new FullTextSearch($field, $wordsList, DB::FULL_TEXT_OR);
    00307         }
    00308
    00312         public static function fullTextRankOr($field, $wordsList)
    00313         {
    00314             return new FullTextRank($field, $wordsList, DB::FULL_TEXT_OR);
    00315         }
    00316
    00320         public static function fullTextRankAnd($field, $wordsList)
    00321         {
    00322             return new FullTextRank($field, $wordsList, DB::FULL_TEXT_AND);
    00323         }
    00324
    00328         public static function orBlock(/* ... */)
    00329         {
    00330             return self::block(
    00331                 func_get_args(),
    00332                 BinaryExpression::EXPRESSION_OR
    00333             );
    00334         }
    00335
    00339         public static function andBlock(/* ... */)
    00340         {
    00341             return self::block(
    00342                 func_get_args(),
    00343                 BinaryExpression::EXPRESSION_AND
    00344             );
    00345         }
    00346
    00350         public static function chain()
    00351         {
    00352             return new LogicalChain();
    00353         }
    00354
    00358         public static function not($field)
    00359         {
    00360             return new PrefixUnaryExpression(PrefixUnaryExpression::NOT, $field);
    00361         }
    00362
    00366         public static function minus($field)
    00367         {
    00368             return new PrefixUnaryExpression(PrefixUnaryExpression::MINUS, $field);
    00369         }
    00370
    00374         private static function block($args, $logic)
    00375         {
    00376             return LogicalChain::block($args, $logic);
    00377         }
    00378     }
    00379 ?>