ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilMySQLQueryUtils.php
Go to the documentation of this file.
1<?php
2
9{
10
18 public function in($field, $values, $negate = false, $type = "")
19 {
20 if (!is_array($values) || count($values) == 0) {
21 // BEGIN fixed mantis #0014191:
22 //return " 1=2 "; // return a false statement on empty array
23 return $negate ? ' 1=1 ' : ' 1=2 ';
24 // END fixed mantis #0014191:
25 }
26 if ($type == "") { // untyped: used ? for prepare/execute
27 $str = $field . (($negate) ? " NOT" : "") . " IN (?" . str_repeat(",?", count($values) - 1) . ")";
28 } else { // typed, use values for query/manipulate
29 $str = $field . (($negate) ? " NOT" : "") . " IN (";
30 $sep = "";
31 foreach ($values as $v) {
32 $str .= $sep . $this->quote($v, $type);
33 $sep = ",";
34 }
35 $str .= ")";
36 }
37
38 return $str;
39 }
40
41
47 public function quote($value, $type = null)
48 {
49 return $this->db_instance->quote($value, $type);
50 }
51
52
58 public function concat(array $values, $allow_null = true)
59 {
60 if (!count($values)) {
61 return ' ';
62 }
63
64 $concat = ' CONCAT(';
65 $first = true;
66 foreach ($values as $field_info) {
67 $val = $field_info[0];
68
69 if (!$first) {
70 $concat .= ',';
71 }
72
73 if ($allow_null) {
74 $concat .= 'COALESCE(';
75 }
76 $concat .= $val;
77
78 if ($allow_null) {
79 $concat .= ",''";
80 $concat .= ')';
81 }
82
83 $first = false;
84 }
85 $concat .= ') ';
86
87 return $concat;
88 }
89
90
97 public function locate($a_needle, $a_string, $a_start_pos = 1)
98 {
99 $locate = ' LOCATE( ';
100 $locate .= $a_needle;
101 $locate .= ',';
102 $locate .= $a_string;
103 $locate .= ',';
104 $locate .= $a_start_pos;
105 $locate .= ') ';
106
107 return $locate;
108 }
109
110
115 public function free(ilPDOStatement $statement)
116 {
117 $statement->closeCursor();
118
119 return true;
120 }
121
122
127 public function quoteIdentifier($identifier)
128 {
129 return $this->db_instance->quoteIdentifier($identifier);
130 }
131
132
140 public function createTable($name, $fields, $options = array())
141 {
142 if (!$name) {
143 throw new ilDatabaseException('no valid table name specified');
144 }
145 if (empty($fields)) {
146 throw new ilDatabaseException('no fields specified for table "' . $name . '"');
147 }
148 $query_fields_array = array();
149 foreach ($fields as $field_name => $field) {
150 $query_fields_array[] = $this->db_instance->getFieldDefinition()->getDeclaration($field['type'], $field_name, $field);
151 }
152
153 $query_fields = implode(', ', $query_fields_array);
154
155 if (!empty($options['primary'])) {
156 $query_fields .= ', PRIMARY KEY (' . implode(', ', array_keys($options['primary'])) . ')';
157 }
158
159 $query = "CREATE TABLE $name ($query_fields)";
160
161 $options_strings = array();
162
163 if (!empty($options['comment'])) {
164 $options_strings['comment'] = 'COMMENT = ' . $this->quote($options['comment'], 'text');
165 }
166
167 if (!empty($options['charset'])) {
168 $options_strings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
169 if (!empty($options['collate'])) {
170 $options_strings['charset'] .= ' COLLATE ' . $options['collate'];
171 }
172 }
173
174 $type = false;
175 if (!empty($options['type'])) {
176 $type = $options['type'];
177 }
178 if ($type) {
179 $options_strings[] = "ENGINE = $type";
180 }
181
182 if (!empty($options_strings)) {
183 $query .= ' ' . implode(' ', $options_strings);
184 }
185
186 return $query;
187 }
188
189
198 public function like($column, $type, $value = "?", $case_insensitive = true)
199 {
200 if (!in_array($type, array(
203 "blob",
204 ))
205 ) {
206 throw new ilDatabaseException("Like: Invalid column type '" . $type . "'.");
207 }
208 if ($value == "?") {
209 if ($case_insensitive) {
210 return "UPPER(" . $column . ") LIKE(UPPER(?))";
211 } else {
212 return $column . " LIKE(?)";
213 }
214 } else {
215 if ($case_insensitive) {
216 // Always quote as text
217 return " UPPER(" . $column . ") LIKE(UPPER(" . $this->quote($value, 'text') . "))";
218 } else {
219 // Always quote as text
220 return " " . $column . " LIKE(" . $this->quote($value, 'text') . ")";
221 }
222 }
223 }
224
225
229 public function now()
230 {
231 return "NOW()";
232 }
233
234
239 public function lock(array $tables)
240 {
241 $lock = 'LOCK TABLES ';
242
243 $counter = 0;
244 foreach ($tables as $table) {
245 if ($counter++) {
246 $lock .= ', ';
247 }
248
249 if (isset($table['sequence']) && $table['sequence']) {
250 $table_name = $this->db_instance->getSequenceName($table['name']);
251 } else {
252 $table_name = $table['name'];
253 }
254
255 $lock .= ($table_name . ' ');
256
257 if ($table['alias'] ?? null) {
258 $lock .= ($table['alias'] . ' ');
259 }
260
261 switch ($table['type']) {
263 $lock .= ' READ ';
264 break;
265
267 $lock .= ' WRITE ';
268 break;
269 }
270 }
271
272 return $lock;
273 }
274
275
279 public function unlock()
280 {
281 return 'UNLOCK TABLES';
282 }
283
284
291 public function createDatabase($a_name, $a_charset = "utf8", $a_collation = "")
292 {
293 if ($a_collation != "") {
294 $sql = "CREATE DATABASE `" . $a_name . "` CHARACTER SET " . $a_charset . " COLLATE " . $a_collation;
295 } else {
296 $sql = "CREATE DATABASE `" . $a_name . "` CHARACTER SET " . $a_charset;
297 }
298
299 return $sql;
300 }
301
302
310 public function groupConcat($a_field_name, $a_seperator = ",", $a_order = null)
311 {
312 if ($a_order === null) {
313 $sql = "GROUP_CONCAT(" . $a_field_name . " SEPARATOR " . $this->quote($a_seperator, "text") . ")";
314 } else {
315 $sql = "GROUP_CONCAT(" . $a_field_name . " ORDER BY " . $a_order . " SEPARATOR " . $this->quote($a_seperator, "text") . ")";
316 }
317 return $sql;
318 }
319
320
324 public function cast($a_field_name, $a_dest_type)
325 {
326 return $a_field_name;
327 }
328}
An exception for terminatinating execution or to throw for unit testing.
Class ilDatabaseException.
Class ilMySQLQueryUtils.
free(ilPDOStatement $statement)
locate($a_needle, $a_string, $a_start_pos=1)
in($field, $values, $negate=false, $type="")
concat(array $values, $allow_null=true)
quote($value, $type=null)
createTable($name, $fields, $options=array())
like($column, $type, $value="?", $case_insensitive=true)
createDatabase($a_name, $a_charset="utf8", $a_collation="")
cast($a_field_name, $a_dest_type)
string
groupConcat($a_field_name, $a_seperator=",", $a_order=null)
Class ilPDOStatement is a Wrapper Class for PDOStatement.
closeCursor()
Pdo allows for a manual closing of the cursor.
Class ilQueryUtils.
if($format !==null) $name
Definition: metadata.php:230
$query
$type