Ejemplo n.º 1
0
 /**
  * Creates a new table
  *
  * @param  SimpleXMLElement    $table  Table
  * @param  string              $colNamePrefix    Prefix to add to all column names
  * @return boolean                               True: success, False: failure
  */
 protected function createTable(SimpleXMLElement $table, $colNamePrefix)
 {
     if ($table->getName() == 'table') {
         $tableName = $this->prefixedName($table, $colNamePrefix);
         $columns = $table->getElementByPath('columns');
         if ($tableName && $columns !== false) {
             $engine = $table->getElementByPath('engine');
             $tableEngine = null;
             if ($engine !== false) {
                 $engineType = $engine->attributes('type');
                 if ($engineType !== null) {
                     $engineTable = $engine->attributes('sameastable');
                     if ($engineTable !== null) {
                         $sameEngine = $this->checkTableEngine($engineTable);
                     } else {
                         $sameEngine = null;
                     }
                     if ($sameEngine) {
                         $tableEngine = $sameEngine;
                     } else {
                         $tableEngine = $engineType;
                     }
                 }
             }
             $sqlColumns = array();
             $tableOptions = array();
             foreach ($columns->children() as $column) {
                 if ($column->getName() == 'column') {
                     $colNamePrefixed = $this->prefixedName($column, $colNamePrefix);
                     $sqlColumns[] = "\n " . $this->_db->NameQuote($colNamePrefixed) . ' ' . $this->fullColumnType($column, $tableName, $tableEngine);
                     if ((int) $column->attributes('auto_increment')) {
                         $tableOptions[] = 'AUTO_INCREMENT=' . (int) $column->attributes('auto_increment');
                     }
                 }
             }
             $indexes = $table->getElementByPath('indexes');
             if ($indexes !== false) {
                 foreach ($indexes->children() as $index) {
                     if ($index->getName() == 'index') {
                         $sqlIndexText = $this->fullIndexType($index, $colNamePrefix);
                         if ($sqlIndexText) {
                             $sqlColumns[] = "\n " . $sqlIndexText;
                         }
                     }
                 }
             }
             if (!$tableEngine) {
                 $cbEngine = $this->checkTableEngine('#__comprofiler');
                 if ($cbEngine) {
                     $tableEngine = $cbEngine;
                 } else {
                     $tableEngine = 'InnoDB';
                 }
             }
             $tableOptions[] = 'ENGINE=' . $tableEngine;
             $collation = $table->attributes('collation');
             if ($collation && $this->_db->versionCompare('4.1')) {
                 $charSet = substr($collation, 0, strpos($collation, '_'));
                 if ($charSet) {
                     $tableOptions[] = 'CHARACTER SET = ' . preg_replace('/[^a-z0-9_]/', '', $charSet);
                     $tableOptions[] = 'COLLATE = ' . preg_replace('/[^a-z0-9_]/', '', $collation);
                 }
             }
             $sql = 'CREATE TABLE ' . $this->_db->NameQuote($tableName) . ' (' . implode(',', $sqlColumns) . "\n )" . implode(', ', $tableOptions);
             if (!$this->doQuery($sql)) {
                 $this->setError(sprintf('%s::createTableof Table %s failed with SQL error: %s', get_class($this), $tableName, $this->_db->getErrorMsg()), $sql);
                 return false;
             } else {
                 $this->setLog(sprintf('Table %s successfully created', $tableName), $sql, 'change');
                 return true;
             }
         }
     }
     return false;
 }