1
|
|
|
<?php |
2
|
|
|
namespace JayaCode\Framework\Core\Database\Query; |
3
|
|
|
|
4
|
|
|
use JayaCode\Framework\Core\Database\Query\Grammar\Grammar; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Query |
8
|
|
|
* @package JayaCode\Framework\Core\Database\Query |
9
|
|
|
*/ |
10
|
|
|
class Query |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
const TYPE_QUERY = 'QUERY'; |
16
|
|
|
/** |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
const TYPE_SELECT = 'SELECT'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
public $table; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $attributes = array( |
30
|
|
|
'where' => array(), |
31
|
|
|
'columns' => array() |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $type = "SELECT"; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param null $table |
41
|
|
|
*/ |
42
|
|
|
public function __construct($table = null) |
43
|
|
|
{ |
44
|
|
|
$this->table = $table; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $queryStr |
49
|
|
|
* @return Query |
50
|
|
|
*/ |
51
|
|
|
public static function sql($queryStr, $params = null) |
52
|
|
|
{ |
53
|
|
|
$query = new self(); |
54
|
|
|
if ($params) { |
55
|
|
|
$query->params = $params; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
$query->setType(self::TYPE_QUERY); |
58
|
|
|
$query->query = $queryStr; |
|
|
|
|
59
|
|
|
return $query; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $table |
64
|
|
|
* @return Query |
65
|
|
|
*/ |
66
|
|
|
public static function table($table) |
67
|
|
|
{ |
68
|
|
|
return new self($table); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $columns |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
|
|
public function select($columns = null) |
76
|
|
|
{ |
77
|
|
|
$this->attributes['columns'] = $columns; |
78
|
|
|
$this->type = "SELECT"; |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $query |
84
|
|
|
* @param string $type |
85
|
|
|
* @return Query |
86
|
|
|
*/ |
87
|
|
|
public function whereQ(Query $query, $type = "AND") |
88
|
|
|
{ |
89
|
|
|
if ($query->getType() == Query::TYPE_QUERY) { |
90
|
|
|
$this->attributes['where'][] = array($type, $query->query); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $column |
97
|
|
|
* @param $value |
98
|
|
|
* @param string $operator |
99
|
|
|
* @param string $type |
100
|
|
|
* @return Query |
101
|
|
|
*/ |
102
|
|
|
public function where($column, $value, $operator = "=", $type = "AND") |
103
|
|
|
{ |
104
|
|
|
$this->attributes['where'][] = array($type, array($column, $operator, $value)); |
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $column |
110
|
|
|
* @param $value |
111
|
|
|
* @param string $operator |
112
|
|
|
* @return Query |
113
|
|
|
*/ |
114
|
|
|
public function andWhere($column, $value, $operator = "=") |
115
|
|
|
{ |
116
|
|
|
return $this->where($column, $value, $operator, "AND"); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $column |
121
|
|
|
* @param $value |
122
|
|
|
* @param string $operator |
123
|
|
|
* @return Query |
124
|
|
|
*/ |
125
|
|
|
public function orWhere($column, $value, $operator = "=") |
126
|
|
|
{ |
127
|
|
|
return $this->where($column, $value, $operator, "OR"); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param Grammar $grammar |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
public function build(Grammar $grammar) |
135
|
|
|
{ |
136
|
|
|
$grammar->setQuery($this); |
137
|
|
|
|
138
|
|
|
$queryStr = $grammar->build(); |
139
|
|
|
$queryParams = isset($this->attributes['params'])?$this->attributes['params']:$grammar->getParams(); |
140
|
|
|
|
141
|
|
|
return [$queryStr, $queryParams]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param $name |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
public function __get($name) |
149
|
|
|
{ |
150
|
|
|
return arr_get($this->attributes, $name); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param $name |
155
|
|
|
* @param $value |
156
|
|
|
*/ |
157
|
|
|
public function __set($name, $value) |
158
|
|
|
{ |
159
|
|
|
$this->attributes[$name] = $value; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function getType() |
166
|
|
|
{ |
167
|
|
|
return $this->type; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $type |
172
|
|
|
*/ |
173
|
|
|
public function setType($type) |
174
|
|
|
{ |
175
|
|
|
$this->type = $type; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write 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.