ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 {
27 return $this->db->loadModule($module);
28 }
29
30
31
32
36 public function getDSN()
37 {
38 return "pgsql://" . $this->getDBUser() . ":" . $this->getDBPassword() . "@" .
39 $this->getDBHost() . "/" . $this->getDBName();
40 }
41
45 public function getDBType()
46 {
47 return "postgres";
48 }
49
53 public static function getReservedWords()
54 {
55 // version: 8.3.6
56 // url: http://www.postgresql.org/docs/current/static/sql-keywords-appendix.html
57 return array(
58 "ALL", "ANALYSE", "ANALYZE", "AND", "ANY", "ARRAY",
59 "AS", "ASC", "ASYMMETRIC", "AUTHORIZATION", "BETWEEN", "BINARY", "BOTH",
60 "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "CONSTRAINT", "CREATE",
61 "CROSS", "CURRENT_DATE", "CURRENT_ROLE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "CURRENT_USER", "DEFAULT",
62 "DEFERRABLE", "DESC", "DISTINCT", "DO", "ELSE", "END", "EXCEPT",
63 "FALSE", "FOR", "FOREIGN", "FREEZE", "FROM", "FULL", "GRANT",
64 "GROUP", "HAVING", "ILIKE", "IN", "INITIALLY", "INNER", "INTERSECT",
65 "INTO", "IS", "ISNULL", "JOIN", "LEADING", "LEFT", "LIKE",
66 "LIMIT", "LOCALTIME", "LOCALTIMESTAMP", "NATURAL", "NEW", "NOT", "NOTNULL",
67 "NULL", "OFF", "OFFSET", "OLD", "ON", "ONLY", "OR",
68 "ORDER", "OUTER", "OVERLAPS", "PLACING", "PRIMARY", "REFERENCES", "RETURNING",
69 "RIGHT", "SELECT", "SESSION_USER", "SIMILAR", "SOME", "SYMMETRIC", "TABLE",
70 "THEN", "TO", "TRAILING", "TRUE", "UNION", "UNIQUE", "USER",
71 "USING", "VERBOSE", "WHEN", "WHERE", "WITH"
72 );
73 }
74
78 public function initConnection()
79 {
80 }
81
86 public function now()
87 {
88 return "now()";
89 }
90
94 public function constraintName($a_table, $a_constraint)
95 {
96 $a_constraint = str_replace($a_table . '_', '', $a_constraint);
97
98 return $a_table . "_" . $a_constraint;
99 }
100
104 public function getPrimaryKeyIdentifier()
105 {
106 return "pk";
107 }
108
112 public function supportsFulltext()
113 {
114 return false;
115 }
116
124 public function replace($a_table, $a_pk_columns, $a_other_columns)
125 {
126 $a_columns = array_merge($a_pk_columns, $a_other_columns);
127 $fields = array();
128 $field_values = array();
129 $placeholders = array();
130 $types = array();
131 $values = array();
132 $lobs = false;
133 $lob = array();
134 $val_field = array();
135 $a = array();
136 $b = array();
137 foreach ($a_columns as $k => $col) {
138 if ($col[0] == 'clob' or $col[0] == 'blob') {
139 $val_field[] = $this->quote($col[1], 'text') . " " . $k;
140 } else {
141 $val_field[] = $this->quote($col[1], $col[0]) . " " . $k;
142 }
143 $fields[] = $k;
144 $placeholders[] = "%s";
145 $placeholders2[] = ":$k";
146 $types[] = $col[0];
147 $values[] = $col[1];
148 $field_values[$k] = $col[1];
149 if ($col[0] == "blob" || $col[0] == "clob") {
150 $lobs = true;
151 $lob[$k] = $k;
152 }
153 $a[] = "a." . $k;
154 $b[] = "b." . $k;
155 }
156 $abpk = array();
157 $aboc = array();
158 $delwhere = array();
159 foreach ($a_pk_columns as $k => $col) {
160 $abpk[] = "a." . $k . " = b." . $k;
161 $delwhere[] = $k . " = " . $this->quote($col[1], $col[0]);
162 }
163 foreach ($a_other_columns as $k => $col) {
164 $aboc[] = "a." . $k . " = b." . $k;
165 }
166 // if ($lobs) // lobs -> use prepare execute (autoexecute broken in PEAR 2.4.1)
167 // {
168 $this->manipulate(
169 "DELETE FROM " . $a_table . " WHERE " .
170 implode($delwhere, " AND ")
171 );
172 $this->insert($a_table, $a_columns);
173
174 //$r = $this->db->extended->autoExecute($a_table, $field_values, MDB2_AUTOQUERY_INSERT, null, $types);
175 $this->handleError($r, "replace, delete/insert(" . $a_table . ")");
176 // }
177 /* else // if no lobs are used, use manipulate
178 {
179 $q = "MERGE INTO ".$a_table." a ".
180 "USING (SELECT ".implode($val_field, ", ")." ".
181 "FROM DUAL) b ON (".implode($abpk, " AND ").") ".
182 "WHEN MATCHED THEN UPDATE SET ".implode($aboc, ", ")." ".
183 "WHEN NOT MATCHED THEN INSERT (".implode($a, ",").") VALUES (".implode($b, ",").")";
184 $r = $this->manipulate($q);
185 }*/
186 return $r;
187 }
188
199 public function lockTables($a_tables)
200 {
201 global $ilLog;
202
203 $locks = array();
204
205 $counter = 0;
206 foreach ($a_tables as $table) {
207 $lock = 'LOCK TABLE ';
208
209 $lock .= ($table['name'] . ' ');
210
211 switch ($table['type']) {
213 $lock .= ' IN SHARE MODE ';
214 break;
215
217 $lock .= ' IN EXCLUSIVE MODE ';
218 break;
219 }
220
221 $locks[] = $lock;
222 }
223
224 // @TODO use and store a unique identifier to allow nested lock/unlocks
225 $this->db->beginTransaction();
226 foreach ($locks as $lock) {
227 $this->db->query($lock);
228 if ($ilLog instanceof ilLog) {
229 $ilLog->write(__METHOD__ . ': ' . $lock);
230 }
231 }
232 return true;
233 }
234
240 public function unlockTables()
241 {
242 $this->db->commit();
243 }
244
245
246 public function getStorageEngine()
247 {
248 return null;
249 }
250
251
252 public function dropFulltextIndex($a_table, $a_name)
253 {
254 return false;
255 }
256
257
258 public function setStorageEngine($storage_engine)
259 {
260 return false;
261 }
262
263
271 public function groupConcat($a_field_name, $a_seperator = ",", $a_order = null)
272 {
273 if ($a_order === null) {
274 $sql = "STRING_AGG(" . $a_field_name . ", " . $this->quote($a_seperator, "text") . ")";
275 } else {
276 $sql = "STRING_AGG(" . $a_field_name . ", " . $this->quote($a_seperator, "text") . " ORDER BY " . $a_order . ")";
277 }
278 return $sql;
279 }
280
281
289 public function locate($a_needle, $a_string, $a_start_pos = 1) : string
290 {
291 $manager = $this->db->loadModule('Manager');
292 return $manager->getQueryUtils()->locate($a_needle, $a_string, $a_start_pos);
293 }
294}
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)
locate($a_needle, $a_string, $a_start_pos=1)
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
getDBUser()
Get database user.
Definition: class.ilDB.php:83
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
if($modEnd===false) $module
Definition: module.php:59
if(empty($password)) $table
Definition: pwgen.php:24