ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilDBPdoReverse.php
Go to the documentation of this file.
1<?php
2require_once('./Services/Database/interfaces/interface.ilDBReverse.php');
3
9class ilDBPdoReverse implements ilDBReverse {
10
14 protected $pdo;
18 protected $db_instance;
19
20
27 public function __construct(\PDO $pdo, ilDBPdo $db_instance) {
28 $this->pdo = $pdo;
29 $this->db_instance = $db_instance;
30 }
31
35 protected $query_utils;
36
37
41 public function getQueryUtils() {
42 if (!$this->query_utils) {
43 $this->query_utils = new ilMySQLQueryUtils($this->db_instance);
44 }
45
46 return $this->query_utils;
47 }
48
54 public function getTableFieldDefinition($table_name, $field_name) {
55
56 $return = array();
57
58 $table = $this->db_instance->quoteIdentifier($table_name);
59 $query = "SHOW COLUMNS FROM $table LIKE " . $this->db_instance->quote($field_name);
60 $res = $this->pdo->query($query);
61 $columns = array();
62 while ($data = $res->fetch(PDO::FETCH_ASSOC)) {
63 $columns[] = $data;
64 }
65
66 $ilDBPdoFieldDefinition = $this->db_instance->getFieldDefinition();
67
68 foreach ($columns as $column) {
69 $column = array_change_key_case($column, CASE_LOWER);
70 $column['name'] = $column['field'];
71 unset($column['field']);
72 // if ($this->db_instance->options['portability']) {
73 // if ($this->db_instance->options['field_case'] == CASE_LOWER) {
74 // $column['name'] = strtolower($column['name']);
75 // } else {
76 // $column['name'] = strtoupper($column['name']);
77 // }
78 // } else {
79 $column = array_change_key_case($column, CASE_LOWER);
80 // }
81 if ($field_name == $column['name']) {
82 $mapped_datatype = $ilDBPdoFieldDefinition->mapNativeDatatype($column);
83
84 list($types, $length, $unsigned, $fixed) = $mapped_datatype;
85 $notnull = false;
86 if (empty($column['null']) || $column['null'] !== 'YES') {
87 $notnull = true;
88 }
89 $default = false;
90 if (array_key_exists('default', $column)) {
91 $default = $column['default'];
92 if (is_null($default) && $notnull) {
93 $default = '';
94 }
95 }
96 $autoincrement = false;
97 if (!empty($column['extra']) && $column['extra'] == 'auto_increment') {
98 $autoincrement = true;
99 }
100
101 $definition[0] = array(
102 'notnull' => $notnull,
103 'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type']),
104 );
105 if (!is_null($length)) {
106 $definition[0]['length'] = $length;
107 }
108 if (!is_null($unsigned)) {
109 $definition[0]['unsigned'] = $unsigned;
110 }
111 if (!is_null($fixed)) {
112 $definition[0]['fixed'] = $fixed;
113 }
114 if ($default !== false) {
115 $definition[0]['default'] = $default;
116 }
117 if ($autoincrement !== false) {
118 $definition[0]['autoincrement'] = $autoincrement;
119 }
120 foreach ($types as $key => $type) {
121 $definition[$key] = $definition[0];
122 if ($type == 'clob' || $type == 'blob') {
123 unset($definition[$key]['default']);
124 }
125 $definition[$key]['type'] = $type;
126 $definition[$key]['mdb2type'] = $type;
127 }
128
129 return $definition;
130 }
131 }
132
133 throw new ilDatabaseException('it was not specified an existing table column');
134 }
135
136
143 public function getTableIndexDefinition($table, $index_name) {
144 $table = $this->db_instance->quoteIdentifier($table, true);
145 $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
146 $index_name_pdo = $this->db_instance->getIndexName($index_name);
147 $result = $this->db_instance->query(sprintf($query, $this->db_instance->quote($index_name_pdo)));
148 $data = $this->db_instance->fetchAssoc($result);
149
150 if ($data) {
151 $index_name = $index_name_pdo;
152 }
153
154 $result = $this->db_instance->query(sprintf($query, $this->db_instance->quote($index_name)));
155
156 $colpos = 1;
157 $definition = array();
158 while (is_object($row = $result->fetchObject())) {
159 $row = array_change_key_case((array)$row, CASE_LOWER);
160
161 $key_name = $row['key_name'];
162
163
164 if ($index_name == $key_name) {
165 if (!$row['non_unique']) {
166 throw new ilDatabaseException('it was not specified an existing table index');
167 }
168 $column_name = $row['column_name'];
169 $definition['fields'][$column_name] = array(
170 'position' => $colpos ++,
171 );
172 if (!empty($row['collation'])) {
173 $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A' ? 'ascending' : 'descending');
174 }
175 }
176 }
177
178 if (empty($definition['fields'])) {
179 throw new ilDatabaseException('it was not specified an existing table index (index does not exist)');
180 }
181
182 return $definition;
183 }
184
185
192 public function getTableConstraintDefinition($table, $constraint_name) {
193 $constraint_name = strtolower($constraint_name);
194 $table = $this->db_instance->quoteIdentifier($table, true);
195 $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
196
197 if (strtolower($constraint_name) != 'primary') {
198 $constraint_name_pdo = $this->db_instance->getIndexName($constraint_name);
199 $result = $this->db_instance->query(sprintf($query, $this->db_instance->quote($constraint_name_pdo)));
200 $data = $this->db_instance->fetchAssoc($result);
201 if ($data) {
202 // apply 'idxname_format' only if the query succeeded, otherwise
203 // fallback to the given $index_name, without transformation
204 $constraint_name = strtolower($constraint_name_pdo);
205 }
206 }
207
208 $result = $this->db_instance->query(sprintf($query, $this->db_instance->quote($constraint_name)));
209
210 $colpos = 1;
211 $definition = array();
212 while (is_object($row = $result->fetchObject())) {
213 $row = (array)$row;
214 $row = array_change_key_case($row, CASE_LOWER);
215 $key_name = $row['key_name'];
216 if ($this->db_instance->options['portability']) {
217 if ($this->db_instance->options['field_case'] == CASE_LOWER) {
218 $key_name = strtolower($key_name);
219 } else {
220 $key_name = strtolower($key_name);
221 }
222 }
223 $key_name = strtolower($key_name); // FSX fix
224 if ($constraint_name == $key_name) {
225 if ($row['non_unique']) {
226 throw new ilDatabaseException(' is not an existing table constraint');
227 }
228 if ($row['key_name'] == 'PRIMARY') {
229 $definition['primary'] = true;
230 } else {
231 $definition['unique'] = true;
232 }
233 $column_name = $row['column_name'];
234 if ($this->db_instance->options['portability']) {
235 if ($this->db_instance->options['field_case'] == CASE_LOWER) {
236 $column_name = strtolower($column_name);
237 } else {
238 $column_name = strtoupper($column_name);
239 }
240 }
241 $definition['fields'][$column_name] = array(
242 'position' => $colpos ++,
243 );
244 if (!empty($row['collation'])) {
245 $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A' ? 'ascending' : 'descending');
246 }
247 }
248 }
249
250 if (empty($definition['fields'])) {
251 throw new ilDatabaseException(' is not an existing table constraint');
252 }
253
254 return $definition;
255 }
256
257
263 public function getTriggerDefinition($trigger) {
264 throw new ilDatabaseException('not yet implemented ' . __METHOD__);
265 }
266
267
274 public function tableInfo($result, $mode = null) {
275 throw new ilDatabaseException('not yet implemented ' . __METHOD__);
276 }
277}
sprintf('%.4f', $callTime)
$column
Definition: 39dropdown.php:62
$result
if(! $in) $columns
Definition: Utf8Test.php:45
An exception for terminatinating execution or to throw for unit testing.
Class ilDBPdoReverse.
tableInfo($result, $mode=null)
getTableIndexDefinition($table, $index_name)
getTriggerDefinition($trigger)
getTableConstraintDefinition($table, $constraint_name)
getTableFieldDefinition($table_name, $field_name)
__construct(\PDO $pdo, ilDBPdo $db_instance)
ilDBPdoReverse constructor.
Class pdoDB.
Class ilDatabaseException.
Class ilMySQLQueryUtils.
Interface ilDBReverse.