Skip to content

Commit b74c2cf

Browse files
committed
feat: add TableName class
1 parent 1994005 commit b74c2cf

File tree

2 files changed

+325
-0
lines changed

2 files changed

+325
-0
lines changed

system/Database/TableName.php

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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 $dbPrefix DB prefix
25+
* @param string $table Table name (w/o DB prefix)
26+
* @param string $escapedTableName Escaped table name (w/z DB prefix)
27+
* @param string $escapedAliasName Escaped alias name
28+
* @param string $schema Schema name
29+
* @param string $database Database name
30+
* @param string $alias Alias name
31+
*/
32+
protected function __construct(
33+
private string $dbPrefix,
34+
private string $table,
35+
private string $escapedTableName,
36+
private string $escapedAliasName,
37+
private string $schema = '',
38+
private string $database = '',
39+
private string $alias = ''
40+
) {
41+
}
42+
43+
/**
44+
* Creates a new instance.
45+
*
46+
* @param string $table Table name (w/o DB prefix)
47+
* @param string $alias Alias name
48+
*/
49+
public static function create(BaseConnection $db, string $table, string $alias = ''): self
50+
{
51+
$escapedTableName = $db->escapeIdentifier($db->DBPrefix . $table);
52+
$escapedAliasName = $db->escapeIdentifier($alias);
53+
54+
return new self(
55+
$db->DBPrefix,
56+
$table,
57+
$escapedTableName,
58+
$escapedAliasName,
59+
'',
60+
'',
61+
$alias
62+
);
63+
}
64+
65+
/**
66+
* Creates a new instance from an actual table name.
67+
*
68+
* @param string $table Actual table name with DB prefix
69+
* @param string $alias Alias name
70+
*/
71+
public static function fromActualName(BaseConnection $db, string $table, string $alias = ''): self
72+
{
73+
$escapedTableName = $db->escapeIdentifier($table);
74+
$escapedAliasName = $db->escapeIdentifier($alias);
75+
76+
return new self(
77+
'',
78+
$table,
79+
$escapedTableName,
80+
$escapedAliasName,
81+
'',
82+
'',
83+
$alias
84+
);
85+
}
86+
87+
/**
88+
* Creates a new instance from full name.
89+
*
90+
* @param string $table Table name (w/o DB prefix)
91+
* @param string $schema Schema name
92+
* @param string $database Database name
93+
* @param string $alias Alias name
94+
*/
95+
public static function fromFullName(
96+
BaseConnection $db,
97+
string $table,
98+
string $schema = '',
99+
string $database = '',
100+
string $alias = ''
101+
): self {
102+
$escapedTableName = '';
103+
if ($database !== '') {
104+
$escapedTableName .= $db->escapeIdentifier($database) . '.';
105+
}
106+
if ($schema !== '') {
107+
$escapedTableName .= $db->escapeIdentifier($schema) . '.';
108+
}
109+
$escapedTableName .= $db->escapeIdentifier($db->DBPrefix . $table);
110+
111+
$escapedAliasName = $db->escapeIdentifier($alias);
112+
113+
return new self(
114+
$db->DBPrefix,
115+
$table,
116+
$escapedTableName,
117+
$escapedAliasName,
118+
$schema,
119+
$database,
120+
$alias
121+
);
122+
}
123+
124+
/**
125+
* Returns the single segment table name w/o DB prefix.
126+
*/
127+
public function getTableName(): string
128+
{
129+
return $this->table;
130+
}
131+
132+
/**
133+
* Returns the actual single segment table name w/z DB prefix.
134+
*/
135+
public function getActualTableName(): string
136+
{
137+
return $this->dbPrefix . $this->table;
138+
}
139+
140+
public function getAlias(): string
141+
{
142+
return $this->alias;
143+
}
144+
145+
public function getSchema(): string
146+
{
147+
return $this->schema;
148+
}
149+
150+
public function getDatabase(): string
151+
{
152+
return $this->database;
153+
}
154+
155+
/**
156+
* Returns the escaped table name.
157+
*/
158+
public function getEscapedTableName(): string
159+
{
160+
return $this->escapedTableName;
161+
}
162+
163+
/**
164+
* Returns the escaped table name with alias.
165+
*/
166+
public function getEscapedTableNameWithAlias(): string
167+
{
168+
if ($this->escapedAliasName === '') {
169+
return $this->escapedTableName;
170+
}
171+
172+
return $this->escapedTableName . ' ' . $this->escapedAliasName;
173+
}
174+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
use CodeIgniter\Test\CIUnitTestCase;
17+
use CodeIgniter\Test\Mock\MockConnection;
18+
19+
/**
20+
* @internal
21+
*
22+
* @group Others
23+
*/
24+
final class TableNameTest extends CIUnitTestCase
25+
{
26+
protected function setUp(): void
27+
{
28+
parent::setUp();
29+
30+
$this->db = new MockConnection([
31+
'database' => 'test',
32+
'DBPrefix' => 'db_',
33+
'schema' => 'dbo',
34+
]);
35+
}
36+
37+
public function testInstantiate(): void
38+
{
39+
$table = 'table';
40+
41+
$tableName = TableName::create($this->db, $table);
42+
43+
$this->assertInstanceOf(TableName::class, $tableName);
44+
}
45+
46+
public function testCreateAndTableName(): void
47+
{
48+
$table = 'table';
49+
50+
$tableName = TableName::create($this->db, $table);
51+
52+
$this->assertSame($table, $tableName->getTableName());
53+
$this->assertSame('db_table', $tableName->getActualTableName());
54+
}
55+
56+
public function testFromActualNameAndTableName(): void
57+
{
58+
$table = 'table';
59+
60+
$tableName = TableName::fromActualName($this->db, $table);
61+
62+
$this->assertSame($table, $tableName->getTableName());
63+
$this->assertSame($table, $tableName->getActualTableName());
64+
}
65+
66+
public function testGetAlias(): void
67+
{
68+
$table = 'table';
69+
$alias = 't';
70+
71+
$tableName = TableName::create($this->db, $table, $alias);
72+
73+
$this->assertSame($alias, $tableName->getAlias());
74+
}
75+
76+
public function testGetSchema(): void
77+
{
78+
$table = 'table';
79+
$schema = 'dbo';
80+
$database = 'test';
81+
82+
$tableName = TableName::fromFullName($this->db, $table, $schema, $database);
83+
84+
$this->assertSame($schema, $tableName->getSchema());
85+
}
86+
87+
public function testGetDatabase(): void
88+
{
89+
$table = 'table';
90+
$schema = 'dbo';
91+
$database = 'test';
92+
93+
$tableName = TableName::fromFullName($this->db, $table, $schema, $database);
94+
95+
$this->assertSame($database, $tableName->getDatabase());
96+
}
97+
98+
public function testGetEscapedTableName(): void
99+
{
100+
$table = 'table';
101+
102+
$tableName = TableName::create($this->db, $table);
103+
104+
$this->assertSame('"db_table"', $tableName->getEscapedTableName());
105+
}
106+
107+
public function testGetEscapedTableNameFullName(): void
108+
{
109+
$table = 'table';
110+
$schema = 'dbo';
111+
$database = 'test';
112+
113+
$tableName = TableName::fromFullName($this->db, $table, $schema, $database);
114+
115+
$this->assertSame('"test"."dbo"."db_table"', $tableName->getEscapedTableName());
116+
}
117+
118+
public function testGetEscapedTableNameWithAlias(): void
119+
{
120+
$table = 'table';
121+
$alias = 't';
122+
123+
$tableName = TableName::create($this->db, $table, $alias);
124+
125+
$this->assertSame('"db_table" "t"', $tableName->getEscapedTableNameWithAlias());
126+
}
127+
128+
public function testGetEscapedTableNameWithAliasWithoutAlias(): void
129+
{
130+
$table = 'table';
131+
132+
$tableName = TableName::create($this->db, $table);
133+
134+
$this->assertSame('"db_table"', $tableName->getEscapedTableNameWithAlias());
135+
}
136+
137+
public function testGetEscapedTableNameWithAliasFullName(): void
138+
{
139+
$table = 'table';
140+
$schema = 'dbo';
141+
$database = 'test';
142+
$alias = 't';
143+
144+
$tableName = TableName::fromFullName($this->db, $table, $schema, $database, $alias);
145+
146+
$this->assertSame(
147+
'"test"."dbo"."db_table" "t"',
148+
$tableName->getEscapedTableNameWithAlias()
149+
);
150+
}
151+
}

0 commit comments

Comments
 (0)