19declare(strict_types=1);
37 if ($this->query_utils ===
null) {
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);
53 while (
$data =
$res->fetch(PDO::FETCH_ASSOC)) {
57 $ilDBPdoFieldDefinition = $this->db_instance->getFieldDefinition();
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);
67 if (empty($column[
'null']) || $column[
'null'] !==
'YES') {
71 if (array_key_exists(
'default', $column)) {
72 $default = $column[
'default'];
73 if ($notnull && is_null($default)) {
77 $autoincrement =
false;
78 if (!empty($column[
'extra']) && $column[
'extra'] ===
'auto_increment') {
79 $autoincrement =
true;
83 'notnull' => $notnull,
84 'nativetype' => preg_replace(
'/^([a-z]+)[^a-z].*/i',
'\\1', (
string) $column[
'type']),
86 if (!is_null($length)) {
87 $definition[0][
'length'] = $length;
89 if (!is_null($unsigned)) {
90 $definition[0][
'unsigned'] = $unsigned;
92 if (!is_null($fixed)) {
93 $definition[0][
'fixed'] = $fixed;
95 if ($default !==
false) {
96 $definition[0][
'default'] = $default;
99 $definition[0][
'autoincrement'] = $autoincrement;
101 foreach ($types as $key => $type) {
102 $definition[$key] = $definition[0];
103 if ($type ===
'clob' || $type ===
'blob') {
104 unset($definition[$key][
'default']);
106 $definition[$key][
'type'] = $type;
107 $definition[$key][
'mdb2type'] = $type;
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);
130 $constraint_name = $index_name_pdo;
133 $result = $this->db_instance->query(sprintf($query, $this->db_instance->quote($constraint_name)));
137 while (is_object($row = $result->fetchObject())) {
138 $row = array_change_key_case((array) $row, CASE_LOWER);
140 $key_name = $row[
'key_name'];
142 if ($constraint_name === $key_name) {
143 if (!$row[
'non_unique']) {
146 $column_name = $row[
'column_name'];
147 $definition[
'fields'][$column_name] = [
148 'position' => $colpos++,
150 if (!empty($row[
'collation'])) {
151 $definition[
'fields'][$column_name][
'sorting'] = ($row[
'collation'] ===
'A' ?
'ascending' :
'descending');
156 if (empty($definition[
'fields'])) {
157 throw new ilDatabaseException(
'it was not specified an existing table index (index does not exist)');
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 */";
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);
180 $index_name = strtolower($constraint_name_pdo);
184 $result = $this->db_instance->query(sprintf($query, $this->db_instance->quote($index_name)));
188 while (is_object($row = $result->fetchObject())) {
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);
195 $key_name = strtolower((
string) $key_name);
196 if ($index_name === $key_name) {
197 if ($row[
'non_unique']) {
200 if (strtolower((
string) $row[
'key_name']) ===
'primary') {
201 $definition[
'primary'] =
true;
203 $definition[
'unique'] =
true;
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);
210 $column_name = strtoupper((
string) $column_name);
213 $definition[
'fields'][$column_name] = [
214 'position' => $colpos++,
216 if (!empty($row[
'collation'])) {
217 $definition[
'fields'][$column_name][
'sorting'] = ($row[
'collation'] ===
'A' ?
'ascending' :
'descending');
222 if (empty($definition[
'fields'])) {
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.