ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ilDBPdoManager Class Reference

Class ilDBPdoManager. More...

+ Inheritance diagram for ilDBPdoManager:
+ Collaboration diagram for ilDBPdoManager:

Public Member Functions

 __construct (\PDO $pdo, ilDBPdo $db_instance)
 ilDBPdoManager constructor. More...
 
 getQueryUtils ()
 
 getDBInstance ()
 
 listTables ($database=null)
 
 listSequences ($database=null)
 
 createConstraint ($table, $name, $definition)
 
 createSequence ($seq_name, $start=1, $options=array())
 
 alterTable ($name, $changes, $check)
 
 createTable ($name, $fields, $options=array())
 
 getIndexName ($idx)
 
 getSequenceName ($sqn)
 
 listTableFields ($table)
 
 listTableConstraints ($table)
 
 listTableIndexes ($table)
 
 createIndex ($table, $name, $definition)
 
 dropIndex ($table, $name)
 
 dropSequence ($table_name)
 
 getTableCreationQuery ($name, $fields, $options=array())
 
 dropConstraint ($table, $name, $primary=false)
 
 dropTable ($name)
 
Parameters
$nameTable-name
Returns
mixed
More...
 
 listTables ($database=null)
 
 listSequences ($database=null)
 
 createConstraint ($table, $name, $definition)
 
 listTableFields ($table)
 
 listTableConstraints ($table)
 
 createSequence ($seq_name, $start=1, $options=array())
 
 listTableIndexes ($table)
 
 alterTable ($name, $changes, $check)
 
 createIndex ($table, $name, $definition)
 
 dropIndex ($table, $name)
 
 dropSequence ($seq_name)
 
 dropConstraint ($table, $name, $primary=false)
 
 dropTable ($name)
 
 getIndexName ($idx)
 
 getSequenceName ($sqn)
 

Protected Member Functions

 fixSequenceName ($sqn, $check=false)
 
 fixIndexName ($idx)
 

Protected Attributes

 $pdo
 
 $db_instance
 
 $query_utils
 

Detailed Description

Constructor & Destructor Documentation

◆ __construct()

ilDBPdoManager::__construct ( \PDO  $pdo,
ilDBPdo  $db_instance 
)

ilDBPdoManager constructor.

Parameters
\PDO$pdo
\ilDBPdo$db_instance

Definition at line 27 of file class.ilDBPdoManager.php.

27 {
28 $this->pdo = $pdo;
29 $this->db_instance = $db_instance;
30 }

References $db_instance, and $pdo.

Member Function Documentation

◆ alterTable()

ilDBPdoManager::alterTable (   $name,
  $changes,
  $check 
)
Parameters
$name
$changes
$check
Returns
bool
Exceptions

ilDatabaseException

Implements ilDBManager.

Definition at line 208 of file class.ilDBPdoManager.php.

208 {
209 $db = $this->db_instance;
210
211 foreach ($changes as $change_name => $change) {
212 switch ($change_name) {
213 case 'add':
214 case 'remove':
215 case 'change':
216 case 'rename':
217 case 'name':
218 break;
219 default:
220 throw new ilDatabaseException('change type "' . $change_name . '" not yet supported');
221 }
222 }
223
224 if ($check) {
225 return true;
226 }
227
228 $query = '';
229 if (!empty($changes['name'])) {
230 $change_name = $db->quoteIdentifier($changes['name']);
231 $query .= 'RENAME TO ' . $change_name;
232 }
233
234 if (!empty($changes['add']) && is_array($changes['add'])) {
235 foreach ($changes['add'] as $field_name => $field) {
236 if ($query) {
237 $query .= ', ';
238 }
239 $query .= 'ADD ' . $db->getFieldDefinition()->getDeclaration($field['type'], $field_name, $field);
240 }
241 }
242
243 if (!empty($changes['remove']) && is_array($changes['remove'])) {
244 foreach ($changes['remove'] as $field_name => $field) {
245 if ($query) {
246 $query .= ', ';
247 }
248 $field_name = $db->quoteIdentifier($field_name);
249 $query .= 'DROP ' . $field_name;
250 }
251 }
252
253 $rename = array();
254 if (!empty($changes['rename']) && is_array($changes['rename'])) {
255 foreach ($changes['rename'] as $field_name => $field) {
256 $rename[$field['name']] = $field_name;
257 }
258 }
259
260 if (!empty($changes['change']) && is_array($changes['change'])) {
261 foreach ($changes['change'] as $field_name => $field) {
262 if ($query) {
263 $query .= ', ';
264 }
265 if (isset($rename[$field_name])) {
266 $old_field_name = $rename[$field_name];
267 unset($rename[$field_name]);
268 } else {
269 $old_field_name = $field_name;
270 }
271 $old_field_name = $db->quoteIdentifier($old_field_name);
272 $query .= "CHANGE $old_field_name " . $this->db_instance->getFieldDefinition()
273 ->getDeclaration($field['definition']['type'], $field_name, $field['definition']);
274 }
275 }
276
277 if (!empty($rename) && is_array($rename)) {
278 foreach ($rename as $rename_name => $renamed_field) {
279 if ($query) {
280 $query .= ', ';
281 }
282 $field = $changes['rename'][$renamed_field];
283 $renamed_field = $db->quoteIdentifier($renamed_field);
284 $query .= 'CHANGE ' . $renamed_field . ' ' . $this->db_instance->getFieldDefinition()
285 ->getDeclaration($field['definition']['type'], $field['name'], $field['definition']);
286 }
287 }
288
289 if (!$query) {
290 return true;
291 }
292
293 $name = $db->quoteIdentifier($name, true);
294
295 $statement = "ALTER TABLE $name $query";
296
297 return $this->pdo->exec($statement);
298 }
Class ilDatabaseException.

References $db_instance, and $query.

◆ createConstraint()

ilDBPdoManager::createConstraint (   $table,
  $name,
  $definition 
)
Parameters
$table
$name
$definition
Returns
mixed
Exceptions

ilDatabaseException

Implements ilDBManager.

Definition at line 131 of file class.ilDBPdoManager.php.

131 {
132 $db = $this->db_instance;
133
134 $table = $db->quoteIdentifier($table, true);
135 $name = $db->quoteIdentifier($db->getIndexName($name), true);
136 $query = "ALTER TABLE $table ADD CONSTRAINT $name";
137 if (!empty($definition['primary'])) {
138 $query .= ' PRIMARY KEY';
139 } elseif (!empty($definition['unique'])) {
140 $query .= ' UNIQUE';
141 }
142 $fields = array();
143 foreach (array_keys($definition['fields']) as $field) {
144 $fields[] = $db->quoteIdentifier($field, true);
145 }
146 $query .= ' (' . implode(', ', $fields) . ')';
147
148 return $this->pdo->exec($query);
149 }

References $db_instance, and $query.

◆ createIndex()

ilDBPdoManager::createIndex (   $table,
  $name,
  $definition 
)
Parameters
$table
$name
$definition
Returns
mixed

Implements ilDBManager.

Definition at line 464 of file class.ilDBPdoManager.php.

464 {
465 $table = $this->db_instance->quoteIdentifier($table, true);
466 $name = $this->db_instance->quoteIdentifier($this->db_instance->getIndexName($name), true);
467 $query = "CREATE INDEX $name ON $table";
468 $fields = array();
469 foreach ($definition['fields'] as $field => $fieldinfo) {
470 if (!empty($fieldinfo['length'])) {
471 $fields[] = $this->db_instance->quoteIdentifier($field, true) . '(' . $fieldinfo['length'] . ')';
472 } else {
473 $fields[] = $this->db_instance->quoteIdentifier($field, true);
474 }
475 }
476 $query .= ' (' . implode(', ', $fields) . ')';
477
478 return $this->pdo->exec($query);
479 }

References $query.

◆ createSequence()

ilDBPdoManager::createSequence (   $seq_name,
  $start = 1,
  $options = array() 
)
Parameters
$seq_name
int$start
array$options
Returns
bool

Implements ilDBManager.

Reimplemented in ilDBPdoManagerPostgres.

Definition at line 158 of file class.ilDBPdoManager.php.

158 {
159 $sequence_name = $this->db_instance->quoteIdentifier($this->db_instance->getSequenceName($seq_name));
160 $seqcol_name = $this->db_instance->quoteIdentifier(ilDBConstants::SEQUENCE_COLUMNS_NAME);
161
162 $options_strings = array();
163
164 if (!empty($options['comment'])) {
165 $options_strings['comment'] = 'COMMENT = ' . $this->db_instance->quote($options['comment'], 'text');
166 }
167
168 if (!empty($options['charset'])) {
169 $options_strings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
170 if (!empty($options['collate'])) {
171 $options_strings['charset'] .= ' COLLATE ' . $options['collate'];
172 }
173 }
174
175 $type = false;
176 if (!empty($options['type'])) {
177 $type = $options['type'];
178 }
179 if ($type) {
180 $options_strings[] = "ENGINE = $type";
181 }
182
183 $query = "CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
184
185 if (!empty($options_strings)) {
186 $query .= ' ' . implode(' ', $options_strings);
187 }
188 $this->pdo->exec($query);
189
190 if ($start == 1) {
191 return true;
192 }
193
194 $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (" . ($start - 1) . ')';
195 $this->pdo->exec($query);
196
197 return true;
198 }
if(!is_array($argv)) $options

References $options, $query, $start, and ilDBConstants\SEQUENCE_COLUMNS_NAME.

◆ createTable()

ilDBPdoManager::createTable (   $name,
  $fields,
  $options = array() 
)
Parameters
$name
$fields
array$options
Returns
int

Reimplemented in ilDBPdoManagerPostgres.

Definition at line 307 of file class.ilDBPdoManager.php.

307 {
308 $options['type'] = $this->db_instance->getStorageEngine();
309
310 return $this->pdo->exec($this->getQueryUtils()->createTable($name, $fields, $options));
311 }
createTable($name, $fields, $options=array())

References $options, createTable(), and getQueryUtils().

Referenced by createTable().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dropConstraint()

ilDBPdoManager::dropConstraint (   $table,
  $name,
  $primary = false 
)
Parameters
$table
$name
bool$primary
Returns
int

Implements ilDBManager.

Reimplemented in ilDBPdoManagerPostgres.

Definition at line 524 of file class.ilDBPdoManager.php.

524 {
525 $db = $this->getDBInstance();
526 $table = $db->quoteIdentifier($table, true);
527 if ($primary || strtolower($name) == 'primary') {
528 $query = "ALTER TABLE $table DROP PRIMARY KEY";
529 } else {
530 $name = $db->quoteIdentifier($db->getIndexName($name), true);
531 $query = "ALTER TABLE $table DROP INDEX $name";
532 }
533
534 return $this->pdo->exec($query);
535 }

References $query, and getDBInstance().

+ Here is the call graph for this function:

◆ dropIndex()

ilDBPdoManager::dropIndex (   $table,
  $name 
)
Parameters
$table
$name
Returns
mixed

Implements ilDBManager.

Reimplemented in ilDBPdoManagerPostgres.

Definition at line 487 of file class.ilDBPdoManager.php.

487 {
488 $table = $this->db_instance->quoteIdentifier($table, true);
489 $name = $this->db_instance->quoteIdentifier($this->db_instance->getIndexName($name), true);
490
491 return $this->pdo->exec("DROP INDEX $name ON $table");
492 }

◆ dropSequence()

ilDBPdoManager::dropSequence (   $table_name)
Parameters
$table_name
Returns
int

Implements ilDBManager.

Reimplemented in ilDBPdoManagerPostgres.

Definition at line 499 of file class.ilDBPdoManager.php.

499 {
500 $sequence_name = $this->db_instance->quoteIdentifier($this->db_instance->getSequenceName($table_name));
501
502 return $this->pdo->exec("DROP TABLE $sequence_name");
503 }

◆ dropTable()

ilDBPdoManager::dropTable (   $name)

Parameters
$nameTable-name
Returns
mixed

Implements ilDBManager.

Definition at line 541 of file class.ilDBPdoManager.php.

541 {
542 $db = $this->getDBInstance();
543
544 $name = $db->quoteIdentifier($name, true);
545
546 return $db->manipulate("DROP TABLE $name");
547 }

References getDBInstance().

+ Here is the call graph for this function:

◆ fixIndexName()

ilDBPdoManager::fixIndexName (   $idx)
protected
Parameters
$idx
Returns
mixed

Reimplemented in ilDBPdoManagerPostgres.

Definition at line 447 of file class.ilDBPdoManager.php.

447 {
448 $idx_pattern = '/^' . preg_replace('/%s/', '([a-z0-9_]+)', ilDBPdoFieldDefinition::INDEX_FORMAT) . '$/i';
449 $idx_name = preg_replace($idx_pattern, '\\1', $idx);
450 if ($idx_name && !strcasecmp($idx, $this->db_instance->getIndexName($idx_name))) {
451 return $idx_name;
452 }
453
454 return $idx;
455 }

References ilDBPdoFieldDefinition\INDEX_FORMAT.

Referenced by listTableConstraints(), and listTableIndexes().

+ Here is the caller graph for this function:

◆ fixSequenceName()

ilDBPdoManager::fixSequenceName (   $sqn,
  $check = false 
)
protected
Parameters
$sqn
bool$check
Returns
bool|mixed

Definition at line 84 of file class.ilDBPdoManager.php.

84 {
85 $seq_pattern = '/^' . preg_replace('/%s/', '([a-z0-9_]+)', ilDBConstants::SEQUENCE_FORMAT) . '$/i';
86 $seq_name = preg_replace($seq_pattern, '\\1', $sqn);
87 if ($seq_name && !strcasecmp($sqn, $this->db_instance->getSequenceName($seq_name))) {
88 return $seq_name;
89 }
90 if ($check) {
91 return false;
92 }
93
94 return $sqn;
95 }

References ilDBConstants\SEQUENCE_FORMAT.

Referenced by listSequences(), and ilDBPdoManagerPostgres\listSequences().

+ Here is the caller graph for this function:

◆ getDBInstance()

ilDBPdoManager::getDBInstance ( )
Returns
\ilDBPdo

Definition at line 54 of file class.ilDBPdoManager.php.

54 {
55 return $this->db_instance;
56 }

References $db_instance.

Referenced by dropConstraint(), ilDBPdoManagerPostgres\dropConstraint(), ilDBPdoManagerPostgres\dropIndex(), dropTable(), and listTableConstraints().

+ Here is the caller graph for this function:

◆ getIndexName()

ilDBPdoManager::getIndexName (   $idx)
Parameters
$idx
Returns
string

Implements ilDBPdoManagerInterface.

Definition at line 324 of file class.ilDBPdoManager.php.

324 {
325 return $this->db_instance->getIndexName($idx);
326 }

Referenced by ilDBPdoManagerPostgres\dropConstraint(), and ilDBPdoManagerPostgres\dropIndex().

+ Here is the caller graph for this function:

◆ getQueryUtils()

ilDBPdoManager::getQueryUtils ( )
Returns
\ilMySQLQueryUtils

Reimplemented in ilDBPdoManagerPostgres.

Definition at line 42 of file class.ilDBPdoManager.php.

42 {
43 if (!$this->query_utils) {
44 $this->query_utils = new ilMySQLQueryUtils($this->db_instance);
45 }
46
47 return $this->query_utils;
48 }
Class ilMySQLQueryUtils.

References $query_utils.

Referenced by createTable(), and getTableCreationQuery().

+ Here is the caller graph for this function:

◆ getSequenceName()

ilDBPdoManager::getSequenceName (   $sqn)
Parameters
$sqn
Returns
string

Implements ilDBPdoManagerInterface.

Definition at line 333 of file class.ilDBPdoManager.php.

333 {
334 return $this->db_instance->getSequenceName($sqn);
335 }

◆ getTableCreationQuery()

ilDBPdoManager::getTableCreationQuery (   $name,
  $fields,
  $options = array() 
)
Parameters
$name
$fields
array$options
Returns
string
Exceptions

ilDatabaseException

Reimplemented in ilDBPdoManagerPostgres.

Definition at line 513 of file class.ilDBPdoManager.php.

513 {
514 return $this->getQueryUtils()->createTable($name, $fields, $options);
515 }

References $options, and getQueryUtils().

+ Here is the call graph for this function:

◆ listSequences()

ilDBPdoManager::listSequences (   $database = null)
Parameters
null$database
Returns
array

Implements ilDBManager.

Reimplemented in ilDBPdoManagerPostgres.

Definition at line 102 of file class.ilDBPdoManager.php.

102 {
103 $query = "SHOW TABLES";
104 if (!is_null($database)) {
105 $query .= " FROM $database";
106 }
107
108 $res = $this->db_instance->query($query);
109
110 $result = array();
111 while ($table_name = $this->db_instance->fetchAssoc($res)) {
112 if ($sqn = $this->fixSequenceName(reset($table_name), true)) {
113 $result[] = $sqn;
114 }
115 }
116 if ($this->db_instance->options['portability']) {
117 $result = array_map(($this->db_instance->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
118 }
119
120 return $result;
121 }
$result
fixSequenceName($sqn, $check=false)

References $query, $res, $result, and fixSequenceName().

+ Here is the call graph for this function:

◆ listTableConstraints()

ilDBPdoManager::listTableConstraints (   $table)
Parameters
$table
Returns
array
Exceptions

ilDatabaseException

Implements ilDBManager.

Reimplemented in ilDBPdoManagerPostgres.

Definition at line 361 of file class.ilDBPdoManager.php.

361 {
362 $key_name = 'Key_name';
363 $non_unique = 'Non_unique';
364
365 $db = $this->getDBInstance();
366 if ($db->options['portability']) {
367 if ($db->options['field_case'] == CASE_LOWER) {
368 $key_name = strtolower($key_name);
369 $non_unique = strtolower($non_unique);
370 } else {
371 $key_name = strtoupper($key_name);
372 $non_unique = strtoupper($non_unique);
373 }
374 }
375
376 $table = $this->db_instance->quoteIdentifier($table);
377 $query = "SHOW INDEX FROM $table";
378 $result_set = $this->db_instance->query($query);
379
380 $result = array();
381 while ($index_data = $this->db_instance->fetchAssoc($result_set)) {
382 if (!$index_data[$non_unique]) {
383 if ($index_data[$key_name] !== 'PRIMARY') {
384 $index = $this->fixIndexName($index_data[$key_name]);
385 } else {
386 $index = 'PRIMARY';
387 }
388 if (!empty($index)) {
389 $index = strtolower($index);
390 $result[$index] = true;
391 }
392 }
393 }
394
395 if ($this->db_instance->options['portability']) {
396 $result = array_change_key_case($result, $this->db_instance->options['field_case']);
397 }
398
399 return array_keys($result);
400 }

References $query, $result, fixIndexName(), and getDBInstance().

+ Here is the call graph for this function:

◆ listTableFields()

ilDBPdoManager::listTableFields (   $table)
Parameters
$table
Returns
array
Exceptions

ilDatabaseException

Implements ilDBManager.

Reimplemented in ilDBPdoManagerPostgres.

Definition at line 343 of file class.ilDBPdoManager.php.

343 {
344 $table = $this->db_instance->quoteIdentifier($table);
345 $query = "SHOW COLUMNS FROM $table";
346 $result = $this->db_instance->query($query);
347 $return = array();
348 while ($data = $this->db_instance->fetchObject($result)) {
349 $return[] = $data->Field;
350 }
351
352 return $return;
353 }

References $data, $query, and $result.

◆ listTableIndexes()

ilDBPdoManager::listTableIndexes (   $table)
Parameters
$table
Returns
array
Exceptions

ilDatabaseException

Implements ilDBManager.

Reimplemented in ilDBPdoManagerPostgres.

Definition at line 408 of file class.ilDBPdoManager.php.

408 {
409 $key_name = 'Key_name';
410 $non_unique = 'Non_unique';
411 if ($this->db_instance->options['portability']) {
412 if ($this->db_instance->options['field_case'] == CASE_LOWER) {
413 $key_name = strtolower($key_name);
414 $non_unique = strtolower($non_unique);
415 } else {
416 $key_name = strtoupper($key_name);
417 $non_unique = strtoupper($non_unique);
418 }
419 }
420
421 $table = $this->db_instance->quoteIdentifier($table);
422 $query = "SHOW INDEX FROM $table";
423 $result_set = $this->db_instance->query($query);
424 $indexes = array();
425 while ($index_data = $this->db_instance->fetchAssoc($result_set)) {
426 $indexes[] = $index_data;
427 }
428 $result = array();
429 foreach ($indexes as $index_data) {
430 if ($index_data[$non_unique] && ($index = $this->fixIndexName($index_data[$key_name]))) {
431 $result[$index] = true;
432 }
433 }
434
435 if ($this->db_instance->options['portability']) {
436 $result = array_change_key_case($result, $this->db_instance->options['field_case']);
437 }
438
439 return array_keys($result);
440 }

References $query, $result, and fixIndexName().

+ Here is the call graph for this function:

◆ listTables()

ilDBPdoManager::listTables (   $database = null)
Parameters
null$database
Returns
array

Implements ilDBManager.

Reimplemented in ilDBPdoManagerPostgres.

Definition at line 63 of file class.ilDBPdoManager.php.

63 {
64 $str = 'SHOW TABLES ' . ($database ? ' IN ' . $database : '');
65 $r = $this->pdo->query($str);
66 $tables = array();
67
68 $sequence_identifier = "_seq";
69 while ($data = $r->fetchColumn()) {
70 if (!preg_match("/{$sequence_identifier}$/um", $data)) {
71 $tables[] = $data;
72 }
73 }
74
75 return $tables;
76 }
$r
Definition: example_031.php:79

References $data, and $r.

Field Documentation

◆ $db_instance

◆ $pdo

ilDBPdoManager::$pdo
protected

Definition at line 14 of file class.ilDBPdoManager.php.

Referenced by __construct().

◆ $query_utils

ilDBPdoManager::$query_utils
protected

Definition at line 36 of file class.ilDBPdoManager.php.

Referenced by getQueryUtils(), and ilDBPdoManagerPostgres\getQueryUtils().


The documentation for this class was generated from the following file: