2012-06-13 13:49:10 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-08-04 16:45:24 -04:00
|
|
|
* meta
|
2012-06-13 13:49:10 -04:00
|
|
|
*
|
2015-08-04 16:45:24 -04:00
|
|
|
* Simple hierarchial data management
|
2012-06-13 13:49:10 -04:00
|
|
|
*
|
2015-08-04 16:45:24 -04:00
|
|
|
* @package meta
|
2012-06-13 13:49:10 -04:00
|
|
|
* @author Timothy J. Warren
|
2015-08-04 16:45:24 -04:00
|
|
|
* @copyright Copyright (c) 2012
|
|
|
|
* @link https://github.com/aviat4ion/meta
|
2012-07-12 10:31:08 -04:00
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
2012-06-13 13:49:10 -04:00
|
|
|
*/
|
2012-07-12 10:31:08 -04:00
|
|
|
|
2012-06-13 13:49:10 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2015-08-04 16:45:24 -04:00
|
|
|
namespace Meta\Base;
|
2012-07-12 10:31:08 -04:00
|
|
|
|
2012-06-13 13:49:10 -04:00
|
|
|
/**
|
|
|
|
* Extend PHP's PDO class to add some more functionality
|
|
|
|
*
|
|
|
|
*/
|
2015-06-04 14:30:42 -04:00
|
|
|
class db extends \Query\Query_Builder {
|
2012-06-13 13:49:10 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DB connection instances
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $instance = array();
|
2012-07-12 10:31:08 -04:00
|
|
|
|
2012-06-13 13:49:10 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-07-12 10:31:08 -04:00
|
|
|
|
2012-06-13 13:49:10 -04:00
|
|
|
/**
|
|
|
|
* Indexed singleton method
|
|
|
|
*
|
|
|
|
* @param string $dbname
|
|
|
|
* @param array $options
|
|
|
|
* @return DB
|
|
|
|
*/
|
|
|
|
public static function &get_instance($dbname="default", array $options=array())
|
|
|
|
{
|
|
|
|
if ( ! isset(self::$instance[$dbname]))
|
|
|
|
{
|
|
|
|
// Include the database config file
|
|
|
|
require_once(MM_APP_PATH.'config/db.php');
|
2012-07-12 10:31:08 -04:00
|
|
|
|
2012-06-13 13:49:10 -04:00
|
|
|
// Get the correct database in the config file
|
|
|
|
if ( ! is_like_array($db_conf[$dbname]))
|
|
|
|
{
|
|
|
|
// Apparently the database doesn't exist
|
|
|
|
$this->get_last_error();
|
|
|
|
trigger_error("Database does not exist", E_USER_ERROR);
|
|
|
|
die();
|
|
|
|
}
|
2012-07-12 10:31:08 -04:00
|
|
|
|
2012-06-13 13:49:10 -04:00
|
|
|
//echo 'Creating new instance of db class.';
|
2015-06-04 14:30:42 -04:00
|
|
|
self::$instance[$dbname] = Query($db_conf[$dbname]);
|
2012-06-13 13:49:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return self::$instance[$dbname];
|
|
|
|
}
|
2012-07-12 10:31:08 -04:00
|
|
|
|
2012-06-13 13:49:10 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-07-12 10:31:08 -04:00
|
|
|
|
2012-06-13 13:49:10 -04:00
|
|
|
/**
|
|
|
|
* Returns the last error from the database
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_last_error()
|
|
|
|
{
|
|
|
|
$error = array();
|
|
|
|
|
|
|
|
if (isset($this->statement))
|
|
|
|
{
|
|
|
|
$error = $this->statement->errorInfo();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$error = $this->errorInfo();
|
|
|
|
}
|
2015-08-04 16:45:24 -04:00
|
|
|
|
2015-06-04 14:30:42 -04:00
|
|
|
list($code, $driver_code, $message) = $error;
|
2012-06-13 13:49:10 -04:00
|
|
|
|
|
|
|
// Contain the content for buffering
|
|
|
|
ob_start();
|
|
|
|
|
|
|
|
include(MM_APP_PATH.'/views/errors/error_db.php');
|
|
|
|
|
|
|
|
$buffer = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
echo $buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// End of db.php
|