Expr.php 19.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<?php
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\ORM\Query;

/**
 * This class is used to generate DQL expressions via a set of PHP static functions
 *
27 28 29 30 31 32 33
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link    www.doctrine-project.org
 * @since   2.0
 * @version $Revision$
 * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
 * @author  Jonathan Wage <jonwage@gmail.com>
 * @author  Roman Borschel <roman@code-factory.org>
34 35 36
 */
class Expr
{
37 38 39 40 41 42
    /**
     * Creates an instance of Expr\Andx with given arguments.
     * Each argument is separated by an "AND". Example:
     *
     *     [php]
     *     // (u.type = ?1) AND (u.role = ?2)
43
     *     $q->where($q->expr()->andx('u.type = ?1', 'u.role = ?2'));
44 45 46 47 48
     *
     * @param mixed $x Optional clause. Defaults = null, but requires
     *                 at least one defined when converting to string.
     * @return Expr\Andx
     */
49
    public function andx($x = null)
50
    {
51
        return new Expr\Andx(func_get_args());
52 53
    }

54 55 56 57 58 59
    /**
     * Creates an instance of Expr\Orx with given arguments.
     * Each argument is separated by an "OR". Example:
     *
     *     [php]
     *     // (u.type = ?1) OR (u.role = ?2)
60
     *     $q->where($q->expr()->orx('u.type = ?1', 'u.role = ?2'));
61 62 63 64 65
     *
     * @param mixed $x Optional clause. Defaults = null, but requires
     *                 at least one defined when converting to string.
     * @return Expr\Orx
     */
66
    public function orx($x = null)
67
    {
68
        return new Expr\Orx(func_get_args());
69 70
    }

71 72 73 74 75 76
    /**
     * Creates an instance of Expr\Select with given arguments.
     * Each argument is separated by a ",". Example:
     *
     *     [php]
     *     // u.id, u.name, u.surname
77
     *     $q->select($q->expr()->select('u.id', 'u.name')->add('u.surname'));
78 79 80 81 82
     *
     * @param mixed $select Optional select. Defaults = null, but requires
     *                      at least one defined when converting to string.
     * @return Expr\Select
     */
83
    public function select($select = null)
84
    {
85
        return new Expr\Select(func_get_args());
86
    }
87
    
88 89 90 91 92
    /**
     * Creates an instance of Expr\From with given arguments.
     *
     *     [php]
     *     // User u
93
     *     $q->from($q->expr()->from('User', 'u'));
94 95 96 97 98
     *
     * @param string $from Entity name.
     * @param string $alias Optional alias to be used by Entity.
     * @return Expr\From
     */
99
    public function from($from, $alias = null)
100 101 102
    {
        return new Expr\From($from, $alias);
    }
103
    
104 105 106 107 108
    /**
     * Creates an instance of Expr\Join with given arguments.
     *
     *     [php]
     *     // LEFT JOIN u.Group g WITH g.name = 'admin'
109
     *     $q->expr()->leftJoin('u.Group', 'g', 'WITH', "g.name = 'admin'")
110 111 112 113 114 115 116 117
     *
     * @param string $join Relation join.
     * @param string $alias Optional alias to be used by Relation.
     * @param string $conditionType Optional type of condition appender. Accepts either string or constant.
     *                              'ON' and 'WITH' are supported strings. Expr\Join::ON and Expr\Join::WITH are supported constants.
     * @param mixed $condition Optional condition to be appended.
     * @return Expr\Join
     */
118
    public function leftJoin($join, $alias = null, $conditionType = null, $condition = null)
119 120 121 122
    {
        return new Expr\Join(Expr\Join::LEFT_JOIN, $join, $alias, $conditionType, $condition);
    }
    
123 124 125 126 127
    /**
     * Creates an instance of Expr\Join with given arguments.
     *
     *     [php]
     *     // INNER JOIN u.Group g WITH g.name = 'admin'
128
     *     $q->expr()->innerJoin('u.Group', 'g', 'WITH', "g.name = 'admin'")
129 130 131 132 133 134 135 136
     *
     * @param string $join Relation join.
     * @param string $alias Optional alias to be used by Relation.
     * @param string $conditionType Optional type of condition appender. Accepts either string or constant.
     *                              'ON' and 'WITH' are supported strings. Expr\Join::ON and Expr\Join::WITH are supported constants.
     * @param mixed $condition Optional condition to be appended.
     * @return Expr\Join
     */
137
    public function innerJoin($join, $alias = null, $conditionType = null, $condition = null)
138 139 140
    {
        return new Expr\Join(Expr\Join::INNER_JOIN, $join, $alias, $conditionType, $condition);
    }
141

142 143 144 145 146
    /**
     * Creates an instance of Expr\OrderBy with given item sort and order.
     * Each argument is separated by a ",". Example:
     *
     *     [php]
147
     *     $q->orderBy($q->expr()->orderBy('u.surname', 'ASC')->add('u.name', 'ASC'));
148 149 150 151 152
     *
     * @param string $sort Optional item sort.
     * @param string $order Optional order to be applied in item.
     * @return Expr\OrderBy
     */
153
    public function orderBy($sort = null, $order = null)
154
    {
155 156 157
        return new Expr\OrderBy($sort, $order);
    }

158 159 160 161 162 163
    /**
     * Creates an instance of Expr\GroupBy with given arguments.
     * Each argument is separated by a ",". Example:
     *
     *     [php]
     *     // u.id, u.name
164
     *     $q->select($q->expr()->groupBy('u.id', 'u.name'));
165 166 167 168 169
     *
     * @param mixed $groupBy Optional group by. Defaults = null, but requires
     *                       at least one defined when converting to string.
     * @return Expr\Select
     */
170
    public function groupBy($groupBy = null)
171 172 173 174
    {
        return new Expr\GroupBy(func_get_args());
    }

175 176 177 178 179 180 181
    /**
     * Creates an instance of Expr\Comparison, with the given arguments.
     * First argument is considered the left expression and the second is the right expression.
     * When converted to string, it will generated a <left expr> = <right expr>. Example:
     *
     *     [php]
     *     // u.id = ?1
182
     *     $q->where($q->expr()->eq('u.id', '?1'));
183 184 185 186 187
     *
     * @param mixed $x Left expression
     * @param mixed $y Right expression
     * @return Expr\Comparison
     */
188
    public function eq($x, $y)
189
    {
190
        return new Expr\Comparison($x, Expr\Comparison::EQ, $y);
191 192
    }

193 194 195 196 197 198 199
    /**
     * Creates an instance of Expr\Comparison, with the given arguments.
     * First argument is considered the left expression and the second is the right expression.
     * When converted to string, it will generated a <left expr> <> <right expr>. Example:
     *
     *     [php]
     *     // u.id <> ?1
200
     *     $q->where($q->expr()->neq('u.id', '?1'));
201 202 203 204 205
     *
     * @param mixed $x Left expression
     * @param mixed $y Right expression
     * @return Expr\Comparison
     */
206
    public function neq($x, $y)
207
    {
208
        return new Expr\Comparison($x, Expr\Comparison::NEQ, $y);
209 210
    }

211 212 213 214 215 216 217
    /**
     * Creates an instance of Expr\Comparison, with the given arguments.
     * First argument is considered the left expression and the second is the right expression.
     * When converted to string, it will generated a <left expr> < <right expr>. Example:
     *
     *     [php]
     *     // u.id < ?1
218
     *     $q->where($q->expr()->lt('u.id', '?1'));
219 220 221 222 223
     *
     * @param mixed $x Left expression
     * @param mixed $y Right expression
     * @return Expr\Comparison
     */
224
    public function lt($x, $y)
225
    {
226
        return new Expr\Comparison($x, Expr\Comparison::LT, $y);
227 228
    }

229 230 231 232 233 234 235
    /**
     * Creates an instance of Expr\Comparison, with the given arguments.
     * First argument is considered the left expression and the second is the right expression.
     * When converted to string, it will generated a <left expr> <= <right expr>. Example:
     *
     *     [php]
     *     // u.id <= ?1
236
     *     $q->where($q->expr()->lte('u.id', '?1'));
237 238 239 240 241
     *
     * @param mixed $x Left expression
     * @param mixed $y Right expression
     * @return Expr\Comparison
     */
242
    public function lte($x, $y)
243
    {
244
        return new Expr\Comparison($x, Expr\Comparison::LTE, $y);
245 246
    }

247 248 249 250 251 252 253
    /**
     * Creates an instance of Expr\Comparison, with the given arguments.
     * First argument is considered the left expression and the second is the right expression.
     * When converted to string, it will generated a <left expr> > <right expr>. Example:
     *
     *     [php]
     *     // u.id > ?1
254
     *     $q->where($q->expr()->gt('u.id', '?1'));
255 256 257 258 259
     *
     * @param mixed $x Left expression
     * @param mixed $y Right expression
     * @return Expr\Comparison
     */
260
    public function gt($x, $y)
261
    {
262
        return new Expr\Comparison($x, Expr\Comparison::GT, $y);
263 264
    }

265 266 267 268 269 270 271
    /**
     * Creates an instance of Expr\Comparison, with the given arguments.
     * First argument is considered the left expression and the second is the right expression.
     * When converted to string, it will generated a <left expr> >= <right expr>. Example:
     *
     *     [php]
     *     // u.id >= ?1
272
     *     $q->where($q->expr()->gte('u.id', '?1'));
273 274 275 276 277
     *
     * @param mixed $x Left expression
     * @param mixed $y Right expression
     * @return Expr\Comparison
     */
278
    public function gte($x, $y)
279
    {
280
        return new Expr\Comparison($x, Expr\Comparison::GTE, $y);
281 282
    }

283 284 285 286 287 288
    /**
     * Creates an instance of AVG() function, with the given argument.
     *
     * @param mixed $x Argument to be used in AVG() function.
     * @return Expr\Func
     */
289
    public function avg($x)
290
    {
291
        return new Expr\Func('AVG', array($x));
292 293
    }

294 295 296 297 298 299
    /**
     * Creates an instance of MAX() function, with the given argument.
     *
     * @param mixed $x Argument to be used in MAX() function.
     * @return Expr\Func
     */
300
    public function max($x)
301
    {
302
        return new Expr\Func('MAX', array($x));
303 304
    }

305 306 307 308 309 310
    /**
     * Creates an instance of MIN() function, with the given argument.
     *
     * @param mixed $x Argument to be used in MIN() function.
     * @return Expr\Func
     */
311
    public function min($x)
312
    {
313
        return new Expr\Func('MIN', array($x));
314 315
    }

316 317 318 319 320 321
    /**
     * Creates an instance of COUNT() function, with the given argument.
     *
     * @param mixed $x Argument to be used in COUNT() function.
     * @return Expr\Func
     */
322
    public function count($x)
323
    {
324
        return new Expr\Func('COUNT', array($x));
325 326
    }

327 328 329 330 331 332
    /**
     * Creates an instance of COUNT(DISTINCT) function, with the given argument.
     *
     * @param mixed $x Argument to be used in COUNT(DISTINCT) function.
     * @return string
     */
333
    public function countDistinct($x)
334
    {
335
        return 'COUNT(DISTINCT ' . implode(', ', func_get_args()) . ')';
336 337
    }

338 339 340 341 342 343
    /**
     * Creates an instance of EXISTS() function, with the given DQL Subquery.
     *
     * @param mixed $subquery DQL Subquery to be used in EXISTS() function.
     * @return Expr\Func
     */
344
    public function exists($subquery)
345
    {
346
        return new Expr\Func('EXISTS', array($subquery));
347 348
    }

349 350 351 352 353 354
    /**
     * Creates an instance of ALL() function, with the given DQL Subquery.
     *
     * @param mixed $subquery DQL Subquery to be used in ALL() function.
     * @return Expr\Func
     */
355
    public function all($subquery)
356
    {
357
        return new Expr\Func('ALL', array($subquery));
358 359
    }

360 361 362 363 364 365
    /**
     * Creates an instance of SOME() function, with the given DQL Subquery.
     *
     * @param mixed $subquery DQL Subquery to be used in SOME() function.
     * @return Expr\Func
     */
366
    public function some($subquery)
367
    {
368
        return new Expr\Func('SOME', array($subquery));
369 370
    }

371 372 373 374 375 376
    /**
     * Creates an instance of ANY() function, with the given DQL subquery.
     *
     * @param mixed $subquery DQL Subquery to be used in ANY() function.
     * @return Expr\Func
     */
377
    public function any($subquery)
378
    {
379
        return new Expr\Func('ANY', array($subquery));
380 381
    }

382 383 384 385 386 387
    /**
     * Creates an instance of NOT() function, with the given restriction.
     *
     * @param mixed $restriction Restriction to be used in NOT() function.
     * @return Expr\Func
     */
388
    public function not($restriction)
389
    {
390
        return new Expr\Func('NOT', array($restriction));
391 392
    }

393 394 395 396 397 398
    /**
     * Creates an instance of ABS() function, with the given argument.
     *
     * @param mixed $x Argument to be used in ABS() function.
     * @return Expr\Func
     */
399
    public function abs($x)
400
    {
401
        return new Expr\Func('ABS', array($x));
402 403
    }

404 405 406 407 408 409 410
    /**
     * Creates a product mathematical expression with the given arguments.
     * First argument is considered the left expression and the second is the right expression.
     * When converted to string, it will generated a <left expr> * <right expr>. Example:
     *
     *     [php]
     *     // u.salary * u.percentAnualSalaryIncrease
411
     *     $q->expr()->prod('u.salary', 'u.percentAnualSalaryIncrease')
412 413 414 415 416
     *
     * @param mixed $x Left expression
     * @param mixed $y Right expression
     * @return Expr\Math
     */
417
    public function prod($x, $y)
418
    {
419
        return new Expr\Math($x, '*', $y);
420 421
    }

422 423 424 425 426 427 428
    /**
     * Creates a difference mathematical expression with the given arguments.
     * First argument is considered the left expression and the second is the right expression.
     * When converted to string, it will generated a <left expr> - <right expr>. Example:
     *
     *     [php]
     *     // u.monthlySubscriptionCount - 1
429
     *     $q->expr()->diff('u.monthlySubscriptionCount', '1')
430 431 432 433 434
     *
     * @param mixed $x Left expression
     * @param mixed $y Right expression
     * @return Expr\Math
     */
435
    public function diff($x, $y)
436
    {
437
        return new Expr\Math($x, '-', $y);
438 439
    }

440 441 442 443 444 445 446
    /**
     * Creates a sum mathematical expression with the given arguments.
     * First argument is considered the left expression and the second is the right expression.
     * When converted to string, it will generated a <left expr> + <right expr>. Example:
     *
     *     [php]
     *     // u.numChildren + 1
447
     *     $q->expr()->diff('u.numChildren', '1')
448 449 450 451 452
     *
     * @param mixed $x Left expression
     * @param mixed $y Right expression
     * @return Expr\Math
     */
453
    public function sum($x, $y)
454
    {
455
        return new Expr\Math($x, '+', $y);
456 457
    }

458 459 460 461 462 463 464
    /**
     * Creates a quotient mathematical expression with the given arguments.
     * First argument is considered the left expression and the second is the right expression.
     * When converted to string, it will generated a <left expr> / <right expr>. Example:
     *
     *     [php]
     *     // u.total - u.period
465
     *     $q->expr()->diff('u.total', 'u.period')
466 467 468 469 470
     *
     * @param mixed $x Left expression
     * @param mixed $y Right expression
     * @return Expr\Math
     */
471
    public function quot($x, $y)
472
    {
473
        return new Expr\Math($x, '/', $y);
474 475
    }

476 477 478 479 480 481
    /**
     * Creates an instance of SQRT() function, with the given argument.
     *
     * @param mixed $x Argument to be used in SQRT() function.
     * @return Expr\Func
     */
482
    public function sqrt($x)
483
    {
484
        return new Expr\Func('SQRT', array($x));
485 486
    }

487 488 489 490 491 492 493
    /**
     * Creates an instance of field IN() function, with the given arguments.
     *
     * @param string $x Field in string format to be restricted by IN() function
     * @param mixed $y Argument to be used in IN() function.
     * @return Expr\Func
     */
494
    public function in($x, $y)
495
    {
496
        return new Expr\Func($x . ' IN', (array) $y);
497 498
    }

499 500 501 502 503 504 505
    /**
     * Creates an instance of field NOT IN() function, with the given arguments.
     *
     * @param string $x Field in string format to be restricted by NOT IN() function
     * @param mixed $y Argument to be used in NOT IN() function.
     * @return Expr\Func
     */
506
    public function notIn($x, $y)
507
    {
508
        return new Expr\Func($x . ' NOT IN', (array) $y);
509 510
    }

511 512 513 514 515 516 517
    /**
     * Creates an instance of field LIKE() comparison, with the given arguments.
     *
     * @param string $x Field in string format to be inspected by LIKE() comparison.
     * @param mixed $y Argument to be used in LIKE() comparison.
     * @return Expr\Comparison
     */
518
    public function like($x, $y)
519
    {
520
        return new Expr\Comparison($x, 'LIKE', $y);
521 522
    }

523 524 525 526 527 528 529
    /**
     * Creates an instance of CONCAT() function, with the given argument.
     *
     * @param mixed $x First argument to be used in CONCAT() function.
     * @param mixed $x Second argument to be used in CONCAT() function.
     * @return Expr\Func
     */
530
    public function concat($x, $y)
531
    {
532
        return new Expr\Func('CONCAT', array($x, $y));
533 534
    }

535 536 537 538 539 540 541 542
    /**
     * Creates an instance of SUBSTR() function, with the given argument.
     *
     * @param mixed $x Argument to be used as string to be cropped by SUBSTR() function.
     * @param integer $from Initial offset to start cropping string. May accept negative values.
     * @param integer $len Length of crop. May accept negative values.
     * @return Expr\Func
     */
543
    public function substr($x, $from, $len)
544
    {
545
        return new Expr\Func('SUBSTR', array($x, $from, $len));
546 547
    }

548 549 550 551 552 553
    /**
     * Creates an instance of LOWER() function, with the given argument.
     *
     * @param mixed $x Argument to be used in LOWER() function.
     * @return Expr\Func
     */
554
    public function lower($x)
555
    {
556
        return new Expr\Func('LOWER', array($x));
557 558
    }

559 560 561 562 563 564
    /**
     * Creates an instance of LOWER() function, with the given argument.
     *
     * @param mixed $x Argument to be used in LOWER() function.
     * @return Expr\Func
     */
565
    public function upper($x)
566
    {
567
        return new Expr\Func('UPPER', array($x));
568 569
    }

570 571 572 573 574 575
    /**
     * Creates an instance of LENGTH() function, with the given argument.
     *
     * @param mixed $x Argument to be used as argument of LENGTH() function.
     * @return Expr\Func
     */
576
    public function length($x)
577
    {
578
        return new Expr\Func('LENGTH', array($x));
579 580
    }

581 582 583 584 585 586
    /**
     * Creates a literal representation of the given argument.
     *
     * @param mixed $literal Argument to be converted to literal.
     * @return string
     */
587
    public function literal($literal)
588
    {
589 590 591 592 593
        if (is_numeric($literal)) {
            return (string) $literal;
        } else {
            return "'" . $literal . "'";
        }
594 595
    }

596 597 598 599 600 601 602 603
    /**
     * Creates an instance of BETWEEN() function, with the given argument.
     *
     * @param mixed $val Valued to be inspected by range values.
     * @param integer $x Starting range value to be used in BETWEEN() function.
     * @param integer $y End point value to be used in BETWEEN() function.
     * @return Expr\Func
     */
604
    public function between($val, $x, $y)
605
    {
606
        return new Expr\Func('BETWEEN', array($val, $x, $y));
607 608
    }

609 610 611 612 613 614
    /**
     * Creates an instance of TRIM() function, with the given argument.
     *
     * @param mixed $x Argument to be used as argument of TRIM() function.
     * @return Expr\Func
     */
615
    public function trim($x)
616
    {
617
        return new Expr\Func('TRIM', $x);
618
    }
619
}