1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Willry\QueryBuilder; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use PDOStatement; |
7
|
|
|
use stdClass; |
8
|
|
|
|
9
|
|
|
class Query extends Base |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param string $columns |
14
|
|
|
* @return $this |
15
|
|
|
*/ |
16
|
|
|
public function selectRaw(string $columns = "*", array $params = []): self |
17
|
|
|
{ |
18
|
|
|
$this->setBindings($params, 'select'); |
19
|
|
|
$this->columns = array_merge($this->columns, explode(',', $columns)); |
20
|
|
|
return $this; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function select(array $columns = ['*']): self |
24
|
|
|
{ |
25
|
|
|
$this->columns = array_merge($this->columns, $columns); |
26
|
|
|
return $this; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $columnOrder |
32
|
|
|
*/ |
33
|
|
|
public function order(string $columnOrder): self |
34
|
|
|
{ |
35
|
|
|
$this->order = "ORDER BY ?"; |
36
|
|
|
$this->setBindings([$columnOrder], 'order'); |
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param array $group |
43
|
|
|
* @return DB |
44
|
|
|
*/ |
45
|
|
|
public function groupBy(string $group): self |
46
|
|
|
{ |
47
|
|
|
|
48
|
|
|
$this->groupBy = "GROUP BY {$group}"; |
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $having |
54
|
|
|
* @return DB |
55
|
|
|
*/ |
56
|
|
|
public function having(string $having, array $params = []): self |
57
|
|
|
{ |
58
|
|
|
$this->having = "HAVING {$having}"; |
59
|
|
|
$this->setBindings($params, 'having'); |
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function get(): ?array |
64
|
|
|
{ |
65
|
|
|
$this->mountQuery(); |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
$this->type = self::TYPE_SELECT; |
69
|
|
|
|
70
|
|
|
$stmt = $this->db->prepare($this->query); |
71
|
|
|
QueryHelpers::bind($stmt, $this->flatBindings()); |
72
|
|
|
$stmt->execute(); |
73
|
|
|
|
74
|
|
|
return $stmt->fetchAll(\PDO::FETCH_CLASS); |
75
|
|
|
} catch (\PDOException $exception) { |
76
|
|
|
$this->handleError($exception); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function first(): ?\stdClass |
81
|
|
|
{ |
82
|
|
|
$this->mountQuery(); |
83
|
|
|
|
84
|
|
|
try { |
85
|
|
|
$this->type = self::TYPE_SELECT; |
86
|
|
|
|
87
|
|
|
$stmt = $this->db->prepare($this->query); |
88
|
|
|
QueryHelpers::bind($stmt, $this->flatBindings()); |
89
|
|
|
$stmt->execute(); |
90
|
|
|
|
91
|
|
|
if (!$stmt->rowCount()) { |
92
|
|
|
return null; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $stmt->fetchObject(); |
96
|
|
|
} catch (\PDOException $exception) { |
97
|
|
|
$this->handleError($exception); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function count(): ?int |
102
|
|
|
{ |
103
|
|
|
$this->mountQuery(); |
104
|
|
|
|
105
|
|
|
try { |
106
|
|
|
$this->type = self::TYPE_SELECT; |
107
|
|
|
|
108
|
|
|
$stmt = $this->db->prepare($this->query); |
109
|
|
|
QueryHelpers::bind($stmt, $this->flatBindings()); |
110
|
|
|
$stmt->execute(); |
111
|
|
|
|
112
|
|
|
return $stmt->rowCount(); |
113
|
|
|
} catch (\PDOException $exception) { |
114
|
|
|
$this->handleError($exception); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|