10 defined(
'JPATH_PLATFORM') or die;
29 public function check()
34 throw new Exception(
'JPLATFORM_ERROR_DATABASE_CONNECTOR_WRONG_TYPE');
38 if (empty($this->from))
40 throw new Exception(
'JPLATFORM_ERROR_NO_TABLES_SPECIFIED');
56 protected function getAddColumnSQL($table, SimpleXMLElement $field)
58 return 'ALTER TABLE ' . $this->db->quoteName($table) .
' ADD COLUMN ' . $this->getColumnSQL($field);
71 protected function getAddKeySQL($table, $keys)
73 return 'ALTER TABLE ' . $this->db->quoteName($table) .
' ADD ' . $this->getKeySQL($keys);
85 protected function getAlterTableSQL(SimpleXMLElement $structure)
87 $table = $this->getRealTableName($structure[
'name']);
88 $oldFields = $this->db->getTableColumns($table);
89 $oldKeys = $this->db->getTableKeys($table);
93 $newFields = $structure->xpath(
'field');
94 $newKeys = $structure->xpath(
'key');
97 foreach ($newFields as $field)
99 $fName = (string) $field[
'Field'];
101 if (isset($oldFields[$fName]))
104 $column = $oldFields[$fName];
107 $change = ((string) $field[
'Type'] != $column->Type) || ((string) $field[
'Null'] != $column->Null)
108 || ((string) $field[
'Default'] != $column->Default) || ((string) $field[
'Extra'] != $column->Extra);
112 $alters[] = $this->getChangeColumnSQL($table, $field);
116 unset($oldFields[$fName]);
121 $alters[] = $this->getAddColumnSQL($table, $field);
126 foreach ($oldFields as $name => $column)
129 $alters[] = $this->getDropColumnSQL($table, $name);
133 $oldLookup = $this->getKeyLookup($oldKeys);
134 $newLookup = $this->getKeyLookup($newKeys);
137 foreach ($newLookup as $name => $keys)
140 if (isset($oldLookup[$name]))
143 $newCount = count($newLookup[$name]);
144 $oldCount = count($oldLookup[$name]);
147 if ($newCount == $oldCount)
150 for ($i = 0; $i < $newCount; $i++)
152 $same = (((string) $newLookup[$name][$i][
'Non_unique'] == $oldLookup[$name][$i]->Non_unique)
153 && ((string) $newLookup[$name][$i][
'Column_name'] == $oldLookup[$name][$i]->Column_name)
154 && ((string) $newLookup[$name][$i][
'Seq_in_index'] == $oldLookup[$name][$i]->Seq_in_index)
155 && ((string) $newLookup[$name][$i][
'Collation'] == $oldLookup[$name][$i]->Collation)
156 && ((string) $newLookup[$name][$i][
'Index_type'] == $oldLookup[$name][$i]->Index_type));
195 $alters[] = $this->getDropKeySQL($table, $name);
196 $alters[] = $this->getAddKeySQL($table, $keys);
200 unset($oldLookup[$name]);
205 $alters[] = $this->getAddKeySQL($table, $keys);
210 foreach ($oldLookup as $name => $keys)
212 if (strtoupper($name) ==
'PRIMARY')
214 $alters[] = $this->getDropPrimaryKeySQL($table);
218 $alters[] = $this->getDropKeySQL($table, $name);
235 protected function getChangeColumnSQL($table, SimpleXMLElement $field)
237 return 'ALTER TABLE ' . $this->db->quoteName($table) .
' CHANGE COLUMN ' . $this->db->quoteName((
string) $field[
'Field']) .
' '
238 . $this->getColumnSQL($field);
250 protected function getColumnSQL(SimpleXMLElement $field)
253 $blobs = array(
'text',
'smalltext',
'mediumtext',
'largetext');
255 $fName = (string) $field[
'Field'];
256 $fType = (string) $field[
'Type'];
257 $fNull = (string) $field[
'Null'];
258 $fDefault = isset($field[
'Default']) ? (string) $field[
'Default'] : null;
259 $fExtra = (string) $field[
'Extra'];
261 $query = $this->db->quoteName($fName) .
' ' . $fType;
265 if (in_array($fType, $blobs) || $fDefault === null)
267 $query .=
' NOT NULL';
272 $query .=
' NOT NULL DEFAULT ' . $this->db->quote($fDefault);
277 if ($fDefault === null)
279 $query .=
' DEFAULT NULL';
284 $query .=
' DEFAULT ' . $this->db->quote($fDefault);
290 $query .=
' ' . strtoupper($fExtra);
306 protected function getDropKeySQL($table, $name)
308 return 'ALTER TABLE ' . $this->db->quoteName($table) .
' DROP KEY ' . $this->db->quoteName($name);
320 protected function getDropPrimaryKeySQL($table)
322 return 'ALTER TABLE ' . $this->db->quoteName($table) .
' DROP PRIMARY KEY';
335 protected function getKeyLookup($keys)
340 foreach ($keys as $key)
342 if ($key instanceof SimpleXMLElement)
344 $kName = (string) $key[
'Key_name'];
348 $kName = $key->Key_name;
351 if (empty($lookup[$kName]))
353 $lookup[$kName] = array();
356 $lookup[$kName][] = $key;
371 protected function getKeySQL($columns)
375 $kNonUnique = (string) $columns[0][
'Non_unique'];
376 $kName = (string) $columns[0][
'Key_name'];
377 $kColumn = (string) $columns[0][
'Column_name'];
381 if ($kName ==
'PRIMARY')
383 $prefix =
'PRIMARY ';
385 elseif ($kNonUnique == 0)
390 $nColumns = count($columns);
395 $kColumns[] = $this->db->quoteName($kColumn);
399 foreach ($columns as $column)
401 $kColumns[] = (string) $column[
'Column_name'];
405 $query = $prefix .
'KEY ' . ($kName !=
'PRIMARY' ? $this->db->quoteName($kName) :
'') .
' (' . implode(
',', $kColumns) .
')';