Ejemplo n.º 1
0
function do_sql($query)
{
    global $SID;
    $dbh = $SID['dbh'];
    // do some cleanup and input checking
    $query = trim($query);
    // trim leading and trailing spaces
    $query_list = split_queries($query);
    // 2.1 - instead of explode
    $qcount = count($query_list);
    $SID['query_start_time'] = microtime(TRUE);
    $stmt_count = 0;
    $select_row_count = 0;
    $affected_row_count = 0;
    $select_qcount = 0;
    $non_select_qcount = 0;
    foreach ($query_list as $k => $query) {
        $query = strip_sql_comments($query);
        $qlen = strlen($query);
        if ($qlen < 1) {
            continue;
        } else {
            $stmt_count++;
        }
        // debug -- display the query
        // message('%d: [%s]', $stmt_count, $query);
        if (is_select($query)) {
            // select statement
            try {
                $sth = $dbh->prepare($query);
                if ($sth) {
                    $sth->execute();
                    $rc = select_results($sth, $qcount, $k + 1);
                    if ($rc) {
                        $select_row_count += $rc;
                    }
                    if (DBENGINE == 'pgsql') {
                        // count affected rows for PostgreSQL
                        // note: must subtract $rc (returned rows) because
                        // PDO/pgsql incorrectly counts rows returned by SELECT statements as affected rows
                        $affected_row_count += $sth->rowCount() - $rc;
                    }
                }
            } catch (PDOException $e) {
                // 2014-03-27 bw -- MySQL is putting out some bogus "general error" errors
                if ($e->getCode() != 'HY000') {
                    error_message('query #%d: %s', $k + 1, $e->getMessage());
                }
            }
            $select_qcount++;
        } else {
            // non-select statement
            try {
                $sth = $dbh->prepare($query);
                if ($sth) {
                    $sth->execute();
                    $affected_row_count += $sth->rowCount();
                }
            } catch (PDOException $e) {
                error_message('query #%d: %s', $k + 1, $e->getMessage());
            }
            $non_select_qcount++;
        }
    }
    // report statistics on results
    $elapsed_time = microtime(TRUE) - $SID['query_start_time'];
    $messages = array();
    if ($stmt_count > 1) {
        array_push($messages, sprintf('%s queries performed', number_format($stmt_count)));
    }
    if ($affected_row_count) {
        array_push($messages, sprintf('%s rows affected', number_format($affected_row_count)));
    }
    if ($select_row_count) {
        array_push($messages, sprintf('%s rows returned', number_format($select_row_count)));
    }
    array_push($messages, sprintf('elapsed time: %s milliseconds', number_format($elapsed_time * 1000, 2)));
    message(join('; ', $messages) . '.');
}
Ejemplo n.º 2
0
function do_sql($query)
{
    global $SID;
    $dbh = $SID['dbh'];
    // do some cleanup and input checking
    $query = trim($query);
    // trim leading and trailing spaces
    $query_list = split_queries($query);
    // 2.1 - instead of explode
    $qcount = count($query_list);
    $SID['query_start_time'] = microtime(TRUE);
    $stmt_count = 0;
    $select_row_count = 0;
    $non_select_row_count = 0;
    $select_qcount = 0;
    $non_select_qcount = 0;
    foreach ($query_list as $k => $query) {
        $query = strip_sql_comments($query);
        $qlen = strlen($query);
        if ($qlen < 1) {
            continue;
        } else {
            $stmt_count++;
        }
        // debug -- display the query
        // message('%d: [%s]', $stmt_count, $query);
        if (is_select($query)) {
            // select statement
            try {
                $sth = $dbh->prepare($query);
                if ($sth) {
                    $sth->execute();
                    $rc = select_results($sth, $qcount, $k + 1);
                    if ($rc) {
                        $select_row_count += $rc;
                    }
                }
            } catch (PDOException $e) {
                error_message('query #%d: %s', $k + 1, $e->getMessage());
            }
            $select_qcount++;
        } else {
            // non-select statement
            try {
                $sth = $dbh->prepare($query);
                if ($sth) {
                    $sth->execute();
                    $non_select_row_count += $sth->rowCount();
                }
            } catch (PDOException $e) {
                error_message('query #%d: %s', $k + 1, $e->getMessage());
            }
            $non_select_qcount++;
        }
    }
    // report statistics on results
    $elapsed_time = microtime(TRUE) - $SID['query_start_time'];
    $messages = array();
    if ($stmt_count > 1) {
        $qstr = 'queries';
        array_push($messages, sprintf('%s %s performed', number_format($stmt_count), $qstr));
    } else {
        $qstr = 'query';
    }
    if ($non_select_row_count) {
        array_push($messages, sprintf('%d non-select %s affected %s rows', $non_select_qcount, $qstr, number_format($non_select_row_count)));
    }
    if ($select_row_count) {
        array_push($messages, sprintf('%d select %s returned %s rows', $select_qcount, $qstr, number_format($select_row_count)));
    }
    array_push($messages, sprintf('elapsed time: %s milliseconds', number_format($elapsed_time * 1000, 2)));
    message(join('; ', $messages) . '.');
}