PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/sbweb/sbweb_logica/lib/symfony/plugins/sfPropelPlugin/lib/vendor/propel/adapter/DBMSSQL.php

http://opac-sbweb.googlecode.com/
PHP | 206 lines | 104 code | 21 blank | 81 comment | 17 complexity | 90da75bc6fddece687ca49ba1d69401c MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. /*
  3. * $Id: DBMSSQL.php 989 2008-03-11 14:29:30Z heltem $
  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 please see
  19. * <http://propel.phpdb.org>.
  20. */
  21. /**
  22. * This is used to connect to a MSSQL database.
  23. *
  24. * @author Hans Lellelid <hans@xmpl.org> (Propel)
  25. * @version $Revision: 989 $
  26. * @package propel.adapter
  27. */
  28. class DBMSSQL extends DBAdapter {
  29. /**
  30. * This method is used to ignore case.
  31. *
  32. * @param in The string to transform to upper case.
  33. * @return The upper case string.
  34. */
  35. public function toUpperCase($in)
  36. {
  37. return "UPPER(" . $in . ")";
  38. }
  39. /**
  40. * This method is used to ignore case.
  41. *
  42. * @param in The string whose case to ignore.
  43. * @return The string in a case that can be ignored.
  44. */
  45. public function ignoreCase($in)
  46. {
  47. return "UPPER(" . $in . ")";
  48. }
  49. /**
  50. * Returns SQL which concatenates the second string to the first.
  51. *
  52. * @param string String to concatenate.
  53. * @param string String to append.
  54. * @return string
  55. */
  56. public function concatString($s1, $s2)
  57. {
  58. return "($s1 + $s2)";
  59. }
  60. /**
  61. * Returns SQL which extracts a substring.
  62. *
  63. * @param string String to extract from.
  64. * @param int Offset to start from.
  65. * @param int Number of characters to extract.
  66. * @return string
  67. */
  68. public function subString($s, $pos, $len)
  69. {
  70. return "SUBSTRING($s, $pos, $len)";
  71. }
  72. /**
  73. * Returns SQL which calculates the length (in chars) of a string.
  74. *
  75. * @param string String to calculate length of.
  76. * @return string
  77. */
  78. public function strLength($s)
  79. {
  80. return "LEN($s)";
  81. }
  82. /**
  83. * @see DBAdapter::quoteIdentifier()
  84. */
  85. public function quoteIdentifier($text)
  86. {
  87. return '[' . $text . ']';
  88. }
  89. /**
  90. * @see DBAdapter::random()
  91. */
  92. public function random($seed = null)
  93. {
  94. return 'rand('.((int) $seed).')';
  95. }
  96. /**
  97. * Simulated Limit/Offset
  98. * This rewrites the $sql query to apply the offset and limit.
  99. * @see DBAdapter::applyLimit()
  100. * @author Justin Carlson <justin.carlson@gmail.com>
  101. */
  102. public function applyLimit(&$sql, $offset, $limit)
  103. {
  104. // make sure offset and limit are numeric
  105. if (!is_numeric($offset) || !is_numeric($limit)){
  106. throw new Exception("DBMSSQL ::applyLimit() expects a number for argument 2 and 3");
  107. }
  108. // obtain the original select statement
  109. preg_match('/\A(.*)select(.*)from/si',$sql,$select_segment);
  110. if (count($select_segment)>0)
  111. {
  112. $original_select = $select_segment[0];
  113. } else {
  114. throw new Exception("DBMSSQL ::applyLimit() could not locate the select statement at the start of the query. ");
  115. }
  116. $modified_select = substr_replace($original_select, null, stristr($original_select,'select') , 6 );
  117. // obtain the original order by clause, or create one if there isn't one
  118. preg_match('/order by(.*)\Z/si',$sql,$order_segment);
  119. if (count($order_segment)>0)
  120. {
  121. $order_by = $order_segment[0];
  122. } else {
  123. // no order by clause, if there are columns we can attempt to sort by the columns in the select statement
  124. $select_items = split(',',$modified_select);
  125. if (count($select_items)>0)
  126. {
  127. $item_number = 0;
  128. $order_by = null;
  129. while ($order_by === null && $item_number<count($select_items))
  130. {
  131. if ($select_items[$item_number]!='*' && !strstr($select_items[$item_number],'('))
  132. {
  133. $order_by = 'order by ' . $select_items[0] . ' asc';
  134. }
  135. $item_number++;
  136. }
  137. }
  138. if ($order_by === null)
  139. {
  140. throw new Exception("DBMSSQL ::applyLimit() could not locate the order by statement at the end of your query or any columns at the start of your query. ");
  141. } else {
  142. $sql.= ' ' . $order_by;
  143. }
  144. }
  145. // remove the original select statement
  146. $sql = str_replace($original_select , null, $sql);
  147. /* modify the sort order by for paging */
  148. $inverted_order = '';
  149. $order_columns = split(',',str_ireplace('order by ','',$order_by));
  150. $original_order_by = $order_by;
  151. $order_by = '';
  152. foreach ($order_columns as $column)
  153. {
  154. // strip "table." from order by columns
  155. $column = array_reverse(split("\.",$column));
  156. $column = $column[0];
  157. // commas if we have multiple sort columns
  158. if (strlen($inverted_order)>0){
  159. $order_by.= ', ';
  160. $inverted_order.=', ';
  161. }
  162. // put together order for paging wrapper
  163. if (stristr($column,' desc'))
  164. {
  165. $order_by .= $column;
  166. $inverted_order .= str_ireplace(' desc',' asc',$column);
  167. } elseif (stristr($column,' asc')) {
  168. $order_by .= $column;
  169. $inverted_order .= str_ireplace(' asc',' desc',$column);
  170. } else {
  171. $order_by .= $column;
  172. $inverted_order .= $column .' desc';
  173. }
  174. }
  175. $order_by = 'order by ' . $order_by;
  176. $inverted_order = 'order by ' . $inverted_order;
  177. // build the query
  178. $offset = ($limit+$offset);
  179. $modified_sql = 'select * from (';
  180. $modified_sql.= 'select top '.$limit.' * from (';
  181. $modified_sql.= 'select top '.$offset.' '.$modified_select.$sql;
  182. $modified_sql.= ') deriveda '.$inverted_order.') derivedb '.$order_by;
  183. $sql = $modified_sql;
  184. }
  185. }