update to build 417

This commit is contained in:
2021-09-01 14:14:00 +08:00
parent a13c4628f5
commit 229778ebf9
50 changed files with 1376 additions and 207 deletions

View File

@@ -21,12 +21,13 @@ class MySQLConnection implements Connection
private $conn;
public function __construct() {
Console::info("Constructing...");
Console::debug("Constructing...");
$this->conn = SqlPoolStorage::$sql_pool->getConnection();
}
public function prepare($sql, $options = []) {
try {
Console::debug("Running SQL prepare: ".$sql);
$statement = $this->conn->prepare($sql, $options);
assert(($statement instanceof PDOStatementProxy) || ($statement instanceof PDOStatement));
} catch (PDOException $exception) {
@@ -51,6 +52,7 @@ class MySQLConnection implements Connection
public function exec($sql) {
try {
Console::debug("Running SQL exec: ".$sql);
$statement = $this->conn->exec($sql);
assert($statement !== false);
return $statement;
@@ -88,7 +90,7 @@ class MySQLConnection implements Connection
}
public function __destruct() {
Console::info("Destructing");
Console::debug("Destructing");
SqlPoolStorage::$sql_pool->putConnection($this->conn);
}
}

View File

@@ -4,16 +4,16 @@
namespace ZM\MySQL;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver as DoctrineDriver;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\MySqlSchemaManager;
use ZM\Config\ZMConfig;
use ZM\Console\Console;
class MySQLDriver implements Driver
class MySQLDriver implements DoctrineDriver
{
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) {
Console::info("Requiring new connection");
Console::debug("Requiring new connection");
return new MySQLConnection();
}

View File

@@ -4,17 +4,12 @@
namespace ZM\MySQL;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception;
class MySQLManager
{
/**
* @return Connection
* @throws Exception
* @return MySQLWrapper
*/
public static function getConnection() {
return DriverManager::getConnection(["driverClass" => MySQLDriver::class]);
public static function getWrapper(): MySQLWrapper {
return new MySQLWrapper();
}
}

View File

@@ -34,4 +34,8 @@ class MySQLPool extends PDOPool
$this->count--;
parent::put($connection);
}
public function getCount() {
return $this->count;
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace ZM\MySQL;
use Doctrine\DBAL\Query\QueryBuilder;
use ZM\Exception\DbException;
class MySQLQueryBuilder extends QueryBuilder
{
private $wrapper;
public function __construct(MySQLWrapper $wrapper) {
parent::__construct($wrapper->getConnection());
$this->wrapper = $wrapper;
}
/**
* @return int|MySQLStatementWrapper
* @throws DbException
*/
public function execute() {
if ($this->getType() === self::SELECT) {
return $this->wrapper->executeQuery($this->getSQL(), $this->getParameters(), $this->getParameterTypes());
}
return $this->wrapper->executeStatement($this->getSQL(), $this->getParameters(), $this->getParameterTypes());
}
}

View File

@@ -29,7 +29,14 @@ class MySQLStatement implements Statement, \IteratorAggregate
}
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = []) {
return $this->statement->setFetchMode($fetchMode, $arg2, $arg3);
if ($arg2 !== null && $arg3 !== [])
return $this->statement->setFetchMode($fetchMode, $arg2, $arg3);
elseif ($arg2 !== null && $arg3 === [])
return $this->statement->setFetchMode($fetchMode, $arg2);
elseif ($arg2 === null && $arg3 !== [])
return $this->statement->setFetchMode($fetchMode, $arg2, $arg3);
else
return $this->statement->setFetchMode($fetchMode);
}
public function fetch($fetchMode = PDO::FETCH_ASSOC, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) {

View File

@@ -0,0 +1,232 @@
<?php
/**
* @noinspection PhpMissingReturnTypeInspection
* @noinspection PhpUnused
*/
namespace ZM\MySQL;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\ForwardCompatibility\Result;
use Throwable;
use Traversable;
use ZM\Exception\DbException;
class MySQLStatementWrapper
{
public $stmt;
public function __construct(?Result $stmt) {
$this->stmt = $stmt;
}
/**
* 获取结果的迭代器
* wrapper method
* @return ResultStatement
*/
public function getIterator() {
return $this->stmt->getIterator();
}
/**
* 获取列数
* wrapper method
* @return int
*/
public function columnCount() {
return $this->stmt->columnCount();
}
/**
* wrapper method
* @return array|false|mixed
* @throws DbException
*/
public function fetchNumeric() {
try {
return $this->stmt->fetchNumeric();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return array|false|mixed
* @throws DbException
*/
public function fetchAssociative() {
try {
return $this->stmt->fetchAssociative();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return false|mixed
* @throws DbException
*/
public function fetchOne() {
try {
return $this->stmt->fetchOne();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return array
* @throws DbException
*/
public function fetchAllNumeric(): array {
try {
return $this->stmt->fetchAllNumeric();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return array
* @throws DbException
*/
public function fetchAllAssociative(): array {
try {
return $this->stmt->fetchAllAssociative();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return array
* @throws DbException
*/
public function fetchAllKeyValue(): array {
try {
return $this->stmt->fetchAllKeyValue();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return array
* @throws DbException
*/
public function fetchAllAssociativeIndexed(): array {
try {
return $this->stmt->fetchAllAssociativeIndexed();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return array
* @throws DbException
*/
public function fetchFirstColumn(): array {
try {
return $this->stmt->fetchFirstColumn();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return Traversable
* @throws DbException
*/
public function iterateNumeric(): Traversable {
try {
return $this->stmt->iterateNumeric();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return Traversable
* @throws DbException
*/
public function iterateAssociative(): Traversable {
try {
return $this->stmt->iterateAssociative();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return Traversable
* @throws DbException
*/
public function iterateKeyValue(): Traversable {
try {
return $this->stmt->iterateKeyValue();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return Traversable
* @throws DbException
*/
public function iterateAssociativeIndexed(): Traversable {
try {
return $this->stmt->iterateAssociativeIndexed();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return Traversable
* @throws DbException
*/
public function iterateColumn(): Traversable {
try {
return $this->stmt->iterateColumn();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return int
* @throws DbException
*/
public function rowCount() {
try {
return $this->stmt->rowCount();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
*/
public function free(): void {
$this->stmt->free();
}
}

View File

@@ -1,15 +1,575 @@
<?php
/**
* @noinspection PhpUnused
*/
namespace ZM\MySQL;
use Closure;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\ParameterType;
use Throwable;
use Traversable;
use ZM\Exception\DbException;
class MySQLWrapper
{
public $connection;
private $connection;
/**
* MySQLWrapper constructor.
* @throws DbException
*/
public function __construct() {
$this->connection = MySQLManager::getConnection();
try {
$this->connection = DriverManager::getConnection(["driverClass" => MySQLDriver::class]);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return string
*/
public function getDatabase(): string {
return $this->connection->getDatabase();
}
/**
* wrapper method
* @return bool
*/
public function isAutoCommit(): bool {
return $this->connection->isAutoCommit();
}
/**
* wrapper method
* @param $autoCommit
*/
public function setAutoCommit($autoCommit) {
$this->connection->setAutoCommit($autoCommit);
}
/**
* wrapper method
* @param string $query
* @param array $params
* @param array $types
* @return array|false
* @throws DbException
*/
public function fetchAssociative(string $query, array $params = [], array $types = []) {
try {
return $this->connection->fetchAssociative($query, $params, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param string $query
* @param array $params
* @param array $types
* @return array|false
* @throws DbException
*/
public function fetchNumeric(string $query, array $params = [], array $types = []) {
try {
return $this->connection->fetchNumeric($query, $params, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* @param string $query
* @param array $params
* @param array $types
* @return false|mixed
* @throws DbException
*/
public function fetchOne(string $query, array $params = [], array $types = []): bool {
try {
return $this->connection->fetchOne($query, $params, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return bool
*/
public function isTransactionActive(): bool {
return $this->connection->isTransactionActive();
}
/**
* @param $table
* @param array $criteria
* @param array $types
* @return int
* @throws DbException
*/
public function delete($table, array $criteria, array $types = []): int {
try {
return $this->connection->delete($table, $criteria, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param $level
* @return int
*/
public function setTransactionIsolation($level): int {
return $this->connection->setTransactionIsolation($level);
}
/**
* wrapper method
* @return int|null
*/
public function getTransactionIsolation(): ?int {
return $this->connection->getTransactionIsolation();
}
/**
* wrapper method
* @param $table
* @param array $data
* @param array $criteria
* @param array $types
* @return int
* @throws DbException
*/
public function update($table, array $data, array $criteria, array $types = []): int {
try {
return $this->connection->update($table, $data, $criteria, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param $table
* @param array $data
* @param array $types
* @return int
* @throws DbException
*/
public function insert($table, array $data, array $types = []): int {
try {
return $this->connection->insert($table, $data, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param $str
* @return string
*/
public function quoteIdentifier($str): string {
return $this->connection->quoteIdentifier($str);
}
/**
* wrapper method
* @param $value
* @param int $type
* @return mixed
*/
public function quote($value, $type = ParameterType::STRING) {
return $this->connection->quote($value, $type);
}
/**
* wrapper method
* @param string $query
* @param array $params
* @param array $types
* @return array
* @throws DbException
*/
public function fetchAllNumeric(string $query, array $params = [], array $types = []): array {
try {
return $this->connection->fetchAllNumeric($query, $params, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param string $query
* @param array $params
* @param array $types
* @return array
* @throws DbException
*/
public function fetchAllAssociative(string $query, array $params = [], array $types = []): array {
try {
return $this->connection->fetchAllAssociative($query, $params, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param string $query
* @param array $params
* @param array $types
* @return array
* @throws DbException
*/
public function fetchAllKeyValue(string $query, array $params = [], array $types = []): array {
try {
return $this->connection->fetchAllKeyValue($query, $params, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param string $query
* @param array $params
* @param array $types
* @return array
* @throws DbException
*/
public function fetchAllAssociativeIndexed(string $query, array $params = [], array $types = []): array {
try {
return $this->connection->fetchAllAssociativeIndexed($query, $params, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param string $query
* @param array $params
* @param array $types
* @return array
* @throws DbException
*/
public function fetchFirstColumn(string $query, array $params = [], array $types = []): array {
try {
return $this->connection->fetchFirstColumn($query, $params, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param string $query
* @param array $params
* @param array $types
* @return Traversable
* @throws DbException
*/
public function iterateNumeric(string $query, array $params = [], array $types = []): Traversable {
try {
return $this->connection->iterateNumeric($query, $params, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param string $query
* @param array $params
* @param array $types
* @return Traversable
* @throws DbException
*/
public function iterateAssociative(string $query, array $params = [], array $types = []): Traversable {
try {
return $this->connection->iterateAssociative($query, $params, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param string $query
* @param array $params
* @param array $types
* @return Traversable
* @throws DbException
*/
public function iterateKeyValue(string $query, array $params = [], array $types = []): Traversable {
try {
return $this->connection->iterateKeyValue($query, $params, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param string $query
* @param array $params
* @param array $types
* @return Traversable
* @throws DbException
*/
public function iterateAssociativeIndexed(string $query, array $params = [], array $types = []): Traversable {
try {
return $this->connection->iterateAssociativeIndexed($query, $params, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param string $query
* @param array $params
* @param array $types
* @return Traversable
* @throws DbException
*/
public function iterateColumn(string $query, array $params = [], array $types = []): Traversable {
try {
return $this->connection->iterateColumn($query, $params, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param $sql
* @param array $params
* @param array $types
* @param QueryCacheProfile|null $qcp
* @return MySQLStatementWrapper
* @throws DbException
*/
public function executeQuery($sql, array $params = [], $types = [], ?QueryCacheProfile $qcp = null): MySQLStatementWrapper {
try {
$query = $this->connection->executeQuery($sql, $params, $types, $qcp);
return new MySQLStatementWrapper($query);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param $sql
* @param $params
* @param $types
* @param QueryCacheProfile $qcp
* @return MySQLStatementWrapper
* @throws DbException
*/
public function executeCacheQuery($sql, $params, $types, QueryCacheProfile $qcp): MySQLStatementWrapper {
try {
$query = $this->connection->executeCacheQuery($sql, $params, $types, $qcp);
return new MySQLStatementWrapper($query);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param $sql
* @param array $params
* @param array $types
* @return int
* @throws DbException
*/
public function executeStatement($sql, array $params = [], array $types = []): int {
try {
return $this->connection->executeStatement($sql, $params, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return int
*/
public function getTransactionNestingLevel(): int {
return $this->connection->getTransactionNestingLevel();
}
/**
* wrapper method
* @param null $name
* @return string
*/
public function lastInsertId($name = null): string {
return $this->connection->lastInsertId($name);
}
/**
* overwrite method to $this->connection->transactional()
* @param Closure $func
* @return mixed
* @throws DbException
*/
public function transactional(Closure $func) {
$this->beginTransaction();
try {
$res = $func($this);
$this->commit();
return $res;
} catch (Throwable $e) {
$this->rollBack();
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param $nestTransactionsWithSavepoints
* @throws DbException
*/
public function setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints) {
try {
$this->connection->setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return bool
*/
public function getNestTransactionsWithSavepoints(): bool {
return $this->connection->getNestTransactionsWithSavepoints();
}
/**
* wrapper method
* @return bool
*/
public function beginTransaction(): bool {
return $this->connection->beginTransaction();
}
/**
* wrapper method
* @return bool
* @throws DbException
*/
public function commit(): bool {
try {
return $this->connection->commit();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return bool
* @throws DbException
*/
public function rollBack(): bool {
try {
return $this->connection->rollBack();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param $savepoint
* @throws DbException
*/
public function createSavepoint($savepoint) {
try {
$this->connection->createSavepoint($savepoint);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param $savepoint
* @throws DbException
*/
public function releaseSavepoint($savepoint) {
try {
$this->connection->releaseSavepoint($savepoint);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param $savepoint
* @throws DbException
*/
public function rollbackSavepoint($savepoint) {
try {
$this->connection->rollbackSavepoint($savepoint);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @throws DbException
*/
public function setRollbackOnly() {
try {
$this->connection->setRollbackOnly();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @return bool
* @throws DbException
*/
public function isRollbackOnly(): bool {
try {
return $this->connection->isRollbackOnly();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* overwrite method to $this->connection->createQueryBuilder
* @return MySQLQueryBuilder
*/
public function createQueryBuilder(): MySQLQueryBuilder {
return new MySQLQueryBuilder($this);
}
public function getConnection(): Connection {
return $this->connection;
}
public function __destruct() {