public function getSql(Column $column, $table)
 {
     if (!$table instanceof Table) {
         $table = new Identifier($table);
     }
     $sql = array();
     $normalized = $column->getType()->getNormalizedPostGISColumnOptions($column->getCustomSchemaOptions());
     $srid = $normalized['srid'];
     // PostGIS 1.5 uses -1 for undefined SRID's
     if ($srid <= 0) {
         $srid = -1;
     }
     $type = strtoupper($normalized['geometry_type']);
     if ('ZM' === substr($type, -2)) {
         $dimension = 4;
         $type = substr($type, 0, -2);
     } elseif ('M' === substr($type, -1)) {
         $dimension = 3;
     } elseif ('Z' === substr($type, -1)) {
         $dimension = 3;
         $type = substr($type, 0, -1);
     } else {
         $dimension = 2;
     }
     // Geometry columns are created by the AddGeometryColumn stored procedure
     $sql[] = sprintf("SELECT AddGeometryColumn('%s', '%s', %d, '%s', %d)", $table->getName(), $column->getName(), $srid, $type, $dimension);
     if ($column->getNotnull()) {
         // Add a NOT NULL constraint to the field
         $sql[] = sprintf('ALTER TABLE %s ALTER %s SET NOT NULL', $table->getQuotedName($this->platform), $column->getQuotedName($this->platform));
     }
     return $sql;
 }
示例#2
0
 /**
  * Creates a column replacement, which has a quoted name.
  *
  * @param Column $column
  *
  * @return Column
  */
 private function createColumnReplacement(Column $column)
 {
     $columnConfig = $column->toArray();
     $columnConfig['platformOptions'] = $column->getPlatformOptions();
     $columnConfig['customSchemaOptions'] = $column->getCustomSchemaOptions();
     return new Column($this->platform->quoteIdentifier($column->getName()), $column->getType(), $columnConfig);
 }
示例#3
0
 public function diffColumn(Column $column1, Column $column2)
 {
     $changedProperties = array();
     if ($column1->getType() != $column2->getType()) {
         //espo: fix problem with executing query for custom types
         $column1DbTypeName = method_exists($column1->getType(), 'getDbTypeName') ? $column1->getType()->getDbTypeName() : $column1->getType()->getName();
         $column2DbTypeName = method_exists($column2->getType(), 'getDbTypeName') ? $column2->getType()->getDbTypeName() : $column2->getType()->getName();
         if (strtolower($column1DbTypeName) != strtolower($column2DbTypeName)) {
             $changedProperties[] = 'type';
         }
         //END: espo
     }
     if ($column1->getNotnull() != $column2->getNotnull()) {
         $changedProperties[] = 'notnull';
     }
     if ($column1->getDefault() != $column2->getDefault()) {
         $changedProperties[] = 'default';
     }
     if ($column1->getUnsigned() != $column2->getUnsigned()) {
         $changedProperties[] = 'unsigned';
     }
     if ($column1->getType() instanceof \Doctrine\DBAL\Types\StringType) {
         // check if value of length is set at all, default value assumed otherwise.
         $length1 = $column1->getLength() ?: 255;
         $length2 = $column2->getLength() ?: 255;
         if ($length1 != $length2) {
             $changedProperties[] = 'length';
         }
         if ($column1->getFixed() != $column2->getFixed()) {
             $changedProperties[] = 'fixed';
         }
     }
     if ($column1->getType() instanceof \Doctrine\DBAL\Types\DecimalType) {
         if (($column1->getPrecision() ?: 10) != ($column2->getPrecision() ?: 10)) {
             $changedProperties[] = 'precision';
         }
         if ($column1->getScale() != $column2->getScale()) {
             $changedProperties[] = 'scale';
         }
     }
     if ($column1->getAutoincrement() != $column2->getAutoincrement()) {
         $changedProperties[] = 'autoincrement';
     }
     // only allow to delete comment if its set to '' not to null.
     if ($column1->getComment() !== null && $column1->getComment() != $column2->getComment()) {
         $changedProperties[] = 'comment';
     }
     $options1 = $column1->getCustomSchemaOptions();
     $options2 = $column2->getCustomSchemaOptions();
     $commonKeys = array_keys(array_intersect_key($options1, $options2));
     foreach ($commonKeys as $key) {
         if ($options1[$key] !== $options2[$key]) {
             $changedProperties[] = $key;
         }
     }
     $diffKeys = array_keys(array_diff_key($options1, $options2) + array_diff_key($options2, $options1));
     $changedProperties = array_merge($changedProperties, $diffKeys);
     return $changedProperties;
 }
示例#4
0
 /**
  * Returns the difference between the fields $field1 and $field2.
  *
  * If there are differences this method returns $field2, otherwise the
  * boolean false.
  *
  * @param \Doctrine\DBAL\Schema\Column $column1
  * @param \Doctrine\DBAL\Schema\Column $column2
  *
  * @return array
  */
 public function diffColumn(Column $column1, Column $column2)
 {
     $properties1 = $column1->toArray();
     $properties2 = $column2->toArray();
     $changedProperties = array();
     foreach (array('type', 'notnull', 'unsigned', 'autoincrement') as $property) {
         if ($properties1[$property] != $properties2[$property]) {
             $changedProperties[] = $property;
         }
     }
     if ($properties1['default'] != $properties2['default'] || null === $properties1['default'] && null !== $properties2['default'] || null === $properties2['default'] && null !== $properties1['default']) {
         $changedProperties[] = 'default';
     }
     if ($properties1['type'] instanceof Types\StringType && !$properties1['type'] instanceof Types\GuidType || $properties1['type'] instanceof Types\BinaryType) {
         // check if value of length is set at all, default value assumed otherwise.
         $length1 = $properties1['length'] ?: 255;
         $length2 = $properties2['length'] ?: 255;
         if ($length1 != $length2) {
             $changedProperties[] = 'length';
         }
         if ($properties1['fixed'] != $properties2['fixed']) {
             $changedProperties[] = 'fixed';
         }
     } elseif ($properties1['type'] instanceof Types\DecimalType) {
         if (($properties1['precision'] ?: 10) != ($properties2['precision'] ?: 10)) {
             $changedProperties[] = 'precision';
         }
         if ($properties1['scale'] != $properties2['scale']) {
             $changedProperties[] = 'scale';
         }
     }
     // A null value and an empty string are actually equal for a comment so they should not trigger a change.
     if ($properties1['comment'] !== $properties2['comment'] && !(null === $properties1['comment'] && '' === $properties2['comment']) && !(null === $properties2['comment'] && '' === $properties1['comment'])) {
         $changedProperties[] = 'comment';
     }
     $customOptions1 = $column1->getCustomSchemaOptions();
     $customOptions2 = $column2->getCustomSchemaOptions();
     // Remove option-as-object
     foreach ($customOptions2 as $k => $v) {
         if (is_object($v)) {
             unset($customOptions2[$k]);
         }
     }
     foreach (array_merge(array_keys($customOptions1), array_keys($customOptions2)) as $key) {
         if (!array_key_exists($key, $properties1) || !array_key_exists($key, $properties2)) {
             $changedProperties[] = $key;
         } elseif ($properties1[$key] !== $properties2[$key]) {
             $changedProperties[] = $key;
         }
     }
     $platformOptions1 = $column1->getPlatformOptions();
     $platformOptions2 = $column2->getPlatformOptions();
     foreach (array_keys(array_intersect_key($platformOptions1, $platformOptions2)) as $key) {
         if ($properties1[$key] !== $properties2[$key]) {
             $changedProperties[] = $key;
         }
     }
     return array_unique($changedProperties);
 }
 /**
  * Returns the difference between the fields $field1 and $field2.
  *
  * If there are differences this method returns $field2, otherwise the
  * boolean false.
  *
  * @param \Doctrine\DBAL\Schema\Column $column1
  * @param \Doctrine\DBAL\Schema\Column $column2
  *
  * @return array
  */
 public function diffColumn(Column $column1, Column $column2)
 {
     $properties1 = $column1->toArray();
     $properties2 = $column2->toArray();
     $changedProperties = array();
     foreach (array('type', 'notnull', 'unsigned', 'autoincrement') as $property) {
         if ($properties1[$property] != $properties2[$property]) {
             $changedProperties[] = $property;
         }
     }
     if ($properties1['default'] != $properties2['default'] || null === $properties1['default'] && null !== $properties2['default'] || null === $properties2['default'] && null !== $properties1['default']) {
         $changedProperties[] = 'default';
     }
     if ($properties1['type'] instanceof Types\StringType || $properties1['type'] instanceof Types\BinaryType) {
         // check if value of length is set at all, default value assumed otherwise.
         $length1 = $properties1['length'] ?: 255;
         $length2 = $properties2['length'] ?: 255;
         if ($length1 != $length2) {
             $changedProperties[] = 'length';
         }
         if ($properties1['fixed'] != $properties2['fixed']) {
             $changedProperties[] = 'fixed';
         }
     } elseif ($properties1['type'] instanceof Types\DecimalType) {
         if (($properties1['precision'] ?: 10) != ($properties2['precision'] ?: 10)) {
             $changedProperties[] = 'precision';
         }
         if ($properties1['scale'] != $properties2['scale']) {
             $changedProperties[] = 'scale';
         }
     }
     // only allow to delete comment if its set to '' not to null.
     if ($properties1['comment'] !== null && $properties1['comment'] != $properties2['comment']) {
         $changedProperties[] = 'comment';
     }
     $customOptions1 = $column1->getCustomSchemaOptions();
     $customOptions2 = $column2->getCustomSchemaOptions();
     foreach (array_merge(array_keys($customOptions1), array_keys($customOptions2)) as $key) {
         if (!array_key_exists($key, $properties1) || !array_key_exists($key, $properties2)) {
             $changedProperties[] = $key;
         } elseif ($properties1[$key] !== $properties2[$key]) {
             $changedProperties[] = $key;
         }
     }
     $platformOptions1 = $column1->getPlatformOptions();
     $platformOptions2 = $column2->getPlatformOptions();
     foreach (array_keys(array_intersect_key($platformOptions1, $platformOptions2)) as $key) {
         if ($properties1[$key] !== $properties2[$key]) {
             $changedProperties[] = $key;
         }
     }
     return array_unique($changedProperties);
 }