## Mysql工具库

### 主从模式请使用 MysqlBase，分库分表模式请使用 MysqlClusterBase

### MysqlBase 主从模式示例
``` php
<?php
namespace App\Models\demo\mysql;

use Api\PhpUtils\Mysql\MysqlBase;

class TestMysql extends MysqlBase
{
    const TABLE_NAME = 'demo'; //表名，必填
    const CONFIG_INDEX = 'test'; //yaconf中的index索引，必填

    public static function getBook1()
    {
        return self::get('*',[
            'id' => 1
        ]);
    }

    public static function insertBook()
    {
        return self::insert([
            'name' => 'bbb',
        ]);
    }

    public static function insertManyBook()
    {
        return self::insert([
            ['name' => 'bbb'],
            ['name' => 'ccc'],
        ]);
    }

    public static function updateBook()
    {
        return self::update([
            'name' => 'abc',
        ], ['id'=> 1]);
    }

    public static function countBook()
    {
        return self::count();
    }

    public static function debugBook()
    {
        return self::debug();
    }
    public static function logBook()
    {
        return self::log();
    }
    public static function lastBook()
    {
        return self::last();
    }
    public static function errorBook()
    {
        return self::error();
    }

    public static function actionBook()
    {
        return self::debug();
    }

    public static function transBook()
    {
        $t = self::beginTransaction();
        var_dump($t);
        $t1 = self::insert([
            'name' => 'ccc',
        ]);

        $t2 = self::update([
            // 'name' => 'abc',
            'name' => 'abcdefg',
        ], ['id'=> 1]);

        var_dump([$t1, $t2]);
        if($t1 && $t2) {
            return self::commit();
        }else { 
            return self::rollback();
        }
    }
}

```

## MysqlClusterBase 主从分片模式
``` php
<?php
namespace App\Models\demo\mysql;

use Api\PhpUtils\Mysql\MysqlClusterBase;

class TestClusterMysql extends MysqlClusterBase
{
    const TABLE_NAME = 'demo_cluster'; //表名，必填
    const CONFIG_INDEX = 'orderIndex'; //yaconf中的index索引，必填
    const SHARDING_DATABASE_COLUMN = 'order_id'; // 数据库分片字段
    const SHARDING_DATABASE_ALGORITHM = 'mod(-7,-5)'; // 数据库分片算法策略
    const SHARDING_DATABASE_COMPAT_COLUMN = 'user_id'; // 兼容数据库分片字段
    const SHARDING_DATABASE_COMPAT_ALGORITHM = 'mod(-2)'; // 兼容数据库分片算法表达式

    public static function getBook()
    {
        return self::getMaster('*',[
            'order_id' => 2105111104548610002,
            'user_id' => 0,
            // 'id' => 1234567890
        ]);
    }

    public static function selectBook()
    {
        return self::selectMaster('*',[
            'order_id' => 2105111104548610002,
            'user_id' => 12386,
            // 'id' => 1234567890
        ]);
    }

    public static function getBook1()
    {
        return self::get('*',[
            'id' => 2
        ]);
    }

    public static function insertBook()
    {
        return self::insert([
            'order_id' => 2105111104548610002,
            'user_id' => 12386,
            'name' => 'bbb',
        ]);
    }

    public static function insertManyBook()
    {
        return self::insert([
            [
            'order_id' => 2105111104548610002,
            'user_id' => 12386,
            'name' => 'aaa',
            ],
            [
            'order_id' => 2105111104548610002,
            'user_id' => 12386,
            'name' => 'bbb',
            ],
            [
            'order_id' => 2105111104548610002,
            'user_id' => 12386,
            'name' => 'ccc',
            ]
        ]);
    }

    public static function updateBook()
    {
        return self::update([
            'name' => 'eee',
        ], 
        [
            'order_id'=> 2105111104548610002,
            'user_id'=> 12386,
        ]);
    }

    public static function countBook()
    {
        return self::count([
            'order_id' => 2105111104548610002,
            'user_id' => 12386,
            // 'id' => 1234567890
        ]);
    }

    public static function debugBook()
    {
        return self::debug();
    }
    public static function logBook()
    {
        return self::log();
    }
    public static function lastBook()
    {
        return self::last();
    }

    public static function actionBook()
    {
        return self::debug();
    }
    public static function errorBook()
    {
        return self::error();
    }
    public static function transBook()
    {
        $t = self::beginTransaction(2105111104548610002);
        var_dump($t);
        $t1 = self::insert([
            'name' => 'aaa',
            'order_id' => 2105111104548610002
        ]);

        $t2 = self::update([
            // 'name' => 'abc',
            'name' => 'abcdefg',
        ], ['order_id'=> 2105111104548610002]);

        var_dump([$t1, $t2]);
        if($t1 && $t2) {
            echo 'commit';
            return self::commit();
        }else { 
            echo 'rollback';
            return self::rollback();
        }
    }

}
```
