Source for file sqlsrv.php

Documentation is available at sqlsrv.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 SQL Server 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')
  66.             {
  67.                 $result 'SELECT * FROM INFORMATION_SCHEMA.Columns ' $wordArray[2' WHERE COLUMN_NAME = ' $this->fixQuote($wordArray[5]);
  68.                 $this->queryType = 'ADD';
  69.                 $this->msgElements = array($this->fixQuote($wordArray[2])$this->fixQuote($wordArray[5]));
  70.             }
  71.             elseif ($alterCommand == 'CREATE INDEX')
  72.             {
  73.                 $index $this->fixQuote(substr($wordArray[5]0strpos($wordArray[5]'(')));
  74.                 $result 'SELECT * FROM SYS.INDEXES ' $wordArray[2' WHERE name = ' $index;
  75.                 $this->queryType = 'CREATE INDEX';
  76.                 $this->msgElements = array($this->fixQuote($wordArray[2])$index);
  77.             }
  78.             elseif (strtoupper($wordArray[3]== 'MODIFY' || strtoupper($wordArray[3]== 'CHANGE')
  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.         }
  85.  
  86.         if ($command == 'CREATE TABLE')
  87.         {
  88.             $table $wordArray[5];
  89.             $result 'SELECT * FROM sys.TABLES WHERE NAME = ' $this->fixQuote($table);
  90.             $this->queryType = 'CREATE_TABLE';
  91.             $this->msgElements = array($this->fixQuote($table));
  92.         }
  93.  
  94.         // Set fields based on results
  95.         if ($this->checkQuery = $result)
  96.         {
  97.             // Unchecked status
  98.             $this->checkStatus = 0;
  99.         }
  100.         else
  101.         {
  102.             // Skipped
  103.             $this->checkStatus = -1;
  104.         }
  105.     }
  106.  
  107.     /**
  108.      * Fix up integer. Fixes problem with MySQL integer descriptions.
  109.      * If you change a column to "integer unsigned" it shows
  110.      * as "int(10) unsigned" in the check query.
  111.      *
  112.      * @param   string  $type1  the column type
  113.      * @param   string  $type2  the column attributes
  114.      *
  115.      * @return  string  The original or changed column type.
  116.      *
  117.      * @since   2.5
  118.      */
  119.     private function fixInteger($type1$type2)
  120.     {
  121.         $result $type1;
  122.         if (strtolower($type1== 'integer' && strtolower(substr($type208)) == 'unsigned')
  123.         {
  124.             $result 'int';
  125.         }
  126.         return $result;
  127.     }
  128.  
  129.     /**
  130.      * Fixes up a string for inclusion in a query.
  131.      * Replaces name quote character with normal quote for literal.
  132.      * Drops trailing semi-colon. Injects the database prefix.
  133.      *
  134.      * @param   string  $string  The input string to be cleaned up.
  135.      *
  136.      * @return  string  The modified string.
  137.      *
  138.      * @since   2.5
  139.      */
  140.     private function fixQuote($string)
  141.     {
  142.         $string str_replace('`'''$string);
  143.         $string str_replace(';'''$string);
  144.         $string str_replace('#__'$this->db->getPrefix()$string);
  145.         return $this->db->quote($string);
  146.     }
  147. }

Documentation generated on Tue, 19 Nov 2013 15:14:18 +0100 by phpDocumentor 1.4.3