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/Database.php

146 lines
4.9 KiB
PHP
Raw Normal View History

2015-05-29 19:27:45 +00:00
<?php
namespace Sakura;
/**
2016-02-02 21:04:15 +00:00
* A wrapper to make the database communication experience smoother.
*
* @package Sakura
2016-02-02 21:04:15 +00:00
* @author Julian van de Groep <me@flash.moe>
*/
class Database
{
2016-02-02 21:04:15 +00:00
/**
* The container for the wrapper.
*
* @var mixed
*/
public static $database;
2015-05-29 19:27:45 +00:00
2016-02-02 21:04:15 +00:00
/**
* Initialise the database wrapper.
*
* @param string $wrapper The wrapper to wrap.
*/
public static function init($wrapper)
{
2015-05-29 19:27:45 +00:00
2015-07-05 00:03:15 +00:00
// Make the wrapper class name lowercase
$wrapper = __NAMESPACE__ . '\DBWrapper\\' . strtolower($wrapper);
2015-05-29 19:27:45 +00:00
// Check if the class exists
if (!class_exists($wrapper)) {
2015-07-05 00:03:15 +00:00
trigger_error('Failed to load database wrapper', E_USER_ERROR);
}
2015-07-05 00:03:15 +00:00
// Initialise SQL wrapper
self::$database = new $wrapper;
2015-05-29 19:27:45 +00:00
}
2016-02-02 21:04:15 +00:00
/**
* Select table row(s).
*
* @param string $table The table to select data from.
* @param array $data The WHERE selectors.
* @param array $order The order in which the data is returned.
* @param array $limit The limit of what should be returned.
* @param array $group The way MySQL will group the data.
* @param bool $distinct Only return distinct values.
* @param string $column Only select from this column.
* @param string $prefix Use a different table prefix than the one from the configuration.
*
* @return \PDOStatement The PDOStatement object for this action.
*/
public static function select($table, $data = null, $order = null, $limit = null, $group = null, $distinct = false, $column = '*', $prefix = null)
{
return self::$database->select($table, $data, $order, $limit, $group, $distinct, $column, $prefix);
2015-08-21 22:07:45 +00:00
}
2016-02-02 21:04:15 +00:00
/**
* Summary of fetch
*
* @param string $table The table to select data from.
* @param bool $fetchAll Whether all result will be returned or just the first one.
* @param array $data The WHERE selectors.
* @param array $order The order in which the data is returned.
* @param array $limit The limit of what should be returned.
* @param array $group The way MySQL will group the data.
* @param bool $distinct Only return distinct values.
* @param string $column Only select from this column.
* @param string $prefix Use a different table prefix than the one from the configuration.
*
* @return array The data the database returned.
*/
public static function fetch($table, $fetchAll = true, $data = null, $order = null, $limit = null, $group = null, $distinct = false, $column = '*', $prefix = null)
{
return self::$database->fetch($table, $fetchAll, $data, $order, $limit, $group, $distinct, $column, $prefix);
2015-05-29 19:27:45 +00:00
}
2016-02-02 21:04:15 +00:00
/**
* Insert data into the database.
*
* @param string $table The table that the data will be inserted into.
* @param array $data The data that should be stored.
* @param string $prefix Use a different table prefix than the one from the configuration.
*
* @return bool Successfulness.
*/
public static function insert($table, $data, $prefix = null)
{
return self::$database->insert($table, $data, $prefix);
2015-05-29 19:27:45 +00:00
}
2016-02-02 21:04:15 +00:00
/**
* Update existing database rows.
*
* @param string $table The table that the updated data will be inserted into.
* @param array $data The data that should be stored.
* @param string $prefix Use a different table prefix than the one from the configuration.
*
* @return bool Successfulness.
*/
public static function update($table, $data, $prefix = null)
{
return self::$database->update($table, $data, $prefix);
2015-05-29 19:27:45 +00:00
}
2016-02-02 21:04:15 +00:00
/**
* Deleted data from the database.
*
* @param string $table The table that the data will be removed from.
* @param array $data The pointers to what should be deleted.
* @param string $prefix Use a different table prefix than the one from the configuration.
*
* @return bool Successfulness.
*/
public static function delete($table, $data, $prefix = null)
{
return self::$database->delete($table, $data, $prefix);
2015-05-29 19:27:45 +00:00
}
2016-02-02 21:04:15 +00:00
/**
* Return the amount of rows from a table.
*
* @param string $table Table to count in.
* @param array $data Data that should be matched.
* @param string $prefix Use a different table prefix than the one from the configuration.
*
* @return array Array containing the SQL result.
*/
public static function count($table, $data = null, $prefix = null)
{
return self::$database->count($table, $data, $prefix);
2015-07-05 00:03:15 +00:00
}
2016-02-02 21:04:15 +00:00
/**
* Get the id of the item that was last inserted into the database.
*
* @param string $name Sequence of which the last id should be returned.
*
* @return string The last inserted id.
*/
public static function lastInsertID($name = null)
{
return self::$database->lastInsertID($name);
}
2015-05-29 19:27:45 +00:00
}