Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions WebFiori/Database/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,6 @@ public function drop(bool $ifExists = false) {

return $this;
}

/**
* Builds the DROP TABLE query. Override in subclasses for DBMS-specific syntax.
*/
protected function buildDropQuery(string $tableName, bool $ifExists): string {
$ifExistsStr = $ifExists ? 'if exists ' : '';
return 'drop table ' . $ifExistsStr . $tableName . ';';
}
/**
* Constructs a query which can be used to drop a column from associated
* table.
Expand Down Expand Up @@ -1502,6 +1494,15 @@ private function getColsToSelect() {

return $columnsToSelect;
}

/**
* Builds the DROP TABLE query. Override in subclasses for DBMS-specific syntax.
*/
protected function buildDropQuery(string $tableName, bool $ifExists): string {
$ifExistsStr = $ifExists ? 'if exists ' : '';

return 'drop table '.$ifExistsStr.$tableName.';';
}
protected function getColType($phpVar) {
$type = gettype($phpVar);

Expand Down
26 changes: 15 additions & 11 deletions WebFiori/Database/Attributes/AttributeTableBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static function build(string $entityClass, string $dbType = 'mysql'): Tab
$reflection = new ReflectionClass($entityClass);

$tableAttr = $reflection->getAttributes(Table::class)[0] ?? null;

if (!$tableAttr) {
throw new InvalidAttributeException("Class $entityClass must have #[Table] attribute");
}
Expand All @@ -35,6 +36,7 @@ public static function build(string $entityClass, string $dbType = 'mysql'): Tab
} else {
foreach ($reflection->getProperties() as $property) {
$columnAttrs = $property->getAttributes(Column::class);

if (empty($columnAttrs)) {
continue;
}
Expand Down Expand Up @@ -116,17 +118,6 @@ private static function addPropertyForeignKey(TableClass $table, string $localCo
);
}

private static function resolveTableName(string $tableOrClass): string {
if (class_exists($tableOrClass)) {
$reflection = new ReflectionClass($tableOrClass);
$tableAttr = $reflection->getAttributes(Table::class)[0] ?? null;
if ($tableAttr) {
return $tableAttr->newInstance()->name;
}
}
return $tableOrClass;
}

private static function columnConfigToArray(Column $config): array {
return [
ColOption::TYPE => $config->type,
Expand All @@ -148,4 +139,17 @@ private static function columnConfigToArray(Column $config): array {
private static function propertyToKey(string $propertyName): string {
return strtolower(preg_replace('/([a-z])([A-Z])/', '$1-$2', $propertyName));
}

private static function resolveTableName(string $tableOrClass): string {
if (class_exists($tableOrClass)) {
$reflection = new ReflectionClass($tableOrClass);
$tableAttr = $reflection->getAttributes(Table::class)[0] ?? null;

if ($tableAttr) {
return $tableAttr->newInstance()->name;
}
}

return $tableOrClass;
}
}
1 change: 1 addition & 0 deletions WebFiori/Database/Attributes/ForeignKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function getColumnsMap(): array {
if ($this->column !== null) {
return [$this->column];
}

return $this->columns;
}
}
3 changes: 2 additions & 1 deletion WebFiori/Database/Attributes/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public function __construct(
public string $property,
public ?string $localKey = null,
public ?string $table = null
) {}
) {
}
}
52 changes: 52 additions & 0 deletions WebFiori/Database/DataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,58 @@ class DataType {
* </ul>
*/
const VARCHAR = 'varchar';
/**
* Mapping of database engines to their supported data types.
*/
private static array $EngineTypes = [
'mysql' => [
self::BIT,
self::BLOB,
self::BLOB_LONG,
self::BLOB_MEDIUM,
self::BLOB_TINY,
self::BOOL,
self::CHAR,
self::DATETIME,
self::DECIMAL,
self::DOUBLE,
self::FLOAT,
self::INT,
self::TEXT,
self::TEXT_MEDIUM,
self::TIMESTAMP,
self::VARCHAR,
],
'mssql' => [
self::BIGINT,
self::BINARY,
self::BIT,
self::BOOL,
self::CHAR,
self::DATE,
self::DATETIME,
self::DATETIME2,
self::DECIMAL,
self::FLOAT,
self::INT,
self::MONEY,
self::NCHAR,
self::NVARCHAR,
self::TIME,
self::VARBINARY,
self::VARCHAR,
],
];

/**
* Returns an array of supported data types for a specific database engine.
*
* @param string $engine The database engine name (e.g., 'mysql', 'mssql')
* @return array Array of supported data type constants, or empty array if engine is not supported
*/
public static function getSupportedDataTypes(string $engine): array {
return self::$EngineTypes[strtolower($engine)] ?? [];
}

/**
* Maps database data type to PHP type.
Expand Down
2 changes: 1 addition & 1 deletion WebFiori/Database/Entity/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private function getDefault(Column $col): string {

if ($default !== null) {
return match ($phpType) {
'string' => " = '" . addslashes($default) . "'",
'string' => " = '".addslashes($default)."'",
'int', 'float' => " = {$default}",
'bool' => $default ? ' = true' : ' = false',
default => ''
Expand Down
22 changes: 2 additions & 20 deletions WebFiori/Database/MsSql/MSSQLColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use WebFiori\Database\Column;
use WebFiori\Database\DatabaseException;
use WebFiori\Database\DataType;
use WebFiori\Database\Factory\ColumnFactory;
use WebFiori\Database\Util\DateTimeValidator;
/**
Expand Down Expand Up @@ -61,26 +62,7 @@ public function __construct(string $name = 'col', string $datatype = 'nvarchar',
$this->isAutoUpdate = false;
$this->isIdintity = false;
$this->setWithExtendedProps(false);
$this->setSupportedTypes([
'int',
'bigint',
'varchar',
'nvarchar',
'char',
'nchar',
'binary',
'varbinary',
'date',
'datetime2',
'datetime',
'time',
'money',
'bit',
'decimal',
'float',
'boolean',
'bool'
]);
$this->setSupportedTypes(DataType::getSupportedDataTypes('mssql'));
$this->setDatatype($datatype);

if (!$this->setSize($size)) {
Expand Down
15 changes: 8 additions & 7 deletions WebFiori/Database/MsSql/MSSQLQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ public function __construct() {
$this->bindings = [];
}

protected function buildDropQuery(string $tableName, bool $ifExists): string {
if ($ifExists) {
return "if object_id('{$tableName}', 'U') is not null drop table {$tableName};";
}
return "drop table {$tableName};";
}

public function addBinding(Column $col, $value) {
$this->bindings[] = $value;
}
Expand Down Expand Up @@ -420,4 +413,12 @@ private function insertHelper(array $colsKeysArr, array $valuesToInsert) {
'vals' => implode(', ', $valsArr)
];
}

protected function buildDropQuery(string $tableName, bool $ifExists): string {
if ($ifExists) {
return "if object_id('{$tableName}', 'U') is not null drop table {$tableName};";
}

return "drop table {$tableName};";
}
}
21 changes: 2 additions & 19 deletions WebFiori/Database/MySql/MySQLColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use WebFiori\Database\Column;
use WebFiori\Database\DatabaseException;
use WebFiori\Database\DataType;
use WebFiori\Database\Factory\ColumnFactory;
use WebFiori\Database\Table;
use WebFiori\Database\Util\DateTimeValidator;
Expand Down Expand Up @@ -64,25 +65,7 @@ class MySQLColumn extends Column {
*/
public function __construct(string $name = 'col', string $datatype = 'varchar',int $size = 1) {
parent::__construct($name);
$this->setSupportedTypes([
'int',
'char',
'varchar',
'timestamp',
'tinyblob',
'blob',
'mediumblob',
'longblob',
'datetime',
'text',
'mediumtext',
'decimal',
'double',
'float',
'boolean',
'bool',
'bit'
]);
$this->setSupportedTypes(DataType::getSupportedDataTypes('mysql'));
$this->setDatatype($datatype);

if (!$this->setSize($size)) {
Expand Down
Loading