Commit 8a80366e by 王召彬

Merge branch 'master' of http://git.2b3.cn/tencent/services

parents c88c089a 3efc775e
......@@ -17,41 +17,23 @@ use Doctrine\Common\Annotations\Annotation\Target;
*/
class Split
{
private $name='';
private $table='';
private $num=100;
public function __construct(array $values)
{
if (isset($values['value'])) {
$this->name = $values['value'];
}
if (isset($values['name'])) {
$this->name = $values['name'];
}
if (isset($values['table'])) {
$this->table = $values['table'];
if (isset($values['num'])) {
$this->num = $values['num'];
}
}
public function setName($name)
public function setNum($num)
{
$this->name = $name;
$this->num = $num;
}
public function setTable($table)
{
$this->table = $table;
}
public function getName() : string
{
return $this->name;
}
public function getTable()
public function getNum()
{
return $this->table;
return $this->num;
}
}
\ No newline at end of file
......@@ -7,16 +7,16 @@
*/
namespace Hdll\Services\Common\Bean\Collector;
use App\Bean\Annotation\Auth;
use Hdll\Services\Common\Bean\Annotation\Split;
use Swoft\Bean\CollectorInterface;
use Swoft\Db\Bean\Collector\EntityCollector;
class SplitCollector implements CollectorInterface
{
/**
* @var array
*/
private static $auth = [];
private static $split = [];
public static function collect(string $className,
$objectAnnotation = null,
......@@ -26,20 +26,19 @@ class SplitCollector implements CollectorInterface
{
if ($objectAnnotation instanceof Split) {
$name = $objectAnnotation->getName();
$scope = $objectAnnotation->getScope();
$entityCollector = EntityCollector::getCollector();
self::$split[$className]['table'] = $entityCollector[$className]['table']['name'];
self::$split[$className]['num'] = $objectAnnotation->getNum();
self::$split['map'][$className] = $entityCollector[$className]['table']['name'];
self::$auth[$className]['name'] = $name;
self::$auth[$className]['order'] = $scope;
self::$auth['map'][$className] = $scope;
}
var_dump($className, $objectAnnotation, $propertyName, $methodName);
}
public static function getCollector()
{
return self::$auth;
return self::$split;
}
}
\ No newline at end of file
......@@ -26,10 +26,10 @@ class SplitParser extends AbstractParser
{
$beanName = $className;
$scope = Scope::SINGLETON;
var_dump('parser');
SplitCollector::collect($className, $objectAnnotation, $propertyName, $methodName, $propertyValue);
return [$beanName, $scope, ""];
}
......
......@@ -48,7 +48,7 @@ class SplitWrapper extends AbstractWrapper
*/
public function isParsePropertyAnnotations(array $annotations): bool
{
return false;
return true;
}
/**
* 是否解析方法注解
......
......@@ -9,7 +9,9 @@
*/
namespace Swoft\Db;
use Hdll\Services\Common\Bean\Collector\SplitCollector;
use Swoft\Core\RequestContext;
use Swoft\Db\Bean\Collector\EntityCollector;
/**
* Query
......@@ -17,30 +19,50 @@ use Swoft\Core\RequestContext;
class Query
{
/**
* Query查询分表处理
* @param string $tableName
* @param string $alias
*
* @param string|null $alias
* @return QueryBuilder
* @throws Exception\DbException
*/
public static function table(string $tableName, string $alias = null): QueryBuilder
{
$query = new QueryBuilder();
$query = $query->table($tableName, $alias);
return $query;
}
$collector = SplitCollector::getCollector();//分表搜集器
private function getTableName($commentString)
{
preg_match('/(?<=Table\(name=").*(?="\))/', $commentString, $matches);
return $tableName = isset($matches[0])?$matches[0]:'';
//如果是实体且为分表实体
if (strpos($tableName, '\\') !== false && isset($collector[$tableName])) {
$className = $tableName;
$tableName = $collector[$tableName]['table'];
$entityCollector = EntityCollector::getCollector();
//设置分表对应的className
EntityCollector::setCollector($tableName.'_'.self::getDbNum($className), $tableName);
//获取分表名
$tableName = $tableName.'_'.self::getDbNum($className);
//设置分表类型
$query->selectInstance($entityCollector[$className]['instance']);
} elseif ($map = array_flip($collector['map']) && isset($map[$tableName]) ) { //非实体
$tableName = $tableName.'_'.self::getDbNum($map[$tableName]);
}
$query = $query->table($tableName, $alias);
return $query;
}
private function getDbNum()
private function getDbNum($className)
{
$splitCollector = SplitCollector::getCollector();
$splitNum = $splitCollector[$className]['num'];
$data = RequestContext::getContextData();
$storeId = $data['userInfo']['storeId'];
return $storeId % 100;
return $storeId % $splitNum;
}
}
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/9/14
* Time: 10:00
*/
namespace Hdll\Services\Common\Traits;
trait EntityCollectorTrait
{
public static function setCollector($tableName, $defaultTableName)
{
return ! isset(self::$entities[$tableName]) && self::$entities[$tableName] = &self::$entities[$defaultTableName];
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment