/library/Zend/Db/Adapter/Driver/Sqlsrv/Sqlsrv.php

https://github.com/christeredvartsen/zf2 · PHP · 189 lines · 89 code · 21 blank · 79 comment · 9 complexity · c4092ddd5a034492b52624db3c3a6404 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Db
  9. */
  10. namespace Zend\Db\Adapter\Driver\Sqlsrv;
  11. use Zend\Db\Adapter\Driver\DriverInterface;
  12. use Zend\Db\Adapter\Exception;
  13. /**
  14. * @category Zend
  15. * @package Zend_Db
  16. * @subpackage Adapter
  17. */
  18. class Sqlsrv implements DriverInterface
  19. {
  20. /**
  21. * @var Connection
  22. */
  23. protected $connection = null;
  24. /**
  25. * @var Statement
  26. */
  27. protected $statementPrototype = null;
  28. /**
  29. * @var Result
  30. */
  31. protected $resultPrototype = null;
  32. /**
  33. * @param array|Connection|resource $connection
  34. * @param null|Statement $statementPrototype
  35. * @param null|Result $resultPrototype
  36. */
  37. public function __construct($connection, Statement $statementPrototype = null, Result $resultPrototype = null)
  38. {
  39. if (!$connection instanceof Connection) {
  40. $connection = new Connection($connection);
  41. }
  42. $this->registerConnection($connection);
  43. $this->registerStatementPrototype(($statementPrototype) ?: new Statement());
  44. $this->registerResultPrototype(($resultPrototype) ?: new Result());
  45. }
  46. /**
  47. * Register connection
  48. *
  49. * @param Connection $connection
  50. * @return Sqlsrv
  51. */
  52. public function registerConnection(Connection $connection)
  53. {
  54. $this->connection = $connection;
  55. $this->connection->setDriver($this);
  56. return $this;
  57. }
  58. /**
  59. * Register statement prototype
  60. *
  61. * @param Statement $statementPrototype
  62. * @return Sqlsrv
  63. */
  64. public function registerStatementPrototype(Statement $statementPrototype)
  65. {
  66. $this->statementPrototype = $statementPrototype;
  67. $this->statementPrototype->setDriver($this);
  68. return $this;
  69. }
  70. /**
  71. * Register result prototype
  72. *
  73. * @param Result $resultPrototype
  74. * @return Sqlsrv
  75. */
  76. public function registerResultPrototype(Result $resultPrototype)
  77. {
  78. $this->resultPrototype = $resultPrototype;
  79. return $this;
  80. }
  81. /**
  82. * Get database paltform name
  83. *
  84. * @param string $nameFormat
  85. * @return string
  86. */
  87. public function getDatabasePlatformName($nameFormat = self::NAME_FORMAT_CAMELCASE)
  88. {
  89. if ($nameFormat == self::NAME_FORMAT_CAMELCASE) {
  90. return 'SqlServer';
  91. }
  92. return 'SQLServer';
  93. }
  94. /**
  95. * Check environment
  96. *
  97. * @throws Exception\RuntimeException
  98. * @return void
  99. */
  100. public function checkEnvironment()
  101. {
  102. if (!extension_loaded('sqlsrv')) {
  103. throw new Exception\RuntimeException('The Sqlsrv extension is required for this adapter but the extension is not loaded');
  104. }
  105. }
  106. /**
  107. * @return Connection
  108. */
  109. public function getConnection()
  110. {
  111. return $this->connection;
  112. }
  113. /**
  114. * @param string|resource $sqlOrResource
  115. * @return Statement
  116. */
  117. public function createStatement($sqlOrResource = null)
  118. {
  119. $statement = clone $this->statementPrototype;
  120. if (is_resource($sqlOrResource)) {
  121. $statement->initialize($sqlOrResource);
  122. } else {
  123. if (!$this->connection->isConnected()) {
  124. $this->connection->connect();
  125. }
  126. $statement->initialize($this->connection->getResource());
  127. if (is_string($sqlOrResource)) {
  128. $statement->setSql($sqlOrResource);
  129. } elseif ($sqlOrResource != null) {
  130. throw new Exception\InvalidArgumentException('createStatement() only accepts an SQL string or a Sqlsrv resource');
  131. }
  132. }
  133. return $statement;
  134. }
  135. /**
  136. * @param resource $resource
  137. * @return Result
  138. */
  139. public function createResult($resource)
  140. {
  141. $result = clone $this->resultPrototype;
  142. $result->initialize($resource, $this->connection->getLastGeneratedValue());
  143. return $result;
  144. }
  145. /**
  146. * @return array
  147. */
  148. public function getPrepareType()
  149. {
  150. return self::PARAMETERIZATION_POSITIONAL;
  151. }
  152. /**
  153. * @param string $name
  154. * @param mixed $type
  155. * @return string
  156. */
  157. public function formatParameterName($name, $type = null)
  158. {
  159. return '?';
  160. }
  161. /**
  162. * @return mixed
  163. */
  164. public function getLastGeneratedValue()
  165. {
  166. return $this->getConnection()->getLastGeneratedValue();
  167. }
  168. }