<commit_before><?php
/**
 * PHPFrame/Database/Database.php
 * 
 * PHP version 5
 * 
 * @category  PHPFrame
 * @package   Database
 * @author    <NAME> <<EMAIL>>
 * @copyright 2009 E-noise.com Limited
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version   SVN: $Id$
 * @link      http://code.google.com/p/phpframe/source/browse/#svn/PHPFrame
 * @since     1.0
 */

/**
 * Abstract Database class
 * 
 * This class deals with the connection(s) to the database(s).
 * 
 * The database class serves singleton objects for each connection, determined 
 * by the dsn and db user credentials.
 * 
 * This class also extends PHPFrame_Subject allowing observer objects to 
 * subscribe for updates.
 * 
 * @category PHPFrame
 * @package  Database
 * @author   <NAME> <<EMAIL>>
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @link     http://code.google.com/p/phpframe/source/browse/#svn/PHPFrame
 * @see      PHPFrame_Subject
 * @since    1.0
 * @abstract
 */
abstract class PHPFrame_Database extends PHPFrame_Subject
{
    /**
     * Return query() result as single primitive value
     * 
     * @var int
     */
    const FETCH_COLUMN=0;
    /**
     * Return query() result as list of single primitive values
     * 
     * @var int
     */
    const FETCH_COLUMN_LIST=1;
    /**
     * Return query() result as numerically indexed array
     * 
     * @var int
     */
    const FETCH_ARRAY=2;
    /**
     * Return query() result as a list of numerically indexed arrays
     * 
     * @var int
     */
    const FETCH_ARRAY_LIST=3;
    /**
     * Return query() result as associative array
     * 
     * @var int
     */
    const FETCH_ASSOC=4;
    /**
     * Return query() result as a list of associative arrays
     * 
     * @var int
     */
    const FETCH_ASSOC_LIST=5;
    /**
     * Return query() result as object
     * 
     * @var int
     */
    const FETCH_OBJ=6;
    /**
     * Return query() result as a list of objects
     * 
     * @var int
     */
    const FETCH_OBJ_LIST=7;
    /**
     * Return query() result as number of rows returned
     * 
     * @var int
     */
    const FETCH_NUM_ROWS=8;
    /**
     * Return query() result as number of records affected
     * 
     * @var int
     */
    const FETCH_AFFECTED_ROWS=9;
    /**
     * Return query() result as last insert id
     * 
     * @var int
     */
    const FETCH_LAST_INSERTID=10;
    /**
     * Return PDOStatement object from query() method
     * 
     * @var int
     */
    const FETCH_STMT=11;
    
    /**
     * An array holding instances of this class
     * 
     * @var array containing objects of type PHPFrame_Database
     */
    private static $_instances=array();
    /**
     * DSN used for the database connection
     * 
     * @var string
     */
    private $_dsn=null;
    /**
     * Database user if any
     * 
     * @var string
     */
    private $_db_user=null;
    /**
     * Database password if any
     * 
     * @var string
     */
    private $_db_pass=null;
    /**
     * A PDO object that represents the connection to the database server.
     * 
     * @var PDO
     */
    private $_pdo=null;
    /**
     * A reference to the PDOStatement object from the last query
     * 
     * @var PDOStatement
     */
    private $_stmt=null;
    
    /**
     * Constructor
     * 
     * The constructor connects to the MySQL server and selects the database.
     * 
     * @param string $dsn     A database source name
     * @param string $db_user The database user name if any.
     * @param string $db_pass The database password if any.
     * 
     * @access private
     * @return void
     * @since  1.0
     */
    private function __construct($dsn, $db_user=null, $db_pass=null)
    {
        // Set internal properties
        $this->_dsn     = $dsn;
        $this->_db_user = $db_user;
        $this->_db_pass = $db_pass;
        
        // Connect to database server
        $this->connect();
    }
    
    /**
     * Magic method invoked when an instance is serialized.
     * 
     * @return array
     * @since  1.0
     */
    public function __sleep()
    {
        // Disconnect from database server by unsetting PDO object
        $this->_pdo = null;
        
        // Remove reference to PDOStatement object 
        $this->_stmt = null;
        
        return array_keys(get_object_vars($this));
    }
    
    /**
     * Magic method invoked when a new instance is unserialized
     * 
     * @return void
     * @since  1.0
     */
    public function __wakeup()
    {   
        // Re-connect to database server
        $this->connect();
    }
    
    /**
     * Get Instance
     * 
     * @param string $dsn     A database source name.
     * @param string $db_user The database user name if any.
     * @param string $db_pass The database password if any.
     * 
     * @return PHPFrame_Database
     */
    public static function getInstance($dsn, $db_user=null, $db_pass=null)
    {
        $dsn = trim((string) $dsn);
        
        if (!is_null($db_user)) {
            $key = $dsn.";user=".$db_user;
        }
        
        if (preg_match('/^(mysql|sqlite)/i', $dsn, $matches)) {
        	$driver = strtolower($matches[1]);
            switch ($driver) {
            	case "sqlite" : 
            		$concrete_class = "PHPFrame_SQLiteDatabase";
            		break;
            	case "mysql" : 
            		$concrete_class = "PHPFrame_MySQLDatabase";
                    break;
            }
        } else {
        	$msg = "Database driver not recognised.";
            throw new PHPFrame_DatabaseException($msg);
        }
        
        if (!isset(self::$_instances[$key])) {
            self::$_instances[$key] = new $concrete_class(
                $dsn, 
                $db_user, 
                $db_pass
            );
        }
        
        return self::$_instances[$key];
    }
    
    /**
     * Create PDO object to represent db connection
     * <commit_msg>Fixed minor bug in abstract Database class.<commit_after><?php
/**
 * PHPFrame/Database/Database.php
 * 
 * PHP version 5
 * 
 * @category  PHPFrame
 * @package   Database
 * @author    <NAME> <<EMAIL>>
 * @copyright 2009 E-noise.com Limited
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version   SVN: $Id$
 * @link      http://code.google.com/p/phpframe/source/browse/#svn/PHPFrame
 * @since     1.0
 */

/**
 * Abstract Database class
 * 
 * This class deals with the connection(s) to the database(s).
 * 
 * The database class serves singleton objects for each connection, determined 
 * by the dsn and db user credentials.
 * 
 * This class also extends PHPFrame_Subject allowing observer objects to 
 * subscribe for updates.
 * 
 * @category PHPFrame
 * @package  Database
 * @author   <NAME> <<EMAIL>>
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @link     http://code.google.com/p/phpframe/source/browse/#svn/PHPFrame
 * @see      PHPFrame_Subject
 * @since    1.0
 * @abstract
 */
abstract class PHPFrame_Database extends PHPFrame_Subject
{
    /**
     * Return query() result as single primitive value
     * 
     * @var int
     */
    const FETCH_COLUMN=0;
    /**
     * Return query() result as list of single primitive values
     * 
     * @var int
     */
    const FETCH_COLUMN_LIST=1;
    /**
     * Return query() result as numerically indexed array
     * 
     * @var int
     */
    const FETCH_ARRAY=2;
    /**
     * Return query() result as a list of numerically indexed arrays
     * 
     * @var int
     */
    const FETCH_ARRAY_LIST=3;
    /**
     * Return query() result as associative array
     * 
     * @var int
     */
    const FETCH_ASSOC=4;
    /**
     * Return query() result as a list of associative arrays
     * 
     * @var int
     */
    const FETCH_ASSOC_LIST=5;
    /**
     * Return query() result as object
     * 
     * @var int
     */
    const FETCH_OBJ=6;
    /**
     * Return query() result as a list of objects
     * 
     * @var int
     */
    const FETCH_OBJ_LIST=7;
    /**
     * Return query() result as number of rows returned
     * 
     * @var int
     */
    const FETCH_NUM_ROWS=8;
    /**
     * Return query() result as number of records affected
     * 
     * @var int
     */
    const FETCH_AFFECTED_ROWS=9;
    /**
     * Return query() result as last insert id
     * 
     * @var int
     */
    const FETCH_LAST_INSERTID=10;
    /**
     * Return PDOStatement object from query() method
     * 
     * @var int
     */
    const FETCH_STMT=11;
    
    /**
     * An array holding instances of this class
     * 
     * @var array containing objects of type PHPFrame_Database
     */
    private static $_instances=array();
    /**
     * DSN used for the database connection
     * 
     * @var string
     */
    private $_dsn=null;
    /**
     * Database user if any
     * 
     * @var string
     */
    private $_db_user=null;
    /**
     * Database password if any
     * 
     * @var string
     */
    private $_db_pass=null;
    /**
     * A PDO object that represents the connection to the database server.
     * 
     * @var PDO
     */
    private $_pdo=null;
    /**
     * A reference to the PDOStatement object from the last query
     * 
     * @var PDOStatement
     */
    private $_stmt=null;
    
    /**
     * Constructor
     * 
     * The constructor connects to the MySQL server and selects the database.
     * 
     * @param string $dsn     A database source name
     * @param string $db_user The database user name if any.
     * @param string $db_pass The database password if any.
     * 
     * @access private
     * @return void
     * @since  1.0
     */
    private function __construct($dsn, $db_user=null, $db_pass=null)
    {
        // Set internal properties
        $this->_dsn     = $dsn;
        $this->_db_user = $db_user;
        $this->_db_pass = $db_pass;
        
        // Connect to database server
        $this->connect();
    }
    
    /**
     * Magic method invoked when an instance is serialized.
     * 
     * @return array
     * @since  1.0
     */
    public function __sleep()
    {
        // Disconnect from database server by unsetting PDO object
        $this->_pdo = null;
        
        // Remove reference to PDOStatement object 
        $this->_stmt = null;
        
        return array_keys(get_object_vars($this));
    }
    
    /**
     * Magic method invoked when a new instance is unserialized
     * 
     * @return void
     * @since  1.0
     */
    public function __wakeup()
    {   
        // Re-connect to database server
        $this->connect();
    }
    
    /**
     * Get Instance
     * 
     * @param string $dsn     A database source name.
     * @param string $db_user The database user name if any.
     * @param string $db_pass The database password if any.
     * 
     * @return PHPFrame_Database
     */
    public static function getInstance($dsn, $db_user=null, $db_pass=null)
    {
        $dsn = trim((string) $dsn);
        
        if (!is_null($db_user)) {
            $dsn .= ";user=".$db_user;
        }
        
        if (preg_match('/^(mysql|sqlite)/i', $dsn, $matches)) {
        	$driver = strtolower($matches[1]);
            switch ($driver) {
            	case "sqlite" : 
            		$concrete_class = "PHPFrame_SQLiteDatabase";
            		break;
            	case "mysql" : 
            		$concrete_class = "PHPFrame_MySQLDatabase";
                    break;
            }
        } else {
        	$msg = "Database driver not recognised.";
            throw new PHPFrame_DatabaseException($msg);
        }
        
        if (!isset(self::$_instances[$dsn])) {
            self::$_instances[$dsn] = new $concrete_class(
                $dsn, 
                $db_user, 
                $db_pass
            );
        }
        
        return self::$_instances[$dsn];
    }
    
    /**
     * Create PDO object to represent db connection
     * 
{'fraction_non_alphanumeric': 0.10886495443247722, 'fraction_numerical': 0.004971002485501243, 'mean_word_length': 1.75845521023766, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '5982392', 'n_tokens_mistral': 3755, 'n_tokens_neox': 3628, 'n_words': 1014}