mssql.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
  6. // | Stig. S. Bakken, Lukas Smith |
  7. // | All rights reserved. |
  8. // +----------------------------------------------------------------------+
  9. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
  10. // | API as well as database abstraction for PHP applications. |
  11. // | This LICENSE is in the BSD license style. |
  12. // | |
  13. // | Redistribution and use in source and binary forms, with or without |
  14. // | modification, are permitted provided that the following conditions |
  15. // | are met: |
  16. // | |
  17. // | Redistributions of source code must retain the above copyright |
  18. // | notice, this list of conditions and the following disclaimer. |
  19. // | |
  20. // | Redistributions in binary form must reproduce the above copyright |
  21. // | notice, this list of conditions and the following disclaimer in the |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // | |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission. |
  28. // | |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
  40. // | POSSIBILITY OF SUCH DAMAGE. |
  41. // +----------------------------------------------------------------------+
  42. // | Author: Frank M. Kromann <frank@kromann.info> |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id$
  46. //
  47. require_once 'MDB2/Driver/Function/Common.php';
  48. // {{{ class MDB2_Driver_Function_mssql
  49. /**
  50. * MDB2 MSSQL driver for the function modules
  51. *
  52. * @package MDB2
  53. * @category Database
  54. * @author Lukas Smith <smith@pooteeweet.org>
  55. */
  56. class MDB2_Driver_Function_mssql extends MDB2_Driver_Function_Common
  57. {
  58. // {{{ executeStoredProc()
  59. /**
  60. * Execute a stored procedure and return any results
  61. *
  62. * @param string $name string that identifies the function to execute
  63. * @param mixed $params array that contains the paramaters to pass the stored proc
  64. * @param mixed $types array that contains the types of the columns in
  65. * the result set
  66. * @param mixed $result_class string which specifies which result class to use
  67. * @param mixed $result_wrap_class string which specifies which class to wrap results in
  68. * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  69. * @access public
  70. */
  71. function executeStoredProc($name, $params = null, $types = null, $result_class = true, $result_wrap_class = false)
  72. {
  73. $db = $this->getDBInstance();
  74. if (MDB2::isError($db)) {
  75. return $db;
  76. }
  77. $query = 'EXECUTE '.$name;
  78. $query .= $params ? ' '.implode(', ', $params) : '';
  79. return $db->query($query, $types, $result_class, $result_wrap_class);
  80. }
  81. // }}}
  82. // {{{ now()
  83. /**
  84. * Return string to call a variable with the current timestamp inside an SQL statement
  85. * There are three special variables for current date and time:
  86. * - CURRENT_TIMESTAMP (date and time, TIMESTAMP type)
  87. * - CURRENT_DATE (date, DATE type)
  88. * - CURRENT_TIME (time, TIME type)
  89. *
  90. * @return string to call a variable with the current timestamp
  91. * @access public
  92. */
  93. function now($type = 'timestamp')
  94. {
  95. switch ($type) {
  96. case 'time':
  97. case 'date':
  98. case 'timestamp':
  99. default:
  100. return 'GETDATE()';
  101. }
  102. }
  103. // }}}
  104. // {{{ unixtimestamp()
  105. /**
  106. * return string to call a function to get the unix timestamp from a iso timestamp
  107. *
  108. * @param string $expression
  109. *
  110. * @return string to call a variable with the timestamp
  111. * @access public
  112. */
  113. function unixtimestamp($expression)
  114. {
  115. return 'DATEDIFF(second, \'19700101\', '. $expression.') + DATEDIFF(second, GETDATE(), GETUTCDATE())';
  116. }
  117. // }}}
  118. // {{{ substring()
  119. /**
  120. * return string to call a function to get a substring inside an SQL statement
  121. *
  122. * @return string to call a function to get a substring
  123. * @access public
  124. */
  125. function substring($value, $position = 1, $length = null)
  126. {
  127. if (null !== $length) {
  128. return "SUBSTRING($value, $position, $length)";
  129. }
  130. return "SUBSTRING($value, $position, LEN($value) - $position + 1)";
  131. }
  132. // }}}
  133. // {{{ concat()
  134. /**
  135. * Returns string to concatenate two or more string parameters
  136. *
  137. * @param string $value1
  138. * @param string $value2
  139. * @param string $values...
  140. * @return string to concatenate two strings
  141. * @access public
  142. **/
  143. function concat($value1, $value2)
  144. {
  145. $args = func_get_args();
  146. return "(".implode(' + ', $args).")";
  147. }
  148. // }}}
  149. // {{{ length()
  150. /**
  151. * return string to call a function to get the length of a string expression
  152. *
  153. * @param string $expression
  154. * @return return string to get the string expression length
  155. * @access public
  156. */
  157. function length($expression)
  158. {
  159. return "LEN($expression)";
  160. }
  161. // }}}
  162. // {{{ guid()
  163. /**
  164. * Returns global unique identifier
  165. *
  166. * @return string to get global unique identifier
  167. * @access public
  168. */
  169. function guid()
  170. {
  171. return 'NEWID()';
  172. }
  173. // }}}
  174. }
  175. // }}}
  176. ?>