From 9bef31439e33b05c9d974cadbd64cada1cbc7ed6 Mon Sep 17 00:00:00 2001 From: Marc Delisle Date: Sat, 5 Mar 2005 23:21:25 +0000 Subject: [PATCH] native comments --- db_datadict.php | 6 +- libraries/database_interface.lib.php | 4 +- libraries/relation.lib.php | 117 +++++++++++++++++++++------ pdf_schema.php | 2 +- tbl_addfield.php | 6 +- tbl_alter.php | 31 +++---- tbl_create.php | 7 +- 7 files changed, 116 insertions(+), 57 deletions(-) diff --git a/db_datadict.php b/db_datadict.php index ab42e6dc0..1469fb4dc 100644 --- a/db_datadict.php +++ b/db_datadict.php @@ -59,7 +59,7 @@ $count = 0; while ($row = PMA_DBI_fetch_assoc($rowset)) { $myfieldname = 'Tables_in_' . htmlspecialchars($db); $table = $row[$myfieldname]; - if ($cfgRelation['commwork']) { + if ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100) { $comments = PMA_getComments($db, $table); } @@ -175,7 +175,7 @@ while ($row = PMA_DBI_fetch_assoc($rowset)) { if ($have_rel) { echo ' ' . $strLinksTo . '' . "\n"; } - if ($cfgRelation['commwork']) { + if ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100) { echo ' ' . $strComments . '' . "\n"; } if ($cfgRelation['mimework']) { @@ -258,7 +258,7 @@ while ($row = PMA_DBI_fetch_assoc($rowset)) { } echo ' ' . "\n"; } - if ($cfgRelation['commwork']) { + if ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100) { echo ' '; if (isset($comments[$field_name])) { echo htmlspecialchars($comments[$field_name]); diff --git a/libraries/database_interface.lib.php b/libraries/database_interface.lib.php index 97f234219..4d071c946 100644 --- a/libraries/database_interface.lib.php +++ b/libraries/database_interface.lib.php @@ -77,7 +77,9 @@ function PMA_DBI_get_fields($database, $table, $link = NULL) { return FALSE; } } - $result = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table), $link); + // here we use a try_query because when coming from + // tbl_create + tbl_properties.inc.php, the table does not exist + $result = PMA_DBI_try_query('SHOW FULL FIELDS FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table), $link); $fields = array(); while ($row = PMA_DBI_fetch_assoc($result)) { diff --git a/libraries/relation.lib.php b/libraries/relation.lib.php index 3adc64b3c..f560765e9 100644 --- a/libraries/relation.lib.php +++ b/libraries/relation.lib.php @@ -418,13 +418,27 @@ function PMA_getDisplayField($db, $table) { * * @access public * - * @author Mike Beck + * @authors Mike Beck + * and lem9 */ function PMA_getComments($db, $table = '') { global $cfgRelation, $charset_connection; if ($table != '') { - $com_qry = 'SELECT column_name, ' . PMA_backquote('comment') . ' FROM ' . PMA_backquote($cfgRelation['column_info']); + + // MySQL 4.1.x native column comments + if (PMA_MYSQL_INT_VERSION >= 40100) { + $fields = PMA_DBI_get_fields($db, $table); + foreach($fields as $key=>$field) { + $tmp_col = $field['Field']; + if (!empty($field['Comment'])) { + $native_comment[$tmp_col] = $field['Comment']; + } + } + } + + // pmadb internal column comments + $com_qry = 'SELECT column_name, comment FROM ' . PMA_backquote($cfgRelation['db']) . '.' .PMA_backquote($cfgRelation['column_info']); if (PMA_MYSQL_INT_VERSION >= 40100) { $com_qry .= ' WHERE CONVERT(db_name USING ' . $charset_connection . ') = \'' . PMA_sqlAddslashes($db) . '\'' . ' AND CONVERT(table_name USING ' . $charset_connection . ') = \'' . PMA_sqlAddslashes($table) . '\''; @@ -432,9 +446,10 @@ function PMA_getComments($db, $table = '') { $com_qry .= ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'' . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''; } - $com_rs = PMA_query_as_cu($com_qry, TRUE); + $com_rs = PMA_query_as_cu($com_qry, TRUE, PMA_DBI_QUERY_STORE); } else { - $com_qry = 'SELECT ' . PMA_backquote('comment') . ' FROM ' . PMA_backquote($cfgRelation['column_info']); + // pmadb internal db comments + $com_qry = 'SELECT ' . PMA_backquote('comment') . ' FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']); if (PMA_MYSQL_INT_VERSION >= 40100) { $com_qry .= ' WHERE CONVERT(db_name USING ' . $charset_connection . ') = \'' . PMA_sqlAddslashes($db) . '\'' . ' AND CONVERT(table_name USING ' . $charset_connection . ') = \'\'' @@ -444,21 +459,41 @@ function PMA_getComments($db, $table = '') { . ' AND table_name = \'\'' . ' AND column_name = \'(db_comment)\''; } - $com_rs = PMA_query_as_cu($com_qry, TRUE); + $com_rs = PMA_query_as_cu($com_qry, TRUE, PMA_DBI_QUERY_STORE); } - $i = 0; - while ($row = PMA_DBI_fetch_assoc($com_rs)) { - $i++; - $col = ($table != '' ? $row['column_name'] : $i); - if (strlen($row['comment']) > 0) { - $comment[$col] = $row['comment']; + if (PMA_DBI_num_rows($com_rs) > 0) { + $i = 0; + while ($row = PMA_DBI_fetch_assoc($com_rs)) { + $i++; + $col = ($table != '' ? $row['column_name'] : $i); + + if (strlen($row['comment']) > 0) { + $comment[$col] = $row['comment']; + // if this version supports native comments and this function + // was called with a table parameter + if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($table)) { + // if native comment found, use it instead of pmadb + if (!empty($native_comment[$col])) { + $comment[$col] = $native_comment[$col]; + } else { + // no native comment, so migrate pmadb-style to native + PMA_setComment($db, $table, $col, $comment[$col],'','native'); + // and erase the pmadb-style comment + PMA_setComment($db, $table, $col, '','','pmadb'); + } + } + } + } // end while + + PMA_DBI_free_result($com_rs); + unset($com_rs); + } else { + if (isset($native_comment)) { + $comment = $native_comment; } - - } // end while - PMA_DBI_free_result($com_rs); - unset($com_rs); + } if (isset($comment) && is_array($comment)) { return $comment; @@ -497,9 +532,41 @@ function PMA_handleSlashes($val) { * * @access public */ -function PMA_setComment($db, $table, $key, $value, $removekey = '') { +function PMA_setComment($db, $table, $col, $comment, $removekey = '', $mode='auto') { global $cfgRelation, $charset_connection; + + if ($mode=='auto') { + if (PMA_MYSQL_INT_VERSION >= 40100) { + $mode='native'; + } else { + $mode='pmadb'; + } + } + + if ($mode == 'native') { + $fields = PMA_DBI_get_fields($db, $table); + // TODO: get directly the information of $col + foreach($fields as $key=>$field) { + $tmp_col = $field['Field']; + $types[$tmp_col] = $field['Type']; + $collations[$tmp_col] = $field['Collation']; + $nulls[$tmp_col] = $field['Null']; + $defaults[$tmp_col] = $field['Default']; + $extras[$tmp_col] = $field['Extra']; + if ($tmp_col == $col) { + break; + } + } + $query = 'ALTER TABLE ' . PMA_backquote($table) . ' CHANGE ' + . PMA_generateAlterTable($col, $col, $types[$col], $collations[$col], $nulls[$col], $defaults[$col], $extras[$col], $comment); + PMA_DBI_try_query($query, NULL, PMA_DBI_QUERY_STORE); + return TRUE; + } + + // $mode == 'pmadb' section: + + if (PMA_MYSQL_INT_VERSION >= 40100) { $cols = array( 'db_name' => 'CONVERT(db_name USING ' . $charset_connection . ')', @@ -514,7 +581,7 @@ function PMA_setComment($db, $table, $key, $value, $removekey = '') { ); } - if ($removekey != '' AND $removekey != $key) { + if ($removekey != '' AND $removekey != $col) { $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_info']) . ' WHERE ' . $cols['db_name'] . ' = \'' . PMA_sqlAddslashes($db) . '\'' . ' AND ' . $cols['table_name'] . ' = \'' . PMA_sqlAddslashes($table) . '\'' @@ -526,33 +593,33 @@ function PMA_setComment($db, $table, $key, $value, $removekey = '') { $test_qry = 'SELECT ' . PMA_backquote('comment') . ', mimetype, transformation, transformation_options FROM ' . PMA_backquote($cfgRelation['column_info']) . ' WHERE ' . $cols['db_name'] . ' = \'' . PMA_sqlAddslashes($db) . '\'' . ' AND ' . $cols['table_name'] . ' = \'' . PMA_sqlAddslashes($table) . '\'' - . ' AND ' . $cols['column_name'] . ' = \'' . PMA_sqlAddslashes($key) . '\''; + . ' AND ' . $cols['column_name'] . ' = \'' . PMA_sqlAddslashes($col) . '\''; $test_rs = PMA_query_as_cu($test_qry, TRUE, PMA_DBI_QUERY_STORE); if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) { $row = PMA_DBI_fetch_assoc($test_rs); PMA_DBI_free_result($test_rs); - if (strlen($value) > 0 || strlen($row['mimetype']) > 0 || strlen($row['transformation']) > 0 || strlen($row['transformation_options']) > 0) { + if (strlen($comment) > 0 || strlen($row['mimetype']) > 0 || strlen($row['transformation']) > 0 || strlen($row['transformation_options']) > 0) { $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['column_info']) - . ' SET ' . PMA_backquote('comment') . ' = \'' . PMA_sqlAddslashes($value) . '\'' + . ' SET ' . PMA_backquote('comment') . ' = \'' . PMA_sqlAddslashes($comment) . '\'' . ' WHERE ' . $cols['db_name'] . ' = \'' . PMA_sqlAddslashes($db) . '\'' . ' AND ' . $cols['table_name'] . ' = \'' . PMA_sqlAddslashes($table) . '\'' - . ' AND ' . $cols['column_name'] . ' = \'' . PMA_sqlAddSlashes($key) . '\''; + . ' AND ' . $cols['column_name'] . ' = \'' . PMA_sqlAddSlashes($col) . '\''; } else { $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_info']) . ' WHERE ' . $cols['db_name'] . ' = \'' . PMA_sqlAddslashes($db) . '\'' . ' AND ' . $cols['table_name'] . ' = \'' . PMA_sqlAddslashes($table) . '\'' - . ' AND ' . $cols['column_name'] . ' = \'' . PMA_sqlAddslashes($key) . '\''; + . ' AND ' . $cols['column_name'] . ' = \'' . PMA_sqlAddslashes($col) . '\''; } - } else if (strlen($value) > 0) { + } else if (strlen($comment) > 0) { $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['column_info']) . ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ') ' . ' VALUES(' . '\'' . PMA_sqlAddslashes($db) . '\',' . '\'' . PMA_sqlAddslashes($table) . '\',' - . '\'' . PMA_sqlAddslashes($key) . '\',' - . '\'' . PMA_sqlAddslashes($value) . '\')'; + . '\'' . PMA_sqlAddslashes($col) . '\',' + . '\'' . PMA_sqlAddslashes($comment) . '\')'; } if (isset($upd_query)){ diff --git a/pdf_schema.php b/pdf_schema.php index d1802b509..038f23b61 100644 --- a/pdf_schema.php +++ b/pdf_schema.php @@ -1278,7 +1278,7 @@ function PMA_RT_DOC($alltables ){ $pdf->ln(); $cfgRelation = PMA_getRelationsParam(); - if ($cfgRelation['commwork']) { + if ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100) { $comments = PMA_getComments($db, $table); } if ($cfgRelation['mimework']) { diff --git a/tbl_addfield.php b/tbl_addfield.php index 9f4a9fa85..541dd1b9c 100644 --- a/tbl_addfield.php +++ b/tbl_addfield.php @@ -197,7 +197,11 @@ if (isset($submit_num_fields)) { $cfgRelation = PMA_getRelationsParam(); // garvin: Update comment table, if a comment was set. - if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork']) { + // lem9: FIXME: here we take care of native comments and + // pmadb-style comments, however, in the case of + // native comments, users do not see the COMMENT clause + // when SQL query is displayed + if (isset($field_comments) && is_array($field_comments) && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) { foreach ($field_comments AS $fieldindex => $fieldcomment) { PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment); } diff --git a/tbl_alter.php b/tbl_alter.php index 5547c129c..b7c50cc40 100644 --- a/tbl_alter.php +++ b/tbl_alter.php @@ -57,30 +57,17 @@ if (isset($do_save_data)) { } else { $query .= ', CHANGE '; } - $query .= PMA_backquote($field_orig[$i]) . ' ' . PMA_backquote($field_name[$i]) . ' ' . $field_type[$i]; - // Some field types shouldn't have lengths + + $full_field_type = $field_type[$i]; if ($field_length[$i] != '' && !preg_match('@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$@i', $field_type[$i])) { - $query .= '(' . $field_length[$i] . ')'; + $full_field_type .= '(' . $field_length[$i] . ')'; } if ($field_attribute[$i] != '') { - $query .= ' ' . $field_attribute[$i]; - } else if (PMA_MYSQL_INT_VERSION >= 40100 && $field_collation[$i] != '' && preg_match('@^(TINYTEXT|TEXT|MEDIUMTEXT|LONGTEXT|VARCHAR|CHAR)$@i', $field_type[$i])) { - $query .= PMA_generateCharsetQueryPart($field_collation[$i]); - } - if ($field_default[$i] != '') { - if (strtoupper($field_default[$i]) == 'NULL') { - $query .= ' DEFAULT NULL'; - } else { - $query .= ' DEFAULT \'' . PMA_sqlAddslashes($field_default[$i]) . '\''; - } - } - if ($field_null[$i] != '') { - $query .= ' ' . $field_null[$i]; - } - if ($field_extra[$i] != '') { - $query .= ' ' . $field_extra[$i]; + $full_field_type .= ' ' . $field_attribute[$i]; } + // take care of native MySQL comments here + $query .= PMA_generateAlterTable($field_orig[$i], $field_name[$i], $full_field_type, (PMA_MYSQL_INT_VERSION >= 40100 && $field_collation[$i] != '' ? $field_collation[$i] : ''), $field_null[$i], $field_default[$i], $field_extra[$i], (PMA_MYSQL_INT_VERSION >= 40100 && $field_comments[$i] != '' ? $field_comments[$i] : '')); } // end for // To allow replication, we first select the db to use and then run queries @@ -101,10 +88,11 @@ if (isset($do_save_data)) { $cfgRelation = PMA_getRelationsParam(); + // take care of pmadb internal comments here // garvin: Update comment table, if a comment was set. - if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork']) { + if (PMA_MYSQL_INT_VERSION < 40100 && isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork']) { foreach ($field_comments AS $fieldindex => $fieldcomment) { - PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment, $field_orig[$fieldindex]); + PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment, $field_orig[$fieldindex], 'pmadb'); } } @@ -191,7 +179,6 @@ if ($abort == FALSE) { $fields_meta[] = PMA_DBI_fetch_assoc($result); PMA_DBI_free_result($result); } - $num_fields = count($fields_meta); $action = 'tbl_alter.php'; require('./tbl_properties.inc.php'); diff --git a/tbl_create.php b/tbl_create.php index eb44d1bc5..183236842 100644 --- a/tbl_create.php +++ b/tbl_create.php @@ -2,7 +2,6 @@ /* $Id$ */ // vim: expandtab sw=4 ts=4 sts=4: - /** * Get some core libraries */ @@ -142,7 +141,7 @@ if (isset($submit_num_fields)) { } unset($unique); - // Builds the fulltextes statements + // Builds the FULLTEXT statements $fulltext = ''; $fulltext_cnt = (isset($field_fulltext) ? count($field_fulltext) : 0); for ($i = 0; $i < $fulltext_cnt; $i++) { @@ -194,7 +193,7 @@ if (isset($submit_num_fields)) { $cfgRelation = PMA_getRelationsParam(); // garvin: Update comment table, if a comment was set. - if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork']) { + if (isset($field_comments) && is_array($field_comments) && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) { foreach ($field_comments AS $fieldindex => $fieldcomment) { PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment); } @@ -239,7 +238,7 @@ if ($abort == FALSE) { else { $action = 'tbl_create.php'; require('./tbl_properties.inc.php'); - // Diplays the footer + // Displays the footer echo "\n"; require_once('./footer.inc.php'); }