function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy) { global $cfgRelation; /** * Get the unique keys in the table */ $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db); $keys_result = PMA_DBI_query($keys_query); $unique_keys = array(); while ($key = PMA_DBI_fetch_assoc($keys_result)) { if ($key['Non_unique'] == 0) { $unique_keys[] = $key['Column_name']; } } PMA_DBI_free_result($keys_result); /** * Gets fields properties */ PMA_DBI_select_db($db); $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table); $result = PMA_DBI_query($local_query); $fields_cnt = PMA_DBI_num_rows($result); // Check if we can use Relations (Mike Beck) if ($do_relation && !empty($cfgRelation['relation'])) { // Find which tables are related with the current one and write it in // an array $res_rel = PMA_getForeigners($db, $table); if ($res_rel && count($res_rel) > 0) { $have_rel = TRUE; } else { $have_rel = FALSE; } } else { $have_rel = FALSE; } // end if /** * Displays the table structure */ $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strStructure'] . ': ' . $table . $crlf . '%' . $crlf . ' \\begin{longtable}{'; if (!PMA_exportOutputHandler($buffer)) { return FALSE; } $columns_cnt = 4; $alignment = '|l|c|c|c|'; if ($do_relation && $have_rel) { $columns_cnt++; $alignment .= 'l|'; } if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) { $columns_cnt++; $alignment .= 'l|'; } if ($do_mime && $cfgRelation['mimework']) { $columns_cnt++; $alignment .= 'l|'; } $buffer = $alignment . '} ' . $crlf; $header = ' \\hline '; $header .= '\\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strField'] . '}} & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strType'] . '}} & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strNull'] . '}} & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strDefault'] . '}}'; if ($do_relation && $have_rel) { $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strLinksTo'] . '}}'; } if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) { $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strComments'] . '}}'; $comments = PMA_getComments($db, $table); } if ($do_mime && $cfgRelation['mimework']) { $header .= ' & \\multicolumn{1}{|c|}{\\textbf{MIME}}'; $mime_map = PMA_getMIME($db, $table, true); } $local_buffer = PMA_texEscape($table); // Table caption for first page and label if (isset($GLOBALS['latex_caption'])) { $buffer .= ' \\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_caption']) . '} \\label{' . str_replace('__TABLE__', $table, $GLOBALS['latex_structure_label']) . '} \\\\' . $crlf; } $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf . '\\endfirsthead' . $crlf; // Table caption on next pages if (isset($GLOBALS['latex_caption'])) { $buffer .= ' \\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_continued_caption']) . '} \\\\ ' . $crlf; } $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ' . $crlf; if (!PMA_exportOutputHandler($buffer)) { return FALSE; } while ($row = PMA_DBI_fetch_assoc($result)) { $type = $row['Type']; // reformat mysql query output - staybyte - 9. June 2001 // loic1: set or enum types: slashes single quotes inside options if (eregi('^(set|enum)\\((.+)\\)$', $type, $tmp)) { $tmp[2] = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1); $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')'; $type_nowrap = ''; $binary = 0; $unsigned = 0; $zerofill = 0; } else { $type_nowrap = ' nowrap="nowrap"'; $type = eregi_replace('BINARY', '', $type); $type = eregi_replace('ZEROFILL', '', $type); $type = eregi_replace('UNSIGNED', '', $type); if (empty($type)) { $type = ' '; } $binary = eregi('BINARY', $row['Type']); $unsigned = eregi('UNSIGNED', $row['Type']); $zerofill = eregi('ZEROFILL', $row['Type']); } if (!isset($row['Default'])) { if ($row['Null'] != '') { $row['Default'] = 'NULL'; } } else { $row['Default'] = $row['Default']; } $field_name = $row['Field']; $local_buffer = $field_name . "" . $type . "" . ($row['Null'] == '' ? $GLOBALS['strNo'] : $GLOBALS['strYes']) . "" . (isset($row['Default']) ? $row['Default'] : ''); if ($do_relation && $have_rel) { $local_buffer .= ""; if (isset($res_rel[$field_name])) { $local_buffer .= $res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')'; } } if ($do_comments && $cfgRelation['commwork']) { $local_buffer .= ""; if (isset($comments[$field_name])) { $local_buffer .= $comments[$field_name]; } } if ($do_mime && $cfgRelation['mimework']) { $local_buffer .= ""; if (isset($mime_map[$field_name])) { $local_buffer .= str_replace('_', '/', $mime_map[$field_name]['mimetype']); } } $local_buffer = PMA_texEscape($local_buffer); if ($row['Key'] == 'PRI') { $pos = strpos($local_buffer, ""); $local_buffer = '\\textit{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos); } if (in_array($field_name, $unique_keys)) { $pos = strpos($local_buffer, ""); $local_buffer = '\\textbf{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos); } $buffer = str_replace("", ' & ', $local_buffer); $buffer .= ' \\\\ \\hline ' . $crlf; if (!PMA_exportOutputHandler($buffer)) { return FALSE; } } // end while PMA_DBI_free_result($result); $buffer = ' \\end{longtable}' . $crlf; return PMA_exportOutputHandler($buffer); }
/** * Outputs table's structure * * @param string $db database name * @param string $table table name * @param string $crlf the end of line sequence * @param string $error_url the url to go back in case of error * @param bool $do_relation whether to include relation comments * @param bool $do_comments whether to include the pmadb-style column comments * as comments in the structure; this is deprecated * but the parameter is left here because export.php * calls PMA_exportStructure() also for other export * types which use this parameter * @param bool $do_mime whether to include mime comments * @param bool $dates whether to include creation/update/check dates * @param string $export_mode 'create_table', 'triggers', 'create_view', 'stand_in' * @param string $export_type 'server', 'database', 'table' * * @return bool Whether it succeeded * * @access public */ function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $export_mode, $export_type) { global $cfgRelation; /* We do not export triggers */ if ($export_mode == 'triggers') { return true; } /** * Get the unique keys in the table */ $unique_keys = array(); $keys = PMA_DBI_get_table_indexes($db, $table); foreach ($keys as $key) { if ($key['Non_unique'] == 0) { $unique_keys[] = $key['Column_name']; } } /** * Gets fields properties */ PMA_DBI_select_db($db); // Check if we can use Relations if ($do_relation && !empty($cfgRelation['relation'])) { // Find which tables are related with the current one and write it in // an array $res_rel = PMA_getForeigners($db, $table); if ($res_rel && count($res_rel) > 0) { $have_rel = true; } else { $have_rel = false; } } else { $have_rel = false; } // end if /** * Displays the table structure */ $buffer = $crlf . '%' . $crlf . '% ' . __('Structure') . ': ' . $table . $crlf . '%' . $crlf . ' \\begin{longtable}{'; if (!PMA_exportOutputHandler($buffer)) { return false; } $columns_cnt = 4; $alignment = '|l|c|c|c|'; if ($do_relation && $have_rel) { $columns_cnt++; $alignment .= 'l|'; } if ($do_comments) { $columns_cnt++; $alignment .= 'l|'; } if ($do_mime && $cfgRelation['mimework']) { $columns_cnt++; $alignment .= 'l|'; } $buffer = $alignment . '} ' . $crlf; $header = ' \\hline '; $header .= '\\multicolumn{1}{|c|}{\\textbf{' . __('Column') . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Type') . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Null') . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Default') . '}}'; if ($do_relation && $have_rel) { $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . __('Links to') . '}}'; } if ($do_comments) { $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . __('Comments') . '}}'; $comments = PMA_getComments($db, $table); } if ($do_mime && $cfgRelation['mimework']) { $header .= ' & \\multicolumn{1}{|c|}{\\textbf{MIME}}'; $mime_map = PMA_getMIME($db, $table, true); } // Table caption for first page and label if (isset($GLOBALS['latex_caption'])) { $buffer .= ' \\caption{' . PMA_expandUserString($GLOBALS['latex_structure_caption'], 'PMA_texEscape', array('table' => $table, 'database' => $db)) . '} \\label{' . PMA_expandUserString($GLOBALS['latex_structure_label'], null, array('table' => $table, 'database' => $db)) . '} \\\\' . $crlf; } $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf . '\\endfirsthead' . $crlf; // Table caption on next pages if (isset($GLOBALS['latex_caption'])) { $buffer .= ' \\caption{' . PMA_expandUserString($GLOBALS['latex_structure_continued_caption'], 'PMA_texEscape', array('table' => $table, 'database' => $db)) . '} \\\\ ' . $crlf; } $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ' . $crlf; if (!PMA_exportOutputHandler($buffer)) { return false; } $fields = PMA_DBI_get_columns($db, $table); foreach ($fields as $row) { $extracted_fieldspec = PMA_extractFieldSpec($row['Type']); $type = $extracted_fieldspec['print_type']; if (empty($type)) { $type = ' '; } if (!isset($row['Default'])) { if ($row['Null'] != 'NO') { $row['Default'] = 'NULL'; } } $field_name = $row['Field']; $local_buffer = $field_name . "" . $type . "" . ($row['Null'] == '' || $row['Null'] == 'NO' ? __('No') : __('Yes')) . "" . (isset($row['Default']) ? $row['Default'] : ''); if ($do_relation && $have_rel) { $local_buffer .= ""; if (isset($res_rel[$field_name])) { $local_buffer .= $res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')'; } } if ($do_comments && $cfgRelation['commwork']) { $local_buffer .= ""; if (isset($comments[$field_name])) { $local_buffer .= $comments[$field_name]; } } if ($do_mime && $cfgRelation['mimework']) { $local_buffer .= ""; if (isset($mime_map[$field_name])) { $local_buffer .= str_replace('_', '/', $mime_map[$field_name]['mimetype']); } } $local_buffer = PMA_texEscape($local_buffer); if ($row['Key'] == 'PRI') { $pos = strpos($local_buffer, ""); $local_buffer = '\\textit{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos); } if (in_array($field_name, $unique_keys)) { $pos = strpos($local_buffer, ""); $local_buffer = '\\textbf{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos); } $buffer = str_replace("", ' & ', $local_buffer); $buffer .= ' \\\\ \\hline ' . $crlf; if (!PMA_exportOutputHandler($buffer)) { return false; } } // end while $buffer = ' \\end{longtable}' . $crlf; return PMA_exportOutputHandler($buffer); }