Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 42 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| PaginatedSqlStorage | |
0.00% |
0 / 42 |
|
0.00% |
0 / 6 |
380 | |
0.00% |
0 / 1 |
| find | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| findPage | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| count | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| delete | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| select | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| query | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2017 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoClientDiagnostic\model\storage; |
| 23 | |
| 24 | use Doctrine\DBAL\DBALException; |
| 25 | use Doctrine\DBAL\Driver\PDOStatement; |
| 26 | use oat\taoClientDiagnostic\exception\StorageException; |
| 27 | |
| 28 | /** |
| 29 | * Sql Implementation that allows to return the data paginated |
| 30 | */ |
| 31 | class PaginatedSqlStorage extends Sql implements PaginatedStorage |
| 32 | { |
| 33 | /** |
| 34 | * Gets an existing record in database by id |
| 35 | * |
| 36 | * @param $id |
| 37 | * @return mixed |
| 38 | * @throws StorageException |
| 39 | */ |
| 40 | public function find($id) |
| 41 | { |
| 42 | if (empty($id)) { |
| 43 | throw new StorageException('Invalid id parameter.'); |
| 44 | } |
| 45 | |
| 46 | try { |
| 47 | return $this->select(null, [self::DIAGNOSTIC_ID => $id], 1)->fetch(\PDO::FETCH_ASSOC); |
| 48 | } catch (DBALException $e) { |
| 49 | throw new StorageException($e->getMessage()); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Gets a page from the storage model based on entity |
| 55 | * @param int $page The page number |
| 56 | * @param int $size The size of a page (number of rows) |
| 57 | * @param array $filter A list of filters (pairs columns => value) |
| 58 | * @return mixed |
| 59 | * @throws StorageException |
| 60 | */ |
| 61 | public function findPage($page = null, $size = self::PAGE_SIZE, $filter = null) |
| 62 | { |
| 63 | try { |
| 64 | $offset = ($page - 1) * $size; |
| 65 | return $this->select(null, $filter, $size, $offset)->fetchAll(\PDO::FETCH_ASSOC); |
| 66 | } catch (DBALException $e) { |
| 67 | throw new StorageException($e->getMessage()); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Gets the number of rows from the storage model based on entity |
| 73 | * @param array $filter A list of filters (pairs columns => value) |
| 74 | * @return int |
| 75 | * @throws StorageException |
| 76 | */ |
| 77 | public function count($filter = null) |
| 78 | { |
| 79 | try { |
| 80 | return $this->select('COUNT(*)', $filter)->fetchColumn(); |
| 81 | } catch (DBALException $e) { |
| 82 | throw new StorageException($e->getMessage()); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Deletes a row from the storage model based on entity |
| 88 | * @param $id |
| 89 | * @param array $filter A list of filters (pairs columns => value) |
| 90 | * @return bool |
| 91 | * @throws StorageException |
| 92 | */ |
| 93 | public function delete($id, $filter = null) |
| 94 | { |
| 95 | if (empty($id)) { |
| 96 | throw new StorageException('Invalid id parameter.'); |
| 97 | } |
| 98 | |
| 99 | if (!$filter) { |
| 100 | $filter = []; |
| 101 | } |
| 102 | |
| 103 | $filter[self::DIAGNOSTIC_ID] = $id; |
| 104 | |
| 105 | try { |
| 106 | \common_Logger::i('Deleting diagnostic result ' . $id); |
| 107 | $query = 'DELETE FROM ' . self::DIAGNOSTIC_TABLE; |
| 108 | return (bool) $this->query($query, $filter)->rowCount(); |
| 109 | } catch (DBALException $e) { |
| 110 | throw new StorageException($e->getMessage()); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Builds and runs a select query |
| 116 | * @param array $columns |
| 117 | * @param array $where |
| 118 | * @param int $size |
| 119 | * @param int $offset |
| 120 | * @return PDOStatement |
| 121 | */ |
| 122 | protected function select($columns = null, $where = null, $size = null, $offset = null) |
| 123 | { |
| 124 | if (!$columns) { |
| 125 | $columns = '*'; |
| 126 | } |
| 127 | if (!is_array($columns)) { |
| 128 | $columns = [$columns]; |
| 129 | } |
| 130 | |
| 131 | $query = 'SELECT ' . implode(',', $columns) . ' FROM ' . self::DIAGNOSTIC_TABLE; |
| 132 | |
| 133 | return $this->query($query, $where, $size, $offset); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Builds and runs a query |
| 138 | * @param string $query |
| 139 | * @param array $where |
| 140 | * @param int $size |
| 141 | * @param int $offset |
| 142 | * @return PDOStatement |
| 143 | */ |
| 144 | protected function query($query, $where = null, $size = null, $offset = null) |
| 145 | { |
| 146 | $params = []; |
| 147 | |
| 148 | if (is_array($where)) { |
| 149 | $conditions = []; |
| 150 | foreach ($where as $column => $value) { |
| 151 | $placeholder = '?'; |
| 152 | $value = '' . $value; |
| 153 | |
| 154 | $conditions[] = $column . ' = ' . $placeholder; |
| 155 | $params[] = $value; |
| 156 | } |
| 157 | if (count($conditions)) { |
| 158 | $query .= ' WHERE ' . implode(' AND ', $conditions); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (!is_null($size)) { |
| 163 | $query = $this->getPersistence()->getPlatForm()->limitStatement($query, $size, $offset); |
| 164 | } |
| 165 | |
| 166 | return $this->getPersistence()->query($query, $params); |
| 167 | } |
| 168 | } |