Skip to content

Commit

Permalink
added reflection for Table, Column, Index, ForeignKey
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 21, 2024
1 parent 05e7104 commit ec49f7e
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ public function getSupplementalDriver(): Driver
}


public function getReflection(): Reflection
{
return new Reflection($this->getDriver());
}


public function setRowNormalizer(?callable $normalizer): static
{
$this->rowNormalizer = $normalizer;
Expand Down
39 changes: 39 additions & 0 deletions src/Database/Reflection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Database;


final class Reflection
{
public function __construct(
private readonly Driver $driver,
) {
}


/** @return Reflection\Table[] */
public function getTables(): array
{
return array_map(
fn($row) => new Reflection\Table($this->driver, ...$row),
$this->driver->getTables(),
);
}


/** @return string[] */
public function getTableNames(): array
{
return array_map(
fn($row) => $row['name'],
$this->driver->getTables(),
);
}
}
39 changes: 39 additions & 0 deletions src/Database/Reflection/Column.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Database\Reflection;

use Nette\Database\Driver;


/**
* Table or result set column reflection.
*/
final class Column
{
public function __construct(
private readonly Driver $driver,
public readonly string $name,
public readonly ?string $table = null,
public readonly string $nativeType = '',
public readonly ?int $size = null,
public readonly bool $nullable = false,
public readonly mixed $default = null,
public readonly bool $autoIncrement = false,
public readonly bool $primary = false,
public readonly array $vendor = [],
) {
}


public function __toString(): string
{
return $this->name;
}
}
34 changes: 34 additions & 0 deletions src/Database/Reflection/ForeignKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Database\Reflection;

use Nette\Database\Driver;


/**
* Foreign key reflection.
*/
final class ForeignKey
{
public function __construct(
private readonly Driver $driver,
public readonly string $name,
public readonly array $columns,
public readonly string $targetTable,
public readonly array $targetColumns,
) {
}


public function __toString(): string
{
return $this->name;
}
}
34 changes: 34 additions & 0 deletions src/Database/Reflection/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Database\Reflection;

use Nette\Database\Driver;


/**
* Index reflection.
*/
final class Index
{
public function __construct(
private readonly Driver $driver,
public readonly string $name,
public readonly array $columns,
public readonly bool $unique = false,
public readonly bool $primary = false,
) {
}


public function __toString(): string
{
return $this->name;
}
}
94 changes: 94 additions & 0 deletions src/Database/Reflection/Table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Database\Reflection;

use Nette\Database\Driver;


/**
* Table reflection.
*/
final class Table
{
private array $columns;


public function __construct(
private readonly Driver $driver,
public readonly string $name,
public readonly bool $view = false,
public readonly ?string $fullName = null,
) {
}


/** @return Column[] */
public function getColumns(): array
{
return array_map(
fn($row) => new Column($this->driver, $row['name'], $row['table'], $row['nativetype'], $row['size'], $row['nullable'], $row['default'], $row['autoincrement'], $row['primary'], $row['vendor']),
$this->columns ??= $this->driver->getColumns($this->name),
);
}


/** @return string[] */
public function getColumnNames(): array
{
return array_map(
fn($row) => $row['name'],
$this->columns ??= $this->driver->getColumns($this->name),
);
}


/** @return string[] */
public function getPrimaryKeys(): array
{
return array_map(
fn($row) => $row['name'],
array_filter(
$this->columns ??= $this->driver->getColumns($this->name),
fn($row) => $row['primary'],
),
);
}


/** @return Index[] */
public function getIndexes(): array
{
return array_map(
fn($row) => new Index($this->driver, ...$row),
$this->driver->getIndexes($this->name),
);
}


/** @return ForeignKey[] */
public function getForeignKeys(): array
{
$tmp = [];
foreach ($this->driver->getForeignKeys($this->name) as $row) {
$id = $row['name'];
$tmp[$id]['name'] = $id;
$tmp[$id]['columns'][] = $row['local'];
$tmp[$id]['targetTable'] = $row['table'];
$tmp[$id]['targetColumns'][] = $row['foreign'];
}
return array_map(fn($row) => new ForeignKey($this->driver, ...$row), $tmp);
}


public function __toString(): string
{
return $this->name;
}
}

0 comments on commit ec49f7e

Please sign in to comment.