1
|
|
|
<?php |
2
|
|
|
namespace JayaCode\Framework\Core\Database\Query\Grammar; |
3
|
|
|
|
4
|
|
|
use JayaCode\Framework\Core\Database\Query\Query; |
5
|
|
|
use Stringy\Stringy; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class GrammarMySql |
9
|
|
|
* @package JayaCode\Framework\Core\Database\Query\Grammar |
10
|
|
|
*/ |
11
|
|
|
class GrammarMySql extends Grammar |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @return string |
16
|
|
|
*/ |
17
|
|
|
public function build() |
18
|
|
|
{ |
19
|
|
|
switch ($this->query->getType()) { |
20
|
|
|
case Query::TYPE_SELECT: |
21
|
|
|
return $this->select(); |
22
|
|
|
|
23
|
|
|
case Query::TYPE_QUERY: |
24
|
|
|
return $this->query->query; |
|
|
|
|
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
private function select() |
32
|
|
|
{ |
33
|
|
|
$this->queryString = "SELECT "; |
34
|
|
|
|
35
|
|
|
$this->queryString .= $this->selectColumn(); |
36
|
|
|
|
37
|
|
|
$this->queryString .= " FROM `{$this->query->table}`"; |
38
|
|
|
|
39
|
|
|
$this->queryString .= $this->where(); |
40
|
|
|
|
41
|
|
|
return $this->queryString; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
private function selectColumn() |
48
|
|
|
{ |
49
|
|
|
$columns = $this->query->columns; |
|
|
|
|
50
|
|
|
if (is_null($columns)) { |
51
|
|
|
$columns = [Query::sql("*")]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (is_string($columns)) { |
55
|
|
|
$columns = array($columns); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
foreach ($columns as $key => $val) { |
59
|
|
|
if ($columns[$key] instanceof Query) { |
60
|
|
|
$columns[$key] = $columns[$key]->query; |
61
|
|
|
} else { |
62
|
|
|
$columns[$key] = "`{$val}`"; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return implode(', ', $columns); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
private function where() |
73
|
|
|
{ |
74
|
|
|
if (count($this->query->where) <= 0) { |
|
|
|
|
75
|
|
|
return ""; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$q = Stringy::create(""); |
79
|
|
|
|
80
|
|
|
foreach ($this->query->where as $where) { |
|
|
|
|
81
|
|
|
$type = $where[0]; |
82
|
|
|
$arr = $where[1]; |
83
|
|
|
|
84
|
|
|
if ($q->count() > 1) { |
85
|
|
|
$q = $q->append(" {$type} "); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (is_string($arr)) { |
89
|
|
|
$q = $q->append($arr); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (is_array($arr)) { |
93
|
|
|
$q = $q->append("`{$arr[0]}`"); |
94
|
|
|
$q = $q->append(" {$arr[1]} "); |
95
|
|
|
$q = $q->append("?"); |
96
|
|
|
|
97
|
|
|
$this->params[] = $arr[2]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $q->count()?$q->prepend(" WHERE ")->__toString():""; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.