|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Trait QueryTrait |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource QueryTrait.php |
|
6
|
|
|
* @created 10.01.2018 |
|
7
|
|
|
* @package chillerlan\Database\Query |
|
8
|
|
|
* @author Smiley <[email protected]> |
|
9
|
|
|
* @copyright 2018 Smiley |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace chillerlan\Database\Query; |
|
14
|
|
|
|
|
15
|
|
|
use chillerlan\Database\Dialects\{ |
|
16
|
|
|
Firebird, MSSQL |
|
17
|
|
|
}; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @extends \chillerlan\Database\Query\StatementAbstract |
|
21
|
|
|
* @implements \chillerlan\Database\Query\Query |
|
22
|
|
|
* |
|
23
|
|
|
* @property \chillerlan\Database\Drivers\DriverInterface $db |
|
24
|
|
|
* @property \chillerlan\Database\Dialects\Dialect $dialect |
|
25
|
|
|
* @property \Psr\Log\LoggerInterface $logger |
|
26
|
|
|
*/ |
|
27
|
|
|
trait QueryTrait{ |
|
28
|
|
|
|
|
29
|
|
|
protected array $sql = []; |
|
30
|
|
|
protected bool $multi = false; |
|
31
|
|
|
protected bool $cached = false; |
|
32
|
|
|
protected int $ttl = 300; |
|
33
|
|
|
protected ?int $limit = null; |
|
34
|
|
|
protected ?int $offset = null; |
|
35
|
|
|
protected array $bindValues = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
|
|
abstract protected function getSQL():array; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param bool|null $multi |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
* @throws \chillerlan\Database\Query\QueryException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function sql(bool $multi = null):string{ |
|
49
|
|
|
$this->multi = $multi ?? false; |
|
50
|
|
|
|
|
51
|
|
|
$sql = trim(implode(' ', $this->getSQL())); |
|
52
|
|
|
|
|
53
|
|
|
// this should only happen on a corrupt dialect implementation |
|
54
|
|
|
if(empty($sql)){ |
|
55
|
|
|
throw new QueryException('empty sql'); // @codeCoverageIgnore |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $sql; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** @inheritdoc */ |
|
62
|
|
|
public function getBindValues():array{ |
|
63
|
|
|
|
|
64
|
|
|
if($this->dialect instanceof Firebird || $this->dialect instanceof MSSQL){ |
|
65
|
|
|
|
|
66
|
|
|
if($this->limit !== null){ |
|
67
|
|
|
$this->bindValues = array_merge([ |
|
68
|
|
|
'limit' => $this->limit, |
|
69
|
|
|
'offset' => $this->offset ?? 0, |
|
70
|
|
|
], $this->bindValues); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
else{ |
|
75
|
|
|
|
|
76
|
|
|
if($this->offset !== null){ |
|
77
|
|
|
$this->bindValues['offset'] = $this->offset; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if($this->limit !== null){ |
|
81
|
|
|
$this->bindValues['limit'] = $this->limit; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $this->bindValues; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** @inheritdoc */ |
|
90
|
|
|
protected function addBindValue(string $key, $value):Statement{ |
|
91
|
|
|
$this->bindValues[$key] = $value; |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** @inheritdoc */ |
|
97
|
|
|
public function limit(int $limit):Statement{ |
|
98
|
|
|
$this->limit = $limit >= 0 ? $limit : 0; |
|
99
|
|
|
|
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** @inheritdoc */ |
|
104
|
|
|
public function offset(int $offset):Statement{ |
|
105
|
|
|
$this->offset = $offset >= 0 ? $offset : 0; |
|
106
|
|
|
|
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** @inheritdoc */ |
|
111
|
|
|
public function cached(int $ttl = null):Statement{ |
|
112
|
|
|
$this->cached = true; |
|
113
|
|
|
|
|
114
|
|
|
if($ttl > 0){ |
|
115
|
|
|
$this->ttl = $ttl; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** @inheritdoc */ |
|
122
|
|
|
public function query(string $index = null){ |
|
123
|
|
|
$sql = $this->sql(false); |
|
124
|
|
|
$bindvalues = $this instanceof BindValues |
|
125
|
|
|
? $this->getBindValues() |
|
126
|
|
|
: null; |
|
127
|
|
|
|
|
128
|
|
|
$this->logger->debug('QueryTrait::query()', ['method' => __METHOD__, 'sql' => $sql, 'val' => $bindvalues, 'index' => $index]); |
|
129
|
|
|
|
|
130
|
|
|
if($this->cached && $this instanceof Select){ |
|
131
|
|
|
return $this->db->preparedCached($sql, $bindvalues, $index, true, $this->ttl); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $this->db->prepared($sql, $bindvalues, $index); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|