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