ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilPostgresQueryUtils.php
Go to the documentation of this file.
1<?php
2require_once('./Services/Database/classes/QueryUtils/class.ilQueryUtils.php');
3
10
18 public function createTable($name, $fields, $options = array()) {
19 if (!$name) {
20 throw new ilDatabaseException('no valid table name specified');
21 }
22 if (empty($fields)) {
23 throw new ilDatabaseException('no fields specified for table "' . $name . '"');
24 }
25 $query_fields_array = array();
26 foreach ($fields as $field_name => $field) {
27 $query_fields_array[] = $this->db_instance->getFieldDefinition()->getDeclaration($field['type'], $field_name, $field);
28 }
29
30 $query_fields = implode(', ', $query_fields_array);
31
32 if (!empty($options['primary'])) {
33 $query_fields .= ', PRIMARY KEY (' . implode(', ', array_keys($options['primary'])) . ')';
34 }
35
36 $query = "CREATE TABLE $name ($query_fields)";
37
38 $options_strings = array();
39
40 if (!empty($options['comment'])) {
41 $options_strings['comment'] = 'COMMENT = ' . $this->quote($options['comment'], 'text');
42 }
43
44 if (!empty($options['charset'])) {
45 $options_strings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
46 if (!empty($options['collate'])) {
47 $options_strings['charset'] .= ' COLLATE ' . $options['collate'];
48 }
49 }
50
51 $type = false;
52 if (!empty($options['type'])) {
53 $type = $options['type'];
54 }
55 if ($type) {
56 $options_strings[] = "ENGINE = $type";
57 }
58
59 if (!empty($options_strings)) {
60 $query .= ' ' . implode(' ', $options_strings);
61 }
62
63 return $query;
64 }
65
66
74 public function in($field, $values, $negate = false, $type = "") {
75 if (count($values) == 0) {
76 // BEGIN fixed mantis #0014191:
77 //return " 1=2 "; // return a false statement on empty array
78 return $negate ? ' 1=1 ' : ' 1=2 ';
79 // END fixed mantis #0014191:
80 }
81 if ($type == "") // untyped: used ? for prepare/execute
82 {
83 $str = $field . (($negate) ? " NOT" : "") . " IN (?" . str_repeat(",?", count($values) - 1) . ")";
84 } else // typed, use values for query/manipulate
85 {
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 return $this->db_instance->quote($value, $type);
106 }
107
108
114 public function concat(array $values, $allow_null = true) {
115 if (!count($values)) {
116 return ' ';
117 }
118
119 $concat = ' CONCAT(';
120 $first = true;
121 foreach ($values as $field_info) {
122 $val = $field_info[0];
123
124 if (!$first) {
125 $concat .= ',';
126 }
127
128 if ($allow_null) {
129 $concat .= 'COALESCE(';
130 }
131 $concat .= $val;
132
133 if ($allow_null) {
134 $concat .= ",''";
135 $concat .= ')';
136 }
137
138 $first = false;
139 }
140 $concat .= ') ';
141
142 return $concat;
143 }
144
145
152 public function locate($a_needle, $a_string, $a_start_pos = 1) {
153 $locate = ' LOCATE( ';
154 $locate .= $a_needle;
155 $locate .= ',';
156 $locate .= $a_string;
157 $locate .= ',';
158 $locate .= $a_start_pos;
159 $locate .= ') ';
160
161 return $locate;
162 }
163
164
169 public function free(ilPDOStatement $statement) {
170 $statement->closeCursor();
171
172 return true;
173 }
174
175
180 public function quoteIdentifier($identifier) {
181 return $this->db_instance->quoteIdentifier($identifier);
182 }
183
184
193 public function like($column, $type, $value = "?", $case_insensitive = true) {
194 if (!in_array($type, array(
197 "blob",
198 ))
199 ) {
200 throw new ilDatabaseException("Like: Invalid column type '" . $type . "'.");
201 }
202 if ($value == "?") {
203 if ($case_insensitive) {
204 return "UPPER(" . $column . ") LIKE(UPPER(?))";
205 } else {
206 return $column . " LIKE(?)";
207 }
208 } else {
209 if ($case_insensitive) {
210 // Always quote as text
211 return " UPPER(" . $column . ") LIKE(UPPER(" . $this->quote($value, 'text') . "))";
212 } else {
213 // Always quote as text
214 return " " . $column . " LIKE(" . $this->quote($value, 'text') . ")";
215 }
216 }
217 }
218
219
223 public function now() {
224 return "now()";
225 }
226
227
232 public function lock(array $tables) {
233 $lock = 'LOCK TABLES ';
234
235 $counter = 0;
236 foreach ($tables as $table) {
237 if ($counter ++) {
238 $lock .= ', ';
239 }
240
241 if (isset($table['sequence']) && $table['sequence']) {
242 $table_name = $this->db_instance->getSequenceName($table['name']);
243 } else {
244 $table_name = $table['name'];
245 }
246
247 $lock .= ($table_name . ' ');
248
249 if ($table['alias']) {
250 $lock .= ($table['alias'] . ' ');
251 }
252
253 switch ($table['type']) {
255 $lock .= ' READ ';
256 break;
257
259 $lock .= ' WRITE ';
260 break;
261 }
262 }
263
264 return $lock;
265 }
266
267
271 public function unlock() {
272 return 'UNLOCK TABLES';
273 }
274
275
282 public function createDatabase($a_name, $a_charset = "utf8", $a_collation = "") {
283 if ($a_collation != "") {
284 $sql = "CREATE DATABASE " . $a_name . " CHARACTER SET " . $a_charset . " COLLATE " . $a_collation;
285 } else {
286 $sql = "CREATE DATABASE " . $a_name . " CHARACTER SET " . $a_charset;
287 }
288
289 return $sql;
290 }
291
292
300 public function groupConcat($a_field_name, $a_seperator = ",", $a_order = NULL) {
301 if ($a_order === NULL) {
302 $sql = "STRING_AGG(" . $a_field_name . ", " . $this->quote($a_seperator, "text") . ")";
303 } else {
304 $sql = "STRING_AGG(" . $a_field_name . ", " . $this->quote($a_seperator, "text") . " ORDER BY " . $a_order . ")";
305
306 }
307 return $sql;
308 }
309
310
314 public function cast($a_field_name, $a_dest_type) {
315 return "CAST({$a_field_name} AS " . $this->db_instance->getFieldDefinition()->getTypeDeclaration(array("type" => $a_dest_type)) . ")";
316 }
317
318}
$column
Definition: 39dropdown.php:62
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)
groupConcat($a_field_name, $a_seperator=",", $a_order=NULL)
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())
cast($a_field_name, $a_dest_type)
string
Class ilQueryUtils.
$counter
if(!is_array($argv)) $options