ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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
5include_once ("./Services/Database/classes/MDB2/class.ilDB.php");
6
18class ilDBPostgreSQL extends ilDB
19{
20
25 public function loadModule($module) {
26 return $this->db->loadModule($module);
27 }
28
29
30
31
35 function getDSN()
36 {
37 return "pgsql://".$this->getDBUser().":".$this->getDBPassword()."@".
38 $this->getDBHost()."/".$this->getDBName();
39 }
40
44 function getDBType()
45 {
46 return "postgres";
47 }
48
52 static function getReservedWords()
53 {
54 // version: 8.3.6
55 // url: http://www.postgresql.org/docs/current/static/sql-keywords-appendix.html
56 return array(
57 "ALL", "ANALYSE", "ANALYZE", "AND", "ANY", "ARRAY",
58 "AS", "ASC", "ASYMMETRIC", "AUTHORIZATION", "BETWEEN", "BINARY", "BOTH",
59 "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "CONSTRAINT", "CREATE",
60 "CROSS", "CURRENT_DATE", "CURRENT_ROLE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "CURRENT_USER", "DEFAULT",
61 "DEFERRABLE", "DESC", "DISTINCT", "DO", "ELSE", "END", "EXCEPT",
62 "FALSE", "FOR", "FOREIGN", "FREEZE", "FROM", "FULL", "GRANT",
63 "GROUP", "HAVING", "ILIKE", "IN", "INITIALLY", "INNER", "INTERSECT",
64 "INTO", "IS", "ISNULL", "JOIN", "LEADING", "LEFT", "LIKE",
65 "LIMIT", "LOCALTIME", "LOCALTIMESTAMP", "NATURAL", "NEW", "NOT", "NOTNULL",
66 "NULL", "OFF", "OFFSET", "OLD", "ON", "ONLY", "OR",
67 "ORDER", "OUTER", "OVERLAPS", "PLACING", "PRIMARY", "REFERENCES", "RETURNING",
68 "RIGHT", "SELECT", "SESSION_USER", "SIMILAR", "SOME", "SYMMETRIC", "TABLE",
69 "THEN", "TO", "TRAILING", "TRUE", "UNION", "UNIQUE", "USER",
70 "USING", "VERBOSE", "WHEN", "WHERE", "WITH"
71 );
72 }
73
77 function initConnection()
78 {
79 }
80
85 function now()
86 {
87 return "now()";
88 }
89
93 function constraintName($a_table, $a_constraint)
94 {
95 $a_constraint = str_replace($a_table . '_', '', $a_constraint);
96
97 return $a_table . "_" . $a_constraint;
98 }
99
104 {
105 return "pk";
106 }
107
112 {
113 return false;
114 }
115
123 function replace($a_table, $a_pk_columns, $a_other_columns)
124 {
125 $a_columns = array_merge($a_pk_columns, $a_other_columns);
126 $fields = array();
127 $field_values = array();
128 $placeholders = array();
129 $types = array();
130 $values = array();
131 $lobs = false;
132 $lob = array();
133 $val_field = array();
134 $a = array();
135 $b = array();
136 foreach ($a_columns as $k => $col)
137 {
138 if($col[0] == 'clob' or $col[0] == 'blob')
139 {
140 $val_field[] = $this->quote($col[1], 'text')." ".$k;
141 }
142 else
143 {
144 $val_field[] = $this->quote($col[1], $col[0])." ".$k;
145 }
146 $fields[] = $k;
147 $placeholders[] = "%s";
148 $placeholders2[] = ":$k";
149 $types[] = $col[0];
150 $values[] = $col[1];
151 $field_values[$k] = $col[1];
152 if ($col[0] == "blob" || $col[0] == "clob")
153 {
154 $lobs = true;
155 $lob[$k] = $k;
156 }
157 $a[] = "a.".$k;
158 $b[] = "b.".$k;
159 }
160 $abpk = array();
161 $aboc = array();
162 $delwhere = array();
163 foreach ($a_pk_columns as $k => $col)
164 {
165 $abpk[] = "a.".$k." = b.".$k;
166 $delwhere[] = $k." = ".$this->quote($col[1], $col[0]);
167 }
168 foreach ($a_other_columns as $k => $col)
169 {
170 $aboc[] = "a.".$k." = b.".$k;
171 }
172// if ($lobs) // lobs -> use prepare execute (autoexecute broken in PEAR 2.4.1)
173// {
174 $this->manipulate("DELETE FROM ".$a_table." WHERE ".
175 implode ($delwhere, " AND ")
176 );
177 $this->insert($a_table, $a_columns);
178
179 //$r = $this->db->extended->autoExecute($a_table, $field_values, MDB2_AUTOQUERY_INSERT, null, $types);
180 $this->handleError($r, "replace, delete/insert(".$a_table.")");
181// }
182/* else // if no lobs are used, use manipulate
183 {
184 $q = "MERGE INTO ".$a_table." a ".
185 "USING (SELECT ".implode($val_field, ", ")." ".
186 "FROM DUAL) b ON (".implode($abpk, " AND ").") ".
187 "WHEN MATCHED THEN UPDATE SET ".implode($aboc, ", ")." ".
188 "WHEN NOT MATCHED THEN INSERT (".implode($a, ",").") VALUES (".implode($b, ",").")";
189 $r = $this->manipulate($q);
190 }*/
191 return $r;
192 }
193
204 public function lockTables($a_tables)
205 {
206 global $ilLog;
207
208 $locks = array();
209
210 $counter = 0;
211 foreach($a_tables as $table)
212 {
213 $lock = 'LOCK TABLE ';
214
215 $lock .= ($table['name'].' ');
216
217 switch($table['type'])
218 {
220 $lock .= ' IN SHARE MODE ';
221 break;
222
224 $lock .= ' IN EXCLUSIVE MODE ';
225 break;
226 }
227
228 $locks[] = $lock;
229 }
230
231 // @TODO use and store a unique identifier to allow nested lock/unlocks
232 $this->db->beginTransaction();
233 foreach($locks as $lock)
234 {
235 $this->db->query($lock);
236 if ($ilLog instanceof ilLog) {
237 $ilLog->write(__METHOD__ . ': ' . $lock);
238 }
239 }
240 return true;
241 }
242
248 public function unlockTables()
249 {
250 $this->db->commit();
251 }
252
253
254 public function getStorageEngine() {
255 return null;
256 }
257
258
259 public function dropFulltextIndex($a_table, $a_name) {
260 return false;
261 }
262
263
264 public function setStorageEngine($storage_engine) {
265 return false;
266 }
267
268
276 public function groupConcat($a_field_name, $a_seperator = ",", $a_order = NULL) {
277 if ($a_order === NULL) {
278 $sql = "STRING_AGG(" . $a_field_name . ", " . $this->quote($a_seperator, "text") . ")";
279 } else {
280 $sql = "STRING_AGG(" . $a_field_name . ", " . $this->quote($a_seperator, "text") . " ORDER BY " . $a_order . ")";
281
282 }
283 return $sql;
284 }
285}
An exception for terminatinating execution or to throw for unit testing.
PostreSQL Database Wrapper.
getDBType()
Get DB Type.
static getReservedWords()
Get reserved words.
lockTables($a_tables)
Lock table.
unlockTables()
Unlock tables.
replace($a_table, $a_pk_columns, $a_other_columns)
Replace into method.
initConnection()
Initialize the database connection.
getPrimaryKeyIdentifier()
Primary key identifier.
setStorageEngine($storage_engine)
constraintName($a_table, $a_constraint)
Constraint names must be "globally" unique in oracle.
supportsFulltext()
Is fulltext index supported?
groupConcat($a_field_name, $a_seperator=",", $a_order=NULL)
dropFulltextIndex($a_table, $a_name)
Database Wrapper.
Definition: class.ilDB.php:30
manipulate($sql)
Data manipulation.
handleError($a_res, $a_info="", $a_level="")
Handle MDB2 Errors.
Definition: class.ilDB.php:423
getDBName()
Get database name.
Definition: class.ilDB.php:163
insert($a_table, $a_columns)
Convenient method for standard insert statements, example field array:
getDBPassword()
Get database password.
Definition: class.ilDB.php:143
getDBHost()
Get database host.
Definition: class.ilDB.php:123
quote($a_query, $a_type=null)
Wrapper for quote method.
logging
Definition: class.ilLog.php:19
$counter
$r
Definition: example_031.php:79