public function getPrimaryKeyDDL(Table $table)
 {
     if ($table->hasPrimaryKey()) {
         $pattern = 'CONSTRAINT %s PRIMARY KEY (%s)';
         return sprintf($pattern, $this->quoteIdentifier($this->getPrimaryKeyName($table)), $this->getColumnListDDL($table->getPrimaryKey()));
     }
 }
Example #2
0
 /**
  * Returns the SQL for the primary key of a Table object.
  *
  * @return string
  */
 public function getPrimaryKeyDDL(Table $table)
 {
     if ($table->hasPrimaryKey() && 1 < count($table->getPrimaryKey())) {
         if ($table->hasAutoIncrementPrimaryKey()) {
             return 'UNIQUE (' . $this->getColumnListDDL($table->getPrimaryKey()) . ')';
         }
         return 'PRIMARY KEY (' . $this->getColumnListDDL($table->getPrimaryKey()) . ')';
     }
 }
Example #3
0
 protected function validateTableColumns(Table $table)
 {
     if (!$table->hasPrimaryKey() && !$table->isSkipSql()) {
         $this->errors[] = sprintf('Table "%s" does not have a primary key defined. Propel requires all tables to have a primary key.', $table->getName());
     }
     $phpNames = [];
     foreach ($table->getColumns() as $column) {
         if (in_array($column->getPhpName(), $phpNames)) {
             $this->errors[] = sprintf('Column "%s" declares a phpName already used in table "%s"', $column->getName(), $table->getName());
         }
         $phpNames[] = $column->getPhpName();
     }
 }
Example #4
0
 public function getAddTableDDL(Table $table)
 {
     $tableDescription = $table->hasDescription() ? $this->getCommentLineDDL($table->getDescription()) : '';
     $lines = array();
     foreach ($table->getColumns() as $column) {
         $lines[] = $this->getColumnDDL($column);
     }
     if ($table->hasPrimaryKey() && count($table->getPrimaryKey()) > 1) {
         $lines[] = $this->getPrimaryKeyDDL($table);
     }
     foreach ($table->getUnices() as $unique) {
         $lines[] = $this->getUniqueDDL($unique);
     }
     $sep = ",\n    ";
     $pattern = "\n%sCREATE TABLE %s\n(\n    %s\n);\n";
     return sprintf($pattern, $tableDescription, $this->quoteIdentifier($table->getName()), implode($sep, $lines));
 }
 /**
  * Builds the DDL SQL to drop the primary key of a table.
  *
  * @param  Table  $table
  * @return string
  */
 public function getDropPrimaryKeyDDL(Table $table)
 {
     if (!$table->hasPrimaryKey()) {
         return '';
     }
     $pattern = "\nALTER TABLE %s DROP PRIMARY KEY;\n";
     return sprintf($pattern, $this->quoteIdentifier($table->getName()));
 }
Example #6
0
 public function getAddTableDDL(Table $table)
 {
     $lines = array();
     foreach ($table->getColumns() as $column) {
         $lines[] = $this->getColumnDDL($column);
     }
     if ($table->hasPrimaryKey()) {
         $lines[] = $this->getPrimaryKeyDDL($table);
     }
     foreach ($table->getUnices() as $unique) {
         $lines[] = $this->getUniqueDDL($unique);
     }
     foreach ($table->getIndices() as $index) {
         $lines[] = $this->getIndexDDL($index);
     }
     foreach ($table->getForeignKeys() as $foreignKey) {
         if ($foreignKey->isSkipSql()) {
             continue;
         }
         $lines[] = str_replace("\n    ", "\n        ", $this->getForeignKeyDDL($foreignKey));
     }
     $vendorSpecific = $table->getVendorInfoForType('mysql');
     if ($vendorSpecific->hasParameter('Type')) {
         $mysqlTableType = $vendorSpecific->getParameter('Type');
     } elseif ($vendorSpecific->hasParameter('Engine')) {
         $mysqlTableType = $vendorSpecific->getParameter('Engine');
     } else {
         $mysqlTableType = $this->getDefaultTableEngine();
     }
     $tableOptions = $this->getTableOptions($table);
     if ($table->getDescription()) {
         $tableOptions[] = 'COMMENT=' . $this->quote($table->getDescription());
     }
     $tableOptions = $tableOptions ? ' ' . implode(' ', $tableOptions) : '';
     $sep = ",\n    ";
     $pattern = "\nCREATE TABLE %s\n(\n    %s\n) %s=%s%s;\n";
     return sprintf($pattern, $this->quoteIdentifier($table->getName()), implode($sep, $lines), $this->getTableEngineKeyword(), $mysqlTableType, $tableOptions);
 }
 public function testGetAutoIncrementPrimaryKey()
 {
     $column1 = $this->getColumnMock('id', array('primary' => true, 'auto_increment' => true));
     $column2 = $this->getColumnMock('title');
     $column3 = $this->getColumnMock('isbn');
     $table = new Table();
     $table->setIdMethod('native');
     $table->addColumn($column1);
     $table->addColumn($column2);
     $table->addColumn($column3);
     $this->assertCount(1, $table->getPrimaryKey());
     $this->assertTrue($table->hasPrimaryKey());
     $this->assertTrue($table->hasAutoIncrementPrimaryKey());
     $this->assertSame($column1, $table->getAutoIncrementPrimaryKey());
 }
 /**
  * Returns the DDL SQL to add the primary key of a table.
  *
  * @param  Table  $table From Table
  * @return string
  */
 public function getAddPrimaryKeyDDL(Table $table)
 {
     if (!$table->hasPrimaryKey()) {
         return '';
     }
     $pattern = "\nALTER TABLE %s ADD %s;\n";
     return sprintf($pattern, $this->quoteIdentifier($table->getName()), $this->getPrimaryKeyDDL($table));
 }
Example #9
0
 /**
  * Returns the SQL for the primary key of a Table object
  * @return     string
  */
 public function getPrimaryKeyDDL(Table $table)
 {
     if ($table->hasPrimaryKey()) {
         return 'PRIMARY KEY (' . $this->getColumnListDDL($table->getPrimaryKey()) . ')';
     }
 }