82 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			82 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
namespace DatabaseHelper;
 | 
						|
 | 
						|
use DatabaseHelper\enums\Aggregation;
 | 
						|
use DatabaseHelper\enums\Charset;
 | 
						|
use DatabaseHelper\enums\Collation;
 | 
						|
use DatabaseHelper\enums\Engine;
 | 
						|
use http\Exception\InvalidArgumentException;
 | 
						|
 | 
						|
class Schema
 | 
						|
{
 | 
						|
    public string $name = '';
 | 
						|
 | 
						|
    public array $columns = [];
 | 
						|
    public array $primaryKey = [];
 | 
						|
    public array $foreignKeys = [];
 | 
						|
 | 
						|
    public Engine $engine = Engine::INNODB;
 | 
						|
    public Charset $charset = Charset::UTF8;
 | 
						|
    public Collation $collation = Collation::UTF8_GENERAL_CI;
 | 
						|
 | 
						|
    public function __construct(string $name) {
 | 
						|
        $this->name = $name;
 | 
						|
    }
 | 
						|
 | 
						|
    public function requireColumn(string $col): void {
 | 
						|
        if (!$this->existsColumn($col))
 | 
						|
            throw new InvalidArgumentException("Column '$col' is not defined.");
 | 
						|
    }
 | 
						|
 | 
						|
    public function existsColumn(string $col): bool {
 | 
						|
        return isset($this->columns[$col]);
 | 
						|
    }
 | 
						|
 | 
						|
    public function existsReference(Schema $schema): bool {
 | 
						|
        foreach ($this->foreignKeys as $foreignKey)
 | 
						|
            if ($foreignKey['table'] == $schema->name)
 | 
						|
                return true;
 | 
						|
        return false;
 | 
						|
    }
 | 
						|
 | 
						|
    public function countEntries(): int {
 | 
						|
        return Database::makeQuery($this)
 | 
						|
            ->aggregate('*', 'count', Aggregation::COUNT)
 | 
						|
            ->query();
 | 
						|
    }
 | 
						|
 | 
						|
    public function columnType(string $col) {
 | 
						|
        return $this->columns[$col]['type'];
 | 
						|
    }
 | 
						|
 | 
						|
    public function primaryKey() {
 | 
						|
        return $this->columns['primary']['name'];
 | 
						|
    }
 | 
						|
 | 
						|
    public function copy(): Schema {
 | 
						|
        $copy = new Schema($this->name);
 | 
						|
        $copy->columns = genericDeepCopy($this->columns);
 | 
						|
        $copy->primaryKey = genericDeepCopy($this->primaryKey);
 | 
						|
        $copy->foreignKeys = genericDeepCopy($this->foreignKeys);
 | 
						|
        $copy->engine = $this->engine;
 | 
						|
        $copy->charset = $this->charset;
 | 
						|
        $copy->collation = $this->collation;
 | 
						|
        return $copy;
 | 
						|
    }
 | 
						|
 | 
						|
    public function include(Schema $other): void {
 | 
						|
        // Create a copy of the other schema.
 | 
						|
        $otherCopy = $other->copy();
 | 
						|
 | 
						|
        // Add any column that isn't already defined.
 | 
						|
        foreach ($otherCopy->columns as $colName => $colDef)
 | 
						|
            if (!isset($this->columns[$colName]))
 | 
						|
                $this->columns[$colName] = $colDef;
 | 
						|
 | 
						|
        // Add any foreign key that doesn't already exist.
 | 
						|
        foreach ($other->foreignKeys as $colName => $foreignKey)
 | 
						|
            if (!isset($this->foreignKeys[$colName]))
 | 
						|
                $this->foreignKeys[$colName] = $foreignKey;
 | 
						|
    }
 | 
						|
}
 |