miniMVC/sys/core/db.php

100 lines
2.0 KiB
PHP
Raw Normal View History

2012-01-13 11:58:06 -05:00
<?php
/**
* MiniMVC
*
* Convention-based micro-framework for PHP
*
2012-04-26 16:50:41 -04:00
* @package miniMVC
2012-01-13 11:58:06 -05:00
* @author Timothy J. Warren
* @copyright Copyright (c) 2011 - 2012
2012-05-23 12:01:13 -04:00
* @link https://github.com/aviat4ion/miniMVC
2012-07-12 10:23:55 -04:00
* @license http://philsturgeon.co.uk/code/dbad-license
2012-01-13 11:58:06 -05:00
*/
2012-07-12 10:23:55 -04:00
2012-01-13 11:58:06 -05:00
// --------------------------------------------------------------------------
2012-05-22 11:11:36 -04:00
namespace miniMVC;
2012-07-12 10:23:55 -04:00
2012-01-13 11:58:06 -05:00
/**
* Extend PHP's PDO class to add some more functionality
*
2012-04-26 16:50:41 -04:00
* @package miniMVC
* @subpackage System
2012-01-13 11:58:06 -05:00
*/
2012-07-12 10:23:55 -04:00
class db extends \Query_Builder {
2012-01-13 11:58:06 -05:00
2012-04-26 16:50:41 -04:00
/**
* DB connection instances
*
* @var array
*/
private static $instance = array();
2012-07-12 10:23:55 -04:00
// --------------------------------------------------------------------------
2012-07-12 10:23:55 -04:00
2012-04-26 16:50:41 -04:00
/**
* Indexed singleton method
*
* @param string $dbname
* @param array $options
* @return DB
*/
public static function &get_instance($dbname="default", array $options=array())
2012-01-13 11:58:06 -05:00
{
if ( ! isset(self::$instance[$dbname]))
{
2012-04-26 16:50:41 -04:00
// Include the database config file
require_once(MM_APP_PATH.'config/db.php');
2012-07-12 10:23:55 -04:00
2012-04-26 16:50:41 -04:00
// Get the correct database in the config file
2012-05-03 13:26:09 -04:00
if ( ! is_like_array($db_conf[$dbname]))
2012-04-26 16:50:41 -04:00
{
// Apparently the database doesn't exist
$this->get_last_error();
trigger_error("Database does not exist", E_USER_ERROR);
die();
}
2012-07-12 10:23:55 -04:00
2012-01-13 11:58:06 -05:00
//echo 'Creating new instance of db class.';
2012-11-08 14:44:16 -05:00
self::$instance[$dbname] = \Query($db_conf[$dbname]);
2012-01-13 11:58:06 -05:00
}
return self::$instance[$dbname];
}
2012-07-12 10:23:55 -04:00
2012-01-13 11:58:06 -05:00
// --------------------------------------------------------------------------
2012-07-12 10:23:55 -04:00
2012-01-13 11:58:06 -05:00
/**
* Returns the last error from the database
*
* @return string
*/
2012-03-05 14:34:51 -05:00
public function get_last_error()
2012-01-13 11:58:06 -05:00
{
$error = array();
2012-01-13 11:58:06 -05:00
2012-05-03 13:26:09 -04:00
if (isset($this->statement))
2012-01-13 11:58:06 -05:00
{
$error = $this->statement->errorInfo();
}
else
{
$error = $this->errorInfo();
}
$code = $error[0];
$driver_code = $error[1];
$message = $error[2];
// Contain the content for buffering
ob_start();
2012-05-14 16:35:11 -04:00
include(MM_APP_PATH.'/views/errors/error_db.php');
2012-01-13 11:58:06 -05:00
$buffer = ob_get_contents();
ob_end_clean();
echo $buffer;
}
}
2012-05-17 16:22:39 -04:00
2012-01-13 11:58:06 -05:00
// End of db.php