Example #1
0
 /**
  * Retrieve columns and primary keys definition array for create table
  *
  * @param Varien_Db_Ddl_Table $table
  * @return array
  */
 protected function _getColumnsDefinition(Varien_Db_Ddl_Table $table)
 {
     $definition = array();
     $primary = array();
     $columns = $table->getColumns();
     if (empty($columns)) {
         throw new Zend_Db_Exception('Table columns are not defined');
     }
     $dataTypes = array(Varien_Db_Ddl_Table::TYPE_BOOLEAN => 'bool', Varien_Db_Ddl_Table::TYPE_TINYINT => 'tinyint', Varien_Db_Ddl_Table::TYPE_SMALLINT => 'smallint', Varien_Db_Ddl_Table::TYPE_INTEGER => 'int', Varien_Db_Ddl_Table::TYPE_BIGINT => 'bigint', Varien_Db_Ddl_Table::TYPE_DOUBLE => 'double', Varien_Db_Ddl_Table::TYPE_FLOAT => 'float', Varien_Db_Ddl_Table::TYPE_REAL => 'real', Varien_Db_Ddl_Table::TYPE_DECIMAL => 'decimal', Varien_Db_Ddl_Table::TYPE_NUMERIC => 'decimal', Varien_Db_Ddl_Table::TYPE_DATE => 'date', Varien_Db_Ddl_Table::TYPE_TIME => 'time', Varien_Db_Ddl_Table::TYPE_TIMESTAMP => 'timestamp', Varien_Db_Ddl_Table::TYPE_CHAR => 'char', Varien_Db_Ddl_Table::TYPE_VARCHAR => 'varchar', Varien_Db_Ddl_Table::TYPE_LONGVARCHAR => 'text', Varien_Db_Ddl_Table::TYPE_CLOB => 'longtext', Varien_Db_Ddl_Table::TYPE_BINARY => 'blob', Varien_Db_Ddl_Table::TYPE_VARBINARY => 'mediumblob', Varien_Db_Ddl_Table::TYPE_LONGVARBINARY => 'longblob', Varien_Db_Ddl_Table::TYPE_BLOB => 'longblob');
     foreach ($columns as $columnData) {
         $cType = $dataTypes[$columnData['COLUMN_TYPE']];
         $cUnsigned = $columnData['UNSIGNED'] ? ' unsigned' : '';
         $cIsNull = $columnData['NULLABLE'] === false ? ' NOT NULL' : '';
         $cDefault = '';
         if (!is_null($columnData['DEFAULT'])) {
             $cDefault = $this->quoteInto(' default ?', $columnData['DEFAULT']);
         }
         // column size
         switch ($columnData['COLUMN_TYPE']) {
             case Varien_Db_Ddl_Table::TYPE_TINYINT:
             case Varien_Db_Ddl_Table::TYPE_SMALLINT:
             case Varien_Db_Ddl_Table::TYPE_INTEGER:
             case Varien_Db_Ddl_Table::TYPE_BIGINT:
                 if (is_numeric($columnData['LENGTH'])) {
                     $cType .= sprintf('(%d)', $columnData['LENGTH']);
                 }
                 break;
             case Varien_Db_Ddl_Table::TYPE_DECIMAL:
             case Varien_Db_Ddl_Table::TYPE_NUMERIC:
                 $cType .= sprintf('(%d,%d)', $columnData['SCALE'], $columnData['PRECISION']);
                 break;
             case Varien_Db_Ddl_Table::TYPE_CHAR:
             case Varien_Db_Ddl_Table::TYPE_VARCHAR:
                 $cType .= sprintf('(%d)', $columnData['LENGTH']);
                 break;
         }
         if ($columnData['PRIMARY']) {
             $primary[$columnData['COLUMN_NAME']] = $columnData['PRIMARY_POSITION'];
         }
         $definition[] = sprintf('  %s %s%s%s%s', $this->quoteIdentifier($columnData['COLUMN_NAME']), $cType, $cUnsigned, $cIsNull, $cDefault);
     }
     // PRIMARY KEY
     if (!empty($primary)) {
         asort($primary, SORT_NUMERIC);
         $primary = array_map(array($this, 'quoteIdentifier'), array_keys($primary));
         $definition[] = sprintf('  PRIMARY KEY (%s)', join(', ', $primary));
     }
     return $definition;
 }
Example #2
0
 /**
  * Retrieve columns and primary keys definition array for create table
  *
  * @param Varien_Db_Ddl_Table $table
  * @return array
  * @throws Zend_Db_Exception
  */
 protected function _getColumnsDefinition(Varien_Db_Ddl_Table $table)
 {
     $definition = array();
     $primary = array();
     $columns = $table->getColumns();
     if (empty($columns)) {
         throw new Zend_Db_Exception('Table columns are not defined');
     }
     foreach ($columns as $columnData) {
         $columnDefinition = $this->_getColumnDefinition($columnData);
         if ($columnData['PRIMARY']) {
             $primary[$columnData['COLUMN_NAME']] = $columnData['PRIMARY_POSITION'];
         }
         $definition[] = sprintf('  %s %s', $this->quoteIdentifier($columnData['COLUMN_NAME']), $columnDefinition);
     }
     // PRIMARY KEY
     if (!empty($primary)) {
         asort($primary, SORT_NUMERIC);
         $primary = array_map(array($this, 'quoteIdentifier'), array_keys($primary));
         $definition[] = sprintf('  PRIMARY KEY (%s)', implode(', ', $primary));
     }
     return $definition;
 }
Example #3
0
 /**
  * Retrieve table indexes definition array for create table
  *
  * @param Varien_Db_Ddl_Table $table
  * @return array
  */
 protected function _getIndexesDefinition(Varien_Db_Ddl_Table $table)
 {
     $definition = array();
     $indexes = $table->getIndexes();
     if (!empty($indexes)) {
         foreach ($indexes as $indexData) {
             if (!empty($indexData['TYPE'])) {
                 switch ($indexData['TYPE']) {
                     case 'primary':
                         $indexType = 'PRIMARY KEY';
                         unset($indexData['INDEX_NAME']);
                         break;
                     default:
                         $indexType = strtoupper($indexData['TYPE']);
                         break;
                 }
             } else {
                 $indexType = 'KEY';
             }
             $columns = array();
             $columnsDefinition = $table->getColumns();
             foreach ($indexData['COLUMNS'] as $columnData) {
                 $columnCode = strtoupper($columnData['NAME']);
                 $column = $this->quoteIdentifier($columnData['NAME']);
                 if (!empty($columnData['SIZE'])) {
                     $column .= sprintf('(%d)', $columnData['SIZE']);
                 } else {
                     if (isset($columnsDefinition[$columnCode]['DATA_TYPE']) && in_array($columnsDefinition[$columnCode]['DATA_TYPE'], array(Varien_Db_Ddl_Table::TYPE_BLOB, Varien_Db_Ddl_Table::TYPE_TEXT))) {
                         if (!empty($columnsDefinition[$columnCode]['LENGTH']) && is_numeric($columnsDefinition[$columnCode]['LENGTH'])) {
                             $column .= "({$columnsDefinition[$columnCode]['LENGTH']})";
                         } else {
                             $column .= '(255)';
                         }
                     }
                 }
                 $columns[] = $column;
             }
             $indexName = isset($indexData['INDEX_NAME']) ? $this->quoteIdentifier($indexData['INDEX_NAME']) : '';
             $definition[] = sprintf('  %s %s (%s)', $indexType, $indexName, implode(', ', $columns));
         }
     }
     return $definition;
 }