Skip to content

Commit

Permalink
Merge pull request #82 from WebFiori/dev
Browse files Browse the repository at this point in the history
Fix to a Detected Bug on Update
  • Loading branch information
usernane authored Nov 7, 2023
2 parents 47d8eae + fc6b2a5 commit 839c360
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 => []
]);
$colObj = $this->getTable()->getColByKey($colKey);
}
$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();

if (isset($valuesToInsert[$colKey])) {
$val = $valuesToInsert[$colKey];
} else {
if (isset($valuesToInsert[$valIndex])) {
$val = $valuesToInsert[$valIndex];
} else {
if (isset($valuesToInsert[$valIndex])) {
$val = $valuesToInsert[$valIndex];
} else {
$val = null;
}
$val = null;
}
}

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);

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($cleanedVal).'';
$data = '0x'.bin2hex($val);
$valsArr[] = $data;
}
restore_error_handler();
} else {
$valsArr[] = $cleanedVal;
$data = '0x'.bin2hex($cleanedVal).'';
$valsArr[] = $data;
}
restore_error_handler();
} else {
$valsArr[] = 'null';
$valsArr[] = $cleanedVal;
}
} else {
$tblName = $this->getTable()->getName();
throw new DatabaseException("The table '$tblName' has no column with key '$colKey'.");
$valsArr[] = 'null';
}

$valIndex++;
}

Expand Down

0 comments on commit 839c360

Please sign in to comment.