Example #1
0
/**
 * Set a single comment to a certain value.
 *
 * @param   string   the name of the db
 * @param   string   the name of the table (may be empty in case of a db comment)
 * @param   string   the name of the column
 * @param   string   the value of the column
 * @param   string   (optional) if a column is renamed, this is the name of the former key which will get deleted
 * @param   string   whether we set pmadb comments, native comments or both
 *
 * @return  boolean  true, if comment-query was made.
 *
 * @global  array    the list of relations settings
 *
 * @access  public
 */
function PMA_setComment($db, $table, $col, $comment, $removekey = '', $mode = 'auto')
{
    global $cfgRelation;
    if ($mode == 'auto') {
        if (PMA_MYSQL_INT_VERSION >= 40100) {
            $mode = 'native';
        } else {
            $mode = 'pmadb';
        }
    }
    // native mode is only for column comments so we need a table name
    if ($mode == 'native' && isset($table) && strlen($table)) {
        $query = 'ALTER TABLE ' . PMA_backquote($table) . ' CHANGE ' . PMA_generateAlterTable($col, $col, '', '', '', '', FALSE, '', FALSE, '', $comment, '', '');
        PMA_DBI_try_query($query, null, PMA_DBI_QUERY_STORE);
        return TRUE;
    }
    // $mode == 'pmadb' section:
    $cols = array('db_name' => 'db_name    ', 'table_name' => 'table_name ', 'column_name' => 'column_name');
    if ($removekey != '' and $removekey != $col) {
        $remove_query = '
             DELETE FROM
                    ' . PMA_backquote($cfgRelation['db']) . '.' . 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($removekey) . '\'';
        PMA_query_as_cu($remove_query);
        unset($remove_query);
    }
    $test_qry = '
         SELECT ' . PMA_backquote('comment') . ',
                mimetype,
                transformation,
                transformation_options
           FROM ' . PMA_backquote($cfgRelation['db']) . '.' . 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($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($comment) > 0 || strlen($row['mimetype']) > 0 || strlen($row['transformation']) > 0 || strlen($row['transformation_options']) > 0) {
            $upd_query = '
                 UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
                    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($col) . '\'';
        } else {
            $upd_query = '
                 DELETE FROM 
                        ' . PMA_backquote($cfgRelation['db']) . '.' . 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($col) . '\'';
        }
    } elseif (strlen($comment) > 0) {
        $upd_query = '
             INSERT INTO
                    ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
                    (db_name, table_name, column_name, ' . PMA_backquote('comment') . ')
             VALUES (
                   \'' . PMA_sqlAddslashes($db) . '\',
                   \'' . PMA_sqlAddslashes($table) . '\',
                   \'' . PMA_sqlAddslashes($col) . '\',
                   \'' . PMA_sqlAddslashes($comment) . '\')';
    }
    if (isset($upd_query)) {
        $upd_rs = PMA_query_as_cu($upd_query);
        unset($upd_query);
        return true;
    } else {
        return false;
    }
}
Example #2
0
         $field_name[$i] = $field_orig[$i];
     }
     $field_default_orig[$i] = urldecode($field_default_orig[$i]);
     if (strcmp(str_replace('"', '"', $field_default_orig[$i]), $field_default[$i]) == 0) {
         $field_default[$i] = $field_default_orig[$i];
     }
     $field_length_orig[$i] = urldecode($field_length_orig[$i]);
     if (strcmp(str_replace('"', '"', $field_length_orig[$i]), $field_length[$i]) == 0) {
         $field_length[$i] = $field_length_orig[$i];
     }
     if (!isset($query)) {
         $query = '';
     } else {
         $query .= ', CHANGE ';
     }
     $query .= PMA_generateAlterTable($field_orig[$i], $field_name[$i], $field_type[$i], $field_length[$i], $field_attribute[$i], isset($field_collation[$i]) ? $field_collation[$i] : '', $field_null[$i], $field_default[$i], isset($field_default_current_timestamp[$i]), $field_extra[$i], isset($field_comments[$i]) ? $field_comments[$i] : '', $field_default_orig[$i]);
 }
 // end for
 // To allow replication, we first select the db to use and then run queries
 // on this db.
 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_DBI_getError(), 'USE ' . PMA_backquote($db) . ';', '', $err_url);
 // Optimization fix - 2 May 2001 - Robbat2
 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' CHANGE ' . $query;
 $error_create = FALSE;
 $result = PMA_DBI_try_query($sql_query) or $error_create = TRUE;
 if ($error_create == FALSE) {
     $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
     $btnDrop = 'Fake';
     // garvin: If comments were sent, enable relation stuff
     require_once './libraries/relation.lib.php';
     require_once './libraries/transformations.lib.php';
Example #3
0
/**
 * Set a single comment to a certain value.
 *
 * @param   string   the name of the db
 * @param   string   the name of the table (may be empty in case of a db comment)
 * @param   string   the name of the column
 * @param   string   the value of the column
 * @param   string   (optional) if a column is renamed, this is the name of the former key which will get deleted
 * @param   string   whether we set pmadb comments, native comments or both
 *
 * @return  boolean  true, if comment-query was made.
 *
 * @global  array    the list of relations settings
 *
 * @access  public
 */
function PMA_setComment($db, $table, $col, $comment, $removekey = '', $mode = 'auto')
{
    global $cfgRelation;
    if ($mode == 'auto') {
        if (PMA_MYSQL_INT_VERSION >= 40100) {
            $mode = 'native';
        } else {
            $mode = 'pmadb';
        }
    }
    // native mode is only for column comments so we need a table name
    if ($mode == 'native' && !empty($table)) {
        $fields = PMA_DBI_get_fields($db, $table);
        // Get more complete field information
        // For now, this is done just for MySQL 4.1.2+ new TIMESTAMP options
        // but later, if the analyser returns more information, it
        // could be executed for any MySQL version and replace
        // the info given by SHOW FULL FIELDS FROM.
        // TODO: put this code into a require()
        // or maybe make it part of PMA_DBI_get_fields();
        if (PMA_MYSQL_INT_VERSION >= 40102) {
            $show_create_table_query = 'SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table);
            $show_create_table_res = PMA_DBI_query($show_create_table_query);
            list(, $show_create_table) = PMA_DBI_fetch_row($show_create_table_res);
            PMA_DBI_free_result($show_create_table_res);
            unset($show_create_table_res, $show_create_table_query);
            $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_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 (PMA_MYSQL_INT_VERSION >= 40102 && isset($analyzed_sql[0]['create_table_fields'][$tmp_col]['on_update_current_timestamp'])) {
                $extras[$tmp_col] = 'ON UPDATE CURRENT_TIMESTAMP';
            }
            if (PMA_MYSQL_INT_VERSION >= 40102 && isset($analyzed_sql[0]['create_table_fields'][$tmp_col]['default_current_timestamp'])) {
                $default_current_timestamps[$tmp_col] = TRUE;
            } else {
                $default_current_timestamps[$tmp_col] = FALSE;
            }
            if ($tmp_col == $col) {
                break;
            }
        }
        if ($nulls[$col] == 'YES') {
            $nulls[$col] = '';
        } else {
            $nulls[$col] = 'NOT NULL';
        }
        $query = 'ALTER TABLE ' . PMA_backquote($table) . ' CHANGE ' . PMA_generateAlterTable($col, $col, $types[$col], $collations[$col], $nulls[$col], $defaults[$col], $default_current_timestamps[$col], $extras[$col], $comment);
        PMA_DBI_try_query($query, NULL, PMA_DBI_QUERY_STORE);
        return TRUE;
    }
    // $mode == 'pmadb' section:
    $cols = array('db_name' => 'db_name    ', 'table_name' => 'table_name ', 'column_name' => 'column_name');
    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) . '\'' . ' AND   ' . $cols['column_name'] . ' = \'' . PMA_sqlAddslashes($removekey) . '\'';
        PMA_query_as_cu($remove_query);
        unset($remove_query);
    }
    $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($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($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($comment) . '\'' . ' WHERE ' . $cols['db_name'] . ' = \'' . PMA_sqlAddslashes($db) . '\'' . ' AND   ' . $cols['table_name'] . ' = \'' . PMA_sqlAddslashes($table) . '\'' . ' 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($col) . '\'';
        }
    } 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($col) . '\',' . '\'' . PMA_sqlAddslashes($comment) . '\')';
        }
    }
    if (isset($upd_query)) {
        $upd_rs = PMA_query_as_cu($upd_query);
        unset($upd_query);
        return true;
    } else {
        return false;
    }
}
Example #4
0
         $field_length[$i] = $field_length_orig[$i];
     }
     if (!isset($query)) {
         $query = '';
     } else {
         $query .= ', CHANGE ';
     }
     $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])) {
         $full_field_type .= '(' . $field_length[$i] . ')';
     }
     if ($field_attribute[$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], isset($field_default_current_timestamp[$i]) ? $field_default_current_timestamp[$i] : '', $field_extra[$i], PMA_MYSQL_INT_VERSION >= 40100 && isset($field_comments[$i]) && $field_comments[$i] != '' ? $field_comments[$i] : '');
 }
 // end for
 // To allow replication, we first select the db to use and then run queries
 // on this db.
 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_DBI_getError(), 'USE ' . PMA_backquote($db) . ';', '', $err_url);
 // Optimization fix - 2 May 2001 - Robbat2
 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' CHANGE ' . $query;
 $error_create = FALSE;
 $result = PMA_DBI_try_query($sql_query) or $error_create = TRUE;
 if ($error_create == FALSE) {
     $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
     $btnDrop = 'Fake';
     // garvin: If comments were sent, enable relation stuff
     require_once './libraries/relation.lib.php';
     require_once './libraries/transformations.lib.php';