Skip to content

Commit

Permalink
Update AbstractQuery.php
Browse files Browse the repository at this point in the history
Do not throw an Exception if Column does not exist.
  • Loading branch information
usernane committed Oct 12, 2023
1 parent 650ffc9 commit 2cc481d
Showing 1 changed file with 35 additions and 72 deletions.
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 @@ public function drop() {
*
* @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 @@ public function offset($offset) {
*
* @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']
]);
$leftColObj = $table->getColByKey($leftCol);
}

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']
]);
$rightColObj = $table->getColByKey($rightCol);
}
$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 @@ public function where($col, $val = null, string $cond = '=', string $joinCond =
* @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 @@ public function whereBetween($col, $firstVal, $secondVal, $joinCond = 'and', $no
* @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 @@ public function whereIn($col, array $vals, $joinCond = 'and', $not = false) {
* @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 @@ public function whereLeft($col, $charsCount, $cond, $val, $joinCond = 'and') {
* @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 @@ public function whereLike($col, $val, $joinCond = 'and', $not = false) {
* @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 @@ public function whereNotBetween($col, $firstVal, $secVal, $joinCond = 'and') {
* @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 @@ public function whereNotIn($col, array $vals, $joinCond = 'and') {
* @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 @@ public function whereNotLike($col, $val, $joinCond = 'and') {
* @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 @@ public function whereNotNull($col, $join = 'and') {
* @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 @@ public function whereNull($col, $join = 'and', $not = false) {
* @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

0 comments on commit 2cc481d

Please sign in to comment.