Skip to content

Commit f70cb78

Browse files
committed
add db component step 1
1 parent d7763d5 commit f70cb78

18 files changed

+266
-35
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,5 @@ composer.lock
409409
#
410410
# Recommended template: PHP.gitignore
411411

412-
/wpcs/*
412+
/wpcs/*
413+
.idea

.idea/misc.xml

-6
This file was deleted.

.idea/modules.xml

-8
This file was deleted.

.idea/pframework.iml

-8
This file was deleted.

.idea/php.xml

-4
This file was deleted.

.idea/vcs.xml

-6
This file was deleted.

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 功能
44

5-
1. [x] 使用symfony的console组建启动swoole的serverr
5+
1. [x] 使用symfony的console组建启动swoole的server
66
2. [x] 使用swoole创建http server
77
3. [x] 使用symfofny的event组件传递http事件
88
6. [x] 参考laravel的ServiceProvider特性设计组建注册
@@ -12,6 +12,7 @@
1212
8. [ ] 使用hash算法进行db组建设计,支持分库分表
1313
9. [ ] 多个包使用composer进行包管理
1414
10. [ ] psr有规范的组件严格遵守规范
15+
10. [ ] 分库分表
1516

1617

1718
## 测试

composer.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"symfony/console": "^3.4",
1616
"symfony/event-dispatcher": "^3.4",
1717
"symfony/dependency-injection": "^3.4",
18-
"symfony/http-foundation": "^3.4"
18+
"symfony/http-foundation": "^3.4",
19+
"ext-pdo": "*",
20+
"latitude/latitude": "^2.3"
1921
},
2022
"require-dev": {
2123
"phpunit/phpunit": "^6.0"

config/app.php

+38
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,42 @@
77
'providers' => [
88
pframework\web\WebApplicationProvider::class,
99
],
10+
'db' => [
11+
'single' => [
12+
'host' => '127.0.0.1',
13+
'port' => 3306,
14+
'db' => 'test',
15+
'username' => 'root',
16+
'password' => 'root',
17+
],
18+
'mutiply' => [
19+
'tables' => [
20+
'order' => [
21+
'shard' => 10,
22+
'strategy' => pframework\db\strategy\Crc32Strategy::class
23+
],
24+
'user' => [
25+
'shard' => 20,
26+
'strategy' => pframework\db\strategy\ModStrategy::class
27+
]
28+
],
29+
'strategy' => pframework\db\strategy\ModStrategy::class,
30+
'connections' => [
31+
[
32+
'host' => '127.0.0.1',
33+
'port' => 3306,
34+
'db' => 'test',
35+
'username' => 'root',
36+
'password' => 'root',
37+
],
38+
[
39+
'host' => '127.0.0.1',
40+
'port' => 3306,
41+
'db' => 'test1',
42+
'username' => 'root',
43+
'password' => 'root',
44+
],
45+
],
46+
]
47+
],
1048
];

src/app/provider/DbProvider.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace app\provider;
4+
5+
use pframework\AbstractServiceProvider;
6+
use pframework\db\Connection;
7+
use pframework\db\MultiplyConnection;
8+
9+
class DbProvider extends AbstractServiceProvider
10+
{
11+
public function boot()
12+
{
13+
$config = $this->app->getConfig();
14+
// 分库
15+
if (isset($config['db']['mutiply'])) {
16+
$multiplyConnectionsConfig = $config['db']['multiply'];
17+
$multiplyConnection = new MultiplyConnection($multiplyConnectionsConfig);
18+
19+
$multiplyConnection->setDbShardStrategy(new $multiplyConnectionsConfig['strategy']);
20+
21+
foreach ($multiplyConnectionsConfig['multiply']['table'] as $tableName => $shardTableConfig) {
22+
$tableStrategy = new $shardTableConfig['strategy']($shardTableConfig['shard']);
23+
$multiplyConnection->addTableShardStrategy($tableName, $tableStrategy);
24+
}
25+
26+
$this->container->set("testShardDb", $multiplyConnection);
27+
}
28+
29+
// 单库
30+
if (isset($config['db']['single'])) {
31+
$connection = new Connection($config['db']['single']);
32+
$this->container->set("testDb", $connection);
33+
}
34+
}
35+
}

src/pframework/db/Connection.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
4+
namespace pframework\db;
5+
6+
use PDO;
7+
8+
9+
class Connection extends PDO implements ConnectionInterface
10+
{
11+
private $config;
12+
13+
public function __construct($config)
14+
{
15+
$this->config = $config;
16+
parent::__construct();
17+
}
18+
19+
public function table($table)
20+
{
21+
// TODO: Implement table() method.
22+
}
23+
24+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
4+
namespace pframework\db;
5+
6+
7+
interface ConnectionInterface
8+
{
9+
public function table($table);
10+
}
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace pframework\db;
4+
5+
use pframework\db\strategy\StrategyInterface;
6+
7+
class MultiplyConnection implements ConnectionInterface
8+
{
9+
/**
10+
* @var StrategyInterface
11+
*/
12+
private $dbShardStrategy;
13+
14+
/**
15+
* @var StrategyInterface[]
16+
*/
17+
private $tableShardStrategy;
18+
19+
private $dbShardValue;
20+
21+
private $tableShardValue;
22+
23+
private $config;
24+
25+
public function __construct($config)
26+
{
27+
$this->config = $config;
28+
}
29+
30+
public function setDbShardStrategy(StrategyInterface $strategy)
31+
{
32+
$this->dbShardStrategy = $strategy;
33+
}
34+
35+
public function addTableShardStrategy(string $table, StrategyInterface $strategy)
36+
{
37+
$this->tableShardStrategy[$table] = $strategy;
38+
}
39+
40+
public function setDbShardValue($dbShardValue)
41+
{
42+
$this->dbShardValue = $dbShardValue;
43+
}
44+
45+
public function setTableShardValue($tableShardValue)
46+
{
47+
$this->tableShardValue = $tableShardValue;
48+
}
49+
50+
public function table($table)
51+
{
52+
$shardDbIndex = $this->dbShardStrategy->shardByValue($this->dbShardValue);
53+
$shardTable = $this->tableShardStrategy[$table]->shardByValue($this->tableShardValue);
54+
return new QueryBuilder($this->getConnection($shardDbIndex), $shardTable);
55+
}
56+
57+
public function getConnection($shardDbIndex)
58+
{
59+
return new Connection();
60+
}
61+
}

src/pframework/db/QueryBuilder.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
4+
namespace pframework\db;
5+
6+
7+
class QueryBuilder implements QueryBuilderInterface
8+
{
9+
/**
10+
* @var ConnectionInterface $connection
11+
*/
12+
private $connection;
13+
14+
private $table;
15+
16+
public function __construct(ConnectionInterface $connection, $table)
17+
{
18+
$this->connection = $connection;
19+
$this->table = $table;
20+
}
21+
22+
public function select($field)
23+
{
24+
$sql = "SELECT * FROM {$this->table} WHERE {$this->where} LIMIT {$this->limit}";
25+
26+
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
4+
namespace pframework\db;
5+
6+
7+
interface QueryBuilderInterface
8+
{
9+
public function __construct(ConnectionInterface $connection, $table);
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
4+
namespace pframework\db\strategy;
5+
6+
7+
class Crc32Strategy implements StrategyInterface
8+
{
9+
private $shardCount;
10+
11+
public function __construct($shardCount)
12+
{
13+
$this->shardCount = $shardCount;
14+
}
15+
16+
public function shardByValue($value)
17+
{
18+
return (sprintf("%u", crc32($value)) >> 16 & 0x7fff) % $this->shardCount;
19+
}
20+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
4+
namespace pframework\db\strategy;
5+
6+
7+
class ModStrategy implements StrategyInterface
8+
{
9+
private $shardCount;
10+
11+
public function __construct($shardCount)
12+
{
13+
$this->shardCount = $shardCount;
14+
}
15+
16+
public function shardByValue($value)
17+
{
18+
return $value % $this->shardCount;
19+
}
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
4+
namespace pframework\db\strategy;
5+
6+
7+
interface StrategyInterface
8+
{
9+
public function __construct($shardCount);
10+
11+
public function shardByValue($value);
12+
}

0 commit comments

Comments
 (0)