mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-23 19:37:23 +08:00
install
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
namespace Nexus\Database;
|
||||
|
||||
class DB
|
||||
{
|
||||
private $driver;
|
||||
|
||||
private static $instance;
|
||||
|
||||
private static $queries = [];
|
||||
|
||||
private $isConnected = false;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private function __clone()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function setDriver(DBInterface $driver)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public static function getInstance()
|
||||
{
|
||||
if (self::$instance) {
|
||||
return self::$instance;
|
||||
}
|
||||
$instance = new self;
|
||||
$driver = new DBMysqli();
|
||||
$instance->setDriver($driver);
|
||||
return self::$instance = $instance;
|
||||
}
|
||||
|
||||
|
||||
public function connect($host, $username, $password, $database, $port)
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
$this->driver->connect($host, $username, $password, $database, $port);
|
||||
$this->isConnected = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function autoConnect()
|
||||
{
|
||||
if ($this->isConnected()) {
|
||||
return;
|
||||
}
|
||||
$config = config('database.mysql');
|
||||
if (!mysql_connect($config['host'], $config['username'], $config['password'], $config['database'], $config['port'])) {
|
||||
throw new DatabaseException(sprintf("mysql connect error: [%s] %s", mysql_errno(), mysql_error()));
|
||||
}
|
||||
mysql_query("SET NAMES UTF8");
|
||||
mysql_query("SET collation_connection = 'utf8_general_ci'");
|
||||
mysql_query("SET sql_mode=''");
|
||||
$this->isConnected = true;
|
||||
}
|
||||
|
||||
public function query(string $sql)
|
||||
{
|
||||
try {
|
||||
$this->autoConnect();
|
||||
return $this->driver->query($sql);
|
||||
} catch (\Exception $e) {
|
||||
do_log(sprintf("%s [%s] %s", $e->getMessage(), $sql, $e->getTraceAsString()));
|
||||
throw new DatabaseException($e->getMessage(), $sql);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return $this->driver->error();
|
||||
}
|
||||
|
||||
public function errno()
|
||||
{
|
||||
return $this->driver->errno();
|
||||
}
|
||||
|
||||
public function numRows($result)
|
||||
{
|
||||
return $this->driver->numRows($result);
|
||||
}
|
||||
|
||||
public function select_db($database)
|
||||
{
|
||||
return $this->driver->selectDb($database);
|
||||
}
|
||||
|
||||
public function fetchAssoc($result)
|
||||
{
|
||||
return $this->driver->fetchAssoc($result);
|
||||
}
|
||||
|
||||
public function fetchRow($result)
|
||||
{
|
||||
return $this->driver->fetchRow($result);
|
||||
}
|
||||
|
||||
public function fetchArray($result, $type = null)
|
||||
{
|
||||
return $this->driver->fetchArray($result, $type);
|
||||
}
|
||||
|
||||
public function affectedRows()
|
||||
{
|
||||
return $this->driver->affectedRows();
|
||||
}
|
||||
|
||||
public function escapeString(string $string)
|
||||
{
|
||||
$this->autoConnect();
|
||||
return $this->driver->escapeString($string);
|
||||
}
|
||||
|
||||
public function lastInsertId()
|
||||
{
|
||||
return $this->driver->lastInsertId();
|
||||
}
|
||||
|
||||
public function freeResult($result)
|
||||
{
|
||||
return $this->driver->freeResult($result);
|
||||
}
|
||||
|
||||
public function isConnected()
|
||||
{
|
||||
return $this->isConnected;
|
||||
}
|
||||
|
||||
public static function insert($table, $data)
|
||||
{
|
||||
if (empty($table) || empty($data) || !is_array($data)) {
|
||||
throw new DatabaseException("require table and data(array).");
|
||||
}
|
||||
$fields = array_map(function ($value) {return "`$value`";}, array_keys($data));
|
||||
$values = array_map(function ($value) {return sqlesc($value);}, array_values($data));
|
||||
$sql = sprintf("insert into `%s` (%s) values (%s)", $table, implode(', ', $fields), implode(', ', $values));
|
||||
sql_query($sql);
|
||||
return mysql_insert_id();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
namespace Nexus\Database;
|
||||
|
||||
interface DBInterface
|
||||
{
|
||||
public function connect($host, $username, $password, $database, $port);
|
||||
|
||||
public function query(string $sql);
|
||||
|
||||
public function error(): string;
|
||||
|
||||
public function errno(): int;
|
||||
|
||||
public function numRows($result): int;
|
||||
|
||||
public function selectDb($database);
|
||||
|
||||
public function fetchAssoc($result);
|
||||
|
||||
public function fetchRow($result);
|
||||
|
||||
public function fetchArray($result, $type);
|
||||
|
||||
public function affectedRows(): int;
|
||||
|
||||
public function escapeString(string $string): string;
|
||||
|
||||
public function lastInsertId(): int;
|
||||
|
||||
public function freeResult($result);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
namespace Nexus\Database;
|
||||
|
||||
class DBMysqli implements DBInterface
|
||||
{
|
||||
private $mysqli;
|
||||
|
||||
public function connect($host, $username, $password, $database, $port)
|
||||
{
|
||||
$mysqli = new \mysqli($host, $username, $password, $database, $port);
|
||||
/* check connection */
|
||||
if (mysqli_connect_errno()) {
|
||||
throw new DatabaseException(mysqli_connect_error());
|
||||
}
|
||||
/* activate reporting */
|
||||
$driver = new \mysqli_driver();
|
||||
$driver->report_mode = MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX;
|
||||
|
||||
return $this->mysqli = $mysqli;
|
||||
}
|
||||
|
||||
public function query(string $sql)
|
||||
{
|
||||
return $this->mysqli->query($sql);
|
||||
}
|
||||
|
||||
public function error(): string
|
||||
{
|
||||
return $this->mysqli->error;
|
||||
}
|
||||
|
||||
public function errno(): int
|
||||
{
|
||||
return $this->mysqli->errno;
|
||||
}
|
||||
|
||||
public function numRows($mysqliResult): int
|
||||
{
|
||||
return $mysqliResult->num_rows;
|
||||
}
|
||||
|
||||
public function selectDb($database)
|
||||
{
|
||||
return $this->mysqli->select_db($database);
|
||||
}
|
||||
|
||||
public function fetchAssoc($mysqliResult)
|
||||
{
|
||||
return $mysqliResult->fetch_assoc();
|
||||
}
|
||||
|
||||
public function fetchRow($mysqliResult)
|
||||
{
|
||||
return $mysqliResult->fetch_row();
|
||||
}
|
||||
|
||||
public function fetchArray($mysqliResult, $type)
|
||||
{
|
||||
if (is_null($type)) {
|
||||
$type = MYSQLI_BOTH;
|
||||
}
|
||||
return $mysqliResult->fetch_array($type);
|
||||
}
|
||||
|
||||
public function affectedRows(): int
|
||||
{
|
||||
return $this->mysqli->affected_rows;
|
||||
}
|
||||
|
||||
public function escapeString(string $string): string
|
||||
{
|
||||
return $this->mysqli->real_escape_string($string);
|
||||
}
|
||||
|
||||
public function lastInsertId(): int
|
||||
{
|
||||
return $this->mysqli->insert_id;
|
||||
}
|
||||
|
||||
public function freeResult($mysqliResult)
|
||||
{
|
||||
return $mysqliResult->free_result();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace Nexus\Database;
|
||||
|
||||
class DatabaseException extends \Exception
|
||||
{
|
||||
|
||||
public function __construct($message, $query = '')
|
||||
{
|
||||
parent::__construct("$message [$query]");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
use Nexus\Database\DB;
|
||||
|
||||
function mysql_connect($host, $username, $password, $database, $port)
|
||||
{
|
||||
return DB::getInstance()->connect($host, $username, $password, $database, $port);
|
||||
}
|
||||
|
||||
function mysql_errno()
|
||||
{
|
||||
return DB::getInstance()->errno();
|
||||
}
|
||||
|
||||
function mysql_error()
|
||||
{
|
||||
return DB::getInstance()->error();
|
||||
}
|
||||
|
||||
function mysql_query(string $sql)
|
||||
{
|
||||
return DB::getInstance()->query($sql);
|
||||
}
|
||||
|
||||
function mysql_select_db($database)
|
||||
{
|
||||
return DB::getInstance()->select_db($database);
|
||||
}
|
||||
|
||||
function mysql_num_rows($result)
|
||||
{
|
||||
return DB::getInstance()->numRows($result);
|
||||
}
|
||||
|
||||
function mysql_fetch_array($result, $type = null)
|
||||
{
|
||||
return DB::getInstance()->fetchArray($result, $type);
|
||||
}
|
||||
|
||||
function mysql_fetch_assoc($result)
|
||||
{
|
||||
return DB::getInstance()->fetchAssoc($result);
|
||||
}
|
||||
|
||||
function mysql_fetch_row($result)
|
||||
{
|
||||
return DB::getInstance()->fetchRow($result);
|
||||
}
|
||||
|
||||
function mysql_affected_rows()
|
||||
{
|
||||
return DB::getInstance()->affectedRows();
|
||||
}
|
||||
|
||||
function mysql_real_escape_string($string)
|
||||
{
|
||||
return DB::getInstance()->escapeString($string);
|
||||
}
|
||||
|
||||
function mysql_insert_id()
|
||||
{
|
||||
return DB::getInstance()->lastInsertId();
|
||||
}
|
||||
|
||||
function mysql_free_result($result)
|
||||
{
|
||||
return DB::getInstance()->freeResult($result);
|
||||
}
|
||||
Reference in New Issue
Block a user