1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Schema; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
8
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @example |
12
|
|
|
* (new Query)->select('col1')->from(new TableName('table1', 't', 'dbo')) |
13
|
|
|
* |
14
|
|
|
* Note: We must use prefix (from connection) with tables with schema equal defaultSchema or without schema and don't use with other schemas |
15
|
|
|
* Note: With ExpressionInterface as tablename - we cannot add prefixes and quoting of table names. |
16
|
|
|
* For example with Oracle: (new Query)->select('*')->from(new Expression('dblink1.dbo.table')) for build `select * from dblink1.dbo.table1` |
17
|
|
|
*/ |
18
|
|
|
final class TableName |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ExpressionInterface|string |
22
|
|
|
*/ |
23
|
|
|
private $name; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string|null |
27
|
|
|
*/ |
28
|
|
|
private ?string $alias; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ExpressionInterface|string|null |
32
|
|
|
*/ |
33
|
|
|
private $schema; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ExpressionInterface|string $name |
37
|
|
|
* @param string|null $alias |
38
|
|
|
* @param ExpressionInterface|string|null $schema |
39
|
|
|
*/ |
40
|
|
|
public function __construct($name, ?string $alias = null, $schema = null) |
41
|
|
|
{ |
42
|
|
|
if (!is_string($name) && !$name instanceof ExpressionInterface) { |
|
|
|
|
43
|
|
|
throw new InvalidArgumentException( |
44
|
|
|
'Name of the table should be a string or an instanceof ExpressionInterface.' |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ($schema !== null && !is_string($schema) && !$schema instanceof ExpressionInterface) { |
|
|
|
|
49
|
|
|
throw new InvalidArgumentException( |
50
|
|
|
'Schema should be null, string or instanceof ExpressionInterface' |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->name = $name; |
55
|
|
|
$this->alias = empty($alias) ? null : $alias; |
56
|
|
|
$this->schema = empty($schema) ? null : $schema; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return ExpressionInterface|string |
61
|
|
|
*/ |
62
|
|
|
public function getName() |
63
|
|
|
{ |
64
|
|
|
return $this->name; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getAlias(): ?string |
68
|
|
|
{ |
69
|
|
|
return $this->alias; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return ExpressionInterface|string|null |
74
|
|
|
*/ |
75
|
|
|
public function getSchema() |
76
|
|
|
{ |
77
|
|
|
return $this->schema; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function hasAlias(): bool |
81
|
|
|
{ |
82
|
|
|
return $this->alias !== null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function hasSchema(): bool |
86
|
|
|
{ |
87
|
|
|
return $this->schema !== null; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|