Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] No Longer Need to Throw Exception if Column Does not Exist #78

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,8 @@ public function testDropCol00() {
*/
public function testDropCol01() {
$schema = new MSSQLTestSchema();
$this->expectException(DatabaseException::class);
$this->expectExceptionMessage('The table [users] has no column with key \'not-exist\'.');
$schema->table('users')->dropCol('not-exist');
$this->assertEquals('alter table [users] drop column [not_exist];', $schema->getLastQuery());
}
/**
* @test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1276,9 +1276,8 @@ public function testDropCol00() {
*/
public function testDropCol01() {
$schema = new MySQLTestSchema();
$this->expectException(DatabaseException::class);
$this->expectExceptionMessage('The table `users` has no column with key \'not-exist\'.');
$schema->table('users')->dropCol('not-exist');
$this->assertEquals('alter table `users` drop column `not_exist`;', $schema->getLastQuery());
}
/**
* @test
Expand Down
107 changes: 35 additions & 72 deletions webfiori/database/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,17 @@
*
* @return MSSQLQuery|MySQLQuery The method will return the same instance at which the
* method is called on.
*
* @throws DatabaseException If no column which has the given key, the method
* will throw an exception.
*
* @since 1.0
*/
public function dropCol($colKey) {
$tblName = $this->getTable()->getName();
$colObj = $this->getTable()->getColByKey($colKey);
$table = $this->getTable();
$tblName = $table->getName();
$colObj = $table->getColByKey($colKey);

if (!($colObj instanceof Column)) {
throw new DatabaseException("The table $tblName has no column with key '$colKey'.");
if ($colObj === null) {
$table->addColumns([
$colKey => ['type' => 'varchar']
]);
$colObj = $table->getColByKey($colKey);
}
$withTick = $colObj->getName();
$stm = "alter table $tblName drop column $withTick;";
Expand Down Expand Up @@ -624,39 +623,39 @@
*
* @since 1.0
*/
public function on($leftCol, $rightCol, $cond = '=', $joinWith = 'and') {
public function on(string $leftCol, string $rightCol, $cond = '=', $joinWith = 'and') {
$table = $this->getTable();

if ($table instanceof JoinTable) {
$leftCol = $table->getLeft()->getColByKey($leftCol);
$leftColObj = $table->getLeft()->getColByKey($leftCol);

if ($leftColObj === null) {
$table->getLeft()->addColumns([
$leftCol => ['type' => 'varchar']

Check warning on line 634 in webfiori/database/AbstractQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/AbstractQuery.php#L633-L634

Added lines #L633 - L634 were not covered by tests
]);
$leftColObj = $table->getColByKey($leftCol);

Check warning on line 636 in webfiori/database/AbstractQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/AbstractQuery.php#L636

Added line #L636 was not covered by tests
}

if ($leftCol instanceof Column) {
$leftCol->setWithTablePrefix(false);
$leftColObj->setWithTablePrefix(false);

if ($leftCol->getOwner() instanceof JoinTable && $leftCol->getAlias() !== null) {
$leftCol->setName($leftCol->getAlias());
}
$leftColName = $leftCol->getOwner()->getName().'.'.$leftCol->getOldName();

$rightCol = $table->getRight()->getColByKey($rightCol);

if ($rightCol instanceof Column) {
$rightCol->setWithTablePrefix(false);
$rightColName = $rightCol->getOwner()->getName().'.'.$rightCol->getOldName();
$cond = new Condition($leftColName, $rightColName, $cond);
$table->addJoinCondition($cond, $joinWith);
} else {
$tableName = $table->getName();
$colsKeys = $table->getColsKeys();
$message = "The table '$tableName' has no column with key '$rightCol'. Available columns: ".implode(',', $colsKeys);
throw new DatabaseException($message);
}
} else {
$tableName = $table->getName();
$colsKeys = $table->getColsKeys();
$message = "The table '$tableName' has no column with key '$leftCol'. Available columns: ".implode(',', $colsKeys);
throw new DatabaseException($message);
if ($leftColObj->getOwner() instanceof JoinTable && $leftColObj->getAlias() !== null) {
$leftColObj->setName($leftColObj->getAlias());
}
$leftColName = $leftColObj->getOwner()->getName().'.'.$leftColObj->getOldName();

$rightColObj = $table->getRight()->getColByKey($rightCol);

if ($rightColObj === null) {
$table->getRight()->addColumns([
$rightCol => ['type' => 'varchar']

Check warning on line 650 in webfiori/database/AbstractQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/AbstractQuery.php#L649-L650

Added lines #L649 - L650 were not covered by tests
]);
$rightColObj = $table->getColByKey($rightCol);

Check warning on line 652 in webfiori/database/AbstractQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/AbstractQuery.php#L652

Added line #L652 was not covered by tests
}
$rightColObj->setWithTablePrefix(false);
$rightColName = $rightColObj->getOwner()->getName().'.'.$rightColObj->getOldName();
$cond = new Condition($leftColName, $rightColName, $cond);
$table->addJoinCondition($cond, $joinWith);

} else {
throw new DatabaseException("The 'on' condition can be only used with join tables.");
}
Expand Down Expand Up @@ -1149,8 +1148,6 @@
* @return AbstractQuery|MySQLQuery The method will return the same instance at which the
* method is called on.
*
* @throws DatabaseException If the table has no column with given key name,
* the method will throw an exception.
*
* @since 1.0.3
*/
Expand Down Expand Up @@ -1184,10 +1181,6 @@
* @return AbstractQuery|MySQLQuery The method will return the same instance at which the
* method is called on.
*
* @throws DatabaseException If the table has no column with given key name,
* the method will throw an exception.
*
* @since 1.0.3
*/
public function whereIn($col, array $vals, $joinCond = 'and', $not = false) {
$this->addWhereHelper([
Expand Down Expand Up @@ -1224,10 +1217,6 @@
* @return AbstractQuery|MySQLQuery The method will return the same instance at which the
* method is called on.
*
* @throws DatabaseException If the table has no column with given key name,
* the method will throw an exception.
*
* @since 1.0.4
*/
public function whereLeft($col, $charsCount, $cond, $val, $joinCond = 'and') {
$this->addWhereHelper([
Expand Down Expand Up @@ -1261,10 +1250,6 @@
* @return AbstractQuery|MySQLQuery The method will return the same instance at which the
* method is called on.
*
* @throws DatabaseException If the table has no column with given key name,
* the method will throw an exception.
*
* @since 1.0.4
*/
public function whereLike($col, $val, $joinCond = 'and', $not = false) {
$this->addWhereHelper([
Expand Down Expand Up @@ -1294,10 +1279,6 @@
* @return AbstractQuery|MySQLQuery The method will return the same instance at which the
* method is called on.
*
* @throws DatabaseException If the table has no column with given key name,
* the method will throw an exception.
*
* @since 1.0.3
*/
public function whereNotBetween($col, $firstVal, $secVal, $joinCond = 'and') {
return $this->whereBetween($col, $firstVal, $secVal, $joinCond, true);
Expand All @@ -1317,10 +1298,6 @@
* @return AbstractQuery|MySQLQuery The method will return the same instance at which the
* method is called on.
*
* @throws DatabaseException If the table has no column with given key name,
* the method will throw an exception.
*
* @since 1.0.3
*/
public function whereNotIn($col, array $vals, $joinCond = 'and') {
return $this->whereIn($col, $vals, $joinCond, true);
Expand All @@ -1342,8 +1319,6 @@
* @return AbstractQuery|MySQLQuery The method will return the same instance at which the
* method is called on.
*
* @throws DatabaseException If the table has no column with given key name,
* the method will throw an exception.
*/
public function whereNotLike($col, $val, $joinCond = 'and') {
return $this->whereLike($col, $val, $joinCond, true);
Expand All @@ -1361,10 +1336,6 @@
* @return AbstractQuery|MySQLQuery The method will return the same instance at which the
* method is called on.
*
* @throws DatabaseException If the table has no column with given key name,
* the method will throw an exception.
*
* @since 1.0.4
*/
public function whereNotNull($col, $join = 'and') {
return $this->whereNull($col, $join, true);
Expand All @@ -1385,10 +1356,6 @@
* @return AbstractQuery|MySQLQuery The method will return the same instance at which the
* method is called on.
*
* @throws DatabaseException If the table has no column with given key name,
* the method will throw an exception.
*
* @since 1.0.4
*/
public function whereNull($col, $join = 'and', $not = false) {
$this->addWhereHelper([
Expand Down Expand Up @@ -1423,10 +1390,6 @@
* @return AbstractQuery|MySQLQuery The method will return the same instance at which the
* method is called on.
*
* @throws DatabaseException If the table has no column with given key name,
* the method will throw an exception.
*
* @since 1.0.4
*/
public function whereRight($col, $charsCount, $cond, $val, $joinCond = 'and') {
$this->addWhereHelper([
Expand Down
Loading