Skip to content
Merged

Dev #125

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
2 changes: 1 addition & 1 deletion .github/workflows/php81.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ jobs:
code-coverage:
name: Coverage
needs: test
uses: WebFiori/workflows/.github/workflows/coverage-codecov.yaml@main
uses: WebFiori/workflows/.github/workflows/coverage-codecov.yaml@v1.2
with:
php-version: '8.1'
coverage-file: 'php-8.1-coverage.xml'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php82.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ jobs:
code-coverage:
name: Coverage
needs: test
uses: WebFiori/workflows/.github/workflows/coverage-codecov.yaml@main
uses: WebFiori/workflows/.github/workflows/coverage-codecov.yaml@v1.2
with:
php-version: '8.2'
coverage-file: 'php-8.2-coverage.xml'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php83.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ jobs:
code-coverage:
name: Coverage
needs: test
uses: WebFiori/workflows/.github/workflows/coverage-codecov.yaml@main
uses: WebFiori/workflows/.github/workflows/coverage-codecov.yaml@v1.2
with:
php-version: '8.3'
coverage-file: 'php-8.3-coverage.xml'
Expand Down
13 changes: 12 additions & 1 deletion .github/workflows/php84.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,15 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: code-coverage
path: php-8.4-coverage.xml
path: php-8.4-coverage.xml


code-coverage:
name: Coverage
needs: test
uses: WebFiori/workflows/.github/workflows/coverage-codecov.yaml@v1.2
with:
php-version: '8.4'
coverage-file: 'php-8.4-coverage.xml'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
8 changes: 4 additions & 4 deletions .github/workflows/php85.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
with:
php-version: 8.5
extensions: mysqli, mbstring, sqlsrv
tools: phpunit:11.5.27, composer
tools: phpunit:11.5.46, composer

- name: Install ODBC Driver for SQL Server
run: |
Expand Down Expand Up @@ -106,7 +106,7 @@ jobs:
code-coverage:
name: Coverage
needs: test
uses: WebFiori/workflows/.github/workflows/coverage-codecov.yaml@main
uses: WebFiori/workflows/.github/workflows/coverage-codecov.yaml@v1.2
with:
php-version: '8.5'
coverage-file: 'php-8.5-coverage.xml'
Expand All @@ -116,13 +116,13 @@ jobs:
code-quality:
name: Code Quality
needs: test
uses: WebFiori/workflows/.github/workflows/quality-sonarcloud.yaml@main
uses: WebFiori/workflows/.github/workflows/quality-sonarcloud.yaml@v1.2
secrets:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

release-prod:
name: Prepare Production Release Branch / Publish Release
needs: [code-coverage, code-quality]
uses: WebFiori/workflows/.github/workflows/release-php.yaml@main
uses: WebFiori/workflows/.github/workflows/release-php.yaml@v1.2
with:
branch: 'main'
12 changes: 10 additions & 2 deletions WebFiori/Database/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,20 @@ public function delete() {
* the method is called on.
*
*/
public function drop() {
public function drop(bool $ifExists = false) {
$table = $this->getTable();
$this->setQuery('drop table '.$table->getName().';');
$this->setQuery($this->buildDropQuery($table->getName(), $ifExists));

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
7 changes: 6 additions & 1 deletion WebFiori/Database/Attributes/ForeignKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

use Attribute;

/**
* Defines a foreign key constraint and optionally a belongsTo relationship.
*/
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class ForeignKey {
public function __construct(
Expand All @@ -11,7 +14,9 @@ public function __construct(
public array $columns = [],
public ?string $name = null,
public string $onUpdate = 'set null',
public string $onDelete = 'set null'
public string $onDelete = 'set null',
public ?string $property = null,
public ?string $entity = null
) {
if ($column !== null && !empty($columns)) {
throw new InvalidAttributeException(
Expand Down
18 changes: 18 additions & 0 deletions WebFiori/Database/Attributes/HasMany.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace WebFiori\Database\Attributes;

use Attribute;

/**
* Defines a one-to-many relationship.
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class HasMany {
public function __construct(
public string $entity,
public string $foreignKey,
public string $property,
public ?string $localKey = null,
public ?string $table = null
) {}
}
32 changes: 21 additions & 11 deletions WebFiori/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,22 +262,32 @@ public function createTable() : AbstractQuery {
/**
* Create SQL query which can be used to create all database tables.
*
* @return AbstractQuery The method will return an instance of the class
* 'AbstractQuery' which can be used to build SQL queries.
* Tables are created in the order they were added. On failure, any
* successfully created tables are dropped in reverse order to handle
* foreign key constraints properly.
*
* .1
* @throws DatabaseException If table creation fails.
*/
public function createTables() : AbstractQuery {
$generatedQuery = '';
public function createTables(): void {
$created = [];

foreach ($this->getTables() as $tableObj) {
if ($tableObj->getColsCount() != 0) {
$generatedQuery .= $tableObj->toSQL()."\n";
try {
foreach ($this->getTables() as $tableObj) {
if ($tableObj->getColsCount() != 0) {
$this->table($tableObj->getNormalName())->createTable()->execute();
$created[] = $tableObj;
}
}
} catch (DatabaseException $e) {
foreach (array_reverse($created) as $tableObj) {
try {
$this->table($tableObj->getNormalName())->drop()->execute();
} catch (DatabaseException $dropError) {
// Continue cleanup
}
}
throw $e;
}
$this->getQueryGenerator()->setQuery($generatedQuery, true);

return $this->getQueryGenerator();
}
/**
* Constructs a query which can be used to remove a record from the
Expand Down
7 changes: 7 additions & 0 deletions WebFiori/Database/MsSql/MSSQLQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ 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
Loading