Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id$
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * Doctrine_Expression_Driver
24
 *
25
 * @package     Doctrine
26
 * @subpackage  Expression
27
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28
 * @link        www.doctrine-project.org
29
 * @since       1.0
30
 * @version     $Revision$
31
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
32
 */
33
class Doctrine_Expression_Driver extends Doctrine_Connection_Module
34
{
35
    public function getIdentifier($column)
36
    {
37
        return $column;
38
    }
39
 
40
    public function getIdentifiers($columns)
41
    {
42
        return $columns;
43
    }
44
 
45
    /**
46
     * regexp
47
     * returns the regular expression operator
48
     *
49
     * @return string
50
     */
51
    public function regexp()
52
    {
53
        throw new Doctrine_Expression_Exception('Regular expression operator is not supported by this database driver.');
54
    }
55
 
56
    /**
57
     * Returns the average value of a column
58
     *
59
     * @param string $column    the column to use
60
     * @return string           generated sql including an AVG aggregate function
61
     */
62
    public function avg($column)
63
    {
64
        $column = $this->getIdentifier($column);
65
        return 'AVG(' .  $column . ')';
66
    }
67
 
68
    /**
69
     * Returns the number of rows (without a NULL value) of a column
70
     *
71
     * If a '*' is used instead of a column the number of selected rows
72
     * is returned.
73
     *
74
     * @param string|integer $column    the column to use
75
     * @return string                   generated sql including a COUNT aggregate function
76
     */
77
    public function count($column)
78
    {
79
        $column = $this->getIdentifier($column);
80
        return 'COUNT(' . $column . ')';
81
    }
82
 
83
    /**
84
     * Returns the highest value of a column
85
     *
86
     * @param string $column    the column to use
87
     * @return string           generated sql including a MAX aggregate function
88
     */
89
    public function max($column)
90
    {
91
        $column = $this->getIdentifier($column);
92
        return 'MAX(' . $column . ')';
93
    }
94
 
95
    /**
96
     * Returns the lowest value of a column
97
     *
98
     * @param string $column the column to use
99
     * @return string
100
     */
101
    public function min($column)
102
    {
103
        $column = $this->getIdentifier($column);
104
        return 'MIN(' . $column . ')';
105
    }
106
 
107
    /**
108
     * Returns the total sum of a column
109
     *
110
     * @param string $column the column to use
111
     * @return string
112
     */
113
    public function sum($column)
114
    {
115
        $column = $this->getIdentifier($column);
116
        return 'SUM(' . $column . ')';
117
    }
118
 
119
    // scalar functions
120
 
121
    /**
122
     * Returns the md5 sum of a field.
123
     *
124
     * Note: Not SQL92, but common functionality
125
     *
126
     * @return string
127
     */
128
    public function md5($column)
129
    {
130
        $column = $this->getIdentifier($column);
131
        return 'MD5(' . $column . ')';
132
    }
133
 
134
    /**
135
     * Returns the length of a text field.
136
     *
137
     * @param string $expression1
138
     * @param string $expression2
139
     * @return string
140
     */
141
    public function length($column)
142
    {
143
        $column = $this->getIdentifier($column);
144
        return 'LENGTH(' . $column . ')';
145
    }
146
 
147
    /**
148
     * Rounds a numeric field to the number of decimals specified.
149
     *
150
     * @param string $expression1
151
     * @param string $expression2
152
     * @return string
153
     */
154
    public function round($column, $decimals = 0)
155
    {
156
        $column = $this->getIdentifier($column);
157
 
158
        return 'ROUND(' . $column . ', ' . $decimals . ')';
159
    }
160
 
161
    /**
162
     * Returns the remainder of the division operation
163
     * $expression1 / $expression2.
164
     *
165
     * @param string $expression1
166
     * @param string $expression2
167
     * @return string
168
     */
169
    public function mod($expression1, $expression2)
170
    {
171
        $expression1 = $this->getIdentifier($expression1);
172
        $expression2 = $this->getIdentifier($expression2);
173
        return 'MOD(' . $expression1 . ', ' . $expression2 . ')';
174
    }
175
 
176
    /**
177
     * trim
178
     * returns the string $str with leading and proceeding space characters removed
179
     *
180
     * @param string $str       literal string or column name
181
     * @return string
182
     */
183
    public function trim($str)
184
    {
185
        return 'TRIM(' . $str . ')';
186
    }
187
 
188
    /**
189
     * rtrim
190
     * returns the string $str with proceeding space characters removed
191
     *
192
     * @param string $str       literal string or column name
193
     * @return string
194
     */
195
    public function rtrim($str)
196
    {
197
        return 'RTRIM(' . $str . ')';
198
    }
199
 
200
    /**
201
     * ltrim
202
     * returns the string $str with leading space characters removed
203
     *
204
     * @param string $str       literal string or column name
205
     * @return string
206
     */
207
    public function ltrim($str)
208
    {
209
        return 'LTRIM(' . $str . ')';
210
    }
211
 
212
    /**
213
     * upper
214
     * Returns the string $str with all characters changed to
215
     * uppercase according to the current character set mapping.
216
     *
217
     * @param string $str       literal string or column name
218
     * @return string
219
     */
220
    public function upper($str)
221
    {
222
        return 'UPPER(' . $str . ')';
223
    }
224
 
225
    /**
226
     * lower
227
     * Returns the string $str with all characters changed to
228
     * lowercase according to the current character set mapping.
229
     *
230
     * @param string $str       literal string or column name
231
     * @return string
232
     */
233
    public function lower($str)
234
    {
235
        return 'LOWER(' . $str . ')';
236
    }
237
 
238
    /**
239
     * locate
240
     * returns the position of the first occurrence of substring $substr in string $str
241
     *
242
     * @param string $substr    literal string to find
243
     * @param string $str       literal string
244
     * @return integer
245
     */
246
    public function locate($str, $substr)
247
    {
248
        return 'LOCATE(' . $str . ', ' . $substr . ')';
249
    }
250
 
251
    /**
252
     * Returns the current system date.
253
     *
254
     * @return string
255
     */
256
    public function now()
257
    {
258
        return 'NOW()';
259
    }
260
 
261
    /**
262
     * soundex
263
     * Returns a string to call a function to compute the
264
     * soundex encoding of a string
265
     *
266
     * The string "?000" is returned if the argument is NULL.
267
     *
268
     * @param string $value
269
     * @return string   SQL soundex function with given parameter
270
     */
271
    public function soundex($value)
272
    {
273
        throw new Doctrine_Expression_Exception('SQL soundex function not supported by this driver.');
274
    }
275
 
276
    /**
277
     * return string to call a function to get a substring inside an SQL statement
278
     *
279
     * Note: Not SQL92, but common functionality.
280
     *
281
     * SQLite only supports the 2 parameter variant of this function
282
     *
283
     * @param string $value         an sql string literal or column name/alias
284
     * @param integer $position     where to start the substring portion
285
     * @param integer $length       the substring portion length
286
     * @return string               SQL substring function with given parameters
287
     */
288
    public function substring($value, $from, $len = null)
289
    {
290
        $value = $this->getIdentifier($value);
291
        if ($len === null)
292
            return 'SUBSTRING(' . $value . ' FROM ' . $from . ')';
293
        else {
294
            $len = $this->getIdentifier($len);
295
            return 'SUBSTRING(' . $value . ' FROM ' . $from . ' FOR ' . $len . ')';
296
        }
297
    }
298
 
299
    /**
300
     * Returns a series of strings concatinated
301
     *
302
     * concat() accepts an arbitrary number of parameters. Each parameter
303
     * must contain an expression or an array with expressions.
304
     *
305
     * @param string|array(string) strings that will be concatinated.
306
     */
307
    public function concat()
308
    {
309
        $args = func_get_args();
310
 
311
        return 'CONCAT(' . join(', ', (array) $args) . ')';
312
    }
313
 
314
    /**
315
     * Returns the SQL for a logical not.
316
     *
317
     * @return string a logical expression
318
     */
319
    public function not($expression)
320
    {
321
        $expression = $this->getIdentifier($expression);
322
        return 'NOT(' . $expression . ')';
323
    }
324
 
325
    /**
326
     * Returns the SQL to perform the same mathematical operation over an array
327
     * of values or expressions.
328
     *
329
     * basicMath() accepts an arbitrary number of parameters. Each parameter
330
     * must contain a value or an expression or an array with values or
331
     * expressions.
332
     *
333
     * @param string $type the type of operation, can be '+', '-', '*' or '/'.
334
     * @param string|array(string)
335
     * @return string an expression
336
     */
337
    private function basicMath($type, array $args)
338
    {
339
        $elements = $this->getIdentifiers($args);
340
        if (count($elements) < 1) {
341
            return '';
342
        }
343
        if (count($elements) == 1) {
344
            return $elements[0];
345
        } else {
346
            return '(' . implode(' ' . $type . ' ', $elements) . ')';
347
        }
348
    }
349
 
350
    /**
351
     * Returns the SQL to add values or expressions together.
352
     *
353
     * add() accepts an arbitrary number of parameters. Each parameter
354
     * must contain a value or an expression or an array with values or
355
     * expressions.
356
     *
357
     * @param string|array(string)
358
     * @return string an expression
359
     */
360
    public function add(array $args)
361
    {
362
        return $this->basicMath('+', $args);
363
    }
364
 
365
    /**
366
     * Returns the SQL to subtract values or expressions from eachother.
367
     *
368
     * subtract() accepts an arbitrary number of parameters. Each parameter
369
     * must contain a value or an expression or an array with values or
370
     * expressions.
371
     *
372
     * @param string|array(string)
373
     * @return string an expression
374
     */
375
    public function sub(array $args)
376
    {
377
        return $this->basicMath('-', $args );
378
    }
379
 
380
    /**
381
     * Returns the SQL to multiply values or expressions by eachother.
382
     *
383
     * multiply() accepts an arbitrary number of parameters. Each parameter
384
     * must contain a value or an expression or an array with values or
385
     * expressions.
386
     *
387
     * @param string|array(string)
388
     * @return string an expression
389
     */
390
    public function mul(array $args)
391
    {
392
        return $this->basicMath('*', $args);
393
    }
394
 
395
    /**
396
     * Returns the SQL to divide values or expressions by eachother.
397
     *
398
     * divide() accepts an arbitrary number of parameters. Each parameter
399
     * must contain a value or an expression or an array with values or
400
     * expressions.
401
     *
402
     * @param string|array(string)
403
     * @return string an expression
404
     */
405
    public function div(array $args)
406
    {
407
        return $this->basicMath('/', $args);
408
    }
409
 
410
    /**
411
     * Returns the SQL to check if two values are equal.
412
     *
413
     * @param string $value1 logical expression to compare
414
     * @param string $value2 logical expression to compare with
415
     * @return string logical expression
416
     */
417
    public function eq($value1, $value2)
418
    {
419
        $value1 = $this->getIdentifier($value1);
420
        $value2 = $this->getIdentifier($value2);
421
        return $value1 . ' = ' . $value2;
422
    }
423
 
424
    /**
425
     * Returns the SQL to check if two values are unequal.
426
     *
427
     * @param string $value1 logical expression to compare
428
     * @param string $value2 logical expression to compare with
429
     * @return string logical expression
430
     */
431
    public function neq($value1, $value2)
432
    {
433
        $value1 = $this->getIdentifier($value1);
434
        $value2 = $this->getIdentifier($value2);
435
        return $value1 . ' <> ' . $value2;
436
    }
437
 
438
    /**
439
     * Returns the SQL to check if one value is greater than another value.
440
     *
441
     * @param string $value1 logical expression to compare
442
     * @param string $value2 logical expression to compare with
443
     * @return string logical expression
444
     */
445
    public function gt($value1, $value2)
446
    {
447
        $value1 = $this->getIdentifier($value1);
448
        $value2 = $this->getIdentifier($value2);
449
        return $value1 . ' > ' . $value2;
450
    }
451
 
452
    /**
453
     * Returns the SQL to check if one value is greater than or equal to
454
     * another value.
455
     *
456
     * @param string $value1 logical expression to compare
457
     * @param string $value2 logical expression to compare with
458
     * @return string logical expression
459
     */
460
    public function gte($value1, $value2)
461
    {
462
        $value1 = $this->getIdentifier($value1);
463
        $value2 = $this->getIdentifier($value2);
464
        return $value1 . ' >= ' . $value2;
465
    }
466
 
467
    /**
468
     * Returns the SQL to check if one value is less than another value.
469
     *
470
     * @param string $value1        logical expression to compare
471
     * @param string $value2        logical expression to compare with
472
     * @return string logical expression
473
     */
474
    public function lt($value1, $value2)
475
    {
476
        $value1 = $this->getIdentifier($value1);
477
        $value2 = $this->getIdentifier($value2);
478
        return $value1 . ' < ' . $value2;
479
    }
480
 
481
    /**
482
     * Returns the SQL to check if one value is less than or equal to
483
     * another value.
484
     *
485
     * @param string $value1        logical expression to compare
486
     * @param string $value2        logical expression to compare with
487
     * @return string logical expression
488
     */
489
    public function lte($value1, $value2)
490
    {
491
        $value1 = $this->getIdentifier($value1);
492
        $value2 = $this->getIdentifier($value2);
493
        return $value1 . ' <= ' . $value2;
494
    }
495
 
496
    /**
497
     * Returns the SQL to check if a value is one in a set of
498
     * given values..
499
     *
500
     * in() accepts an arbitrary number of parameters. The first parameter
501
     * must always specify the value that should be matched against. Successive
502
     * must contain a logical expression or an array with logical expressions.
503
     * These expressions will be matched against the first parameter.
504
     *
505
     * @param string $column        the value that should be matched against
506
     * @param string|array(string)  values that will be matched against $column
507
     * @return string logical expression
508
     */
509
    public function in($column, $values)
510
    {
511
        if ( ! is_array($values)) {
512
            $values = array($values);
513
        }
514
        $values = $this->getIdentifiers($values);
515
        $column = $this->getIdentifier($column);
516
 
517
        if (count($values) == 0) {
518
            throw new Doctrine_Expression_Exception('Values array for IN operator should not be empty.');
519
        }
520
        return $column . ' IN (' . implode(', ', $values) . ')';
521
    }
522
 
523
    /**
524
     * Returns SQL that checks if a expression is null.
525
     *
526
     * @param string $expression the expression that should be compared to null
527
     * @return string logical expression
528
     */
529
    public function isNull($expression)
530
    {
531
        $expression = $this->getIdentifier($expression);
532
        return $expression . ' IS NULL';
533
    }
534
 
535
    /**
536
     * Returns SQL that checks if a expression is not null.
537
     *
538
     * @param string $expression the expression that should be compared to null
539
     * @return string logical expression
540
     */
541
    public function isNotNull($expression)
542
    {
543
        $expression = $this->getIdentifier($expression);
544
        return $expression . ' IS NOT NULL';
545
    }
546
 
547
    /**
548
     * Returns SQL that checks if an expression evaluates to a value between
549
     * two values.
550
     *
551
     * The parameter $expression is checked if it is between $value1 and $value2.
552
     *
553
     * Note: There is a slight difference in the way BETWEEN works on some databases.
554
     * http://www.w3schools.com/sql/sql_between.asp. If you want complete database
555
     * independence you should avoid using between().
556
     *
557
     * @param string $expression the value to compare to
558
     * @param string $value1 the lower value to compare with
559
     * @param string $value2 the higher value to compare with
560
     * @return string logical expression
561
     */
562
    public function between($expression, $value1, $value2)
563
    {
564
        $expression = $this->getIdentifier($expression);
565
        $value1 = $this->getIdentifier($value1);
566
        $value2 = $this->getIdentifier($value2);
567
        return $expression . ' BETWEEN ' .$value1 . ' AND ' . $value2;
568
    }
569
 
570
    /**
571
     * Returns global unique identifier
572
     *
573
     * @return string to get global unique identifier
574
     */
575
    public function guid()
576
    {
577
        throw new Doctrine_Expression_Exception('method not implemented');
578
    }
579
 
580
    /**
581
     * returns arcus cosine SQL string
582
     *
583
     * @return string
584
     */
585
    public function acos($value)
586
    {
587
        return 'ACOS(' . $value . ')';
588
    }
589
 
590
    /**
591
     * sin
592
     *
593
     * @param string $value
594
     * @return void
595
     */
596
    public function sin($value)
597
    {
598
        return 'SIN(' . $value . ')';
599
    }
600
 
601
    /**
602
     * pi
603
     *
604
     * @return void
605
     */
606
    public function pi()
607
    {
608
        return 'PI()';
609
    }
610
 
611
    /**
612
     * cos
613
     *
614
     * @param string $value
615
     * @return void
616
     */
617
    public function cos($value)
618
    {
619
        return 'COS(' . $value . ')';
620
    }
621
 
622
    /**
623
     * coalesce
624
     *
625
     * @return string
626
     */
627
    public function coalesce()
628
    {
629
        $args = func_get_args();
630
 
631
	    return 'COALESCE(' . join(', ', (array) $args) . ')';
632
    }
633
 
634
    /**
635
     * __call
636
     *
637
     * for all native RDBMS functions the function name itself is returned
638
     */
639
    public function __call($m, $a)
640
    {
641
        if ($this->conn->getAttribute(Doctrine_Core::ATTR_PORTABILITY) & Doctrine_Core::PORTABILITY_EXPR) {
642
            throw new Doctrine_Expression_Exception('Unknown expression: ' . $m);
643
        }
644
 
645
        return $m . '(' . implode(', ', $a) . ')';
646
    }
647
}