Example #1
0
 /**
  * create a new table
  *
  * @param string $name   Name of the database that should be created
  * @param array $fields  Associative array that contains the definition of each field of the new table
  *                       The indexes of the array entries are the names of the fields of the table an
  *                       the array entry values are associative arrays like those that are meant to be
  *                       passed with the field definitions to get[Type]Declaration() functions.
  *                          array(
  *                              'id' => array(
  *                                  'type' => 'integer',
  *                                  'unsigned' => 1
  *                                  'notnull' => 1
  *                                  'default' => 0
  *                              ),
  *                              'name' => array(
  *                                  'type' => 'text',
  *                                  'length' => 12
  *                              ),
  *                              'password' => array(
  *                                  'type' => 'text',
  *                                  'length' => 12
  *                              )
  *                          );
  * @param array $options  An associative array of table options:
  *                          array(
  *                              'comment' => 'Foo',
  *                              'charset' => 'utf8',
  *                              'collate' => 'utf8_unicode_ci',
  *                              'type'    => 'innodb',
  *                          );
  *
  * @return void
  * @override
  */
 public function getCreateTableSql($name, array $fields, array $options = array())
 {
     if (!$name) {
         throw DoctrineException::missingTableName();
     }
     if (empty($fields)) {
         throw DoctrineException::missingFieldsArrayForTable($name);
     }
     $queryFields = $this->getColumnDeclarationListSql($fields);
     if (isset($options['uniqueConstraints']) && !empty($options['uniqueConstraints'])) {
         foreach ($options['uniqueConstraints'] as $uniqueConstraint) {
             $queryFields .= ', UNIQUE(' . implode(', ', array_values($uniqueConstraint)) . ')';
         }
     }
     // add all indexes
     if (isset($options['indexes']) && !empty($options['indexes'])) {
         foreach ($options['indexes'] as $index => $definition) {
             $queryFields .= ', ' . $this->getIndexDeclarationSql($index, $definition);
         }
     }
     // attach all primary keys
     if (isset($options['primary']) && !empty($options['primary'])) {
         $keyColumns = array_unique(array_values($options['primary']));
         $queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
     }
     $query = 'CREATE ';
     if (!empty($options['temporary'])) {
         $query .= 'TEMPORARY ';
     }
     $query .= 'TABLE ' . $name . ' (' . $queryFields . ')';
     $optionStrings = array();
     if (isset($options['comment'])) {
         $optionStrings['comment'] = 'COMMENT = ' . $this->quote($options['comment'], 'text');
     }
     if (isset($options['charset'])) {
         $optionStrings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
         if (isset($options['collate'])) {
             $optionStrings['charset'] .= ' COLLATE ' . $options['collate'];
         }
     }
     // get the type of the table
     if (isset($options['engine'])) {
         $optionStrings[] = 'ENGINE = ' . $options['engine'];
     } else {
         // default to innodb
         //            $optionStrings[] = 'ENGINE = InnoDB';
         $optionStrings[] = 'ENGINE = MyISAM';
     }
     if (!empty($optionStrings)) {
         $query .= ' ' . implode(' ', $optionStrings);
     }
     $sql[] = $query;
     if (isset($options['foreignKeys'])) {
         foreach ((array) $options['foreignKeys'] as $k => $definition) {
             if (is_array($definition)) {
                 $sql[] = $this->getCreateForeignKeySql($name, $definition);
             }
         }
     }
     return $sql;
 }