Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.00% |
16 / 20 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| common_persistence_sql_MultipleOperations | |
80.00% |
16 / 20 |
|
0.00% |
0 / 2 |
6.29 | |
0.00% |
0 / 1 |
| insertMultiple | |
94.12% |
16 / 17 |
|
0.00% |
0 / 1 |
4.00 | |||
| updateMultiple | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 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 | * @author "Jérôme Bogaerts, <jerome@taotesting.com>" |
| 21 | * @license GPLv2 |
| 22 | * @package generis |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | trait common_persistence_sql_MultipleOperations |
| 27 | { |
| 28 | /** @var common_persistence_sql_UpdateMultiple */ |
| 29 | private $updateMultiple = null; |
| 30 | |
| 31 | public function insertMultiple($tableName, array $data, array $types = []) |
| 32 | { |
| 33 | if (is_array($data) && count($data) > 0) { |
| 34 | $platform = $this->getPlatform(); |
| 35 | |
| 36 | $quotedColumnIdentifiers = array_map( |
| 37 | function ($value) use ($platform) { |
| 38 | return $platform->quoteIdentifier($value); |
| 39 | }, |
| 40 | array_keys($data[0]) |
| 41 | ); |
| 42 | |
| 43 | $query = "INSERT INTO ${tableName} (" . implode(', ', $quotedColumnIdentifiers) . ') VALUES '; |
| 44 | $valuesQueries = []; |
| 45 | $allValues = []; |
| 46 | |
| 47 | foreach ($data as $values) { |
| 48 | $valuesQueries[] .= '(' . implode(', ', array_fill(0, count($values), '?')) . ')'; |
| 49 | $allValues = array_merge($allValues, array_values($values)); |
| 50 | } |
| 51 | |
| 52 | $query .= implode(', ', $valuesQueries); |
| 53 | |
| 54 | return $this->exec($query, $allValues, $types); |
| 55 | } else { |
| 56 | return 0; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @example |
| 62 | * 'table_name' |
| 63 | * 'data' => |
| 64 | * [ |
| 65 | * [ |
| 66 | * 'conditions' => [ |
| 67 | * 'c1' => 'c1value', |
| 68 | * 'c2' => 'c2value' |
| 69 | * ] |
| 70 | * 'updateValues' => [ |
| 71 | * 'c3' => 'c3value' |
| 72 | * ] |
| 73 | * ], |
| 74 | * [ |
| 75 | * 'conditions' => [ |
| 76 | * 'c1' => 'c1value', |
| 77 | * 'c2' => 'c2value', |
| 78 | * 'c3' => 'c3value', |
| 79 | * ] |
| 80 | * 'updateValues' => [ |
| 81 | * 'c9' => 'c8value' |
| 82 | * ] |
| 83 | * ] |
| 84 | * ] |
| 85 | * |
| 86 | * @param string $table |
| 87 | * @param array $data |
| 88 | * @return bool |
| 89 | * @throws Exception |
| 90 | */ |
| 91 | public function updateMultiple($table, array $data) |
| 92 | { |
| 93 | if ($this->updateMultiple === null) { |
| 94 | $this->updateMultiple = new common_persistence_sql_UpdateMultiple($this->getDbalConnection()); |
| 95 | } |
| 96 | |
| 97 | return $this->updateMultiple->updateMultiple($table, $data); |
| 98 | } |
| 99 | } |