Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 61 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| common_persistence_sql_UpdateMultiple | |
0.00% |
0 / 61 |
|
0.00% |
0 / 7 |
380 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| updateMultiple | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| buildQuery | |
0.00% |
0 / 47 |
|
0.00% |
0 / 1 |
90 | |||
| assertValidRow | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
12 | |||
| extractWhensConditions | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| extractValuesToUpdate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| extractConditions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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 (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | use Doctrine\DBAL\Connection; |
| 22 | |
| 23 | class common_persistence_sql_UpdateMultiple |
| 24 | { |
| 25 | /** @var Connection */ |
| 26 | private $dbalConnection; |
| 27 | |
| 28 | /** |
| 29 | * @param Connection $dbalConnection |
| 30 | */ |
| 31 | public function __construct(Connection $dbalConnection) |
| 32 | { |
| 33 | $this->dbalConnection = $dbalConnection; |
| 34 | } |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * @example |
| 39 | * 'table_name' |
| 40 | * 'data' => |
| 41 | * [ |
| 42 | * [ |
| 43 | * 'conditions' => [ |
| 44 | * 'c1' => 'c1value', |
| 45 | * 'c2' => 'c2value' |
| 46 | * ] |
| 47 | * 'updateValues' => [ |
| 48 | * 'c3' => 'c3value' |
| 49 | * ] |
| 50 | * ], |
| 51 | * [ |
| 52 | * 'conditions' => [ |
| 53 | * 'c1' => 'c1value', |
| 54 | * 'c2' => 'c2value', |
| 55 | * 'c3' => 'c3value', |
| 56 | * ] |
| 57 | * 'updateValues' => [ |
| 58 | * 'c9' => 'c8value' |
| 59 | * ] |
| 60 | * ] |
| 61 | * ] |
| 62 | * |
| 63 | * @param string $table |
| 64 | * @param array $data |
| 65 | * @return bool |
| 66 | * @throws Exception |
| 67 | */ |
| 68 | public function updateMultiple($table, array $data) |
| 69 | { |
| 70 | list($query, $params) = $this->buildQuery($table, $data); |
| 71 | |
| 72 | $stmt = $this->dbalConnection->prepare($query); |
| 73 | foreach ($params as $param => $value) { |
| 74 | $stmt->bindValue($param, $value); |
| 75 | } |
| 76 | |
| 77 | return $stmt->execute($params); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @param $table |
| 82 | * @param array $data |
| 83 | * @return array|bool |
| 84 | * @throws Exception |
| 85 | */ |
| 86 | public function buildQuery($table, array $data) |
| 87 | { |
| 88 | if (empty($data)) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | $prepareQueryData = []; |
| 93 | $allColumns = []; |
| 94 | $params = []; |
| 95 | |
| 96 | foreach ($data as $row) { |
| 97 | $this->assertValidRow($row); |
| 98 | $conditions = $this->extractConditions($row); |
| 99 | $updateValues = $this->extractValuesToUpdate($row); |
| 100 | |
| 101 | foreach ($updateValues as $updateColumn => $updateValue) { |
| 102 | $prepareQueryData[$updateColumn][] = [ |
| 103 | 'value' => $updateValue, |
| 104 | 'conditions' => $this->extractWhensConditions($conditions) |
| 105 | ]; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | $queryColumns = []; |
| 110 | foreach ($prepareQueryData as $column => $queryData) { |
| 111 | $queryColumnUpdate = " $column = ( CASE "; |
| 112 | |
| 113 | foreach ($queryData as $index => $datum) { |
| 114 | $conditions = $datum['conditions']; |
| 115 | $updateValue = $datum['value']; |
| 116 | $conditionsString = []; |
| 117 | |
| 118 | foreach ($conditions as $indexCondition => $condition) { |
| 119 | $conditionColumn = $condition['conditionColumn']; |
| 120 | $conditionValue = $condition['conditionValue']; |
| 121 | |
| 122 | $key = ':' . $index . '_' . $column . '_' . $indexCondition . '_' . $conditionColumn |
| 123 | . '_conditionvalue'; |
| 124 | $conditionsString[] = " $conditionColumn = $key "; |
| 125 | $allColumns[$conditionColumn][] = $conditionValue; |
| 126 | $params[$key] = $conditionValue; |
| 127 | } |
| 128 | |
| 129 | $key = ':' . $index . '_' . $column . '_updatedvalue'; |
| 130 | $queryColumnUpdate .= " WHEN " . implode(' AND ', $conditionsString) . " THEN $key"; |
| 131 | $params[$key] = $updateValue; |
| 132 | } |
| 133 | |
| 134 | $queryColumnUpdate .= " ELSE $column END)"; |
| 135 | $queryColumns[] = $queryColumnUpdate; |
| 136 | } |
| 137 | |
| 138 | $query = 'UPDATE ' . $table . ' SET ' . implode(', ', $queryColumns); |
| 139 | $wheres = []; |
| 140 | |
| 141 | foreach ($allColumns as $columnWhere => $columnWhereValues) { |
| 142 | $uniqueColumnValues = array_unique($columnWhereValues); |
| 143 | $placeHolders = []; |
| 144 | foreach ($uniqueColumnValues as $index => $value) { |
| 145 | $key = ':in_condition_' . $columnWhere . '_' . $index; |
| 146 | $placeHolders[] = $key; |
| 147 | $params[$key] = $value; |
| 148 | } |
| 149 | $placeHolders = implode(',', $placeHolders); |
| 150 | $wheres[] = " $columnWhere IN ($placeHolders)"; |
| 151 | } |
| 152 | |
| 153 | $query .= ' WHERE ' . implode(' OR ', $wheres); |
| 154 | |
| 155 | return [$query, $params]; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @param $row |
| 160 | * @throws Exception |
| 161 | */ |
| 162 | private function assertValidRow(array $row) |
| 163 | { |
| 164 | if (empty($row['conditions']) || empty($row['updateValues'])) { |
| 165 | throw new Exception('You must provide conditions and updateValues'); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * @param $conditions |
| 171 | * @return array |
| 172 | */ |
| 173 | private function extractWhensConditions($conditions) |
| 174 | { |
| 175 | $whens = []; |
| 176 | foreach ($conditions as $conditionColumn => $conditionValue) { |
| 177 | $whens[] = ['conditionColumn' => $conditionColumn, 'conditionValue' => $conditionValue]; |
| 178 | } |
| 179 | |
| 180 | return $whens; |
| 181 | } |
| 182 | |
| 183 | private function extractValuesToUpdate($row) |
| 184 | { |
| 185 | return $row['updateValues']; |
| 186 | } |
| 187 | |
| 188 | private function extractConditions($row) |
| 189 | { |
| 190 | return $row['conditions']; |
| 191 | } |
| 192 | } |