mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-21 07:35:34 +08:00
update to 2.5.0-b3 (build 410)
This commit is contained in:
94
src/ZM/MySQL/MySQLConnection.php
Normal file
94
src/ZM/MySQL/MySQLConnection.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php /** @noinspection PhpComposerExtensionStubsInspection */
|
||||
|
||||
|
||||
namespace ZM\MySQL;
|
||||
|
||||
|
||||
use Doctrine\DBAL\Driver\Connection;
|
||||
use Doctrine\DBAL\ParameterType;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use PDOStatement;
|
||||
use Swoole\Database\PDOProxy;
|
||||
use Swoole\Database\PDOStatementProxy;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Exception\DbException;
|
||||
use ZM\Store\MySQL\SqlPoolStorage;
|
||||
|
||||
class MySQLConnection implements Connection
|
||||
{
|
||||
/** @var PDO|PDOProxy */
|
||||
private $conn;
|
||||
|
||||
public function __construct() {
|
||||
Console::info("Constructing...");
|
||||
$this->conn = SqlPoolStorage::$sql_pool->getConnection();
|
||||
}
|
||||
|
||||
public function prepare($sql, $options = []) {
|
||||
try {
|
||||
$statement = $this->conn->prepare($sql, $options);
|
||||
assert(($statement instanceof PDOStatementProxy) || ($statement instanceof PDOStatement));
|
||||
} catch (PDOException $exception) {
|
||||
throw new DbException($exception->getMessage(), $exception->getCode(), $exception);
|
||||
}
|
||||
return new MySQLStatement($statement);
|
||||
}
|
||||
|
||||
public function query(...$args) {
|
||||
try {
|
||||
$statement = $this->conn->query(...$args);
|
||||
assert(($statement instanceof PDOStatementProxy) || ($statement instanceof PDOStatement));
|
||||
} catch (PDOException $exception) {
|
||||
throw new DbException($exception->getMessage(), $exception->getCode(), $exception);
|
||||
}
|
||||
return new MySQLStatement($statement);
|
||||
}
|
||||
|
||||
public function quote($value, $type = ParameterType::STRING) {
|
||||
return $this->conn->quote($value, $type);
|
||||
}
|
||||
|
||||
public function exec($sql) {
|
||||
try {
|
||||
$statement = $this->conn->exec($sql);
|
||||
assert($statement !== false);
|
||||
return $statement;
|
||||
} catch (PDOException $exception) {
|
||||
throw new DbException($exception->getMessage(), $exception->getCode(), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function lastInsertId($name = null) {
|
||||
try {
|
||||
return $name === null ? $this->conn->lastInsertId() : $this->conn->lastInsertId($name);
|
||||
} catch (PDOException $exception) {
|
||||
throw new DbException($exception->getMessage(), $exception->getCode(), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function beginTransaction() {
|
||||
return $this->conn->beginTransaction();
|
||||
}
|
||||
|
||||
public function commit() {
|
||||
return $this->conn->commit();
|
||||
}
|
||||
|
||||
public function rollBack() {
|
||||
return $this->conn->rollBack();
|
||||
}
|
||||
|
||||
public function errorCode() {
|
||||
return $this->conn->errorCode();
|
||||
}
|
||||
|
||||
public function errorInfo() {
|
||||
return $this->conn->errorInfo();
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
Console::info("Destructing!!!");
|
||||
SqlPoolStorage::$sql_pool->putConnection($this->conn);
|
||||
}
|
||||
}
|
||||
40
src/ZM/MySQL/MySQLDriver.php
Normal file
40
src/ZM/MySQL/MySQLDriver.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\MySQL;
|
||||
|
||||
|
||||
use Doctrine\DBAL\Driver;
|
||||
use Doctrine\DBAL\Platforms\MySqlPlatform;
|
||||
use Doctrine\DBAL\Schema\MySqlSchemaManager;
|
||||
use ZM\Config\ZMConfig;
|
||||
use ZM\Console\Console;
|
||||
|
||||
class MySQLDriver implements Driver
|
||||
{
|
||||
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) {
|
||||
Console::info("Requiring new connection");
|
||||
return new MySQLConnection();
|
||||
}
|
||||
|
||||
public function getDatabasePlatform(): MySqlPlatform {
|
||||
return new MySqlPlatform();
|
||||
}
|
||||
|
||||
public function getSchemaManager($conn) {
|
||||
return new MySqlSchemaManager($conn);
|
||||
}
|
||||
|
||||
public function getName() {
|
||||
return 'pdo_mysql_pool';
|
||||
}
|
||||
|
||||
public function getDatabase($conn) {
|
||||
$params = ZMConfig::get("global", "mysql_config");
|
||||
|
||||
if (isset($params['dbname'])) {
|
||||
return $params['dbname'];
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
20
src/ZM/MySQL/MySQLManager.php
Normal file
20
src/ZM/MySQL/MySQLManager.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\MySQL;
|
||||
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\DBAL\DriverManager;
|
||||
use Doctrine\DBAL\Exception;
|
||||
|
||||
class MySQLManager
|
||||
{
|
||||
/**
|
||||
* @return Connection
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function getConnection() {
|
||||
return DriverManager::getConnection(["driverClass" => MySQLDriver::class]);
|
||||
}
|
||||
}
|
||||
37
src/ZM/MySQL/MySQLPool.php
Normal file
37
src/ZM/MySQL/MySQLPool.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php /** @noinspection PhpComposerExtensionStubsInspection */
|
||||
|
||||
/** @noinspection PhpReturnDocTypeMismatchInspection */
|
||||
|
||||
|
||||
namespace ZM\MySQL;
|
||||
|
||||
|
||||
use PDO;
|
||||
use Swoole\Database\PDOConfig;
|
||||
use Swoole\Database\PDOPool;
|
||||
use Swoole\Database\PDOProxy;
|
||||
|
||||
class MySQLPool extends PDOPool
|
||||
{
|
||||
private $count = 0;
|
||||
|
||||
public function __construct(PDOConfig $config, int $size = self::DEFAULT_SIZE) {
|
||||
parent::__construct($config, $size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PDO|PDOProxy|void
|
||||
*/
|
||||
public function getConnection() {
|
||||
$this->count++;
|
||||
return parent::get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PDO|PDOProxy $connection
|
||||
*/
|
||||
public function putConnection($connection) {
|
||||
$this->count--;
|
||||
parent::put($connection);
|
||||
}
|
||||
}
|
||||
78
src/ZM/MySQL/MySQLStatement.php
Normal file
78
src/ZM/MySQL/MySQLStatement.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php /** @noinspection PhpComposerExtensionStubsInspection */
|
||||
|
||||
|
||||
namespace ZM\MySQL;
|
||||
|
||||
|
||||
use Doctrine\DBAL\Driver\Statement;
|
||||
use Doctrine\DBAL\Driver\StatementIterator;
|
||||
use Doctrine\DBAL\ParameterType;
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
use Swoole\Database\PDOStatementProxy;
|
||||
|
||||
class MySQLStatement implements Statement, \IteratorAggregate
|
||||
{
|
||||
/** @var PDOStatement|PDOStatementProxy */
|
||||
private $statement;
|
||||
|
||||
public function __construct($obj) {
|
||||
$this->statement = $obj;
|
||||
}
|
||||
|
||||
public function closeCursor() {
|
||||
return $this->statement->closeCursor();
|
||||
}
|
||||
|
||||
public function columnCount() {
|
||||
return $this->statement->columnCount();
|
||||
}
|
||||
|
||||
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = []) {
|
||||
return $this->statement->setFetchMode($fetchMode, $arg2, $arg3);
|
||||
}
|
||||
|
||||
public function fetch($fetchMode = PDO::FETCH_ASSOC, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) {
|
||||
return $this->statement->fetch($fetchMode, $cursorOrientation, $cursorOffset);
|
||||
}
|
||||
|
||||
public function fetchAll($fetchMode = PDO::FETCH_ASSOC, $fetchArgument = null, $ctorArgs = null) {
|
||||
return $this->statement->fetchAll($fetchMode, $fetchArgument, $ctorArgs);
|
||||
}
|
||||
|
||||
public function fetchColumn($columnIndex = 0) {
|
||||
return $this->statement->fetchColumn($columnIndex);
|
||||
}
|
||||
|
||||
public function bindValue($param, $value, $type = ParameterType::STRING) {
|
||||
return $this->statement->bindValue($param, $value, $type);
|
||||
}
|
||||
|
||||
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null) {
|
||||
return $this->statement->bindParam($param, $variable, $type, $length);
|
||||
}
|
||||
|
||||
public function errorCode() {
|
||||
return $this->statement->errorCode();
|
||||
}
|
||||
|
||||
public function errorInfo() {
|
||||
return $this->statement->errorInfo();
|
||||
}
|
||||
|
||||
public function execute($params = null) {
|
||||
return $this->statement->execute($params);
|
||||
}
|
||||
|
||||
public function rowCount() {
|
||||
return $this->statement->rowCount();
|
||||
}
|
||||
|
||||
public function getIterator() {
|
||||
return new StatementIterator($this);
|
||||
}
|
||||
|
||||
public function current() {
|
||||
return $this->statement->current();
|
||||
}
|
||||
}
|
||||
18
src/ZM/MySQL/MySQLWrapper.php
Normal file
18
src/ZM/MySQL/MySQLWrapper.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\MySQL;
|
||||
|
||||
|
||||
class MySQLWrapper
|
||||
{
|
||||
public $connection;
|
||||
|
||||
public function __construct() {
|
||||
$this->connection = MySQLManager::getConnection();
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
$this->connection->close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user