ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
ilDBPdoReverse.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28{
30
31 public function __construct(protected \PDO $pdo, protected Internal $db_instance)
32 {
33 }
34
35 public function getQueryUtils(): \ilMySQLQueryUtils
36 {
37 if ($this->query_utils === null) {
38 $this->query_utils = new ilMySQLQueryUtils($this->db_instance);
39 }
40
41 return $this->query_utils;
42 }
43
47 public function getTableFieldDefinition(string $table_name, string $field_name): array
48 {
49 $table = $this->db_instance->quoteIdentifier($table_name);
50 $query = "SHOW COLUMNS FROM $table LIKE " . $this->db_instance->quote($field_name);
51 $res = $this->pdo->query($query);
52 $columns = [];
53 while ($data = $res->fetch(PDO::FETCH_ASSOC)) {
54 $columns[] = $data;
55 }
56
57 $ilDBPdoFieldDefinition = $this->db_instance->getFieldDefinition();
58
59 foreach ($columns as $column) {
60 $column = array_change_key_case($column, CASE_LOWER);
61 $column['name'] = $column['field'];
62 unset($column['field']);
63 $column = array_change_key_case($column, CASE_LOWER);
64 if ($field_name === $column['name'] && $ilDBPdoFieldDefinition !== null) {
65 [$types, $length, $unsigned, $fixed] = $ilDBPdoFieldDefinition->mapNativeDatatype($column);
66 $notnull = false;
67 if (empty($column['null']) || $column['null'] !== 'YES') {
68 $notnull = true;
69 }
70 $default = false;
71 if (array_key_exists('default', $column)) {
72 $default = $column['default'];
73 if ($notnull && is_null($default)) {
74 $default = '';
75 }
76 }
77 $autoincrement = false;
78 if (!empty($column['extra']) && $column['extra'] === 'auto_increment') {
79 $autoincrement = true;
80 }
81
82 $definition[0] = [
83 'notnull' => $notnull,
84 'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', (string) $column['type']),
85 ];
86 if (!is_null($length)) {
87 $definition[0]['length'] = $length;
88 }
89 if (!is_null($unsigned)) {
90 $definition[0]['unsigned'] = $unsigned;
91 }
92 if (!is_null($fixed)) {
93 $definition[0]['fixed'] = $fixed;
94 }
95 if ($default !== false) {
96 $definition[0]['default'] = $default;
97 }
98 if ($autoincrement) {
99 $definition[0]['autoincrement'] = $autoincrement;
100 }
101 foreach ($types as $key => $type) {
102 $definition[$key] = $definition[0];
103 if ($type === 'clob' || $type === 'blob') {
104 unset($definition[$key]['default']);
105 }
106 $definition[$key]['type'] = $type;
107 $definition[$key]['mdb2type'] = $type;
108 }
109
110 return $definition;
111 }
112 }
113
114 throw new ilDatabaseException('it was not specified an existing table column');
115 }
116
121 public function getTableIndexDefinition(string $table, string $constraint_name): array
122 {
123 $table = $this->db_instance->quoteIdentifier($table, true);
124 $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
125 $index_name_pdo = $this->db_instance->getIndexName($constraint_name);
126 $result = $this->db_instance->query(sprintf($query, $this->db_instance->quote($index_name_pdo)));
127 $data = $this->db_instance->fetchAssoc($result);
128
129 if ($data) {
130 $constraint_name = $index_name_pdo;
131 }
132
133 $result = $this->db_instance->query(sprintf($query, $this->db_instance->quote($constraint_name)));
134
135 $colpos = 1;
136 $definition = [];
137 while (is_object($row = $result->fetchObject())) {
138 $row = array_change_key_case((array) $row, CASE_LOWER);
139
140 $key_name = $row['key_name'];
141
142 if ($constraint_name === $key_name) {
143 if (!$row['non_unique']) {
144 throw new ilDatabaseException('it was not specified an existing table index');
145 }
146 $column_name = $row['column_name'];
147 $definition['fields'][$column_name] = [
148 'position' => $colpos++,
149 ];
150 if (!empty($row['collation'])) {
151 $definition['fields'][$column_name]['sorting'] = ($row['collation'] === 'A' ? 'ascending' : 'descending');
152 }
153 }
154 }
155
156 if (empty($definition['fields'])) {
157 throw new ilDatabaseException('it was not specified an existing table index (index does not exist)');
158 }
159
160 return $definition;
161 }
162
167 public function getTableConstraintDefinition(string $table, string $index_name): array
168 {
169 $index_name = strtolower($index_name);
170 $table = $this->db_instance->quoteIdentifier($table, true);
171 $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
172
173 if (strtolower($index_name) !== 'primary') {
174 $constraint_name_pdo = $this->db_instance->getIndexName($index_name);
175 $result = $this->db_instance->query(sprintf($query, $this->db_instance->quote($constraint_name_pdo)));
176 $data = $this->db_instance->fetchAssoc($result);
177 if ($data) {
178 // apply 'idxname_format' only if the query succeeded, otherwise
179 // fallback to the given $index_name, without transformation
180 $index_name = strtolower($constraint_name_pdo);
181 }
182 }
183
184 $result = $this->db_instance->query(sprintf($query, $this->db_instance->quote($index_name)));
185
186 $colpos = 1;
187 $definition = [];
188 while (is_object($row = $result->fetchObject())) {
189 $row = (array) $row;
190 $row = array_change_key_case($row, CASE_LOWER);
191 $key_name = $row['key_name'];
192 if ($this->db_instance->options['portability'] ?? null) {
193 $key_name = strtolower((string) $key_name);
194 }
195 $key_name = strtolower((string) $key_name); // FSX fix
196 if ($index_name === $key_name) {
197 if ($row['non_unique']) {
198 throw new ilDatabaseException(' is not an existing table constraint');
199 }
200 if (strtolower((string) $row['key_name']) === 'primary') {
201 $definition['primary'] = true;
202 } else {
203 $definition['unique'] = true;
204 }
205 $column_name = $row['column_name'];
206 if ($this->db_instance->options['portability'] ?? null) {
207 if ($this->db_instance->options['field_case'] == CASE_LOWER) {
208 $column_name = strtolower((string) $column_name);
209 } else {
210 $column_name = strtoupper((string) $column_name);
211 }
212 }
213 $definition['fields'][$column_name] = [
214 'position' => $colpos++,
215 ];
216 if (!empty($row['collation'])) {
217 $definition['fields'][$column_name]['sorting'] = ($row['collation'] === 'A' ? 'ascending' : 'descending');
218 }
219 }
220 }
221
222 if (empty($definition['fields'])) {
223 throw new ilDatabaseException(' is not an existing table constraint');
224 }
225
226 return $definition;
227 }
228
232 public function getTriggerDefinition(string $trigger): array
233 {
234 throw new ilDatabaseException('not yet implemented ' . __METHOD__);
235 }
236}
Class ilDBPdoReverse.
getTableIndexDefinition(string $table, string $constraint_name)
ilMySQLQueryUtils $query_utils
getTableConstraintDefinition(string $table, string $index_name)
getTriggerDefinition(string $trigger)
__construct(protected \PDO $pdo, protected Internal $db_instance)
getTableFieldDefinition(string $table_name, string $field_name)
Class ilDatabaseException.
Class ilMySQLQueryUtils.
Interface ilDBReverse.
Definition: ilDBReverse.php:27
$res
Definition: ltiservices.php:69