Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Sep 13, 2018
2 parents c9be434 + ec83561 commit b289b33
Show file tree
Hide file tree
Showing 63 changed files with 2,981 additions and 445 deletions.
8 changes: 5 additions & 3 deletions src/Bean/BeanFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,22 @@ private static function getMethodsTpl($ref, $class)
}
$paramsTpls = static::getMethodParamTpls($method);
$methodReturnType = static::getMethodReturnType($method);
$returnsReference = $method->returnsReference() ? '&' : '';
$tpl .= <<<TPL
public function {$method->name}({$paramsTpls['define']}){$methodReturnType}
public function {$returnsReference}{$method->name}({$paramsTpls['define']}){$methodReturnType}
{
\$__args__ = func_get_args();
{$paramsTpls['set_args']}
return \$this->beanProxy->call(
\$__result__ = \$this->beanProxy->call(
'{$method->name}',
function({$paramsTpls['define']}){
\$__args__ = func_get_args();
{$paramsTpls['set_args']}
return parent::{$method->name}(...\$__args__);
return {$returnsReference}parent::{$method->name}(...\$__args__);
},
\$__args__
);
return \$__result__;
}
TPL;
Expand Down
75 changes: 0 additions & 75 deletions src/Db/Aop/MysqlAspect.php

This file was deleted.

14 changes: 12 additions & 2 deletions src/Db/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,26 @@ public static function release($db)
* 返回一个查询器
* @param string $poolName
* @param string $modelClass
* @param boolean $newInstance
* @return IQuery
*/
public static function query($poolName = null, $modelClass = null): IQuery
public static function query($poolName = null, $modelClass = null, $newInstance = false): IQuery
{
if(null === $poolName)
{
$poolName = static::getDefaultPoolName();
}

return BeanFactory::newInstance(Query::class, static::getInstance($poolName), $modelClass);
if($newInstance)
{
$db = static::getNewInstance($poolName);
}
else
{
$db = static::getInstance($poolName);
}

return BeanFactory::newInstance(Query::class, $db, $modelClass);
}

/**
Expand Down
97 changes: 97 additions & 0 deletions src/Db/Drivers/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
namespace Imi\Db\Drivers;

use Imi\Db\Interfaces\IDb;
use Imi\Util\Defer;

abstract class Base implements IDb
{
/**
* 启动一个事务
* @return Defer
*/
public function deferBeginTransaction(): Defer
{
return $this->parseDefer('beginTransaction');
}

/**
* 提交一个事务
* @return Defer
*/
public function deferCommit(): Defer
{
return $this->parseDefer('commit');
}

/**
* 回滚一个事务
* @return Defer
*/
public function deferRollBack(): Defer
{
return $this->parseDefer('rollback');
}

/**
* 执行一条 SQL 语句,并返回受影响的行数
* @param string $sql
* @return Defer
*/
public function deferExec(string $sql): Defer
{
return $this->parseDefer('exec', $sql);
}

/**
* 准备执行语句并返回一个语句对象
* @param string $sql
* @param array $driverOptions
* @return Defer
*/
public function deferPrepare(string $sql, array $driverOptions = []): Defer
{
return $this->parseDefer('prepare', $sql, $driverOptions);
}

/**
* 执行一条SQL语句,返回一个结果集作为PDOStatement对象
* @param string $sql
* @return Defer
*/
public function deferQuery(string $sql): Defer
{
return $this->parseDefer('query', $sql);
}

/**
* 处理延迟调用
*
* @param string $methodName
* @param array $args
* @return Defer
*/
protected function parseDefer($methodName, ...$args)
{
$innerMethodName = '__' . $methodName;
$instance = $this->getInstance();
if(method_exists($instance, 'setDefer'))
{
$instance->setDefer(true);
$generate = $this->$innerMethodName(...$args);
$generate->next();
$callable = function() use($instance, $generate){
$result = $instance->recv();
$generate->send($result);
return $generate->getReturn();
};
}
else
{
$callable = function(){
return $this->$methodName(...$args);
};
}
return new Defer($callable);
}
}
51 changes: 51 additions & 0 deletions src/Db/Drivers/BaseStatement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
namespace Imi\Db\Drivers;

use Imi\Util\Defer;
use Imi\Db\Interfaces\IStatement;
use Imi\Bean\BeanFactory;


abstract class BaseStatement implements IStatement
{
/**
* 执行一条预处理语句,延迟执行
* @param array $inputParameters
* @return Defer
*/
public function deferExecute(array $inputParameters = null): Defer
{
return $this->parseDefer('execute', $inputParameters);
}

/**
* 处理延迟调用
*
* @param string $methodName
* @param array $args
* @return Defer
*/
protected function parseDefer($methodName, ...$args)
{
$innerMethodName = '__' . $methodName;
$db = $this->getDb()->getInstance();
if(method_exists($db, 'setDefer'))
{
$db->setDefer(true);
$generate = $this->$innerMethodName(...$args);
$generate->next();
$callable = function() use($db, $generate){
$result = $db->recv();
$generate->send($result);
return $generate->getReturn();
};
}
else
{
$callable = function(){
return $this->$methodName(...$args);
};
}
return new Defer($callable);
}
}
Loading

0 comments on commit b289b33

Please sign in to comment.