001.
<?php
002.
003.
namespace classes;
004.
005.
class
table
006.
{
007.
use
config, query {
008.
query::__construct
as
private
__queryConstruct;
009.
}
010.
011.
protected
$table
;
012.
protected
$commands
;
013.
014.
public
function
__construct(
$table
) {
015.
$this
->table =
$table
;
016.
$this
->commands = [];
017.
$this
->__queryConstruct(
$this
->configuration());
018.
}
019.
020.
public
function
all() {
021.
$this
->add([
022.
'condition'
=> false,
023.
'command'
=>
"select * from {$this->table};"
,
024.
'values'
=> []
025.
]);
026.
027.
return
$this
->get();
028.
}
029.
030.
public
function
first() {
031.
$this
->add([
032.
'condition'
=> false,
033.
'command'
=>
"select * from {$this->table} limit 1;"
,
034.
'values'
=> []
035.
]);
036.
037.
return
$this
->get();
038.
}
039.
040.
public
function
select(Array
$fields
) {
041.
$implode_fields
= implode (
', '
,
$fields
);
042.
043.
$this
->add([
044.
'condition'
=> false,
045.
'command'
=>
"select {$implode_fields} from {$this->table}"
,
046.
'values'
=> []
047.
]);
048.
049.
return
$this
;
050.
}
051.
052.
053.
public
function
where(
$field
,
$operator
,
$value
) {
054.
$this
->add([
055.
'condition'
=> true,
056.
'command'
=>
"{$field} {$operator} ?"
,
057.
'values'
=> [
$value
]
058.
]);
059.
060.
return
$this
;
061.
}
062.
063.
public
function
orWhere(
$field
,
$operator
,
$value
) {
064.
$this
->add([
065.
'condition'
=> false,
066.
'command'
=>
"or {$field} {$operator} ?"
,
067.
'values'
=> [
$value
]
068.
]);
069.
070.
return
$this
;
071.
}
072.
073.
public
function
whereBetween(
$field
, Array
$values
) {
074.
$this
->add([
075.
'condition'
=> true,
076.
'command'
=>
"{$field} between ? and ?"
,
077.
'values'
=>
$values
078.
]);
079.
080.
return
$this
;
081.
}
082.
083.
public
function
whereNotBetween(
$field
, Array
$values
) {
084.
$this
->add([
085.
'condition'
=> true,
086.
'command'
=>
"{$field} not between ? and ?"
,
087.
'values'
=>
$values
088.
]);
089.
090.
return
$this
;
091.
}
092.
093.
public
function
whereIn(
$field
, Array
$values
) {
094.
$implode_values
= implode (
', '
,
array_map
(
095.
function
() {
096.
return
'?'
;
097.
},
098.
$values
099.
));
100.
101.
$this
->add([
102.
'condition'
=> true,
103.
'command'
=>
"{$field} in ({$implode_values})"
,
104.
'values'
=>
$values
105.
]);
106.
107.
return
$this
;
108.
}
109.
110.
public
function
whereNotIn(
$field
, Array
$values
) {
111.
$implode_values
= implode (
', '
,
array_map
(
112.
function
() {
113.
return
'?'
;
114.
},
115.
$values
116.
));
117.
118.
$this
->add([
119.
'condition'
=> true,
120.
'command'
=>
"{$field} not in ({$implode_values})"
,
121.
'values'
=>
$values
122.
]);
123.
124.
return
$this
;
125.
}
126.
127.
public
function
whereNull(
$field
) {
128.
$this
->add([
129.
'condition'
=> true,
130.
'command'
=>
"{$field} is null"
,
131.
'values'
=> []
132.
]);
133.
134.
return
$this
;
135.
}
136.
137.
public
function
whereNotNull(
$field
) {
138.
$this
->add([
139.
'condition'
=> true,
140.
'command'
=>
"{$field} is not null"
,
141.
'values'
=> []
142.
]);
143.
144.
return
$this
;
145.
}
146.
147.
public
function
join(
$table
,
$field1
,
$operator
,
$field2
) {
148.
$this
->add([
149.
'condition'
=> false,
150.
'command'
=>
"inner join {$table} on {$field1} {$operator} {$field2}"
,
151.
'values'
=> []
152.
]);
153.
154.
return
$this
;
155.
}
156.
157.
public
function
leftJoin(
$table
,
$field1
,
$operator
,
$field2
) {
158.
$this
->add([
159.
'condition'
=> false,
160.
'command'
=>
"left join {$table} on {$field1} {$operator} {$field2}"
,
161.
'values'
=> []
162.
]);
163.
164.
return
$this
;
165.
}
166.
167.
public
function
groupBy(Array
$fields
) {
168.
$implode_fields
= implode (
', '
,
$fields
);
169.
170.
$this
->add([
171.
'condition'
=> false,
172.
'command'
=>
"group by {$implode_fields}"
,
173.
'values'
=> []
174.
]);
175.
176.
return
$this
;
177.
}
178.
179.
public
function
having(
$field
,
$operator
,
$value
) {
180.
$this
->add([
181.
'condition'
=> false,
182.
'command'
=>
"having {$field} {$operator} ?"
,
183.
'values'
=>
$value
184.
]);
185.
186.
return
$this
;
187.
}
188.
189.
public
function
orderBy(
$field
,
$order
) {
190.
$this
->add([
191.
'condition'
=> false,
192.
'command'
=>
"order by {$field} {$order}"
,
193.
'values'
=> []
194.
]);
195.
196.
return
$this
;
197.
}
198.
199.
public
function
get() {
200.
$command
=
$this
->builder();
201.
202.
return
$this
->runQuery(
$command
[0],
$command
[1]);
203.
}
204.
205.
public
function
insert(Array
$fields
) {
206.
}
207.
208.
public
function
update(Array
$fields
) {
209.
}
210.
211.
public
function
delete
() {
212.
}
213.
214.
protected
function
add(
$command
) {
215.
$this
->commands[] = [
216.
'condition'
=>
$command
[
'condition'
],
217.
'command'
=>
$command
[
'command'
],
218.
'values'
=>
$command
[
'values'
]
219.
];
220.
}
221.
222.
protected
function
builder() {
223.
$query
=
''
;
224.
$values
= [];
225.
$first_condition
= false;
226.
227.
foreach
(
$this
->commands
as
$command
) {
228.
if
(
$command
[
'condition'
] == false) {
229.
$query
.=
"{$command['command']} "
;
230.
}
231.
else
{
232.
$condition
= (
$first_condition
) ?
'and'
:
'where'
;
233.
$query
.=
"{$condition} {$command['command']} "
;
234.
$first_condition
= true;
235.
}
236.
237.
$values
=
array_merge
(
$values
,
$command
[
'values'
]);
238.
}
239.
240.
return
[
$query
,
$values
];
241.
}
242.
}