|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Helix\DB; |
|
4
|
|
|
|
|
5
|
|
|
use Helix\DB; |
|
6
|
|
|
use Helix\DB\SQL\AggregateTrait; |
|
7
|
|
|
use Helix\DB\SQL\ComparisonTrait; |
|
8
|
|
|
use Helix\DB\SQL\DateTimeTrait; |
|
9
|
|
|
use Helix\DB\SQL\NumTrait; |
|
10
|
|
|
use Helix\DB\SQL\TextTrait; |
|
11
|
|
|
use Helix\DB\SQL\ValueInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Immutable column expression. Can produce all available transformations. |
|
15
|
|
|
* |
|
16
|
|
|
* @immutable Mutations operate on and return clones. |
|
17
|
|
|
* |
|
18
|
|
|
* @method static static factory(DB $db, string $name, string $qualifier = '') |
|
19
|
|
|
*/ |
|
20
|
|
|
class Column implements ValueInterface { |
|
21
|
|
|
|
|
22
|
|
|
use FactoryTrait; |
|
23
|
|
|
use AggregateTrait; |
|
24
|
|
|
use ComparisonTrait; |
|
25
|
|
|
use DateTimeTrait; |
|
26
|
|
|
use NumTrait; |
|
27
|
|
|
use TextTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $name; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $qualifier; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param DB $db |
|
41
|
|
|
* @param string $name |
|
42
|
|
|
* @param string $qualifier |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct (DB $db, string $name, string $qualifier = '') { |
|
45
|
|
|
$this->db = $db; |
|
46
|
|
|
$this->name = $name; |
|
47
|
|
|
$this->qualifier = $qualifier; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Returns the qualified name. |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __toString (): string { |
|
56
|
|
|
if (strlen($this->qualifier)) { |
|
57
|
|
|
return "{$this->qualifier}.{$this->name}"; |
|
58
|
|
|
} |
|
59
|
|
|
return $this->name; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
final public function getName (): string { |
|
66
|
|
|
return $this->name; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
final public function getQualifier (): string { |
|
73
|
|
|
return $this->qualifier; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns a {@link Select} for the column's values. The column must be qualified. |
|
78
|
|
|
* |
|
79
|
|
|
* @return Select|scalar[] |
|
80
|
|
|
*/ |
|
81
|
|
|
public function select () { |
|
82
|
|
|
return Select::factory($this->db, $this->qualifier, [$this->name]) |
|
83
|
|
|
->setFetcher(function(Statement $statement) { |
|
84
|
|
|
while (false !== $value = $statement->fetchColumn()) { |
|
85
|
|
|
yield $value; |
|
86
|
|
|
} |
|
87
|
|
|
}); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $name |
|
92
|
|
|
* @return $this |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setName (string $name) { |
|
95
|
|
|
$clone = clone $this; |
|
96
|
|
|
$clone->name = $name; |
|
97
|
|
|
return $clone; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $qualifier |
|
102
|
|
|
* @return $this |
|
103
|
|
|
*/ |
|
104
|
|
|
public function setQualifier (string $qualifier) { |
|
105
|
|
|
$clone = clone $this; |
|
106
|
|
|
$clone->qualifier = $qualifier; |
|
107
|
|
|
return $clone; |
|
108
|
|
|
} |
|
109
|
|
|
} |