| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: Sqlite.php 7490 2010-03-29 19:53:27Z jwage $
|
|
|
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_Sqlite
|
|
|
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: 7490 $
|
|
|
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 |
|
|
|
83 |
public static function sha1Impl($str)
|
|
|
84 |
{
|
|
|
85 |
return sha1($str);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
public static function ltrimImpl($str)
|
|
|
89 |
{
|
|
|
90 |
return ltrim($str);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
public static function rtrimImpl($str)
|
|
|
94 |
{
|
|
|
95 |
return rtrim($str);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
public static function trimImpl($str)
|
|
|
99 |
{
|
|
|
100 |
return trim($str);
|
|
|
101 |
}
|
|
|
102 |
public static function nowImpl()
|
|
|
103 |
{
|
|
|
104 |
return date('Y-m-d h:i:s');
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* returns the regular expression operator
|
|
|
109 |
*
|
|
|
110 |
* @return string
|
|
|
111 |
*/
|
|
|
112 |
public function regexp()
|
|
|
113 |
{
|
|
|
114 |
return 'RLIKE';
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* soundex
|
|
|
119 |
* Returns a string to call a function to compute the
|
|
|
120 |
* soundex encoding of a string
|
|
|
121 |
*
|
|
|
122 |
* The string "?000" is returned if the argument is NULL.
|
|
|
123 |
*
|
|
|
124 |
* @param string $value
|
|
|
125 |
* @return string SQL soundex function with given parameter
|
|
|
126 |
*/
|
|
|
127 |
public function soundex($value)
|
|
|
128 |
{
|
|
|
129 |
return 'SOUNDEX(' . $value . ')';
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Return string to call a variable with the current timestamp inside an SQL statement
|
|
|
134 |
* There are three special variables for current date and time.
|
|
|
135 |
*
|
|
|
136 |
* @return string sqlite function as string
|
|
|
137 |
*/
|
|
|
138 |
public function now($type = 'timestamp')
|
|
|
139 |
{
|
|
|
140 |
switch ($type) {
|
|
|
141 |
case 'time':
|
|
|
142 |
return 'time(\'now\')';
|
|
|
143 |
case 'date':
|
|
|
144 |
return 'date(\'now\')';
|
|
|
145 |
case 'timestamp':
|
|
|
146 |
default:
|
|
|
147 |
return 'datetime(\'now\')';
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
* return string to call a function to get random value inside an SQL statement
|
|
|
153 |
*
|
|
|
154 |
* @return string to generate float between 0 and 1
|
|
|
155 |
*/
|
|
|
156 |
public function random()
|
|
|
157 |
{
|
|
|
158 |
return '((RANDOM() + 2147483648) / 4294967296)';
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* return string to call a function to get a substring inside an SQL statement
|
|
|
163 |
*
|
|
|
164 |
* Note: Not SQL92, but common functionality.
|
|
|
165 |
*
|
|
|
166 |
* SQLite only supports the 2 parameter variant of this function
|
|
|
167 |
*
|
|
|
168 |
* @param string $value an sql string literal or column name/alias
|
|
|
169 |
* @param integer $position where to start the substring portion
|
|
|
170 |
* @param integer $length the substring portion length
|
|
|
171 |
* @return string SQL substring function with given parameters
|
|
|
172 |
*/
|
|
|
173 |
public function substring($value, $position, $length = null)
|
|
|
174 |
{
|
|
|
175 |
if ($length !== null) {
|
|
|
176 |
return 'SUBSTR(' . $value . ', ' . $position . ', ' . $length . ')';
|
|
|
177 |
}
|
|
|
178 |
return 'SUBSTR(' . $value . ', ' . $position . ', LENGTH(' . $value . '))';
|
|
|
179 |
}
|
|
|
180 |
}
|