move classes/db_mysql.class.php class to Gazelle\DB\Mysql

This commit is contained in:
Spine
2022-09-22 07:43:37 +00:00
parent 47b8cb0358
commit ea14a1b8c5
13 changed files with 36 additions and 39 deletions

View File

@@ -3,11 +3,11 @@
namespace Gazelle;
abstract class Base {
protected static \DB_MYSQL $db;
protected static \Gazelle\Cache $cache;
protected static DB\Mysql $db;
protected static Cache $cache;
protected static \Twig\Environment $twig;
public static function initialize(\Gazelle\Cache $cache, \DB_MYSQL $db, \Twig\Environment $twig) {
public static function initialize(Cache $cache, DB\Mysql $db, \Twig\Environment $twig) {
self::$db = $db;
self::$cache = $cache;
self::$twig = $twig;

View File

@@ -92,7 +92,7 @@ class DB extends Base {
if (self::$db->affected_rows() == 0) {
return [false, "condition selected 0 rows"];
}
} catch (\DB_MYSQL_DuplicateKeyException $e) {
} catch (DB\Mysql_DuplicateKeyException $e) {
// do nothing, for some reason it was already deleted
}

View File

@@ -1,6 +1,6 @@
<?php
use \Gazelle\Util\Irc;
namespace Gazelle\DB;
//-----------------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////////////
@@ -15,12 +15,6 @@ turned off by setting $Escape to false in next_record or to_array.
//--------- Basic usage -------------------------------------------------------------
* Creating the object.
require(SERVER_ROOT.'/classes/mysql.class.php');
$DB = new DB_MYSQL;
-----
* Making a query
$DB->prepare_query("
@@ -41,12 +35,12 @@ $array = $DB->next_record();
* The conventional way of retrieving a row from a result set is as follows:
list($All, $Columns, $That, $You, $Select) = $DB->next_record();
[$All, $Columns, $That, $You, $Select[ = $DB->next_record();
-----
* This is how you loop over the result set:
while (list($All, $Columns, $That, $You, $Select) = $DB->next_record()) {
while ([$All, $Columns, $That, $You, $Select] = $DB->next_record()) {
echo "Do stuff with $All of the ".$Columns.$That.$You.$Select;
}
-----
@@ -101,14 +95,13 @@ set_query_id($ResultSet)
Of course, this example is contrived, but you get the point.
-------------------------------------------------------------------------------------
*///---------------------------------------------------------------------------------
class DB_MYSQL_Exception extends Exception {}
class DB_MYSQL_DuplicateKeyException extends DB_MYSQL_Exception {}
class Mysql_Exception extends \Exception {}
class Mysql_DuplicateKeyException extends Mysql_Exception {}
class DB_MYSQL {
class Mysql {
/** @var mysqli|bool */
public $LinkID = false;
/** @var mysqli_result|bool */
@@ -151,11 +144,11 @@ class DB_MYSQL {
private function halt($Msg) {
if ($this->Errno == 1062) {
throw new DB_MYSQL_DuplicateKeyException;
throw new Mysql_DuplicateKeyException;
}
global $Debug;
$Debug->saveCase("MySQL: error({$this->Errno}) {$this->Error} query=[$this->PreparedQuery]");
throw new DB_MYSQL_Exception($Msg);
throw new Mysql_Exception($Msg);
}
public function connect() {
@@ -191,8 +184,8 @@ class DB_MYSQL {
* Prepares an SQL statement for execution with data.
*
* Normally, you'll most likely just want to be using
* DB_MYSQL::prepared_query to call both DB_MYSQL::prepare
* and DB_MYSQL::execute for one-off queries, you can use
* Mysql::prepared_query to call both Mysql::prepare()
* and Mysql::execute() for one-off queries, you can use
* this separately in the case where you plan to be running
* this query repeatedly while just changing the bound
* parameters (such as if doing a bulk update or the like).
@@ -253,7 +246,7 @@ class DB_MYSQL {
return $Statement->get_result();
} catch (\mysqli_sql_exception $e) {
if (mysqli_error($this->LinkID) == 1062) {
throw new DB_MYSQL_DuplicateKeyException;
throw new Mysql_DuplicateKeyException;
}
}
};
@@ -265,7 +258,7 @@ class DB_MYSQL {
/**
* Prepare and execute a prepared query returning the result set.
*
* Utility function that wraps DB_MYSQL::prepare and DB_MYSQL::execute
* Utility function that wraps Mysql::prepare() and Mysql::execute()
* as most times, the query is going to be one-off and this will save
* on keystrokes. If you do plan to be executing a prepared query
* multiple times with different bound parameters, you'll want to call

View File

@@ -12,7 +12,7 @@ class Debug {
protected const MAX_MEMORY = 80 * 1024 * 1024; //Maximum memory used per pageload
protected static Cache $cache;
protected static \DB_MYSQL $db;
protected static DB\Mysql $db;
protected static int $caseCount = 0;
protected static array $Errors = [];
@@ -23,7 +23,7 @@ class Debug {
protected static float $startTime;
protected static $cpuTime = false;
public function __construct(\Gazelle\Cache $cache, \DB_MYSQL $db) {
public function __construct(\Gazelle\Cache $cache, DB\Mysql $db) {
if (self::$cpuTime === false) {
$r = getrusage();
self::$cpuTime = $r['ru_utime.tv_sec'] * 1000000 + $r['ru_utime.tv_usec'];

View File

@@ -51,7 +51,7 @@ class SiteOption extends \Gazelle\Base {
VALUES (?, ?, ?)
', $name, $value, $comment
);
} catch (\DB_MYSQL_DuplicateKeyException $e) {
} catch (\Gazelle\DB\Mysql_DuplicateKeyException $e) {
return null;
}
self::$cache->cache_value(sprintf(self::CACHE_KEY, $name), $value);

View File

@@ -289,7 +289,7 @@ class Bonus extends \Gazelle\BaseUser {
VALUES (?, (SELECT ID FROM user_attr WHERE Name = ?))
", $this->user->id(), 'feature-seedbox'
);
} catch (\DB_MYSQL_DuplicateKeyException $e) {
} catch (\Gazelle\DB\Mysql_DuplicateKeyException $e) {
// no point in buying a second time
self::$db->rollback();
return false;

View File

@@ -190,7 +190,7 @@ class Seedbox extends \Gazelle\BaseUser {
AND user_seedbox_id = ?
", mb_substr($name, 0, 100), $this->user->id(), $this->hashid->decode($seedbox['id'])[0]
);
} catch (\DB_MYSQL_DuplicateKeyException $e) {
} catch (\Gazelle\DB\Mysql_DuplicateKeyException $e) {
// do nothing
} finally {
$n += self::$db->affected_rows();

View File

@@ -182,9 +182,7 @@ class Wiki extends BaseObject {
/**
* Add an alias to an existing article
*
* @param string $alias
* @param int $userId user id of the person adding the alias
* @throws \DB_MYSQL_DuplicateKeyException if alias already exists on another article
* @throws DB\Mysql_DuplicateKeyException if alias already exists on another article
*/
public function addAlias(string $alias, int $userId): int {
self::$db->prepared_query("

View File

@@ -179,7 +179,7 @@ if (!file_exists($file)) {
try {
require_once($file);
}
catch (\DB_MYSQL_Exception $e) {
catch (Gazelle\DB\Mysql_Exception $e) {
if (DEBUG_MODE || (isset($Viewer) && $Viewer->permitted('site_debug'))) {
echo $Twig->render('error-db.twig', [
'message' => $e->getMessage(),
@@ -201,17 +201,17 @@ if ($Router->hasRoutes()) {
/** @noinspection PhpIncludeInspection */
require_once($Router->getRoute($action));
}
catch (\Gazelle\Exception\RouterException $exception) {
catch (Gazelle\Exception\RouterException $exception) {
error(404);
}
catch (\Gazelle\Exception\InvalidAccessException $exception) {
catch (Gazelle\Exception\InvalidAccessException $exception) {
error(403);
}
catch (\DB_MYSQL_Exception $e) {
catch (Gazelle\DB\Mysql_Exception $e) {
$Debug->saveError($e);
error("That was not supposed to happen, please send a Staff Message to \"Staff\" for investigation.");
}
catch (\Exception $e) {
catch (Exception $e) {
$Debug->saveError($e);
}
}

View File

@@ -11,7 +11,7 @@ if (!defined('SITE_NAME')) {
}
$Cache = new Gazelle\Cache;
$DB = new DB_MYSQL(SQLDB, SQLLOGIN, SQLPASS, SQLHOST, SQLPORT, SQLSOCK);
$DB = new Gazelle\DB\Mysql(SQLDB, SQLLOGIN, SQLPASS, SQLHOST, SQLPORT, SQLSOCK);
$Debug = new Gazelle\Debug($Cache, $DB);
$Debug->setStartTime($now)
->handle_errors()

View File

@@ -48,6 +48,7 @@ if (isset($_GET['mode']) && $_GET['mode'] === 'userrank') {
'composer_version' => $info->composerVersion(),
'package' => $info->composerPackages(),
'phinx' => $info->phinx(),
'mysql_version' => $DB->scalar('SELECT @@version'),
'no_pk' => $info->tablesWithoutPK(),
]);
}

View File

@@ -11,7 +11,7 @@ if (!$article->editable($Viewer)) {
try {
$article->addAlias(trim($_POST['alias']), $Viewer->id());
} catch (DB_MYSQL_DuplicateKeyException $e) {
} catch (Gazelle\DB\Mysql_DuplicateKeyException $e) {
error('The alias you attempted to add is already assigned to an article.');
}

View File

@@ -48,11 +48,16 @@ div#phpinfo hr {width: 934px; background-color: #ccc; border: 0; height: 1px;}
<h3>PHP</h3>
<div class="box pad">
PHP Version: {{ php_version }} <br />
Version: {{ php_version }} <br />
<a onclick="toggle_display('phpinfo')" href='javascript:void(0)'>Toggle PHP Info</a><br />
<div id="phpinfo">{{ phpinfo|raw }}</div>
</div>
<h3>Mysql</h3>
<div class="box pad">
Version: {{ mysql_version }}
</div>
<h3>Git</h3>
<div class="box pad">
<span style="width: 150px; display: inline-block;">Branch:</span> {{ git_branch }}<br />