Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
34 / 34 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
SqlCreator | |
100.00% |
34 / 34 |
|
100.00% |
4 / 4 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getExportSql | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getCreateTableSql | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
getInsertSql | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
4 |
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) 2020 (original work) Open Assessment Technologies SA; |
19 | * |
20 | */ |
21 | |
22 | namespace oat\tao\model\export\implementation\sql; |
23 | |
24 | /** |
25 | * Class SqlCreator |
26 | * @package oat\tao\model\export\implementation\sql |
27 | */ |
28 | class SqlCreator |
29 | { |
30 | /** |
31 | * @var ExportedTable |
32 | */ |
33 | private $table; |
34 | |
35 | public function __construct(ExportedTable $table) |
36 | { |
37 | $this->table = $table; |
38 | } |
39 | |
40 | /** |
41 | * @return string |
42 | */ |
43 | public function getExportSql(): string |
44 | { |
45 | $sqlCreateTable = $this->getCreateTableSql(); |
46 | $sqlInsert = $this->getInsertSql(); |
47 | |
48 | return $sqlCreateTable . PHP_EOL . PHP_EOL . $sqlInsert; |
49 | } |
50 | |
51 | /** |
52 | * @return string |
53 | */ |
54 | private function getCreateTableSql(): string |
55 | { |
56 | $columnsCreatingStringArray = []; |
57 | |
58 | foreach ($this->table->getColumns() as $column) { |
59 | $columnsCreatingStringArray[] = $column->getColumnCreatingString(); |
60 | } |
61 | |
62 | return sprintf( |
63 | "CREATE TABLE IF NOT EXISTS %s (%s\t%s%s);", |
64 | $this->table->getTableName(), |
65 | PHP_EOL, |
66 | implode("," . PHP_EOL . "\t", $columnsCreatingStringArray), |
67 | PHP_EOL |
68 | ); |
69 | } |
70 | |
71 | /** |
72 | * @return string |
73 | */ |
74 | private function getInsertSql(): string |
75 | { |
76 | $columnNamesArray = []; |
77 | foreach ($this->table->getColumns() as $column) { |
78 | $columnNamesArray[] = $column->getFormattedName(); |
79 | } |
80 | |
81 | $fieldInsertArray = []; |
82 | foreach ($this->table->getRows() as $row) { |
83 | $rowValuesArray = []; |
84 | |
85 | /**@var $field ExportedField */ |
86 | foreach ($row as $key => $field) { |
87 | $rowValuesArray[] = $field->getFormattedValue(); |
88 | } |
89 | |
90 | $rowValuesString = implode("," . PHP_EOL . "\t ", $rowValuesArray); |
91 | $fieldInsertArray[] = "(" . PHP_EOL . "\t $rowValuesString" . PHP_EOL . "\t)"; |
92 | } |
93 | |
94 | $columnNamesString = implode("," . PHP_EOL . "\t ", $columnNamesArray); |
95 | $fieldInsertString = implode("," . PHP_EOL . "\t", $fieldInsertArray); |
96 | |
97 | return sprintf( |
98 | "INSERT INTO %s (%s\t %s%s) VALUES %s;", |
99 | $this->table->getTableName(), |
100 | PHP_EOL, |
101 | $columnNamesString, |
102 | PHP_EOL, |
103 | $fieldInsertString |
104 | ); |
105 | } |
106 | } |