<commit_before><?php
/**
 * KumbiaPHP web & app Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://wiki.kumbiaphp.com/Licencia
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to <EMAIL> so we can send you a copy immediately.
 *
 * Clase que maneja el pool de conexiones
 * 
 * @category   Kumbia
 * @package    Db 
 * @copyright  Copyright (c) 2005-2009 Kumbia Team (http://www.kumbiaphp.com)
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
 */

/**
 * @see DbBaseInterface
 */
require CORE_PATH . 'libs/db/db_base_interface.php';
/**
 * @see DbBase
 */
require CORE_PATH . 'libs/db/db_base.php';

/**
 * Clase que maneja el pool de conexiones
 *
 */
class Db
{
    /**
     * Singleton de conexiones a base de datos
     *
     * @var array
     **/
    protected static $_connections = array();

    /**
     * Realiza una conexión directa al motor de base de datos
     * usando el driver de Kumbia
     *
     * @param boolean $new nueva conexion
     * @param string $database base de datos a donde conectar
     * @return db
     */
    public static function factory ($database = null, $new = false)
    {
        /**
         * Cargo el mode para mi aplicacion
         **/
        if (! $database) {
            $database = Config::get('config.application.database');
        }
        $databases = Config::read('databases');
        $config = $databases[$database];
        /**
         * Cargo valores por defecto para la conexion
         * en caso de que no existan
         **/
        if (! isset($config['port'])) {
            $config['port'] = 0;
        }
        if (! isset($config['dsn'])) {
            $config['dsn'] = '';
        }
        if (! isset($config['host'])) {
            $config['host'] = '';
        }
        if (! isset($config['username'])) {
            $config['username'] = '';
        }
        if (! isset($config['password'])) {
            $config['password'] = '';
        }
        //Si no es una conexion nueva y existe la conexion singleton
        if (! $new && isset(self::$_connections[$database])) {
            return self::$_connections[$database];
        }
        //Cargo la clase adaptadora necesaria
        if (isset($config['pdo'])) {
            try {
            	$connection = new PDO($config['type'] . ":" . $config['dsn'], $config['username'], $config['password']);
            } catch (PDOException $e) {
                throw new KumbiaException($e->getMessage());
            }
        } else {
            $dbclass = "Db{$config['type']}";
            //if (! class_exists($dbclass)) {
                require_once CORE_PATH . 'libs/db/adapters/' . $config['type'] . '.php';
            //}
            
            if (! class_exists($dbclass)) {
                throw new KumbiaException("No existe la clase $dbclass, necesaria para iniciar el adaptador");
            }
            $connection = new $dbclass($config);
        }

        //Si no es para conexion nueva, la cargo en el singleton
        if (! $new) {
            self::$_connections[$database] = $connection;
        }
        return $connection;
    }
}<commit_msg>Limpieza y mejora de velocidad en la clase Db. Posiblemente podriamos crear otro singleton para los adaptadores de db.<commit_after><?php
/**
 * KumbiaPHP web & app Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://wiki.kumbiaphp.com/Licencia
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to <EMAIL>@kumbiaphp.com so we can send you a copy immediately.
 *
 * Clase que maneja el pool de conexiones
 * 
 * @category   Kumbia
 * @package    Db 
 * @copyright  Copyright (c) 2005-2010 Kumbia Team (http://www.kumbiaphp.com)
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
 */

/**
 * @see DbBaseInterface
 */
require CORE_PATH . 'libs/db/db_base_interface.php';
/**
 * @see DbBase
 */
require CORE_PATH . 'libs/db/db_base.php';

/**
 * Clase que maneja el pool de conexiones
 *
 */
class Db
{
    /**
     * Singleton de conexiones a base de datos
     *
     * @var array
     **/
    protected static $_connections = array();

    /**
     * Devuelve la conexión, si no existe llama a Db::connect para crearla
     *
     * @param boolean $new nueva conexion //TODO mirar si es necesaria
     * @param string $database base de datos a donde conectar
     * @return db
     */
    public static function factory ($database = null, $new = false)
    {
        /**
         * Cargo el mode para mi aplicacion
         **/
        if (! $database) {
            $database = Config::get('config.application.database');
        }
	//Si no es una conexion nueva y existe la conexion singleton
        if (isset(self::$_connections[$database])) {
	    return self::$_connections[$database];
        }
	
	return self::connect($database);
    }

    /**
     * Realiza una conexión directa al motor de base de datos
     * usando el driver de Kumbia
     *
     * @param string $database base de datos a donde conectar
     * @return db
     */
    private static function connect($database)
    {
        $databases = Config::read('databases');
        $config = $databases[$database];

	// carga los valores por defecto para la conexión, si no existen
	$default = array ('port' => 0, 'dsn' => NULL, 'dbname' => NULL, 'host' => 'localhost', 'username' => NULL, 'password' => <PASSWORD>);
	$config = $config + $default;

        //Cargo la clase adaptadora necesaria
        if (isset($config['pdo'])) {
            try {
            	$connection = new PDO($config['type'] . ":" . $config['dsn'], $config['username'], $config['password']);
            } catch (PDOException $e) {
                throw new KumbiaException($e->getMessage());
            }
        } else {
            $dbclass = "Db{$config['type']}";
            if (! include_once CORE_PATH . 'libs/db/adapters/' . $config['type'] . '.php'){
		throw new KumbiaException("No existe la clase $dbclass, necesaria para iniciar el adaptador");
	    }
        }

        return new $dbclass($config);
    }
}
{'fraction_non_alphanumeric': 0.13110224132637396, 'fraction_numerical': 0.00276327909118821, 'mean_word_length': 2.217283950617284, 'pattern_counts': {'":': 2, '<': 8, '<?xml version=': 0, '>': 14, 'https://': 0, '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': '6675490', 'n_tokens_mistral': 2003, 'n_tokens_neox': 1938, 'n_words': 603}