[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/cms/schema/ -> changeitemsqlsrv.php (source)

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


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1