PageRenderTime 32ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/wee/db/mssql/weeMSSQLDatabase.class.php

https://github.com/extend/wee
PHP | 203 lines | 114 code | 21 blank | 68 comment | 4 complexity | 6488aa54aed39d3fb6815b4f6cd546c1 MD5 | raw file
  1. <?php
  2. /*
  3. Web:Extend
  4. Copyright (c) 2006-2010 Dev:Extend
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. if (!defined('ALLOW_INCLUSION')) die;
  18. /**
  19. MS SQL database driver.
  20. */
  21. class weeMSSQLDatabase extends weeDatabase
  22. {
  23. /**
  24. Link resource for this database connection.
  25. */
  26. protected $rLink;
  27. /**
  28. The name of the underlying DBMS (mssql).
  29. */
  30. protected $sDBMS = 'mssql';
  31. /**
  32. Number of affected rows for the previous query.
  33. Stocked here to prevent errors if getPKId is called.
  34. */
  35. protected $iNumAffectedRows;
  36. /**
  37. Initialises a new mssql database.
  38. This database driver accepts the following parameters:
  39. * host: The server of the database as specified by mssql_connect.
  40. * user: The username.
  41. * password: The password.
  42. * dbname: The name of the database.
  43. @param $aParams The parameters of the database.
  44. @throw ConfigurationException The MSSQL PHP extension is missing.
  45. @throw DatabaseException The connection failed.
  46. */
  47. public function __construct($aParams = array())
  48. {
  49. function_exists('mssql_connect') or burn('ConfigurationException',
  50. sprintf(_WT('The "%s" PHP extension is required by this database driver.'), 'MSSQL'));
  51. // mssql_connect triggers a warning if the connection failed.
  52. // Don't use mssql_get_last_message here as it does not always return
  53. // something useful on connection failure.
  54. $this->rLink = mssql_connect(array_value($aParams, 'host'), array_value($aParams, 'user'), array_value($aParams, 'password'), true);
  55. $this->rLink !== false or burn('DatabaseException',
  56. sprintf(_WT("Failed to connect to the database with the following error:\n%s"), array_value(error_get_last(), 'message')));
  57. if (isset($aParams['dbname']))
  58. $this->selectDb($aParams['dbname']);
  59. }
  60. /**
  61. Does the mssql-dependent logic of the escape operation.
  62. @param $mValue The value to escape.
  63. @return string The escaped value.
  64. */
  65. public function doEscape($mValue)
  66. {
  67. // Bool isn't supported directly, cast to int
  68. if (is_bool($mValue))
  69. $mValue = (int)$mValue;
  70. return "'" . str_replace("'", "''", $mValue) . "'";
  71. }
  72. /**
  73. Execute an SQL query.
  74. @param $sQueryString The query string
  75. @return weePgSQLResult Only with SELECT queries: an object for results handling
  76. */
  77. protected function doQuery($sQueryString)
  78. {
  79. // mssql_query triggers a warning when the query could not be executed.
  80. $m = @mssql_query($sQueryString, $this->rLink);
  81. $m === false and burn('DatabaseException',
  82. sprintf(_WT("Failed to execute the query with the following error:\n%s"), mssql_get_last_message()));
  83. // Get it now since it can be wrong if numAffectedRows is called after getPKId
  84. $this->iNumAffectedRows = mssql_rows_affected($this->rLink);
  85. if (is_resource($m))
  86. return new weeMSSQLResult($m);
  87. }
  88. /**
  89. Escape the given identifier for safe concatenation in an SQL query.
  90. @param $sValue The identifier to escape.
  91. @return string The escaped identifier.
  92. @throw InvalidArgumentException The given value is not a valid mssql identifier.
  93. */
  94. public function escapeIdent($sValue)
  95. {
  96. if ($sValue instanceof Printable)
  97. $sValue = $sValue->toString();
  98. $i = strlen($sValue);
  99. $i != 0 && $i < 129 or burn('InvalidArgumentException',
  100. _WT('The given value is not a valid identifier.'));
  101. return '[' . str_replace(']', ']]', $sValue) . ']';
  102. }
  103. /**
  104. Returns the name of the pgsql dbmeta class.
  105. @param mixed The name of the mssql dbmeta class.
  106. */
  107. public function getMetaClass()
  108. {
  109. return 'weeMSSQLDbMeta';
  110. }
  111. /**
  112. Returns the last sequence value generated by the database in this session.
  113. @param $sName Unused in this database driver.
  114. @return int The last value generated.
  115. @throw IllegalStateException No value has been generated yet for the given sequence in this session.
  116. */
  117. public function getPKId($sName = null)
  118. {
  119. $r = mssql_query('SELECT SCOPE_IDENTITY()', $this->rLink, 1);
  120. $m = mssql_result($r, 0, 0);
  121. $m !== null or burn('IllegalStateException',
  122. _WT('No sequence value has been generated yet by the database in this session.'));
  123. return $m;
  124. }
  125. /**
  126. Returns the number of affected rows in the last INSERT, UPDATE or DELETE query.
  127. You can't use this method safely to check if your UPDATE executed successfully,
  128. since the UPDATE statement does not always update rows that are already up-to-date.
  129. @return int The number of affected rows in the last query.
  130. */
  131. public function numAffectedRows()
  132. {
  133. return $this->iNumAffectedRows;
  134. }
  135. /**
  136. Prepare an SQL query statement.
  137. @param $sQuery The query to prepare.
  138. @return weeMSSQLStatement The prepared statement.
  139. */
  140. public function prepare($sQuery)
  141. {
  142. return new weeMSSQLStatement($this, $this->rLink, $sQuery);
  143. }
  144. /**
  145. Changes database without reconnecting.
  146. The new database must be on the same host of the previous.
  147. @param $sDatabase The database to use.
  148. @throw DatabaseException Failed to select the database.
  149. */
  150. public function selectDb($sDatabase)
  151. {
  152. // mssql_select_db triggers a warning when the selection failed.
  153. @mssql_select_db($sDatabase, $this->rLink) or burn('DatabaseException',
  154. sprintf(_WT('Failed to select the database "%s" with the following message:'), $sDatabase)
  155. . "\n" . mssql_get_last_message());
  156. }
  157. }