Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.83% |
55 / 58 |
|
84.62% |
11 / 13 |
CRAP | |
0.00% |
0 / 1 |
| core_kernel_persistence_smoothsql_SmoothRdf | |
94.83% |
55 / 58 |
|
84.62% |
11 / 13 |
22.07 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPersistence | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| search | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| add | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
3 | |||
| addTripleCollection | |
75.00% |
6 / 8 |
|
0.00% |
0 / 1 |
4.25 | |||
| insertTriples | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| insertValues | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| remove | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| getIterator | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getModel | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| tripleToValue | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| getTripleParameterTypes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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-2020 (original work) Open Assessment Technologies SA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | declare(strict_types=1); |
| 23 | |
| 24 | use Doctrine\DBAL\ParameterType; |
| 25 | use oat\generis\model\data\Ontology; |
| 26 | use oat\generis\model\data\RdfInterface; |
| 27 | |
| 28 | /** |
| 29 | * Implementation of the RDF interface for the smooth sql driver |
| 30 | * |
| 31 | * @author joel bout <joel@taotesting.com> |
| 32 | * @package generis |
| 33 | */ |
| 34 | class core_kernel_persistence_smoothsql_SmoothRdf implements RdfInterface |
| 35 | { |
| 36 | public const BATCH_SIZE = 100; |
| 37 | |
| 38 | public const TRIPLE_PARAMETER_TYPE = [ |
| 39 | // modelid |
| 40 | ParameterType::INTEGER, |
| 41 | // subject |
| 42 | ParameterType::STRING, |
| 43 | // predicate |
| 44 | ParameterType::STRING, |
| 45 | // object |
| 46 | ParameterType::STRING, |
| 47 | // l_language |
| 48 | ParameterType::STRING, |
| 49 | // epoch |
| 50 | ParameterType::STRING, |
| 51 | // author |
| 52 | ParameterType::STRING, |
| 53 | ]; |
| 54 | |
| 55 | /** |
| 56 | * @var core_kernel_persistence_smoothsql_SmoothModel |
| 57 | */ |
| 58 | private $model; |
| 59 | |
| 60 | public function __construct(core_kernel_persistence_smoothsql_SmoothModel $model) |
| 61 | { |
| 62 | $this->model = $model; |
| 63 | } |
| 64 | |
| 65 | protected function getPersistence() |
| 66 | { |
| 67 | return $this->model->getPersistence(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * (non-PHPdoc) |
| 72 | * @see \oat\generis\model\data\RdfInterface::get() |
| 73 | */ |
| 74 | public function get($subject, $predicate) |
| 75 | { |
| 76 | throw new \common_Exception('Not implemented'); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * (non-PHPdoc) |
| 81 | * @see \oat\generis\model\data\RdfInterface::search() |
| 82 | */ |
| 83 | public function search($predicate, $object) |
| 84 | { |
| 85 | throw new \common_Exception('Not implemented'); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * (non-PHPdoc) |
| 90 | * @see \oat\generis\model\data\RdfInterface::add() |
| 91 | */ |
| 92 | public function add(core_kernel_classes_Triple $triple) |
| 93 | { |
| 94 | $query = "INSERT INTO statements ( modelId, subject, predicate, object, l_language, epoch, author) " |
| 95 | . "VALUES ( ? , ? , ? , ? , ? , ?, ?);"; |
| 96 | |
| 97 | return $this->getPersistence()->exec( |
| 98 | $query, |
| 99 | [ |
| 100 | $triple->modelid, |
| 101 | $triple->subject, |
| 102 | $triple->predicate, |
| 103 | $triple->object, |
| 104 | is_null($triple->lg) ? '' : $triple->lg, |
| 105 | $this->getPersistence()->getPlatForm()->getNowExpression(), |
| 106 | is_null($triple->author) ? '' : $triple->author |
| 107 | ], |
| 108 | $this->getTripleParameterTypes() |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @inheritDoc |
| 114 | */ |
| 115 | public function addTripleCollection(iterable $triples) |
| 116 | { |
| 117 | $valuesToInsert = []; |
| 118 | |
| 119 | foreach ($triples as $triple) { |
| 120 | $valuesToInsert [] = $triple; |
| 121 | |
| 122 | if (count($valuesToInsert) >= self::BATCH_SIZE) { |
| 123 | $this->insertTriples($valuesToInsert); |
| 124 | $valuesToInsert = []; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (!empty($valuesToInsert)) { |
| 129 | $this->insertTriples($valuesToInsert); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | protected function insertTriples(array $triples) |
| 134 | { |
| 135 | $values = array_map([$this,"tripleToValue"], $triples); |
| 136 | return $this->insertValues($values); |
| 137 | } |
| 138 | |
| 139 | protected function insertValues(array $valuesToInsert) |
| 140 | { |
| 141 | $types = []; |
| 142 | foreach ($valuesToInsert as $value) { |
| 143 | array_push($types, ...$this->getTripleParameterTypes()); |
| 144 | } |
| 145 | |
| 146 | return $this->getPersistence()->insertMultiple('statements', $valuesToInsert, $types); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * (non-PHPdoc) |
| 151 | * @see \oat\generis\model\data\RdfInterface::remove() |
| 152 | */ |
| 153 | public function remove(core_kernel_classes_Triple $triple) |
| 154 | { |
| 155 | $query = "DELETE FROM statements WHERE subject = ? AND predicate = ? AND object = ? AND l_language = ?;"; |
| 156 | return $this->getPersistence()->exec( |
| 157 | $query, |
| 158 | [ |
| 159 | $triple->subject, |
| 160 | $triple->predicate, |
| 161 | $triple->object, |
| 162 | is_null($triple->lg) ? '' : $triple->lg |
| 163 | ] |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | public function getIterator() |
| 168 | { |
| 169 | return new core_kernel_persistence_smoothsql_SmoothIterator( |
| 170 | $this->getPersistence(), |
| 171 | array_diff($this->model->getReadableModels(), ['1']) |
| 172 | ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * @return Ontology |
| 177 | */ |
| 178 | protected function getModel() |
| 179 | { |
| 180 | return $this->model; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @param core_kernel_classes_Triple $triple |
| 185 | * @return array |
| 186 | */ |
| 187 | protected function tripleToValue(core_kernel_classes_Triple $triple): array |
| 188 | { |
| 189 | return [ |
| 190 | 'modelid' => $triple->modelid, |
| 191 | 'subject' => $triple->subject, |
| 192 | 'predicate' => $triple->predicate, |
| 193 | 'object' => $triple->object, |
| 194 | 'l_language' => is_null($triple->lg) ? '' : $triple->lg, |
| 195 | 'author' => is_null($triple->author) ? '' : $triple->author, |
| 196 | 'epoch' => $this->getPersistence()->getPlatForm()->getNowExpression() |
| 197 | ]; |
| 198 | } |
| 199 | |
| 200 | protected function getTripleParameterTypes(): array |
| 201 | { |
| 202 | return self::TRIPLE_PARAMETER_TYPE; |
| 203 | } |
| 204 | } |