📄 table.class.php
字号:
for ($j = 0; $j < $primary_cnt && $field_primary[$j] != $index; $j++) { // void } // end for if (isset($field_primary[$j]) && $field_primary[$j] == $index) { $query .= ' PRIMARY KEY'; unset($field_primary[$j]); } // end if } // end if (auto_increment) } if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($comment)) { $query .= " COMMENT '" . PMA_sqlAddslashes($comment) . "'"; } return $query; } // end function /** * Counts and returns (or displays) the number of records in a table * * Revision 13 July 2001: Patch for limiting dump size from * vinay@sanisoft.com & girish@sanisoft.com * * @param string the current database name * @param string the current table name * @param boolean whether to retain or to displays the result * @param boolean whether to force an exact count * * @return mixed the number of records if retain is required, true else * * @access public */ function countRecords($db, $table, $ret = false, $force_exact = false) { $row_count = false; if (! $force_exact) { $row_count = PMA_DBI_fetch_value( 'SHOW TABLE STATUS FROM ' . PMA_backquote($db) . ' LIKE \'' . PMA_sqlAddslashes($table, true) . '\';', 0, 'Rows'); } $tbl_is_view = PMA_Table::isView($db, $table); // for a VIEW, $row_count is always false at this point if (false === $row_count || $row_count < $GLOBALS['cfg']['MaxExactCount']) { if (! $tbl_is_view) { $row_count = PMA_DBI_fetch_value( 'SELECT COUNT(*) FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table)); } else { // For complex views, even trying to get a partial record // count could bring down a server, so we offer an // alternative: setting MaxExactCountViews to 0 will bypass // completely the record counting for views if ($GLOBALS['cfg']['MaxExactCountViews'] == 0) { $row_count = 0; } else { // Counting all rows of a VIEW could be too long, so use // a LIMIT clause. // Use try_query because it can fail ( a VIEW is based on // a table that no longer exists) $result = PMA_DBI_try_query( 'SELECT 1 FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . ' LIMIT ' . $GLOBALS['cfg']['MaxExactCountViews'], null, PMA_DBI_QUERY_STORE); if (!PMA_DBI_getError()) { $row_count = PMA_DBI_num_rows($result); PMA_DBI_free_result($result); } } } } if ($ret) { return $row_count; } /** * @deprecated at the moment nowhere is $return = false used */ // Note: as of PMA 2.8.0, we no longer seem to be using // PMA_Table::countRecords() in display mode. echo PMA_formatNumber($row_count, 0); if ($tbl_is_view) { echo ' ' . sprintf($GLOBALS['strViewMaxExactCount'], $GLOBALS['cfg']['MaxExactCount'], '[a@./Documentation.html#cfg_MaxExactCount@_blank]', '[/a]'); } } // end of the 'PMA_Table::countRecords()' function /** * @todo add documentation */ function generateAlter($oldcol, $newcol, $type, $length, $attribute, $collation, $null, $default, $default_current_timestamp, $extra, $comment='', $default_orig) { $empty_a = array(); return PMA_backquote($oldcol) . ' ' . PMA_Table::generateFieldSpec($newcol, $type, $length, $attribute, $collation, $null, $default, $default_current_timestamp, $extra, $comment, $empty_a, -1, $default_orig); } // end function /** * Inserts existing entries in a PMA_* table by reading a value from an old entry * * @param string The array index, which Relation feature to check * ('relwork', 'commwork', ...) * @param string The array index, which PMA-table to update * ('bookmark', 'relation', ...) * @param array Which fields will be SELECT'ed from the old entry * @param array Which fields will be used for the WHERE query * (array('FIELDNAME' => 'FIELDVALUE')) * @param array Which fields will be used as new VALUES. These are the important * keys which differ from the old entry. * (array('FIELDNAME' => 'NEW FIELDVALUE')) * @global string relation variable * * @author Garvin Hicking <me@supergarv.de> */ function duplicateInfo($work, $pma_table, $get_fields, $where_fields, $new_fields) { $last_id = -1; if ($GLOBALS['cfgRelation'][$work]) { $select_parts = array(); $row_fields = array(); foreach ($get_fields as $get_field) { $select_parts[] = PMA_backquote($get_field); $row_fields[$get_field] = 'cc'; } $where_parts = array(); foreach ($where_fields as $_where => $_value) { $where_parts[] = PMA_backquote($_where) . ' = \'' . PMA_sqlAddslashes($_value) . '\''; } $new_parts = array(); $new_value_parts = array(); foreach ($new_fields as $_where => $_value) { $new_parts[] = PMA_backquote($_where); $new_value_parts[] = PMA_sqlAddslashes($_value); } $table_copy_query = ' SELECT ' . implode(', ', $select_parts) . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation'][$pma_table]) . ' WHERE ' . implode(' AND ', $where_parts); // must use PMA_DBI_QUERY_STORE here, since we execute another // query inside the loop $table_copy_rs = PMA_query_as_cu($table_copy_query, true, PMA_DBI_QUERY_STORE); while ($table_copy_row = @PMA_DBI_fetch_assoc($table_copy_rs)) { $value_parts = array(); foreach ($table_copy_row as $_key => $_val) { if (isset($row_fields[$_key]) && $row_fields[$_key] == 'cc') { $value_parts[] = PMA_sqlAddslashes($_val); } } $new_table_query = ' INSERT IGNORE INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation'][$pma_table]) . ' (' . implode(', ', $select_parts) . ', ' . implode(', ', $new_parts) . ') VALUES (\'' . implode('\', \'', $value_parts) . '\', \'' . implode('\', \'', $new_value_parts) . '\')'; PMA_query_as_cu($new_table_query); $last_id = PMA_DBI_insert_id(); } // end while PMA_DBI_free_result($table_copy_rs); return $last_id; } return true; } // end of 'PMA_Table::duplicateInfo()' function /** * Copies or renames table * @todo use RENAME for move operations * - would work only if the databases are on the same filesystem, * how can we check that? try the operation and * catch an error? * - for views, only if MYSQL > 50013 * - still have to handle pmadb synch. * * @author Michal Cihar <michal@cihar.com> */ function moveCopy($source_db, $source_table, $target_db, $target_table, $what, $move, $mode) { global $err_url; if (! isset($GLOBALS['sql_query'])) { $GLOBALS['sql_query'] = ''; } // set export settings we need $GLOBALS['sql_backquotes'] = 1; $GLOBALS['asfile'] = 1; // Ensure the target is valid if (! $GLOBALS['PMA_List_Database']->exists($source_db, $target_db)) { /** * @todo exit really needed here? or just a return? */ exit; } $source = PMA_backquote($source_db) . '.' . PMA_backquote($source_table); if (! isset($target_db) || ! strlen($target_db)) { $target_db = $source_db; } // Doing a select_db could avoid some problems with replicated databases, // when moving table from replicated one to not replicated one PMA_DBI_select_db($target_db); $target = PMA_backquote($target_db) . '.' . PMA_backquote($target_table); // do not create the table if dataonly if ($what != 'dataonly') { require_once './libraries/export/sql.php'; $no_constraints_comments = true; $GLOBALS['sql_constraints_query'] = ''; $sql_structure = PMA_getTableDef($source_db, $source_table, "\n", $err_url); unset($no_constraints_comments); $parsed_sql = PMA_SQP_parse($sql_structure); $analyzed_sql = PMA_SQP_analyze($parsed_sql); $i = 0; if (empty($analyzed_sql[0]['create_table_fields'])) { // this is not a CREATE TABLE, so find the first VIEW $target_for_view = PMA_backquote($target_db); while (true) { if ($parsed_sql[$i]['type'] == 'alpha_reservedWord' && $parsed_sql[$i]['data'] == 'VIEW') { break; } $i++; } } unset($analyzed_sql); /* nijel: Find table name in query and replace it */ while ($parsed_sql[$i]['type'] != 'quote_backtick') { $i++; } /* no need to PMA_backquote() */ if (isset($target_for_view)) { // this a view definition; we just found the first db name // that follows DEFINER VIEW // so change it for the new db name $parsed_sql[$i]['data'] = $target_for_view; // then we have to find all references to the source db // and change them to the target db, ensuring we stay into // the $parsed_sql limits $last = $parsed_sql['len'] - 1; $backquoted_source_db = PMA_backquote($source_db); for (++$i; $i <= $last; $i++) { if ($parsed_sql[$i]['type'] == 'quote_backtick' && $parsed_sql[$i]['data'] == $backquoted_source_db) { $parsed_sql[$i]['data'] = $target_for_view; } } unset($last,$backquoted_source_db); } else { $parsed_sql[$i]['data'] = $target; } /* Generate query back */ $sql_structure = PMA_SQP_formatHtml($parsed_sql, 'query_only'); // If table exists, and 'add drop table' is selected: Drop it! $drop_query = ''; if (isset($GLOBALS['drop_if_exists']) && $GLOBALS['drop_if_exists'] == 'true') { if (PMA_Table::_isView($target_db,$target_table)) { $drop_query = 'DROP VIEW'; } else { $drop_query = 'DROP TABLE'; } $drop_query .= ' IF EXISTS ' . PMA_backquote($target_db) . '.' . PMA_backquote($target_table); PMA_DBI_query($drop_query); $GLOBALS['sql_query'] .= "\n" . $drop_query . ';'; // garvin: If an existing table gets deleted, maintain any // entries for the PMA_* tables $maintain_relations = true; } @PMA_DBI_query($sql_structure); $GLOBALS['sql_query'] .= "\n" . $sql_structure . ';'; if (($move || isset($GLOBALS['add_constraints'])) && !empty($GLOBALS['sql_constraints_query'])) { $parsed_sql = PMA_SQP_parse($GLOBALS['sql_constraints_query']); $i = 0; // find the first quote_backtick, it must be the source table name while ($parsed_sql[$i]['type'] != 'quote_backtick') { $i++; // maybe someday we should guard against going over limit //if ($i == $parsed_sql['len']) { // break; //} } // replace it by the target table name, no need to PMA_backquote() $parsed_sql[$i]['data'] = $target; // now we must remove all quote_backtick that follow a CONSTRAINT // keyword, because a constraint name must be unique in a db $cnt = $parsed_sql['len'] - 1; for ($j = $i; $j < $cnt; $j++) { if ($parsed_sql[$j]['type'] == 'alpha_reservedWord' && strtoupper($parsed_sql[$j]['data']) == 'CONSTRAINT') { if ($parsed_sql[$j+1]['type'] == 'quote_backtick') { $parsed_sql[$j+1]['data'] = ''; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -