ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilDBPostgreSQL.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 
5 include_once ("./Services/Database/classes/class.ilDB.php");
6 
18 class ilDBPostgreSQL extends ilDB
19 {
20 
24  function getDSN()
25  {
26  return "pgsql://".$this->getDBUser().":".$this->getDBPassword()."@".
27  $this->getDBHost()."/".$this->getDBName();
28  }
29 
33  function getDBType()
34  {
35  return "postgres";
36  }
37 
41  static function getReservedWords()
42  {
43  // version: 8.3.6
44  // url: http://www.postgresql.org/docs/current/static/sql-keywords-appendix.html
45  return array(
46  "ALL", "ANALYSE", "ANALYZE", "AND", "ANY", "ARRAY",
47  "AS", "ASC", "ASYMMETRIC", "AUTHORIZATION", "BETWEEN", "BINARY", "BOTH",
48  "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "CONSTRAINT", "CREATE",
49  "CROSS", "CURRENT_DATE", "CURRENT_ROLE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "CURRENT_USER", "DEFAULT",
50  "DEFERRABLE", "DESC", "DISTINCT", "DO", "ELSE", "END", "EXCEPT",
51  "FALSE", "FOR", "FOREIGN", "FREEZE", "FROM", "FULL", "GRANT",
52  "GROUP", "HAVING", "ILIKE", "IN", "INITIALLY", "INNER", "INTERSECT",
53  "INTO", "IS", "ISNULL", "JOIN", "LEADING", "LEFT", "LIKE",
54  "LIMIT", "LOCALTIME", "LOCALTIMESTAMP", "NATURAL", "NEW", "NOT", "NOTNULL",
55  "NULL", "OFF", "OFFSET", "OLD", "ON", "ONLY", "OR",
56  "ORDER", "OUTER", "OVERLAPS", "PLACING", "PRIMARY", "REFERENCES", "RETURNING",
57  "RIGHT", "SELECT", "SESSION_USER", "SIMILAR", "SOME", "SYMMETRIC", "TABLE",
58  "THEN", "TO", "TRAILING", "TRUE", "UNION", "UNIQUE", "USER",
59  "USING", "VERBOSE", "WHEN", "WHERE", "WITH"
60  );
61  }
62 
66  function initConnection()
67  {
68  }
69 
74  function now()
75  {
76  return "now()";
77  }
78 
82  function constraintName($a_table, $a_constraint)
83  {
84  return $a_table."_".$a_constraint;
85  }
86 
91  {
92  return "pk";
93  }
94 
98  function supportsFulltext()
99  {
100  return false;
101  }
102 
110  function replace($a_table, $a_pk_columns, $a_other_columns)
111  {
112  $a_columns = array_merge($a_pk_columns, $a_other_columns);
113  $fields = array();
114  $field_values = array();
115  $placeholders = array();
116  $types = array();
117  $values = array();
118  $lobs = false;
119  $lob = array();
120  $val_field = array();
121  $a = array();
122  $b = array();
123  foreach ($a_columns as $k => $col)
124  {
125  if($col[0] == 'clob' or $col[0] == 'blob')
126  {
127  $val_field[] = $this->quote($col[1], 'text')." ".$k;
128  }
129  else
130  {
131  $val_field[] = $this->quote($col[1], $col[0])." ".$k;
132  }
133  $fields[] = $k;
134  $placeholders[] = "%s";
135  $placeholders2[] = ":$k";
136  $types[] = $col[0];
137  $values[] = $col[1];
138  $field_values[$k] = $col[1];
139  if ($col[0] == "blob" || $col[0] == "clob")
140  {
141  $lobs = true;
142  $lob[$k] = $k;
143  }
144  $a[] = "a.".$k;
145  $b[] = "b.".$k;
146  }
147  $abpk = array();
148  $aboc = array();
149  $delwhere = array();
150  foreach ($a_pk_columns as $k => $col)
151  {
152  $abpk[] = "a.".$k." = b.".$k;
153  $delwhere[] = $k." = ".$this->quote($col[1], $col[0]);
154  }
155  foreach ($a_other_columns as $k => $col)
156  {
157  $aboc[] = "a.".$k." = b.".$k;
158  }
159 // if ($lobs) // lobs -> use prepare execute (autoexecute broken in PEAR 2.4.1)
160 // {
161  $this->manipulate("DELETE FROM ".$a_table." WHERE ".
162  implode ($delwhere, " AND ")
163  );
164  $this->insert($a_table, $a_columns);
165 
166  //$r = $this->db->extended->autoExecute($a_table, $field_values, MDB2_AUTOQUERY_INSERT, null, $types);
167  $this->handleError($r, "replace, delete/insert(".$a_table.")");
168 // }
169 /* else // if no lobs are used, use manipulate
170  {
171  $q = "MERGE INTO ".$a_table." a ".
172  "USING (SELECT ".implode($val_field, ", ")." ".
173  "FROM DUAL) b ON (".implode($abpk, " AND ").") ".
174  "WHEN MATCHED THEN UPDATE SET ".implode($aboc, ", ")." ".
175  "WHEN NOT MATCHED THEN INSERT (".implode($a, ",").") VALUES (".implode($b, ",").")";
176  $r = $this->manipulate($q);
177  }*/
178  return $r;
179  }
180 
190  public function lockTables($a_tables)
191  {
192  global $ilLog;
193 
194  $locks = array();
195 
196  $counter = 0;
197  foreach($a_tables as $table)
198  {
199  $lock = 'LOCK TABLE ';
200 
201  $lock .= ($table['name'].' ');
202 
203  switch($table['type'])
204  {
205  case ilDB::LOCK_READ:
206  $lock .= ' IN SHARE MODE ';
207  break;
208 
209  case ilDB::LOCK_WRITE:
210  $lock .= ' IN EXCLUSIVE MODE ';
211  break;
212  }
213 
214  $locks[] = $lock;
215  }
216 
217  // @TODO use and store a unique identifier to allow nested lock/unlocks
218  $this->db->beginTransaction();
219  foreach($locks as $lock)
220  {
221  $this->db->query($lock);
222  $ilLog->write(__METHOD__.': '.$lock);
223  }
224  return true;
225  }
226 
231  public function unlockTables()
232  {
233  $this->db->commit();
234  }
235 
236 }
237 ?>