Sqlite.php 5.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?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>.
 */
21
Doctrine::autoload('Doctrine_Expression_Driver');
22 23 24
/**
 * Doctrine_Expression_Sqlite
 *
zYne's avatar
zYne committed
25
 * @package     Doctrine
26
 * @subpackage  Expression
zYne's avatar
zYne committed
27 28 29 30 31 32
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
33
class Doctrine_Expression_Sqlite extends Doctrine_Expression_Driver
lsmith's avatar
lsmith committed
34
{
35 36 37
    /**
     * Returns the md5 sum of the data that SQLite's md5() function receives.
     *
38
     * @param mixed $data
39 40
     * @return string
     */
lsmith's avatar
lsmith committed
41 42
    public static function md5Impl($data)
    {
43 44 45 46 47
        return md5($data);
    }
    /**
     * Returns the modules of the data that SQLite's mod() function receives.
     *
48 49
     * @param integer $dividend
     * @param integer $divisor
50 51
     * @return string
     */
lsmith's avatar
lsmith committed
52 53
    public static function modImpl($dividend, $divisor)
    {
54 55 56 57
        return $dividend % $divisor;
    }

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