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