Class it_dbi:
/**
* Semi-internal: send a raw SQL query and return mysql result value
* @param $query complete SQL query string
* @return MySQL result which is false for errors. May die on error if safety is big enough
*/
function query($query, $p = array())
{
$p += $this->_p;
$start = gettimeofday(true);
if (($writing = !it::match('^(EXPLAIN|SELECT|SHOW)', $query, array('utf8' => false))))
{
if ($p['server_update'])
{
debug("switching to update server \"{$p['server_update']}\"", 5);
$this->_p['server'] = $p['server'] = $p['server_update'];
unset($this->_p['server_update'], $p['server_update'], $this->_link);
}
else if ($p['server'] == "localhost" && $p['db'] == $GLOBALS['ULTRADB'] && !it::match('^test_', $GLOBALS['ULTRADB']) && preg_grep('/replicate-do/', (array)@it::file($GLOBALS['ULTRAHOME'] . "/etc/my.cnf")))
if (($t = @it::file($GLOBALS['ULTRAHOME'] . "/doc/machines.txt")) && preg_grep("/^" . gethostname() . "/", array_slice($t, 2)))
it::error("local mysql write on a replication slave machine?");
}
$this->_connect($p); # must be called after update server switching code
debug("{$p['user']}@{$p['server']}:{$p['db']}" . '.' . get_class($this) . "::query(\"$query\")", 4);
if (!($result = $this->_query($query, $p)))
{
if ($result === null || !$p['safety'])
return false;
$this->_fatal("query() failed", $query);
}
else if (it::match('^(CREATE|ALTER|DROP) ', $query, array('utf8' => false)))
{
# Purge cache for schema changes (after modifying table)
$dbid = "{$p['user']}@{$p['server']}:{$p['db']}";
static::_state_purgeshared($dbid);
}
if ($writing && $this->_p['throttle_writes'])
{
it::log('debug', 'dbi-throttle', round(1000000 * (gettimeofday(true) - $start) * $this->_p['throttle_writes']));
usleep(round(1000000 * (gettimeofday(true) - $start) * $this->_p['throttle_writes']));
}
$msec = round(1000 * (gettimeofday(true) - $start));
$slow = $msec >= 2000;
if ($GLOBALS['debug_sqllog'] || $GLOBALS['debug_sqltrace'] || $slow)
{
$backtrace = (EDC('sqltrace') || $slow) ? it_debug::backtrace(1) : null;
$truncquery = strlen($query) > 1000 ? mb_substr($query, 0, 1000) . '...' : $query;
it::log('sqllog', "$msec\t$truncquery\t$backtrace\t" . $this->_p['server'] . ($slow ? "\tSLOW" : ""));
$this->_sqllog[] = array(
'time' => $msec,
'query' => $query,
) + ($backtrace ? array('backtrace' => $backtrace) : array());
}
return $result;
}