From d886a7956eaac1ded2e699ea957d103a7c898e83 Mon Sep 17 00:00:00 2001 From: Carlos Proensa Date: Sat, 26 Aug 2017 14:42:48 +0200 Subject: [PATCH] Cast custom fields distinct values Cast values for number based custom fields, when retrieving distinct values. This allows: - The values are sorted numerically on the list - The values returned match what will be shown in the custom field, in case of type mismatch. For example, when having text stored in a numeric field. Fixes: #23265, #23266 --- core/custom_field_api.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/core/custom_field_api.php b/core/custom_field_api.php index 3370c37657..5eef68ec0d 100644 --- a/core/custom_field_api.php +++ b/core/custom_field_api.php @@ -1228,16 +1228,30 @@ function custom_field_distinct_values( array $p_field_def, $p_project_id = ALL_P $t_filter_in = ' ( ' . $t_select_string . $t_from_string . $t_join_string . $t_where_string . ' )'; $t_params = $t_query_clauses['where_values']; - $t_query = 'SELECT DISTINCT cfst.value FROM {custom_field_string} cfst' + # which types need special type cast + switch( $p_field_def['type'] ) { + case CUSTOM_FIELD_TYPE_FLOAT: + # mysql can't cast to float, use alternative syntax + $t_select_expr = db_is_mysql() ? 'cfst.value+0.0' : 'CAST(cfst.value AS FLOAT)'; + break; + case CUSTOM_FIELD_TYPE_DATE: + case CUSTOM_FIELD_TYPE_NUMERIC: + $t_select_expr = 'CAST(cfst.value AS DECIMAL)'; + break; + default: # no cast needed + $t_select_expr = 'cfst.value'; + } + + $t_query = 'SELECT DISTINCT ' . $t_select_expr . ' AS cast_value FROM {custom_field_string} cfst' . ' WHERE cfst.bug_id IN ' . $t_filter_in . ' AND cfst.field_id = ' . db_param() - . ' ORDER BY cfst.value'; + . ' ORDER BY cast_value'; $t_params[] = (int)$p_field_def['id']; $t_result = db_query( $t_query, $t_params ); while( $t_row = db_fetch_array( $t_result ) ) { - if( !is_blank( trim( $t_row['value'] ) ) ) { - array_push( $t_return_arr, $t_row['value'] ); + if( !is_blank( trim( $t_row['cast_value'] ) ) ) { + array_push( $t_return_arr, $t_row['cast_value'] ); } }