-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added reflection for Table, Column, Index, ForeignKey
- Loading branch information
Showing
6 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?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(), | ||
); | ||
} | ||
|
||
|
||
public function getColumn(string $name): Column | ||
{ | ||
return $this->columns()[$name] ?? throw new \InvalidArgumentException("Column '$name' not found in table '$this->name'."); | ||
} | ||
|
||
|
||
/** @return string[] */ | ||
public function getColumnNames(): array | ||
{ | ||
return array_map( | ||
fn($row) => $row['name'], | ||
$this->columns(), | ||
); | ||
} | ||
|
||
|
||
/** @return string[] */ | ||
public function getPrimaryKeys(): array | ||
{ | ||
return array_map( | ||
fn($row) => $row['name'], | ||
array_filter( | ||
$this->columns(), | ||
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); | ||
} | ||
|
||
|
||
private function columns(): array | ||
{ | ||
return $this->columns ??= $this->driver->getColumns($this->name); | ||
} | ||
|
||
|
||
public function __toString(): string | ||
{ | ||
return $this->name; | ||
} | ||
} |