Source for file postgresql.php

Documentation is available at postgresql.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 PostgreSQL DDL query to see if it has been run.
  14.  *
  15.  * @package     Joomla.Libraries
  16.  * @subpackage  Schema
  17.  * @since       3.0
  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  3.0
  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 'SELECT column_name FROM information_schema.columns WHERE table_name='
  68.                 . $this->fixQuote($wordArray[2]' AND column_name=' $this->fixQuote($wordArray[5]);
  69.  
  70.                 $this->queryType = 'ADD_COLUMN';
  71.                 $this->msgElements = array($this->fixQuote($wordArray[2])$this->fixQuote($wordArray[5]));
  72.             }
  73.             elseif ($alterCommand === 'ALTER COLUMN')
  74.             {
  75.                 if (strtoupper($wordArray[6]== 'TYPE')
  76.                 {
  77.                     $type '';
  78.                     for ($i 7$i count($wordArray)$i++)
  79.                     {
  80.                         $type .= $wordArray[$i' ';
  81.                     }
  82.  
  83.                     if ($pos strpos($type'('))
  84.                     {
  85.                         $type substr($type0$pos);
  86.                     }
  87.  
  88.                     if ($pos strpos($type';'))
  89.                     {
  90.                         $type substr($type0$pos);
  91.                     }
  92.  
  93.                     $result 'SELECT column_name, data_type FROM information_schema.columns WHERE table_name='
  94.                         . $this->fixQuote($wordArray[2]' AND column_name=' $this->fixQuote($wordArray[5])
  95.                         . ' AND data_type=' $this->fixQuote($type);
  96.  
  97.                     $this->queryType = 'CHANGE_COLUMN_TYPE';
  98.                     $this->msgElements = array($this->fixQuote($wordArray[2])$this->fixQuote($wordArray[5])$type);
  99.                 }
  100.                 elseif (strtoupper($wordArray[7' ' $wordArray[8]== 'NOT NULL')
  101.                 {
  102.                     if (strtoupper($wordArray[6]== 'SET')
  103.                     {
  104.                         // SET NOT NULL
  105.                         $isNullable $this->fixQuote('NO');
  106.                     }
  107.                     else
  108.                     {
  109.                         // DROP NOT NULL
  110.                         $isNullable $this->fixQuote('YES');
  111.                     }
  112.                     $result 'SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_name='
  113.                         . $this->fixQuote($wordArray[2]' AND column_name=' $this->fixQuote($wordArray[5])
  114.                         . ' AND is_nullable=' $isNullable;
  115.  
  116.                     $this->queryType = 'CHANGE_COLUMN_TYPE';
  117.                     $this->checkQueryExpected = 1;
  118.                     $this->msgElements = array($this->fixQuote($wordArray[2])$this->fixQuote($wordArray[5])$isNullable);
  119.                 }
  120.                 elseif (strtoupper($wordArray[7]=== 'DEFAULT')
  121.                 {
  122.                     if (strtoupper($wordArray[6]== 'SET')
  123.                     {
  124.                         $isNullDef 'IS NOT NULL';
  125.                     }
  126.                     else
  127.                     {
  128.                         // DROP DEFAULT
  129.                         $isNullDef 'IS NULL';
  130.                     }
  131.                     $result 'SELECT column_name, data_type, column_default FROM information_schema.columns WHERE table_name='
  132.                         . $this->fixQuote($wordArray[2]' AND column_name=' $this->fixQuote($wordArray[5])
  133.                         . ' AND column_default ' $isNullDef;
  134.  
  135.                     $this->queryType = 'CHANGE_COLUMN_TYPE';
  136.                     $this->checkQueryExpected = 1;
  137.                     $this->msgElements = array($this->fixQuote($wordArray[2])$this->fixQuote($wordArray[5])$isNullDef);
  138.                 }
  139.             }
  140.         }
  141.         elseif ($command === 'DROP INDEX')
  142.         {
  143.             if (strtoupper($wordArray[2$wordArray[3]== 'IFEXISTS')
  144.             {
  145.                 $idx $this->fixQuote($wordArray[4]);
  146.             }
  147.             else
  148.             {
  149.                 $idx $this->fixQuote($wordArray[2]);
  150.             }
  151.  
  152.             $result 'SELECT * FROM pg_indexes WHERE indexname=' $idx;
  153.             $this->queryType = 'DROP_INDEX';
  154.             $this->checkQueryExpected = 0;
  155.             $this->msgElements = array($this->fixQuote($idx));
  156.         }
  157.         elseif ($command == 'CREATE INDEX' || (strtoupper($command $wordArray[2]== 'CREATE UNIQUE INDEX'))
  158.         {
  159.             if ($wordArray[1=== 'UNIQUE')
  160.             {
  161.                 $idx $this->fixQuote($wordArray[3]);
  162.                 $table $this->fixQuote($wordArray[5]);
  163.             }
  164.             else
  165.             {
  166.                 $idx $this->fixQuote($wordArray[2]);
  167.                 $table $this->fixQuote($wordArray[4]);
  168.             }
  169.  
  170.             $result 'SELECT * FROM pg_indexes WHERE indexname=' $idx ' AND tablename=' $table;
  171.             $this->queryType = 'ADD_INDEX';
  172.             $this->checkQueryExpected = 1;
  173.             $this->msgElements = array($table$idx);
  174.         }
  175.  
  176.         if ($command == 'CREATE TABLE')
  177.         {
  178.             if (strtoupper($wordArray[2$wordArray[3$wordArray[4]== 'IFNOTEXISTS')
  179.             {
  180.                 $table $this->fixQuote($wordArray[5]);
  181.             }
  182.             else
  183.             {
  184.                 $table $this->fixQuote($wordArray[2]);
  185.             }
  186.             $result 'SELECT table_name FROM information_schema.tables WHERE table_name=' $table;
  187.             $this->queryType = 'CREATE_TABLE';
  188.             $this->checkQueryExpected = 1;
  189.             $this->msgElements = array($table);
  190.         }
  191.  
  192.         // Set fields based on results
  193.         if ($this->checkQuery = $result)
  194.         {
  195.             // Unchecked status
  196.             $this->checkStatus = 0;
  197.         }
  198.         else
  199.         {
  200.             // Skipped
  201.             $this->checkStatus = -1;
  202.         }
  203.     }
  204.  
  205.     /**
  206.      * Fix up integer. Fixes problem with PostgreSQL integer descriptions.
  207.      * If you change a column to "integer unsigned" it shows
  208.      * as "int(10) unsigned" in the check query.
  209.      *
  210.      * @param   string  $type1  the column type
  211.      * @param   string  $type2  the column attributes
  212.      *
  213.      * @return  string  The original or changed column type.
  214.      *
  215.      * @since   3.0
  216.      */
  217.     private function fixInteger($type1$type2)
  218.     {
  219.         $result $type1;
  220.         if (strtolower($type1== 'integer' && strtolower(substr($type208)) == 'unsigned')
  221.         {
  222.             $result 'unsigned int(10)';
  223.         }
  224.         return $result;
  225.     }
  226.  
  227.     /**
  228.      * Fixes up a string for inclusion in a query.
  229.      * Replaces name quote character with normal quote for literal.
  230.      * Drops trailing semi-colon. Injects the database prefix.
  231.      *
  232.      * @param   string  $string  The input string to be cleaned up.
  233.      *
  234.      * @return  string  The modified string.
  235.      *
  236.      * @since   3.0
  237.      */
  238.     private function fixQuote($string)
  239.     {
  240.         $string str_replace('"'''$string);
  241.         $string str_replace(';'''$string);
  242.         $string str_replace('#__'$this->db->getPrefix()$string);
  243.         return $this->db->quote($string);
  244.     }
  245. }

Documentation generated on Tue, 19 Nov 2013 15:11:10 +0100 by phpDocumentor 1.4.3