1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BfwSql\Queries\Parts; |
4
|
|
|
|
5
|
|
|
use \Exception; |
6
|
|
|
|
7
|
|
|
class SubQuery |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @const ERR_QUERY_NOT_OBJECT_OR_STRING Exception code if the query is |
11
|
|
|
* not an object or a string. |
12
|
|
|
*/ |
13
|
|
|
const ERR_QUERY_NOT_OBJECT_OR_STRING = 2513001; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @const ERR_QUERY_OBJECT_BAD_INSTANCE Exception code in the case of |
17
|
|
|
* the query is an object but with not the expected class. |
18
|
|
|
*/ |
19
|
|
|
const ERR_QUERY_OBJECT_BAD_INSTANCE = 2513002; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string $query The sub-query |
23
|
|
|
*/ |
24
|
|
|
protected $query = ''; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string $shortcut The shortcut to use into the request |
28
|
|
|
*/ |
29
|
|
|
protected $shortcut = ''; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Define the shortcut and the sql query. |
33
|
|
|
* If the sub-query is an object, call method to obtain the string query |
34
|
|
|
* |
35
|
|
|
* @param string $shortcut The shortcut to use into the request |
36
|
|
|
* @param string|\BfwSql\Queries\AbstractQuery $query The sub-query |
37
|
|
|
*/ |
38
|
|
|
public function __construct(string $shortcut, $query) |
39
|
|
|
{ |
40
|
|
|
$this->shortcut = $shortcut; |
41
|
|
|
$this->query = $this->obtainAssembledQuery($query); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Getter accessor to property query |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getQuery(): string |
50
|
|
|
{ |
51
|
|
|
return $this->query; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Getter accessor to property shortcut |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getShortcut(): string |
60
|
|
|
{ |
61
|
|
|
return $this->shortcut; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Obtain the sql request (string type) from the arguments |
66
|
|
|
* If it's already a string, just return the string. |
67
|
|
|
* Else, if it's an AbstractQuery, use the method assemble() to obtain |
68
|
|
|
* the string sql request. |
69
|
|
|
* |
70
|
|
|
* @param string|\BfwSql\Queries\AbstractQuery $query The sub-query |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
* |
74
|
|
|
* @throws \Exception If the sub-query has not the correct type or if it's |
75
|
|
|
* not the correct class instance. |
76
|
|
|
*/ |
77
|
|
|
protected function obtainAssembledQuery($query): string |
78
|
|
|
{ |
79
|
|
|
if (is_string($query)) { |
80
|
|
|
return $query; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (!is_object($query)) { |
84
|
|
|
throw new Exception( |
85
|
|
|
'A sub-query should be a string or an AbstractQuery object', |
86
|
|
|
self::ERR_QUERY_NOT_OBJECT_OR_STRING |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($query instanceof \BfwSql\Queries\AbstractQuery === false) { |
91
|
|
|
throw new Exception( |
92
|
|
|
'If a sub-query is an object, ' |
93
|
|
|
.'it should be an instance of AbstractQuery object', |
94
|
|
|
self::ERR_QUERY_OBJECT_BAD_INSTANCE |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $query->assemble(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Generate the sql query to use for to this sub-query |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function generate(): string |
107
|
|
|
{ |
108
|
|
|
return '('.$this->query.') AS `'.$this->shortcut.'`'; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|