ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilMySQLQueryUtils.php
Go to the documentation of this file.
1<?php
2require_once('./Services/Database/classes/QueryUtils/class.ilQueryUtils.php');
3
10
18 public function in($field, $values, $negate = false, $type = "") {
19 if (count($values) == 0) {
20 // BEGIN fixed mantis #0014191:
21 //return " 1=2 "; // return a false statement on empty array
22 return $negate ? ' 1=1 ' : ' 1=2 ';
23 // END fixed mantis #0014191:
24 }
25 if ($type == "") // untyped: used ? for prepare/execute
26 {
27 $str = $field . (($negate) ? " NOT" : "") . " IN (?" . str_repeat(",?", count($values) - 1) . ")";
28 } else // typed, use values for query/manipulate
29 {
30 $str = $field . (($negate) ? " NOT" : "") . " IN (";
31 $sep = "";
32 foreach ($values as $v) {
33 $str .= $sep . $this->quote($v, $type);
34 $sep = ",";
35 }
36 $str .= ")";
37 }
38
39 return $str;
40 }
41
42
48 public function quote($value, $type = null) {
49 return $this->db_instance->quote($value, $type);
50 }
51
52
58 public function concat(array $values, $allow_null = true) {
59 if (!count($values)) {
60 return ' ';
61 }
62
63 $concat = ' CONCAT(';
64 $first = true;
65 foreach ($values as $field_info) {
66 $val = $field_info[0];
67
68 if (!$first) {
69 $concat .= ',';
70 }
71
72 if ($allow_null) {
73 $concat .= 'COALESCE(';
74 }
75 $concat .= $val;
76
77 if ($allow_null) {
78 $concat .= ",''";
79 $concat .= ')';
80 }
81
82 $first = false;
83 }
84 $concat .= ') ';
85
86 return $concat;
87 }
88
89
96 public function locate($a_needle, $a_string, $a_start_pos = 1) {
97 $locate = ' LOCATE( ';
98 $locate .= $a_needle;
99 $locate .= ',';
100 $locate .= $a_string;
101 $locate .= ',';
102 $locate .= $a_start_pos;
103 $locate .= ') ';
104
105 return $locate;
106 }
107
108
113 public function free(ilPDOStatement $statement) {
114 $statement->closeCursor();
115
116 return true;
117 }
118
119
124 public function quoteIdentifier($identifier) {
125 return $this->db_instance->quoteIdentifier($identifier);
126 }
127
128
136 public function createTable($name, $fields, $options = array()) {
137 if (!$name) {
138 throw new ilDatabaseException('no valid table name specified');
139 }
140 if (empty($fields)) {
141 throw new ilDatabaseException('no fields specified for table "' . $name . '"');
142 }
143 $query_fields_array = array();
144 foreach ($fields as $field_name => $field) {
145 $query_fields_array[] = $this->db_instance->getFieldDefinition()->getDeclaration($field['type'], $field_name, $field);
146 }
147
148 $query_fields = implode(', ', $query_fields_array);
149
150 if (!empty($options['primary'])) {
151 $query_fields .= ', PRIMARY KEY (' . implode(', ', array_keys($options['primary'])) . ')';
152 }
153
154 $query = "CREATE TABLE $name ($query_fields)";
155
156 $options_strings = array();
157
158 if (!empty($options['comment'])) {
159 $options_strings['comment'] = 'COMMENT = ' . $this->quote($options['comment'], 'text');
160 }
161
162 if (!empty($options['charset'])) {
163 $options_strings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
164 if (!empty($options['collate'])) {
165 $options_strings['charset'] .= ' COLLATE ' . $options['collate'];
166 }
167 }
168
169 $type = false;
170 if (!empty($options['type'])) {
171 $type = $options['type'];
172 }
173 if ($type) {
174 $options_strings[] = "ENGINE = $type";
175 }
176
177 if (!empty($options_strings)) {
178 $query .= ' ' . implode(' ', $options_strings);
179 }
180
181 return $query;
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 = "GROUP_CONCAT(" . $a_field_name . " SEPARATOR " . $this->quote($a_seperator, "text") . ")";
303 } else {
304 $sql = "GROUP_CONCAT(" . $a_field_name . " ORDER BY " . $a_order . " SEPARATOR " . $this->quote($a_seperator, "text"). ")";
305
306 }
307 return $sql;
308 }
309
310
314 public function cast($a_field_name, $a_dest_type) {
315 return $a_field_name;
316 }
317
318}
$column
Definition: 39dropdown.php:62
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="")
groupConcat($a_field_name, $a_seperator=",", $a_order=NULL)
cast($a_field_name, $a_dest_type)
string
Class ilPDOStatement is a Wrapper Class for PDOStatement.
closeCursor()
Pdo allows for a manual closing of the cursor.
Class ilQueryUtils.
$counter
if(!is_array($argv)) $options