miniMVC/sys/core/db.php

124 lines
2.5 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
* @link https://github.com/timw4mail/miniMVC
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* 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-04-26 16:50:41 -04:00
class DB extends Query_Builder {
2012-01-13 11:58:06 -05:00
2012-04-26 16:50:41 -04:00
use JSObject;
/**
* DB connection instances
*
* @var array
*/
2012-05-14 14:35:57 -04:00
private static $instance = [];
2012-01-13 11:58:06 -05:00
2012-04-26 16:50:41 -04:00
/**
* Indexed singleton method
*
* @param string $dbname
* @param array $options
* @return DB
*/
2012-05-14 14:35:57 -04:00
public static function &get_instance($dbname="default", array $options=[])
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');
// 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-01-13 11:58:06 -05:00
//echo 'Creating new instance of db class.';
2012-04-26 16:50:41 -04:00
self::$instance[$dbname] = new DB($db_conf[$dbname]);
2012-01-13 11:58:06 -05:00
}
return self::$instance[$dbname];
}
// --------------------------------------------------------------------------
/**
2012-05-01 16:39:14 -04:00
* Constructor to override JSObject trait
2012-01-13 11:58:06 -05:00
*
2012-04-26 16:50:41 -04:00
* @param array $params
2012-01-13 11:58:06 -05:00
*/
2012-05-15 16:53:10 -04:00
public function __construct(array $params=[])
2012-01-13 11:58:06 -05:00
{
2012-04-26 16:50:41 -04:00
// Let's try connecting now!
parent::__construct($params);
2012-01-13 11:58:06 -05:00
}
// --------------------------------------------------------------------------
/**
2012-04-26 16:50:41 -04:00
* Override __call in trait to call __call in Query Builder...lol
2012-01-13 11:58:06 -05:00
*
2012-04-26 16:50:41 -04:00
* @param string $name
* @param array $params
* @return mixed
2012-01-13 11:58:06 -05:00
*/
2012-05-14 14:35:57 -04:00
public function __call($name, $params=[])
2012-01-13 11:58:06 -05:00
{
2012-04-26 16:50:41 -04:00
return parent::__call($name, $params);
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
{
2012-05-14 14:35:57 -04:00
$error = [];
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;
}
}
// End of db.php