ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilPostgresQueryUtils.php
Go to the documentation of this file.
1<?php
2require_once('./Services/Database/classes/QueryUtils/class.ilQueryUtils.php');
3
10{
11
19 public function createTable($name, $fields, $options = array())
20 {
21 if (!$name) {
22 throw new ilDatabaseException('no valid table name specified');
23 }
24 if (empty($fields)) {
25 throw new ilDatabaseException('no fields specified for table "' . $name . '"');
26 }
27 $query_fields_array = array();
28 foreach ($fields as $field_name => $field) {
29 $query_fields_array[] = $this->db_instance->getFieldDefinition()->getDeclaration($field['type'], $field_name, $field);
30 }
31
32 $query_fields = implode(', ', $query_fields_array);
33
34 if (!empty($options['primary'])) {
35 $query_fields .= ', PRIMARY KEY (' . implode(', ', array_keys($options['primary'])) . ')';
36 }
37
38 $query = "CREATE TABLE $name ($query_fields)";
39
40 $options_strings = array();
41
42 if (!empty($options['comment'])) {
43 $options_strings['comment'] = 'COMMENT = ' . $this->quote($options['comment'], 'text');
44 }
45
46 if (!empty($options['charset'])) {
47 $options_strings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
48 if (!empty($options['collate'])) {
49 $options_strings['charset'] .= ' COLLATE ' . $options['collate'];
50 }
51 }
52
53 $type = false;
54 if (!empty($options['type'])) {
55 $type = $options['type'];
56 }
57 if ($type) {
58 $options_strings[] = "ENGINE = $type";
59 }
60
61 if (!empty($options_strings)) {
62 $query .= ' ' . implode(' ', $options_strings);
63 }
64
65 return $query;
66 }
67
68
76 public function in($field, $values, $negate = false, $type = "")
77 {
78 if (count($values) == 0) {
79 // BEGIN fixed mantis #0014191:
80 //return " 1=2 "; // return a false statement on empty array
81 return $negate ? ' 1=1 ' : ' 1=2 ';
82 // END fixed mantis #0014191:
83 }
84 if ($type == "") { // untyped: used ? for prepare/execute
85 $str = $field . (($negate) ? " NOT" : "") . " IN (?" . str_repeat(",?", count($values) - 1) . ")";
86 } else { // typed, use values for query/manipulate
87 $str = $field . (($negate) ? " NOT" : "") . " IN (";
88 $sep = "";
89 foreach ($values as $v) {
90 $str .= $sep . $this->quote($v, $type);
91 $sep = ",";
92 }
93 $str .= ")";
94 }
95
96 return $str;
97 }
98
99
105 public function quote($value, $type = null)
106 {
107 return $this->db_instance->quote($value, $type);
108 }
109
110
116 public function concat(array $values, $allow_null = true)
117 {
118 if (!count($values)) {
119 return ' ';
120 }
121
122 $concat = ' CONCAT(';
123 $first = true;
124 foreach ($values as $field_info) {
125 $val = $field_info[0];
126
127 if (!$first) {
128 $concat .= ',';
129 }
130
131 if ($allow_null) {
132 $concat .= 'COALESCE(';
133 }
134 $concat .= $val;
135
136 if ($allow_null) {
137 $concat .= ",''";
138 $concat .= ')';
139 }
140
141 $first = false;
142 }
143 $concat .= ') ';
144
145 return $concat;
146 }
147
148
155 public function locate($a_needle, $a_string, $a_start_pos = 1)
156 {
157 $locate = ' STRPOS(SUBSTR(';
158 $locate .= $a_string;
159 $locate .= ', ';
160 $locate .= $a_start_pos;
161 $locate .= '), ';
162 $locate .= $a_needle;
163 $locate .= ') + ';
164 $locate .= --$a_start_pos;
165
166 return $locate;
167 }
168
169
174 public function free(ilPDOStatement $statement)
175 {
176 $statement->closeCursor();
177
178 return true;
179 }
180
181
186 public function quoteIdentifier($identifier)
187 {
188 return $this->db_instance->quoteIdentifier($identifier);
189 }
190
191
200 public function like($column, $type, $value = "?", $case_insensitive = true)
201 {
202 if (!in_array($type, array(
205 "blob",
206 ))
207 ) {
208 throw new ilDatabaseException("Like: Invalid column type '" . $type . "'.");
209 }
210 if ($value == "?") {
211 if ($case_insensitive) {
212 return "UPPER(" . $column . ") LIKE(UPPER(?))";
213 } else {
214 return $column . " LIKE(?)";
215 }
216 } else {
217 if ($case_insensitive) {
218 // Always quote as text
219 return " UPPER(" . $column . ") LIKE(UPPER(" . $this->quote($value, 'text') . "))";
220 } else {
221 // Always quote as text
222 return " " . $column . " LIKE(" . $this->quote($value, 'text') . ")";
223 }
224 }
225 }
226
227
231 public function now()
232 {
233 return "now()";
234 }
235
236
241 public function lock(array $tables)
242 {
243 $lock = 'LOCK TABLES ';
244
245 $counter = 0;
246 foreach ($tables as $table) {
247 if ($counter++) {
248 $lock .= ', ';
249 }
250
251 if (isset($table['sequence']) && $table['sequence']) {
252 $table_name = $this->db_instance->getSequenceName($table['name']);
253 } else {
254 $table_name = $table['name'];
255 }
256
257 $lock .= ($table_name . ' ');
258
259 if ($table['alias']) {
260 $lock .= ($table['alias'] . ' ');
261 }
262
263 switch ($table['type']) {
265 $lock .= ' READ ';
266 break;
267
269 $lock .= ' WRITE ';
270 break;
271 }
272 }
273
274 return $lock;
275 }
276
277
281 public function unlock()
282 {
283 return 'UNLOCK TABLES';
284 }
285
286
293 public function createDatabase($a_name, $a_charset = "utf8", $a_collation = "")
294 {
295 if ($a_collation != "") {
296 $sql = "CREATE DATABASE " . $a_name . " CHARACTER SET " . $a_charset . " COLLATE " . $a_collation;
297 } else {
298 $sql = "CREATE DATABASE " . $a_name . " CHARACTER SET " . $a_charset;
299 }
300
301 return $sql;
302 }
303
304
312 public function groupConcat($a_field_name, $a_seperator = ",", $a_order = null)
313 {
314 if ($a_order === null) {
315 $sql = "STRING_AGG(" . $a_field_name . ", " . $this->quote($a_seperator, "text") . ")";
316 } else {
317 $sql = "STRING_AGG(" . $a_field_name . ", " . $this->quote($a_seperator, "text") . " ORDER BY " . $a_order . ")";
318 }
319 return $sql;
320 }
321
322
326 public function cast($a_field_name, $a_dest_type)
327 {
328 return "CAST({$a_field_name} AS " . $this->db_instance->getFieldDefinition()->getTypeDeclaration(array("type" => $a_dest_type)) . ")";
329 }
330}
An exception for terminatinating execution or to throw for unit testing.
Class ilDatabaseException.
Class ilPDOStatement is a Wrapper Class for PDOStatement.
closeCursor()
Pdo allows for a manual closing of the cursor.
Class ilPostgresQueryUtils.
locate($a_needle, $a_string, $a_start_pos=1)
createDatabase($a_name, $a_charset="utf8", $a_collation="")
like($column, $type, $value="?", $case_insensitive=true)
in($field, $values, $negate=false, $type="")
free(ilPDOStatement $statement)
concat(array $values, $allow_null=true)
createTable($name, $fields, $options=array())
groupConcat($a_field_name, $a_seperator=",", $a_order=null)
cast($a_field_name, $a_dest_type)
string
Class ilQueryUtils.
$query
$type
if(empty($password)) $table
Definition: pwgen.php:24
$values