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
d448c95a
Commit
d448c95a
authored
Sep 14, 2018
by
feixiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
query分表重构
parent
9f312bb1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
54 additions
and
20 deletions
+54
-20
src/Common/Bean/Collector/SplitCollector.php
+4
-7
src/Common/Bean/Parser/SplitParser.php
+2
-2
src/Common/Bean/Wrapper/SplitWrapper.php
+1
-1
src/Common/Entity/Query.php
+30
-10
src/Common/Traits/EntityCollectorTrait.php
+17
-0
No files found.
src/Common/Bean/Collector/SplitCollector.php
View file @
d448c95a
...
...
@@ -7,9 +7,9 @@
*/
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
{
...
...
@@ -26,15 +26,12 @@ class SplitCollector implements CollectorInterface
{
if
(
$objectAnnotation
instanceof
Split
)
{
$name
=
$objectAnnotation
->
getName
();
$scope
=
$objectAnnotation
->
getScope
();
$entityCollector
=
EntityCollector
::
getCollector
();
self
::
$auth
[
$className
][
'name'
]
=
$name
;
self
::
$auth
[
$className
][
'order'
]
=
$scope
;
self
::
$auth
[
'map'
][
$className
]
=
$scope
;
self
::
$auth
[
$className
][
'table'
]
=
$entityCollector
[
$className
][
'table'
][
'name'
];;
self
::
$auth
[
'map'
][
$className
]
=
$entityCollector
[
$className
][
'table'
][
'name'
];
}
var_dump
(
$className
,
$objectAnnotation
,
$propertyName
,
$methodName
);
}
public
static
function
getCollector
()
...
...
src/Common/Bean/Parser/SplitParser.php
View file @
d448c95a
...
...
@@ -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
,
""
];
}
...
...
src/Common/Bean/Wrapper/SplitWrapper.php
View file @
d448c95a
...
...
@@ -48,7 +48,7 @@ class SplitWrapper extends AbstractWrapper
*/
public
function
isParsePropertyAnnotations
(
array
$annotations
)
:
bool
{
return
fals
e
;
return
tru
e
;
}
/**
* 是否解析方法注解
...
...
src/Common/Entity/Query.php
View file @
d448c95a
...
...
@@ -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,24 +19,42 @@ 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
(),
$tableName
);
//获取分表名
$tableName
=
$tableName
.
'_'
.
self
::
getDbNum
();
//设置分表类型
$query
->
selectInstance
(
$entityCollector
[
$className
][
'instance'
]);
}
elseif
(
$map
=
array_flip
(
$collector
[
'map'
])
&&
isset
(
$map
[
$tableName
])
)
{
//非实体
$tableName
=
$tableName
.
'_'
.
self
::
getDbNum
();
}
$query
=
$query
->
table
(
$tableName
,
$alias
);
return
$query
;
}
private
function
getDbNum
()
...
...
src/Common/Traits/EntityCollectorTrait.php
0 → 100644
View file @
d448c95a
<?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