2015-05-29 19:27:45 +00:00
|
|
|
<?php
|
|
|
|
namespace Sakura;
|
|
|
|
|
2015-10-18 19:06:30 +00:00
|
|
|
/**
|
2016-02-02 21:04:15 +00:00
|
|
|
* A wrapper to make the database communication experience smoother.
|
|
|
|
*
|
2015-10-18 19:06:30 +00:00
|
|
|
* @package Sakura
|
2016-02-02 21:04:15 +00:00
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
2015-10-18 19:06:30 +00:00
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
class Database
|
|
|
|
{
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* The container for the wrapper.
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
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.
|
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
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
|
2015-09-14 20:51:23 +00:00
|
|
|
$wrapper = __NAMESPACE__ . '\DBWrapper\\' . strtolower($wrapper);
|
2015-05-29 19:27:45 +00:00
|
|
|
|
|
|
|
// Check if the class exists
|
2015-09-14 20:51:23 +00:00
|
|
|
if (!class_exists($wrapper)) {
|
2015-07-05 00:03:15 +00:00
|
|
|
trigger_error('Failed to load database wrapper', E_USER_ERROR);
|
2015-09-06 01:04:55 +00:00
|
|
|
}
|
|
|
|
|
2015-07-05 00:03:15 +00:00
|
|
|
// Initialise SQL wrapper
|
2015-09-14 20:51:23 +00:00
|
|
|
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.
|
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
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.
|
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
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.
|
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
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.
|
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
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.
|
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
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.
|
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
public static function count($table, $data = null, $prefix = null)
|
|
|
|
{
|
|
|
|
return self::$database->count($table, $data, $prefix);
|
2015-07-05 00:03:15 +00:00
|
|
|
}
|
2015-10-19 21:25:20 +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.
|
|
|
|
*/
|
2015-10-19 21:25:20 +00:00
|
|
|
public static function lastInsertID($name = null)
|
|
|
|
{
|
|
|
|
return self::$database->lastInsertID($name);
|
|
|
|
}
|
2015-05-29 19:27:45 +00:00
|
|
|
}
|