From ea205df76d3f735c54948254b8fc74c13ecf38ff Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Sat, 21 Dec 2024 18:59:46 -0300 Subject: [PATCH] Repositories --- app/src/Repository/Data.php | 25 ++++++++++ app/src/Repository/Table.php | 95 ++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 app/src/Repository/Data.php create mode 100644 app/src/Repository/Table.php diff --git a/app/src/Repository/Data.php b/app/src/Repository/Data.php new file mode 100644 index 0000000..fc1a8c7 --- /dev/null +++ b/app/src/Repository/Data.php @@ -0,0 +1,25 @@ +queryBuilder + ->select() + ->from($table); + $results = $this->connection->query($query); + $this->size = $results->rowCount(); + while ($row = $results->fetch()) { + yield $row; + } + } +} \ No newline at end of file diff --git a/app/src/Repository/Table.php b/app/src/Repository/Table.php new file mode 100644 index 0000000..afbf37b --- /dev/null +++ b/app/src/Repository/Table.php @@ -0,0 +1,95 @@ +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(); + } + } +} \ No newline at end of file