ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 require_once('./Services/Database/classes/PDO/Manager/class.ilDBPdoManagerPostgres.php');
4 require_once('class.ilDBPdo.php');
5 require_once('./Services/Database/classes/PDO/FieldDefinition/class.ilDBPdoPostgresFieldDefinition.php');
6 require_once('./Services/Database/classes/PDO/Reverse/class.ilDBPdoReversePostgres.php');
7 
13 class ilDBPdoPostgreSQL extends ilDBPdo implements ilDBInterface
14 {
15  const POSTGRE_STD_PORT = 5432;
19  protected $port = self::POSTGRE_STD_PORT;
23  protected $storage_engine = null;
27  protected $manager;
28 
29 
30  public function generateDSN()
31  {
32  if (!$this->getPort()) {
33  $this->setPort(self::POSTGRE_STD_PORT);
34  }
35  $this->dsn = 'pgsql:host=' . $this->getHost() . ';port=' . $this->getPort() . ';dbname=' . $this->getDbname() . ';user='
36  . $this->getUsername() . ';password=' . $this->getPassword() . '';
37  }
38 
39 
45  public function connect($return_false_for_error = false)
46  {
47  $this->generateDSN();
48  try {
49  $this->pdo = new PDO($this->getDSN(), $this->getUsername(), $this->getPassword(), $this->getAttributes());
50  $this->initHelpers();
51  } catch (Exception $e) {
52  $this->error_code = $e->getCode();
53  if ($return_false_for_error) {
54  return false;
55  }
56  throw $e;
57  }
58 
59  return ($this->pdo->errorCode() == PDO::ERR_NONE);
60  }
61 
62 
63  protected function getAdditionalAttributes()
64  {
65  return array(
66  PDO::ATTR_EMULATE_PREPARES => true,
67  );
68  }
69 
70 
71  public function initHelpers()
72  {
73  $this->manager = new ilDBPdoManagerPostgres($this->pdo, $this);
74  $this->reverse = new ilDBPdoReversePostgres($this->pdo, $this);
75  $this->field_definition = new ilDBPdoPostgresFieldDefinition($this);
76  }
77 
78 
82  public function getPrimaryKeyIdentifier()
83  {
84  return "pk";
85  }
86 
87 
91  public function supportsFulltext()
92  {
93  return false;
94  }
95 
96 
100  public function supportsTransactions()
101  {
102  return true;
103  }
104 
105 
111  public function constraintName($a_table, $a_constraint)
112  {
113  $a_constraint = str_replace($a_table . '_', '', $a_constraint);
114 
115  return $a_table . '_' . $a_constraint;
116  }
117 
118 
123  public function getIndexName($index_name_base)
124  {
125  return parent::getIndexName($index_name_base); // TODO: Change the autogenerated stub
126  }
127 
128 
136  public function replace($a_table, $a_pk_columns, $a_other_columns)
137  {
138  $a_columns = array_merge($a_pk_columns, $a_other_columns);
139  $fields = array();
140  $field_values = array();
141  $placeholders = array();
142  $types = array();
143  $values = array();
144  $lobs = false;
145  $lob = array();
146  $val_field = array();
147  $a = array();
148  $b = array();
149  foreach ($a_columns as $k => $col) {
150  if ($col[0] == 'clob' or $col[0] == 'blob') {
151  $val_field[] = $this->quote($col[1], 'text') . " " . $k;
152  } else {
153  $val_field[] = $this->quote($col[1], $col[0]) . " " . $k;
154  }
155  $fields[] = $k;
156  $placeholders[] = "%s";
157  $placeholders2[] = ":$k";
158  $types[] = $col[0];
159  $values[] = $col[1];
160  $field_values[$k] = $col[1];
161  if ($col[0] == "blob" || $col[0] == "clob") {
162  $lobs = true;
163  $lob[$k] = $k;
164  }
165  $a[] = "a." . $k;
166  $b[] = "b." . $k;
167  }
168  $abpk = array();
169  $aboc = array();
170  $delwhere = array();
171  foreach ($a_pk_columns as $k => $col) {
172  $abpk[] = "a." . $k . " = b." . $k;
173  $delwhere[] = $k . " = " . $this->quote($col[1], $col[0]);
174  }
175  foreach ($a_other_columns as $k => $col) {
176  $aboc[] = "a." . $k . " = b." . $k;
177  }
178  // if ($lobs) // lobs -> use prepare execute (autoexecute broken in PEAR 2.4.1)
179  // {
180  $this->manipulate("DELETE FROM " . $a_table . " WHERE " . implode($delwhere, " AND "));
181  $this->insert($a_table, $a_columns);
182 
183  return true;
184  }
185 
186 
192  public function lockTables($a_tables)
193  {
194  global $ilLog;
195 
196  $locks = array();
197 
198  $counter = 0;
199  foreach ($a_tables as $table) {
200  if (!isset($table['sequence']) && $table['sequence']) {
201  $lock = 'LOCK TABLE ' . $table['name'];
202 
203  switch ($table['type']) {
205  $lock .= ' IN SHARE MODE ';
206  break;
207 
209  $lock .= ' IN EXCLUSIVE MODE ';
210  break;
211  }
212 
213  $locks[] = $lock;
214  }
215  }
216 
217  // @TODO use and store a unique identifier to allow nested lock/unlocks
218  $this->beginTransaction();
219  foreach ($locks as $lock) {
220  $this->query($lock);
221  }
222 
223  return true;
224  }
225 
226 
231  public function unlockTables()
232  {
233  $this->commit();
234  }
235 
236 
237  public function getStorageEngine()
238  {
239  return null;
240  }
241 
242 
244  {
245  return false;
246  }
247 
248  //
249  //
250  //
251 
257  public function nextId($table_name)
258  {
259  $sequence_name = $table_name . '_seq';
260  $query = "SELECT NEXTVAL('$sequence_name')";
261  $result = $this->query($query, 'integer');
262  $data = $result->fetchObject();
263 
264  return $data->nextval;
265  }
266 
267 
273  public function dropTable($table_name, $error_if_not_existing = false)
274  {
275  try {
276  $this->pdo->exec("DROP TABLE $table_name");
277  } catch (PDOException $PDOException) {
278  if ($error_if_not_existing) {
279  throw $PDOException;
280  }
281 
282  return false;
283  }
284 
285  return true;
286  }
287 
288 
294  public function quoteIdentifier($identifier, $check_option = false)
295  {
296  return '"' . $identifier . '"';
297  }
298 
299 
304  public function tableExists($table_name)
305  {
306  $tables = $this->listTables();
307 
308  if (is_array($tables)) {
309  if (in_array($table_name, $tables)) {
310  return true;
311  }
312  }
313 
314  return false;
315  }
316 
317 
322  protected function appendLimit($query)
323  {
324  if ($this->limit !== null && $this->offset !== null) {
325  $query .= ' LIMIT ' . (int) $this->limit . ' OFFSET ' . (int) $this->offset;
326  $this->limit = null;
327  $this->offset = null;
328 
329  return $query;
330  }
331 
332  return $query;
333  }
334 
335 
342  public function tableColumnExists($table_name, $column_name)
343  {
344  return in_array($column_name, $this->manager->listTableFields($table_name));
345  }
346 
347 
354  public function renameTable($a_name, $a_new_name)
355  {
356  // check table name
357  try {
358  $this->checkTableName($a_new_name);
359  } catch (ilDatabaseException $e) {
360  return true;
361  }
362 
363  if ($this->tableExists($a_new_name)) {
364  return true;
365  }
366  try {
367  $this->manager->alterTable($a_name, array( "name" => $a_new_name ), false);
368  } catch (Exception $e) {
369  return true;
370  }
371 
372  return true;
373  }
374 
375 
381  public function createSequence($table_name, $start = 1)
382  {
383  if (in_array($table_name, $this->manager->listSequences())) {
384  return true;
385  }
386  try {
387  parent::createSequence($table_name, $start); // TODO: Change the autogenerated stub
388  } catch (Exception $e) {
389  return true;
390  }
391  }
392 
393 
402  public function createTable($table_name, $fields, $drop_table = false, $ignore_erros = false)
403  {
404  if ($this->tableExists($table_name)) {
405  return true;
406  }
407  try {
408  return parent::createTable($table_name, $fields, $drop_table, $ignore_erros); // TODO: Change the autogenerated stub
409  } catch (Exception $e) {
410  return true;
411  }
412  }
413 
414 
420  public function addPrimaryKey($table_name, $primary_keys)
421  {
422  require_once('./Services/Database/classes/class.ilDBAnalyzer.php');
423  $ilDBAnalyzer = new ilDBAnalyzer($this);
424  if ($ilDBAnalyzer->getPrimaryKeyInformation($table_name)) {
425  return true;
426  }
427  try {
428  return parent::addPrimaryKey($table_name, $primary_keys); // TODO: Change the autogenerated stub
429  } catch (Exception $e) {
430  return true;
431  }
432  }
433 
434 
435  public function addIndex($table_name, $fields, $index_name = '', $fulltext = false)
436  {
437  $indices = $this->manager->listTableIndexes($table_name);
438  if (in_array($this->constraintName($table_name, $index_name), $indices)) {
439  return true;
440  }
441  try {
442  return parent::addIndex($table_name, $fields, $index_name, $fulltext); // TODO: Change the autogenerated stub
443  } catch (Exception $e) {
444  return true;
445  }
446  }
447 
448 
449  public function addUniqueConstraint($table, $fields, $name = "con")
450  {
451  try {
452  return parent::addUniqueConstraint($table, $fields, $name); // TODO: Change the autogenerated stub
453  } catch (Exception $e) {
454  return true;
455  }
456  }
457 
462  public function dropPrimaryKey($table_name)
463  {
464  return $this->manager->dropConstraint($table_name, "pk", true);
465  }
466 }
createTable($table_name, $fields, $drop_table=false, $ignore_erros=false)
dropTable($table_name, $error_if_not_existing=false)
checkTableName($a_name)
Class ilDBPdoPostgreSQL.
$result
renameTable($a_name, $a_new_name)
Class ilDBPdoPostgresFieldDefinition.
quote($value, $type=null)
getPrimaryKeyIdentifier()
Primary key identifier.
setStorageEngine($storage_engine)
Class ilDatabaseException.
replace($a_table, $a_pk_columns, $a_other_columns)
constraintName($a_table, $a_constraint)
Class pdoDB.
$counter
Interface ilDBInterface.
if($format !==null) $name
Definition: metadata.php:146
quoteIdentifier($identifier, $check_option=false)
tableColumnExists($table_name, $column_name)
query($query)
addPrimaryKey($table_name, $primary_keys)
$query
addIndex($table_name, $fields, $index_name='', $fulltext=false)
Create styles array
The data for the language used.
setPort($port)
createSequence($table_name, $start=1)
getIndexName($index_name_base)
insert($table_name, $values)
addUniqueConstraint($table, $fields, $name="con")
if(empty($password)) $table
Definition: pwgen.php:24
This class gives all kind of DB information using the MDB2 manager and reverse module.
manipulate($query)
connect($return_false_for_error=false)