| 1 | <?php |
| 2 | /* |
| 3 | * $Id: Sqlite.php 2963 2007-10-21 06:23:59Z Jonathan.Wage $ |
| 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.phpdoctrine.org>. |
| 20 | */ |
| 21 | Doctrine::autoload('Doctrine_Expression_Driver'); |
| 22 | /** |
| 23 | * Doctrine_Expression_Sqlite |
| 24 | * |
| 25 | * @package Doctrine |
| 26 | * @subpackage Expression |
| 27 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
| 28 | * @link www.phpdoctrine.org |
| 29 | * @since 1.0 |
| 30 | * @version $Revision: 2963 $ |
| 31 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi> |
| 32 | */ |
| 33 | class Doctrine_Expression_Sqlite extends Doctrine_Expression_Driver |
| 34 | { |
| 35 | /** |
| 36 | * Returns the md5 sum of the data that SQLite's md5() function receives. |
| 37 | * |
| 38 | * @param mixed $data |
| 39 | * @return string |
| 40 | */ |
| 41 | public static function md5Impl($data) |
| 42 | { |
| 43 | return md5($data); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns the modules of the data that SQLite's mod() function receives. |
| 48 | * |
| 49 | * @param integer $dividend |
| 50 | * @param integer $divisor |
| 51 | * @return string |
| 52 | */ |
| 53 | public static function modImpl($dividend, $divisor) |
| 54 | { |
| 55 | return $dividend % $divisor; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns a concatenation of the data that SQLite's concat() function receives. |
| 60 | * |
| 61 | * @return string |
| 62 | */ |
| 63 | public static function concatImpl() |
| 64 | { |
| 65 | $args = func_get_args(); |
| 66 | return join('', $args); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * locate |
| 71 | * returns the position of the first occurrence of substring $substr in string $str that |
| 72 | * SQLite's locate() function receives |
| 73 | * |
| 74 | * @param string $substr literal string to find |
| 75 | * @param string $str literal string |
| 76 | * @return string |
| 77 | */ |
| 78 | public static function locateImpl($substr, $str) |
| 79 | { |
| 80 | return strpos($str, $substr); |
| 81 | } |
| 82 | public static function sha1Impl($str) |
| 83 | { |
| 84 | return sha1($str); |
| 85 | } |
| 86 | public static function ltrimImpl($str) |
| 87 | { |
| 88 | return ltrim($str); |
| 89 | } |
| 90 | public static function rtrimImpl($str) |
| 91 | { |
| 92 | return rtrim($str); |
| 93 | } |
| 94 | public static function trimImpl($str) |
| 95 | { |
| 96 | return trim($str); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * returns the regular expression operator |
| 101 | * |
| 102 | * @return string |
| 103 | */ |
| 104 | public function regexp() |
| 105 | { |
| 106 | return 'RLIKE'; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * soundex |
| 111 | * Returns a string to call a function to compute the |
| 112 | * soundex encoding of a string |
| 113 | * |
| 114 | * The string "?000" is returned if the argument is NULL. |
| 115 | * |
| 116 | * @param string $value |
| 117 | * @return string SQL soundex function with given parameter |
| 118 | */ |
| 119 | public function soundex($value) |
| 120 | { |
| 121 | return 'SOUNDEX(' . $value . ')'; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Return string to call a variable with the current timestamp inside an SQL statement |
| 126 | * There are three special variables for current date and time. |
| 127 | * |
| 128 | * @return string sqlite function as string |
| 129 | */ |
| 130 | public function now($type = 'timestamp') |
| 131 | { |
| 132 | switch ($type) { |
| 133 | case 'time': |
| 134 | return 'time(\'now\')'; |
| 135 | case 'date': |
| 136 | return 'date(\'now\')'; |
| 137 | case 'timestamp': |
| 138 | default: |
| 139 | return 'datetime(\'now\')'; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * return string to call a function to get random value inside an SQL statement |
| 145 | * |
| 146 | * @return string to generate float between 0 and 1 |
| 147 | */ |
| 148 | public function random() |
| 149 | { |
| 150 | return '((RANDOM() + 2147483648) / 4294967296)'; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * return string to call a function to get a substring inside an SQL statement |
| 155 | * |
| 156 | * Note: Not SQL92, but common functionality. |
| 157 | * |
| 158 | * SQLite only supports the 2 parameter variant of this function |
| 159 | * |
| 160 | * @param string $value an sql string literal or column name/alias |
| 161 | * @param integer $position where to start the substring portion |
| 162 | * @param integer $length the substring portion length |
| 163 | * @return string SQL substring function with given parameters |
| 164 | */ |
| 165 | public function substring($value, $position, $length = null) |
| 166 | { |
| 167 | if ($length !== null) { |
| 168 | return 'SUBSTR(' . $value . ', ' . $position . ', ' . $length . ')'; |
| 169 | } |
| 170 | return 'SUBSTR(' . $value . ', ' . $position . ', LENGTH(' . $value . '))'; |
| 171 | } |
| 172 | } |