This repository was archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathDatabaseDetector.php
More file actions
233 lines (193 loc) · 6.61 KB
/
Copy pathDatabaseDetector.php
File metadata and controls
233 lines (193 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
namespace srag\DIC\LiveVoting\Database;
use ilDBConstants;
use ilDBInterface;
use ilDBPdoInterface;
use ilDBPdoPostgreSQL;
use ilDBStatement;
use PDO;
use srag\DIC\LiveVoting\Exception\DICException;
use stdClass;
/**
* Class DatabaseDetector
*
* @package srag\DIC\LiveVoting\Database
*/
class DatabaseDetector extends AbstractILIASDatabaseDetector
{
/**
* @var self|null
*/
protected static $instance = null;
/**
* Implements the primaryExistsByFields method from ilDBInterface.
*
* This method was added to address compatibility issues with newer versions
* of ILIAS, specifically ILIAS8 and ILIAS7
* @see https://github.com/ILIAS-eLearning/ILIAS/commit/3d4ccc6a24d5d3e487d9e303a41a7bb12262eb48
*
* @param string $table_name The name of the table.
* @param array $fields An array of fields.
* @return bool A stub return. This should be adjusted based on the actual implementation.
*/
public function primaryExistsByFields(string $table_name, array $fields): bool
{
// TODO: Implement the actual logic for this method.
return false;
}
/**
* @param ilDBInterface $db
*
* @return self
*
* @throws DICException DatabaseDetector only supports ilDBPdoInterface!
*/
public static function getInstance(ilDBInterface $db) : self
{
if (!($db instanceof ilDBPdoInterface)) {
throw new DICException("DatabaseDetector only supports ilDBPdoInterface!");
}
if (self::$instance === null) {
self::$instance = new self($db);
}
return self::$instance;
}
/**
* @inheritDoc
*/
public function createAutoIncrement(string $table_name, string $field) : void
{
$table_name_q = $this->quoteIdentifier($table_name);
$field_q = $this->quoteIdentifier($field);
$seq_name = $table_name . "_seq";
$seq_name_q = $this->quoteIdentifier($seq_name);
switch (true) {
case($this->db instanceof ilDBPdoPostgreSQL):
$this->manipulate('CREATE SEQUENCE IF NOT EXISTS ' . $seq_name_q);
$this->manipulate('ALTER TABLE ' . $table_name_q . ' ALTER COLUMN ' . $field_q . ' TYPE INT, ALTER COLUMN ' . $field_q
. ' SET NOT NULL, ALTER COLUMN ' . $field_q . ' SET DEFAULT nextval(' . $seq_name_q . ')');
break;
default:
$this->manipulate('ALTER TABLE ' . $table_name_q . ' MODIFY COLUMN ' . $field_q . ' INT NOT NULL AUTO_INCREMENT');
break;
}
}
/**
* @inheritDoc
*/
public function createOrUpdateTable(string $table_name, array $columns, array $primary_columns) : void
{
if (!$this->tableExists($table_name)) {
$this->createTable($table_name, $columns);
if (!empty($primary_columns)) {
$this->addPrimaryKey($table_name, $primary_columns);
}
} else {
foreach ($columns as $column_name => $column) {
if (!$this->tableColumnExists($table_name, $column_name)) {
$this->addTableColumn($table_name, $column_name, $column);
}
}
}
}
/**
* @inheritDoc
*/
public function dropAutoIncrementTable(string $table_name) : void
{
$seq_name = $table_name . "_seq";
$seq_name_q = $this->quoteIdentifier($seq_name);
switch (true) {
case($this->db instanceof ilDBPdoPostgreSQL):
$this->manipulate('DROP SEQUENCE IF EXISTS ' . $seq_name_q);
break;
default:
// Nothing to do in MySQL
break;
}
}
/**
* @inheritDoc
*/
public function fetchAllCallback(ilDBStatement $stm, callable $callback) : array
{
return array_map($callback, $this->fetchAllClass($stm, stdClass::class));
}
/**
* @inheritDoc
*/
public function fetchAllClass(ilDBStatement $stm, string $class_name) : array
{
return PdoStatementContextHelper::getPdoStatement($stm)->fetchAll(PDO::FETCH_CLASS, $class_name);
}
/**
* @inheritDoc
*/
public function fetchObjectCallback(ilDBStatement $stm, callable $callback) : ?object
{
$data = $this->fetchObjectClass($stm, stdClass::class);
if ($data !== null) {
return $callback($data);
} else {
return null;
}
}
/**
* @inheritDoc
*/
public function fetchObjectClass(ilDBStatement $stm, string $class_name) : ?object
{
$data = PdoStatementContextHelper::getPdoStatement($stm)->fetchObject($class_name);
if ($data !== false) {
return $data;
} else {
return null;
}
}
/**
* @inheritDoc
*/
public function multipleInsert(string $table_name, array $columns, array $values) : void
{
if (empty($columns) || empty($values)) {
return;
}
$this->manipulate('INSERT INTO ' . $this->quoteIdentifier($table_name) . ' (' . implode(',', $columns) . ') VALUES ' . implode(',', array_map(function (array $values2) : string {
return '(' . implode(',', array_map(function (array $value) : string {
return $this->quote($value[0], $value[1]);
}, $values2)) . ')';
}, $values)));
}
/**
* @inheritDoc
*/
public function resetAutoIncrement(string $table_name, string $field) : void
{
$table_name_q = $this->quoteIdentifier($table_name);
$field_q = $this->quoteIdentifier($field);
switch (true) {
case($this->db instanceof ilDBPdoPostgreSQL):
$this->manipulate('SELECT setval(' . $table_name_q . ', (SELECT MAX(' . $field_q . ') FROM ' . $table_name_q . '))');
break;
default:
$this->manipulate('ALTER TABLE ' . $table_name_q
. ' AUTO_INCREMENT=1'); // 1 has the effect MySQL will automatic calculate next max id
break;
}
}
/**
* @inheritDoc
*/
public function store(string $table_name, array $values, string $primary_key_field,/*?*/ int $primary_key_value = 0) : int
{
if (empty($primary_key_value)) {
$this->insert($table_name, $values);
return $this->getLastInsertId();
} else {
$this->update($table_name, $values, [
$primary_key_field => [ilDBConstants::T_INTEGER, $primary_key_value]
]);
return $primary_key_value;
}
}
}