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