Skip to content

Commit

Permalink
Merge pull request #79 from WebFiori/bug-fix
Browse files Browse the repository at this point in the history
Fix to a Bug When Joining Two Tables
  • Loading branch information
usernane authored Oct 17, 2023
2 parents b237ef5 + 38fcaaa commit 53f3107
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
4 changes: 2 additions & 2 deletions webfiori/database/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ public function on(string $leftCol, string $rightCol, $cond = '=', $joinWith = '
$table->getLeft()->addColumns([
$leftCol => ['type' => 'varchar']
]);
$leftColObj = $table->getColByKey($leftCol);
$leftColObj = $table->getLeft()->getColByKey($leftCol);
}

$leftColObj->setWithTablePrefix(false);
Expand All @@ -649,7 +649,7 @@ public function on(string $leftCol, string $rightCol, $cond = '=', $joinWith = '
$table->getRight()->addColumns([
$rightCol => ['type' => 'varchar']
]);
$rightColObj = $table->getColByKey($rightCol);
$rightColObj = $table->getRight()->getColByKey($rightCol);
}
$rightColObj->setWithTablePrefix(false);
$rightColName = $rightColObj->getOwner()->getName().'.'.$rightColObj->getOldName();
Expand Down
69 changes: 66 additions & 3 deletions webfiori/database/JoinTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
*/
namespace webfiori\database;

use webfiori\database\mssql\MSSQLColumn;
use webfiori\database\mssql\MSSQLQuery;
use webfiori\database\mssql\MSSQLTable;
use webfiori\database\mysql\MySQLColumn;
use webfiori\database\mysql\MySQLQuery;
use webfiori\database\mysql\MySQLTable;
/**
Expand Down Expand Up @@ -99,6 +101,67 @@ public function addJoinCondition(Condition $cond, string $joinOp = 'and') {
$this->joinConditions = $newCond;
}
}
/**
* Adds multiple columns at once.
*
* @param array $colsArr An associative array. The keys will act as column
* key in the table. The value of the key can be an object of type 'MSSQLColumn' or 'MySQLColumn'
* or be an associative array of column options. The available options
* are:
* <ul>
* <li><b>name</b>: The name of the column in the database. If not provided,
* the name of the key will be used but with every '-' replaced by '_'.</li>
* <li><b>datatype</b>: The datatype of the column. If not provided, 'varchar'
* will be used. Note that the value 'type' can be used as an
* alias to this index.</li>
* <li><b>size</b>: Size of the column (if datatype does support size).
* If not provided, 1 will be used.</li>
* <li><b>default</b>: A default value for the column if its value
* is not present in case of insert.</li>
* <li><b>is-null</b>: A boolean. If the column allows null values, this should
* be set to true. Default is false.</li>
* <li><b>is-primary</b>: A boolean. It must be set to true if the column
* represents a primary key. Note that the column will be set as unique
* once its set as a primary.</li>
* <li><b>is-unique</b>: A boolean. If set to true, a unique index will
* be created for the column.</li>
* <li><b>auto-update</b>: A boolean. If the column datatype is
* 'datetime' or similar type and this parameter is set to true, the time of update will
* change automatically without having to change it manually.</li>
* <li><b>scale</b>: Number of numbers to the left of the decimal
* point. Only supported for decimal datatype.</li>
* </ul>
*
* @return Table The method will return the instance at which the method
* is called on.
*
*/
public function addColumns(array $colsArr) : Table {
$arrToAdd = [];

foreach ($colsArr as $key => $arrOrObj) {
if ($arrOrObj instanceof Column) {
$arrToAdd[$key] = $arrOrObj;
} else {
if (gettype($arrOrObj) == 'array') {
if (!isset($arrOrObj['name'])) {
$arrOrObj['name'] = str_replace('-', '_', $key);
}
if ($this->getLeft() instanceof MSSQLTable) {
$colObj = MSSQLColumn::createColObj($arrOrObj);
} else {
$colObj = MySQLColumn::createColObj($arrOrObj);
}

if ($colObj instanceof Column) {
$arrToAdd[$key] = $colObj;
}
}
}
}

return parent::addColumns($arrToAdd);
}
/**
* Returns a string which represents the join condition of the two tables.
*
Expand Down Expand Up @@ -214,10 +277,10 @@ private function addColsHelper(bool $left = true) {
}
}
private function copyCol(Column $column) {
if ($column instanceof mysql\MySQLColumn) {
$copyCol = new mysql\MySQLColumn($column->getName(), $column->getDatatype(), $column->getSize());
if ($column instanceof MySQLColumn) {
$copyCol = new MySQLColumn($column->getName(), $column->getDatatype(), $column->getSize());
} else {
$copyCol = new mssql\MSSQLColumn($column->getName(), $column->getDatatype(), $column->getSize());
$copyCol = new MSSQLColumn($column->getName(), $column->getDatatype(), $column->getSize());
}
$copyCol->setOwner($column->getOwner());
$copyCol->setCustomFilter($column->getCustomCleaner());
Expand Down

0 comments on commit 53f3107

Please sign in to comment.