PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/setup/includes/drivers/modinstalldriver_sqlsrv.class.php

http://github.com/modxcms/revolution
PHP | 191 lines | 183 code | 0 blank | 8 comment | 0 complexity | af566aa39245c0bee2c68048e2ccda3f MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. require_once (strtr(realpath(dirname(__FILE__)), '\\', '/') . '/modinstalldriver.class.php');
  11. /**
  12. * Provides query abstraction for setup using the sqlsrv native database driver.
  13. *
  14. * @package setup
  15. * @subpackage drivers
  16. */
  17. class modInstallDriver_sqlsrv extends modInstallDriver {
  18. /**
  19. * Check for sqlsrv extension
  20. * {@inheritDoc}
  21. */
  22. public function verifyExtension() {
  23. return extension_loaded('sqlsrv');
  24. }
  25. /**
  26. * Check for sqlsrv_pdo extension
  27. * {@inheritDoc}
  28. */
  29. public function verifyPDOExtension() {
  30. return extension_loaded('pdo_sqlsrv');
  31. }
  32. /**
  33. * SQL Server syntax for default collation query
  34. * {@inheritDoc}
  35. */
  36. public function getCollation() {
  37. $collation = 'Latin1_General_100_BIN2';
  38. // $stmt = $this->xpdo->query("SELECT CAST(SERVERPROPERTY('Collation') AS varchar(128))");
  39. // if ($stmt && $stmt instanceof PDOStatement) {
  40. // $rows = $stmt->fetchAll(PDO::FETCH_COLUMN);
  41. // $collation = reset($rows);
  42. // }
  43. return $collation;
  44. }
  45. /**
  46. * SQL Server syntax for collation listing
  47. * {@inheritDoc}
  48. */
  49. public function getCollations($collation = '') {
  50. $collations = null;
  51. $stmt = $this->xpdo->query("SELECT * FROM sys.fn_helpcollations()");
  52. if ($stmt && $stmt instanceof PDOStatement) {
  53. $collations = array();
  54. if (empty($collation)) $collation = $this->getCollation();
  55. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  56. $col = array();
  57. $col['selected'] = ($row['name']==$collation ? ' selected="selected"' : '');
  58. $col['value'] = $row['name'];
  59. $col['name'] = $row['name'];
  60. $collations[$row['name']] = $col;
  61. }
  62. ksort($collations);
  63. }
  64. return $collations;
  65. }
  66. public function getCharset($collation = '') {
  67. $charset = 'Latin1';
  68. if (empty($collation)) {
  69. $collation = $this->getCollation();
  70. }
  71. $pos = strpos($collation, '_');
  72. if ($pos > 0) {
  73. $charset = substr($collation, 0, $pos);
  74. }
  75. return $charset;
  76. }
  77. /**
  78. * SQL Server syntax for charset listing
  79. * {@inheritDoc}
  80. */
  81. public function getCharsets($charset = '') {
  82. if (empty($charset)) {
  83. $charset = $this->getCharset();
  84. }
  85. return array($charset => array(
  86. 'selected' => ' selected="selected"',
  87. 'value' => $charset,
  88. 'name' => $charset
  89. ));
  90. }
  91. /**
  92. * SQL Server syntax for table prefix check
  93. * {@inheritDoc}
  94. */
  95. public function testTablePrefix($database,$prefix) {
  96. return 'SELECT COUNT('.$this->xpdo->escape('id').') AS '.$this->xpdo->escape('ct').' FROM '.$this->xpdo->escape($prefix.'site_content');
  97. }
  98. /**
  99. * SQL Server syntax for table truncation
  100. * {@inheritDoc}
  101. */
  102. public function truncate($table) {
  103. return 'TRUNCATE '.$this->xpdo->escape($table);
  104. }
  105. /**
  106. * SQL Server check for server version
  107. *
  108. * @TODO Get this to actually check the server version.
  109. *
  110. * {@inheritDoc}
  111. */
  112. public function verifyServerVersion() {
  113. return array('result' => 'success','message' => $this->install->lexicon('sqlsrv_version_success',array('version' => '')));
  114. $handler = @sqlsrv_connect($this->install->settings->get('database_server'),$this->install->settings->get('database_user'),$this->install->settings->get('database_password'));
  115. $serverInfo = @sqlsrv_server_info($handler);
  116. $sqlsrvVersion = $serverInfo['SQLServerVersion'];
  117. $sqlsrvVersion = $this->_sanitizeVersion($sqlsrvVersion);
  118. if (empty($sqlsrvVersion)) {
  119. return array('result' => 'warning', 'message' => $this->install->lexicon('sqlsrv_version_server_nf'),'version' => $sqlsrvVersion);
  120. }
  121. $sqlsrv_ver_comp = version_compare($sqlsrvVersion,'10.50.0','>=');
  122. if (!$sqlsrv_ver_comp) { /* ancient driver warning */
  123. return array('result' => 'failure','message' => $this->install->lexicon('sqlsrv_version_fail',array('version' => $sqlsrvVersion)),'version' => $sqlsrvVersion);
  124. } else {
  125. return array('result' => 'success','message' => $this->install->lexicon('sqlsrv_version_success',array('version' => $sqlsrvVersion)),'version' => $sqlsrvVersion);
  126. }
  127. }
  128. /**
  129. * SQL Server check for client version
  130. *
  131. * @TODO Get this to actually check the client version.
  132. *
  133. * {@inheritDoc}
  134. */
  135. public function verifyClientVersion() {
  136. return array('result' => 'success', 'message' => $this->install->lexicon('sqlsrv_version_client_success',array('version' => '')));
  137. $clientInfo = @sqlsrv_client_info();
  138. $sqlsrvVersion = $clientInfo['DriverVer'];
  139. $sqlsrvVersion = $this->_sanitizeVersion($sqlsrvVersion);
  140. if (empty($sqlsrvVersion)) {
  141. return array('result' => 'warning','message' => $this->install->lexicon('sqlsrv_version_client_nf'),'version' => $sqlsrvVersion);
  142. }
  143. $sqlsrv_ver_comp = version_compare($sqlsrvVersion,'10.50.0','>=');
  144. if (!$sqlsrv_ver_comp) {
  145. return array('result' => 'warning','message' => $this->install->lexicon('sqlsrv_version_client_old',array('version' => $sqlsrvVersion)),'version' => $sqlsrvVersion);
  146. } else {
  147. return array('result' => 'success','message' => $this->install->lexicon('sqlsrv_version_success',array('version' => $sqlsrvVersion)),'version' => $sqlsrvVersion);
  148. }
  149. }
  150. /**
  151. * SQL Server syntax to add an index
  152. * {@inheritDoc}
  153. */
  154. public function addIndex($table,$name,$column) {
  155. return 'ALTER TABLE '.$this->xpdo->escape($table).' ADD INDEX '.$this->xpdo->escape($name).' ('.$this->xpdo->escape($column).')"';
  156. }
  157. /**
  158. * SQL Server syntax to drop an index
  159. * {@inheritDoc}
  160. */
  161. public function dropIndex($table,$index) {
  162. return 'ALTER TABLE '.$this->xpdo->escape($table).' DROP INDEX '.$this->xpdo->escape($index);
  163. }
  164. /**
  165. * Cleans a sqlsrv version string that often has extra junk in certain distros
  166. *
  167. * @param string $sqlsrvVersion The version note to sanitize
  168. * @return string The sanitized version
  169. */
  170. protected function _sanitizeVersion($sqlsrvVersion) {
  171. return $sqlsrvVersion;
  172. }
  173. }