ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilMySQLQueryUtils.php
Go to the documentation of this file.
1 <?php
2 require_once('./Services/Database/classes/QueryUtils/class.ilQueryUtils.php');
3 
10 {
11 
19  public function in($field, $values, $negate = false, $type = "")
20  {
21  if (count($values) == 0) {
22  // BEGIN fixed mantis #0014191:
23  //return " 1=2 "; // return a false statement on empty array
24  return $negate ? ' 1=1 ' : ' 1=2 ';
25  // END fixed mantis #0014191:
26  }
27  if ($type == "") { // untyped: used ? for prepare/execute
28  $str = $field . (($negate) ? " NOT" : "") . " IN (?" . str_repeat(",?", count($values) - 1) . ")";
29  } else { // typed, use values for query/manipulate
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  {
50  return $this->db_instance->quote($value, $type);
51  }
52 
53 
59  public function concat(array $values, $allow_null = true)
60  {
61  if (!count($values)) {
62  return ' ';
63  }
64 
65  $concat = ' CONCAT(';
66  $first = true;
67  foreach ($values as $field_info) {
68  $val = $field_info[0];
69 
70  if (!$first) {
71  $concat .= ',';
72  }
73 
74  if ($allow_null) {
75  $concat .= 'COALESCE(';
76  }
77  $concat .= $val;
78 
79  if ($allow_null) {
80  $concat .= ",''";
81  $concat .= ')';
82  }
83 
84  $first = false;
85  }
86  $concat .= ') ';
87 
88  return $concat;
89  }
90 
91 
98  public function locate($a_needle, $a_string, $a_start_pos = 1)
99  {
100  $locate = ' LOCATE( ';
101  $locate .= $a_needle;
102  $locate .= ',';
103  $locate .= $a_string;
104  $locate .= ',';
105  $locate .= $a_start_pos;
106  $locate .= ') ';
107 
108  return $locate;
109  }
110 
111 
116  public function free(ilPDOStatement $statement)
117  {
118  $statement->closeCursor();
119 
120  return true;
121  }
122 
123 
128  public function quoteIdentifier($identifier)
129  {
130  return $this->db_instance->quoteIdentifier($identifier);
131  }
132 
133 
141  public function createTable($name, $fields, $options = array())
142  {
143  if (!$name) {
144  throw new ilDatabaseException('no valid table name specified');
145  }
146  if (empty($fields)) {
147  throw new ilDatabaseException('no fields specified for table "' . $name . '"');
148  }
149  $query_fields_array = array();
150  foreach ($fields as $field_name => $field) {
151  $query_fields_array[] = $this->db_instance->getFieldDefinition()->getDeclaration($field['type'], $field_name, $field);
152  }
153 
154  $query_fields = implode(', ', $query_fields_array);
155 
156  if (!empty($options['primary'])) {
157  $query_fields .= ', PRIMARY KEY (' . implode(', ', array_keys($options['primary'])) . ')';
158  }
159 
160  $query = "CREATE TABLE $name ($query_fields)";
161 
162  $options_strings = array();
163 
164  if (!empty($options['comment'])) {
165  $options_strings['comment'] = 'COMMENT = ' . $this->quote($options['comment'], 'text');
166  }
167 
168  if (!empty($options['charset'])) {
169  $options_strings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
170  if (!empty($options['collate'])) {
171  $options_strings['charset'] .= ' COLLATE ' . $options['collate'];
172  }
173  }
174 
175  $type = false;
176  if (!empty($options['type'])) {
177  $type = $options['type'];
178  }
179  if ($type) {
180  $options_strings[] = "ENGINE = $type";
181  }
182 
183  if (!empty($options_strings)) {
184  $query .= ' ' . implode(' ', $options_strings);
185  }
186 
187  return $query;
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 = "GROUP_CONCAT(" . $a_field_name . " SEPARATOR " . $this->quote($a_seperator, "text") . ")";
315  } else {
316  $sql = "GROUP_CONCAT(" . $a_field_name . " ORDER BY " . $a_order . " SEPARATOR " . $this->quote($a_seperator, "text") . ")";
317  }
318  return $sql;
319  }
320 
321 
325  public function cast($a_field_name, $a_dest_type)
326  {
327  return $a_field_name;
328  }
329 }
locate($a_needle, $a_string, $a_start_pos=1)
quote($value, $type=null)
$type
Class ilPDOStatement is a Wrapper Class for PDOStatement.
createTable($name, $fields, $options=array())
createDatabase($a_name, $a_charset="utf8", $a_collation="")
Class ilDatabaseException.
$counter
if($format !==null) $name
Definition: metadata.php:146
free(ilPDOStatement $statement)
$column
Definition: 39dropdown.php:62
groupConcat($a_field_name, $a_seperator=",", $a_order=null)
closeCursor()
Pdo allows for a manual closing of the cursor.
like($column, $type, $value="?", $case_insensitive=true)
$query
Create styles array
The data for the language used.
Class ilMySQLQueryUtils.
cast($a_field_name, $a_dest_type)
string
concat(array $values, $allow_null=true)
in($field, $values, $negate=false, $type="")
if(empty($password)) $table
Definition: pwgen.php:24
Class ilQueryUtils.
if(!isset($_REQUEST['ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20