/database/class.DbMssql.php

https://github.com/palmic/2yaml · PHP · 237 lines · 178 code · 27 blank · 32 comment · 32 complexity · f98d2e67ce78d7d6f239f6863e5412a3 MD5 · raw file

  1. <?php
  2. /**
  3. * Database platform implementation for MSSQL.
  4. * @author Michal Palma <palmic at email dot cz>
  5. * @package DbControl
  6. * @version 1.5
  7. * @date 2006-01-11
  8. */
  9. class DbMssql extends DbPlatform
  10. {
  11. //== Attributes ======================================================================
  12. //== constructors ====================================================================
  13. public function DbMssql(DbParametersMessenger $dbParametersMessenger, DbStateHandler $dbStateHandler) {
  14. if (!in_array("mssql", get_loaded_extensions())) {
  15. throw new DbControlException("Your PHP configuration does not support MSSQL extension. Check php.ini.");
  16. }
  17. $this->dbParametersMessenger = $dbParametersMessenger;
  18. $this->dbStateHandler = $dbStateHandler;
  19. }
  20. //== destructors =====================================================================
  21. public function __destruct() {
  22. if ($this->activeTransaction) {
  23. if ($this->autoCommit) {
  24. $this->transactionCommit();
  25. }
  26. else {
  27. $this->transactionRollback();
  28. }
  29. }
  30. @mssql_close($this->connection);
  31. }
  32. //== public functions ================================================================
  33. public function fetchAssoc($result) {
  34. if (!is_resource($result)) {
  35. throw new DbControlException("Ilegal parameter result. Must be valid result resource.");
  36. }
  37. else {
  38. return @mssql_fetch_assoc($result);
  39. }
  40. }
  41. public function getLastId() {
  42. try {
  43. $result = $this->query("SELECT LAST_INSERT_ID()");
  44. $id = mssql_result($result, 0, 0);
  45. }
  46. catch (Exception $e) {
  47. throw new DbControlException("Error in trying to acquire Last inserted id.\n" . $e->getMessage(), $e->getCode());
  48. }
  49. return $id;
  50. }
  51. public function selectDb(/*string*/ $dbName) {
  52. if (!is_string($dbName)) {
  53. throw new DbControlException("Ilegal parameter dbName. Must be string.");
  54. }
  55. try {
  56. $this->query("USE ". $dbName);
  57. }
  58. catch (Exception $e) {
  59. throw $e;
  60. }
  61. }
  62. public function query(/*string*/ $query) {
  63. if (!is_string($query)) {
  64. throw new DbControlException("Ilegal parameter query. Must be string.");
  65. }
  66. try {
  67. $result = @mssql_query($query, $this->getConnection());
  68. // v pripade vkladani musime zajistit povoleni vlozeni zaznamu s definovanym Primary key
  69. if (!$result) {
  70. if (eregi("^INSERT INTO (\[[[:alnum:]_]+\]) ", $query, $regs)) {
  71. @mssql_query("SET IDENTITY_INSERT ". $regs[1] ." ON", $this->getConnection());
  72. $result = @mssql_query($query, $this->getConnection());
  73. }
  74. }
  75. if (!$result) {
  76. $this->throwMssqlException("SQL query caused Error. Query: ". $query);
  77. }
  78. }
  79. catch (Exception $e) {
  80. throw $e;
  81. }
  82. return $result;
  83. }
  84. public function getNumRows(/*resource*/ $result) {
  85. if (!is_resource($result)) {
  86. throw new DbControlException("Ilegal parameter result. Must be valid result resource.");
  87. }
  88. else {
  89. return @mssql_num_rows($result);
  90. }
  91. }
  92. public function getColnames(/*resource*/ $result) {
  93. if (!is_resource($result)) {
  94. throw new DbControlException("Ilegal parameter result. Must be valid result resource.");
  95. }
  96. if (!$numFields = @mssql_num_fields($result)) {
  97. throw new DbControlException("No Column in result.");
  98. }
  99. for ($i = 0; $i < $numFields; $i++) {
  100. if (!$colname = @mssql_field_name($result, $i)) {
  101. $this->throwMssqlException("Colnames reading error.");
  102. }
  103. $colnames[$i] = $colname;
  104. }
  105. return $colnames;
  106. }
  107. public function transactionStart(/*boolean*/ $autoCommit) {
  108. if (!is_bool($autoCommit)) {
  109. throw new DbControlException("Ilegal parameter autoCommit. Must be boolean.");
  110. }
  111. if (!$this->activeTransaction) {
  112. try {
  113. $this->query("BEGIN TRANSACTION");
  114. }
  115. catch (Exception $e) {
  116. throw $e;
  117. }
  118. $this->autoCommit = $autoCommit;
  119. $this->activeTransaction = true;
  120. }
  121. else {
  122. throw new DbControlException("Multiple transactions are not supported.");
  123. }
  124. }
  125. public function transactionCommit() {
  126. if ($this->activeTransaction) {
  127. try {
  128. $this->query("COMMIT");
  129. }
  130. catch (Exception $e) {
  131. throw $e;
  132. }
  133. $this->activeTransaction = false;
  134. }
  135. else {
  136. throw new DbControlException("No transaction active.");
  137. }
  138. }
  139. public function transactionRollback() {
  140. if ($this->activeTransaction) {
  141. try {
  142. $this->query("ROLLBACK");
  143. }
  144. catch (Exception $e) {
  145. throw $e;
  146. }
  147. $this->activeTransaction = false;
  148. }
  149. else {
  150. throw new DbControlException("No transaction active.");
  151. }
  152. }
  153. //== protected functions =============================================================
  154. protected function connect() {
  155. if(!is_resource($this->connection)) {
  156. $hostString = $this->dbParametersMessenger->loginHost;
  157. // we are not using port value in MSSQL connection - there was problems with that.
  158. // if (strlen($this->dbParametersMessenger->port) > 0) {
  159. // $hostString = $this->dbParametersMessenger->loginHost .":". $this->dbParametersMessenger->port;
  160. // }
  161. // else {
  162. // $hostString = $this->dbParametersMessenger->loginHost;
  163. // }
  164. for ($i = 0; $i < 50; $i++) {
  165. $this->connection = @mssql_pconnect($hostString, $this->dbParametersMessenger->loginName, $this->dbParametersMessenger->loginPassword);
  166. if (is_resource($this->connection)) break;
  167. sleep(0.2);
  168. }
  169. if (!is_resource($this->connection)) {
  170. throw new DbControlException("Cant connect to database Mssql.\nhost = ". $this->dbParametersMessenger->loginHost, -1);
  171. }
  172. //setting default task database schema - if defined
  173. # Caution: Using $this->query() may rewrite query that is waiting for execute
  174. if (strlen($schema = $this->dbParametersMessenger->schema) > 0) {
  175. if (!@mssql_query("USE $schema;", $this->connection)) {
  176. $this->throwMssqlException("Cannot select default database schema '$schema'.");
  177. }
  178. }
  179. }
  180. }
  181. /**
  182. * getter for connection
  183. * @return valid connection resource
  184. */
  185. protected function getConnection() {
  186. try {
  187. $this->connect();
  188. }
  189. catch (Exception $e) {
  190. throw $e;
  191. }
  192. return $this->connection;
  193. }
  194. /**
  195. * Throws Exception with Mssql error infos
  196. * @return void
  197. */
  198. protected function throwMssqlException(/*string*/ $addToMessage = "") {
  199. if (is_string($addToMessage)) {
  200. $message = $addToMessage ."\n". @mssql_get_last_message();
  201. }
  202. else {
  203. $message = @mssql_get_last_message();
  204. }
  205. throw new DbControlException($message);
  206. }
  207. }
  208. ?>