Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
services
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tencent
services
Commits
8a80366e
Commit
8a80366e
authored
Sep 14, 2018
by
王召彬
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
http://git.2b3.cn/tencent/services
parents
c88c089a
3efc775e
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
71 additions
and
49 deletions
+71
-49
src/Common/Bean/Annotation/Split.php
+8
-25
src/Common/Bean/Collector/SplitCollector.php
+9
-9
src/Common/Bean/Parser/SplitParser.php
+2
-2
src/Common/Bean/Wrapper/SplitWrapper.php
+1
-1
src/Common/Entity/Query.php
+34
-12
src/Common/Traits/EntityCollectorTrait.php
+17
-0
No files found.
src/Common/Bean/Annotation/Split.php
View file @
8a80366e
...
@@ -17,41 +17,23 @@ use Doctrine\Common\Annotations\Annotation\Target;
...
@@ -17,41 +17,23 @@ use Doctrine\Common\Annotations\Annotation\Target;
*/
*/
class
Split
class
Split
{
{
private
$name
=
''
;
private
$num
=
100
;
private
$table
=
''
;
public
function
__construct
(
array
$values
)
public
function
__construct
(
array
$values
)
{
{
if
(
isset
(
$values
[
'value'
]))
{
if
(
isset
(
$values
[
'num'
]))
{
$this
->
name
=
$values
[
'value'
];
$this
->
num
=
$values
[
'num'
];
}
if
(
isset
(
$values
[
'name'
]))
{
$this
->
name
=
$values
[
'name'
];
}
if
(
isset
(
$values
[
'table'
]))
{
$this
->
table
=
$values
[
'table'
];
}
}
}
}
public
function
setN
ame
(
$name
)
public
function
setN
um
(
$num
)
{
{
$this
->
n
ame
=
$name
;
$this
->
n
um
=
$num
;
}
}
public
function
setTable
(
$table
)
{
$this
->
table
=
$table
;
}
public
function
getName
()
:
string
{
return
$this
->
name
;
}
public
function
get
Table
()
public
function
get
Num
()
{
{
return
$this
->
table
;
return
$this
->
num
;
}
}
}
}
\ No newline at end of file
src/Common/Bean/Collector/SplitCollector.php
View file @
8a80366e
...
@@ -7,16 +7,16 @@
...
@@ -7,16 +7,16 @@
*/
*/
namespace
Hdll\Services\Common\Bean\Collector
;
namespace
Hdll\Services\Common\Bean\Collector
;
use
App\Bean\Annotation\Auth
;
use
Hdll\Services\Common\Bean\Annotation\Split
;
use
Hdll\Services\Common\Bean\Annotation\Split
;
use
Swoft\Bean\CollectorInterface
;
use
Swoft\Bean\CollectorInterface
;
use
Swoft\Db\Bean\Collector\EntityCollector
;
class
SplitCollector
implements
CollectorInterface
class
SplitCollector
implements
CollectorInterface
{
{
/**
/**
* @var array
* @var array
*/
*/
private
static
$
auth
=
[];
private
static
$
split
=
[];
public
static
function
collect
(
string
$className
,
public
static
function
collect
(
string
$className
,
$objectAnnotation
=
null
,
$objectAnnotation
=
null
,
...
@@ -26,20 +26,19 @@ class SplitCollector implements CollectorInterface
...
@@ -26,20 +26,19 @@ class SplitCollector implements CollectorInterface
{
{
if
(
$objectAnnotation
instanceof
Split
)
{
if
(
$objectAnnotation
instanceof
Split
)
{
$name
=
$objectAnnotation
->
getName
();
$entityCollector
=
EntityCollector
::
getCollector
();
$scope
=
$objectAnnotation
->
getScope
();
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
()
public
static
function
getCollector
()
{
{
return
self
::
$
auth
;
return
self
::
$
split
;
}
}
}
}
\ No newline at end of file
src/Common/Bean/Parser/SplitParser.php
View file @
8a80366e
...
@@ -26,10 +26,10 @@ class SplitParser extends AbstractParser
...
@@ -26,10 +26,10 @@ class SplitParser extends AbstractParser
{
{
$beanName
=
$className
;
$beanName
=
$className
;
$scope
=
Scope
::
SINGLETON
;
$scope
=
Scope
::
SINGLETON
;
var_dump
(
'parser'
);
SplitCollector
::
collect
(
$className
,
$objectAnnotation
,
$propertyName
,
$methodName
,
$propertyValue
);
SplitCollector
::
collect
(
$className
,
$objectAnnotation
,
$propertyName
,
$methodName
,
$propertyValue
);
return
[
$beanName
,
$scope
,
""
];
return
[
$beanName
,
$scope
,
""
];
}
}
...
...
src/Common/Bean/Wrapper/SplitWrapper.php
View file @
8a80366e
...
@@ -48,7 +48,7 @@ class SplitWrapper extends AbstractWrapper
...
@@ -48,7 +48,7 @@ class SplitWrapper extends AbstractWrapper
*/
*/
public
function
isParsePropertyAnnotations
(
array
$annotations
)
:
bool
public
function
isParsePropertyAnnotations
(
array
$annotations
)
:
bool
{
{
return
fals
e
;
return
tru
e
;
}
}
/**
/**
* 是否解析方法注解
* 是否解析方法注解
...
...
src/Common/Entity/Query.php
View file @
8a80366e
...
@@ -9,7 +9,9 @@
...
@@ -9,7 +9,9 @@
*/
*/
namespace
Swoft\Db
;
namespace
Swoft\Db
;
use
Hdll\Services\Common\Bean\Collector\SplitCollector
;
use
Swoft\Core\RequestContext
;
use
Swoft\Core\RequestContext
;
use
Swoft\Db\Bean\Collector\EntityCollector
;
/**
/**
* Query
* Query
...
@@ -17,30 +19,50 @@ use Swoft\Core\RequestContext;
...
@@ -17,30 +19,50 @@ use Swoft\Core\RequestContext;
class
Query
class
Query
{
{
/**
/**
* Query查询分表处理
* @param string $tableName
* @param string $tableName
* @param string $alias
* @param string|null $alias
*
* @return QueryBuilder
* @return QueryBuilder
* @throws Exception\DbException
*/
*/
public
static
function
table
(
string
$tableName
,
string
$alias
=
null
)
:
QueryBuilder
public
static
function
table
(
string
$tableName
,
string
$alias
=
null
)
:
QueryBuilder
{
{
$query
=
new
QueryBuilder
();
$query
=
new
QueryBuilder
();
$query
=
$query
->
table
(
$tableName
,
$alias
);
return
$query
;
$collector
=
SplitCollector
::
getCollector
();
//分表搜集器
}
private
function
getTableName
(
$commentString
)
//如果是实体且为分表实体
{
if
(
strpos
(
$tableName
,
'\\'
)
!==
false
&&
isset
(
$collector
[
$tableName
]))
{
preg_match
(
'/(?<=Table\(name=").*(?="\))/'
,
$commentString
,
$matches
);
$className
=
$tableName
;
return
$tableName
=
isset
(
$matches
[
0
])
?
$matches
[
0
]
:
''
;
$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
();
$data
=
RequestContext
::
getContextData
();
$storeId
=
$data
[
'userInfo'
][
'storeId'
];
$storeId
=
$data
[
'userInfo'
][
'storeId'
];
return
$storeId
%
100
;
return
$storeId
%
$splitNum
;
}
}
}
}
src/Common/Traits/EntityCollectorTrait.php
0 → 100644
View file @
8a80366e
<?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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment