Source for file mysql.php

Documentation is available at mysql.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  Schema
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 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     Joomla.Libraries
  16.  * @subpackage  Schema
  17.  * @since       2.5
  18.  */
  19. {
  20.     /**
  21.      * Checks a DDL query to see if it is a known type
  22.      * If yes, build a check query to see if the DDL has been run on the database.
  23.      * If successful, the $msgElements, $queryType, $checkStatus and $checkQuery fields are populated.
  24.      * The $msgElements contains the text to create the user message.
  25.      * The $checkQuery contains the SQL query to check whether the schema change has
  26.      * been run against the current database. The $queryType contains the type of
  27.      * DDL query that was run (for example, CREATE_TABLE, ADD_COLUMN, CHANGE_COLUMN_TYPE, ADD_INDEX).
  28.      * The $checkStatus field is set to zero if the query is created
  29.      *
  30.      * If not successful, $checkQuery is empty and , and $checkStatus is -1.
  31.      * For example, this will happen if the current line is a non-DDL statement.
  32.      *
  33.      * @return void 
  34.      *
  35.      * @since  2.5
  36.      */
  37.     protected function buildCheckQuery()
  38.     {
  39.         // Initialize fields in case we can't create a check query
  40.         $this->checkStatus = -1// change status to skipped
  41.         $result null;
  42.  
  43.         // Remove any newlines
  44.         $this->updateQuery = str_replace("\n"''$this->updateQuery);
  45.  
  46.         // Fix up extra spaces around () and in general
  47.         $find array('#((\s*)\(\s*([^)\s]+)\s*)(\))#''#(\s)(\s*)#');
  48.         $replace array('($3)''$1');
  49.         $updateQuery preg_replace($find$replace$this->updateQuery);
  50.         $wordArray explode(' '$updateQuery);
  51.  
  52.         // First, make sure we have an array of at least 6 elements
  53.         // if not, we can't make a check query for this one
  54.         if (count($wordArray6)
  55.         {
  56.             // Done with method
  57.             return;
  58.         }
  59.  
  60.         // We can only make check queries for alter table and create table queries
  61.         $command strtoupper($wordArray[0' ' $wordArray[1]);
  62.         if ($command === 'ALTER TABLE')
  63.         {
  64.             $alterCommand strtoupper($wordArray[3' ' $wordArray[4]);
  65.             if ($alterCommand == 'ADD COLUMN')
  66.             {
  67.                 $result 'SHOW COLUMNS IN ' $wordArray[2' WHERE field = ' $this->fixQuote($wordArray[5]);
  68.                 $this->queryType = 'ADD_COLUMN';
  69.                 $this->msgElements = array($this->fixQuote($wordArray[2])$this->fixQuote($wordArray[5]));
  70.             }
  71.             elseif ($alterCommand == 'ADD INDEX' || $alterCommand == 'ADD UNIQUE')
  72.             {
  73.                 if ($pos strpos($wordArray[5]'('))
  74.                 {
  75.                     $index $this->fixQuote(substr($wordArray[5]0$pos));
  76.                 }
  77.                 else
  78.                 {
  79.                     $index $this->fixQuote($wordArray[5]);
  80.                 }
  81.                 $result 'SHOW INDEXES IN ' $wordArray[2' WHERE Key_name = ' $index;
  82.                 $this->queryType = 'ADD_INDEX';
  83.                 $this->msgElements = array($this->fixQuote($wordArray[2])$index);
  84.             }
  85.             elseif ($alterCommand == 'DROP INDEX')
  86.             {
  87.                 $index $this->fixQuote($wordArray[5]);
  88.                 $result 'SHOW INDEXES IN ' $wordArray[2' WHERE Key_name = ' $index;
  89.                 $this->queryType = 'DROP_INDEX';
  90.                 $this->checkQueryExpected = 0;
  91.                 $this->msgElements = array($this->fixQuote($wordArray[2])$index);
  92.             }
  93.             elseif ($alterCommand == 'DROP COLUMN')
  94.             {
  95.                 $index $this->fixQuote($wordArray[5]);
  96.                 $result 'SHOW COLUMNS IN ' $wordArray[2' WHERE Field = ' $index;
  97.                 $this->queryType = 'DROP_COLUMN';
  98.                 $this->checkQueryExpected = 0;
  99.                 $this->msgElements = array($this->fixQuote($wordArray[2])$index);
  100.             }
  101.             elseif (strtoupper($wordArray[3]== 'MODIFY')
  102.             {
  103.                 // Kludge to fix problem with "integer unsigned"
  104.                 $type $this->fixQuote($wordArray[5]);
  105.                 if (isset($wordArray[6]))
  106.                 {
  107.                     $type $this->fixQuote($this->fixInteger($wordArray[5]$wordArray[6]));
  108.                 }
  109.                 $result 'SHOW COLUMNS IN ' $wordArray[2' WHERE field = ' $this->fixQuote($wordArray[4]' AND type = ' $type;
  110.                 $this->queryType = 'CHANGE_COLUMN_TYPE';
  111.                 $this->msgElements = array($this->fixQuote($wordArray[2])$this->fixQuote($wordArray[4])$type);
  112.             }
  113.             elseif (strtoupper($wordArray[3]== 'CHANGE')
  114.             {
  115.                 // Kludge to fix problem with "integer unsigned"
  116.                 $type $this->fixQuote($this->fixInteger($wordArray[6]$wordArray[7]));
  117.                 $result 'SHOW COLUMNS IN ' $wordArray[2' WHERE field = ' $this->fixQuote($wordArray[4]' AND type = ' $type;
  118.                 $this->queryType = 'CHANGE_COLUMN_TYPE';
  119.                 $this->msgElements = array($this->fixQuote($wordArray[2])$this->fixQuote($wordArray[4])$type);
  120.             }
  121.         }
  122.  
  123.         if ($command == 'CREATE TABLE')
  124.         {
  125.             if (strtoupper($wordArray[2$wordArray[3$wordArray[4]== 'IFNOTEXISTS')
  126.             {
  127.                 $table $wordArray[5];
  128.             }
  129.             else
  130.             {
  131.                 $table $wordArray[2];
  132.             }
  133.             $result 'SHOW TABLES LIKE ' $this->fixQuote($table);
  134.             $this->queryType = 'CREATE_TABLE';
  135.             $this->msgElements = array($this->fixQuote($table));
  136.         }
  137.  
  138.         // Set fields based on results
  139.         if ($this->checkQuery = $result)
  140.         {
  141.             // Unchecked status
  142.             $this->checkStatus = 0;
  143.         }
  144.         else
  145.         {
  146.             // Skipped
  147.             $this->checkStatus = -1;
  148.         }
  149.     }
  150.  
  151.     /**
  152.      * Fix up integer. Fixes problem with MySQL integer descriptions.
  153.      * If you change a column to "integer unsigned" it shows
  154.      * as "int(10) unsigned" in the check query.
  155.      *
  156.      * @param   string  $type1  the column type
  157.      * @param   string  $type2  the column attributes
  158.      *
  159.      * @return  string  The original or changed column type.
  160.      *
  161.      * @since   2.5
  162.      */
  163.     private function fixInteger($type1$type2)
  164.     {
  165.         $result $type1;
  166.         if (strtolower($type1== "integer" && strtolower(substr($type208)) == 'unsigned')
  167.         {
  168.             $result 'int(10) unsigned';
  169.         }
  170.         return $result;
  171.     }
  172.  
  173.     /**
  174.      * Fixes up a string for inclusion in a query.
  175.      * Replaces name quote character with normal quote for literal.
  176.      * Drops trailing semi-colon. Injects the database prefix.
  177.      *
  178.      * @param   string  $string  The input string to be cleaned up.
  179.      *
  180.      * @return  string  The modified string.
  181.      *
  182.      * @since   2.5
  183.      */
  184.     private function fixQuote($string)
  185.     {
  186.         $string str_replace('`'''$string);
  187.         $string str_replace(';'''$string);
  188.         $string str_replace('#__'$this->db->getPrefix()$string);
  189.         return $this->db->quote($string);
  190.     }
  191. }

Documentation generated on Tue, 19 Nov 2013 15:09:07 +0100 by phpDocumentor 1.4.3