PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Doctrine/Doctrine/Expression/Sqlite.php

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