|
1
|
|
|
<?php |
|
2
|
|
|
namespace Darya\Database\Storage; |
|
3
|
|
|
|
|
4
|
|
|
use Darya\Database\Storage\Query\Join; |
|
5
|
|
|
use Darya\Storage\Query as StorageQuery; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Darya's database storage query representation. |
|
9
|
|
|
* |
|
10
|
|
|
* Provides joins and subqueries. |
|
11
|
|
|
* |
|
12
|
|
|
* @property-read Join[] $joins |
|
13
|
|
|
* @property-read StorageQuery $insertSubquery |
|
14
|
|
|
* |
|
15
|
|
|
* @author Chris Andrew <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class Query extends StorageQuery { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Joins to apply to the query. |
|
21
|
|
|
* |
|
22
|
|
|
* @var Join[] |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $joins = array(); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The subquery to use for the insert query. |
|
28
|
|
|
* |
|
29
|
|
|
* @var StorageQuery |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $insertSubquery; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Add an inner join to the query. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $resource |
|
37
|
|
|
* @param mixed $condition [optional] |
|
38
|
|
|
* @param string $type [optional] |
|
39
|
|
|
* @return $this |
|
40
|
|
|
*/ |
|
41
|
|
|
public function join($resource, $condition = null, $type = 'inner') { |
|
42
|
|
|
$join = new Join($type, $resource); |
|
43
|
|
|
|
|
44
|
|
|
if (is_callable($condition)) { |
|
45
|
|
|
call_user_func($condition, $join); |
|
46
|
|
|
} else { |
|
47
|
|
|
$join->on($condition); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$this->joins[] = $join; |
|
51
|
|
|
|
|
52
|
|
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Add a left join to the query. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $resource |
|
59
|
|
|
* @param mixed $condition |
|
60
|
|
|
* @return $this |
|
61
|
|
|
*/ |
|
62
|
|
|
public function leftJoin($resource, $condition = null) { |
|
63
|
|
|
$this->join($resource, $condition, 'left'); |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Add a right join to the query. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $resource |
|
72
|
|
|
* @param mixed $condition |
|
73
|
|
|
* @return $this |
|
74
|
|
|
*/ |
|
75
|
|
|
public function rightJoin($resource, $condition = null) { |
|
76
|
|
|
$this->join($resource, $condition, 'right'); |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Make this a create query with the given subquery. |
|
83
|
|
|
* |
|
84
|
|
|
* Optionally selects |
|
85
|
|
|
* |
|
86
|
|
|
* @param StorageQuery $query |
|
87
|
|
|
* @return $this |
|
88
|
|
|
*/ |
|
89
|
|
|
public function createFrom(StorageQuery $query) { |
|
90
|
|
|
$this->modify(static::CREATE); |
|
91
|
|
|
$this->insertSubquery = $query; |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Alias for createFrom(). |
|
98
|
|
|
* |
|
99
|
|
|
* @param StorageQuery $query |
|
100
|
|
|
* @return $this |
|
101
|
|
|
*/ |
|
102
|
|
|
public function insertFrom(StorageQuery $query) { |
|
103
|
|
|
$this->modify(static::CREATE); |
|
104
|
|
|
$this->createFrom($query); |
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|