ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ilMySQLQueryUtils Class Reference

Class ilMySQLQueryUtils. More...

+ Inheritance diagram for ilMySQLQueryUtils:
+ Collaboration diagram for ilMySQLQueryUtils:

Public Member Functions

 in ($field, $values, $negate=false, $type="")
 
 quote ($value, $type=null)
 
 concat (array $values, $allow_null=true)
 
 locate ($a_needle, $a_string, $a_start_pos=1)
 
 free (ilPDOStatement $statement)
 
 quoteIdentifier ($identifier)
 
 createTable ($name, $fields, $options=array())
 
 like ($column, $type, $value="?", $case_insensitive=true)
 
 now ()
 
 lock (array $tables)
 
 unlock ()
 
 createDatabase ($a_name, $a_charset="utf8", $a_collation="")
 
 groupConcat ($a_field_name, $a_seperator=",", $a_order=NULL)
 
 cast ($a_field_name, $a_dest_type)
 
Parameters
string$a_field_name
mixed$a_dest_type
Returns
string
More...
 
- Public Member Functions inherited from ilQueryUtils
 __construct (ilDBInterface $ilDBInterface)
 ilMySQLQueryUtils constructor. More...
 
 in ($field, $values, $negate=false, $type="")
 
 quote ($value, $type=null)
 
 concat (array $values, $allow_null=true)
 
 locate ($a_needle, $a_string, $a_start_pos=1)
 
 free (ilPDOStatement $statement)
 
 quoteIdentifier ($identifier)
 
 createTable ($name, $fields, $options=array())
 
 like ($column, $type, $value="?", $case_insensitive=true)
 
 now ()
 
 lock (array $tables)
 
 unlock ()
 
 createDatabase ($a_name, $a_charset="utf8", $a_collation="")
 
 groupConcat ($a_field_name, $a_seperator=",", $a_order=NULL)
 
Parameters
string$a_field_name
string$a_seperator
string$a_order
Returns
string
More...
 
 cast ($a_field_name, $a_dest_type)
 
Parameters
string$a_field_name
mixed$a_dest_type
Returns
string
More...
 

Additional Inherited Members

- Protected Attributes inherited from ilQueryUtils
 $db_instance
 

Detailed Description

Member Function Documentation

◆ cast()

ilMySQLQueryUtils::cast (   $a_field_name,
  $a_dest_type 
)

Parameters
string$a_field_name
mixed$a_dest_type
Returns
string

Implements ilQueryUtilsInterface.

Definition at line 314 of file class.ilMySQLQueryUtils.php.

314  {
315  return $a_field_name;
316  }

◆ concat()

ilMySQLQueryUtils::concat ( array  $values,
  $allow_null = true 
)
Parameters
array$values
bool$allow_null
Returns
string

Implements ilQueryUtilsInterface.

Definition at line 58 of file class.ilMySQLQueryUtils.php.

58  {
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  }

◆ createDatabase()

ilMySQLQueryUtils::createDatabase (   $a_name,
  $a_charset = "utf8",
  $a_collation = "" 
)
Parameters
$a_name
string$a_charset
string$a_collation
Returns
mixed

Implements ilQueryUtilsInterface.

Definition at line 282 of file class.ilMySQLQueryUtils.php.

282  {
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  }

◆ createTable()

ilMySQLQueryUtils::createTable (   $name,
  $fields,
  $options = array() 
)
Parameters
$name
$fields
array$options
Returns
string
Exceptions

Implements ilQueryUtilsInterface.

Definition at line 136 of file class.ilMySQLQueryUtils.php.

References $options, $query, array, and quote().

136  {
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  }
quote($value, $type=null)
Class ilDatabaseException.
if(!is_array($argv)) $options
Create styles array
The data for the language used.
+ Here is the call graph for this function:

◆ free()

ilMySQLQueryUtils::free ( ilPDOStatement  $statement)
Parameters
\ilPDOStatement$statement
Returns
bool

Implements ilQueryUtilsInterface.

Definition at line 113 of file class.ilMySQLQueryUtils.php.

References ilPDOStatement\closeCursor().

113  {
114  $statement->closeCursor();
115 
116  return true;
117  }
closeCursor()
Pdo allows for a manual closing of the cursor.
+ Here is the call graph for this function:

◆ groupConcat()

ilMySQLQueryUtils::groupConcat (   $a_field_name,
  $a_seperator = ",",
  $a_order = NULL 
)
Parameters
string$a_field_name
string$a_seperator
string$a_order
Returns
string

Implements ilQueryUtilsInterface.

Definition at line 300 of file class.ilMySQLQueryUtils.php.

References quote().

300  {
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  }
quote($value, $type=null)
+ Here is the call graph for this function:

◆ in()

ilMySQLQueryUtils::in (   $field,
  $values,
  $negate = false,
  $type = "" 
)
Parameters
string$field
string[]$values
bool$negate
string$type
Returns
string

Implements ilQueryUtilsInterface.

Definition at line 18 of file class.ilMySQLQueryUtils.php.

References quote().

18  {
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  }
quote($value, $type=null)
+ Here is the call graph for this function:

◆ like()

ilMySQLQueryUtils::like (   $column,
  $type,
  $value = "?",
  $case_insensitive = true 
)
Parameters
$column
$type
string$value
bool$case_insensitive
Returns
string
Exceptions

Implements ilQueryUtilsInterface.

Definition at line 193 of file class.ilMySQLQueryUtils.php.

References $column, array, quote(), ilDBConstants\T_CLOB, and ilDBConstants\T_TEXT.

193  {
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  }
quote($value, $type=null)
Class ilDatabaseException.
$column
Definition: 39dropdown.php:62
Create styles array
The data for the language used.
+ Here is the call graph for this function:

◆ locate()

ilMySQLQueryUtils::locate (   $a_needle,
  $a_string,
  $a_start_pos = 1 
)
Parameters
$a_needle
$a_string
int$a_start_pos
Returns
string

Implements ilQueryUtilsInterface.

Definition at line 96 of file class.ilMySQLQueryUtils.php.

96  {
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  }

◆ lock()

ilMySQLQueryUtils::lock ( array  $tables)
Parameters
array$tables
Returns
string

Implements ilQueryUtilsInterface.

Definition at line 232 of file class.ilMySQLQueryUtils.php.

References $counter, ilDBConstants\LOCK_READ, and ilDBConstants\LOCK_WRITE.

232  {
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  }
$counter

◆ now()

ilMySQLQueryUtils::now ( )
Returns
string

Implements ilQueryUtilsInterface.

Definition at line 223 of file class.ilMySQLQueryUtils.php.

223  {
224  return "NOW()";
225  }

◆ quote()

ilMySQLQueryUtils::quote (   $value,
  $type = null 
)
Parameters
mixed$value
null$type
Returns
string

Implements ilQueryUtilsInterface.

Definition at line 48 of file class.ilMySQLQueryUtils.php.

Referenced by createTable(), groupConcat(), in(), and like().

48  {
49  return $this->db_instance->quote($value, $type);
50  }
+ Here is the caller graph for this function:

◆ quoteIdentifier()

ilMySQLQueryUtils::quoteIdentifier (   $identifier)
Parameters
$identifier
Returns
string

Implements ilQueryUtilsInterface.

Definition at line 124 of file class.ilMySQLQueryUtils.php.

124  {
125  return $this->db_instance->quoteIdentifier($identifier);
126  }

◆ unlock()

ilMySQLQueryUtils::unlock ( )
Returns
string

Implements ilQueryUtilsInterface.

Definition at line 271 of file class.ilMySQLQueryUtils.php.

271  {
272  return 'UNLOCK TABLES';
273  }

The documentation for this class was generated from the following file: