ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilDBPdoReverse.php
Go to the documentation of this file.
1 <?php
2 
24 class ilDBPdoReverse implements ilDBReverse
25 {
26 
30  protected $pdo;
34  protected $db_instance;
35 
36 
43  public function __construct(\PDO $pdo, ilDBPdo $db_instance)
44  {
45  $this->pdo = $pdo;
46  $this->db_instance = $db_instance;
47  }
48 
52  protected $query_utils;
53 
54 
58  public function getQueryUtils()
59  {
60  if (!$this->query_utils) {
61  $this->query_utils = new ilMySQLQueryUtils($this->db_instance);
62  }
63 
64  return $this->query_utils;
65  }
66 
72  public function getTableFieldDefinition($table_name, $field_name)
73  {
74  $return = array();
75 
76  $table = $this->db_instance->quoteIdentifier($table_name);
77  $query = "SHOW COLUMNS FROM $table LIKE " . $this->db_instance->quote($field_name);
78  $res = $this->pdo->query($query);
79  $columns = array();
80  while ($data = $res->fetch(PDO::FETCH_ASSOC)) {
81  $columns[] = $data;
82  }
83 
84  $ilDBPdoFieldDefinition = $this->db_instance->getFieldDefinition();
85 
86  foreach ($columns as $column) {
87  $column = array_change_key_case($column, CASE_LOWER);
88  $column['name'] = $column['field'];
89  unset($column['field']);
90  // if ($this->db_instance->options['portability']) {
91  // if ($this->db_instance->options['field_case'] == CASE_LOWER) {
92  // $column['name'] = strtolower($column['name']);
93  // } else {
94  // $column['name'] = strtoupper($column['name']);
95  // }
96  // } else {
97  $column = array_change_key_case($column, CASE_LOWER);
98  // }
99  if ($field_name == $column['name']) {
100  $mapped_datatype = $ilDBPdoFieldDefinition->mapNativeDatatype($column);
101 
102  list($types, $length, $unsigned, $fixed) = $mapped_datatype;
103  $notnull = false;
104  if (empty($column['null']) || $column['null'] !== 'YES') {
105  $notnull = true;
106  }
107  $default = false;
108  if (array_key_exists('default', $column)) {
109  $default = $column['default'];
110  if (is_null($default) && $notnull) {
111  $default = '';
112  }
113  }
114  $autoincrement = false;
115  if (!empty($column['extra']) && $column['extra'] == 'auto_increment') {
116  $autoincrement = true;
117  }
118 
119  $definition[0] = array(
120  'notnull' => $notnull,
121  'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type']),
122  );
123  if (!is_null($length)) {
124  $definition[0]['length'] = $length;
125  }
126  if (!is_null($unsigned)) {
127  $definition[0]['unsigned'] = $unsigned;
128  }
129  if (!is_null($fixed)) {
130  $definition[0]['fixed'] = $fixed;
131  }
132  if ($default !== false) {
133  $definition[0]['default'] = $default;
134  }
135  if ($autoincrement !== false) {
136  $definition[0]['autoincrement'] = $autoincrement;
137  }
138  foreach ($types as $key => $type) {
139  $definition[$key] = $definition[0];
140  if ($type == 'clob' || $type == 'blob') {
141  unset($definition[$key]['default']);
142  }
143  $definition[$key]['type'] = $type;
144  $definition[$key]['mdb2type'] = $type;
145  }
146 
147  return $definition;
148  }
149  }
150 
151  throw new ilDatabaseException('it was not specified an existing table column');
152  }
153 
154 
161  public function getTableIndexDefinition($table, $index_name)
162  {
163  $table = $this->db_instance->quoteIdentifier($table, true);
164  $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
165  $index_name_pdo = $this->db_instance->getIndexName($index_name);
166  $result = $this->db_instance->query(sprintf($query, $this->db_instance->quote($index_name_pdo)));
167  $data = $this->db_instance->fetchAssoc($result);
168 
169  if ($data) {
170  $index_name = $index_name_pdo;
171  }
172 
173  $result = $this->db_instance->query(sprintf($query, $this->db_instance->quote($index_name)));
174 
175  $colpos = 1;
176  $definition = array();
177  while (is_object($row = $result->fetchObject())) {
178  $row = array_change_key_case((array) $row, CASE_LOWER);
179 
180  $key_name = $row['key_name'];
181 
182 
183  if ($index_name == $key_name) {
184  if (!$row['non_unique']) {
185  throw new ilDatabaseException('it was not specified an existing table index');
186  }
187  $column_name = $row['column_name'];
188  $definition['fields'][$column_name] = array(
189  'position' => $colpos++,
190  );
191  if (!empty($row['collation'])) {
192  $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A' ? 'ascending' : 'descending');
193  }
194  }
195  }
196 
197  if (empty($definition['fields'])) {
198  throw new ilDatabaseException('it was not specified an existing table index (index does not exist)');
199  }
200 
201  return $definition;
202  }
203 
204 
211  public function getTableConstraintDefinition($table, $constraint_name)
212  {
213  $constraint_name = strtolower($constraint_name);
214  $table = $this->db_instance->quoteIdentifier($table, true);
215  $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
216 
217  if (strtolower($constraint_name) != 'primary') {
218  $constraint_name_pdo = $this->db_instance->getIndexName($constraint_name);
219  $result = $this->db_instance->query(sprintf($query, $this->db_instance->quote($constraint_name_pdo)));
220  $data = $this->db_instance->fetchAssoc($result);
221  if ($data) {
222  // apply 'idxname_format' only if the query succeeded, otherwise
223  // fallback to the given $index_name, without transformation
224  $constraint_name = strtolower($constraint_name_pdo);
225  }
226  }
227 
228  $result = $this->db_instance->query(sprintf($query, $this->db_instance->quote($constraint_name)));
229 
230  $colpos = 1;
231  $definition = array();
232  while (is_object($row = $result->fetchObject())) {
233  $row = (array) $row;
234  $row = array_change_key_case($row, CASE_LOWER);
235  $key_name = $row['key_name'];
236  if ($this->db_instance->options['portability'] ?? false) {
237  if (($this->db_instance->options['field_case'] ?? null) == CASE_LOWER) {
238  $key_name = strtolower($key_name);
239  } else {
240  $key_name = strtolower($key_name);
241  }
242  }
243  $key_name = strtolower($key_name); // FSX fix
244  if ($constraint_name == $key_name) {
245  if ($row['non_unique']) {
246  throw new ilDatabaseException(' is not an existing table constraint');
247  }
248  if (($row['key_name'] ?? '') === 'PRIMARY') {
249  $definition['primary'] = true;
250  } else {
251  $definition['unique'] = true;
252  }
253  $column_name = $row['column_name'];
254  if ($this->db_instance->options['portability'] ?? false) {
255  if (($this->db_instance->options['field_case'] ?? null) == CASE_LOWER) {
256  $column_name = strtolower($column_name);
257  } else {
258  $column_name = strtoupper($column_name);
259  }
260  }
261  $definition['fields'][$column_name] = array(
262  'position' => $colpos++,
263  );
264  if (!empty($row['collation'])) {
265  $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A' ? 'ascending' : 'descending');
266  }
267  }
268  }
269 
270  if (empty($definition['fields'])) {
271  throw new ilDatabaseException(' is not an existing table constraint');
272  }
273 
274  return $definition;
275  }
276 
277 
283  public function getTriggerDefinition($trigger)
284  {
285  throw new ilDatabaseException('not yet implemented ' . __METHOD__);
286  }
287 }
$data
Definition: storeScorm.php:23
$result
getTableFieldDefinition($table_name, $field_name)
$type
getTableIndexDefinition($table, $index_name)
Class ilDatabaseException.
Interface ilDBReverse.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
foreach($_POST as $key=> $value) $res
$query
__construct(\PDO $pdo, ilDBPdo $db_instance)
ilDBPdoReverse constructor.
Class ilMySQLQueryUtils.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getTriggerDefinition($trigger)
if(! $in) $columns
Definition: Utf8Test.php:45
getTableConstraintDefinition($table, $constraint_name)