Classes

File io/db/factory/DBFactory.class.php

File io/db/factory/DBFactory.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 
<?php
/**
 * This factory provides the <code>DBConnection</code> and the <code>SQLQuerier</code>
 * for the right sgbd.
 * @package     IO
 * @subpackage  DB\factory
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Loic ROUCHON <horn@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2014 12 22
 * @since       PHPBoost 3.0 - 2009 10 01
*/

class DBFactory
{
    const MYSQL = 0x01;
    const PDO_MYSQL = 0x11;
    const PDO_SQLITE = 0x12;
    const PDO_POSTGRESQL = 0x13;

    /**
     * @var DBConnection
     */
    private static $db_connection;

    /**
     * @var DBMSFactory
     */
    private static $factory;

    private static $config_file;

    public static function __static()
    {
        self::$config_file = '/kernel/db/config.php';
    }

    public static function load_prefix()
    {
        @include_once(PATH_TO_ROOT . self::$config_file);
    }

    public static function init_factory($dbms)
    {
        require_once(PATH_TO_ROOT . '/kernel/db/tables.php');
        switch ($dbms)
        {
            case self::PDO_MYSQL:
                self::$factory = new PDOMySQLDBFactory();
                break;
            case self::MYSQL:
            default:
                self::$factory = new MySQLDBFactory();
                break;
        }
    }

    /**
     * returns the currently opened <code>DBConnection</code> instance or if none,
     * creates a new one
     * @return DBConnection the currently opened <code>DBConnection</code> instance
     */
    public static function get_db_connection()
    {
        if (self::$db_connection === null)
        {
            $data = self::load_config();
            self::init_factory($data['dbms']);
            self::$db_connection = self::new_db_connection();
            self::$db_connection->connect($data);
        }
        return self::$db_connection;
    }

    public static function close_db_connection()
    {
        if (self::$db_connection != null)
        {
            self::$db_connection->disconnect();
        }
    }

    public static function reset_db_connection()
    {
        self::$db_connection = null;
    }

    public static function set_db_connection(DBConnection $connection)
    {
        self::$db_connection = $connection;
    }

    /**
     * returns a new <code>DBConnection</code> instance
     * @return SQLQuerier a new <code>DBConnection</code> instance
     */
    public static function new_db_connection()
    {
        return self::get_factory()->new_db_connection();
    }

    /**
     * returns a new <code>SQLQuerier</code> instance
     * @param DBConnection $db_connection the db connection that the <code>SQLQuerier</code> will use
     * @return SQLQuerier a new <code>SQLQuerier</code> instance
     */
    public static function new_sql_querier(DBConnection $db_connection)
    {
        return self::get_factory()->new_sql_querier($db_connection);
    }

    public static function new_dbms_util(SQLQuerier $querier, $dbms_type = null)
    {
        return self::get_factory()->new_dbms_util($querier);
    }

    private static function load_config()
    {
        if (file_exists(PATH_TO_ROOT . self::$config_file))
        {
            include PATH_TO_ROOT . self::$config_file;
            if (defined('PHPBOOST_INSTALLED'))
            {
                return $db_connection_data;
            }
        }
        throw new PHPBoostNotInstalledException();
    }

    /**
     * @return DBMSFactory
     */
    private static function get_factory()
    {
        return self::$factory;
    }
}
?>