mirror of
https://github.com/torrentpier/torrentpier
synced 2025-08-14 18:48:21 -07:00
Transfer announce to the php7-optimized database layer
This commit is contained in:
parent
195fe90fc8
commit
54693c00b8
4 changed files with 184 additions and 463 deletions
|
@ -27,6 +27,8 @@ define('IN_TRACKER', true);
|
|||
define('BB_ROOT', './../');
|
||||
require(BB_ROOT . 'common.php');
|
||||
|
||||
global $bb_cfg, $tr_cfg;
|
||||
|
||||
if (empty($_SERVER['HTTP_USER_AGENT'])) {
|
||||
header('Location: http://127.0.0.1', true, 301);
|
||||
die;
|
||||
|
@ -177,9 +179,7 @@ function msg_die($msg)
|
|||
}
|
||||
|
||||
$output = bencode(array(
|
||||
# 'interval' => (int) 1800,
|
||||
'min interval' => (int)1800,
|
||||
# 'peers' => (string) DUMMY_PEER,
|
||||
'failure reason' => (string)$msg,
|
||||
'warning message' => (string)$msg,
|
||||
));
|
||||
|
@ -187,9 +187,6 @@ function msg_die($msg)
|
|||
die($output);
|
||||
}
|
||||
|
||||
# $agent = !empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '-';
|
||||
# bb_log("$agent | ". str_compact($peer_id) ."\n", 'agent');
|
||||
|
||||
// Start announcer
|
||||
define('TR_ROOT', './');
|
||||
require(TR_ROOT . 'includes/init_tr.php');
|
||||
|
@ -316,7 +313,7 @@ if ($lp_info) {
|
|||
}
|
||||
|
||||
// Up/Down speed
|
||||
$speed_up = $speed_down = 0;
|
||||
$speed_up = $speed_down = $uploaded = $downloaded = 0;
|
||||
|
||||
if ($lp_info && $lp_info['update_time'] < TIMENOW) {
|
||||
if ($uploaded > $lp_info['uploaded']) {
|
||||
|
|
|
@ -27,9 +27,11 @@ if (!defined('IN_TRACKER')) {
|
|||
die(basename(__FILE__));
|
||||
}
|
||||
|
||||
global $tr_cfg;
|
||||
|
||||
// Exit if tracker is disabled
|
||||
if ($tr_cfg['off']) {
|
||||
tr_die($tr_cfg['off_reason']);
|
||||
msg_die($tr_cfg['off_reason']);
|
||||
}
|
||||
|
||||
//
|
||||
|
@ -77,419 +79,3 @@ function error_exit($msg = '')
|
|||
|
||||
tracker_exit();
|
||||
}
|
||||
|
||||
// Database
|
||||
class sql_db
|
||||
{
|
||||
public $cfg = array();
|
||||
public $cfg_keys = array('dbhost', 'dbname', 'dbuser', 'dbpasswd', 'charset', 'persist');
|
||||
public $link = null;
|
||||
public $result = null;
|
||||
public $db_server = '';
|
||||
public $selected_db = null;
|
||||
|
||||
public $locked = false;
|
||||
|
||||
public $num_queries = 0;
|
||||
public $sql_starttime = 0;
|
||||
public $sql_inittime = 0;
|
||||
public $sql_timetotal = 0;
|
||||
public $sql_last_time = 0;
|
||||
public $slow_time = 0;
|
||||
|
||||
public $dbg = array();
|
||||
public $dbg_id = 0;
|
||||
public $dbg_enabled = false;
|
||||
public $cur_query = null;
|
||||
|
||||
public $DBS = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct($cfg_values)
|
||||
{
|
||||
global $DBS;
|
||||
|
||||
$this->cfg = array_combine($this->cfg_keys, $cfg_values);
|
||||
$this->dbg_enabled = sql_dbg_enabled();
|
||||
$this->slow_time = SQL_SLOW_QUERY_TIME;
|
||||
|
||||
$this->DBS['num_queries'] =& $DBS->num_queries;
|
||||
$this->DBS['sql_inittime'] =& $DBS->sql_inittime;
|
||||
$this->DBS['sql_timetotal'] =& $DBS->sql_timetotal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize connection
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
// Connect to server
|
||||
$this->link = $this->connect();
|
||||
|
||||
// Select database
|
||||
$this->selected_db = $this->select_db();
|
||||
|
||||
// Set charset
|
||||
if ($this->cfg['charset'] && !@mysql_set_charset($this->cfg['charset'], $this->link)) {
|
||||
if (!$this->sql_query("SET NAMES {$this->cfg['charset']}")) {
|
||||
error_exit("Could not set charset {$this->cfg['charset']}");
|
||||
}
|
||||
}
|
||||
|
||||
$this->num_queries = 0;
|
||||
$this->sql_inittime = $this->sql_timetotal;
|
||||
$this->DBS['sql_inittime'] += $this->sql_inittime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open connection
|
||||
*/
|
||||
public function connect()
|
||||
{
|
||||
$this->cur_query = 'connect';
|
||||
$this->debug('start');
|
||||
|
||||
$connect_type = ($this->cfg['persist']) ? 'mysql_pconnect' : 'mysql_connect';
|
||||
|
||||
if (!$link = $connect_type($this->cfg['dbhost'], $this->cfg['dbuser'], $this->cfg['dbpasswd'])) {
|
||||
$this->log_error();
|
||||
}
|
||||
|
||||
register_shutdown_function(array(&$this, 'close'));
|
||||
|
||||
$this->debug('end');
|
||||
$this->cur_query = null;
|
||||
|
||||
# if (DBG_LOG) dbg_log(' ', 'DB-connect'. ($link ? '' : '-FAIL'));
|
||||
|
||||
if (!$link) {
|
||||
if (function_exists('dummy_exit')) {
|
||||
dummy_exit(mt_rand(1200, 2400));
|
||||
} else {
|
||||
die;
|
||||
}
|
||||
}
|
||||
|
||||
return $link;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select database
|
||||
*/
|
||||
public function select_db()
|
||||
{
|
||||
$this->cur_query = 'select db';
|
||||
$this->debug('start');
|
||||
|
||||
if (!mysql_select_db($this->cfg['dbname'], $this->link)) {
|
||||
$this->log_error();
|
||||
error_exit("Could not select database '{$this->cfg['dbname']}'");
|
||||
}
|
||||
|
||||
$this->debug('end');
|
||||
$this->cur_query = null;
|
||||
|
||||
return $this->cfg['dbname'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Base query method
|
||||
*/
|
||||
public function sql_query($query)
|
||||
{
|
||||
if (!is_resource($this->link)) {
|
||||
$this->init();
|
||||
}
|
||||
$this->cur_query = $query;
|
||||
$this->debug('start');
|
||||
|
||||
if (!$this->result = mysql_query($query, $this->link)) {
|
||||
$this->log_error();
|
||||
}
|
||||
|
||||
$this->debug('end');
|
||||
$this->cur_query = null;
|
||||
|
||||
$this->num_queries++;
|
||||
$this->DBS['num_queries']++;
|
||||
|
||||
return $this->result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute query WRAPPER (with error handling)
|
||||
*/
|
||||
public function query($query)
|
||||
{
|
||||
if (!$result = $this->sql_query($query)) {
|
||||
$this->trigger_error();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return number of rows
|
||||
*/
|
||||
public function num_rows($result = false)
|
||||
{
|
||||
$num_rows = false;
|
||||
|
||||
if ($result or $result = $this->result) {
|
||||
$num_rows = is_resource($result) ? mysql_num_rows($result) : false;
|
||||
}
|
||||
|
||||
return $num_rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return number of affected rows
|
||||
*/
|
||||
public function affected_rows()
|
||||
{
|
||||
return is_resource($this->link) ? mysql_affected_rows($this->link) : -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch current row
|
||||
*/
|
||||
public function sql_fetchrow($result)
|
||||
{
|
||||
return is_resource($result) ? mysql_fetch_assoc($result) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of sql_fetchrow()
|
||||
*/
|
||||
public function fetch_next($result)
|
||||
{
|
||||
return $this->sql_fetchrow($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch row WRAPPER (with error handling)
|
||||
*/
|
||||
public function fetch_row($query)
|
||||
{
|
||||
if (!$result = $this->sql_query($query)) {
|
||||
$this->trigger_error();
|
||||
}
|
||||
|
||||
return $this->sql_fetchrow($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch all rows
|
||||
*/
|
||||
public function sql_fetchrowset($result)
|
||||
{
|
||||
$rowset = array();
|
||||
|
||||
while ($row = mysql_fetch_assoc($result)) {
|
||||
$rowset[] = $row;
|
||||
}
|
||||
|
||||
return $rowset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch all rows WRAPPER (with error handling)
|
||||
*/
|
||||
public function fetch_rowset($query)
|
||||
{
|
||||
if (!$result = $this->sql_query($query)) {
|
||||
$this->trigger_error();
|
||||
}
|
||||
|
||||
return $this->sql_fetchrowset($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape string used in sql query
|
||||
*/
|
||||
public function escape($v, $check_type = false)
|
||||
{
|
||||
if (!is_resource($this->link)) {
|
||||
$this->init();
|
||||
}
|
||||
if (!$check_type) {
|
||||
return mysql_real_escape_string($v);
|
||||
}
|
||||
|
||||
switch (true) {
|
||||
case is_string($v):
|
||||
return "'" . mysql_real_escape_string($v) . "'";
|
||||
case is_int($v):
|
||||
return "$v";
|
||||
case is_bool($v):
|
||||
return ($v) ? '1' : '0';
|
||||
case is_float($v):
|
||||
return "'$v'";
|
||||
case is_null($v):
|
||||
return 'NULL';
|
||||
}
|
||||
// if $v has unsuitable type
|
||||
$this->trigger_error(__FUNCTION__ . ' - wrong params');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return sql error array
|
||||
*/
|
||||
public function sql_error()
|
||||
{
|
||||
$return_ary = array(
|
||||
'code' => '',
|
||||
'message' => 'not connected',
|
||||
);
|
||||
|
||||
if (is_resource($this->link)) {
|
||||
$return_ary = array(
|
||||
'code' => mysql_errno($this->link),
|
||||
'message' => mysql_error($this->link),
|
||||
);
|
||||
}
|
||||
|
||||
return $return_ary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close sql connection
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
if (is_resource($this->link)) {
|
||||
mysql_close($this->link);
|
||||
}
|
||||
|
||||
$this->link = $this->selected_db = null;
|
||||
|
||||
if (DBG_LOG) {
|
||||
dbg_log(str_repeat(' ', $this->num_queries), 'DB-num_queries-' . php_sapi_name());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get info about last query
|
||||
*/
|
||||
public function query_info()
|
||||
{
|
||||
$info = array();
|
||||
|
||||
if ($num = $this->num_rows($this->result)) {
|
||||
$info[] = "$num rows";
|
||||
}
|
||||
|
||||
if (is_resource($this->link) and $ext = mysql_info($this->link)) {
|
||||
$info[] = "$ext";
|
||||
} elseif (!$num && ($aff = $this->affected_rows($this->result) and $aff != -1)) {
|
||||
$info[] = "$aff rows";
|
||||
}
|
||||
|
||||
return join(', ', $info);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store debug info
|
||||
*/
|
||||
public function debug($mode)
|
||||
{
|
||||
if (!SQL_DEBUG) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($mode == 'start') {
|
||||
if (SQL_CALC_QUERY_TIME || DBG_LOG || SQL_LOG_SLOW_QUERIES) {
|
||||
$this->sql_starttime = utime();
|
||||
$this->sql_last_time = 0;
|
||||
}
|
||||
} elseif ($mode == 'end') {
|
||||
if (SQL_CALC_QUERY_TIME || DBG_LOG || SQL_LOG_SLOW_QUERIES) {
|
||||
$this->sql_last_time = utime() - $this->sql_starttime;
|
||||
$this->sql_timetotal += $this->sql_last_time;
|
||||
$this->DBS['sql_timetotal'] += $this->sql_last_time;
|
||||
|
||||
if (SQL_LOG_SLOW_QUERIES && $this->sql_last_time > $this->slow_time) {
|
||||
$msg = date('m-d H:i:s') . LOG_SEPR;
|
||||
$msg .= sprintf('%03d', round($this->sql_last_time));
|
||||
$msg .= LOG_SEPR . sprintf('%.1f', sys('la'));
|
||||
$msg .= LOG_SEPR . str_compact($this->cur_query);
|
||||
$msg .= LOG_SEPR . ' # ' . $this->query_info();
|
||||
$msg .= LOG_SEPR . $this->debug_find_source();
|
||||
bb_log($msg . LOG_LF, 'sql_slow_tr');
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger error
|
||||
*/
|
||||
public function trigger_error($msg = '')
|
||||
{
|
||||
if (error_reporting()) {
|
||||
if (!$msg) {
|
||||
$msg = 'DB Error';
|
||||
}
|
||||
|
||||
if (DBG_TRACKER === true) {
|
||||
$err = $this->sql_error();
|
||||
$msg .= trim(sprintf(' #%06d %s', $err['code'], $err['message']));
|
||||
} else {
|
||||
$msg .= " [" . $this->debug_find_source() . "]";
|
||||
}
|
||||
|
||||
error_exit($msg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find caller source
|
||||
*/
|
||||
public function debug_find_source()
|
||||
{
|
||||
$source = '';
|
||||
$backtrace = debug_backtrace();
|
||||
|
||||
foreach ($backtrace as $trace) {
|
||||
if ($trace['file'] !== __FILE__) {
|
||||
$source = str_replace(BB_PATH, '', $trace['file']) . '(' . $trace['line'] . ')';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Log error
|
||||
*/
|
||||
public function log_error()
|
||||
{
|
||||
if (!SQL_LOG_ERRORS) {
|
||||
return;
|
||||
}
|
||||
if (!error_reporting()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$msg = array();
|
||||
$err = $this->sql_error();
|
||||
$msg[] = str_compact(sprintf('#%06d %s', $err['code'], $err['message']));
|
||||
$msg[] = '';
|
||||
$msg[] = str_compact($this->cur_query);
|
||||
$msg[] = '';
|
||||
$msg[] = 'Source : ' . $this->debug_find_source();
|
||||
$msg[] = 'IP : ' . @$_SERVER['REMOTE_ADDR'];
|
||||
$msg[] = 'Date : ' . date('Y-m-d H:i:s');
|
||||
$msg[] = 'Agent : ' . @$_SERVER['HTTP_USER_AGENT'];
|
||||
$msg[] = 'Req_URI : ' . @$_SERVER['REQUEST_URI'];
|
||||
$msg[] = 'Referer : ' . @$_SERVER['HTTP_REFERER'];
|
||||
$msg[] = 'Method : ' . @$_SERVER['REQUEST_METHOD'];
|
||||
$msg[] = 'Request : ' . trim(print_r($_REQUEST, true)) . str_repeat('_', 78) . LOG_LF;
|
||||
$msg[] = '';
|
||||
bb_log($msg, 'sql_error_tr');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,6 +27,8 @@ define('IN_TRACKER', true);
|
|||
define('BB_ROOT', './../');
|
||||
require(BB_ROOT . 'common.php');
|
||||
|
||||
global $tr_cfg;
|
||||
|
||||
if (!$tr_cfg['scrape']) {
|
||||
msg_die('Please disable SCRAPE!');
|
||||
}
|
||||
|
|
|
@ -27,10 +27,13 @@ if (!defined('SQL_DEBUG')) {
|
|||
die(basename(__FILE__));
|
||||
}
|
||||
|
||||
/**
|
||||
* Class sql_db
|
||||
*/
|
||||
class sql_db
|
||||
{
|
||||
public $cfg = array();
|
||||
public $cfg_keys = array('dbhost', 'dbname', 'dbuser', 'dbpasswd', 'charset', 'persist');
|
||||
public $cfg = [];
|
||||
public $cfg_keys = ['dbhost', 'dbname', 'dbuser', 'dbpasswd', 'charset', 'persist'];
|
||||
private $link = null;
|
||||
public $result = null;
|
||||
public $db_server = '';
|
||||
|
@ -38,7 +41,7 @@ class sql_db
|
|||
public $inited = false;
|
||||
|
||||
public $locked = false;
|
||||
public $locks = array();
|
||||
public $locks = [];
|
||||
|
||||
public $num_queries = 0;
|
||||
public $sql_starttime = 0;
|
||||
|
@ -47,7 +50,7 @@ class sql_db
|
|||
public $cur_query_time = 0;
|
||||
public $slow_time = 0;
|
||||
|
||||
public $dbg = array();
|
||||
public $dbg = [];
|
||||
public $dbg_id = 0;
|
||||
public $dbg_enabled = false;
|
||||
public $cur_query = null;
|
||||
|
@ -56,12 +59,13 @@ class sql_db
|
|||
public $explain_hold = '';
|
||||
public $explain_out = '';
|
||||
|
||||
public $shutdown = array();
|
||||
public $shutdown = [];
|
||||
|
||||
public $DBS = array();
|
||||
public $DBS = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* sql_db constructor.
|
||||
* @param $cfg_values
|
||||
*/
|
||||
public function __construct($cfg_values)
|
||||
{
|
||||
|
@ -120,7 +124,7 @@ class sql_db
|
|||
die("Could not connect to mysql server $server");
|
||||
}
|
||||
|
||||
register_shutdown_function(array(&$this, 'close'));
|
||||
register_shutdown_function([&$this, 'close']);
|
||||
|
||||
$this->debug('stop');
|
||||
$this->cur_query = null;
|
||||
|
@ -128,6 +132,10 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Base query method
|
||||
*
|
||||
* @param $query
|
||||
*
|
||||
* @return bool|mysqli_result|null
|
||||
*/
|
||||
public function sql_query($query)
|
||||
{
|
||||
|
@ -160,6 +168,10 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Execute query WRAPPER (with error handling)
|
||||
*
|
||||
* @param $query
|
||||
*
|
||||
* @return bool|mysqli_result|null
|
||||
*/
|
||||
public function query($query)
|
||||
{
|
||||
|
@ -172,6 +184,10 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Return number of rows
|
||||
*
|
||||
* @param bool $result
|
||||
*
|
||||
* @return bool|int
|
||||
*/
|
||||
public function num_rows($result = false)
|
||||
{
|
||||
|
@ -186,6 +202,8 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Return number of affected rows
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function affected_rows()
|
||||
{
|
||||
|
@ -194,6 +212,12 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Fetch current field
|
||||
*
|
||||
* @param $field
|
||||
* @param int $rownum
|
||||
* @param int $query_id
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function sql_fetchfield($field, $rownum = -1, $query_id = 0)
|
||||
{
|
||||
|
@ -222,6 +246,13 @@ class sql_db
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mysqli_result $res
|
||||
* @param $row
|
||||
* @param int $field
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function sql_result(mysqli_result $res, $row, $field = 0)
|
||||
{
|
||||
$res->data_seek($row);
|
||||
|
@ -231,6 +262,11 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Fetch current row
|
||||
*
|
||||
* @param $result
|
||||
* @param string $field_name
|
||||
*
|
||||
* @return array|bool|null
|
||||
*/
|
||||
public function sql_fetchrow($result, $field_name = '')
|
||||
{
|
||||
|
@ -245,6 +281,9 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Alias of sql_fetchrow()
|
||||
* @param $result
|
||||
*
|
||||
* @return array|bool|null
|
||||
*/
|
||||
public function fetch_next($result)
|
||||
{
|
||||
|
@ -253,6 +292,10 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Fetch row WRAPPER (with error handling)
|
||||
* @param $query
|
||||
* @param string $field_name
|
||||
*
|
||||
* @return array|bool|null
|
||||
*/
|
||||
public function fetch_row($query, $field_name = '')
|
||||
{
|
||||
|
@ -265,10 +308,15 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Fetch all rows
|
||||
*
|
||||
* @param $result
|
||||
* @param string $field_name
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function sql_fetchrowset($result, $field_name = '')
|
||||
{
|
||||
$rowset = array();
|
||||
$rowset = [];
|
||||
|
||||
while ($row = mysqli_fetch_assoc($result)) {
|
||||
$rowset[] = ($field_name) ? $row[$field_name] : $row;
|
||||
|
@ -279,6 +327,11 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Fetch all rows WRAPPER (with error handling)
|
||||
*
|
||||
* @param $query
|
||||
* @param string $field_name
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function fetch_rowset($query, $field_name = '')
|
||||
{
|
||||
|
@ -291,6 +344,11 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Fetch all rows WRAPPER (with error handling)
|
||||
*
|
||||
* @param $query
|
||||
* @param string $field_name
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function fetch_all($query, $field_name = '')
|
||||
{
|
||||
|
@ -303,6 +361,8 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Get last inserted id after insert statement
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
public function sql_nextid()
|
||||
{
|
||||
|
@ -311,6 +371,8 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Free sql result
|
||||
*
|
||||
* @param bool $result
|
||||
*/
|
||||
public function sql_freeresult($result = false)
|
||||
{
|
||||
|
@ -325,6 +387,12 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Escape data used in sql query
|
||||
*
|
||||
* @param $v
|
||||
* @param bool $check_type
|
||||
* @param bool $dont_escape
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function escape($v, $check_type = false, $dont_escape = false)
|
||||
{
|
||||
|
@ -353,6 +421,10 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Escape string
|
||||
*
|
||||
* @param $str
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function escape_string($str)
|
||||
{
|
||||
|
@ -364,13 +436,19 @@ class sql_db
|
|||
}
|
||||
|
||||
/**
|
||||
* Build SQL statement from array (based on same method from phpBB3, idea from Ikonboard)
|
||||
*
|
||||
* Build SQL statement from array.
|
||||
* Possible $query_type values: INSERT, INSERT_SELECT, MULTI_INSERT, UPDATE, SELECT
|
||||
*
|
||||
* @param $query_type
|
||||
* @param $input_ary
|
||||
* @param bool $data_already_escaped
|
||||
* @param bool $check_data_type_in_escape
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function build_array($query_type, $input_ary, $data_already_escaped = false, $check_data_type_in_escape = true)
|
||||
{
|
||||
$fields = $values = $ary = $query = array();
|
||||
$fields = $values = $ary = $query = [];
|
||||
$dont_escape = $data_already_escaped;
|
||||
$check_type = $check_data_type_in_escape;
|
||||
|
||||
|
@ -400,7 +478,7 @@ class sql_db
|
|||
$values[] = $this->escape($val, $check_type, $dont_escape);
|
||||
}
|
||||
$ary[] = '(' . join(', ', $values) . ')';
|
||||
$values = array();
|
||||
$values = [];
|
||||
}
|
||||
$fields = join(', ', array_keys($input_ary[0]));
|
||||
$values = join(",\n", $ary);
|
||||
|
@ -420,22 +498,29 @@ class sql_db
|
|||
return "\n" . $query . "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function get_empty_sql_array()
|
||||
{
|
||||
return array(
|
||||
'SELECT' => array(),
|
||||
'select_options' => array(),
|
||||
'FROM' => array(),
|
||||
'INNER JOIN' => array(),
|
||||
'LEFT JOIN' => array(),
|
||||
'WHERE' => array(),
|
||||
'GROUP BY' => array(),
|
||||
'HAVING' => array(),
|
||||
'ORDER BY' => array(),
|
||||
'LIMIT' => array(),
|
||||
);
|
||||
return [
|
||||
'SELECT' => [],
|
||||
'select_options' => [],
|
||||
'FROM' => [],
|
||||
'INNER JOIN' => [],
|
||||
'LEFT JOIN' => [],
|
||||
'WHERE' => [],
|
||||
'GROUP BY' => [],
|
||||
'HAVING' => [],
|
||||
'ORDER BY' => [],
|
||||
'LIMIT' => [],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $sql_ary
|
||||
* @return string
|
||||
*/
|
||||
public function build_sql($sql_ary)
|
||||
{
|
||||
$sql = '';
|
||||
|
@ -478,13 +563,15 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Return sql error array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function sql_error()
|
||||
{
|
||||
if ($this->link) {
|
||||
return array('code' => mysqli_errno($this->link), 'message' => mysqli_error($this->link));
|
||||
return ['code' => mysqli_errno($this->link), 'message' => mysqli_error($this->link)];
|
||||
} else {
|
||||
return array('code' => '', 'message' => 'not connected');
|
||||
return ['code' => '', 'message' => 'not connected'];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -512,6 +599,8 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Add shutdown query
|
||||
*
|
||||
* @param $sql
|
||||
*/
|
||||
public function add_shutdown_query($sql)
|
||||
{
|
||||
|
@ -541,14 +630,15 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Lock tables
|
||||
*
|
||||
* @param $tables
|
||||
* @param string $lock_type
|
||||
*
|
||||
* @return bool|mysqli_result|null
|
||||
*/
|
||||
public function lock($tables, $lock_type = 'WRITE')
|
||||
{
|
||||
if ($this->cfg['persist']) {
|
||||
# return true;
|
||||
}
|
||||
|
||||
$tables_sql = array();
|
||||
$tables_sql = [];
|
||||
|
||||
foreach ((array)$tables as $table_name) {
|
||||
$tables_sql[] = "$table_name $lock_type";
|
||||
|
@ -562,6 +652,8 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Unlock tables
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function unlock()
|
||||
{
|
||||
|
@ -574,6 +666,11 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Obtain user level lock
|
||||
*
|
||||
* @param $name
|
||||
* @param int $timeout
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_lock($name, $timeout = 0)
|
||||
{
|
||||
|
@ -590,6 +687,10 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Obtain user level lock status
|
||||
*
|
||||
* @param $name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function release_lock($name)
|
||||
{
|
||||
|
@ -605,6 +706,10 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Release user level lock
|
||||
*
|
||||
* @param $name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function is_free_lock($name)
|
||||
{
|
||||
|
@ -615,6 +720,10 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Make per db unique lock name
|
||||
*
|
||||
* @param $name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_lock_name($name)
|
||||
{
|
||||
|
@ -627,10 +736,12 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Get info about last query
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function query_info()
|
||||
{
|
||||
$info = array();
|
||||
$info = [];
|
||||
|
||||
if ($num = $this->num_rows($this->result)) {
|
||||
$info[] = "$num rows";
|
||||
|
@ -647,6 +758,8 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Get server version
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function server_version()
|
||||
{
|
||||
|
@ -655,8 +768,11 @@ class sql_db
|
|||
}
|
||||
|
||||
/**
|
||||
* Set slow query marker for xx seconds
|
||||
* This will disable counting other queries as "slow" during this time
|
||||
* Set slow query marker for xx seconds.
|
||||
* This will disable counting other queries as "slow" during this time.
|
||||
*
|
||||
* @param int $ignoring_time
|
||||
* @param int $new_priority
|
||||
*/
|
||||
public function expect_slow_query($ignoring_time = 60, $new_priority = 10)
|
||||
{
|
||||
|
@ -672,6 +788,8 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Store debug info
|
||||
*
|
||||
* @param $mode
|
||||
*/
|
||||
public function debug($mode)
|
||||
{
|
||||
|
@ -727,6 +845,8 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Trigger error
|
||||
*
|
||||
* @param string $msg
|
||||
*/
|
||||
public function trigger_error($msg = 'DB Error')
|
||||
{
|
||||
|
@ -744,6 +864,10 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Find caller source
|
||||
*
|
||||
* @param string $mode
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function debug_find_source($mode = '')
|
||||
{
|
||||
|
@ -764,6 +888,8 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Prepare for logging
|
||||
* @param int $queries_count
|
||||
* @param string $log_file
|
||||
*/
|
||||
public function log_next_query($queries_count = 1, $log_file = 'sql_queries')
|
||||
{
|
||||
|
@ -773,11 +899,13 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Log query
|
||||
*
|
||||
* @param string $log_file
|
||||
*/
|
||||
public function log_query($log_file = 'sql_queries')
|
||||
{
|
||||
$q_time = ($this->cur_query_time >= 10) ? round($this->cur_query_time, 0) : sprintf('%.4f', $this->cur_query_time);
|
||||
$msg = array();
|
||||
$msg = [];
|
||||
$msg[] = round($this->sql_starttime);
|
||||
$msg[] = date('m-d H:i:s', $this->sql_starttime);
|
||||
$msg[] = sprintf('%-6s', $q_time);
|
||||
|
@ -794,6 +922,8 @@ class sql_db
|
|||
|
||||
/**
|
||||
* Log slow query
|
||||
*
|
||||
* @param string $log_file
|
||||
*/
|
||||
public function log_slow_query($log_file = 'sql_slow_bb')
|
||||
{
|
||||
|
@ -812,7 +942,7 @@ class sql_db
|
|||
return;
|
||||
}
|
||||
|
||||
$msg = array();
|
||||
$msg = [];
|
||||
$err = $this->sql_error();
|
||||
$msg[] = str_compact(sprintf('#%06d %s', $err['code'], $err['message']));
|
||||
$msg[] = '';
|
||||
|
@ -832,7 +962,13 @@ class sql_db
|
|||
}
|
||||
|
||||
/**
|
||||
* Explain queries (based on code from phpBB3)
|
||||
* Explain queries
|
||||
*
|
||||
* @param $mode
|
||||
* @param string $html_table
|
||||
* @param string $row
|
||||
*
|
||||
* @return bool|string
|
||||
*/
|
||||
public function explain($mode, $html_table = '', $row = '')
|
||||
{
|
||||
|
@ -843,7 +979,7 @@ class sql_db
|
|||
switch ($mode) {
|
||||
case 'start':
|
||||
$this->explain_hold = '';
|
||||
// TODO: добавить поддержку многотабличных запросов
|
||||
|
||||
if (preg_match('#UPDATE ([a-z0-9_]+).*?WHERE(.*)/#', $query, $m)) {
|
||||
$query = "SELECT * FROM $m[1] WHERE $m[2]";
|
||||
} elseif (preg_match('#DELETE FROM ([a-z0-9_]+).*?WHERE(.*)#s', $query, $m)) {
|
||||
|
@ -897,7 +1033,7 @@ class sql_db
|
|||
$this->explain_hold .= '<tr>';
|
||||
foreach (array_values($row) as $i => $val) {
|
||||
$class = !($i % 2) ? 'row1' : 'row2';
|
||||
$this->explain_hold .= '<td class="' . $class . ' gen">' . str_replace(array("{$this->selected_db}.", ',', ';'), array('', ', ', ';<br />'), $val) . '</td>';
|
||||
$this->explain_hold .= '<td class="' . $class . ' gen">' . str_replace(["{$this->selected_db}.", ',', ';'], ['', ', ', ';<br />'], $val) . '</td>';
|
||||
}
|
||||
$this->explain_hold .= '</tr>';
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue