Skip to content

Commit

Permalink
esd
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangjincheng committed Jun 5, 2019
1 parent ecc9923 commit 1f8074f
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 99 deletions.
69 changes: 69 additions & 0 deletions src/Mysql/Mysqli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Created by PhpStorm.
* User: 白猫
* Date: 2019/6/4
* Time: 18:31
*/

namespace ESD\Plugins\Mysql;


use ESD\Psr\DB\DBInterface;
use ReflectionClass;

class Mysqli implements DBInterface
{
/**
* @var
*/
private $_mysqli;

private $_lastQuery;

/**
* Mysqli constructor.
* @param $params
* @throws \ReflectionException
*/
public function __construct($params)
{
$mysqlic = new ReflectionClass('mysqli');
$this->_mysqli = $mysqlic->newInstanceArgs($params);
}

public function __call($name, $arguments)
{
$this->_lastQuery = json_encode($arguments);
return $this->execute($name, function () use ($name, $arguments) {
return call_user_func_array([$this->_mysqli, $name], $arguments);
});
}

public function __set($name, $value)
{
$this->_mysqli->$name = $value;
}

public function __get($name)
{
return $this->_mysqli->$name;
}

public function getType()
{
return "mysqli";
}

public function execute($name, callable $call = null)
{
if ($call != null) {
return $call();
}
}

public function getLastQuery()
{
return $this->_lastQuery;
}
}
138 changes: 39 additions & 99 deletions src/Mysql/MysqliDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
namespace ESD\Plugins\Mysql;


use ESD\Psr\DB\DBInterface;
use ESD\Core\Exception;
use ESD\Core\Plugins\Logger\GetLogger;
use ESD\Server\Co\Server;

class MysqliDb extends \MysqliDb implements DBInterface
class MysqliDb extends \MysqliDb
{
use GetLogger;

Expand All @@ -28,10 +28,45 @@ public function isTransactionInProgress(): bool
return $this->_transaction_in_progress ?? false;
}

/**
* A method to connect to the database
*
* @param null|string $connectionName
* @throws \Exception
* @return void
*/
public function connect($connectionName = 'default')
{
if (!isset($this->connectionsSettings[$connectionName]))
throw new Exception('Connection profile not set');

$pro = $this->connectionsSettings[$connectionName];
$params = array_values($pro);
$charset = array_pop($params);

if ($this->isSubQuery) {
return;
}

if (empty($pro['host']) && empty($pro['socket'])) {
throw new Exception('MySQL host or socket is not set');
}

$mysqli = new Mysqli($params);

if ($mysqli->connect_error) {
throw new Exception('Connect Error ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error, $mysqli->connect_errno);
}

if (!empty($charset)) {
$mysqli->set_charset($charset);
}
$this->_mysqli[$connectionName] = $mysqli;
$this->debug("mysql connect $connectionName");
}

/**
* @return \MysqliDb
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
*/
public function reset()
{
Expand All @@ -44,16 +79,6 @@ public function reset()
return $result;
}

/**
* @param string $connectionName
* @throws \Exception
*/
public function connect($connectionName = 'default')
{
parent::connect($connectionName);
$this->debug("mysql connect $connectionName");
}

/**
* @param string $connection
* @throws \Exception
Expand All @@ -68,89 +93,4 @@ public function getType()
{
return "mysqli";
}

public function replace($tableName, $insertData)
{
return $this->execute(function () use ($tableName, $insertData) {
return parent::replace($tableName, $insertData);
});
}

public function insert($tableName, $insertData)
{
return $this->execute(function () use ($tableName, $insertData) {
return parent::insert($tableName, $insertData);
});
}

public function delete($tableName, $numRows = null)
{
return $this->execute(function () use ($tableName, $numRows) {
return parent::delete($tableName, $numRows);
});
}

public function update($tableName, $tableData, $numRows = null)
{
return $this->execute(function () use ($tableName, $tableData, $numRows) {
return parent::update($tableName, $tableData, $numRows);
});
}

public function get($tableName, $numRows = null, $columns = '*')
{
return $this->execute(function () use ($tableName, $numRows, $columns) {
return parent::get($tableName, $numRows, $columns);
});
}

public function query($query, $numRows = null)
{
return $this->execute(function () use ($query, $numRows) {
return parent::query($query, $numRows);
});
}

public function rawQuery($query, $bindParams = null)
{
return $this->execute(function () use ($query, $bindParams) {
return parent::rawQuery($query, $bindParams);
});
}

public function rollback()
{
$this->_lastQuery = "ROLLBACK";
return $this->execute(function () {
return parent::rollback();
});
}

public function commit()
{
$this->_lastQuery = "COMMIT";
return $this->execute(function () {
return parent::commit();
});
}

public function startTransaction()
{
$this->_lastQuery = "BEGIN";
return $this->execute(function () {
parent::startTransaction();
});
}

/**
* 执行代理
* @param callable|null $call
* @return mixed
*/
public function execute(callable $call = null)
{
if ($call != null) {
return $call();
}
}
}

0 comments on commit 1f8074f

Please sign in to comment.