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