function _connect() { global $_DB_DATAOBJECT; $sum = $this->_getDbDsnMD5(); if (!empty($_DB_DATAOBJECT['CONNECTIONS'][$sum]) && !PEAR::isError($_DB_DATAOBJECT['CONNECTIONS'][$sum])) { $exists = true; } else { $exists = false; } // @fixme horrible evil hack! // // In multisite configuration we don't want to keep around a separate // connection for every database; we could end up with thousands of // connections open per thread. In an ideal world we might keep // a connection per server and select different databases, but that'd // be reliant on having the same db username/pass as well. // // MySQL connections are cheap enough we're going to try just // closing out the old connection and reopening when we encounter // a new DSN. // // WARNING WARNING if we end up actually using multiple DBs at a time // we'll need some fancier logic here. if (!$exists && !empty($_DB_DATAOBJECT['CONNECTIONS']) && php_sapi_name() == 'cli') { foreach ($_DB_DATAOBJECT['CONNECTIONS'] as $index => $conn) { if (!empty($conn)) { $conn->disconnect(); } unset($_DB_DATAOBJECT['CONNECTIONS'][$index]); } } $result = parent::_connect(); if ($result && !$exists) { $DB =& $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; if (common_config('db', 'type') == 'mysql' && common_config('db', 'utf8')) { $conn = $DB->connection; if (!empty($conn)) { if ($DB instanceof DB_mysqli) { mysqli_set_charset($conn, 'utf8'); } else { if ($DB instanceof DB_mysql) { mysql_set_charset('utf8', $conn); } } } } // Needed to make timestamp values usefully comparable. if (common_config('db', 'type') == 'mysql') { parent::_query("set time_zone='+0:00'"); } } return $result; }
/** * sends query to database - this is the private one that must work * - internal functions use this rather than $this->query() * * Overridden to do logging. * * @param string $string * @access private * @return mixed none or PEAR_Error */ function _query($string) { if (common_config('db', 'annotate_queries')) { $string = $this->annotateQuery($string); } $start = microtime(true); $result = parent::_query($string); $delta = microtime(true) - $start; $limit = common_config('db', 'log_slow_queries'); if ($limit > 0 && $delta >= $limit || common_config('db', 'log_queries')) { $clean = $this->sanitizeQuery($string); common_log(LOG_DEBUG, sprintf("DB query (%0.3fs): %s", $delta, $clean)); } return $result; }