|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Database; |
| 15 | + |
| 16 | +/** |
| 17 | + * Represents a table name in SQL. |
| 18 | + * |
| 19 | + * @see \CodeIgniter\Database\TableNameTest |
| 20 | + */ |
| 21 | +class TableName |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @param string $actualTable Actual table name |
| 25 | + * @param string $logicalTable Logical table name (w/o DB prefix) |
| 26 | + * @param string $schema Schema name |
| 27 | + * @param string $database Database name |
| 28 | + * @param string $alias Alias name |
| 29 | + */ |
| 30 | + protected function __construct( |
| 31 | + private readonly string $actualTable, |
| 32 | + private readonly string $logicalTable = '', |
| 33 | + private readonly string $schema = '', |
| 34 | + private readonly string $database = '', |
| 35 | + private readonly string $alias = '' |
| 36 | + ) { |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Creates a new instance. |
| 41 | + * |
| 42 | + * @param string $table Table name (w/o DB prefix) |
| 43 | + * @param string $alias Alias name |
| 44 | + */ |
| 45 | + public static function create(BaseConnection $db, string $table, string $alias = ''): self |
| 46 | + { |
| 47 | + return new self( |
| 48 | + $db->DBPrefix . $table, |
| 49 | + $table, |
| 50 | + '', |
| 51 | + '', |
| 52 | + $alias |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Creates a new instance from an actual table name. |
| 58 | + * |
| 59 | + * @param string $actualTable Actual table name with DB prefix |
| 60 | + * @param string $alias Alias name |
| 61 | + */ |
| 62 | + public static function fromActualName(BaseConnection $db, string $actualTable, string $alias = ''): self |
| 63 | + { |
| 64 | + $prefix = $db->DBPrefix; |
| 65 | + $logicalTable = ''; |
| 66 | + |
| 67 | + if (str_starts_with($actualTable, $prefix)) { |
| 68 | + $logicalTable = substr($actualTable, strlen($prefix)); |
| 69 | + } |
| 70 | + |
| 71 | + return new self( |
| 72 | + $actualTable, |
| 73 | + $logicalTable, |
| 74 | + '', |
| 75 | + $alias |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Creates a new instance from full name. |
| 81 | + * |
| 82 | + * @param string $table Table name (w/o DB prefix) |
| 83 | + * @param string $schema Schema name |
| 84 | + * @param string $database Database name |
| 85 | + * @param string $alias Alias name |
| 86 | + */ |
| 87 | + public static function fromFullName( |
| 88 | + BaseConnection $db, |
| 89 | + string $table, |
| 90 | + string $schema = '', |
| 91 | + string $database = '', |
| 92 | + string $alias = '' |
| 93 | + ): self { |
| 94 | + return new self( |
| 95 | + $db->DBPrefix . $table, |
| 96 | + $table, |
| 97 | + $schema, |
| 98 | + $database, |
| 99 | + $alias |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Returns the single segment table name w/o DB prefix. |
| 105 | + */ |
| 106 | + public function getTableName(): string |
| 107 | + { |
| 108 | + return $this->logicalTable; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns the actual single segment table name w/z DB prefix. |
| 113 | + */ |
| 114 | + public function getActualTableName(): string |
| 115 | + { |
| 116 | + return $this->actualTable; |
| 117 | + } |
| 118 | + |
| 119 | + public function getAlias(): string |
| 120 | + { |
| 121 | + return $this->alias; |
| 122 | + } |
| 123 | + |
| 124 | + public function getSchema(): string |
| 125 | + { |
| 126 | + return $this->schema; |
| 127 | + } |
| 128 | + |
| 129 | + public function getDatabase(): string |
| 130 | + { |
| 131 | + return $this->database; |
| 132 | + } |
| 133 | +} |
0 commit comments