Skip to content

Commit

Permalink
Remove use of db_num_rows for select queries which return data
Browse files Browse the repository at this point in the history
i.e. db_fetch_array is also called following the call of db_num_rows
  • Loading branch information
mantis committed Sep 6, 2014
1 parent a0c223a commit 575e3e7
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 90 deletions.
23 changes: 11 additions & 12 deletions core/custom_field_api.php
Expand Up @@ -918,12 +918,12 @@ function custom_field_get_sequence( $p_field_id, $p_project_id ) {
project_id=' . db_param();
$t_result = db_query( $t_query, array( $p_field_id, $p_project_id ), 1 );

if( 0 == db_num_rows( $t_result ) ) {
$t_row = db_fetch_array( $t_result );

if( !$t_row ) {
return false;
}

$t_row = db_fetch_array( $t_result );

return $t_row['sequence'];
}

Expand Down Expand Up @@ -1082,19 +1082,19 @@ function custom_field_distinct_values( array $p_field_def, $p_project_id = ALL_P
FROM ' . $t_from . '
WHERE ' . $t_where1 . $t_where2 . '
ORDER BY cfst.value';

$t_result = db_query( $t_query, $t_params );
$t_row_count = db_num_rows( $t_result );
if( 0 == $t_row_count ) {
return false;
}
$t_row_count = 0;

for( $i = 0;$i < $t_row_count;$i++ ) {
$t_row = db_fetch_array( $t_result );
while( $t_row = db_fetch_array( $t_result ) ) {
$t_row_count++;
if( !is_blank( trim( $t_row['value'] ) ) ) {
array_push( $t_return_arr, $t_row['value'] );
}
}

if( 0 == $t_row_count ) {
return false;
}
}
return $t_return_arr;
}
Expand Down Expand Up @@ -1178,7 +1178,7 @@ function custom_field_set_value( $p_field_id, $p_bug_id, $p_value, $p_log_insert
bug_id=' . db_param();
$t_result = db_query( $t_query, array( $p_field_id, $p_bug_id ) );

if( db_num_rows( $t_result ) > 0 ) {
if( $t_row = db_fetch_array( $t_result ) ) {
$t_query = 'UPDATE {custom_field_string}
SET ' . $t_value_field . '=' . db_param() . '
WHERE field_id=' . db_param() . ' AND
Expand All @@ -1190,7 +1190,6 @@ function custom_field_set_value( $p_field_id, $p_bug_id, $p_value, $p_log_insert
);
db_query( $t_query, $t_params );

$t_row = db_fetch_array( $t_result );
history_log_event_direct( $p_bug_id, $t_name, custom_field_database_to_value( $t_row[$t_value_field], $t_type ), $p_value );
} else {
$t_query = 'INSERT INTO {custom_field_string}
Expand Down
32 changes: 10 additions & 22 deletions core/filter_api.php
Expand Up @@ -2056,11 +2056,9 @@ function filter_get_bug_rows( &$p_page_number, &$p_per_page, &$p_page_count, &$p
}

$t_result = db_query( $t_select_string . $t_from_string . $t_join_string . $t_where_string . $t_order_string, $t_query_clauses['where_values'], $p_per_page, $t_offset );
$t_row_count = db_num_rows( $t_result );

$t_id_array_lastmod = array();
for( $i = 0;$i < $t_row_count;$i++ ) {
$t_row = db_fetch_array( $t_result );
while( $t_row = db_fetch_array( $t_result ) ) {
$t_id_array_lastmod[] = (int)$t_row['id'];
$t_rows[] = $t_row;
}
Expand All @@ -2082,8 +2080,7 @@ function filter_cache_result( array $p_rows, array $p_id_array_lastmod ) {
# perform query
$t_result = db_query( $t_query );
$t_row_count = db_num_rows( $t_result );
for( $i = 0;$i < $t_row_count;$i++ ) {
$t_row = db_fetch_array( $t_result );
while ( $t_row = db_fetch_array( $t_result ) ) {
$t_stats[$t_row['bug_id']] = $t_row;
}

Expand Down Expand Up @@ -4496,9 +4493,8 @@ function filter_db_set_for_current_user( $p_project_id, $p_is_public, $p_name, $
AND name=' . db_param();
$t_result = db_query( $t_query, array( $t_user_id, $c_project_id, $p_name ) );

if( db_num_rows( $t_result ) > 0 ) {
$t_row = db_fetch_array( $t_result );

$t_row = db_fetch_array( $t_result );
if( $t_row ) {
$t_query = 'UPDATE {filters}
SET is_public=' . db_param() . ',
filter_string=' . db_param() . '
Expand All @@ -4521,8 +4517,7 @@ function filter_db_set_for_current_user( $p_project_id, $p_is_public, $p_name, $
AND name=' . db_param();
$t_result = db_query( $t_query, array( $t_user_id, $c_project_id, $p_name ) );

if( db_num_rows( $t_result ) > 0 ) {
$t_row = db_fetch_array( $t_result );
if( $t_row = db_fetch_array( $t_result ) ) {
return $t_row['id'];
}

Expand Down Expand Up @@ -4557,9 +4552,7 @@ function filter_db_get_filter( $p_filter_id, $p_user_id = null ) {
$t_query = 'SELECT * FROM {filters} WHERE id=' . db_param();
$t_result = db_query( $t_query, array( $c_filter_id ) );

if( db_num_rows( $t_result ) > 0 ) {
$t_row = db_fetch_array( $t_result );

if( $t_row = db_fetch_array( $t_result ) ) {
if( $t_row['user_id'] != $t_user_id ) {
if( $t_row['is_public'] != true ) {
return null;
Expand Down Expand Up @@ -4603,8 +4596,7 @@ function filter_db_get_project_current( $p_project_id, $p_user_id = null ) {
AND name=' . db_param();
$t_result = db_query( $t_query, array( $c_user_id, $c_project_id, '' ) );

if( db_num_rows( $t_result ) > 0 ) {
$t_row = db_fetch_array( $t_result );
if( $t_row = db_fetch_array( $t_result ) ) {
return $t_row['id'];
}

Expand All @@ -4622,9 +4614,7 @@ function filter_db_get_name( $p_filter_id ) {
$t_query = 'SELECT * FROM {filters} WHERE id=' . db_param();
$t_result = db_query( $t_query, array( $c_filter_id ) );

if( db_num_rows( $t_result ) > 0 ) {
$t_row = db_fetch_array( $t_result );

if( $t_row = db_fetch_array( $t_result ) ) {
if( $t_row['user_id'] != auth_get_current_user_id() ) {
if( $t_row['is_public'] != true ) {
return null;
Expand Down Expand Up @@ -4659,7 +4649,7 @@ function filter_db_can_delete_filter( $p_filter_id ) {

$t_result = db_query( $t_query, array( $c_filter_id, $t_user_id, -1 ) );

if( db_num_rows( $t_result ) > 0 ) {
if( db_result( $result ) > 0 ) {
return true;
}

Expand Down Expand Up @@ -4733,10 +4723,8 @@ function filter_db_get_available_queries( $p_project_id = null, $p_user_id = nul
OR user_id = ' . db_param() . ')
ORDER BY is_public DESC, name ASC';
$t_result = db_query( $t_query, array( $t_project_id, db_prepare_bool( true ), $t_user_id ) );
$t_query_count = db_num_rows( $t_result );

for( $i = 0; $i < $t_query_count; $i++ ) {
$t_row = db_fetch_array( $t_result );
while( $t_row = db_fetch_array( $t_result ) ) {
$t_overall_query_arr[$t_row['id']] = $t_row['name'];
}

Expand Down
13 changes: 4 additions & 9 deletions core/print_api.php
Expand Up @@ -413,12 +413,10 @@ function print_news_item_option_list() {
WHERE project_id=' . db_param() . '
ORDER BY date_posted DESC';
}
$t_result = db_query( $t_query, ($t_global == true ? array() : array( $t_project_id ) ) );
$t_news_count = db_num_rows( $t_result );

for( $i = 0;$i < $t_news_count;$i++ ) {
$t_row = db_fetch_array( $t_result );
$t_result = db_query( $t_query, ($t_global == true ? array() : array( $t_project_id ) ) );

while( $t_row = db_fetch_array( $t_result ) ) {
$t_headline = string_display( $t_row['headline'] );
$t_announcement = $t_row['announcement'];
$t_view_state = $t_row['view_state'];
Expand Down Expand Up @@ -873,10 +871,8 @@ function print_build_option_list( $p_build = '' ) {
WHERE ' . $t_project_where . '
ORDER BY build DESC';
$t_result = db_query( $t_query );
$t_option_count = db_num_rows( $t_result );

for( $i = 0;$i < $t_option_count;$i++ ) {
$t_row = db_fetch_array( $t_result );
while( $t_row = db_fetch_array( $t_result ) ) {
$t_overall_build_arr[] = $t_row['build'];
}

Expand Down Expand Up @@ -1084,8 +1080,7 @@ function print_project_user_list_option_list2( $p_user_id ) {
ORDER BY p.name';
$t_result = db_query( $t_query, array( (int)$p_user_id, true ) );
$t_category_count = db_num_rows( $t_result );
for( $i = 0;$i < $t_category_count;$i++ ) {
$t_row = db_fetch_array( $t_result );
while( $t_row = db_fetch_array( $t_result ) ) {
$t_project_name = string_attribute( $t_row['name'] );
$t_user_id = $t_row['id'];
echo '<option value="' . $t_user_id . '">' . $t_project_name . '</option>';
Expand Down
29 changes: 13 additions & 16 deletions core/project_api.php
Expand Up @@ -169,10 +169,8 @@ function project_cache_all() {
if( !$g_cache_project_all ) {
$t_query = 'SELECT * FROM {project}';
$t_result = db_query( $t_query );
$t_count = db_num_rows( $t_result );
for( $i = 0;$i < $t_count;$i++ ) {
$t_row = db_fetch_array( $t_result );

while( $t_row = db_fetch_array( $t_result ) ) {
$g_cache_project[(int)$t_row['id']] = $t_row;
}

Expand Down Expand Up @@ -482,10 +480,11 @@ function project_get_id_by_name( $p_project_name ) {
$t_query = 'SELECT id FROM {project} WHERE name = ' . db_param();
$t_result = db_query( $t_query, array( $p_project_name ), 1 );

if( db_num_rows( $t_result ) == 0 ) {
return 0;
$t_id = db_result( $t_result );
if( $t_id ) {
return $t_id;
} else {
return db_result( $t_result );
return 0;
}
}

Expand Down Expand Up @@ -561,8 +560,9 @@ function project_get_local_user_access_level( $p_project_id, $p_user_id ) {
WHERE user_id=' . db_param() . ' AND project_id=' . db_param();
$t_result = db_query( $t_query, array( (int)$p_user_id, $p_project_id ) );

if( db_num_rows( $t_result ) > 0 ) {
return (int)db_result( $t_result );
$t_level = db_result( $t_result );
if( $t_level ) {
return (int)$t_level;
} else {
return false;
}
Expand All @@ -582,8 +582,8 @@ function project_get_local_user_rows( $p_project_id ) {
$t_user_rows = array();
$t_row_count = db_num_rows( $t_result );

for( $i = 0;$i < $t_row_count;$i++ ) {
array_push( $t_user_rows, db_fetch_array( $t_result ) );
while( $t_row = db_fetch_array( $t_result ) ) {
array_push( $t_user_rows, $t_row );
}

return $t_user_rows;
Expand Down Expand Up @@ -671,9 +671,7 @@ function project_get_all_user_rows( $p_project_id = ALL_PROJECTS, $p_access_leve
AND access_level ' . $t_global_access_clause;

$t_result = db_query( $t_query, array( $t_on ) );
$t_row_count = db_num_rows( $t_result );
for( $i = 0;$i < $t_row_count;$i++ ) {
$t_row = db_fetch_array( $t_result );
while( $t_row = db_fetch_array( $t_result ) ) {
$t_users[$t_row['id']] = $t_row;
}
}
Expand All @@ -687,9 +685,8 @@ function project_get_all_user_rows( $p_project_id = ALL_PROJECTS, $p_access_leve
AND l.project_id = ' . db_param();

$t_result = db_query( $t_query, array( $t_on, $c_project_id ) );
$t_row_count = db_num_rows( $t_result );
for( $i = 0; $i < $t_row_count; $i++ ) {
$t_row = db_fetch_array( $t_result );

while( $t_row = db_fetch_array( $t_result ) ) {
if( is_array( $p_access_level ) ) {
$t_keep = in_array( $t_row['access_level'], $p_access_level );
} else {
Expand Down
11 changes: 6 additions & 5 deletions core/sponsorship_api.php
Expand Up @@ -119,7 +119,9 @@ function sponsorship_cache_row( $p_sponsorship_id, $p_trigger_errors = true ) {
$t_query = 'SELECT * FROM {sponsorship} WHERE id=' . db_param();
$t_result = db_query( $t_query, array( $c_sponsorship_id ) );

if( 0 == db_num_rows( $t_result ) ) {
$t_row = db_fetch_array( $t_result );

if( !$t_row ) {
$g_cache_sponsorships[$c_sponsorship_id] = false;

if( $p_trigger_errors ) {
Expand All @@ -130,7 +132,6 @@ function sponsorship_cache_row( $p_sponsorship_id, $p_trigger_errors = true ) {
}
}

$t_row = db_fetch_array( $t_result );
$g_cache_sponsorships[$c_sponsorship_id] = $t_row;

return $t_row;
Expand Down Expand Up @@ -178,12 +179,12 @@ function sponsorship_get_id( $p_bug_id, $p_user_id = null ) {
$t_query = 'SELECT id FROM {sponsorship} WHERE bug_id=' . db_param() . ' AND user_id = ' . db_param();
$t_result = db_query( $t_query, array( (int)$p_bug_id, $c_user_id ), 1 );

if( db_num_rows( $t_result ) == 0 ) {
$t_row = db_fetch_array( $t_result );

if( !$t_row ) {
return false;
}

$t_row = db_fetch_array( $t_result );

return (integer)$t_row['id'];
}

Expand Down
4 changes: 2 additions & 2 deletions core/tag_api.php
Expand Up @@ -59,10 +59,10 @@
* @return boolean True if tag exists
*/
function tag_exists( $p_tag_id ) {
$t_query = 'SELECT * FROM {tag} WHERE id=' . db_param();
$t_query = 'SELECT id FROM {tag} WHERE id=' . db_param();
$t_result = db_query( $t_query, array( $p_tag_id ) );

return db_num_rows( $t_result ) > 0;
return ( db_result( $t_result ) !== false );
}

/**
Expand Down
6 changes: 1 addition & 5 deletions core/user_api.php
Expand Up @@ -239,11 +239,7 @@ function user_is_name_unique( $p_username ) {
$t_query = 'SELECT username FROM {user} WHERE username=' . db_param();
$t_result = db_query( $t_query, array( $p_username ), 1 );

if( db_num_rows( $t_result ) > 0 ) {
return false;
} else {
return true;
}
return !db_result( $t_result );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lost_pwd.php
Expand Up @@ -74,16 +74,16 @@
# @todo Consider moving this query to user_api.php
$t_query = 'SELECT id FROM {user} WHERE username = ' . db_param() . ' AND email = ' . db_param() . ' AND enabled=' . db_param();
$t_result = db_query( $t_query, array( $f_username, $f_email, true ) );
$t_row = db_fetch_array( $t_result );

if( 0 == db_num_rows( $t_result ) ) {
if( !$t_row ) {
trigger_error( ERROR_LOST_PASSWORD_NOT_MATCHING_DATA, ERROR );
}

if( is_blank( $f_email ) ) {
trigger_error( ERROR_LOST_PASSWORD_NO_EMAIL_SPECIFIED, ERROR );
}

$t_row = db_fetch_array( $t_result );
$t_user_id = $t_row['id'];

if( user_is_protected( $t_user_id ) ) {
Expand Down

0 comments on commit 575e3e7

Please sign in to comment.