From fc6b2a5c41843234dba2f451d8f277b8f8d071bd Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Tue, 7 Nov 2023 13:26:05 +0300 Subject: [PATCH] Fix to a Detected Bug on Update --- webfiori/database/mssql/MSSQLQuery.php | 92 +++++++++++++------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/webfiori/database/mssql/MSSQLQuery.php b/webfiori/database/mssql/MSSQLQuery.php index d800a44..6697d1b 100644 --- a/webfiori/database/mssql/MSSQLQuery.php +++ b/webfiori/database/mssql/MSSQLQuery.php @@ -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(); @@ -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++; }