|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Phossa Project |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.4 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Library |
|
8
|
|
|
* @package Phossa2\Query |
|
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
|
10
|
|
|
* @license http://mit-license.org/ MIT License |
|
11
|
|
|
* @link http://www.phossa.com/ |
|
12
|
|
|
*/ |
|
13
|
|
|
/*# declare(strict_types=1); */ |
|
14
|
|
|
|
|
15
|
|
|
namespace Phossa2\Query\Traits\Clause; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Query\Interfaces\ClauseInterface; |
|
18
|
|
|
use Phossa2\Query\Interfaces\Clause\OnInterface; |
|
19
|
|
|
use Phossa2\Query\Interfaces\ExpressionInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* OnTrait |
|
23
|
|
|
* |
|
24
|
|
|
* Implementation of OnInterface |
|
25
|
|
|
* |
|
26
|
|
|
* @package Phossa2\Query |
|
27
|
|
|
* @author Hong Zhang <[email protected]> |
|
28
|
|
|
* @see OnInterface |
|
29
|
|
|
* @version 2.0.0 |
|
30
|
|
|
* @since 2.0.0 added |
|
31
|
|
|
*/ |
|
32
|
|
|
trait OnTrait |
|
33
|
|
|
{ |
|
34
|
|
|
use AbstractTrait; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritDoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function on( |
|
40
|
|
|
$firstTableCol, |
|
41
|
|
|
/*# string */ $operator = ClauseInterface::NO_OPERATOR, |
|
42
|
|
|
/*# string */ $secondTableCol = ClauseInterface::NO_VALUE |
|
43
|
|
|
) { |
|
44
|
|
|
return $this->realOn($firstTableCol, $operator, $secondTableCol); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritDoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function orOn( |
|
51
|
|
|
$firstTableCol, |
|
52
|
|
|
/*# string */ $operator = ClauseInterface::NO_OPERATOR, |
|
53
|
|
|
/*# string */ $secondTableCol = ClauseInterface::NO_VALUE |
|
54
|
|
|
) { |
|
55
|
|
|
return $this->realOn($firstTableCol, $operator, $secondTableCol, true); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritDoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function onRaw(/*# string */ $rawString, array $params = []) |
|
62
|
|
|
{ |
|
63
|
|
|
$rawString = $this->positionedParam($rawString, $params); |
|
64
|
|
|
return $this->realOn($rawString); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritDoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function orOnRaw(/*# string */ $rawString, array $params = []) |
|
71
|
|
|
{ |
|
72
|
|
|
$rawString = $this->positionedParam($rawString, $params); |
|
73
|
|
|
return $this->realOn( |
|
74
|
|
|
$rawString, |
|
75
|
|
|
ClauseInterface::NO_OPERATOR, |
|
76
|
|
|
ClauseInterface::NO_VALUE, |
|
77
|
|
|
true |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string|ExpressionInterface $firstTableCol |
|
83
|
|
|
* @param string $operator |
|
84
|
|
|
* @param string $secondTableCol |
|
85
|
|
|
* @param bool $or |
|
86
|
|
|
* @return $this |
|
87
|
|
|
* @access protected |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function realOn( |
|
90
|
|
|
$firstTableCol, |
|
91
|
|
|
/*# string */ $operator = ClauseInterface::NO_OPERATOR, |
|
92
|
|
|
/*# string */ $secondTableCol = ClauseInterface::NO_VALUE, |
|
93
|
|
|
/*# bool */ $or = false |
|
94
|
|
|
) { |
|
95
|
|
|
if (is_object($firstTableCol)) { |
|
96
|
|
|
$on = [$or, $firstTableCol]; |
|
97
|
|
|
} elseif (ClauseInterface::NO_OPERATOR === $operator) { |
|
98
|
|
|
$on = [$or, $firstTableCol, '=', $firstTableCol]; |
|
99
|
|
|
} elseif (ClauseInterface::NO_VALUE === $secondTableCol) { |
|
100
|
|
|
$on = [$or, $firstTableCol, '=', $operator]; |
|
101
|
|
|
} else { |
|
102
|
|
|
$on = [$or, $firstTableCol, $operator, $secondTableCol]; |
|
103
|
|
|
} |
|
104
|
|
|
$clause = &$this->getClause('ON'); |
|
105
|
|
|
$clause[] = $on; |
|
106
|
|
|
|
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Build ON |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $prefix |
|
114
|
|
|
* @param array $settings |
|
115
|
|
|
* @return string |
|
116
|
|
|
* @access protected |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function buildOn( |
|
119
|
|
|
/*# string */ $prefix, |
|
120
|
|
|
array $settings |
|
121
|
|
|
)/*# : string */ { |
|
122
|
|
|
$result = []; |
|
123
|
|
|
$clause = &$this->getClause('ON'); |
|
124
|
|
|
foreach ($clause as $idx => $on) { |
|
125
|
|
|
$res = []; |
|
126
|
|
|
if (is_object($on[1])) { |
|
127
|
|
|
$res[] = $this->quoteItem($on[1], $settings); |
|
128
|
|
|
} else { |
|
129
|
|
|
$res[] = $this->quote($on[1], $settings); // first col |
|
130
|
|
|
$res[] = $on[2]; // operator |
|
131
|
|
|
$res[] = $this->quote($on[3], $settings); // second col |
|
132
|
|
|
} |
|
133
|
|
|
$result[] = $this->onOrAnd($idx, $on[0]) . join(' ', $res); |
|
134
|
|
|
} |
|
135
|
|
|
return trim($this->joinClause($prefix, '', $result, $settings)); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Prepend OR or AND |
|
140
|
|
|
* |
|
141
|
|
|
* @param int $index |
|
142
|
|
|
* @param bool $or |
|
143
|
|
|
* @return string |
|
144
|
|
|
* @access protected |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function onOrAnd(/*# int */ $index, /*# bool */ $or) |
|
147
|
|
|
{ |
|
148
|
|
|
return $index ? ($or ? 'OR ' : 'AND ' ) : ''; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|