Repositories

This commit is contained in:
Juan Pablo Vial
2024-12-21 18:59:46 -03:00
parent 916895e489
commit ea205df76d
2 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,25 @@
<?php
namespace ProVM\Repository;
use Generator;
use PDO;
use ProVM\Concept;
class Data
{
public function __construct(protected Concept\Database\Connection $connection, protected Concept\Database\Query\Builder $queryBuilder) {}
public int $size;
public function getAll(string $table): Generator
{
$query = $this->queryBuilder
->select()
->from($table);
$results = $this->connection->query($query);
$this->size = $results->rowCount();
while ($row = $results->fetch()) {
yield $row;
}
}
}

View File

@ -0,0 +1,95 @@
<?php
namespace ProVM\Repository;
use PDO;
use ProVM\Concept;
use ProVM\Parser;
class Table
{
public function __construct(protected Concept\Database\Connection $connection, protected string $databaseName) {}
public function getAll(): array
{
if (!isset($this->tables)) {
$results = $this->connection->query('SHOW TABLES');
$rows = $results->fetchAll();
$this->tables = array_map(function(array $row) {
return $row["Tables_in_{$this->databaseName}"];
}, $rows);
}
return $this->tables;
}
public function getDefinition(string $table): string
{
if (!isset($this->definitions[$table])) {
$results = $this->connection->query("SHOW CREATE TABLE {$table}");
$rows = $results->fetchAll();
$this->definitions[$table] = $rows[0]["Create Table"];
}
return $this->definitions[$table];
}
public function parseDefinition(string $table, int $tabOffset = 2): string
{
$this->extractLines($table);
$tableLine = "\$this->table('{$table}'";
if (count($this->primary) === 1) {
$tableLine .= ", ['id' => '{$this->primary[0]}']";
} elseif (count($this->primary) > 1) {
$primaryString = implode(', ', array_map(function(string $key) {return "'{$key}'";}, $this->primary));
$tableLine .= ", ['id' => false, 'primary key' => [{$primaryString}]]";
}
$tableLine .= ')';
$output = [str_repeat("\t", $tabOffset) . $tableLine];
foreach ($this->columns as $column) {
$output[] = str_repeat("\t", $tabOffset + 1) . $column;
}
foreach ($this->constraints as $constraint) {
$output[] = str_repeat("\t", $tabOffset + 1) . $constraint;
}
return implode(PHP_EOL, $output);
}
public function getIndex(string $table): int
{
return array_search($table, $this->tables);
}
protected array $tables = [];
protected array $definitions = [];
protected array $primary = [];
protected array $columns = [];
protected array $constraints = [];
protected function extractLines(string $table): void
{
$this->primary = [];
$this->columns = [];
$this->constraints = [];
$lines = explode(PHP_EOL, $this->definitions[$table]);
array_shift($lines);
foreach ($lines as $line) {
$trimmedLine = trim($line);
if (str_starts_with($trimmedLine, ') ENGINE')) {
break;
}
if (str_starts_with($trimmedLine, '`id`') or str_starts_with($trimmedLine, 'KEY')) {
continue;
}
if (str_starts_with($trimmedLine, 'PRIMARY KEY')) {
if (str_contains($line, '`id`')) {
continue;
}
$ini = strpos($line, '(') +1;
$end = strpos($line, ')', $ini);
$this->primary []= substr($line, $ini, $end - $ini);
continue;
}
if (str_starts_with($trimmedLine, 'CONSTRAINT')) {
$this->constraints []= (new Parser\Constraint($line))->parse();
continue;
}
$this->columns []= (new Parser\Column($line))->parse();
}
}
}