Skip to content

Commit

Permalink
Fix to a Detected Bug on Update
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Nov 7, 2023
1 parent 6cf9886 commit fc6b2a5
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions webfiori/database/mssql/MSSQLQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,11 @@ public function update(array $newColsVals) {
foreach ($newColsVals as $colKey => $newVal) {
$colObj = $this->getTable()->getColByKey($colKey);

if (!$colObj instanceof MSSQLColumn) {
throw new DatabaseException("The table '$tblName' has no column with key '$colKey'.");
if ($colObj === null) {
$this->getTable()->addColumns([
$colKey => []

Check warning on line 259 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L258-L259

Added lines #L258 - L259 were not covered by tests
]);
$colObj = $this->getTable()->getColByKey($colKey);

Check warning on line 261 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L261

Added line #L261 was not covered by tests
}
$colName = $colObj->getName();

Expand Down Expand Up @@ -307,65 +310,62 @@ private function insertHelper(array $colsKeysArr, array $valuesToInsert) {
$column = $this->getTable()->getColByKey($colKey);
}

if ($column instanceof MSSQLColumn) {
$columnsWithVals[] = $colKey;
$colsNamesArr[] = $column->getName();
$type = $column->getDatatype();

if (isset($valuesToInsert[$colKey])) {
$val = $valuesToInsert[$colKey];
$columnsWithVals[] = $colKey;
$colsNamesArr[] = $column->getName();
$type = $column->getDatatype();

Check warning on line 316 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L314-L316

Added lines #L314 - L316 were not covered by tests

if (isset($valuesToInsert[$colKey])) {
$val = $valuesToInsert[$colKey];

Check warning on line 319 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L318-L319

Added lines #L318 - L319 were not covered by tests
} else {
if (isset($valuesToInsert[$valIndex])) {
$val = $valuesToInsert[$valIndex];

Check warning on line 322 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L321-L322

Added lines #L321 - L322 were not covered by tests
} else {
if (isset($valuesToInsert[$valIndex])) {
$val = $valuesToInsert[$valIndex];
} else {
$val = null;
}
$val = null;

Check warning on line 324 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L324

Added line #L324 was not covered by tests
}
}

if ($val !== null) {
$cleanedVal = $column->cleanValue($val);

if ($type == 'binary' || $type == 'varbinary') {
//chr(0) to remove null bytes in path.
$fixedPath = str_replace('\\', '/', str_replace(chr(0), '', $val));
set_error_handler(function (int $no, string $message)
{
throw new DatabaseException($message, $no);
});

if (strlen($fixedPath) != 0 && file_exists($fixedPath)) {
$file = fopen($fixedPath, 'r');
$data = '';

if ($file !== false) {
$fileContent = fread($file, filesize($fixedPath));

if ($fileContent !== false) {
$data = '0x'.bin2hex($fileContent);
$valsArr[] = $data;
} else {
$valsArr[] = 'null';
}
fclose($file);
} else {
$data = '0x'.bin2hex($val);
if ($val !== null) {
$cleanedVal = $column->cleanValue($val);

Check warning on line 329 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L328-L329

Added lines #L328 - L329 were not covered by tests

if ($type == 'binary' || $type == 'varbinary') {

Check warning on line 331 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L331

Added line #L331 was not covered by tests
//chr(0) to remove null bytes in path.
$fixedPath = str_replace('\\', '/', str_replace(chr(0), '', $val));
set_error_handler(function (int $no, string $message)

Check warning on line 334 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L333-L334

Added lines #L333 - L334 were not covered by tests
{
throw new DatabaseException($message, $no);
});

Check warning on line 337 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L336-L337

Added lines #L336 - L337 were not covered by tests

if (strlen($fixedPath) != 0 && file_exists($fixedPath)) {
$file = fopen($fixedPath, 'r');
$data = '';

Check warning on line 341 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L339-L341

Added lines #L339 - L341 were not covered by tests

if ($file !== false) {
$fileContent = fread($file, filesize($fixedPath));

Check warning on line 344 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L343-L344

Added lines #L343 - L344 were not covered by tests

if ($fileContent !== false) {
$data = '0x'.bin2hex($fileContent);

Check warning on line 347 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L346-L347

Added lines #L346 - L347 were not covered by tests
$valsArr[] = $data;
} else {
$valsArr[] = 'null';

Check warning on line 350 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L350

Added line #L350 was not covered by tests
}
fclose($file);

Check warning on line 352 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L352

Added line #L352 was not covered by tests
} else {
$data = '0x'.bin2hex($cleanedVal).'';
$data = '0x'.bin2hex($val);

Check warning on line 354 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L354

Added line #L354 was not covered by tests
$valsArr[] = $data;
}
restore_error_handler();
} else {
$valsArr[] = $cleanedVal;
$data = '0x'.bin2hex($cleanedVal).'';
$valsArr[] = $data;

Check warning on line 359 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L358-L359

Added lines #L358 - L359 were not covered by tests
}
restore_error_handler();

Check warning on line 361 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L361

Added line #L361 was not covered by tests
} else {
$valsArr[] = 'null';
$valsArr[] = $cleanedVal;

Check warning on line 363 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L363

Added line #L363 was not covered by tests
}
} else {
$tblName = $this->getTable()->getName();
throw new DatabaseException("The table '$tblName' has no column with key '$colKey'.");
$valsArr[] = 'null';

Check warning on line 366 in webfiori/database/mssql/MSSQLQuery.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mssql/MSSQLQuery.php#L366

Added line #L366 was not covered by tests
}

$valIndex++;
}

Expand Down

0 comments on commit fc6b2a5

Please sign in to comment.