diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog index 91f9aa45ea..84f102388b 100644 --- a/pandora_console/ChangeLog +++ b/pandora_console/ChangeLog @@ -1,3 +1,17 @@ +2009-02-24 Esteban Sanchez + + * include/functions_db.php: Fixed a bug in + format_array_to_where_clause_sql() when only offset and limit where + given. A new parameter was added to solve it, so the behaviour is a + bit different now (take a look to the doc examples). Changes were also + done in other functions to reflect this new situation. + + * include/functions_alerts.php, include/functions_modules.php: Updated + to changes in format_array_to_where_clause_sql() + + * include/functions_ui.php: Added a class to each link of the + paginator. Style correction. + 2009-02-24 Esteban Sanchez * include/auth/mysql.php: Added get_user_id(). Removed field check on diff --git a/pandora_console/include/functions_alerts.php b/pandora_console/include/functions_alerts.php index f3f8a7c7d6..675adec36d 100644 --- a/pandora_console/include/functions_alerts.php +++ b/pandora_console/include/functions_alerts.php @@ -617,7 +617,7 @@ function get_alerts_agent_module ($id_agent_module, $disabled = false, $filter = $where .= ' AND disabled = 0 '; if ($filter) { - $where .= ' AND '.format_array_to_where_clause_sql ($filter); + $where .= format_array_to_where_clause_sql ($filter, 'AND', ' AND '); } $sql = sprintf ('SELECT * FROM talert_template_modules diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php index 0f16956a3f..9562d5d045 100644 --- a/pandora_console/include/functions_db.php +++ b/pandora_console/include/functions_db.php @@ -1577,9 +1577,13 @@ function get_db_row_filter ($table, $filter, $fields = false, $where_join = 'AND } if (is_array ($filter)) - $filter = format_array_to_where_clause_sql ($filter, $where_join); + $filter = format_array_to_where_clause_sql ($filter, $where_join, ' WHERE '); + else if (is_string ($filter)) + $filter = 'WHERE '.$filter; + else + $filter = ''; - $sql = sprintf ('SELECT %s FROM %s WHERE %s', + $sql = sprintf ('SELECT %s FROM %s %s', $fields, $table, $filter); return get_db_row_sql ($sql); @@ -1652,9 +1656,13 @@ function get_db_all_rows_filter ($table, $filter, $fields = false, $where_join = } if (is_array ($filter)) - $filter = format_array_to_where_clause_sql ($filter, $where_join); + $filter = format_array_to_where_clause_sql ($filter, $where_join, ' WHERE '); + else if (is_string ($filter)) + $filter = 'WHERE '.$filter; + else + $filter = ''; - $sql = sprintf ('SELECT %s FROM %s WHERE %s', + $sql = sprintf ('SELECT %s FROM %s %s', $fields, $table, $filter); return get_db_all_rows_sql ($sql); @@ -1843,15 +1851,14 @@ function format_array_to_update_sql ($values) { * * This function is useful to generate a WHERE clause for a SQL sentence from * a list of values. Example code: - * - * - $values = array (); - $values['name'] = "Name"; - $values['description'] = "Long description"; - $values['limit'] = $config['block_size']; // Assume it's 20 - $sql = 'SELECT * FROM table WHERE '.format_array_to_where_clause_sql ($values); - echo $sql; - + +$values = array (); +$values['name'] = "Name"; +$values['description'] = "Long description"; +$values['limit'] = $config['block_size']; // Assume it's 20 +$sql = 'SELECT * FROM table WHERE '.format_array_to_where_clause_sql ($values); +echo $sql; + * Will return: * * SELECT * FROM table WHERE `name` = "Name" AND `description` = "Long description" LIMIT 20 @@ -1861,11 +1868,33 @@ function format_array_to_update_sql ($values) { * There are special parameters such as 'limit' and 'offset' that will be used * as LIMIT and OFFSET clauses respectively. * @param string Join operator. AND by default. + * @param string A prefix to be added to the string. It's useful when limit and + * offset could be given to avoid this cases: + +$values = array (); +$values['limit'] = 10; +$values['offset'] = 20; +$sql = 'SELECT * FROM table WHERE '.format_array_to_where_clause_sql ($values); +// Wrong SQL: SELECT * FROM table WHERE LIMIT 10 OFFSET 20 + +$values = array (); +$values['limit'] = 10; +$values['offset'] = 20; +$sql = 'SELECT * FROM table WHERE '.format_array_to_where_clause_sql ($values, 'AND', 'WHERE'); +// Good SQL: SELECT * FROM table LIMIT 10 OFFSET 20 + +$values = array (); +$values['value'] = 5; +$values['limit'] = 10; +$values['offset'] = 20; +$sql = 'SELECT * FROM table WHERE '.format_array_to_where_clause_sql ($values, 'AND', 'WHERE'); +// Good SQL: SELECT * FROM table WHERE value = 5 LIMIT 10 OFFSET 20 + * * @return string Values joined into an SQL string that can fits into the WHERE * clause of an SQL sentence. */ -function format_array_to_where_clause_sql ($values, $join = 'AND') { +function format_array_to_where_clause_sql ($values, $join = 'AND', $prefix = false) { $fields = array (); if (! is_array ($values)) { @@ -1875,6 +1904,15 @@ function format_array_to_where_clause_sql ($values, $join = 'AND') { $query = ''; $limit = ''; $offset = ''; + if (isset ($values['limit'])) { + $limit = sprintf (' LIMIT %d', $values['limit']); + unset ($values['limit']); + } + + if (isset ($values['offset'])) { + $offset = sprintf (' OFFSET %d', $values['offset']); + unset ($values['offset']); + } $i = 1; $max = count ($values); foreach ($values as $field => $value) { @@ -1882,16 +1920,6 @@ function format_array_to_where_clause_sql ($values, $join = 'AND') { /* Avoid numeric field names */ continue; - if ($field == 'limit') { - $limit = sprintf (' LIMIT %d', $value); - continue; - } - - if ($field == 'offset') { - $offset = sprintf (' OFFSET %d', $value); - continue; - } - if ($field[0] != "`") { $field = "`".$field."`"; } @@ -1912,7 +1940,7 @@ function format_array_to_where_clause_sql ($values, $join = 'AND') { $i++; } - return $query.$limit.$offset; + return (! empty ($query) ? $prefix: '').$query.$limit.$offset; } /** @@ -2729,12 +2757,11 @@ function process_sql_update ($table, $values, $where = false, $where_join = 'AND } if ($where) { - $query .= ' WHERE '; if (is_string ($where)) { /* FIXME: Should we clean the string for sanity? */ $query .= $where; } else if (is_array ($where)) { - $query .= format_array_to_where_clause_sql ($where, $where_join); + $query .= format_array_to_where_clause_sql ($where, $where_join, ' WHERE '); } } diff --git a/pandora_console/include/functions_modules.php b/pandora_console/include/functions_modules.php index f03ca5a806..7a58c0d887 100644 --- a/pandora_console/include/functions_modules.php +++ b/pandora_console/include/functions_modules.php @@ -93,8 +93,7 @@ function get_network_components ($id_module, $filter = false) { $where = ''; if (is_array ($filter)) { - $where = ' AND '; - $where .= format_array_to_where_clause_sql ($filter); + $where = format_array_to_where_clause_sql ($filter, 'AND', ' AND '); } $sql = sprintf ('SELECT * FROM tnetwork_component diff --git a/pandora_console/include/functions_ui.php b/pandora_console/include/functions_ui.php index e6d43cd3b9..bffbdd77e7 100644 --- a/pandora_console/include/functions_ui.php +++ b/pandora_console/include/functions_ui.php @@ -691,8 +691,8 @@ function pagination ($count, $url, $offset = 0, $pagination = 0, $return = false return false; } // If exists more registers than I can put in a page, calculate index markers - $index_counter = ceil($count/$pagination); // Number of blocks of block_size with data - $index_page = ceil($offset/$pagination)-(ceil($block_limit/2)); // block to begin to show data; + $index_counter = ceil ($count /$pagination); // Number of blocks of block_size with data + $index_page = ceil ($offset / $pagination) - (ceil ($block_limit / 2)); // block to begin to show data; if ($index_page < 0) $index_page = 0; @@ -719,13 +719,13 @@ function pagination ($count, $url, $offset = 0, $pagination = 0, $return = false $output = '
'; // Show GOTO FIRST button - $output .= ''.print_image ("images/control_start_blue.png", true, array ("class" => "bot")).' '; + $output .= ' '; // Show PREVIOUS button - if ($index_page > 0){ - $index_page_prev= ($index_page-(floor($block_limit/2)))*$pagination; + if ($index_page > 0) { + $index_page_prev = ($index_page - (floor ($block_limit / 2))) * $pagination; if ($index_page_prev < 0) $index_page_prev = 0; - $output .= ''.print_image ("images/control_rewind_blue.png", true, array ("class" => "bot")).''; + $output .= ''; } $output .= "  "; // Draw blocks markers @@ -741,7 +741,7 @@ function pagination ($count, $url, $offset = 0, $pagination = 0, $return = false $inicio_bloque_fake = $inicio_bloque + 1; // To Calculate last block (doesnt end with round data, // it must be shown if not round to block limit) - $output .= ''; + $output .= ''.print_image ("images/control_fastforward_blue.png", true, array ("class" => "bot")).''; + $output .= ''; $i = $index_counter; } // if exists more registers than i can put in a page (defined by $block_size config parameter) @@ -765,7 +765,7 @@ function pagination ($count, $url, $offset = 0, $pagination = 0, $return = false // as painted in last block (last integer block). if (($count - $pagination) > 0) { $myoffset = floor (($count - 1) / $pagination) * $pagination; - $output .= ''.print_image ("images/control_end_blue.png", true, array ("class" => "bot")).''; + $output .= ''; } // End div and layout $output .= "
";