Advertisement
Guest User

unzend.com_288

a guest
Jun 30th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 22.63 KB | None | 0 0
  1. <?php
  2. // Ioncube Decoder Unzend.Com Email unzend@gmail.com
  3. // http://www.unzend.com
  4. /**
  5.  * CDbSchema class file.
  6.  *
  7.  * @author Qiang Xue <qiang.xue@gmail.com>
  8.  * @link http://www.yiiframework.com/
  9.  * @copyright 2008-2013 Yii Software LLC
  10.  * @license http://www.yiiframework.com/license/
  11.  */
  12.  
  13. /**
  14.  * CDbSchema is the base class for retrieving metadata information.
  15.  *
  16.  * @property CDbConnection $dbConnection Database connection. The connection is active.
  17.  * @property array $tables The metadata for all tables in the database.
  18.  * Each array element is an instance of {@link CDbTableSchema} (or its child class).
  19.  * The array keys are table names.
  20.  * @property array $tableNames All table names in the database.
  21.  * @property CDbCommandBuilder $commandBuilder The SQL command builder for this connection.
  22.  *
  23.  * @author Qiang Xue <qiang.xue@gmail.com>
  24.  * @package system.db.schema
  25.  * @since 1.0
  26.  */
  27. abstract class CDbSchema extends CComponent
  28. {
  29.     /**
  30.      * @var array the abstract column types mapped to physical column types.
  31.      * @since 1.1.6
  32.      */
  33.     public $columnTypes=array();
  34.  
  35.     private $_tableNames=array();
  36.     private $_tables=array();
  37.     private $_connection;
  38.     private $_builder;
  39.     private $_cacheExclude=array();
  40.  
  41.     /**
  42.      * Loads the metadata for the specified table.
  43.      * @param string $name table name
  44.      * @return CDbTableSchema driver dependent table metadata, null if the table does not exist.
  45.      */
  46.     abstract protected function loadTable($name);
  47.  
  48.     /**
  49.      * Constructor.
  50.      * @param CDbConnection $conn database connection.
  51.      */
  52.     public function __construct($conn)
  53.     {
  54.         $this->_connection=$conn;
  55.         foreach($conn->schemaCachingExclude as $name)
  56.             $this->_cacheExclude[$name]=true;
  57.     }
  58.  
  59.     /**
  60.      * @return CDbConnection database connection. The connection is active.
  61.      */
  62.     public function getDbConnection()
  63.     {
  64.         return $this->_connection;
  65.     }
  66.  
  67.     /**
  68.      * Obtains the metadata for the named table.
  69.      * @param string $name table name
  70.      * @param boolean $refresh if we need to refresh schema cache for a table.
  71.      * Parameter available since 1.1.9
  72.      * @return CDbTableSchema table metadata. Null if the named table does not exist.
  73.      */
  74.     public function getTable($name,$refresh=false)
  75.     {
  76.         if($refresh===false && isset($this->_tables[$name]))
  77.             return $this->_tables[$name];
  78.         else
  79.         {
  80.             if($this->_connection->tablePrefix!==null && strpos($name,'{{')!==false)
  81.                 $realName=preg_replace('/\{\{(.*?)\}\}/',$this->_connection->tablePrefix.'$1',$name);
  82.             else
  83.                 $realName=$name;
  84.  
  85.             // temporarily disable query caching
  86.             if($this->_connection->queryCachingDuration>0)
  87.             {
  88.                 $qcDuration=$this->_connection->queryCachingDuration;
  89.                 $this->_connection->queryCachingDuration=0;
  90.             }
  91.  
  92.             if(!isset($this->_cacheExclude[$name]) && ($duration=$this->_connection->schemaCachingDuration)>0 && $this->_connection->schemaCacheID!==false && ($cache=Yii::app()->getComponent($this->_connection->schemaCacheID))!==null)
  93.             {
  94.                 $key='yii:dbschema'.$this->_connection->connectionString.':'.$this->_connection->username.':'.$name;
  95.                 $table=$cache->get($key);
  96.                 if($refresh===true || $table===false)
  97.                 {
  98.                     $table=$this->loadTable($realName);
  99.                     if($table!==null)
  100.                         $cache->set($key,$table,$duration);
  101.                 }
  102.                 $this->_tables[$name]=$table;
  103.             }
  104.             else
  105.                 $this->_tables[$name]=$table=$this->loadTable($realName);
  106.  
  107.             if(isset($qcDuration))  // re-enable query caching
  108.                 $this->_connection->queryCachingDuration=$qcDuration;
  109.  
  110.             return $table;
  111.         }
  112.     }
  113.  
  114.     /**
  115.      * Returns the metadata for all tables in the database.
  116.      * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
  117.      * @return array the metadata for all tables in the database.
  118.      * Each array element is an instance of {@link CDbTableSchema} (or its child class).
  119.      * The array keys are table names.
  120.      */
  121.     public function getTables($schema='')
  122.     {
  123.         $tables=array();
  124.         foreach($this->getTableNames($schema) as $name)
  125.         {
  126.             if(($table=$this->getTable($name))!==null)
  127.                 $tables[$name]=$table;
  128.         }
  129.         return $tables;
  130.     }
  131.  
  132.     /**
  133.      * Returns all table names in the database.
  134.      * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
  135.      * If not empty, the returned table names will be prefixed with the schema name.
  136.      * @return array all table names in the database.
  137.      */
  138.     public function getTableNames($schema='')
  139.     {
  140.         if(!isset($this->_tableNames[$schema]))
  141.             $this->_tableNames[$schema]=$this->findTableNames($schema);
  142.         return $this->_tableNames[$schema];
  143.     }
  144.  
  145.     /**
  146.      * @return CDbCommandBuilder the SQL command builder for this connection.
  147.      */
  148.     public function getCommandBuilder()
  149.     {
  150.         if($this->_builder!==null)
  151.             return $this->_builder;
  152.         else
  153.             return $this->_builder=$this->createCommandBuilder();
  154.     }
  155.  
  156.     /**
  157.      * Refreshes the schema.
  158.      * This method resets the loaded table metadata and command builder
  159.      * so that they can be recreated to reflect the change of schema.
  160.      */
  161.     public function refresh()
  162.     {
  163.         if(($duration=$this->_connection->schemaCachingDuration)>0 && $this->_connection->schemaCacheID!==false && ($cache=Yii::app()->getComponent($this->_connection->schemaCacheID))!==null)
  164.         {
  165.             foreach(array_keys($this->_tables) as $name)
  166.             {
  167.                 if(!isset($this->_cacheExclude[$name]))
  168.                 {
  169.                     $key='yii:dbschema'.$this->_connection->connectionString.':'.$this->_connection->username.':'.$name;
  170.                     $cache->delete($key);
  171.                 }
  172.             }
  173.         }
  174.         $this->_tables=array();
  175.         $this->_tableNames=array();
  176.         $this->_builder=null;
  177.     }
  178.  
  179.     /**
  180.      * Quotes a table name for use in a query.
  181.      * If the table name contains schema prefix, the prefix will also be properly quoted.
  182.      * @param string $name table name
  183.      * @return string the properly quoted table name
  184.      * @see quoteSimpleTableName
  185.      */
  186.     public function quoteTableName($name)
  187.     {
  188.         if(strpos($name,'.')===false)
  189.             return $this->quoteSimpleTableName($name);
  190.         $parts=explode('.',$name);
  191.         foreach($parts as $i=>$part)
  192.             $parts[$i]=$this->quoteSimpleTableName($part);
  193.         return implode('.',$parts);
  194.  
  195.     }
  196.  
  197.     /**
  198.      * Quotes a simple table name for use in a query.
  199.      * A simple table name does not schema prefix.
  200.      * @param string $name table name
  201.      * @return string the properly quoted table name
  202.      * @since 1.1.6
  203.      */
  204.     public function quoteSimpleTableName($name)
  205.     {
  206.         return "'".$name."'";
  207.     }
  208.  
  209.     /**
  210.      * Quotes a column name for use in a query.
  211.      * If the column name contains prefix, the prefix will also be properly quoted.
  212.      * @param string $name column name
  213.      * @return string the properly quoted column name
  214.      * @see quoteSimpleColumnName
  215.      */
  216.     public function quoteColumnName($name)
  217.     {
  218.         if(($pos=strrpos($name,'.'))!==false)
  219.         {
  220.             $prefix=$this->quoteTableName(substr($name,0,$pos)).'.';
  221.             $name=substr($name,$pos+1);
  222.         }
  223.         else
  224.             $prefix='';
  225.         return $prefix . ($name==='*' ? $name : $this->quoteSimpleColumnName($name));
  226.     }
  227.  
  228.     /**
  229.      * Quotes a simple column name for use in a query.
  230.      * A simple column name does not contain prefix.
  231.      * @param string $name column name
  232.      * @return string the properly quoted column name
  233.      * @since 1.1.6
  234.      */
  235.     public function quoteSimpleColumnName($name)
  236.     {
  237.         return '"'.$name.'"';
  238.     }
  239.  
  240.     /**
  241.      * Compares two table names.
  242.      * The table names can be either quoted or unquoted. This method
  243.      * will consider both cases.
  244.      * @param string $name1 table name 1
  245.      * @param string $name2 table name 2
  246.      * @return boolean whether the two table names refer to the same table.
  247.      */
  248.     public function compareTableNames($name1,$name2)
  249.     {
  250.         $name1=str_replace(array('"','`',"'"),'',$name1);
  251.         $name2=str_replace(array('"','`',"'"),'',$name2);
  252.         if(($pos=strrpos($name1,'.'))!==false)
  253.             $name1=substr($name1,$pos+1);
  254.         if(($pos=strrpos($name2,'.'))!==false)
  255.             $name2=substr($name2,$pos+1);
  256.         if($this->_connection->tablePrefix!==null)
  257.         {
  258.             if(strpos($name1,'{')!==false)
  259.                 $name1=$this->_connection->tablePrefix.str_replace(array('{','}'),'',$name1);
  260.             if(strpos($name2,'{')!==false)
  261.                 $name2=$this->_connection->tablePrefix.str_replace(array('{','}'),'',$name2);
  262.         }
  263.         return $name1===$name2;
  264.     }
  265.  
  266.     /**
  267.      * Resets the sequence value of a table's primary key.
  268.      * The sequence will be reset such that the primary key of the next new row inserted
  269.      * will have the specified value or max value of a primary key plus one (i.e. sequence trimming).
  270.      * @param CDbTableSchema $table the table schema whose primary key sequence will be reset
  271.      * @param integer|null $value the value for the primary key of the next new row inserted.
  272.      * If this is not set, the next new row's primary key will have the max value of a primary
  273.      * key plus one (i.e. sequence trimming).
  274.      * @since 1.1
  275.      */
  276.     public function resetSequence($table,$value=null)
  277.     {
  278.     }
  279.  
  280.     /**
  281.      * Enables or disables integrity check.
  282.      * @param boolean $check whether to turn on or off the integrity check.
  283.      * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
  284.      * @since 1.1
  285.      */
  286.     public function checkIntegrity($check=true,$schema='')
  287.     {
  288.     }
  289.  
  290.     /**
  291.      * Creates a command builder for the database.
  292.      * This method may be overridden by child classes to create a DBMS-specific command builder.
  293.      * @return CDbCommandBuilder command builder instance
  294.      */
  295.     protected function createCommandBuilder()
  296.     {
  297.         return new CDbCommandBuilder($this);
  298.     }
  299.  
  300.     /**
  301.      * Returns all table names in the database.
  302.      * This method should be overridden by child classes in order to support this feature
  303.      * because the default implementation simply throws an exception.
  304.      * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
  305.      * If not empty, the returned table names will be prefixed with the schema name.
  306.      * @throws CDbException if current schema does not support fetching all table names
  307.      * @return array all table names in the database.
  308.      */
  309.     protected function findTableNames($schema='')
  310.     {
  311.         throw new CDbException(Yii::t('yii','{class} does not support fetching all table names.',
  312.             array('{class}'=>get_class($this))));
  313.     }
  314.  
  315.     /**
  316.      * Converts an abstract column type into a physical column type.
  317.      * The conversion is done using the type map specified in {@link columnTypes}.
  318.      * These abstract column types are supported (using MySQL as example to explain the corresponding
  319.      * physical types):
  320.      * <ul>
  321.      * <li>pk: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY"</li>
  322.      * <li>string: string type, will be converted into "varchar(255)"</li>
  323.      * <li>text: a long string type, will be converted into "text"</li>
  324.      * <li>integer: integer type, will be converted into "int(11)"</li>
  325.      * <li>boolean: boolean type, will be converted into "tinyint(1)"</li>
  326.      * <li>float: float number type, will be converted into "float"</li>
  327.      * <li>decimal: decimal number type, will be converted into "decimal"</li>
  328.      * <li>datetime: datetime type, will be converted into "datetime"</li>
  329.      * <li>timestamp: timestamp type, will be converted into "timestamp"</li>
  330.      * <li>time: time type, will be converted into "time"</li>
  331.      * <li>date: date type, will be converted into "date"</li>
  332.      * <li>binary: binary data type, will be converted into "blob"</li>
  333.      * </ul>
  334.      *
  335.      * If the abstract type contains two or more parts separated by spaces (e.g. "string NOT NULL"), then only
  336.      * the first part will be converted, and the rest of the parts will be appended to the conversion result.
  337.      * For example, 'string NOT NULL' is converted to 'varchar(255) NOT NULL'.
  338.      * @param string $type abstract column type
  339.      * @return string physical column type.
  340.      * @since 1.1.6
  341.      */
  342.     public function getColumnType($type)
  343.     {
  344.         if(isset($this->columnTypes[$type]))
  345.             return $this->columnTypes[$type];
  346.         elseif(($pos=strpos($type,' '))!==false)
  347.         {
  348.             $t=substr($type,0,$pos);
  349.             return (isset($this->columnTypes[$t]) ? $this->columnTypes[$t] : $t).substr($type,$pos);
  350.         }
  351.         else
  352.             return $type;
  353.     }
  354.  
  355.     /**
  356.      * Builds a SQL statement for creating a new DB table.
  357.      *
  358.      * The columns in the new  table should be specified as name-definition pairs (e.g. 'name'=>'string'),
  359.      * where name stands for a column name which will be properly quoted by the method, and definition
  360.      * stands for the column type which can contain an abstract DB type.
  361.      * The {@link getColumnType} method will be invoked to convert any abstract type into a physical one.
  362.      *
  363.      * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly
  364.      * inserted into the generated SQL.
  365.      *
  366.      * @param string $table the name of the table to be created. The name will be properly quoted by the method.
  367.      * @param array $columns the columns (name=>definition) in the new table.
  368.      * @param string $options additional SQL fragment that will be appended to the generated SQL.
  369.      * @return string the SQL statement for creating a new DB table.
  370.      * @since 1.1.6
  371.      */
  372.     public function createTable($table, $columns, $options=null)
  373.     {
  374.         $cols=array();
  375.         foreach($columns as $name=>$type)
  376.         {
  377.             if(is_string($name))
  378.                 $cols[]="\t".$this->quoteColumnName($name).' '.$this->getColumnType($type);
  379.             else
  380.                 $cols[]="\t".$type;
  381.         }
  382.         $sql="CREATE TABLE ".$this->quoteTableName($table)." (\n".implode(",\n",$cols)."\n)";
  383.         return $options===null ? $sql : $sql.' '.$options;
  384.     }
  385.  
  386.     /**
  387.      * Builds a SQL statement for renaming a DB table.
  388.      * @param string $table the table to be renamed. The name will be properly quoted by the method.
  389.      * @param string $newName the new table name. The name will be properly quoted by the method.
  390.      * @return string the SQL statement for renaming a DB table.
  391.      * @since 1.1.6
  392.      */
  393.     public function renameTable($table, $newName)
  394.     {
  395.         return 'RENAME TABLE ' . $this->quoteTableName($table) . ' TO ' . $this->quoteTableName($newName);
  396.     }
  397.  
  398.     /**
  399.      * Builds a SQL statement for dropping a DB table.
  400.      * @param string $table the table to be dropped. The name will be properly quoted by the method.
  401.      * @return string the SQL statement for dropping a DB table.
  402.      * @since 1.1.6
  403.      */
  404.     public function dropTable($table)
  405.     {
  406.         return "DROP TABLE ".$this->quoteTableName($table);
  407.     }
  408.  
  409.     /**
  410.      * Builds a SQL statement for truncating a DB table.
  411.      * @param string $table the table to be truncated. The name will be properly quoted by the method.
  412.      * @return string the SQL statement for truncating a DB table.
  413.      * @since 1.1.6
  414.      */
  415.     public function truncateTable($table)
  416.     {
  417.         return "TRUNCATE TABLE ".$this->quoteTableName($table);
  418.     }
  419.  
  420.     /**
  421.      * Builds a SQL statement for adding a new DB column.
  422.      * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method.
  423.      * @param string $column the name of the new column. The name will be properly quoted by the method.
  424.      * @param string $type the column type. The {@link getColumnType} method will be invoked to convert abstract column type (if any)
  425.      * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL.
  426.      * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
  427.      * @return string the SQL statement for adding a new column.
  428.      * @since 1.1.6
  429.      */
  430.     public function addColumn($table, $column, $type)
  431.     {
  432.         return 'ALTER TABLE ' . $this->quoteTableName($table)
  433.             . ' ADD ' . $this->quoteColumnName($column) . ' '
  434.             . $this->getColumnType($type);
  435.     }
  436.  
  437.     /**
  438.      * Builds a SQL statement for dropping a DB column.
  439.      * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method.
  440.      * @param string $column the name of the column to be dropped. The name will be properly quoted by the method.
  441.      * @return string the SQL statement for dropping a DB column.
  442.      * @since 1.1.6
  443.      */
  444.     public function dropColumn($table, $column)
  445.     {
  446.         return "ALTER TABLE ".$this->quoteTableName($table)
  447.             ." DROP COLUMN ".$this->quoteColumnName($column);
  448.     }
  449.  
  450.     /**
  451.      * Builds a SQL statement for renaming a column.
  452.      * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method.
  453.      * @param string $name the old name of the column. The name will be properly quoted by the method.
  454.      * @param string $newName the new name of the column. The name will be properly quoted by the method.
  455.      * @return string the SQL statement for renaming a DB column.
  456.      * @since 1.1.6
  457.      */
  458.     public function renameColumn($table, $name, $newName)
  459.     {
  460.         return "ALTER TABLE ".$this->quoteTableName($table)
  461.             . " RENAME COLUMN ".$this->quoteColumnName($name)
  462.             . " TO ".$this->quoteColumnName($newName);
  463.     }
  464.  
  465.     /**
  466.      * Builds a SQL statement for changing the definition of a column.
  467.      * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method.
  468.      * @param string $column the name of the column to be changed. The name will be properly quoted by the method.
  469.      * @param string $type the new column type. The {@link getColumnType} method will be invoked to convert abstract column type (if any)
  470.      * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL.
  471.      * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
  472.      * @return string the SQL statement for changing the definition of a column.
  473.      * @since 1.1.6
  474.      */
  475.     public function alterColumn($table, $column, $type)
  476.     {
  477.         return 'ALTER TABLE ' . $this->quoteTableName($table) . ' CHANGE '
  478.             . $this->quoteColumnName($column) . ' '
  479.             . $this->quoteColumnName($column) . ' '
  480.             . $this->getColumnType($type);
  481.     }
  482.  
  483.     /**
  484.      * Builds a SQL statement for adding a foreign key constraint to an existing table.
  485.      * The method will properly quote the table and column names.
  486.      * @param string $name the name of the foreign key constraint.
  487.      * @param string $table the table that the foreign key constraint will be added to.
  488.      * @param string $columns the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas.
  489.      * @param string $refTable the table that the foreign key references to.
  490.      * @param string $refColumns the name of the column that the foreign key references to. If there are multiple columns, separate them with commas.
  491.      * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
  492.      * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
  493.      * @return string the SQL statement for adding a foreign key constraint to an existing table.
  494.      * @since 1.1.6
  495.      */
  496.     public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete=null, $update=null)
  497.     {
  498.         $columns=preg_split('/\s*,\s*/',$columns,-1,PREG_SPLIT_NO_EMPTY);
  499.         foreach($columns as $i=>$col)
  500.             $columns[$i]=$this->quoteColumnName($col);
  501.         $refColumns=preg_split('/\s*,\s*/',$refColumns,-1,PREG_SPLIT_NO_EMPTY);
  502.         foreach($refColumns as $i=>$col)
  503.             $refColumns[$i]=$this->quoteColumnName($col);
  504.         $sql='ALTER TABLE '.$this->quoteTableName($table)
  505.             .' ADD CONSTRAINT '.$this->quoteColumnName($name)
  506.             .' FOREIGN KEY ('.implode(', ', $columns).')'
  507.             .' REFERENCES '.$this->quoteTableName($refTable)
  508.             .' ('.implode(', ', $refColumns).')';
  509.         if($delete!==null)
  510.             $sql.=' ON DELETE '.$delete;
  511.         if($update!==null)
  512.             $sql.=' ON UPDATE '.$update;
  513.         return $sql;
  514.     }
  515.  
  516.     /**
  517.      * Builds a SQL statement for dropping a foreign key constraint.
  518.      * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method.
  519.      * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method.
  520.      * @return string the SQL statement for dropping a foreign key constraint.
  521.      * @since 1.1.6
  522.      */
  523.     public function dropForeignKey($name, $table)
  524.     {
  525.         return 'ALTER TABLE '.$this->quoteTableName($table)
  526.             .' DROP CONSTRAINT '.$this->quoteColumnName($name);
  527.     }
  528.  
  529.     /**
  530.      * Builds a SQL statement for creating a new index.
  531.      * @param string $name the name of the index. The name will be properly quoted by the method.
  532.      * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method.
  533.      * @param string $column the column(s) that should be included in the index. If there are multiple columns, please separate them
  534.      * by commas. Each column name will be properly quoted by the method, unless a parenthesis is found in the name.
  535.      * @param boolean $unique whether to add UNIQUE constraint on the created index.
  536.      * @return string the SQL statement for creating a new index.
  537.      * @since 1.1.6
  538.      */
  539.     public function createIndex($name, $table, $column, $unique=false)
  540.     {
  541.         $cols=array();
  542.         $columns=preg_split('/\s*,\s*/',$column,-1,PREG_SPLIT_NO_EMPTY);
  543.         foreach($columns as $col)
  544.         {
  545.             if(strpos($col,'(')!==false)
  546.                 $cols[]=$col;
  547.             else
  548.                 $cols[]=$this->quoteColumnName($col);
  549.         }
  550.         return ($unique ? 'CREATE UNIQUE INDEX ' : 'CREATE INDEX ')
  551.             . $this->quoteTableName($name).' ON '
  552.             . $this->quoteTableName($table).' ('.implode(', ',$cols).')';
  553.     }
  554.  
  555.     /**
  556.      * Builds a SQL statement for dropping an index.
  557.      * @param string $name the name of the index to be dropped. The name will be properly quoted by the method.
  558.      * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method.
  559.      * @return string the SQL statement for dropping an index.
  560.      * @since 1.1.6
  561.      */
  562.     public function dropIndex($name, $table)
  563.     {
  564.         return 'DROP INDEX '.$this->quoteTableName($name).' ON '.$this->quoteTableName($table);
  565.     }
  566.  
  567.     /**
  568.      * Builds a SQL statement for adding a primary key constraint to an existing table.
  569.      * @param string $name the name of the primary key constraint.
  570.      * @param string $table the table that the primary key constraint will be added to.
  571.      * @param string|array $columns comma separated string or array of columns that the primary key will consist of.
  572.      * Array value can be passed since 1.1.14.
  573.      * @return string the SQL statement for adding a primary key constraint to an existing table.
  574.      * @since 1.1.13
  575.      */
  576.     public function addPrimaryKey($name,$table,$columns)
  577.     {
  578.         if(is_string($columns))
  579.             $columns=preg_split('/\s*,\s*/',$columns,-1,PREG_SPLIT_NO_EMPTY);
  580.         foreach($columns as $i=>$col)
  581.             $columns[$i]=$this->quoteColumnName($col);
  582.         return 'ALTER TABLE ' . $this->quoteTableName($table) . ' ADD CONSTRAINT '
  583.             . $this->quoteColumnName($name) . '  PRIMARY KEY ('
  584.             . implode(', ', $columns). ' )';
  585.     }
  586.  
  587.     /**
  588.      * Builds a SQL statement for removing a primary key constraint to an existing table.
  589.      * @param string $name the name of the primary key constraint to be removed.
  590.      * @param string $table the table that the primary key constraint will be removed from.
  591.      * @return string the SQL statement for removing a primary key constraint from an existing table.
  592.      * @since 1.1.13
  593.      */
  594.     public function dropPrimaryKey($name,$table)
  595.     {
  596.         return 'ALTER TABLE ' . $this->quoteTableName($table) . ' DROP CONSTRAINT '
  597.             . $this->quoteColumnName($name);
  598.     }
  599. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement