*      db      The database namespace defined in $lc_databases of config.php [default: "default"]
 *
 * @package     PHPLucidFrame\Console
 * @since       PHPLucidFrame v 1.14.0
 * @copyright   Copyright (c), PHPLucidFrame.
 * @author      Sithu K. <*****@*****.**>
 * @link        http://phplucidframe.com
 * @license     http://www.opensource.org/licenses/mit-license.php MIT License
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE
 */
use LucidFrame\Core\SchemaManager;
_consoleCommand('schema:load')->setDescription('Process the schema and import the database')->addArgument('db', 'The database namespace defined in $lc_databases of config.php', 'default')->setDefinition(function (\LucidFrame\Console\Command $cmd) {
    $db = $cmd->getArgument('db');
    $schema = _schema($db);
    if ($schema === null) {
        _writeln('Failed to load schema.');
    } elseif ($schema === false) {
        _writeln('Unable to find the schema file "%s".', DB . 'schema.' . $db . '.php');
    } else {
        if ($cmd->confirm('The database will be purged. Type "y" or "yes" to continue:')) {
            $sm = new SchemaManager($schema);
            if ($sm->import($db)) {
                _writeln('Schema is successfully loaded. The database for "%s" has been imported.', $db);
            } else {
                _writeln('No schema is loaded.');
            }
        } else {
            _writeln('Aborted.');
        }
/**
 * Establish a new database connection to the MySQL server
 * @param string $namespace Namespace of the configuration.
 */
function db_connect($namespace = 'default')
{
    global $_conn;
    global $_DB;
    $conf = db_config($namespace);
    # Connection
    $_conn = mysqli_connect($conf['host'], $conf['username'], $conf['password']);
    if (!$_conn) {
        die('Not connected mysqli!');
    }
    # Force MySQL to use the UTF-8 character set. Also set the collation, if a certain one has been set;
    # otherwise, MySQL defaults to 'utf8_general_ci' # for UTF-8.
    if (!db_setCharset('utf8')) {
        printf("Error loading character set utf8: %s", mysqli_error($_conn));
    }
    if (db_collation()) {
        db_query('SET NAMES utf8 COLLATE ' . db_collation());
    }
    # Select DB
    if (!mysqli_select_db($_conn, $conf['database'])) {
        die('Can\'t use  : ' . $conf['database'] . ' - ' . mysqli_error($_conn));
    }
    $_DB = new stdClass();
    $_DB->name = $conf['database'];
    $_DB->namespace = $namespace;
    # Load the schema of the currently connected database
    $schema = _schema($namespace, true);
    $_DB->schemaManager = new SchemaManager($schema);
    if (!$_DB->schemaManager->isLoaded()) {
        $_DB->schemaManager->build($namespace);
    }
}