Skip to content

Commit

Permalink
Refacoring column sql builder
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Apr 15, 2015
1 parent c8e90f6 commit 32005be
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/SQLBuilder/Universal/Syntax/AlterTableChangeColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function toSql(BaseDriver $driver, ArgumentArray $args)
}

// the 'toColumn' must be a type of Column, we need at least column type to rename.
$sql .= ' ' . $driver->quoteIdentifier($this->toColumn->getName()) . ' ' . $this->toColumn->getType();
$sql .= ' ' . $driver->quoteIdentifier($this->toColumn->getName()) . ' ' . $this->toColumn->buildDefinitionSqlForModify($driver, $args);

if ($driver instanceof MySQLDriver) {
if ($this->after) {
Expand Down
2 changes: 1 addition & 1 deletion src/SQLBuilder/Universal/Syntax/AlterTableModifyColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function toSql(BaseDriver $driver, ArgumentArray $args)
if (!$this->column->getType()) {
throw new IncompleteSettingsException('Missing column type');
}
$sql .= $this->column->buildDefinitionSql($driver, $args);
$sql .= $this->column->buildDefinitionSqlForModify($driver, $args);

if ($this->after) {
$sql .= ' AFTER ' . $driver->quoteIdentifier($this->after);
Expand Down
47 changes: 36 additions & 11 deletions src/SQLBuilder/Universal/Syntax/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ public function __construct($name = NULL, $type = NULL, array $extraAttributes =

public function type($type)
{
if ($type == 'integer') {
$type = 'int';
}
$this->type = $type;
return $this;
}
Expand Down Expand Up @@ -213,7 +216,7 @@ public function mediumInt($length = NULL)

public function int($length = NULL)
{
$this->type = 'integer';
$this->type = 'int';
$this->isa = 'int';
if ($length) {
$this->length = $length;
Expand Down Expand Up @@ -446,8 +449,11 @@ public function set($a)
public function autoIncrement()
{
$this->autoIncrement = true;
$this->type = 'integer';
$this->isa = 'int';
if (!$this->type) {
$this->type = 'int';
$this->unsigned = TRUE;
}
return $this;
}

Expand Down Expand Up @@ -769,7 +775,17 @@ public function buildTypeClause(BaseDriver $driver)
return ' SERIAL';
}
}
return ' ' . $this->buildTypeName();

$sql = ' ' . $this->buildTypeName();

if ($driver instanceof MySQLDriver) {
if ($this->isa === 'enum' && !empty($this->enum)) {
$sql .= $this->buildEnumClause($driver);
} elseif ($this->isa === 'set' && !empty($this->set)) {
$sql .= $this->buildSetClause($driver);
}
}
return $sql;
}

public function buildPgSQLDefinitionSql(BaseDriver $driver, ArgumentArray $args)
Expand All @@ -786,23 +802,32 @@ public function buildPgSQLDefinitionSql(BaseDriver $driver, ArgumentArray $args)
return $sql;
}

public function buildDefinitionSql(BaseDriver $driver, ArgumentArray $args)
public function buildDefinitionSqlForModify(BaseDriver $driver, ArgumentArray $args)
{
$isa = $this->isa ?: 'str';

$sql = '';
$sql .= $driver->quoteIdentifier($this->name);

$sql .= $this->buildTypeClause($driver);

if ($isa === 'enum' && !empty($this->enum)) {
$sql .= $this->buildEnumClause($driver);
} elseif ($isa === 'set' && !empty($this->set)) {
$sql .= $this->buildSetClause($driver);
$sql .= $this->buildUnsignedClause($driver);
$sql .= $this->buildNullClause($driver);
$sql .= $this->buildDefaultClause($driver);
$sql .= $this->buildAutoIncrementClause($driver);
if ($this->comment) {
$sql .= ' COMMENT ' . $driver->deflate($this->comment);
}
return $sql;
}

$sql .= $this->buildUnsignedClause($driver);
public function buildDefinitionSql(BaseDriver $driver, ArgumentArray $args)
{
$isa = $this->isa ?: 'str';

$sql = '';
$sql .= $driver->quoteIdentifier($this->name);

$sql .= $this->buildTypeClause($driver);
$sql .= $this->buildUnsignedClause($driver);
$sql .= $this->buildNullClause($driver);
$sql .= $this->buildDefaultClause($driver);
$sql .= $this->buildPrimaryKeyClause($driver);
Expand Down

0 comments on commit 32005be

Please sign in to comment.