[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Schema/ChangeItem/ -> SqlsrvChangeItem.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2012 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Schema\ChangeItem;
  11  
  12  use Joomla\CMS\Schema\ChangeItem;
  13  
  14  // phpcs:disable PSR1.Files.SideEffects
  15  \defined('JPATH_PLATFORM') or die;
  16  // phpcs:enable PSR1.Files.SideEffects
  17  
  18  /**
  19   * Checks the database schema against one SQL Server DDL query to see if it has been run.
  20   *
  21   * @since  2.5
  22   */
  23  class SqlsrvChangeItem extends ChangeItem
  24  {
  25      /**
  26       * Checks a DDL query to see if it is a known type
  27       * If yes, build a check query to see if the DDL has been run on the database.
  28       * If successful, the $msgElements, $queryType, $checkStatus and $checkQuery fields are populated.
  29       * The $msgElements contains the text to create the user message.
  30       * The $checkQuery contains the SQL query to check whether the schema change has
  31       * been run against the current database. The $queryType contains the type of
  32       * DDL query that was run (for example, CREATE_TABLE, ADD_COLUMN, CHANGE_COLUMN_TYPE, ADD_INDEX).
  33       * The $checkStatus field is set to zero if the query is created
  34       *
  35       * If not successful, $checkQuery is empty and , and $checkStatus is -1.
  36       * For example, this will happen if the current line is a non-DDL statement.
  37       *
  38       * @return void
  39       *
  40       * @since  2.5
  41       */
  42      protected function buildCheckQuery()
  43      {
  44          // Initialize fields in case we can't create a check query
  45  
  46          // Change status to skipped
  47          $this->checkStatus = -1;
  48          $result = null;
  49  
  50          // Remove any newlines
  51          $this->updateQuery = str_replace("\n", '', $this->updateQuery);
  52  
  53          // Fix up extra spaces around () and in general
  54          $find = array('#((\s*)\(\s*([^)\s]+)\s*)(\))#', '#(\s)(\s*)#');
  55          $replace = array('($3)', '$1');
  56          $updateQuery = preg_replace($find, $replace, $this->updateQuery);
  57          $wordArray = explode(' ', $updateQuery);
  58  
  59          // First, make sure we have an array of at least 6 elements
  60          // if not, we can't make a check query for this one
  61          if (\count($wordArray) < 6) {
  62              // Done with method
  63              return;
  64          }
  65  
  66          // We can only make check queries for alter table and create table queries
  67          $command = strtoupper($wordArray[0] . ' ' . $wordArray[1]);
  68  
  69          if ($command === 'ALTER TABLE') {
  70              $alterCommand = strtoupper($wordArray[3] . ' ' . $wordArray[4]);
  71  
  72              if ($alterCommand === 'ADD') {
  73                  $result = 'SELECT * FROM INFORMATION_SCHEMA.Columns ' . $wordArray[2] . ' WHERE COLUMN_NAME = ' . $this->fixQuote($wordArray[5]);
  74                  $this->queryType = 'ADD';
  75                  $this->msgElements = array($this->fixQuote($wordArray[2]), $this->fixQuote($wordArray[5]));
  76              } elseif ($alterCommand === 'CREATE INDEX') {
  77                  $index = $this->fixQuote(substr($wordArray[5], 0, strpos($wordArray[5], '(')));
  78                  $result = 'SELECT * FROM SYS.INDEXES ' . $wordArray[2] . ' WHERE name = ' . $index;
  79                  $this->queryType = 'CREATE INDEX';
  80                  $this->msgElements = array($this->fixQuote($wordArray[2]), $index);
  81              } elseif (strtoupper($wordArray[3]) === 'MODIFY' || strtoupper($wordArray[3]) === 'CHANGE') {
  82                  $result = 'SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS  WHERE table_name = ' . $this->fixQuote($wordArray[2]);
  83                  $this->queryType = 'ALTER COLUMN COLUMN_NAME =' . $this->fixQuote($wordArray[4]);
  84                  $this->msgElements = array($this->fixQuote($wordArray[2]), $this->fixQuote($wordArray[4]));
  85              }
  86          }
  87  
  88          if ($command === 'CREATE TABLE') {
  89              $table = $wordArray[2];
  90              $result = 'SELECT * FROM sys.TABLES WHERE NAME = ' . $this->fixQuote($table);
  91              $this->queryType = 'CREATE_TABLE';
  92              $this->msgElements = array($this->fixQuote($table));
  93          }
  94  
  95          // Set fields based on results
  96          if ($this->checkQuery = $result) {
  97              // Unchecked status
  98              $this->checkStatus = 0;
  99          } else {
 100              // Skipped
 101              $this->checkStatus = -1;
 102          }
 103      }
 104  
 105      /**
 106       * Fix up integer. Fixes problem with MySQL integer descriptions.
 107       * If you change a column to "integer unsigned" it shows
 108       * as "int(10) unsigned" in the check query.
 109       *
 110       * @param   string  $type1  the column type
 111       * @param   string  $type2  the column attributes
 112       *
 113       * @return  string  The original or changed column type.
 114       *
 115       * @since   2.5
 116       */
 117      private function fixInteger($type1, $type2)
 118      {
 119          $result = $type1;
 120  
 121          if (strtolower($type1) === 'integer' && strtolower(substr($type2, 0, 8)) === 'unsigned') {
 122              $result = 'int';
 123          }
 124  
 125          return $result;
 126      }
 127  
 128      /**
 129       * Fixes up a string for inclusion in a query.
 130       * Replaces name quote character with normal quote for literal.
 131       * Drops trailing semicolon. Injects the database prefix.
 132       *
 133       * @param   string  $string  The input string to be cleaned up.
 134       *
 135       * @return  string  The modified string.
 136       *
 137       * @since   2.5
 138       */
 139      private function fixQuote($string)
 140      {
 141          $string = str_replace('[', '', $string);
 142          $string = str_replace(']', '', $string);
 143          $string = str_replace('"', '', $string);
 144          $string = str_replace(';', '', $string);
 145          $string = str_replace('#__', $this->db->getPrefix(), $string);
 146  
 147          return $this->db->quote($string);
 148      }
 149  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer