aboutsummaryrefslogtreecommitdiff
path: root/ratatoeskr/sys/db.php
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2020-10-05 21:44:27 +0200
committerLaria Carolin Chabowski <laria@laria.me>2020-10-05 21:44:27 +0200
commit01842f99b65b06d2647470c3b867719e72dabde7 (patch)
tree6ee373d5df46cf1097b06fa37bcfb0dc9b09d811 /ratatoeskr/sys/db.php
parent5e347e4efaa81c2108256dc927208cd55dc10baa (diff)
downloadratatoeskr-cms-01842f99b65b06d2647470c3b867719e72dabde7.tar.gz
ratatoeskr-cms-01842f99b65b06d2647470c3b867719e72dabde7.tar.bz2
ratatoeskr-cms-01842f99b65b06d2647470c3b867719e72dabde7.zip
Introduce some new database wrappers
This will allow us to avoid using globals and use the autoloader in the future.
Diffstat (limited to 'ratatoeskr/sys/db.php')
-rw-r--r--ratatoeskr/sys/db.php119
1 files changed, 47 insertions, 72 deletions
diff --git a/ratatoeskr/sys/db.php b/ratatoeskr/sys/db.php
index 17a8b8a..a2ccc13 100644
--- a/ratatoeskr/sys/db.php
+++ b/ratatoeskr/sys/db.php
@@ -10,133 +10,108 @@
* See "ratatoeskr/licenses/ratatoeskr" for more information.
*/
+use r7r\cms\sys\Database;
+use r7r\cms\sys\Env;
+use r7r\cms\sys\DbTransaction;
+
if (!defined("SETUP")) {
require_once(dirname(__FILE__) . "/../config.php");
}
require_once(dirname(__FILE__) . "/utils.php");
+// The global database connection.
+// It's usage is deprecated, use the Database object supplied by Env::database() instead.
+/** @var PDO|null $db_con */
$db_con = null;
-/*
- * Function: db_connect
+/**
+ * Establish the global connection to the MySQL database.
+ * This sets the global {@see $db_con}.
*
- * Establish a connection to the MySQL database.
+ * @deprecated Use the {@see Database} object supplied by {@see Env::database()} instead.
*/
-function db_connect()
+function db_connect(): void
{
- global $config;
global $db_con;
- $db_con = new PDO(
- "mysql:host=" . $config["mysql"]["server"] . ";dbname=" . $config["mysql"]["db"] . ";charset=utf8",
- $config["mysql"]["user"],
- $config["mysql"]["passwd"],
- [
- PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
- ]
- );
- $db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ $db_con = Env::getGlobal()->database()->getPdo();
}
-/*
- * Function: sub_prefix
- *
+/**
* Substitutes "PREFIX_" in the input string with the prefix from the config.
+ *
+ * @param mixed|string $q
+ * @return string
+ * @deprecated Use {@see Database::subPrefix()} instead.
*/
-function sub_prefix($q)
+function sub_prefix($q): string
{
- global $config;
- return str_replace("PREFIX_", $config["mysql"]["prefix"], $q);
+ return Env::getGlobal()->database()->subPrefix((string)$q);
}
-/*
- * Function: prep_stmt
- *
+/**
* Prepares a SQL statement using the global DB connection.
* This will also replace "PREFIX_" with the prefix defined in 'config.php'.
*
- * Parameters:
- * $q - The query / statement to prepare.
+ * @param mixed|string $q The query / statement to prepare.
+ * @return PDOStatement
*
- * Returns:
- * A PDOStatement object.
+ * @deprecated Use {@see Database::prepStmt()} instead.
*/
-function prep_stmt($q)
+function prep_stmt($q): PDOStatement
{
- global $db_con;
-
- return $db_con->prepare(sub_prefix($q));
+ return Env::getGlobal()->database()->prepStmt((string)$q);
}
-/*
- * Function: qdb
+/**
+ * Prepares statement (1st argument) with {@see prep_stmt()} and executes it with the remaining arguments.
*
- * Prepares statement (1st argument) with <prep_stmt> and executes it with the remaining arguments.
+ * @param mixed ...$args
+ * @return PDOStatement
*
- * Returns:
- * A PDOStatement object.
+ * @deprecated Use {@see Database::query()} instead.
*/
-function qdb()
+function qdb(...$args): PDOStatement
{
- $args = func_get_args();
if (count($args) < 1) {
throw new InvalidArgumentException("qdb needs at least 1 argument");
}
- $stmt = prep_stmt($args[0]);
- $stmt->execute(array_slice($args, 1));
- return $stmt;
+ return Env::getGlobal()->database()->query((string)$args[0], ...array_slice($args, 1));
}
-/*
- * Class: Transaction
- *
+/**
* Makes using transactions easier.
+ *
+ * @deprecated Use {@see DbTransaction} instead.
*/
class Transaction
{
- public $startedhere;
+ /** @var DbTransaction */
+ private $tx;
- /*
- * Constructor: __construct
- *
+ /**
* Start a new transaction.
*/
public function __construct()
{
- global $db_con;
- $this->startedhere = !($db_con->inTransaction());
- if ($this->startedhere) {
- $db_con->beginTransaction();
- }
+ $this->tx = new DbTransaction(Env::getGlobal()->database());
}
- /*
- * Function: commit
- *
+ /**
* Commit the transaction.
*/
- public function commit()
+ public function commit(): void
{
- global $db_con;
-
- if ($this->startedhere) {
- $db_con->commit();
- }
+ $this->tx->commit();
}
- /*
- * Function: rollback
- *
- * Toll the transaction back.
+ /**
+ * Roll the transaction back.
*/
- public function rollback()
+ public function rollback(): void
{
- global $db_con;
-
- if ($this->startedhere) {
- $db_con->rollBack();
- }
+ $this->tx->rollback();
}
}