1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Database\Sql\Queries; |
4
|
|
|
|
5
|
|
|
use Win\Database\RepositoryInterface; |
6
|
|
|
use Win\Database\Sql\Clauses\Limit; |
7
|
|
|
use Win\Database\Sql\Clauses\OrderBy; |
8
|
|
|
use Win\Database\Sql\Clauses\Where; |
9
|
|
|
use Win\Database\Sql\Query; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* SELECT * FROM |
13
|
|
|
*/ |
14
|
|
|
class Select extends Query |
15
|
|
|
{ |
16
|
|
|
/** @var string[] */ |
17
|
|
|
public $columns; |
18
|
|
|
|
19
|
|
|
/** @var Where */ |
20
|
|
|
protected $where; |
21
|
|
|
|
22
|
|
|
/** @var Limit */ |
23
|
|
|
protected $limit; |
24
|
|
|
|
25
|
|
|
/** @var OrderBy */ |
26
|
|
|
protected $orderBy; |
27
|
|
|
|
28
|
|
|
public function __construct(RepositoryInterface $repository) |
29
|
|
|
{ |
30
|
|
|
parent::__construct($repository); |
31
|
|
|
$this->init(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function init() |
35
|
|
|
{ |
36
|
|
|
$this->columns = ['*']; |
37
|
|
|
$this->where = new Where(); |
38
|
|
|
$this->limit = new Limit(); |
39
|
|
|
$this->orderBy = new OrderBy(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** @return string */ |
43
|
|
|
public function toString() |
44
|
|
|
{ |
45
|
|
|
return 'SELECT ' . implode(',', $this->columns) . ' FROM ' |
46
|
|
|
. $this->table |
47
|
|
|
. $this->where |
48
|
|
|
. $this->orderBy |
49
|
|
|
. $this->limit; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** @return mixed[] */ |
53
|
|
|
public function getValues() |
54
|
|
|
{ |
55
|
|
|
return $this->where->values(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** @return int */ |
59
|
|
|
public function count() |
60
|
|
|
{ |
61
|
|
|
$this->columns = ['count(*)']; |
62
|
|
|
$stmt = $this->conn->stmt($this, $this->getValues()); |
63
|
|
|
$this->init(); |
64
|
|
|
|
65
|
|
|
return $stmt->fetchColumn(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** @return mixed[] */ |
69
|
|
|
public function execute() |
70
|
|
|
{ |
71
|
|
|
$all = $this->conn->fetchAll($this, $this->getValues()); |
72
|
|
|
$this->init(); |
73
|
|
|
|
74
|
|
|
return $all; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** @param string $orderBy */ |
78
|
|
|
public function orderBy($orderBy) |
79
|
|
|
{ |
80
|
|
|
$this->orderBy->set($orderBy); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** @return Where */ |
84
|
|
|
public function where() |
85
|
|
|
{ |
86
|
|
|
return $this->where; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** @param int Limit */ |
90
|
|
|
public function limit($limit) |
91
|
|
|
{ |
92
|
|
|
$this->limit->set($limit); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|