Skip to content

Commit

Permalink
Replace remaining uses of deprecated db_num_rows() function
Browse files Browse the repository at this point in the history
The db_num_rows() function is deprecated with the new database API
because PDO doesn't know how many rows are being returned until all rows
have been requested from the server. We therefore have to perform the
row count ourselves - when necessary.

In many cases it may make more sense to perform a SELECT(*) query to
retrieve counts. This is less costly than returning data that we're not
going to use. This is something to consider in the future as the core is
upgraded to use more modern/robust database queries.
  • Loading branch information
davidhicks committed Dec 18, 2011
1 parent e1c3b57 commit 393315c
Show file tree
Hide file tree
Showing 12 changed files with 309 additions and 252 deletions.
33 changes: 17 additions & 16 deletions application/core/bug_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1337,30 +1337,33 @@ function bug_get_bugnote_stats( $p_bug_id ) {
global $g_cache_bug;
$c_bug_id = db_prepare_int( $p_bug_id );

if( !is_null( $g_cache_bug[$c_bug_id]['_stats'] ) ) {
if( $g_cache_bug[$c_bug_id]['_stats'] === false ) {
if ( !is_null( $g_cache_bug[$c_bug_id]['_stats'] ) ) {
if ( $g_cache_bug[$c_bug_id]['_stats'] === false )
return false;
} else {
else
$t_stats = $g_cache_bug[$c_bug_id]['_stats'];
}
return $t_stats;
}

$t_bugnote_table = db_get_table( 'bugnote' );

$query = "SELECT last_modified
$t_query = "SELECT last_modified
FROM $t_bugnote_table
WHERE bug_id=" . db_param() . "
ORDER BY last_modified DESC";
$result = db_query_bound( $query, array( $c_bug_id ) );
$row = db_fetch_array( $result );
$t_result = db_query_bound( $t_query, array( $c_bug_id ) );

if( false === $row ) {
$t_bugnote_count = 0;
while ( $t_row = db_fetch_array( $t_result ) ) {
$t_bugnote_count++;
}

if ( $t_bugnote_count === 0 ) {
return false;
}

$t_stats['last_modified'] = $row['last_modified'];
$t_stats['count'] = db_num_rows( $result );
$t_stats['last_modified'] = $t_row['last_modified'];
$t_stats['count'] = $t_bugnote_count;

return $t_stats;
}
Expand Down Expand Up @@ -1770,17 +1773,15 @@ function bug_get_monitors( $p_bug_id ) {
$t_user_table = db_get_table( 'user' );

# get the bugnote data
$query = "SELECT user_id, enabled
$t_query = "SELECT user_id, enabled
FROM $t_bug_monitor_table m, $t_user_table u
WHERE m.bug_id=" . db_param() . " AND m.user_id = u.id
ORDER BY u.realname, u.username";
$result = db_query_bound($query, array( $c_bug_id ) );
$num_users = db_num_rows($result);
$t_result = db_query_bound( $t_query, array( $c_bug_id ) );

$t_users = array();
for ( $i = 0; $i < $num_users; $i++ ) {
$row = db_fetch_array( $result );
$t_users[$i] = $row['user_id'];
while( $t_row = db_fetch_array( $t_result ) ) {
$t_users[] = $t_row['user_id'];
}

user_cache_array_rows( $t_users );
Expand Down
82 changes: 42 additions & 40 deletions application/core/file_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,44 +341,45 @@ function file_delete_attachments( $p_bug_id ) {
$t_method = config_get( 'file_upload_method' );

# Delete files from disk
$query = "SELECT diskfile, filename
$t_query = "SELECT diskfile, filename
FROM $t_bug_file_table
WHERE bug_id=" . db_param();
$result = db_query_bound( $query, array( $c_bug_id ) );
$t_result = db_query_bound( $t_query, array( $c_bug_id ) );

$file_count = db_num_rows( $result );
if( 0 == $file_count ) {
return true;
$t_files = array();
while ( $t_row = db_fetch_array( $t_result ) ) {
$t_files[] = $t_row;
}

if(( DISK == $t_method ) || ( FTP == $t_method ) ) {
$t_file_count = count( $t_files );
if ( $t_file_count === 0 )
return true;

if ( ( $t_method == DISK ) || ( $t_method == FTP ) ) {

# there may be more than one file
$ftp = 0;
if( FTP == $t_method ) {
$ftp = file_ftp_connect();
}
$t_ftp = 0;
if ( FTP == $t_method )
$t_ftp = file_ftp_connect();

for( $i = 0;$i < $file_count;$i++ ) {
$row = db_fetch_array( $result );
for ( $i = 0; $i < $t_file_count; $i++ ) {
$t_file = $t_files[$i];

$t_local_diskfile = file_normalize_attachment_path( $row['diskfile'], bug_get_field( $p_bug_id, 'project_id' ) );
$t_local_diskfile = file_normalize_attachment_path( $t_file['diskfile'], bug_get_field( $p_bug_id, 'project_id' ) );
file_delete_local( $t_local_diskfile );

if( FTP == $t_method ) {
file_ftp_delete( $ftp, $row['diskfile'] );
}
if ( $t_method == FTP )
file_ftp_delete( $t_ftp, $t_file['diskfile'] );
}

if( FTP == $t_method ) {
file_ftp_disconnect( $ftp );
}
if ( $t_method == FTP )
file_ftp_disconnect( $t_ftp );
}

# Delete the corresponding db records
$query = "DELETE FROM $t_bug_file_table
$t_query = "DELETE FROM $t_bug_file_table
WHERE bug_id=" . db_param();
$result = db_query_bound( $query, array( $c_bug_id ) );
$t_result = db_query_bound( $t_query, array( $c_bug_id ) );

# db_query errors on failure so:
return true;
Expand All @@ -389,41 +390,42 @@ function file_delete_project_files( $p_project_id ) {
$t_method = config_get( 'file_upload_method' );

# Delete the file physically (if stored via DISK or FTP)
if(( DISK == $t_method ) || ( FTP == $t_method ) ) {
if ( ( $t_method == DISK ) || ( $t_method == FTP ) ) {

# Delete files from disk
$query = "SELECT diskfile, filename
$t_query = "SELECT diskfile, filename
FROM $t_project_file_table
WHERE project_id=" . db_param();
$result = db_query_bound( $query, array( (int) $p_project_id ) );

$file_count = db_num_rows( $result );
$t_result = db_query_bound( $t_query, array( (int) $p_project_id ) );

$ftp = 0;
if( FTP == $t_method ) {
$ftp = file_ftp_connect();
$t_files = array();
while ( $t_row = db_fetch_array( $t_result ) ) {
$t_files[] = $t_row;
}

for( $i = 0;$i < $file_count;$i++ ) {
$row = db_fetch_array( $result );
$t_ftp = 0;
if ( $t_method == FTP )
$t_ftp = file_ftp_connect();

$t_file_count = count( $t_files );
for ( $i = 0; $i < $t_file_count; $i++ ) {
$t_file = $t_files[$i];

$t_local_diskfile = file_normalize_attachment_path( $row['diskfile'], $p_project_id );
$t_local_diskfile = file_normalize_attachment_path( $t_file['diskfile'], $p_project_id );
file_delete_local( $t_local_diskfile );

if( FTP == $t_method ) {
file_ftp_delete( $ftp, $row['diskfile'] );
}
if ( $t_method == FTP )
file_ftp_delete( $t_ftp, $t_file['diskfile'] );
}

if( FTP == $t_method ) {
file_ftp_disconnect( $ftp );
}
if ( $t_method == FTP )
file_ftp_disconnect( $t_ftp );
}

# Delete the corresponding db records
$query = "DELETE FROM $t_project_file_table
$t_query = "DELETE FROM $t_project_file_table
WHERE project_id=" . db_param();
$result = db_query_bound( $query, array( (int) $p_project_id ) );
$t_result = db_query_bound( $t_query, array( (int) $p_project_id ) );
}

# Delete all cached files that are older than configured number of days.
Expand Down
4 changes: 2 additions & 2 deletions application/core/relationship_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ function relationship_get_all_src( $p_src_bug_id ) {
$t_bug_array[] = $row['destination_bug_id'];
$i++;
}
unset( $t_bug_relationship_data[$t_relationship_count] );
unset( $t_bug_relationship_data[$i] );
if( !empty( $t_bug_array ) ) {
bug_cache_array_rows( $t_bug_array );
}
Expand Down Expand Up @@ -421,7 +421,7 @@ function relationship_get_all_dest( $p_dest_bug_id ) {
$t_bug_array[] = $row['source_bug_id'];
$i++;
}
unset( $t_bug_relationship_data[$t_relationship_count] );
unset( $t_bug_relationship_data[$i] );

if( !empty( $t_bug_array ) ) {
bug_cache_array_rows( $t_bug_array );
Expand Down
52 changes: 31 additions & 21 deletions application/core/user_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function user_ensure_name_unique( $p_username ) {
# Check if the realname is a valid username (does not account for uniqueness)
# Return 0 if it is invalid, The number of matches + 1
function user_is_realname_unique( $p_username, $p_realname ) {
if( is_blank( $p_realname ) ) {
if ( is_blank( $p_realname ) ) {
# don't bother checking if realname is blank
return 1;
}
Expand All @@ -250,35 +250,40 @@ function user_is_realname_unique( $p_username, $p_realname ) {
$p_realname = trim( $p_realname );

# allow realname to match username
$t_count = 0;
if( $p_realname <> $p_username ) {
$t_duplicate_count = 0;
if ( $p_realname !== $p_username ) {
# check realname does not match an existing username
# but allow it to match the current user
$t_target_user = user_get_id_by_name( $p_username );
$t_other_user = user_get_id_by_name( $p_realname );
if( ( 0 != $t_other_user ) && ( $t_target_user != $t_other_user ) ) {
if ( ( $t_other_user !== 0 ) && ( $t_target_user !== $t_other_user ) ) {
return 0;
}

# check to see if the realname is unique
$t_user_table = db_get_table( 'user' );
$query = "SELECT id
$t_query = "SELECT id
FROM $t_user_table
WHERE realname=" . db_param();
$result = db_query_bound( $query, array( $p_realname ) );
$t_count = db_num_rows( $result );
$t_result = db_query_bound( $t_query, array( $p_realname ) );

if( $t_count > 0 ) {
$t_users = array();
while ( $t_row = db_fetch_array( $t_result ) ) {
$t_users[] = $t_row;
}
$t_duplicate_count = count( $t_users );

if ( $t_duplicate_count > 0 ) {
# set flags for non-unique realnames
if( config_get( 'differentiate_duplicates' ) ) {
for( $i = 0;$i < $t_count;$i++ ) {
$t_user_id = db_result( $result );
if ( config_get( 'differentiate_duplicates' ) ) {
for ( $i = 0; $i < $t_duplicate_count; $i++ ) {
$t_user_id = $t_users[$i]['id'];
user_set_field( $t_user_id, 'duplicate_realname', ON );
}
}
}
}
return $t_count + 1;
return $t_duplicate_count + 1;
}

# --------------------
Expand Down Expand Up @@ -606,19 +611,24 @@ function user_delete( $p_user_id ) {
user_delete_project_specific_access_levels( $p_user_id );

# unset non-unique realname flags if necessary
if( config_get( 'differentiate_duplicates' ) ) {
if ( config_get( 'differentiate_duplicates' ) ) {
$c_realname = user_get_field( $p_user_id, 'realname' );
$query = "SELECT id
$t_query = "SELECT id
FROM $t_user_table
WHERE realname=" . db_param();
$result = db_query_bound( $query, array( $c_realname ) );
$t_count = db_num_rows( $result );
$t_result = db_query_bound( $t_query, array( $c_realname ) );

if( $t_count == 2 ) {
$t_users = array();
while ( $t_row = db_fetch_array( $t_result ) ) {
$t_users[] = $t_row;
}

$t_user_count = count( $t_users );

if ( $t_user_count == 2 ) {
# unset flags if there are now only 2 unique names
for( $i = 0;$i < $t_count;$i++ ) {
$t_user_id = db_result( $result );
for ( $i = 0; $i < $t_user_count; $i++ ) {
$t_user_id = $t_users[$i]['id'];
user_set_field( $t_user_id, 'duplicate_realname', OFF );
}
}
Expand All @@ -627,9 +637,9 @@ function user_delete( $p_user_id ) {
user_clear_cache( $p_user_id );

# Remove account
$query = "DELETE FROM $t_user_table
$t_query = "DELETE FROM $t_user_table
WHERE id=" . db_param();
db_query_bound( $query, array( $c_user_id ) );
db_query_bound( $t_query, array( $c_user_id ) );

return true;
}
Expand Down
37 changes: 21 additions & 16 deletions application/services/soap/mc_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,26 +254,31 @@ function mci_filter_db_get_available_queries( $p_project_id = null, $p_user_id =
# Get the list of available queries. By sorting such that public queries are
# first, we can override any query that has the same name as a private query
# with that private one
$query = "SELECT * FROM $t_filters_table
$t_query = "SELECT * FROM $t_filters_table
WHERE (project_id='$t_project_id'
OR project_id='0')
AND name!=''
ORDER BY is_public DESC, name ASC";
$result = db_query_bound( $query, array() );
$query_count = db_num_rows( $result );

for( $i = 0;$i < $query_count;$i++ ) {
$row = db_fetch_array( $result );
if(( $row['user_id'] == $t_user_id ) || db_prepare_bool( $row['is_public'] ) ) {

$t_filter_detail = explode( '#', $row['filter_string'], 2 );
if ( !isset($t_filter_detail[1]) ) {
continue;
}
$t_filter = unserialize( $t_filter_detail[1] );
$t_filter = filter_ensure_valid_filter( $t_filter );
$row['url'] = filter_get_url( $t_filter );
$t_overall_query_arr[$row['name']] = $row;
$t_result = db_query_bound( $t_query, array() );

$t_queries = array();
while ( $t_row = db_fetch_array( $t_result ) ) {
$t_queries[] = $t_row;
}

$t_query_count = count( $t_queries );

for ( $i = 0; $i < $t_query_count; $i++ ) {
$t_query = $t_queries[$i];
if ( ( $t_query['user_id'] == $t_user_id ) || db_prepare_bool( $t_query['is_public'] ) ) {
$t_filter_detail = explode( '#', $t_query['filter_string'], 2 );
if ( !isset( $t_filter_detail[1] ) ) {
continue;
}
$t_filter = unserialize( $t_filter_detail[1] );
$t_filter = filter_ensure_valid_filter( $t_filter );
$t_query['url'] = filter_get_url( $t_filter );
$t_overall_query_arr[$t_query['name']] = $t_query;
}
}

Expand Down
Loading

0 comments on commit 393315c

Please sign in to comment.