Show
Ignore:
Timestamp:
07/21/07 02:40:43 (5 years ago)
Author:
benoitg
Message:
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/wifidog-auth/wifidog/classes/AbstractDb.php

    r1249 r1262  
    192192    } 
    193193 
     194        /** 
     195     * Execute an SQL query and returns the RAW postgresql result handle or throws an error 
     196     * This is NOT for general use, as it breaks abstraction 
     197     * @param $sql SQL query to execute 
     198     * @param $resultSet The postgresql result handle 
     199     * @param $debug When set to true the function will spit out debug informations 
     200     * @return TRUE indicated the query went fine, FALSE something went wrong 
     201     */ 
     202    function execSqlRaw($sql, & $resultSet, $debug = false) 
     203    { 
     204        // Get a connection handle 
     205        $connection = $this->connect(NULL); 
     206 
     207        // In debug mode spit out the SQL query 
     208        if ($debug == TRUE) 
     209        { 
     210            // Header 
     211            echo "<hr/><p/>execSql() : "._("SQL Query")."<br/>\n<pre>{$sql}</pre></p>\n"; 
     212 
     213            // Prepend EXPLAIN statement to the SQL query 
     214            $result = @pg_query($connection, "EXPLAIN ".$sql); 
     215            if($result) { 
     216                echo "<p>"._("Query plan")." :<br/>\n"; 
     217                $plan_array = pg_fetch_all($result); 
     218 
     219                foreach ($plan_array as $plan_line) 
     220                echo $plan_line['QUERY PLAN']."<br/>\n"; 
     221                echo "</p>\n"; 
     222            } 
     223 
     224        } 
     225 
     226        // Start the clockwatch 
     227        $sql_starttime = microtime(); 
     228        $result = @pg_query($connection, $sql); 
     229        $sql_endtime = microtime(); 
     230 
     231        $sql_timetaken = $this->logQueries($sql, 'SELECT', $sql_starttime, $sql_endtime); 
     232 
     233        if ($debug == TRUE) 
     234        echo "<p>".sprintf(_("Elapsed time for query execution : %.6f second(s)"), $sql_timetaken)."</p>\n"; 
     235 
     236        if ($result == FALSE) 
     237        { 
     238            echo "<p>execSql() : "._("An error occured while executing the following SQL query")." :<br>{$sql}</p>"; 
     239            echo "<p>"._("Error message")." : <br/>".pg_last_error($connection)."</p>"; 
     240            echo "<p>"._("Backtrace:")."</p>"; 
     241            echo "<pre>"; 
     242            $btArray = debug_backtrace(); 
     243            foreach($btArray as $index=>$bt) { 
     244                printf("#%d %s(%d): %s%s%s()\n", $index, $bt['file'], $bt['line'], $bt['class'], $bt['type'], $bt['function']); 
     245            } 
     246            echo "</pre>"; 
     247            $resultSet = NULL; 
     248            $return_value = FALSE; 
     249        } 
     250        else 
     251        if (pg_num_rows($result) == 0) 
     252        { 
     253            $resultSet = NULL; 
     254            $return_value = TRUE; 
     255        } 
     256        else 
     257        { 
     258            $resultSet = $result; 
     259            $return_value = TRUE; 
     260        } 
     261        return $return_value; 
     262    } 
     263     
    194264    /* Logs a sql query for profiling purposes */ 
    195265    function logQueries($sql, $type, $sql_starttime, $sql_endtime)