This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/libraries/DBv2.php

113 lines
2.9 KiB
PHP
Raw Normal View History

2016-02-25 16:06:29 +00:00
<?php
/**
* Holds the database wrapper (v2).
2016-03-08 23:07:58 +00:00
*
2016-02-25 16:06:29 +00:00
* @package Sakura
*/
namespace Sakura;
use PDO;
use PDOException;
use PDOStatement;
/**
* A wrapper to make the database communication experience smoother.
2016-03-08 23:07:58 +00:00
*
2016-02-25 16:06:29 +00:00
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class DBv2
{
/**
* The container for the PDO object.
2016-03-08 23:07:58 +00:00
*
2016-02-25 16:06:29 +00:00
* @var PDO
*/
public static $db = null;
/**
* The table prefix
2016-03-08 23:07:58 +00:00
*
2016-02-25 16:06:29 +00:00
* @var string
*/
public static $prefix = '';
/**
* Open the SQL connection and creates a PDO object.
2016-03-08 23:07:58 +00:00
*
2016-02-25 16:06:29 +00:00
* @param string $server A PDO driver.
* @param array $dsnParts An array consisting out of DSN string parts.
* @param string $username The username used to authenticate with the SQL server.
* @param string $password The password for the same purpose.
* @param array $options Additional PDO options.
*/
public static function open($server, $dsnParts, $username = null, $password = null, $prefix = '', $options = [])
{
// Check if the selected driver is available
if (!in_array($server, PDO::getAvailableDrivers())) {
trigger_error('A driver for the selected SQL server wasn\'t found!', E_USER_ERROR);
return;
}
// Set the table prefix
self::$prefix = $prefix;
// Create start of the DSN
$dsn = "{$server}:";
// Append the parts
foreach ($dsnParts as $name => $value) {
$dsn .= "{$name}={$value};";
}
try {
// Connect to SQL server using PDO
self::$db = new PDO($dsn, $username, $password, $options);
} catch (PDOException $e) {
// Catch connection errors
trigger_error($e->getMessage(), E_USER_ERROR);
}
self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
}
/**
* Closes the PDO object.
*/
public static function close()
{
self::$db = null;
}
/**
* Get the id of the item that was last inserted into the database.
2016-03-08 23:07:58 +00:00
*
2016-02-25 16:06:29 +00:00
* @param string $name Sequence of which the last id should be returned.
2016-03-08 23:07:58 +00:00
*
2016-02-25 16:06:29 +00:00
* @return string The last inserted id.
*/
public static function lastID($name = null)
{
return self::$db->lastInsertID($name);
}
/**
* Prepares a statement for execution and returns a statement object.
2016-03-08 23:07:58 +00:00
*
2016-02-25 16:06:29 +00:00
* @param string $stmt The statement to prepare.
* @param array $opts Statement specific driver options.
2016-03-08 23:07:58 +00:00
*
2016-02-25 16:06:29 +00:00
* @return PDOStatement
*/
public static function prepare($stmt, $opts = [])
{
// Replace the table prefix
$stmt = str_replace('{prefix}', self::$prefix, $stmt);
return self::$db->prepare($stmt, $opts);
}
}