forked from leftypol/leftypol
database.php: trim
This commit is contained in:
parent
21321306bb
commit
66cb9728b7
1 changed files with 23 additions and 23 deletions
|
@ -8,30 +8,30 @@ defined('TINYBOARD') or exit;
|
|||
|
||||
class PreparedQueryDebug {
|
||||
protected $query, $explain_query = false;
|
||||
|
||||
|
||||
public function __construct($query) {
|
||||
global $pdo, $config;
|
||||
$query = preg_replace("/[\n\t]+/", ' ', $query);
|
||||
|
||||
|
||||
$this->query = $pdo->prepare($query);
|
||||
if ($config['debug'] && $config['debug_explain'] && preg_match('/^(SELECT|INSERT|UPDATE|DELETE) /i', $query))
|
||||
$this->explain_query = $pdo->prepare("EXPLAIN $query");
|
||||
}
|
||||
public function __call($function, $args) {
|
||||
global $config, $debug;
|
||||
|
||||
|
||||
if ($config['debug'] && $function == 'execute') {
|
||||
if ($this->explain_query) {
|
||||
$this->explain_query->execute() or error(db_error($this->explain_query));
|
||||
}
|
||||
$start = microtime(true);
|
||||
}
|
||||
|
||||
|
||||
if ($this->explain_query && $function == 'bindValue')
|
||||
call_user_func_array(array($this->explain_query, $function), $args);
|
||||
|
||||
|
||||
$return = call_user_func_array(array($this->query, $function), $args);
|
||||
|
||||
|
||||
if ($config['debug'] && $function == 'execute') {
|
||||
$time = microtime(true) - $start;
|
||||
$debug['sql'][] = array(
|
||||
|
@ -42,7 +42,7 @@ class PreparedQueryDebug {
|
|||
);
|
||||
$debug['time']['db_queries'] += $time;
|
||||
}
|
||||
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
|
@ -51,16 +51,16 @@ function sql_open() {
|
|||
global $pdo, $config, $debug;
|
||||
if ($pdo)
|
||||
return true;
|
||||
|
||||
|
||||
|
||||
|
||||
if ($config['debug'])
|
||||
$start = microtime(true);
|
||||
|
||||
|
||||
if (isset($config['db']['server'][0]) && $config['db']['server'][0] == ':')
|
||||
$unix_socket = substr($config['db']['server'], 1);
|
||||
else
|
||||
$unix_socket = false;
|
||||
|
||||
|
||||
$dsn = $config['db']['type'] . ':' .
|
||||
($unix_socket ? 'unix_socket=' . $unix_socket : 'host=' . $config['db']['server']) .
|
||||
';dbname=' . $config['db']['database'];
|
||||
|
@ -70,17 +70,17 @@ function sql_open() {
|
|||
$options = array(
|
||||
PDO::ATTR_TIMEOUT => $config['db']['timeout'],
|
||||
);
|
||||
|
||||
|
||||
if ($config['db']['type'] == "mysql")
|
||||
$options[PDO::MYSQL_ATTR_USE_BUFFERED_QUERY] = true;
|
||||
|
||||
|
||||
if ($config['db']['persistent'])
|
||||
$options[PDO::ATTR_PERSISTENT] = true;
|
||||
$pdo = new PDO($dsn, $config['db']['user'], $config['db']['password'], $options);
|
||||
|
||||
|
||||
if ($config['debug'])
|
||||
$debug['time']['db_connect'] = '~' . round((microtime(true) - $start) * 1000, 2) . 'ms';
|
||||
if ($config['db']['type'] == "mysql"){
|
||||
if ($config['db']['type'] == "mysql"){
|
||||
if (mysql_version() >= 50503)
|
||||
query('SET NAMES utf8mb4') or error(db_error());
|
||||
else
|
||||
|
@ -89,11 +89,11 @@ function sql_open() {
|
|||
return $pdo;
|
||||
} catch(PDOException $e) {
|
||||
$message = $e->getMessage();
|
||||
|
||||
|
||||
// Remove any sensitive information
|
||||
$message = str_replace($config['db']['user'], '<em>hidden</em>', $message);
|
||||
$message = str_replace($config['db']['password'], '<em>hidden</em>', $message);
|
||||
|
||||
|
||||
// Print error
|
||||
error(_('Database error: ') . $message);
|
||||
}
|
||||
|
@ -107,11 +107,11 @@ function mysql_version() {
|
|||
|
||||
function prepare($query) {
|
||||
global $pdo, $debug, $config;
|
||||
|
||||
|
||||
$query = preg_replace('/``('.$config['board_regex'].')``/u', '`' . $config['db']['prefix'] . '$1`', $query);
|
||||
|
||||
|
||||
sql_open();
|
||||
|
||||
|
||||
if ($config['debug'])
|
||||
return new PreparedQueryDebug($query);
|
||||
|
||||
|
@ -120,11 +120,11 @@ function prepare($query) {
|
|||
|
||||
function query($query) {
|
||||
global $pdo, $debug, $config;
|
||||
|
||||
|
||||
$query = preg_replace('/``('.$config['board_regex'].')``/u', '`' . $config['db']['prefix'] . '$1`', $query);
|
||||
|
||||
|
||||
sql_open();
|
||||
|
||||
|
||||
if ($config['debug']) {
|
||||
if ($config['debug_explain'] && preg_match('/^(SELECT|INSERT|UPDATE|DELETE) /i', $query)) {
|
||||
$explain = $pdo->query("EXPLAIN $query") or error(db_error());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue