<?php
/********************************
* Quickchef PDO Database connection class
*
* @author Jeramy Wenserit
* @version 0.1.1
*
*********************************/
abstract class qcPdoDatabase {
/********* CONFIG ***************/
const DEFAULTPATH = '/var/www/quickchef/app/models/qcPdoDatabase/';
const SQL_SERVER = '127.0.0.1'; /* default SQL Server ... Override in subclasses if different */
const SQL_USER = 'root'; /* default SQL username ... Override in subclasses if different */
const SQL_PASS = ''; /* default SQL password ... Override in subclasses if different */
const SQL_SERVICE = 'mysql';
const SQL_DATABASE = 'quickchef';
/********* END OF CONFIG ***********/
// SQL connection handles
const NONSPEC = 'qcPdoStandard';
const USERS = 'qcPdoUsers';
const FORUM = 'qcPdoForum';
//SQL data container
const SQLDATA = 'qcPdoSqlData';
// variables
protected $pdoConn = null;
protected function __construct() {
// forces user to use getDbObject('name')
}
protected function connect() {
$this->pdoConn =
new PDO(self::SQL_SERVICE.':host='.self::SQL_SERVER.';dbname='.self::SQL_DATABASE, self::SQL_USER, self::SQL_PASS);
}
protected function close() {
$this->pdoConn = null;
}
static public function getDbObject($name = null) {
if ($name) {
if (file_exists(self::DEFAULTPATH.$name.".php"))
{
// load require file of sub-class
require_once(self::DEFAULTPATH.$name.".php");
// now check weather the class actually exists in the file
if (class_exists($name)) {
$newClass = new $name();
// will only call Init() if its a sub-class of this class
if (!$newClass instanceof qcPdoSqlData)
{
$newClass->init();
}
return $newClass;
}
else
{
throw new exception($name.": class doesn't exist in ".self::DEFAULTPATH.$name.".php");
return null; //returns null because the file or class didn't exist
}
}
else
{
throw new exception(self::DEFAULTPATH.$name.".php: File doesn't exist.");
return null; //returns null because the file or class didn't exist
}
}
else
{
throw new exception('$name contains a NULL Value, please specifiy a database connection type');
return null; // return null if no name specified
}
}
/*
* Standard QC PDO Query - Pass qcPdoSqlData reference to me changed
*/
protected function query(qcPdoSqlData $sql) {
$this->connect();
$rowCount =0;
try {
$preSetFetch = $this->pdoConn->query($sql->readSql());
if ($preSetFetch)
{
foreach ($preSetFetch->fetchAll(PDO::FETCH_ASSOC) as $row)
{
$sql->appendRows($row);
$sql->setSuccessful();
$rowCount++;
}
}
if ($rowCount === 0)
{
$sql->setFailure(qcPdoSqlData::NO_ROWS);
}
/*else
{
$sql->setFailure("Invalid Username or Password.");
}*/
}
catch (exception $e)
{
$sql->setFailure($e->getMessage());
}
$this->close();
}
protected function insertRow(qcPdoSqlData $sql)
{
$this->connect();
$sql->setSuccessful(); // set as successful just incase a failure doesn't occur.
$this->pdoConn->exec($sql->readSql()) or
$sql->setFailure($this->pdoConn->errorInfo());
$this->close();
}
protected function updateRow(qcPdoSqlData $sql)
{
$this->insertRow($sql);
}
abstract protected function init();
//abstract public function getData();
}
?>
/************************************************************************
*************************************************************************/