forked from RhubarbPHP/Module.Stem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPdoRepository.php
More file actions
243 lines (196 loc) · 7.38 KB
/
Copy pathPdoRepository.php
File metadata and controls
243 lines (196 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/*
* Copyright 2015 RhubarbPHP
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Rhubarb\Stem\Repositories;
require_once __DIR__ . '/Repository.php';
use Rhubarb\Crown\Logging\Log;
use Rhubarb\Stem\Collections\Collection;
use Rhubarb\Stem\Exceptions\RepositoryConnectionException;
use Rhubarb\Stem\Exceptions\RepositoryStatementException;
use Rhubarb\Stem\Models\Model;
use Rhubarb\Stem\StemSettings;
abstract class PdoRepository extends Repository
{
protected static $lastStatement = "";
protected static $lastParams = [];
protected static $secondLastStatement = "";
protected $lastSortsUsed = [];
/**
* The default connection to use if an explicit connection isn't passed in.
*
* @var /PDO
*/
protected static $defaultConnection = null;
/**
* Return's the default connection.
*/
public static function getDefaultConnection()
{
if (self::$defaultConnection === null) {
$db = new StemSettings();
self::$defaultConnection = static::getConnection($db);
}
return self::$defaultConnection;
}
/**
* @param StemSettings $dbSettings
* @return \PDO
* @throws \Rhubarb\Stem\Exceptions\RepositoryConnectionException
*/
public static function getConnection(StemSettings $dbSettings)
{
throw new RepositoryConnectionException("This repository has no getConnection() implementation.");
}
/**
* Discards the default connection.
*/
public static function resetDefaultConnection()
{
self::$defaultConnection = null;
}
/**
* A collection of PDO objects for each active connection.
*
* @var /PDO[]
*/
protected static $connections = array();
/**
* Returns the last SQL statement executed.
*
* Used by unit tests to ensure performance optimisations have taken effect.
*/
public static function getPreviousStatement($secondLast = false)
{
return ($secondLast) ? self::$secondLastStatement : self::$lastStatement;
}
/**
* Returns the last SQL parameters used.
*
* Used by unit tests to ensure interactions with the database are correct.
*/
public static function getPreviousParameters()
{
return self::$lastParams;
}
public function canFilterExclusivelyByRepository(Collection $collection, &$namedParams = [], &$propertiesToAutoHydrate = [])
{
$filteredExclusivelyByRepository = true;
$filter = $collection->getFilter();
if ($filter !== null) {
$filter->filterWithRepository($this, $namedParams, $propertiesToAutoHydrate);
$filteredExclusivelyByRepository = $filter->wasFilteredByRepository();
}
return $filteredExclusivelyByRepository;
}
public function reHydrateObject(Model $object, $uniqueIdentifier)
{
unset($this->cachedObjectData[$uniqueIdentifier]);
$this->hydrateObject($object, $uniqueIdentifier);
}
protected function getManualSortsRequiredForList(Collection $list)
{
$sorts = $list->getSorts();
$sorts = array_diff_key($sorts, array_flip($this->lastSortsUsed));
return $sorts;
}
/**
* Executes the statement with any supplied named parameters on the connection provided.
*
* If no connection is provided the default connection will be used.
*
* @param $statement
* @param array $namedParameters
* @param \PDO $connection
* @param bool $isInsertQuery True if the query is an insert and the ID should be returned
* @throws \Rhubarb\Stem\Exceptions\RepositoryStatementException
* @return \PDOStatement
*/
public static function executeStatement($statement, $namedParameters = array(), $connection = null, $isInsertQuery = false)
{
if ($connection === null) {
$connection = static::getDefaultConnection();
}
self::$secondLastStatement = self::$lastStatement;
self::$lastStatement = $statement;
self::$lastParams = $namedParameters;
$pdoStatement = $connection->prepare($statement);
Log::CreateEntry(Log::PERFORMANCE_LEVEL | Log::REPOSITORY_LEVEL, function () use ($statement, $namedParameters, $connection) {
$newStatement = $statement;
array_walk($namedParameters, function ($value, $key) use (&$newStatement, &$params, $connection) {
// Note this is not attempting to make secure queries - this is purely illustrative for the logs
// However we do at least do addslashes so if you want to cut and paste a query from the log to
// try it - it should work in most cases.
$newStatement = str_replace(':' . $key, $connection->quote($value), $newStatement);
});
return "Executing PDO statement " . $newStatement;
}, "PDO");
if (!$pdoStatement->execute($namedParameters)) {
$error = $pdoStatement->errorInfo();
throw new RepositoryStatementException($error[2], $statement);
}
if ($isInsertQuery) {
$pdoStatement = $connection->lastInsertId();
}
Log::CreateEntry(Log::PERFORMANCE_LEVEL | Log::REPOSITORY_LEVEL, "Statement successful", "PDO");
return $pdoStatement;
}
public static function executeInsertStatement($sql, $namedParameters = [], $connection = null)
{
return self::executeStatement($sql, $namedParameters, $connection, true);
}
/**
* Checks if raw repository data needs transformed before passing to the model.
*
* @param $modelData
* @return mixed
*/
protected function transformDataFromRepository($modelData)
{
foreach ($this->columnTransforms as $columnName => $transforms) {
if ($transforms[0] !== null) {
$closure = $transforms[0];
$modelData[$columnName] = $closure($modelData[$columnName]);
}
}
return $modelData;
}
/**
* Executes the statement and returns the first column of the first row.
*
* @param $statement
* @param array $namedParameters
* @param null $connection
* @return string
*/
public static function returnSingleValue($statement, $namedParameters = array(), $connection = null)
{
$statement = self::executeStatement($statement, $namedParameters, $connection);
return $statement->fetchColumn(0);
}
/**
* Returns the first row of results from the statement
*
* @param $statement
* @param array $namedParameters
* @param null $connection
* @return string
*/
public static function returnFirstRow($statement, $namedParameters = array(), $connection = null)
{
$statement = self::executeStatement($statement, $namedParameters, $connection);
return $statement->fetch(\PDO::FETCH_ASSOC);
}
}