/** * Get the SQL syntax for a key. * * @param array $columns An array of SimpleXMLElement objects comprising the key. * * @return string * * @since 11.1 */ protected function getKeySQL($columns) { // TODO Error checking on array and element types. $kNonUnique = (string) $columns[0]['Non_unique']; $kName = (string) $columns[0]['Key_name']; $kColumn = (string) $columns[0]['Column_name']; $kCollation = (string) $columns[0]['Collation']; $kNull = (string) $columns[0]['Null']; $kType = (string) $columns[0]['Index_type']; $kComment = (string) $columns[0]['Comment']; $prefix = ''; if ($kName == 'PRIMARY') { $prefix = 'PRIMARY '; } else { if ($kNonUnique == 0) { $prefix = 'UNIQUE '; } } $nColumns = count($columns); $kColumns = array(); if ($nColumns == 1) { $kColumns[] = $this->db->quoteName($kColumn); } else { foreach ($columns as $column) { $kColumns[] = (string) $column['Column_name']; } } $sql = $prefix . 'KEY ' . ($kName != 'PRIMARY' ? $this->db->quoteName($kName) : '') . ' (' . implode(',', $kColumns) . ')'; return $sql; }
/** * Get the details list of keys for a table. * * @param string $table The name of the table. * * @return array An arry of the column specification for the table. * * @since 11.1 * * @throws Exception * @todo Move into database connector class. */ protected function getKeys($table) { if (empty($this->cache['keys'][$table])) { // Get the details columns information. $this->db->setQuery('SHOW KEYS FROM ' . $this->db->quoteName($table)); $this->cache['keys'][$table] = $this->db->loadObjectList(); // Check for a db error. if ($this->db->getErrorNum()) { throw new Exception($this->db->getErrorMsg()); } } return $this->cache['keys'][$table]; }
/** * Builds the XML structure to export. * * @return array An array of XML lines (strings). * * @since 11.1 * @throws Exception if an error occurs. */ protected function buildXmlStructure() { $buffer = array(); $query = $this->db->getQuery(true); foreach ($this->from as $table) { // Replace the magic prefix if found. $table = $this->getGenericTableName($table); /* * Table structure */ // Get the details columns information. $fields = $this->db->getTableColumns($table, false); $keys = $this->db->getTableKeys($table); $buffer[] = ' <table_structure name="' . $table . '">'; foreach ($fields as $field) { $buffer[] = ' <field' . ' Field="' . $field->Field . '"' . ' Type="' . $field->Type . '"' . ' Null="' . $field->Null . '"' . ' Key="' . $field->Key . '"' . (isset($field->Default) ? ' Default="' . $field->Default . '"' : '') . ' Extra="' . $field->Extra . '"' . ' Comment="' . htmlspecialchars($field->Comment) . '"' . ' />'; } foreach ($keys as $key) { $buffer[] = ' <key' . ' Table="' . $table . '"' . ' Non_unique="' . $key->Non_unique . '"' . ' Key_name="' . $key->Key_name . '"' . ' Seq_in_index="' . $key->Seq_in_index . '"' . ' Column_name="' . $key->Column_name . '"' . ' Collation="' . $key->Collation . '"' . ' Null="' . $key->Null . '"' . ' Index_type="' . $key->Index_type . '"' . ' Comment="' . htmlspecialchars($key->Comment) . '"' . ' />'; } $buffer[] = ' </table_structure>'; /* * Table data */ if (!$this->options->get('with-data')) { continue; } $query->clear()->from($this->db->quoteName($table))->select('*'); $rows = $this->db->setQuery($query)->loadObjectList(); $buffer[] = ' <table_data name="' . $table . '">'; foreach ($rows as $row) { $buffer[] = ' <row>'; foreach ($row as $fieldName => $fieldValue) { $buffer[] = ' <field' . ' name="' . $fieldName . '">' . htmlspecialchars($fieldValue) . '</field>'; } $buffer[] = ' </row>'; } $buffer[] = ' </table_data>'; } return $buffer; }