getUniqueCondition() public static method

Function to generate unique condition for specified row.
public static getUniqueCondition ( resource $handle, integer $fields_cnt, array $fields_meta, array $row, boolean $force_unique = false, string | boolean $restrict_to_table = false, array $analyzed_sql_results = null ) : array
$handle resource current query result
$fields_cnt integer number of fields
$fields_meta array meta information about fields
$row array current row
$force_unique boolean generate condition only on pk or unique
$restrict_to_table string | boolean restrict the unique condition to this table or false if none
$analyzed_sql_results array the analyzed query
return array the calculated condition and whether condition is unique
 /**
  * Zoom submit action
  *
  * @param string $dataLabel Data label
  * @param string $goto      Goto
  *
  * @return void
  */
 public function zoomSubmitAction($dataLabel, $goto)
 {
     //Query generation part
     $sql_query = $this->_buildSqlQuery();
     $sql_query .= ' LIMIT ' . $_POST['maxPlotLimit'];
     //Query execution part
     $result = $this->dbi->query($sql_query . ";", null, DatabaseInterface::QUERY_STORE);
     $fields_meta = $this->dbi->getFieldsMeta($result);
     $data = array();
     while ($row = $this->dbi->fetchAssoc($result)) {
         //Need a row with indexes as 0,1,2 for the getUniqueCondition
         // hence using a temporary array
         $tmpRow = array();
         foreach ($row as $val) {
             $tmpRow[] = $val;
         }
         //Get unique condition on each row (will be needed for row update)
         $uniqueCondition = Util::getUniqueCondition($result, count($this->_columnNames), $fields_meta, $tmpRow, true, false, null);
         //Append it to row array as where_clause
         $row['where_clause'] = $uniqueCondition[0];
         $tmpData = array($_POST['criteriaColumnNames'][0] => $row[$_POST['criteriaColumnNames'][0]], $_POST['criteriaColumnNames'][1] => $row[$_POST['criteriaColumnNames'][1]], 'where_clause' => $uniqueCondition[0]);
         $tmpData[$dataLabel] = $dataLabel ? $row[$dataLabel] : '';
         $data[] = $tmpData;
     }
     unset($tmpData);
     //Displays form for point data and scatter plot
     $titles = array('Browse' => Util::getIcon('b_browse.png', __('Browse foreign values')));
     $this->response->addHTML(Template::get('table/search/zoom_result_form')->render(array('_db' => $this->db, '_table' => $this->table, '_columnNames' => $this->_columnNames, '_foreigners' => $this->_foreigners, '_columnNullFlags' => $this->_columnNullFlags, '_columnTypes' => $this->_columnTypes, 'titles' => $titles, 'goto' => $goto, 'data' => $data)));
 }
Esempio n. 2
0
 /**
  * Prepare multi field edit/delete links
  *
  * @param integer &$dt_result           the link id associated to the query which
  *                                      results have to be displayed
  * @param array   $analyzed_sql_results analyzed sql results
  * @param string  $del_link             the display element - 'del_link'
  *
  * @return string $links_html html content
  *
  * @access  private
  *
  * @see     getTable()
  */
 private function _getMultiRowOperationLinks(&$dt_result, $analyzed_sql_results, $del_link)
 {
     $links_html = '<div class="print_ignore" >';
     $url_query = $this->__get('url_query');
     $delete_text = $del_link == self::DELETE_ROW ? __('Delete') : __('Kill');
     $links_html .= '<img class="selectallarrow" width="38" height="22"' . ' src="' . $this->__get('pma_theme_image') . 'arrow_' . $this->__get('text_dir') . '.png' . '"' . ' alt="' . __('With selected:') . '" />';
     $links_html .= '<input type="checkbox" ' . 'id="resultsForm_' . $this->__get('unique_id') . '_checkall" ' . 'class="checkall_box" title="' . __('Check all') . '" /> ' . '<label for="resultsForm_' . $this->__get('unique_id') . '_checkall">' . __('Check all') . '</label> ' . '<i style="margin-left: 2em">' . __('With selected:') . '</i>' . "\n";
     $links_html .= Util::getButtonOrImage('submit_mult', 'mult_submit', 'submit_mult_change', __('Edit'), 'b_edit.png', 'edit');
     $links_html .= Util::getButtonOrImage('submit_mult', 'mult_submit', 'submit_mult_copy', __('Copy'), 'b_insrow.png', 'copy');
     $links_html .= Util::getButtonOrImage('submit_mult', 'mult_submit', 'submit_mult_delete', $delete_text, 'b_drop.png', 'delete');
     if ($analyzed_sql_results['querytype'] == 'SELECT') {
         $links_html .= Util::getButtonOrImage('submit_mult', 'mult_submit', 'submit_mult_export', __('Export'), 'b_tblexport.png', 'export');
     }
     $links_html .= "</div>\n";
     $links_html .= '<input type="hidden" name="sql_query"' . ' value="' . htmlspecialchars($this->__get('sql_query')) . '" />' . "\n";
     if (!empty($url_query)) {
         $links_html .= '<input type="hidden" name="url_query"' . ' value="' . $url_query . '" />' . "\n";
     }
     // fetch last row of the result set
     $GLOBALS['dbi']->dataSeek($dt_result, $this->__get('num_rows') - 1);
     $row = $GLOBALS['dbi']->fetchRow($dt_result);
     // $clause_is_unique is needed by getTable() to generate the proper param
     // in the multi-edit and multi-delete form
     list($where_clause, $clause_is_unique, $condition_array) = Util::getUniqueCondition($dt_result, $this->__get('fields_cnt'), $this->__get('fields_meta'), $row, false, false, $analyzed_sql_results);
     unset($where_clause, $condition_array);
     // reset to first row for the loop in _getTableBody()
     $GLOBALS['dbi']->dataSeek($dt_result, 0);
     $links_html .= '<input type="hidden" name="clause_is_unique"' . ' value="' . $clause_is_unique . '" />' . "\n";
     $links_html .= '</form>' . "\n";
     return $links_html;
 }
Esempio n. 3
0
 /**
  * Outputs the content of a table in SQL format
  *
  * @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 string $sql_query SQL query for obtaining data
  * @param array  $aliases   Aliases of db/table/columns
  *
  * @return bool Whether it succeeded
  */
 public function exportData($db, $table, $crlf, $error_url, $sql_query, $aliases = array())
 {
     global $current_row, $sql_backquotes;
     $db_alias = $db;
     $table_alias = $table;
     $this->initAlias($aliases, $db_alias, $table_alias);
     if (isset($GLOBALS['sql_compatibility'])) {
         $compat = $GLOBALS['sql_compatibility'];
     } else {
         $compat = 'NONE';
     }
     $formatted_table_name = Util::backquoteCompat($table_alias, $compat, $sql_backquotes);
     // Do not export data for a VIEW, unless asked to export the view as a table
     // (For a VIEW, this is called only when exporting a single VIEW)
     if ($GLOBALS['dbi']->getTable($db, $table)->isView() && empty($GLOBALS['sql_views_as_tables'])) {
         $head = $this->_possibleCRLF() . $this->_exportComment() . $this->_exportComment('VIEW ' . ' ' . $formatted_table_name) . $this->_exportComment(__('Data:') . ' ' . __('None')) . $this->_exportComment() . $this->_possibleCRLF();
         if (!PMA_exportOutputHandler($head)) {
             return false;
         }
         return true;
     }
     $result = $GLOBALS['dbi']->tryQuery($sql_query, null, DatabaseInterface::QUERY_UNBUFFERED);
     // a possible error: the table has crashed
     $tmp_error = $GLOBALS['dbi']->getError();
     if ($tmp_error) {
         return PMA_exportOutputHandler($this->_exportComment(__('Error reading data:') . ' (' . $tmp_error . ')'));
     }
     if ($result == false) {
         $GLOBALS['dbi']->freeResult($result);
         return true;
     }
     $fields_cnt = $GLOBALS['dbi']->numFields($result);
     // Get field information
     $fields_meta = $GLOBALS['dbi']->getFieldsMeta($result);
     $field_flags = array();
     for ($j = 0; $j < $fields_cnt; $j++) {
         $field_flags[$j] = $GLOBALS['dbi']->fieldFlags($result, $j);
     }
     $field_set = array();
     for ($j = 0; $j < $fields_cnt; $j++) {
         $col_as = $fields_meta[$j]->name;
         if (!empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
             $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
         }
         $field_set[$j] = Util::backquoteCompat($col_as, $compat, $sql_backquotes);
     }
     if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'UPDATE') {
         // update
         $schema_insert = 'UPDATE ';
         if (isset($GLOBALS['sql_ignore'])) {
             $schema_insert .= 'IGNORE ';
         }
         // avoid EOL blank
         $schema_insert .= Util::backquoteCompat($table_alias, $compat, $sql_backquotes) . ' SET';
     } else {
         // insert or replace
         if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'REPLACE') {
             $sql_command = 'REPLACE';
         } else {
             $sql_command = 'INSERT';
         }
         // delayed inserts?
         if (isset($GLOBALS['sql_delayed'])) {
             $insert_delayed = ' DELAYED';
         } else {
             $insert_delayed = '';
         }
         // insert ignore?
         if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'INSERT' && isset($GLOBALS['sql_ignore'])) {
             $insert_delayed .= ' IGNORE';
         }
         //truncate table before insert
         if (isset($GLOBALS['sql_truncate']) && $GLOBALS['sql_truncate'] && $sql_command == 'INSERT') {
             $truncate = 'TRUNCATE TABLE ' . Util::backquoteCompat($table_alias, $compat, $sql_backquotes) . ";";
             $truncatehead = $this->_possibleCRLF() . $this->_exportComment() . $this->_exportComment(__('Truncate table before insert') . ' ' . $formatted_table_name) . $this->_exportComment() . $crlf;
             PMA_exportOutputHandler($truncatehead);
             PMA_exportOutputHandler($truncate);
         }
         // scheme for inserting fields
         if ($GLOBALS['sql_insert_syntax'] == 'complete' || $GLOBALS['sql_insert_syntax'] == 'both') {
             $fields = implode(', ', $field_set);
             $schema_insert = $sql_command . $insert_delayed . ' INTO ' . Util::backquoteCompat($table_alias, $compat, $sql_backquotes) . ' (' . $fields . ') VALUES';
         } else {
             $schema_insert = $sql_command . $insert_delayed . ' INTO ' . Util::backquoteCompat($table_alias, $compat, $sql_backquotes) . ' VALUES';
         }
     }
     //\x08\\x09, not required
     $search = array("", "\n", "\r", "");
     $replace = array('\\0', '\\n', '\\r', '\\Z');
     $current_row = 0;
     $query_size = 0;
     if (($GLOBALS['sql_insert_syntax'] == 'extended' || $GLOBALS['sql_insert_syntax'] == 'both') && (!isset($GLOBALS['sql_type']) || $GLOBALS['sql_type'] != 'UPDATE')) {
         $separator = ',';
         $schema_insert .= $crlf;
     } else {
         $separator = ';';
     }
     while ($row = $GLOBALS['dbi']->fetchRow($result)) {
         if ($current_row == 0) {
             $head = $this->_possibleCRLF() . $this->_exportComment() . $this->_exportComment(__('Dumping data for table') . ' ' . $formatted_table_name) . $this->_exportComment() . $crlf;
             if (!PMA_exportOutputHandler($head)) {
                 return false;
             }
         }
         // We need to SET IDENTITY_INSERT ON for MSSQL
         if (isset($GLOBALS['sql_compatibility']) && $GLOBALS['sql_compatibility'] == 'MSSQL' && $current_row == 0) {
             if (!PMA_exportOutputHandler('SET IDENTITY_INSERT ' . Util::backquoteCompat($table_alias, $compat, $sql_backquotes) . ' ON ;' . $crlf)) {
                 return false;
             }
         }
         $current_row++;
         $values = array();
         for ($j = 0; $j < $fields_cnt; $j++) {
             // NULL
             if (!isset($row[$j]) || is_null($row[$j])) {
                 $values[] = 'NULL';
             } elseif ($fields_meta[$j]->numeric && $fields_meta[$j]->type != 'timestamp' && !$fields_meta[$j]->blob) {
                 // a number
                 // timestamp is numeric on some MySQL 4.1, BLOBs are
                 // sometimes numeric
                 $values[] = $row[$j];
             } elseif (stristr($field_flags[$j], 'BINARY') !== false && isset($GLOBALS['sql_hex_for_binary'])) {
                 // a true BLOB
                 // - mysqldump only generates hex data when the --hex-blob
                 //   option is used, for fields having the binary attribute
                 //   no hex is generated
                 // - a TEXT field returns type blob but a real blob
                 //   returns also the 'binary' flag
                 // empty blobs need to be different, but '0' is also empty
                 // :-(
                 if (empty($row[$j]) && $row[$j] != '0') {
                     $values[] = '\'\'';
                 } else {
                     $values[] = '0x' . bin2hex($row[$j]);
                 }
             } elseif ($fields_meta[$j]->type == 'bit') {
                 // detection of 'bit' works only on mysqli extension
                 $values[] = "b'" . Util::sqlAddSlashes(Util::printableBitValue($row[$j], $fields_meta[$j]->length)) . "'";
             } elseif (!empty($GLOBALS['exporting_metadata']) && $row[$j] == '@LAST_PAGE') {
                 $values[] = '@LAST_PAGE';
             } else {
                 // something else -> treat as a string
                 $values[] = '\'' . str_replace($search, $replace, Util::sqlAddSlashes($row[$j])) . '\'';
             }
             // end if
         }
         // end for
         // should we make update?
         if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'UPDATE') {
             $insert_line = $schema_insert;
             for ($i = 0; $i < $fields_cnt; $i++) {
                 if (0 == $i) {
                     $insert_line .= ' ';
                 }
                 if ($i > 0) {
                     // avoid EOL blank
                     $insert_line .= ',';
                 }
                 $insert_line .= $field_set[$i] . ' = ' . $values[$i];
             }
             list($tmp_unique_condition, $tmp_clause_is_unique) = Util::getUniqueCondition($result, $fields_cnt, $fields_meta, $row, false, false, null);
             $insert_line .= ' WHERE ' . $tmp_unique_condition;
             unset($tmp_unique_condition, $tmp_clause_is_unique);
         } else {
             // Extended inserts case
             if ($GLOBALS['sql_insert_syntax'] == 'extended' || $GLOBALS['sql_insert_syntax'] == 'both') {
                 if ($current_row == 1) {
                     $insert_line = $schema_insert . '(' . implode(', ', $values) . ')';
                 } else {
                     $insert_line = '(' . implode(', ', $values) . ')';
                     $insertLineSize = mb_strlen($insert_line);
                     $sql_max_size = $GLOBALS['sql_max_query_size'];
                     if (isset($sql_max_size) && $sql_max_size > 0 && $query_size + $insertLineSize > $sql_max_size) {
                         if (!PMA_exportOutputHandler(';' . $crlf)) {
                             return false;
                         }
                         $query_size = 0;
                         $current_row = 1;
                         $insert_line = $schema_insert . $insert_line;
                     }
                 }
                 $query_size += mb_strlen($insert_line);
                 // Other inserts case
             } else {
                 $insert_line = $schema_insert . '(' . implode(', ', $values) . ')';
             }
         }
         unset($values);
         if (!PMA_exportOutputHandler(($current_row == 1 ? '' : $separator . $crlf) . $insert_line)) {
             return false;
         }
     }
     // end while
     if ($current_row > 0) {
         if (!PMA_exportOutputHandler(';' . $crlf)) {
             return false;
         }
     }
     // We need to SET IDENTITY_INSERT OFF for MSSQL
     if (isset($GLOBALS['sql_compatibility']) && $GLOBALS['sql_compatibility'] == 'MSSQL' && $current_row > 0) {
         $outputSucceeded = PMA_exportOutputHandler($crlf . 'SET IDENTITY_INSERT ' . Util::backquoteCompat($table_alias, $compat, $sql_backquotes) . ' OFF;' . $crlf);
         if (!$outputSucceeded) {
             return false;
         }
     }
     $GLOBALS['dbi']->freeResult($result);
     return true;
 }