Skip to content

Commit

Permalink
fix: fix database query builder alter dilimater
Browse files Browse the repository at this point in the history
  • Loading branch information
SonyPradana committed Oct 25, 2024
1 parent a295c79 commit 63e1cd5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
37 changes: 19 additions & 18 deletions src/System/Database/MySchema/Table/Alter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,11 @@ protected function builder(): string
{
$query = [];

// alter
$query = array_merge($query, $this->getModify());
// merge alter, add, drop, rename
$query = array_merge($query, $this->getModify(), $this->getColumns(), $this->getDrops(), $this->getRename());
$query = implode(', ', $query);

// add
$query = array_merge($query, $this->getColumns());

// drop
$drops = array_map(fn ($column) => "DROP COLUMN `{$column}`;", $this->drop_columns);
$query = array_merge($query, $drops);

// rename
$query = array_merge($query, $this->getRename());

$query = implode(' ', $query);

return "ALTER TABLE {$this->table_name} {$query}";
return "ALTER TABLE {$this->table_name} {$query};";
}

/** @return string[] */
Expand All @@ -87,7 +76,7 @@ private function getModify(): array
$res = [];

foreach ($this->alter_columns as $attribute) {
$res[] = "MODIFY COLUMN {$attribute->__toString()};";
$res[] = "MODIFY COLUMN {$attribute->__toString()}";
}

return $res;
Expand All @@ -99,7 +88,7 @@ private function getRename(): array
$res = [];

foreach ($this->rename_columns as $old => $new) {
$res[] = "; RENAME COLUMN `{$old}` TO `{$new}`;";
$res[] = "RENAME COLUMN `{$old}` TO `{$new}`";
}

return $res;
Expand All @@ -111,7 +100,19 @@ private function getColumns(): array
$res = [];

foreach ($this->add_columns as $attribute) {
$res[] = "ADD {$attribute->__toString()};";
$res[] = "ADD {$attribute->__toString()}";
}

return $res;
}

/** @return string[] */
private function getDrops(): array
{
$res = [];

foreach ($this->drop_columns as $drop) {
$res[] = "DROP COLUMN `{$drop}`";
}

return $res;
Expand Down
2 changes: 1 addition & 1 deletion tests/DataBase/MySchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function itCanUpdateDatabaseTable()
public function itCanExecuteUsingRawQuery()
{
$schema = new MySchema($this->pdo_schema);
$raw = $schema->raw('ALTER TABLE testing_db.users MODIFY COLUMN `user` varchar(20); ADD `status` int(3); DROP COLUMN `stat`');
$raw = $schema->raw('ALTER TABLE testing_db.users MODIFY COLUMN `user` varchar(20), ADD `status` int(3), DROP COLUMN `stat`');

$this->assertTrue($raw->execute());
}
Expand Down
19 changes: 9 additions & 10 deletions tests/DataBase/Query/Schema/Table/AlterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function itCanGenerateQueryUsingModifyColumn()
$schema('update_add')->int(17);

$this->assertEquals(
'ALTER TABLE testing_db.test MODIFY COLUMN `create_add` int(17); MODIFY COLUMN `update_add` int(17);',
'ALTER TABLE testing_db.test MODIFY COLUMN `create_add` int(17), MODIFY COLUMN `update_add` int(17);',
$schema->__toString()
);
}
Expand All @@ -29,7 +29,7 @@ public function itCanGenerateQueryUsingAddColumn()
$schema->add('LastName')->varchar(255);

$this->assertEquals(
'ALTER TABLE testing_db.test ADD `PersonID` int; ADD `LastName` varchar(255);',
'ALTER TABLE testing_db.test ADD `PersonID` int, ADD `LastName` varchar(255);',
$schema->__toString()
);
}
Expand All @@ -42,7 +42,7 @@ public function itCanGenerateQueryUsingDropColumn()
$schema->drop('LastName');

$this->assertEquals(
'ALTER TABLE testing_db.test DROP COLUMN `PersonID`; DROP COLUMN `LastName`;',
'ALTER TABLE testing_db.test DROP COLUMN `PersonID`, DROP COLUMN `LastName`;',
$schema->__toString()
);
}
Expand All @@ -54,9 +54,8 @@ public function itCanGenerateQueryUsingRenameColumn()
$schema->rename('PersonID', 'person_id');

$this->assertEquals(
'ALTER TABLE testing_db.test ; RENAME COLUMN `PersonID` TO `person_id`;',
$schema->__toString(),
'use start ; to prevent sql error'
'ALTER TABLE testing_db.test RENAME COLUMN `PersonID` TO `person_id`;',
$schema->__toString()
);
}

Expand All @@ -68,7 +67,7 @@ public function itCanGenerateQueryUsingRenameColumnMultyple()
$schema->rename('PersonID', 'person_id');

$this->assertEquals(
'ALTER TABLE testing_db.test ; RENAME COLUMN `PersonID` TO `person_id`;',
'ALTER TABLE testing_db.test RENAME COLUMN `PersonID` TO `person_id`;',
$schema->__toString(),
'multy rename column will use last rename'
);
Expand All @@ -83,7 +82,7 @@ public function itCanGenerateQueryUsingAltersColumn()
$schema->column('create_add')->int(17);

$this->assertEquals(
'ALTER TABLE testing_db.test MODIFY COLUMN `create_add` int(17); ADD `PersonID` int(4); DROP COLUMN `LastName`;',
'ALTER TABLE testing_db.test MODIFY COLUMN `create_add` int(17), ADD `PersonID` int(4), DROP COLUMN `LastName`;',
$schema->__toString()
);
}
Expand All @@ -96,7 +95,7 @@ public function itCanGenerateQueryUsingModifyColumnAndOrderit()
$schema->column('create_add')->after('id');

$this->assertEquals(
'ALTER TABLE testing_db.test MODIFY COLUMN `uuid` int(17) FIRST; MODIFY COLUMN `create_add` AFTER `id`;',
'ALTER TABLE testing_db.test MODIFY COLUMN `uuid` int(17) FIRST, MODIFY COLUMN `create_add` AFTER `id`;',
$schema->__toString()
);
}
Expand All @@ -109,7 +108,7 @@ public function itCanGenerateQueryUsingAddColumnAndOrderit()
$schema->add('create_add')->int(17)->after('id');

$this->assertEquals(
'ALTER TABLE testing_db.test ADD `uuid` int(17) FIRST; ADD `create_add` int(17) AFTER `id`;',
'ALTER TABLE testing_db.test ADD `uuid` int(17) FIRST, ADD `create_add` int(17) AFTER `id`;',
$schema->__toString()
);
}
Expand Down

0 comments on commit 63e1cd5

Please sign in to comment.