# Source for file Sqlite.php

Documentation is available at Sqlite.php

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