ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilDBPdoPostgreSQL.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
9class ilDBPdoPostgreSQL extends ilDBPdo implements ilDBInterface
10{
11 const POSTGRE_STD_PORT = 5432;
19 protected $storage_engine = null;
23 protected $manager;
24
25
26 public function generateDSN()
27 {
28 if (!$this->getPort()) {
29 $this->setPort(self::POSTGRE_STD_PORT);
30 }
31 $this->dsn = 'pgsql:host=' . $this->getHost() . ';port=' . $this->getPort() . ';dbname=' . $this->getDbname() . ';user='
32 . $this->getUsername() . ';password=' . $this->getPassword() . '';
33 }
34
35
41 public function connect($return_false_for_error = false)
42 {
43 $this->generateDSN();
44 try {
45 $this->pdo = new PDO($this->getDSN(), $this->getUsername(), $this->getPassword(), $this->getAttributes());
46 $this->initHelpers();
47 } catch (Exception $e) {
48 $this->error_code = $e->getCode();
49 if ($return_false_for_error) {
50 return false;
51 }
52 throw $e;
53 }
54
55 return ($this->pdo->errorCode() == PDO::ERR_NONE);
56 }
57
58
59 protected function getAdditionalAttributes()
60 {
61 return array(
62 PDO::ATTR_EMULATE_PREPARES => true,
63 );
64 }
65
66
67 public function initHelpers()
68 {
69 $this->manager = new ilDBPdoManagerPostgres($this->pdo, $this);
70 $this->reverse = new ilDBPdoReversePostgres($this->pdo, $this);
71 $this->field_definition = new ilDBPdoPostgresFieldDefinition($this);
72 }
73
74
78 public function getPrimaryKeyIdentifier()
79 {
80 return "pk";
81 }
82
83
87 public function supportsFulltext()
88 {
89 return false;
90 }
91
92
96 public function supportsTransactions()
97 {
98 return true;
99 }
100
101
107 public function constraintName($a_table, $a_constraint)
108 {
109 $a_constraint = str_replace($a_table . '_', '', $a_constraint);
110
111 return $a_table . '_' . $a_constraint;
112 }
113
114
119 public function getIndexName($index_name_base)
120 {
121 return parent::getIndexName($index_name_base); // TODO: Change the autogenerated stub
122 }
123
124
132 public function replace($a_table, $a_pk_columns, $a_other_columns)
133 {
134 $a_columns = array_merge($a_pk_columns, $a_other_columns);
135 $fields = array();
136 $field_values = array();
137 $placeholders = array();
138 $types = array();
139 $values = array();
140 $lobs = false;
141 $lob = array();
142 $val_field = array();
143 $a = array();
144 $b = array();
145 foreach ($a_columns as $k => $col) {
146 if ($col[0] == 'clob' or $col[0] == 'blob') {
147 $val_field[] = $this->quote($col[1], 'text') . " " . $k;
148 } else {
149 $val_field[] = $this->quote($col[1], $col[0]) . " " . $k;
150 }
151 $fields[] = $k;
152 $placeholders[] = "%s";
153 $placeholders2[] = ":$k";
154 $types[] = $col[0];
155 $values[] = $col[1];
156 $field_values[$k] = $col[1];
157 if ($col[0] == "blob" || $col[0] == "clob") {
158 $lobs = true;
159 $lob[$k] = $k;
160 }
161 $a[] = "a." . $k;
162 $b[] = "b." . $k;
163 }
164 $abpk = array();
165 $aboc = array();
166 $delwhere = array();
167 foreach ($a_pk_columns as $k => $col) {
168 $abpk[] = "a." . $k . " = b." . $k;
169 $delwhere[] = $k . " = " . $this->quote($col[1], $col[0]);
170 }
171 foreach ($a_other_columns as $k => $col) {
172 $aboc[] = "a." . $k . " = b." . $k;
173 }
174 // if ($lobs) // lobs -> use prepare execute (autoexecute broken in PEAR 2.4.1)
175 // {
176 $this->manipulate("DELETE FROM " . $a_table . " WHERE " . implode(" AND ", $delwhere));
177 $this->insert($a_table, $a_columns);
178
179 return true;
180 }
181
182
188 public function lockTables($a_tables)
189 {
190 $locks = array();
191
192 $counter = 0;
193 foreach ($a_tables as $table) {
194 if (!isset($table['sequence']) && $table['sequence']) {
195 $lock = 'LOCK TABLE ' . $table['name'];
196
197 switch ($table['type']) {
199 $lock .= ' IN SHARE MODE ';
200 break;
201
203 $lock .= ' IN EXCLUSIVE MODE ';
204 break;
205 }
206
207 $locks[] = $lock;
208 }
209 }
210
211 // @TODO use and store a unique identifier to allow nested lock/unlocks
212 $this->beginTransaction();
213 foreach ($locks as $lock) {
214 $this->query($lock);
215 }
216
217 return true;
218 }
219
220
225 public function unlockTables()
226 {
227 $this->commit();
228 }
229
230
231 public function getStorageEngine()
232 {
233 return null;
234 }
235
236
238 {
239 return false;
240 }
241
242 //
243 //
244 //
245
251 public function nextId($table_name)
252 {
253 $sequence_name = $table_name . '_seq';
254 $query = "SELECT NEXTVAL('$sequence_name')";
255 $result = $this->query($query, 'integer');
256 $data = $result->fetchObject();
257
258 return $data->nextval;
259 }
260
261
267 public function dropTable($table_name, $error_if_not_existing = false)
268 {
269 try {
270 $this->pdo->exec("DROP TABLE $table_name");
271 } catch (PDOException $PDOException) {
272 if ($error_if_not_existing) {
273 throw $PDOException;
274 }
275
276 return false;
277 }
278
279 return true;
280 }
281
282
288 public function quoteIdentifier($identifier, $check_option = false)
289 {
290 return '"' . $identifier . '"';
291 }
292
293
298 public function tableExists($table_name)
299 {
300 $tables = $this->listTables();
301
302 if (is_array($tables)) {
303 if (in_array($table_name, $tables)) {
304 return true;
305 }
306 }
307
308 return false;
309 }
310
311
316 protected function appendLimit($query)
317 {
318 if ($this->limit !== null && $this->offset !== null) {
319 $query .= ' LIMIT ' . (int) $this->limit . ' OFFSET ' . (int) $this->offset;
320 $this->limit = null;
321 $this->offset = null;
322
323 return $query;
324 }
325
326 return $query;
327 }
328
329
336 public function tableColumnExists($table_name, $column_name)
337 {
338 return in_array($column_name, $this->manager->listTableFields($table_name));
339 }
340
341
348 public function renameTable($a_name, $a_new_name)
349 {
350 // check table name
351 try {
352 $this->checkTableName($a_new_name);
353 } catch (ilDatabaseException $e) {
354 return true;
355 }
356
357 if ($this->tableExists($a_new_name)) {
358 return true;
359 }
360 try {
361 $this->manager->alterTable($a_name, [ "name" => $a_new_name ], false);
362 if ($this->sequenceExists($a_name)) {
363 $this->manager->alterTable($this->getSequenceName($a_name), [ "name" => $this->getSequenceName($a_new_name) ], false);
364 }
365 } catch (Exception $e) {
366 return true;
367 }
368
369 return true;
370 }
371
372
378 public function createSequence($table_name, $start = 1)
379 {
380 if (in_array($table_name, $this->manager->listSequences())) {
381 return true;
382 }
383 try {
384 parent::createSequence($table_name, $start); // TODO: Change the autogenerated stub
385 } catch (Exception $e) {
386 return true;
387 }
388 }
389
390
399 public function createTable($table_name, $fields, $drop_table = false, $ignore_erros = false)
400 {
401 if ($this->tableExists($table_name)) {
402 return true;
403 }
404 try {
405 return parent::createTable($table_name, $fields, $drop_table, $ignore_erros); // TODO: Change the autogenerated stub
406 } catch (Exception $e) {
407 return true;
408 }
409 }
410
411
417 public function addPrimaryKey($table_name, $primary_keys)
418 {
419 $ilDBAnalyzer = new ilDBAnalyzer($this);
420 if ($ilDBAnalyzer->getPrimaryKeyInformation($table_name)) {
421 return true;
422 }
423 try {
424 return parent::addPrimaryKey($table_name, $primary_keys); // TODO: Change the autogenerated stub
425 } catch (Exception $e) {
426 return true;
427 }
428 }
429
430
431 public function addIndex($table_name, $fields, $index_name = '', $fulltext = false)
432 {
433 $indices = $this->manager->listTableIndexes($table_name);
434 if (in_array($this->constraintName($table_name, $index_name), $indices)) {
435 return true;
436 }
437 try {
438 return parent::addIndex($table_name, $fields, $index_name, $fulltext); // TODO: Change the autogenerated stub
439 } catch (Exception $e) {
440 return true;
441 }
442 }
443
444
445 public function addUniqueConstraint($table, $fields, $name = "con")
446 {
447 try {
448 return parent::addUniqueConstraint($table, $fields, $name); // TODO: Change the autogenerated stub
449 } catch (Exception $e) {
450 return true;
451 }
452 }
453
458 public function dropPrimaryKey($table_name)
459 {
460 return $this->manager->dropConstraint($table_name, "pk", true);
461 }
462}
$result
An exception for terminatinating execution or to throw for unit testing.
This class gives all kind of DB information using the database manager and reverse module.
Class ilDBPdoPostgreSQL.
getPrimaryKeyIdentifier()
Primary key identifier.
tableColumnExists($table_name, $column_name)
constraintName($a_table, $a_constraint)
quoteIdentifier($identifier, $check_option=false)
replace($a_table, $a_pk_columns, $a_other_columns)
addIndex($table_name, $fields, $index_name='', $fulltext=false)
renameTable($a_name, $a_new_name)
dropTable($table_name, $error_if_not_existing=false)
setStorageEngine($storage_engine)
createSequence($table_name, $start=1)
connect($return_false_for_error=false)
addPrimaryKey($table_name, $primary_keys)
addUniqueConstraint($table, $fields, $name="con")
createTable($table_name, $fields, $drop_table=false, $ignore_erros=false)
getIndexName($index_name_base)
Class ilDBPdoPostgresFieldDefinition.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
query($query)
quote($value, $type=null)
setPort($port)
checkTableName($a_name)
getSequenceName($table_name)
manipulate($query)
sequenceExists($sequence)
insert($table_name, $values)
Class ilDatabaseException.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if($format !==null) $name
Definition: metadata.php:230
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$query
$data
Definition: storeScorm.php:23