1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?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.org>.
*/
Doctrine::autoload('Doctrine_Expression_Driver');
/**
* Doctrine_Expression_Sqlite
*
* @package Doctrine
* @subpackage Expression
* @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>
*/
class Doctrine_Expression_Sqlite extends Doctrine_Expression_Driver
{
/**
* Returns the md5 sum of the data that SQLite's md5() function receives.
*
* @param mixed $data
* @return string
*/
public static function md5Impl($data)
{
return md5($data);
}
/**
* Returns the modules of the data that SQLite's mod() function receives.
*
* @param integer $dividend
* @param integer $divisor
* @return string
*/
public static function modImpl($dividend, $divisor)
{
return $dividend % $divisor;
}
/**
* 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);
}
public static function sha1Impl($str)
{
return sha1($str);
}
public static function ltrimImpl($str)
{
return ltrim($str);
}
public static function rtrimImpl($str)
{
return rtrim($str);
}
public static function trimImpl($str)
{
return trim($str);
}
/**
* returns the regular expression operator
*
* @return string
*/
public function regexp()
{
return 'RLIKE';
}
/**
* 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 . ')';
}
/**
* 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
*
* @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
*/
public function substring($value, $position, $length = null)
{
if ($length !== null) {
return 'SUBSTR(' . $value . ', ' . $position . ', ' . $length . ')';
}
return 'SUBSTR(' . $value . ', ' . $position . ', LENGTH(' . $value . '))';
}
}