Sqlsrv.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Db
  17. * @subpackage Adapter
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Db_Adapter_Abstract
  24. */
  25. #require_once 'Zend/Db/Adapter/Abstract.php';
  26. /**
  27. * @see Zend_Db_Statement_Sqlsrv
  28. */
  29. #require_once 'Zend/Db/Statement/Sqlsrv.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Db
  33. * @subpackage Adapter
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Db_Adapter_Sqlsrv extends Zend_Db_Adapter_Abstract
  38. {
  39. /**
  40. * User-provided configuration.
  41. *
  42. * Basic keys are:
  43. *
  44. * username => (string) Connect to the database as this username.
  45. * password => (string) Password associated with the username.
  46. * dbname => The name of the local SQL Server instance
  47. *
  48. * @var array
  49. */
  50. protected $_config = array(
  51. 'dbname' => null,
  52. 'username' => null,
  53. 'password' => null,
  54. );
  55. /**
  56. * Last insert id from INSERT query
  57. *
  58. * @var int
  59. */
  60. protected $_lastInsertId;
  61. /**
  62. * Query used to fetch last insert id
  63. *
  64. * @var string
  65. */
  66. protected $_lastInsertSQL = 'SELECT SCOPE_IDENTITY() as Current_Identity';
  67. /**
  68. * Keys are UPPERCASE SQL datatypes or the constants
  69. * Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE.
  70. *
  71. * Values are:
  72. * 0 = 32-bit integer
  73. * 1 = 64-bit integer
  74. * 2 = float or decimal
  75. *
  76. * @var array Associative array of datatypes to values 0, 1, or 2.
  77. */
  78. protected $_numericDataTypes = array(
  79. Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
  80. Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
  81. Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
  82. 'INT' => Zend_Db::INT_TYPE,
  83. 'SMALLINT' => Zend_Db::INT_TYPE,
  84. 'TINYINT' => Zend_Db::INT_TYPE,
  85. 'BIGINT' => Zend_Db::BIGINT_TYPE,
  86. 'DECIMAL' => Zend_Db::FLOAT_TYPE,
  87. 'FLOAT' => Zend_Db::FLOAT_TYPE,
  88. 'MONEY' => Zend_Db::FLOAT_TYPE,
  89. 'NUMERIC' => Zend_Db::FLOAT_TYPE,
  90. 'REAL' => Zend_Db::FLOAT_TYPE,
  91. 'SMALLMONEY' => Zend_Db::FLOAT_TYPE,
  92. );
  93. /**
  94. * Default class name for a DB statement.
  95. *
  96. * @var string
  97. */
  98. protected $_defaultStmtClass = 'Zend_Db_Statement_Sqlsrv';
  99. /**
  100. * Creates a connection resource.
  101. *
  102. * @return void
  103. * @throws Zend_Db_Adapter_Sqlsrv_Exception
  104. */
  105. protected function _connect()
  106. {
  107. if (is_resource($this->_connection)) {
  108. // connection already exists
  109. return;
  110. }
  111. if (!extension_loaded('sqlsrv')) {
  112. /**
  113. * @see Zend_Db_Adapter_Sqlsrv_Exception
  114. */
  115. #require_once 'Zend/Db/Adapter/Sqlsrv/Exception.php';
  116. throw new Zend_Db_Adapter_Sqlsrv_Exception('The Sqlsrv extension is required for this adapter but the extension is not loaded');
  117. }
  118. $serverName = $this->_config['host'];
  119. if (isset($this->_config['port'])) {
  120. $port = (integer) $this->_config['port'];
  121. $serverName .= ', ' . $port;
  122. }
  123. $connectionInfo = array(
  124. 'Database' => $this->_config['dbname'],
  125. );
  126. if (isset($this->_config['username']) && isset($this->_config['password']))
  127. {
  128. $connectionInfo += array(
  129. 'UID' => $this->_config['username'],
  130. 'PWD' => $this->_config['password'],
  131. );
  132. }
  133. // else - windows authentication
  134. if (!empty($this->_config['driver_options'])) {
  135. foreach ($this->_config['driver_options'] as $option => $value) {
  136. // A value may be a constant.
  137. if (is_string($value)) {
  138. $constantName = strtoupper($value);
  139. if (defined($constantName)) {
  140. $connectionInfo[$option] = constant($constantName);
  141. } else {
  142. $connectionInfo[$option] = $value;
  143. }
  144. }
  145. }
  146. }
  147. $this->_connection = sqlsrv_connect($serverName, $connectionInfo);
  148. if (!$this->_connection) {
  149. /**
  150. * @see Zend_Db_Adapter_Sqlsrv_Exception
  151. */
  152. #require_once 'Zend/Db/Adapter/Sqlsrv/Exception.php';
  153. throw new Zend_Db_Adapter_Sqlsrv_Exception(sqlsrv_errors());
  154. }
  155. }
  156. /**
  157. * Check for config options that are mandatory.
  158. * Throw exceptions if any are missing.
  159. *
  160. * @param array $config
  161. * @throws Zend_Db_Adapter_Exception
  162. */
  163. protected function _checkRequiredOptions(array $config)
  164. {
  165. // we need at least a dbname
  166. if (! array_key_exists('dbname', $config)) {
  167. /** @see Zend_Db_Adapter_Exception */
  168. #require_once 'Zend/Db/Adapter/Exception.php';
  169. throw new Zend_Db_Adapter_Exception("Configuration array must have a key for 'dbname' that names the database instance");
  170. }
  171. if (! array_key_exists('password', $config) && array_key_exists('username', $config)) {
  172. /**
  173. * @see Zend_Db_Adapter_Exception
  174. */
  175. #require_once 'Zend/Db/Adapter/Exception.php';
  176. throw new Zend_Db_Adapter_Exception("Configuration array must have a key for 'password' for login credentials.
  177. If Windows Authentication is desired, both keys 'username' and 'password' should be ommited from config.");
  178. }
  179. if (array_key_exists('password', $config) && !array_key_exists('username', $config)) {
  180. /**
  181. * @see Zend_Db_Adapter_Exception
  182. */
  183. #require_once 'Zend/Db/Adapter/Exception.php';
  184. throw new Zend_Db_Adapter_Exception("Configuration array must have a key for 'username' for login credentials.
  185. If Windows Authentication is desired, both keys 'username' and 'password' should be ommited from config.");
  186. }
  187. }
  188. /**
  189. * Set the transaction isoltion level.
  190. *
  191. * @param integer|null $level A fetch mode from SQLSRV_TXN_*.
  192. * @return true
  193. * @throws Zend_Db_Adapter_Sqlsrv_Exception
  194. */
  195. public function setTransactionIsolationLevel($level = null)
  196. {
  197. $this->_connect();
  198. $sql = null;
  199. // Default transaction level in sql server
  200. if ($level === null)
  201. {
  202. $level = SQLSRV_TXN_READ_COMMITTED;
  203. }
  204. switch ($level) {
  205. case SQLSRV_TXN_READ_UNCOMMITTED:
  206. $sql = "READ UNCOMMITTED";
  207. break;
  208. case SQLSRV_TXN_READ_COMMITTED:
  209. $sql = "READ COMMITTED";
  210. break;
  211. case SQLSRV_TXN_REPEATABLE_READ:
  212. $sql = "REPEATABLE READ";
  213. break;
  214. case SQLSRV_TXN_SNAPSHOT:
  215. $sql = "SNAPSHOT";
  216. break;
  217. case SQLSRV_TXN_SERIALIZABLE:
  218. $sql = "SERIALIZABLE";
  219. break;
  220. default:
  221. #require_once 'Zend/Db/Adapter/Sqlsrv/Exception.php';
  222. throw new Zend_Db_Adapter_Sqlsrv_Exception("Invalid transaction isolation level mode '$level' specified");
  223. }
  224. if (!sqlsrv_query($this->_connection, "SET TRANSACTION ISOLATION LEVEL $sql;")) {
  225. #require_once 'Zend/Db/Adapter/Sqlsrv/Exception.php';
  226. throw new Zend_Db_Adapter_Sqlsrv_Exception("Transaction cannot be changed to '$level'");
  227. }
  228. return true;
  229. }
  230. /**
  231. * Test if a connection is active
  232. *
  233. * @return boolean
  234. */
  235. public function isConnected()
  236. {
  237. return (is_resource($this->_connection)
  238. && (get_resource_type($this->_connection) == 'SQL Server Connection')
  239. );
  240. }
  241. /**
  242. * Force the connection to close.
  243. *
  244. * @return void
  245. */
  246. public function closeConnection()
  247. {
  248. if ($this->isConnected()) {
  249. sqlsrv_close($this->_connection);
  250. }
  251. $this->_connection = null;
  252. }
  253. /**
  254. * Returns an SQL statement for preparation.
  255. *
  256. * @param string $sql The SQL statement with placeholders.
  257. * @return Zend_Db_Statement_Sqlsrv
  258. */
  259. public function prepare($sql)
  260. {
  261. $this->_connect();
  262. $stmtClass = $this->_defaultStmtClass;
  263. if (!class_exists($stmtClass)) {
  264. /**
  265. * @see Zend_Loader
  266. */
  267. #require_once 'Zend/Loader.php';
  268. Zend_Loader::loadClass($stmtClass);
  269. }
  270. $stmt = new $stmtClass($this, $sql);
  271. $stmt->setFetchMode($this->_fetchMode);
  272. return $stmt;
  273. }
  274. /**
  275. * Quote a raw string.
  276. *
  277. * @param string $value Raw string
  278. * @return string Quoted string
  279. */
  280. protected function _quote($value)
  281. {
  282. if (is_int($value)) {
  283. return $value;
  284. } elseif (is_float($value)) {
  285. return sprintf('%F', $value);
  286. }
  287. $value = addcslashes($value, "\000\032");
  288. return "'" . str_replace("'", "''", $value) . "'";
  289. }
  290. /**
  291. * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
  292. *
  293. * As a convention, on RDBMS brands that support sequences
  294. * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
  295. * from the arguments and returns the last id generated by that sequence.
  296. * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
  297. * returns the last value generated for such a column, and the table name
  298. * argument is disregarded.
  299. *
  300. * @param string $tableName OPTIONAL Name of table.
  301. * @param string $primaryKey OPTIONAL Name of primary key column.
  302. * @return string
  303. */
  304. public function lastInsertId($tableName = null, $primaryKey = null)
  305. {
  306. if ($tableName) {
  307. $tableName = $this->quote($tableName);
  308. $sql = 'SELECT IDENT_CURRENT (' . $tableName . ') as Current_Identity';
  309. return (string) $this->fetchOne($sql);
  310. }
  311. if ($this->_lastInsertId > 0) {
  312. return (string) $this->_lastInsertId;
  313. }
  314. $sql = $this->_lastInsertSQL;
  315. return (string) $this->fetchOne($sql);
  316. }
  317. /**
  318. * Inserts a table row with specified data.
  319. *
  320. * @param mixed $table The table to insert data into.
  321. * @param array $bind Column-value pairs.
  322. * @return int The number of affected rows.
  323. */
  324. public function insert($table, array $bind)
  325. {
  326. // extract and quote col names from the array keys
  327. $cols = array();
  328. $vals = array();
  329. foreach ($bind as $col => $val) {
  330. $cols[] = $this->quoteIdentifier($col, true);
  331. if ($val instanceof Zend_Db_Expr) {
  332. $vals[] = $val->__toString();
  333. unset($bind[$col]);
  334. } else {
  335. $vals[] = '?';
  336. }
  337. }
  338. // build the statement
  339. $sql = "INSERT INTO "
  340. . $this->quoteIdentifier($table, true)
  341. . ' (' . implode(', ', $cols) . ') '
  342. . 'VALUES (' . implode(', ', $vals) . ')'
  343. . ' ' . $this->_lastInsertSQL;
  344. // execute the statement and return the number of affected rows
  345. $stmt = $this->query($sql, array_values($bind));
  346. $result = $stmt->rowCount();
  347. $stmt->nextRowset();
  348. $this->_lastInsertId = $stmt->fetchColumn();
  349. return $result;
  350. }
  351. /**
  352. * Returns a list of the tables in the database.
  353. *
  354. * @return array
  355. */
  356. public function listTables()
  357. {
  358. $this->_connect();
  359. $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
  360. return $this->fetchCol($sql);
  361. }
  362. /**
  363. * Returns the column descriptions for a table.
  364. *
  365. * The return value is an associative array keyed by the column name,
  366. * as returned by the RDBMS.
  367. *
  368. * The value of each array element is an associative array
  369. * with the following keys:
  370. *
  371. * SCHEMA_NAME => string; name of schema
  372. * TABLE_NAME => string;
  373. * COLUMN_NAME => string; column name
  374. * COLUMN_POSITION => number; ordinal position of column in table
  375. * DATA_TYPE => string; SQL datatype name of column
  376. * DEFAULT => string; default expression of column, null if none
  377. * NULLABLE => boolean; true if column can have nulls
  378. * LENGTH => number; length of CHAR/VARCHAR
  379. * SCALE => number; scale of NUMERIC/DECIMAL
  380. * PRECISION => number; precision of NUMERIC/DECIMAL
  381. * UNSIGNED => boolean; unsigned property of an integer type
  382. * PRIMARY => boolean; true if column is part of the primary key
  383. * PRIMARY_POSITION => integer; position of column in primary key
  384. * IDENTITY => integer; true if column is auto-generated with unique values
  385. *
  386. * @todo Discover integer unsigned property.
  387. *
  388. * @param string $tableName
  389. * @param string $schemaName OPTIONAL
  390. * @return array
  391. */
  392. public function describeTable($tableName, $schemaName = null)
  393. {
  394. /**
  395. * Discover metadata information about this table.
  396. */
  397. $sql = "exec sp_columns @table_name = " . $this->quoteIdentifier($tableName, true);
  398. $stmt = $this->query($sql);
  399. $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
  400. // ZF-7698
  401. $stmt->closeCursor();
  402. if (count($result) == 0) {
  403. return array();
  404. }
  405. $owner = 1;
  406. $table_name = 2;
  407. $column_name = 3;
  408. $type_name = 5;
  409. $precision = 6;
  410. $length = 7;
  411. $scale = 8;
  412. $nullable = 10;
  413. $column_def = 12;
  414. $column_position = 16;
  415. /**
  416. * Discover primary key column(s) for this table.
  417. */
  418. $tableOwner = $result[0][$owner];
  419. $sql = "exec sp_pkeys @table_owner = " . $tableOwner
  420. . ", @table_name = " . $this->quoteIdentifier($tableName, true);
  421. $stmt = $this->query($sql);
  422. $primaryKeysResult = $stmt->fetchAll(Zend_Db::FETCH_NUM);
  423. $primaryKeyColumn = array();
  424. // Per http://msdn.microsoft.com/en-us/library/ms189813.aspx,
  425. // results from sp_keys stored procedure are:
  426. // 0=TABLE_QUALIFIER 1=TABLE_OWNER 2=TABLE_NAME 3=COLUMN_NAME 4=KEY_SEQ 5=PK_NAME
  427. $pkey_column_name = 3;
  428. $pkey_key_seq = 4;
  429. foreach ($primaryKeysResult as $pkeysRow) {
  430. $primaryKeyColumn[$pkeysRow[$pkey_column_name]] = $pkeysRow[$pkey_key_seq];
  431. }
  432. $desc = array();
  433. $p = 1;
  434. foreach ($result as $key => $row) {
  435. $identity = false;
  436. $words = explode(' ', $row[$type_name], 2);
  437. if (isset($words[0])) {
  438. $type = $words[0];
  439. if (isset($words[1])) {
  440. $identity = (bool) preg_match('/identity/', $words[1]);
  441. }
  442. }
  443. $isPrimary = array_key_exists($row[$column_name], $primaryKeyColumn);
  444. if ($isPrimary) {
  445. $primaryPosition = $primaryKeyColumn[$row[$column_name]];
  446. } else {
  447. $primaryPosition = null;
  448. }
  449. $desc[$this->foldCase($row[$column_name])] = array(
  450. 'SCHEMA_NAME' => null, // @todo
  451. 'TABLE_NAME' => $this->foldCase($row[$table_name]),
  452. 'COLUMN_NAME' => $this->foldCase($row[$column_name]),
  453. 'COLUMN_POSITION' => (int) $row[$column_position],
  454. 'DATA_TYPE' => $type,
  455. 'DEFAULT' => $row[$column_def],
  456. 'NULLABLE' => (bool) $row[$nullable],
  457. 'LENGTH' => $row[$length],
  458. 'SCALE' => $row[$scale],
  459. 'PRECISION' => $row[$precision],
  460. 'UNSIGNED' => null, // @todo
  461. 'PRIMARY' => $isPrimary,
  462. 'PRIMARY_POSITION' => $primaryPosition,
  463. 'IDENTITY' => $identity,
  464. );
  465. }
  466. return $desc;
  467. }
  468. /**
  469. * Leave autocommit mode and begin a transaction.
  470. *
  471. * @return void
  472. * @throws Zend_Db_Adapter_Sqlsrv_Exception
  473. */
  474. protected function _beginTransaction()
  475. {
  476. if (!sqlsrv_begin_transaction($this->_connection)) {
  477. #require_once 'Zend/Db/Adapter/Sqlsrv/Exception.php';
  478. throw new Zend_Db_Adapter_Sqlsrv_Exception(sqlsrv_errors());
  479. }
  480. }
  481. /**
  482. * Commit a transaction and return to autocommit mode.
  483. *
  484. * @return void
  485. * @throws Zend_Db_Adapter_Sqlsrv_Exception
  486. */
  487. protected function _commit()
  488. {
  489. if (!sqlsrv_commit($this->_connection)) {
  490. #require_once 'Zend/Db/Adapter/Sqlsrv/Exception.php';
  491. throw new Zend_Db_Adapter_Sqlsrv_Exception(sqlsrv_errors());
  492. }
  493. }
  494. /**
  495. * Roll back a transaction and return to autocommit mode.
  496. *
  497. * @return void
  498. * @throws Zend_Db_Adapter_Sqlsrv_Exception
  499. */
  500. protected function _rollBack()
  501. {
  502. if (!sqlsrv_rollback($this->_connection)) {
  503. #require_once 'Zend/Db/Adapter/Sqlsrv/Exception.php';
  504. throw new Zend_Db_Adapter_Sqlsrv_Exception(sqlsrv_errors());
  505. }
  506. }
  507. /**
  508. * Set the fetch mode.
  509. *
  510. * @todo Support FETCH_CLASS and FETCH_INTO.
  511. *
  512. * @param integer $mode A fetch mode.
  513. * @return void
  514. * @throws Zend_Db_Adapter_Sqlsrv_Exception
  515. */
  516. public function setFetchMode($mode)
  517. {
  518. switch ($mode) {
  519. case Zend_Db::FETCH_NUM: // seq array
  520. case Zend_Db::FETCH_ASSOC: // assoc array
  521. case Zend_Db::FETCH_BOTH: // seq+assoc array
  522. case Zend_Db::FETCH_OBJ: // object
  523. $this->_fetchMode = $mode;
  524. break;
  525. case Zend_Db::FETCH_BOUND: // bound to PHP variable
  526. #require_once 'Zend/Db/Adapter/Sqlsrv/Exception.php';
  527. throw new Zend_Db_Adapter_Sqlsrv_Exception('FETCH_BOUND is not supported yet');
  528. break;
  529. default:
  530. #require_once 'Zend/Db/Adapter/Sqlsrv/Exception.php';
  531. throw new Zend_Db_Adapter_Sqlsrv_Exception("Invalid fetch mode '$mode' specified");
  532. break;
  533. }
  534. }
  535. /**
  536. * Adds an adapter-specific LIMIT clause to the SELECT statement.
  537. *
  538. * @param string $sql
  539. * @param integer $count
  540. * @param integer $offset OPTIONAL
  541. * @return string
  542. * @throws Zend_Db_Adapter_Sqlsrv_Exception
  543. */
  544. public function limit($sql, $count, $offset = 0)
  545. {
  546. $count = intval($count);
  547. if ($count <= 0) {
  548. #require_once 'Zend/Db/Adapter/Exception.php';
  549. throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid");
  550. }
  551. $offset = intval($offset);
  552. if ($offset < 0) {
  553. /** @see Zend_Db_Adapter_Exception */
  554. #require_once 'Zend/Db/Adapter/Exception.php';
  555. throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid");
  556. }
  557. if ($offset == 0) {
  558. $sql = preg_replace('/^SELECT\s/i', 'SELECT TOP ' . $count . ' ', $sql);
  559. } else {
  560. $orderby = stristr($sql, 'ORDER BY');
  561. if (!$orderby) {
  562. $over = 'ORDER BY (SELECT 0)';
  563. } else {
  564. $over = preg_replace('/\"[^,]*\".\"([^,]*)\"/i', '"inner_tbl"."$1"', $orderby);
  565. }
  566. // Remove ORDER BY clause from $sql
  567. $sql = preg_replace('/\s+ORDER BY(.*)/', '', $sql);
  568. // Add ORDER BY clause as an argument for ROW_NUMBER()
  569. $sql = "SELECT ROW_NUMBER() OVER ($over) AS \"ZEND_DB_ROWNUM\", * FROM ($sql) AS inner_tbl";
  570. $start = $offset + 1;
  571. if ($count == PHP_INT_MAX) {
  572. $sql = "WITH outer_tbl AS ($sql) SELECT * FROM outer_tbl WHERE \"ZEND_DB_ROWNUM\" >= $start";
  573. }
  574. else {
  575. $end = $offset + $count;
  576. $sql = "WITH outer_tbl AS ($sql) SELECT * FROM outer_tbl WHERE \"ZEND_DB_ROWNUM\" BETWEEN $start AND $end";
  577. }
  578. }
  579. return $sql;
  580. }
  581. /**
  582. * Check if the adapter supports real SQL parameters.
  583. *
  584. * @param string $type 'positional' or 'named'
  585. * @return bool
  586. */
  587. public function supportsParameters($type)
  588. {
  589. if ($type == 'positional') {
  590. return true;
  591. }
  592. // if its 'named' or anything else
  593. return false;
  594. }
  595. /**
  596. * Retrieve server version in PHP style
  597. *
  598. * @return string
  599. */
  600. public function getServerVersion()
  601. {
  602. $this->_connect();
  603. $serverInfo = sqlsrv_server_info($this->_connection);
  604. if ($serverInfo !== false) {
  605. return $serverInfo['SQLServerVersion'];
  606. }
  607. return null;
  608. }
  609. }