Sqlite.php 5.07 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
<?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.phpdoctrine.com>.
 */
Doctrine::autoload('Doctrine_Expression');
/**
 * Doctrine_Expression_Sqlite
 *
zYne's avatar
zYne committed
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>
 */
33
class Doctrine_Expression_Sqlite extends Doctrine_Expression {
34 35 36
    /**
     * Returns the md5 sum of the data that SQLite's md5() function receives.
     *
37
     * @param mixed $data
38 39 40 41 42 43 44 45
     * @return string
     */
    public static function md5Impl($data) {
        return md5($data);
    }
    /**
     * Returns the modules of the data that SQLite's mod() function receives.
     *
46 47
     * @param integer $dividend
     * @param integer $divisor
48 49 50 51 52 53 54
     * @return string
     */
    public static function modImpl($dividend, $divisor) {
        return $dividend % $divisor;
    }

    /**
55
     * Returns a concatenation of the data that SQLite's concat() function receives.
56 57 58 59 60 61 62
     *
     * @return string
     */
    public static function concatImpl() {
        $args = func_get_args();
        return join( '', $args );
    }
63 64 65 66 67 68 69 70 71 72 73 74
    /**
     * locate
     * returns the position of the first occurrence of substring $substr in string $str that
     * SQLite's locate() function receives
     *
     * @param string $substr    literal string to find
     * @param string $str       literal string
     * @return string
     */
    public static function locateImpl($substr, $str) {
        return strpos($str, $substr);
    }
zYne's avatar
zYne committed
75
    public static function sha1Impl($str) {
76 77
        return sha1($str);                                  	
    }
zYne's avatar
zYne committed
78
    public static function ltrimImpl($str) {
79 80
        return ltrim($str);
    }
zYne's avatar
zYne committed
81
    public static function rtrimImpl($str) {
82 83
        return rtrim($str);
    }
zYne's avatar
zYne committed
84
    public static function trimImpl($str) {
85 86
        return trim($str);
    }
87
    /**
88
     * returns the regular expression operator 
89
     *
90 91 92 93 94
     * @return string
     */
    public function regexp() {
        return 'RLIKE';
    }
95 96 97 98 99 100 101 102 103 104 105 106 107
    /**
     * soundex
     * Returns a string to call a function to compute the 
     * 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
     */
    public function soundex($value) {
        return 'SOUNDEX(' . $value . ')';
    }
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    /**
     * Return string to call a variable with the current timestamp inside an SQL statement
     * There are three special variables for current date and time.
     *
     * @return string       sqlite function as string
     */
    public function now($type = 'timestamp') {
        switch ($type) {
        case 'time':
            return 'time(\'now\')';
        case 'date':
            return 'date(\'now\')';
        case 'timestamp':
        default:
            return 'datetime(\'now\')';
        }
    }
    /**
     * return string to call a function to get random value inside an SQL statement
     *
     * @return string to generate float between 0 and 1
     */
    public function random() {
        return '((RANDOM() + 2147483648) / 4294967296)';
    }
    /**
     * return string to call a function to get a substring inside an SQL statement
     *
     * Note: Not SQL92, but common functionality. 
     * 
     * SQLite only supports the 2 parameter variant of this function
139
     *
140 141 142 143
     * @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
144
     */
145
    public function substring($value, $position, $length = null) {
146 147
        if($length !== null)
            return 'SUBSTR(' . $value . ', ' . $position . ', ' . $length . ')';
148

149
        return 'SUBSTR(' . $value . ', ' . $position . ', LENGTH(' . $value . '))';
150 151
    }
}