19 declare(strict_types=1);
38 if ($this->query_utils ===
null) {
50 $table = $this->db_instance->quoteIdentifier($table_name);
51 $query =
"SHOW COLUMNS FROM $table LIKE " . $this->db_instance->quote($field_name);
52 $res = $this->pdo->query($query);
54 while (
$data =
$res->fetch(PDO::FETCH_ASSOC)) {
58 $ilDBPdoFieldDefinition = $this->db_instance->getFieldDefinition();
60 foreach ($columns as $column) {
61 $column = array_change_key_case($column, CASE_LOWER);
62 $column[
'name'] = $column[
'field'];
63 unset($column[
'field']);
64 $column = array_change_key_case($column, CASE_LOWER);
65 if ($field_name === $column[
'name'] && $ilDBPdoFieldDefinition !==
null) {
66 [$types, $length, $unsigned, $fixed] = $ilDBPdoFieldDefinition->mapNativeDatatype($column);
68 if (empty($column[
'null']) || $column[
'null'] !==
'YES') {
72 if (array_key_exists(
'default', $column)) {
73 $default = $column[
'default'];
74 if ($notnull && is_null($default)) {
78 $autoincrement =
false;
79 if (!empty($column[
'extra']) && $column[
'extra'] ===
'auto_increment') {
80 $autoincrement =
true;
84 'notnull' => $notnull,
85 'nativetype' => preg_replace(
'/^([a-z]+)[^a-z].*/i',
'\\1', (
string) $column[
'type']),
87 if (!is_null($length)) {
88 $definition[0][
'length'] = $length;
90 if (!is_null($unsigned)) {
91 $definition[0][
'unsigned'] = $unsigned;
93 if (!is_null($fixed)) {
94 $definition[0][
'fixed'] = $fixed;
96 if ($default !==
false) {
97 $definition[0][
'default'] = $default;
100 $definition[0][
'autoincrement'] = $autoincrement;
102 foreach ($types as $key => $type) {
103 $definition[$key] = $definition[0];
104 if ($type ===
'clob' || $type ===
'blob') {
105 unset($definition[$key][
'default']);
107 $definition[$key][
'type'] = $type;
108 $definition[$key][
'mdb2type'] = $type;
124 $table = $this->db_instance->quoteIdentifier($table,
true);
125 $query =
"SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
126 $index_name_pdo = $this->db_instance->getIndexName($constraint_name);
127 $result = $this->db_instance->query(sprintf($query, $this->db_instance->quote($index_name_pdo)));
128 $data = $this->db_instance->fetchAssoc($result);
131 $constraint_name = $index_name_pdo;
134 $result = $this->db_instance->query(sprintf($query, $this->db_instance->quote($constraint_name)));
138 while (is_object($row = $result->fetchObject())) {
139 $row = array_change_key_case((array) $row, CASE_LOWER);
141 $key_name = $row[
'key_name'];
143 if ($constraint_name === $key_name) {
144 if (!$row[
'non_unique']) {
147 $column_name = $row[
'column_name'];
148 $definition[
'fields'][$column_name] = [
149 'position' => $colpos++,
151 if (!empty($row[
'collation'])) {
152 $definition[
'fields'][$column_name][
'sorting'] = ($row[
'collation'] ===
'A' ?
'ascending' :
'descending');
157 if (empty($definition[
'fields'])) {
158 throw new ilDatabaseException(
'it was not specified an existing table index (index does not exist)');
170 $index_name = strtolower($index_name);
171 $table = $this->db_instance->quoteIdentifier($table,
true);
172 $query =
"SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
174 if (strtolower($index_name) !==
'primary') {
175 $constraint_name_pdo = $this->db_instance->getIndexName($index_name);
176 $result = $this->db_instance->query(sprintf($query, $this->db_instance->quote($constraint_name_pdo)));
177 $data = $this->db_instance->fetchAssoc($result);
181 $index_name = strtolower($constraint_name_pdo);
185 $result = $this->db_instance->query(sprintf($query, $this->db_instance->quote($index_name)));
189 while (is_object($row = $result->fetchObject())) {
191 $row = array_change_key_case($row, CASE_LOWER);
192 $key_name = $row[
'key_name'];
193 if ($this->db_instance->options[
'portability'] ??
null) {
194 $key_name = strtolower((
string) $key_name);
196 $key_name = strtolower((
string) $key_name);
197 if ($index_name === $key_name) {
198 if ($row[
'non_unique']) {
201 if (strtolower((
string) $row[
'key_name']) ===
'primary') {
202 $definition[
'primary'] =
true;
204 $definition[
'unique'] =
true;
206 $column_name = $row[
'column_name'];
207 if ($this->db_instance->options[
'portability'] ??
null) {
208 if ($this->db_instance->options[
'field_case'] == CASE_LOWER) {
209 $column_name = strtolower((
string) $column_name);
211 $column_name = strtoupper((
string) $column_name);
214 $definition[
'fields'][$column_name] = [
215 'position' => $colpos++,
217 if (!empty($row[
'collation'])) {
218 $definition[
'fields'][$column_name][
'sorting'] = ($row[
'collation'] ===
'A' ?
'ascending' :
'descending');
223 if (empty($definition[
'fields'])) {
ilMySQLQueryUtils $query_utils
getTableConstraintDefinition(string $table, string $index_name)
getTriggerDefinition(string $trigger)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getTableFieldDefinition(string $table_name, string $field_name)
__construct(protected \PDO $pdo, protected \ilDBPdo $db_instance)
ilDBPdoReverse constructor.
getTableIndexDefinition(string $table, string $constraint_name)