Skip to content

Commit

Permalink
Merge pull request #71 from WebFiori/dev
Browse files Browse the repository at this point in the history
Fix to a Bug for Bool Type in MSSQL
  • Loading branch information
usernane authored Jul 25, 2023
2 parents 264519d + dcf2d93 commit b062d56
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 24 deletions.
8 changes: 6 additions & 2 deletions webfiori/database/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
* @since 1.0.1
*/
abstract class Column {
/**
* An array that holds names of values which represents boolean type.
*/
const BOOL_TYPES = ['bool', 'boolean'];
/**
*
* @var string|null
Expand Down Expand Up @@ -512,7 +516,7 @@ public function setDatatype(string $type) {
throw new DatabaseException('Column datatype not supported: \''.$trimmed.'\'.');
}

if ($trimmed == 'bool' || $trimmed == 'boolean') {
if (in_array($trimmed, Column::BOOL_TYPES)) {
$this->setIsNull(false);
}

Expand Down Expand Up @@ -548,7 +552,7 @@ public function setDefault($defaultVal) {
public function setIsNull(bool $bool) : bool {
$colDatatype = $this->getDatatype();

if (!($colDatatype == 'bool' || $colDatatype == 'boolean') && !$this->isPrimary()) {
if (!(in_array($colDatatype, Column::BOOL_TYPES)) && !$this->isPrimary()) {
$this->isNull = $bool === true;

return true;
Expand Down
2 changes: 1 addition & 1 deletion webfiori/database/EntityMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ private function appendSetter($attrName, $colName, $phpType, $setterName, $colDa
." **/\n"
.' public function '.$setterName.'($'.$attrName.") {\n";

if ($colDatatype == 'boolean' || $colDatatype == 'bool') {
if (in_array($colDatatype, Column::BOOL_TYPES)) {
$this->classStr .= ' $this->'.$attrName.' = $'.$attrName." === true || $".$attrName." == 'Y' || $".$attrName." == 1;\n";
} else {
$this->classStr .= ' $this->'.$attrName.' = $'.$attrName.";\n";
Expand Down
18 changes: 10 additions & 8 deletions webfiori/database/mssql/MSSQLColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use webfiori\database\Column;
use webfiori\database\ColumnFactory;
use webfiori\database\DatabaseException;
use webfiori\database\DateTimeValidator;
/**
* A class that represents a column in MSSQL table.
Expand Down Expand Up @@ -295,7 +296,7 @@ public function getDefault() {
}
} else if ($dt == 'int' || $dt == 'bigint') {
$retVal = intval($defaultVal);
} else if ($dt == 'boolean') {
} else if (in_array($dt, Column::BOOL_TYPES)) {
return $defaultVal === 1 || $defaultVal === true;
} else if ($dt == 'float' || $dt == 'decimal' || $dt == 'money') {
$retVal = floatval($defaultVal);
Expand Down Expand Up @@ -337,7 +338,7 @@ public function getName() : string {
public function getPHPType() : string {
$colType = $this->getDatatype();

if ($colType == 'boolean') {
if (in_array($colType, Column::BOOL_TYPES)) {
$isNullStr = '';
} else {
$isNullStr = $this->isNull() ? '|null' : '';
Expand All @@ -347,7 +348,7 @@ public function getPHPType() : string {
return 'int'.$isNullStr;
} else if ($colType == 'decimal' || $colType == 'float' || $colType == 'money') {
return 'float'.$isNullStr;
} else if ($colType == 'boolean' || $colType == 'bool') {
} else if (in_array($colType, Column::BOOL_TYPES)) {
return 'bool'.$isNullStr;
} else if ($colType == 'varchar' || $colType == 'nvarchar'
|| $colType == 'datetime2' || $colType == 'date'
Expand Down Expand Up @@ -408,6 +409,7 @@ public function setAutoUpdate(bool $bool) {
* column type is not supported.
*/
public function setDatatype(string $type) {

parent::setDatatype($type);

if (!($this->getDatatype() == 'int' || $this->getDatatype() == 'bigint')) {
Expand Down Expand Up @@ -500,7 +502,7 @@ private function cleanValueHelper($val) {
return null;
} else if ($colDatatype == 'int' || $colDatatype == 'bigint') {
$cleanedVal = intval($val);
} else if ($colDatatype == 'boolean') {
} else if (in_array($colDatatype, Column::BOOL_TYPES)) {
if ($val === true) {
$cleanedVal = 1;
} else {
Expand Down Expand Up @@ -528,7 +530,7 @@ private function cleanValueHelper($val) {
$cleanedVal = filter_var(addslashes($val));
} else if ($valType == 'double') {
$cleanedVal = "'".floatval($val)."'";
} else if ($valType == 'boolean') {
} else if (in_array($colDatatype, Column::BOOL_TYPES)) {
if ($val === true) {
$cleanedVal = 1;
} else {
Expand Down Expand Up @@ -572,7 +574,7 @@ private function defaultPartString() {
$colDefault = $this->getDefault();

if ($colDefault !== null) {
if ($colDataType == 'boolean') {
if (in_array($colDataType, Column::BOOL_TYPES)) {
if ($this->getDefault() === true) {
return 'default 1 ';
} else {
Expand Down Expand Up @@ -602,7 +604,7 @@ private function firstColPartString() {
|| $colDataType == 'char' || $colDataType == 'nchar'
|| $colDataType == 'binary' || $colDataType == 'varbinary') {
$retVal .= $colDataTypeSq.'('.$this->getSize().') ';
} else if ($colDataType == 'boolean') {
} else if (in_array($colDataType, Column::BOOL_TYPES)) {
$retVal .= '[bit] ';
} else if ($colDataType == 'decimal') {
if ($this->getSize() != 0) {
Expand All @@ -626,7 +628,7 @@ private function firstColPartString() {
private function nullPartString() {
$colDataType = $this->getDatatype();

if (!$this->isNull() || $colDataType == 'boolean') {
if (!$this->isNull() || in_array($colDataType, Column::BOOL_TYPES)) {
return 'not null ';
} else {
return 'null ';
Expand Down
4 changes: 2 additions & 2 deletions webfiori/database/mssql/MSSQLQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function modifyCol($colKey, $location = null) {
$tblName = $this->getTable()->getName();
$colObj = $this->getTable()->getColByKey($colKey);

if (!($colObj instanceof MySQLColumn)) {
if (!($colObj instanceof MSSQLColumn)) {
throw new DatabaseException("The table '$tblName' has no column with key '$colKey'.");
}

Expand Down Expand Up @@ -416,7 +416,7 @@ private function insertHelper(array $colsKeysArr, array $valuesToInsert) {
$colsNamesArr[] = $colObj->getName();
$type = $colObj->getDatatype();

if ($type == 'boolean' || $type == 'bool') {
if (in_array($type, Column::BOOL_TYPES)) {
$valsArr[] = $colObj->cleanValue($defaultVal);
} else if ($defaultVal == 'now' || $defaultVal == 'current_timestamp' || $defaultVal == 'now()') {
if ($type == 'datetime2') {
Expand Down
20 changes: 10 additions & 10 deletions webfiori/database/mysql/MySQLColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public function getDefault() {
}
} else if ($dt == 'int') {
$retVal = intval($defaultVal);
} else if ($dt == 'boolean' || $dt == 'bool') {
} else if (in_array($dt, Column::BOOL_TYPES)) {
return $defaultVal === "b'1'" || $defaultVal === true;
} else if ($dt == 'mixed') {
$retVal = substr($defaultVal, 1, strlen($defaultVal) - 2);
Expand Down Expand Up @@ -326,7 +326,7 @@ public function getName() : string {
public function getPHPType() : string {
$colType = $this->getDatatype();

if ($colType == 'bool' || $colType == 'boolean') {
if (in_array($colType, Column::BOOL_TYPES)) {
$isNullStr = '';
} else {
$isNullStr = $this->isNull() ? '|null' : '';
Expand All @@ -336,7 +336,7 @@ public function getPHPType() : string {
return 'int'.$isNullStr;
} else if ($colType == 'decimal' || $colType == 'double' || $colType == 'float') {
return 'float'.$isNullStr;
} else if ($colType == 'boolean' || $colType == 'bool') {
} else if (in_array($colType, Column::BOOL_TYPES)) {
return 'bool'.$isNullStr;
} else if ($colType == 'varchar' || $colType == 'datetime'
|| $colType == 'timestamp' || $colType == 'blob'
Expand Down Expand Up @@ -450,7 +450,7 @@ public function setDefault($default) {
* @since 1.0
*/
public function setIsAutoInc(bool $bool) {
if ($this->isPrimary() && gettype($bool) == 'boolean' && $this->getDatatype() == 'int') {
if ($this->isPrimary() && $this->getDatatype() == 'int') {
$this->isAutoInc = $bool;

return true;
Expand Down Expand Up @@ -584,7 +584,7 @@ public function setSize(int $size) : bool {
$type = $this->getDatatype();
$retVal = false;

if ($type == 'boolean' || $type == 'bool') {
if (in_array($type, Column::BOOL_TYPES)) {
$retVal = parent::setSize(1);
} else if ($type == 'varchar' || $type == 'text') {
$retVal = $this->textTypeSize($size);
Expand All @@ -606,7 +606,7 @@ private function cleanValueHelper($val) {
return null;
} else if ($colDatatype == 'int') {
$cleanedVal = intval($val);
} else if ($colDatatype == 'bool' || $colDatatype == 'boolean') {
} else if (in_array($colDatatype, Column::BOOL_TYPES)) {
if ($val === true) {
return "b'1'";
} else {
Expand Down Expand Up @@ -645,7 +645,7 @@ private function cleanValueHelper($val) {
$cleanedVal = "'".filter_var(addslashes($val))."'";
} else if ($valType == 'double') {
$cleanedVal = "'".floatval($val)."'";
} else if ($valType == 'boolean') {
} else if (in_array($valType, Column::BOOL_TYPES)) {
if ($val === true) {
$cleanedVal = "b'1'";
} else {
Expand Down Expand Up @@ -694,7 +694,7 @@ private function defaultPart() {
$colDefault = $this->getDefault();

if ($colDefault !== null) {
if ($colDataType == 'boolean' || $colDataType == 'bool') {
if (in_array($colDataType, Column::BOOL_TYPES)) {
if ($this->getDefault() === true) {
return 'default b\'1\' ';
} else {
Expand Down Expand Up @@ -729,7 +729,7 @@ private function firstColPart() {
}
} else if ($colDataType == 'varchar' || $colDataType == 'text') {
$retVal .= $colDataType.'('.$this->getSize().') ';
} else if ($colDataType == 'boolean' || $colDataType == 'bool') {
} else if (in_array($colDataType, Column::BOOL_TYPES)) {
$retVal .= 'bit(1) ';
} else if ($colDataType == 'decimal' || $colDataType == 'float' || $colDataType == 'double') {
if ($this->getSize() != 0) {
Expand Down Expand Up @@ -762,7 +762,7 @@ private function intSize($size) {
private function nullPart() {
$colDataType = $this->getDatatype();

if (!$this->isNull() || $colDataType == 'boolean' || $colDataType == 'bool') {
if (!$this->isNull() || in_array($colDataType, Column::BOOL_TYPES)) {
return 'not null ';
} else {
return 'null ';
Expand Down
2 changes: 1 addition & 1 deletion webfiori/database/mysql/MySQLQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ private function insertHelper(array $colsKeysArr, array $valuesToInsert) {
$colsNamesArr[] = $colObj->getName();
$type = $colObj->getDatatype();

if ($type == 'boolean' || $type == 'bool') {
if (in_array($type, Column::BOOL_TYPES)) {
$valsArr[] = $colObj->cleanValue($defaultVal);
} else if (($type == 'datetime' || $type == 'timestamp') && ($defaultVal == 'now' || $defaultVal == 'now()' || $defaultVal == 'current_timestamp')) {
$valsArr[] = "'".date('Y-m-d H:i:s')."'";
Expand Down

0 comments on commit b062d56

Please sign in to comment.