Skip to content

Commit

Permalink
feat(db): master-salve db support :sparkels:
Browse files Browse the repository at this point in the history
  • Loading branch information
TIGERB committed May 21, 2017
1 parent 64665dd commit 6e9748f
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 5 deletions.
13 changes: 13 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ dbname = easyphp
dbhost = localhost
username = easyphp
password = easyphp
slave = 0,1

[database-slave-0]
dbname = easyphp
dbhost = localhost
username = easyphp
password = easyphp

[database-slave-1]
dbname = easyphp
dbhost = localhost
username = easyphp
password = easyphp

[nosql]
support = redis
Expand Down
15 changes: 14 additions & 1 deletion README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ vendor [composer目录]
├── pre-commit [git pre-commit预commit钩子示例文件]
├── commit-msg [git commit-msg示例文件]
.babelrc [babel配置文件]
.env [环境变量文件]
.env.example [环境变量示例文件]
.gitignore [git忽略文件配置]
build [php打包脚本]
cli [框架cli模式运行脚本]
Expand Down Expand Up @@ -180,6 +180,14 @@ require('../framework/run.php');

框架中所有的异常输出和控制器输出都是json格式,因为我认为在前后端完全分离的今天,这是很友善的,目前我们不需要再去考虑别的东西。

# 请求参数校验,目前提供必传,长度,数字类型校验,使用如下
```
$request = App::$container->getSingle('request');
$request->check('username', 'require');
$request->check('password', 'length', 12);
$request->check('code', 'number');
```

[[file: framework/Request.php](https://github.com/TIGERB/easy-php/blob/master/framework/Request.php)]

[[file: framework/Response.php](https://github.com/TIGERB/easy-php/blob/master/framework/Response.php)]
Expand Down Expand Up @@ -669,3 +677,8 @@ cp ./.git-hooks/* ./git/hooks
- 懒加载优化框架加载流程
- 变更Helper助手类的成员方法为框架函数,简化使用提高生产效率
- 性能测试和优化

- v0.6.9(2017/05/21)
- 提供更友善的开发api帮助
+ 请求参数校验:require/length/number
- 支持mysql主从配置
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Entry file ----> Register autoload function
----> View
```

In addition, unit test, nosql support, api documents and some auxiliary scripts, etc. Finnally, My framework directory as follows:
In addition, unit test, nosql support, api documents and some auxiliary scripts, e.g. Finnally, My framework directory as follows:

# Project Directory Structure

Expand Down Expand Up @@ -114,7 +114,7 @@ vendor [composer vendor directory]
├── pre-commit [git pre-commit example file]
├── commit-msg [git commit-msg example file]
.babelrc [babel config file]
.env [the environment variables file]
.env.example [the environment variables example file]
.gitignore [git ignore config file]
build [build php code to phar file script]
cli [run this framework with the php cli mode]
Expand Down Expand Up @@ -177,6 +177,14 @@ Loading framework-defined and user-defined config files.

All output is json in the framework, neithor framework's core error or business logic's output, beacuse I think is friendly.

# Request param check, Support require/length/number check at present. Use as follows:
```
$request = App::$container->getSingle('request');
$request->check('username', 'require');
$request->check('password', 'length', 12);
$request->check('code', 'number');
```

[[file: framework/Request.php](https://github.com/TIGERB/easy-php/blob/master/framework/Request.php)]

[[file: framework/Response.php](https://github.com/TIGERB/easy-php/blob/master/framework/Response.php)]
Expand Down
2 changes: 2 additions & 0 deletions app/demo/controllers/DbOperationDemo.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function dbFindDemo()
->orderBy('id asc')
->findOne();
$sql = $instance->sql;
$database = $instance->masterSlave;

// return $sql;
return $res;
Expand All @@ -69,6 +70,7 @@ public function dbFindAllDemo()
->limit(5)
->findAll(['id','create_at']);
$sql = $instance->sql;
$database = $instance->masterSlave;

// return $sql;
return $res;
Expand Down
19 changes: 17 additions & 2 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,28 @@
********************************************/

return [
/* 默认配置 */
/* 主库配置 */
'database' => [
'dbtype' => env('database')['dbtype'],
'dbprefix' => env('database')['dbprefix'],
'dbname' => env('database')['dbname'],
'dbhost' => env('database')['dbhost'],
'username' => env('database')['username'],
'password' => env('database')['password']
'password' => env('database')['password'],
'slave' => explode(',', env('database')['slave'])
],
/* 从库0配置 */
'database-slave-0' => [
'dbname' => env('database-slave-0')['dbname'],
'dbhost' => env('database-slave-0')['dbhost'],
'username' => env('database-slave-0')['username'],
'password' => env('database-slave-0')['password'],
],
/* 从库1配置 */
'database-slave-1' => [
'dbname' => env('database-slave-1')['dbname'],
'dbhost' => env('database-slave-1')['dbhost'],
'username' => env('database-slave-1')['username'],
'password' => env('database-slave-1')['password'],
]
];
7 changes: 7 additions & 0 deletions framework/orm/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ class DB
*/
protected $id = '';

/**
* 当前查询主从
*
* @var object
*/
private $masterSlave = '';

/**
* 构造函数
*/
Expand Down
39 changes: 39 additions & 0 deletions framework/orm/db/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ class Mysql
*/
public function __construct()
{

}

/**
* 初始化主库
*/
public function initMaster(DB $db)
{

$config = APP::$container->getSingle('config');
$config = $config->config;
$dbConfig = $config['database'];
Expand All @@ -88,6 +97,30 @@ public function __construct()
$this->username = $dbConfig['username'];
$this->password = $dbConfig['password'];

$db->masterSlave = 'master';
$this->connect();
}

/**
* 初始化从库
*/
public function initSlave(DB $db)
{
$config = APP::$container->getSingle('config');
if (! isset($config->config['database']['slave'])) {
$this->initMaster($db);
return;
}
$slave = $config->config['database']['slave'];
$randSlave = $slave[array_rand($slave)];
$dbConfig = $config->config["database-slave-{$randSlave}"];
$this->dbhost = $dbConfig['dbhost'];
$this->dbname = $dbConfig['dbname'];
$this->dsn = "mysql:dbname={$this->dbname};host={$this->dbhost};";
$this->username = $dbConfig['username'];
$this->password = $dbConfig['password'];

$db->masterSlave = "slave-{$randSlave}";
$this->connect();
}

Expand Down Expand Up @@ -136,6 +169,7 @@ public function __set($name = '', $value = '')
*/
public function findOne(DB $db)
{
$this->initSlave($db);
$this->pdoStatement = $this->pdo->prepare($db->sql);
$this->bindValue($db);
$this->pdoStatement->execute();
Expand All @@ -150,6 +184,7 @@ public function findOne(DB $db)
*/
public function findAll(DB $db)
{
$this->initSlave($db);
$this->pdoStatement = $this->pdo->prepare($db->sql);
$this->bindValue($db);
$this->pdoStatement->execute();
Expand All @@ -164,6 +199,7 @@ public function findAll(DB $db)
*/
public function save(DB $db)
{
$this->initMaster($db);
$this->pdoStatement = $this->pdo->prepare($db->sql);
$this->bindValue($db);
$res = $this->pdoStatement->execute();
Expand All @@ -181,6 +217,7 @@ public function save(DB $db)
*/
public function delete(DB $db)
{
$this->initMaster($db);
$this->pdoStatement = $this->pdo->prepare($db->sql);
$this->bindValue($db);
$this->pdoStatement->execute();
Expand All @@ -195,6 +232,7 @@ public function delete(DB $db)
*/
public function update(DB $db)
{
$this->initMaster($db);
$this->pdoStatement = $this->pdo->prepare($db->sql);
$this->bindValue($db);
return $this->pdoStatement->execute();
Expand All @@ -208,6 +246,7 @@ public function update(DB $db)
*/
public function query(DB $db)
{
$this->initMaster($db);
$res = [];
foreach ($this->pdo->query($db->sql, PDO::FETCH_ASSOC) as $v) {
$res[] = $v;
Expand Down

0 comments on commit 6e9748f

Please sign in to comment.