Expression.php 20.3 KB
Newer Older
1
<?php
lsmith's avatar
lsmith committed
2
/*
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *  $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.phpdoctrine.com>.
 */
21
Doctrine::autoload('Doctrine_Connection_Module');
22 23 24
/**
 * Doctrine_Expression
 *
25 26 27 28 29 30 31 32
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
lsmith's avatar
lsmith committed
33 34 35 36
class Doctrine_Expression extends Doctrine_Connection_Module
{
    public function getIdentifier($column)
    {
zYne's avatar
zYne committed
37 38
        return $column;
    }
lsmith's avatar
lsmith committed
39 40
    public function getIdentifiers($columns)
    {
zYne's avatar
zYne committed
41 42
        return $columns;
    }
43 44
    /**
     * regexp
lsmith's avatar
lsmith committed
45
     * returns the regular expression operator
46 47 48
     *
     * @return string
     */
lsmith's avatar
lsmith committed
49 50
    public function regexp()
    {
lsmith's avatar
lsmith committed
51
        throw new Doctrine_Expression_Exception('Regular expression operator is not supported by this database driver.');
52
    }
53 54 55 56 57 58
    /**
     * Returns the average value of a column
     *
     * @param string $column    the column to use
     * @return string           generated sql including an AVG aggregate function
     */
lsmith's avatar
lsmith committed
59 60
    public function avg($column)
    {
61 62 63 64 65 66 67 68 69 70 71 72 73
        $column = $this->getIdentifier($column);
        return 'AVG(' .  $column . ')';
    }

    /**
     * Returns the number of rows (without a NULL value) of a column
     *
     * If a '*' is used instead of a column the number of selected rows
     * is returned.
     *
     * @param string|integer $column    the column to use
     * @return string                   generated sql including a COUNT aggregate function
     */
lsmith's avatar
lsmith committed
74 75
    public function count($column)
    {
76 77 78 79 80 81 82 83 84 85
        $column = $this->getIdentifier($column);
        return 'COUNT(' . $column . ')';
    }

    /**
     * Returns the highest value of a column
     *
     * @param string $column    the column to use
     * @return string           generated sql including a MAX aggregate function
     */
lsmith's avatar
lsmith committed
86 87
    public function max($column)
    {
88 89 90 91 92 93 94 95 96 97
        $column = $this->getIdentifier($column);
        return 'MAX(' . $column . ')';
    }

    /**
     * Returns the lowest value of a column
     *
     * @param string $column the column to use
     * @return string
     */
lsmith's avatar
lsmith committed
98 99
    public function min($column)
    {
100 101 102 103 104 105 106 107 108 109
        $column = $this->getIdentifier($column);
        return 'MIN(' . $column . ')';
    }

    /**
     * Returns the total sum of a column
     *
     * @param string $column the column to use
     * @return string
     */
lsmith's avatar
lsmith committed
110 111
    public function sum($column)
    {
112 113 114 115 116 117 118 119 120 121 122 123 124
        $column = $this->getIdentifier($column);
        return 'SUM(' . $column . ')';
    }

    // scalar functions

    /**
     * Returns the md5 sum of a field.
     *
     * Note: Not SQL92, but common functionality
     *
     * @return string
     */
lsmith's avatar
lsmith committed
125 126
    public function md5($column)
    {
127 128 129 130 131 132 133 134 135 136 137
        $column = $this->getIdentifier($column);
        return 'MD5(' . $column . ')';
    }

    /**
     * Returns the length of a text field.
     *
     * @param string $expression1
     * @param string $expression2
     * @return string
     */
lsmith's avatar
lsmith committed
138 139
    public function length($column)
    {
140 141 142 143 144 145 146 147 148 149 150
        $column = $this->getIdentifier($column);
        return 'LENGTH(' . $column . ')';
    }

    /**
     * Rounds a numeric field to the number of decimals specified.
     *
     * @param string $expression1
     * @param string $expression2
     * @return string
     */
lsmith's avatar
lsmith committed
151 152
    public function round($column, $decimals = 0)
    {
153 154 155 156 157 158 159 160 161 162 163 164 165
        $column = $this->getIdentifier($column);

        return 'ROUND(' . $column . ', ' . $decimals . ')';
    }

    /**
     * Returns the remainder of the division operation
     * $expression1 / $expression2.
     *
     * @param string $expression1
     * @param string $expression2
     * @return string
     */
lsmith's avatar
lsmith committed
166 167
    public function mod($expression1, $expression2)
    {
168 169 170 171 172 173 174 175 176 177 178
        $expression1 = $this->getIdentifier($expression1);
        $expression2 = $this->getIdentifier($expression2);
        return 'MOD(' . $expression1 . ', ' . $expression2 . ')';
    }
    /**
     * ltrim
     * returns the string $str with leading space characters removed
     *
     * @param string $str       literal string or column name
     * @return string
     */
lsmith's avatar
lsmith committed
179 180
    public function ltrim($str)
    {
181 182 183 184
        return 'LTRIM(' . $str . ')';
    }
    /**
     * upper
lsmith's avatar
lsmith committed
185
     * Returns the string $str with all characters changed to
186
     * uppercase according to the current character set mapping.
187 188 189 190
     *
     * @param string $str       literal string or column name
     * @return string
     */
lsmith's avatar
lsmith committed
191 192
    public function upper($str)
    {
193 194 195 196
        return 'UPPER(' . $str . ')';
    }
    /**
     * lower
lsmith's avatar
lsmith committed
197
     * Returns the string $str with all characters changed to
198
     * lowercase according to the current character set mapping.
199 200 201 202
     *
     * @param string $str       literal string or column name
     * @return string
     */
lsmith's avatar
lsmith committed
203 204
    public function lower($str)
    {
205 206 207 208 209 210
        return 'LOWER(' . $str . ')';
    }
    /**
     * locate
     * returns the position of the first occurrence of substring $substr in string $str
     *
zYne's avatar
zYne committed
211
     * @param string $substr    literal string to find
212
     * @param string $str       literal string
zYne's avatar
zYne committed
213
     * @return integer
214
     */
lsmith's avatar
lsmith committed
215 216
    public function locate($str, $substr)
    {
217 218 219 220 221 222 223
        return 'LOCATE(' . $str . ', ' . $substr . ')';
    }
    /**
     * Returns the current system date.
     *
     * @return string
     */
lsmith's avatar
lsmith committed
224 225
    public function now()
    {
226 227
        return 'NOW()';
    }
zYne's avatar
zYne committed
228 229
    /**
     * soundex
lsmith's avatar
lsmith committed
230
     * Returns a string to call a function to compute the
zYne's avatar
zYne committed
231 232 233 234 235 236 237
     * soundex encoding of a string
     *
     * The string "?000" is returned if the argument is NULL.
     *
     * @param string $value
     * @return string   SQL soundex function with given parameter
     */
lsmith's avatar
lsmith committed
238 239
    public function soundex($value)
    {
zYne's avatar
zYne committed
240 241
        throw new Doctrine_Expression_Exception('SQL soundex function not supported by this driver.');
    }
242
    /**
243
     * return string to call a function to get a substring inside an SQL statement
244
     *
lsmith's avatar
lsmith committed
245 246
     * Note: Not SQL92, but common functionality.
     *
247
     * SQLite only supports the 2 parameter variant of this function
248
     *
249 250 251 252
     * @param string $value         an sql string literal or column name/alias
     * @param integer $position     where to start the substring portion
     * @param integer $length       the substring portion length
     * @return string               SQL substring function with given parameters
253
     */
lsmith's avatar
lsmith committed
254 255
    public function substring($value, $from, $len = null)
    {
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        $value = $this->getIdentifier($value);
        if ($len === null)
            return 'SUBSTRING(' . $value . ' FROM ' . $from . ')';
        else {
            $len = $this->getIdentifier($len);
            return 'SUBSTRING(' . $value . ' FROM ' . $from . ' FOR ' . $len . ')';
        }
    }
    /**
     * Returns a series of strings concatinated
     *
     * concat() accepts an arbitrary number of parameters. Each parameter
     * must contain an expression or an array with expressions.
     *
     * @param string|array(string) strings that will be concatinated.
     */
zYne's avatar
zYne committed
272
    public function concat()
lsmith's avatar
lsmith committed
273
    {
zYne's avatar
zYne committed
274 275 276
    	$args = func_get_args();

        return 'CONCAT(' . join(', ', (array) $args) . ')';
277
    }
278 279 280 281 282 283 284 285 286 287 288 289 290
    /**
     * Returns the SQL for a logical not.
     *
     * Example:
     * <code>
     * $q = new Doctrine_Query();
     * $e = $q->expr;
     * $q->select('*')->from('table')
     *   ->where($e->eq('id', $e->not('null'));
     * </code>
     *
     * @return string a logical expression
     */
lsmith's avatar
lsmith committed
291 292
    public function not($expression)
    {
293
        $expression = $this->getIdentifier($expression);
zYne's avatar
zYne committed
294
        return 'NOT(' . $expression . ')';
295 296 297 298 299 300 301 302 303 304 305 306 307
    }
    /**
     * Returns the SQL to perform the same mathematical operation over an array
     * of values or expressions.
     *
     * basicMath() accepts an arbitrary number of parameters. Each parameter
     * must contain a value or an expression or an array with values or
     * expressions.
     *
     * @param string $type the type of operation, can be '+', '-', '*' or '/'.
     * @param string|array(string)
     * @return string an expression
     */
lsmith's avatar
lsmith committed
308 309
    private function basicMath($type, array $args)
    {
zYne's avatar
zYne committed
310
        $elements = $this->getIdentifiers($args);
lsmith's avatar
lsmith committed
311
        if (count($elements) < 1) {
312
            return '';
lsmith's avatar
lsmith committed
313
        }
314 315 316
        if (count($elements) == 1) {
            return $elements[0];
        } else {
zYne's avatar
zYne committed
317
            return '(' . implode(' ' . $type . ' ', $elements) . ')';
318 319 320 321 322 323 324 325 326 327 328 329
        }
    }
    /**
     * Returns the SQL to add values or expressions together.
     *
     * add() accepts an arbitrary number of parameters. Each parameter
     * must contain a value or an expression or an array with values or
     * expressions.
     *
     * Example:
     * <code>
     * $q = new Doctrine_Query();
lsmith's avatar
lsmith committed
330
     * $e = $q->expr;
331 332 333 334 335 336 337 338 339
     *
     * $q->select('u.*')
     *   ->from('User u')
     *   ->where($e->eq($e->add('id', 2), 12));
     * </code>
     *
     * @param string|array(string)
     * @return string an expression
     */
lsmith's avatar
lsmith committed
340 341
    public function add(array $args)
    {
342 343 344 345 346 347 348 349 350 351 352 353 354
        return $this->basicMath('+', $args);
    }

    /**
     * Returns the SQL to subtract values or expressions from eachother.
     *
     * subtract() accepts an arbitrary number of parameters. Each parameter
     * must contain a value or an expression or an array with values or
     * expressions.
     *
     * Example:
     * <code>
     * $q = new Doctrine_Query();
lsmith's avatar
lsmith committed
355
     * $e = $q->expr;
356 357 358 359 360 361 362 363 364
     *
     * $q->select('u.*')
     *   ->from('User u')
     *   ->where($e->eq($e->sub('id', 2), 12));
     * </code>
     *
     * @param string|array(string)
     * @return string an expression
     */
lsmith's avatar
lsmith committed
365 366
    public function sub(array $args)
    {
367 368 369 370 371 372 373 374 375 376 377 378 379
        return $this->basicMath('-', $args );
    }

    /**
     * Returns the SQL to multiply values or expressions by eachother.
     *
     * multiply() accepts an arbitrary number of parameters. Each parameter
     * must contain a value or an expression or an array with values or
     * expressions.
     *
     * Example:
     * <code>
     * $q = new Doctrine_Query();
lsmith's avatar
lsmith committed
380
     * $e = $q->expr;
381 382 383 384 385 386 387 388 389
     *
     * $q->select('u.*')
     *   ->from('User u')
     *   ->where($e->eq($e->mul('id', 2), 12));
     * </code>
     *
     * @param string|array(string)
     * @return string an expression
     */
lsmith's avatar
lsmith committed
390 391
    public function mul(array $args)
    {
392 393 394 395 396 397 398 399 400 401 402 403 404
        return $this->basicMath('*', $args);
    }

    /**
     * Returns the SQL to divide values or expressions by eachother.
     *
     * divide() accepts an arbitrary number of parameters. Each parameter
     * must contain a value or an expression or an array with values or
     * expressions.
     *
     * Example:
     * <code>
     * $q = new Doctrine_Query();
lsmith's avatar
lsmith committed
405
     * $e = $q->expr;
406 407 408 409 410 411 412 413 414
     *
     * $q->select('u.*')
     *   ->from('User u')
     *   ->where($e->eq($e->div('id', 2), 12));
     * </code>
     *
     * @param string|array(string)
     * @return string an expression
     */
lsmith's avatar
lsmith committed
415 416
    public function div(array $args)
    {
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
        return $this->basicMath('/', $args);
    }

    /**
     * Returns the SQL to check if two values are equal.
     *
     * Example:
     * <code>
     * $q = new Doctrine_Query();
     * $q->select('u.*')
     *   ->from('User u')
     *   ->where($q->expr->eq('id', 1));
     * </code>
     *
     * @param string $value1 logical expression to compare
     * @param string $value2 logical expression to compare with
     * @return string logical expression
     */
lsmith's avatar
lsmith committed
435 436
    public function eq($value1, $value2)
    {
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
        $value1 = $this->getIdentifier($value1);
        $value2 = $this->getIdentifier($value2);
        return $value1 . ' = ' . $value2;
    }

    /**
     * Returns the SQL to check if two values are unequal.
     *
     * Example:
     * <code>
     * $q = new Doctrine_Query();
     * $q->select('u.*')
     *   ->from('User u')
     *   ->where($q->expr->neq('id', 1));
     * </code>
     *
     * @param string $value1 logical expression to compare
     * @param string $value2 logical expression to compare with
     * @return string logical expression
     */
lsmith's avatar
lsmith committed
457 458
    public function neq($value1, $value2)
    {
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
        $value1 = $this->getIdentifier($value1);
        $value2 = $this->getIdentifier($value2);
        return $value1 . ' <> ' . $value2;
    }

    /**
     * Returns the SQL to check if one value is greater than another value.
     *
     * Example:
     * <code>
     * $q = new Doctrine_Query();
     * $q->select('u.*')
     *   ->from('User u')
     *   ->where($q->expr->gt('id', 1));
     * </code>
     *
     * @param string $value1 logical expression to compare
     * @param string $value2 logical expression to compare with
     * @return string logical expression
     */
lsmith's avatar
lsmith committed
479 480
    public function gt($value1, $value2)
    {
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
        $value1 = $this->getIdentifier($value1);
        $value2 = $this->getIdentifier($value2);
        return $value1 . ' > ' . $value2;
    }

    /**
     * Returns the SQL to check if one value is greater than or equal to
     * another value.
     *
     * Example:
     * <code>
     * $q = new Doctrine_Query();
     * $q->select('u.*')
     *   ->from('User u')
     *   ->where($q->expr->gte('id', 1));
     * </code>
     *
     * @param string $value1 logical expression to compare
     * @param string $value2 logical expression to compare with
     * @return string logical expression
     */
lsmith's avatar
lsmith committed
502 503
    public function gte($value1, $value2)
    {
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
        $value1 = $this->getIdentifier($value1);
        $value2 = $this->getIdentifier($value2);
        return $value1 . ' >= ' . $value2;
    }

    /**
     * Returns the SQL to check if one value is less than another value.
     *
     * Example:
     * <code>
     * $q = new Doctrine_Query();
     * $q->select('u.*')
     *   ->from('User u')
     *   ->where($q->expr->lt('id', 1));
     * </code>
     *
     * @param string $value1        logical expression to compare
     * @param string $value2        logical expression to compare with
     * @return string logical expression
     */
lsmith's avatar
lsmith committed
524 525
    public function lt($value1, $value2)
    {
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
        $value1 = $this->getIdentifier($value1);
        $value2 = $this->getIdentifier($value2);
        return $value1 . ' < ' . $value2;
    }

    /**
     * Returns the SQL to check if one value is less than or equal to
     * another value.
     *
     * Example:
     * <code>
     * $q = new Doctrine_Query();
     * $q->select('u.*')
     *   ->from('User u')
     *   ->where($q->expr->lte('id', 1));
     * </code>
     *
     * @param string $value1        logical expression to compare
     * @param string $value2        logical expression to compare with
     * @return string logical expression
     */
lsmith's avatar
lsmith committed
547 548
    public function lte($value1, $value2)
    {
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
        $value1 = $this->getIdentifier($value1);
        $value2 = $this->getIdentifier($value2);
        return $value1 . ' <= ' . $value2;
    }

    /**
     * Returns the SQL to check if a value is one in a set of
     * given values..
     *
     * in() accepts an arbitrary number of parameters. The first parameter
     * must always specify the value that should be matched against. Successive
     * must contain a logical expression or an array with logical expressions.
     * These expressions will be matched against the first parameter.
     *
     * Example:
     * <code>
     * $q = new Doctrine_Query();
     * $q->select('u.*')
     *   ->from('User u')
     *   ->where($q->expr->in( 'id', array(1,2,3)));
     * </code>
     *
     * @param string $column        the value that should be matched against
     * @param string|array(string)  values that will be matched against $column
     * @return string logical expression
     */
lsmith's avatar
lsmith committed
575 576
    public function in($column, $values)
    {
lsmith's avatar
lsmith committed
577
        if ( ! is_array($values)) {
578
            $values = array($values);
lsmith's avatar
lsmith committed
579
        }
580 581 582
        $values = $this->getIdentifiers($values);
        $column = $this->getIdentifier($column);

lsmith's avatar
lsmith committed
583
        if (count($values) == 0) {
584
            throw new Doctrine_Expression_Exception('Values array for IN operator should not be empty.');
lsmith's avatar
lsmith committed
585
        }
zYne's avatar
zYne committed
586
        return $column . ' IN (' . implode(', ', $values) . ')';
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
    }
    /**
     * Returns SQL that checks if a expression is null.
     *
     * Example:
     * <code>
     * $q = new Doctrine_Query();
     * $q->select('u.*')
     *   ->from('User u')
     *   ->where($q->expr->isNull('id'));
     * </code>
     *
     * @param string $expression the expression that should be compared to null
     * @return string logical expression
     */
lsmith's avatar
lsmith committed
602 603
    public function isNull($expression)
    {
604 605 606
        $expression = $this->getIdentifier($expression);
        return $expression . ' IS NULL';
    }
zYne's avatar
zYne committed
607 608 609 610 611 612 613 614 615 616 617 618 619 620
    /**
     * Returns SQL that checks if a expression is not null.
     *
     * Example:
     * <code>
     * $q = new Doctrine_Query();
     * $q->select('u.*')
     *   ->from('User u')
     *   ->where($q->expr->isNotNull('id'));
     * </code>
     *
     * @param string $expression the expression that should be compared to null
     * @return string logical expression
     */
lsmith's avatar
lsmith committed
621 622
    public function isNotNull($expression)
    {
zYne's avatar
zYne committed
623 624 625
        $expression = $this->getIdentifier($expression);
        return $expression . ' IS NOT NULL';
    }
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
    /**
     * Returns SQL that checks if an expression evaluates to a value between
     * two values.
     *
     * The parameter $expression is checked if it is between $value1 and $value2.
     *
     * Note: There is a slight difference in the way BETWEEN works on some databases.
     * http://www.w3schools.com/sql/sql_between.asp. If you want complete database
     * independence you should avoid using between().
     *
     * Example:
     * <code>
     * $q = new Doctrine_Query();
     * $q->select('u.*')
     *   ->from('User u')
     *   ->where($q->expr->between('id', 1, 5));
     * </code>
     *
     * @param string $expression the value to compare to
     * @param string $value1 the lower value to compare with
     * @param string $value2 the higher value to compare with
     * @return string logical expression
     */
lsmith's avatar
lsmith committed
649 650
    public function between($expression, $value1, $value2)
    {
651 652 653 654 655
        $expression = $this->getIdentifier($expression);
        $value1 = $this->getIdentifier($value1);
        $value2 = $this->getIdentifier($value2);
        return $expression . ' BETWEEN ' .$value1 . ' AND ' . $value2;
    }
zYne's avatar
zYne committed
656 657 658 659 660 661 662 663 664
    /**
     * Returns global unique identifier
     *
     * @return string to get global unique identifier
     */
    public function guid()
    {
        throw new Doctrine_Expression_Exception('method not implemented');
    }
zYne's avatar
zYne committed
665 666 667 668 669 670 671 672 673
    /**
     * returns arcus cosine SQL string
     *
     * @return string
     */
    public function acos($value)
    {
        return 'ACOS(' . $value . ')';
    }
zYne's avatar
zYne committed
674 675 676 677 678 679 680 681 682 683
    /**
     * __call
     *
     * for all native RDBMS functions the function name itself is returned
     */
    public function __call($m, $a) 
    {
    	if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_EXPR) {
            throw new Doctrine_Expression_Exception('Unknown expression ' . $m);
        }
zYne's avatar
zYne committed
684
        return $m . '()';
zYne's avatar
zYne committed
685
    }
686
}