<?php namespace Config;
/**
 * {{www.xunruicms.com}}
 * {{迅睿内容管理框架系统}}
 * 本文件是框架系统文件，二次开发时不可以修改本文件
 **/

use CodeIgniter\Database\Config;

/**
 * Database Configuration
 */ 
class Database extends Config
{
    /**
     * The directory that holds the Migrations
     * and Seeds directories.
     * @var string
     */
    public  $filesPath = WRITEPATH.'database/';

    /**
     * Lets you choose which connection group to
     * use if no other is specified.
     *
     * @var string
     */
    public  $defaultGroup = 'default';

    /**
     * The default database connection.
     *
     * @var array
     */
    public  $default = [
        'DSN'          => '',
        'hostname'     => 'localhost',
        'username'     => '',
        'password'     => '',
        'database'     => '',
        'DBDriver'     => 'MySQLi',
        'DBPrefix'     => '',
        'pConnect'     => false,
        'DBDebug'     => true,
        'cacheOn'     => true,
        'cacheDir'     => WRITEPATH.'database/',
        'charset'      => 'utf8mb4',
        'DBCollat'     => 'utf8mb4_general_ci',
        'swapPre'      => '',
        'encrypt'      => false,
        'compress'     => false,
        'strictOn'     => false,
        'foreignKeys'   => true,
        'numberNative' => false,
        'failover'     => []
    ];
    public  $db1 = [];
    public  $db2 = [];
    public  $db3 = [];
    public  $db4 = [];
    public  $db5 = [];
    public  $db6 = [];
    public  $db7 = [];
    public  $db8 = [];
    public  $db9 = [];

    private $mykey = 1;
    private $mydb = [];

    //--------------------------------------------------------------------

    public function __construct()
    {
        parent::__construct();

        if (!is_file(CONFIGPATH.'database.php')) {
            return $this;
        }

        $db = [];
        require CONFIGPATH.'database.php';

        if (isset($db['failover']) && $db['failover']) {
            // 备用库
            $this->default['failover'] = $db['failover'];
            unset($db['failover']);
        }

        foreach ($this->default as $p => $t) {
            foreach ($db as $name => $v) {
                if (isset($this->$name)) {
                    // 默认库
                    $this->$name[$p] = isset($v[$p]) ? $v[$p] : $t;
                } else {
                    // 自定义库
                    $key = $this->_get_key($name);
                    if ($key) {
                        $this->$key[$p] = isset($v[$p]) ? $v[$p] : $t;
                    }

                }
            }
        }

        // 判断数据库名称的规范性（SQLite3 为文件路径，允许 .db 等扩展名）
        $driver = isset($this->default['DBDriver']) ? (string) $this->default['DBDriver'] : 'MySQLi';
        if (strcasecmp($driver, 'SQLite3') !== 0) {
            if (is_numeric($this->default['database'])) {
                exit('数据库名称不能是数字');
            } elseif (strpos((string) $this->default['database'], '.') !== false) {
                exit('数据库名称不能存在.号');
            }
        } else {
            // SQLite 默认等待写锁，降低 database is locked 概率
            if (!isset($this->default['busyTimeout'])) {
                $this->default['busyTimeout'] = 10000;
            }
        }

    }

    private function _get_key($name) {
        if (isset($this->mydb[$name])) {
            return $this->mydb[$name];
        } else {
            $this->mydb[$name] = 'db'.$this->mykey;
            $this->mykey++;
        }
        return $this->mydb[$name];
    }

    public function get_group($name) {
        if (isset($this->mydb[$name])) {
            return $this->mydb[$name];
        }
        return 'default';
    }

    //--------------------------------------------------------------------

}

