Skip to content

Commit

Permalink
Redirect db_query() to DbQuery class
Browse files Browse the repository at this point in the history
  • Loading branch information
cproensa authored and dregad committed Mar 5, 2018
1 parent 713b9da commit cff81ff
Showing 1 changed file with 2 additions and 88 deletions.
90 changes: 2 additions & 88 deletions core/database_api.php
Expand Up @@ -309,94 +309,8 @@ function db_query_bound() {
* @return IteratorAggregate|boolean adodb result set or false if the query failed.
*/
function db_query( $p_query, array $p_arr_parms = null, $p_limit = -1, $p_offset = -1, $p_pop_param = true ) {
global $g_queries_array, $g_db, $g_db_log_queries, $g_db_param;

$t_db_type = config_get_global( 'db_type' );

static $s_check_params;
if( $s_check_params === null ) {
$s_check_params = ( db_is_pgsql() || $t_db_type == 'odbc_mssql' || $t_db_type == 'mssqlnative' );
}

$t_start = microtime( true );

# This ensures that we don't get an error from ADOdb if $p_arr_parms == null,
# as Execute() expects either an array or false if there are no parameters -
# null actually gets treated as array( 0 => null )
if( is_null( $p_arr_parms ) ) {
$p_arr_parms = array();
}

if( !empty( $p_arr_parms ) && $s_check_params ) {
$t_params = count( $p_arr_parms );
for( $i = 0;$i < $t_params;$i++ ) {
if( $p_arr_parms[$i] === false ) {
$p_arr_parms[$i] = 0;
} elseif( $p_arr_parms[$i] === true && $t_db_type == 'mssqlnative' ) {
$p_arr_parms[$i] = 1;
}
}
}

static $s_prefix;
static $s_suffix;
if( $s_prefix === null ) {
# Determine table prefix and suffixes including trailing and leading '_'
$s_prefix = trim( config_get_global( 'db_table_prefix' ) );
$s_suffix = trim( config_get_global( 'db_table_suffix' ) );

if( !empty( $s_prefix ) && '_' != substr( $s_prefix, -1 ) ) {
$s_prefix .= '_';
}
if( !empty( $s_suffix ) && '_' != substr( $s_suffix, 0, 1 ) ) {
$s_suffix = '_' . $s_suffix;
}
}

$p_query = strtr($p_query, array(
'{' => $s_prefix,
'}' => $s_suffix,
) );

# Pushing params to safeguard the ADOdb parameter count (required for pgsql)
$g_db_param->push();

if( db_is_oracle() ) {
$p_query = db_oracle_adapt_query_syntax( $p_query, $p_arr_parms );
}

if( ( $p_limit != -1 ) || ( $p_offset != -1 ) ) {
$t_result = $g_db->SelectLimit( $p_query, $p_limit, $p_offset, $p_arr_parms );
} else {
$t_result = $g_db->Execute( $p_query, $p_arr_parms );
}

# Restore ADOdb parameter count
$g_db_param->pop();

$t_elapsed = number_format( microtime( true ) - $t_start, 4 );

if( ON == $g_db_log_queries ) {
$t_query_text = db_format_query_log_msg( $p_query, $p_arr_parms );
log_event( LOG_DATABASE, array( $t_query_text, $t_elapsed ) );
} else {
# If not logging the queries the actual text is not needed
$t_query_text = '';
}
array_push( $g_queries_array, array( $t_query_text, $t_elapsed ) );

# Restore param stack: only pop if asked to AND the query has params
if( $p_pop_param && !empty( $p_arr_parms ) ) {
$g_db_param->pop();
}

if( !$t_result ) {
db_error( $p_query );
trigger_error( ERROR_DB_QUERY_FAILED, ERROR );
return false;
} else {
return $t_result;
}
# Use DbQuery class to execute the query
return DbQuery::compat_db_query( $p_query, $p_arr_parms, $p_limit, $p_offset, $p_pop_param );
}

/**
Expand Down

0 comments on commit cff81ff

Please sign in to comment.