ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 */
3require_once('./Services/Database/classes/PDO/Manager/class.ilDBPdoManagerPostgres.php');
4require_once('class.ilDBPdo.php');
5require_once('./Services/Database/classes/PDO/FieldDefinition/class.ilDBPdoPostgresFieldDefinition.php');
6require_once('./Services/Database/classes/PDO/Reverse/class.ilDBPdoReversePostgres.php');
7
13class ilDBPdoPostgreSQL extends ilDBPdo implements ilDBInterface {
14
15 const POSTGRE_STD_PORT = 5432;
23 protected $storage_engine = null;
27 protected $manager;
28
29
30 public function generateDSN() {
31 if (!$this->getPort()) {
32 $this->setPort(self::POSTGRE_STD_PORT);
33 }
34 $this->dsn = 'pgsql:host=' . $this->getHost() . ';port=' . $this->getPort() . ';dbname=' . $this->getDbname() . ';user='
35 . $this->getUsername() . ';password=' . $this->getPassword() . '';
36 }
37
38
44 public function connect($return_false_for_error = false) {
45 $this->generateDSN();
46 try {
47 $this->pdo = new PDO($this->getDSN(), $this->getUsername(), $this->getPassword(), $this->getAttributes());
48 $this->initHelpers();
49 } catch (Exception $e) {
50 $this->error_code = $e->getCode();
51 if ($return_false_for_error) {
52 return false;
53 }
54 throw $e;
55 }
56
57 return ($this->pdo->errorCode() == PDO::ERR_NONE);
58 }
59
60
61 protected function getAdditionalAttributes() {
62 return array(
63 PDO::ATTR_EMULATE_PREPARES => true,
64 );
65 }
66
67
68 public function initHelpers() {
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
79 return "pk";
80 }
81
82
86 public function supportsFulltext() {
87 return false;
88 }
89
90
94 public function supportsTransactions() {
95 return true;
96 }
97
98
104 public function constraintName($a_table, $a_constraint) {
105 $a_constraint = str_replace($a_table . '_', '', $a_constraint);
106
107 return $a_table . '_' . $a_constraint;
108 }
109
110
115 public function getIndexName($index_name_base) {
116 return parent::getIndexName($index_name_base); // TODO: Change the autogenerated stub
117 }
118
119
127 public function replace($a_table, $a_pk_columns, $a_other_columns) {
128 $a_columns = array_merge($a_pk_columns, $a_other_columns);
129 $fields = array();
130 $field_values = array();
131 $placeholders = array();
132 $types = array();
133 $values = array();
134 $lobs = false;
135 $lob = array();
136 $val_field = array();
137 $a = array();
138 $b = array();
139 foreach ($a_columns as $k => $col) {
140 if ($col[0] == 'clob' or $col[0] == 'blob') {
141 $val_field[] = $this->quote($col[1], 'text') . " " . $k;
142 } else {
143 $val_field[] = $this->quote($col[1], $col[0]) . " " . $k;
144 }
145 $fields[] = $k;
146 $placeholders[] = "%s";
147 $placeholders2[] = ":$k";
148 $types[] = $col[0];
149 $values[] = $col[1];
150 $field_values[$k] = $col[1];
151 if ($col[0] == "blob" || $col[0] == "clob") {
152 $lobs = true;
153 $lob[$k] = $k;
154 }
155 $a[] = "a." . $k;
156 $b[] = "b." . $k;
157 }
158 $abpk = array();
159 $aboc = array();
160 $delwhere = array();
161 foreach ($a_pk_columns as $k => $col) {
162 $abpk[] = "a." . $k . " = b." . $k;
163 $delwhere[] = $k . " = " . $this->quote($col[1], $col[0]);
164 }
165 foreach ($a_other_columns as $k => $col) {
166 $aboc[] = "a." . $k . " = b." . $k;
167 }
168 // if ($lobs) // lobs -> use prepare execute (autoexecute broken in PEAR 2.4.1)
169 // {
170 $this->manipulate("DELETE FROM " . $a_table . " WHERE " . implode($delwhere, " AND "));
171 $this->insert($a_table, $a_columns);
172
173 return true;
174 }
175
176
182 public function lockTables($a_tables) {
183 global $ilLog;
184
185 $locks = array();
186
187 $counter = 0;
188 foreach ($a_tables as $table) {
189 if (!isset($table['sequence']) && $table['sequence']) {
190 $lock = 'LOCK TABLE ' . $table['name'];
191
192 switch ($table['type']) {
194 $lock .= ' IN SHARE MODE ';
195 break;
196
198 $lock .= ' IN EXCLUSIVE MODE ';
199 break;
200 }
201
202 $locks[] = $lock;
203 }
204 }
205
206 // @TODO use and store a unique identifier to allow nested lock/unlocks
207 $this->beginTransaction();
208 foreach ($locks as $lock) {
209 $this->query($lock);
210 }
211
212 return true;
213 }
214
215
220 public function unlockTables() {
221 $this->commit();
222 }
223
224
225 public function getStorageEngine() {
226 return null;
227 }
228
229
231 return false;
232 }
233
234 //
235 //
236 //
237
243 public function nextId($table_name) {
244 $sequence_name = $table_name . '_seq';
245 $query = "SELECT NEXTVAL('$sequence_name')";
246 $result = $this->query($query, 'integer');
247 $data = $result->fetchObject();
248
249 return $data->nextval;
250 }
251
252
258 public function dropTable($table_name, $error_if_not_existing = false) {
259 try {
260 $this->pdo->exec("DROP TABLE $table_name");
261 } catch (PDOException $PDOException) {
262 if ($error_if_not_existing) {
263 throw $PDOException;
264 }
265
266 return false;
267 }
268
269 return true;
270 }
271
272
278 public function quoteIdentifier($identifier, $check_option = false) {
279 return '"'.$identifier.'"';
280 }
281
282
287 public function tableExists($table_name) {
288 $tables = $this->listTables();
289
290 if (is_array($tables)) {
291 if (in_array($table_name, $tables)) {
292 return true;
293 }
294 }
295
296 return false;
297 }
298
299
304 protected function appendLimit($query) {
305 if ($this->limit !== null && $this->offset !== null) {
306 $query .= ' LIMIT ' . (int)$this->limit . ' OFFSET ' . (int)$this->offset;
307 $this->limit = null;
308 $this->offset = null;
309
310 return $query;
311 }
312
313 return $query;
314 }
315
316
323 public function tableColumnExists($table_name, $column_name) {
324 return in_array($column_name, $this->manager->listTableFields($table_name));
325 }
326
327
334 public function renameTable($a_name, $a_new_name) {
335 // check table name
336 try {
337 $this->checkTableName($a_new_name);
338 } catch (ilDatabaseException $e) {
339 return true;
340 }
341
342 if ($this->tableExists($a_new_name)) {
343 return true;
344 }
345 try {
346 $this->manager->alterTable($a_name, array( "name" => $a_new_name ), false);
347 } catch (Exception $e) {
348 return true;
349 }
350
351 return true;
352 }
353
354
360 public function createSequence($table_name, $start = 1) {
361 if (in_array($table_name, $this->manager->listSequences())) {
362 return true;
363 }
364 try {
365 parent::createSequence($table_name, $start); // TODO: Change the autogenerated stub
366 } catch (Exception $e) {
367 return true;
368 }
369 }
370
371
380 public function createTable($table_name, $fields, $drop_table = false, $ignore_erros = false) {
381 if ($this->tableExists($table_name)) {
382 return true;
383 }
384 try {
385 return parent::createTable($table_name, $fields, $drop_table, $ignore_erros); // TODO: Change the autogenerated stub
386 } catch (Exception $e) {
387 return true;
388 }
389 }
390
391
397 public function addPrimaryKey($table_name, $primary_keys) {
398 require_once('./Services/Database/classes/class.ilDBAnalyzer.php');
399 $ilDBAnalyzer = new ilDBAnalyzer($this);
400 if ($ilDBAnalyzer->getPrimaryKeyInformation($table_name)) {
401 return true;
402 }
403 try {
404 return parent::addPrimaryKey($table_name, $primary_keys); // TODO: Change the autogenerated stub
405 } catch (Exception $e) {
406 return true;
407 }
408 }
409
410
411 public function addIndex($table_name, $fields, $index_name = '', $fulltext = false) {
412 $indices = $this->manager->listTableIndexes($table_name);
413 if (in_array($this->constraintName($table_name, $index_name), $indices)) {
414 return true;
415 }
416 try {
417 return parent::addIndex($table_name, $fields, $index_name, $fulltext); // TODO: Change the autogenerated stub
418 } catch (Exception $e) {
419 return true;
420 }
421 }
422
423
424 public function addUniqueConstraint($table, $fields, $name = "con") {
425 try {
426 return parent::addUniqueConstraint($table, $fields, $name); // TODO: Change the autogenerated stub
427 } catch (Exception $e) {
428 return true;
429 }
430 }
431
436 public function dropPrimaryKey($table_name) {
437 return $this->manager->dropConstraint($table_name, "pk", true);
438 }
439
440}
441
$result
An exception for terminatinating execution or to throw for unit testing.
This class gives all kind of DB information using the MDB2 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.
Class pdoDB.
query($query)
quote($value, $type=null)
setPort($port)
checkTableName($a_name)
manipulate($query)
insert($table_name, $values)
Class ilDatabaseException.
$counter
Interface ilDBInterface.