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

/library/Doctrine/Doctrine/Expression/Pgsql.php

https://github.com/ostric/e-learning
PHP | 233 lines | 82 code | 17 blank | 134 comment | 6 complexity | dfaec7b92e949e6c8874473e7f885975 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: Pgsql.php 4958 2008-09-12 20:21:45Z 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_Pgsql
  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: 4958 $
  30. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  31. */
  32. class Doctrine_Expression_Pgsql extends Doctrine_Expression_Driver
  33. {
  34. /**
  35. * Returns the md5 sum of a field.
  36. *
  37. * Note: Not SQL92, but common functionality
  38. *
  39. * md5() works with the default PostgreSQL 8 versions.
  40. *
  41. * If you are using PostgreSQL 7.x or older you need
  42. * to make sure that the digest procedure is installed.
  43. * If you use RPMS (Redhat and Mandrake) install the postgresql-contrib
  44. * package. You must then install the procedure by running this shell command:
  45. * <code>
  46. * psql [dbname] < /usr/share/pgsql/contrib/pgcrypto.sql
  47. * </code>
  48. * You should make sure you run this as the postgres user.
  49. *
  50. * @return string
  51. */
  52. public function md5($column)
  53. {
  54. $column = $this->getIdentifier($column);
  55. return 'MD5(' . $column . ')';
  56. }
  57. /**
  58. * Returns part of a string.
  59. *
  60. * Note: Not SQL92, but common functionality.
  61. *
  62. * @param string $value the target $value the string or the string column.
  63. * @param int $from extract from this characeter.
  64. * @param int $len extract this amount of characters.
  65. * @return string sql that extracts part of a string.
  66. */
  67. public function substring($value, $from, $len = null)
  68. {
  69. $value = $this->getIdentifier($value);
  70. if ($len === null) {
  71. $len = $this->getIdentifier($len);
  72. return 'SUBSTR(' . $value . ', ' . $from . ')';
  73. } else {
  74. return 'SUBSTR(' . $value . ', ' . $from . ', ' . $len . ')';
  75. }
  76. }
  77. /**
  78. * Returns a series of strings concatinated
  79. *
  80. * concat() accepts an arbitrary number of parameters. Each parameter
  81. * must contain an expression or an array with expressions.
  82. *
  83. * @param string|array(string) strings that will be concatinated.
  84. * @return string
  85. */
  86. /**
  87. * PostgreSQLs AGE(<timestamp1> [, <timestamp2>]) function.
  88. *
  89. * @param string $timestamp1 timestamp to subtract from NOW()
  90. * @param string $timestamp2 optional; if given: subtract arguments
  91. * @return string
  92. */
  93. public function age($timestamp1, $timestamp2 = null) {
  94. if ( $timestamp2 == null ) {
  95. return 'AGE(' . $timestamp1 . ')';
  96. }
  97. return 'AGE(' . $timestamp1 . ', ' . $timestamp2 . ')';
  98. }
  99. /**
  100. * PostgreSQLs DATE_PART( <text>, <time> ) function.
  101. *
  102. * @param string $text what to extract
  103. * @param string $time timestamp or interval to extract from
  104. * @return string
  105. */
  106. public function date_part($text, $time) {
  107. return 'DATE_PART(' . $text . ', ' . $time . ')';
  108. }
  109. /**
  110. * PostgreSQLs TO_CHAR( <time>, <text> ) function.
  111. *
  112. * @param string $time timestamp or interval
  113. * @param string $text how to the format the output
  114. * @return string
  115. */
  116. public function to_char($time, $text) {
  117. return 'TO_CHAR(' . $time . ', ' . $text . ')';
  118. }
  119. /**
  120. * PostgreSQLs CONCAT() function
  121. *
  122. * @param an array of values
  123. * @return string
  124. */
  125. public function concat()
  126. {
  127. $args = func_get_args();
  128. return join(' || ' , $args);
  129. }
  130. /**
  131. * Returns the SQL string to return the current system date and time.
  132. *
  133. * @return string
  134. */
  135. public function now()
  136. {
  137. return 'LOCALTIMESTAMP(0)';
  138. }
  139. /**
  140. * regexp
  141. *
  142. * @return string the regular expression operator
  143. */
  144. public function regexp()
  145. {
  146. return 'SIMILAR TO';
  147. }
  148. /**
  149. * return string to call a function to get random value inside an SQL statement
  150. *
  151. * @return return string to generate float between 0 and 1
  152. * @access public
  153. */
  154. public function random()
  155. {
  156. return 'RANDOM()';
  157. }
  158. /**
  159. * build a pattern matching string
  160. *
  161. * EXPERIMENTAL
  162. *
  163. * WARNING: this function is experimental and may change signature at
  164. * any time until labelled as non-experimental
  165. *
  166. * @access public
  167. *
  168. * @param array $pattern even keys are strings, odd are patterns (% and _)
  169. * @param string $operator optional pattern operator (LIKE, ILIKE and maybe others in the future)
  170. * @param string $field optional field name that is being matched against
  171. * (might be required when emulating ILIKE)
  172. *
  173. * @return string SQL pattern
  174. */
  175. public function matchPattern($pattern, $operator = null, $field = null)
  176. {
  177. $match = '';
  178. if ( ! is_null($operator)) {
  179. $field = is_null($field) ? '' : $field.' ';
  180. $operator = strtoupper($operator);
  181. switch ($operator) {
  182. // case insensitive
  183. case 'ILIKE':
  184. $match = $field.'ILIKE ';
  185. break;
  186. // case sensitive
  187. case 'LIKE':
  188. $match = $field.'LIKE ';
  189. break;
  190. default:
  191. throw new Doctrine_Expression_Pgsql_Exception('not a supported operator type:'. $operator);
  192. }
  193. }
  194. $match.= "'";
  195. foreach ($pattern as $key => $value) {
  196. if ($key % 2) {
  197. $match.= $value;
  198. } else {
  199. $match.= $this->conn->escapePattern($this->conn->escape($value));
  200. }
  201. }
  202. $match.= "'";
  203. $match.= $this->patternEscapeString();
  204. return $match;
  205. }
  206. /**
  207. * return syntax for pgsql TRANSLATE() dbms function
  208. *
  209. * @return string $sql
  210. */
  211. public function translate($string, $from, $to)
  212. {
  213. $translate = 'TRANSLATE(' . $string . ', ' . $from . ', ' . $to . ')';
  214. return $translate;
  215. }
  216. }