<commit_before># Master Slave DBAccess

[![Latest Stable Version](https://poser.pugx.org/indievox/master-slave-db-access/v/stable)](https://packagist.org/packages/indievox/master-slave-db-access)
[![Build Status](https://travis-ci.org/indievox-inc/MasterSlaveDBAccess.svg?branch=master)](https://travis-ci.org/indievox-inc/MasterSlaveDBAccess)
[![codecov.io](https://codecov.io/github/indievox-inc/MasterSlaveDBAccess/coverage.svg?branch=master)](https://codecov.io/github/indievox-inc/MasterSlaveDBAccess?branch=master)
[![Codacy Badge](https://api.codacy.com/project/badge/grade/a4789015127043baa6d5636af6964809)](https://www.codacy.com/app/hub/MasterSlaveDBAccess)

簡易的 MySQL database 伺服器存取類別，使用 POD 及 singleton pattern 實作，支援 Master-Slave 架構。

Simple MySQL database server access class use PDO and singleton pattern, support Master-Slave database cluster.

# Usage

## Install by composer

```php
composer require indievox/master-slave-db-access:dev-master
```

## Include auto load

```php
require_once "/path/to/your/vendor/autoload.php";
use iNDIEVOX\MasterSlaveDBAccess\MasterSlaveDBAccess;
```

## Config your database connections

Here's the example of database connection config

```php

$db_config = array(
    "database_server" => array(
        "master"=>array(
            "db_host"=>'localhost',
            "db_name"=>'homestead',
            "db_user"=>'root',
            "db_password"=>''
        ),
        "slave1"=>array(
            "db_host"=>'localhost',
            "db_name"=>'homestead',
            "db_user"=>'root',
            "db_password"=>''
        ),
        "slave2"=>array(
            "db_host"=>'localhost',
            "db_name"=>'homestead',
            "db_user"=>'root',
            "db_password"=>'',
        )
    ),
    "slave_database_name" => array(
        'slave1',
        'slave2'
    )
);

```

## Get database access instance

```php

$db_obj = MasterSlaveDBAccess::getInstance($db_config);

```

## Use database access instance to excute query

```php

$select_sql = "SELECT id, email FROM user WHERE id=:id ";
$param = array(
    ":id" => '1'
);
$query_result = $db_obj->selectCommand($select_sql, $param);

foreach ($query_result as $query_result_data) {
    echo $query_result_data["id"];
    echo $query_result_data["email"];
}

```

## Force read master database when you need

```php

MasterSlaveDBAccess::forceSwitchMaster();
$select_sql = "SELECT id, email FROM user WHERE id=:id ";
$param = array(
    ":id" => '1'
);
$query_result = $db_obj->selectCommand($select_sql, $param);

foreach ($query_result as $query_result_data) {
    echo $query_result_data["id"];
    echo $query_result_data["email"];
}

```

## Use "right" method to excute query, the class will auto switch to right read/write connection to process the query

```php

// init conneciton, use slave(read) connection
$db_obj = MasterSlaveDBAccess::getInstance($db_config);

$insert_sql = "INSERT INTO user ".
    "(id, path, is_deleted, create_time, modify_time, delete_time) ".
    "VALUES ".
    "(:id, :path, :is_deleted, :create_time, :modify_time, :delete_time);";

$param = array(
    ":id"           => '1',
    ":path"         => 'fukuball',<commit_msg>add some comments to config file<commit_after># Master Slave DBAccess

[![Latest Stable Version](https://poser.pugx.org/indievox/master-slave-db-access/v/stable)](https://packagist.org/packages/indievox/master-slave-db-access)
[![Build Status](https://travis-ci.org/indievox-inc/MasterSlaveDBAccess.svg?branch=master)](https://travis-ci.org/indievox-inc/MasterSlaveDBAccess)
[![codecov.io](https://codecov.io/github/indievox-inc/MasterSlaveDBAccess/coverage.svg?branch=master)](https://codecov.io/github/indievox-inc/MasterSlaveDBAccess?branch=master)
[![Codacy Badge](https://api.codacy.com/project/badge/grade/a4789015127043baa6d5636af6964809)](https://www.codacy.com/app/hub/MasterSlaveDBAccess)

簡易的 MySQL database 伺服器存取類別，使用 POD 及 singleton pattern 實作，支援 Master-Slave 架構。

Simple MySQL database server access class use PDO and singleton pattern, support Master-Slave database cluster.

# Usage

## Install by composer

```php
composer require indievox/master-slave-db-access:dev-master
```

## Include auto load

```php
require_once "/path/to/your/vendor/autoload.php";
use iNDIEVOX\MasterSlaveDBAccess\MasterSlaveDBAccess;
```

## Config your database connections

Here's the example of database connection config

```php

$db_config = array(
    "database_server" => array(
        "master"=>array(
            "db_host"=>'localhost', // change to your master host
            "db_name"=>'homestead', // change to your master database name
            "db_user"=>'root',      // change to your master database username
            "db_password"=>''       // change to your master database password
        ),
        "slave1"=>array(
            "db_host"=>'localhost', // change to your slave host
            "db_name"=>'homestead', // change to your slave database name
            "db_user"=>'root',      // change to your slave database username
            "db_password"=>''       // change to your slave database password
        ),
        "slave2"=>array(
            "db_host"=>'localhost', // change to your slave host
            "db_name"=>'homestead', // change to your slave database name
            "db_user"=>'root',      // change to your slave database username
            "db_password"=>''       // change to your slave database password
        )
    ),
    "slave_database_name" => array(
        'slave1',
        'slave2'
    )
);

```

## Get database access instance

```php

$db_obj = MasterSlaveDBAccess::getInstance($db_config);

```

## Use database access instance to excute query

```php

$select_sql = "SELECT id, email FROM user WHERE id=:id ";
$param = array(
    ":id" => '1'
);
$query_result = $db_obj->selectCommand($select_sql, $param);

foreach ($query_result as $query_result_data) {
    echo $query_result_data["id"];
    echo $query_result_data["email"];
}

```

## Force read master database when you need

```php

MasterSlaveDBAccess::forceSwitchMaster();
$select_sql = "SELECT id, email FROM user WHERE id=:id ";
$param = array(
    ":id" => '1'
);
$query_result = $db_obj->selectCommand($select_sql, $param);

foreach ($query_result as $query_result_data) {
    echo $query_result_data["id"];
    echo $query_result_data["email"];
}

```

## Use "right" method to excute query, the class will auto switch to right read/write connection to process the query

```php
use iNDIEVOX\MasterSlaveDBAccess\MasterSlaveDBAccess;

// init conneciton, use slave(read) connection
$db_obj = MasterSlaveDBAccess::getInstance($db_config);

$insert_sql = "INSERT INTO user ".
    "(id, path, is_deleted, create_time, modify_time, delete_time) ".
    "VALUES ".
    "(:id, :path, :is_deleted, :create_time, :modify_time, :delete_time);";

$param = array(
    ":id"           => '1',
    ":path"         => 'fukuball',
{'fraction_non_alphanumeric': 0.1499712147380541, 'fraction_numerical': 0.009211283822682787, 'mean_word_length': 3.6952702702702704, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 49, 'https://': 16, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1234051', 'n_tokens_mistral': 2379, 'n_tokens_neox': 2333, 'n_words': 493}