PHP Classes
elePHPant
Icontem

File: db.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of ZLioxygon  >  Minimalistic DB  >  db.php  >  Download  
File: db.php
Role: Class source
Content type: text/plain
Description: Main PHP file
Class: Minimalistic DB
Database abstraction layer with minimal interface
Author: By
Last change:
Date: 2 years ago
Size: 5,027 bytes
 

Contents

Class file image Download
<?php
/******************************************************************************
define path to classes dir
/******************************************************************************/
define('DB_CLASSES', 'classes/');
/******************************************************************************
autoloader DB classes
/******************************************************************************/
spl_autoload_register(
    function(
$class){
       
$filename = DB_CLASSES . $class . '.class.php';
        if(
is_readable($filename)){
            include_once(
$filename);
        }
    },
true);
/******************************************************************************
DB factory-function
/******************************************************************************/
function db($input = false){
    static
$instance;
    if(
$instance !== null and $input === false){
        return(
$instance);
    }
    else{
       
// select config
       
if(is_array($input)){
           
var_dump('ARRAY');
        }
        elseif(
is_string($input)){
            if((
$config = json_decode($input, true)) !== null){
               
var_dump('JSON');
            }
            elseif(
is_readable($input)){
               
var_dump('FILE');
               
$config = json_decode(file_get_contents($input), true);
                if(
$config === null){
                    throw new
db_Exeption('DB: Error config file "' . $input . '"!', 1);
                }
            }
            else{
                throw new
db_Exeption('DB: Error input string!', 1);
            }
        }
        else{
           
var_dump('CLASS');
            try{
               
$config = json_decode(json_encode(new db_config()), true);
            }
            catch(
Exception $e){
                throw new
db_Exeption('DB: Error configuration!', 1);
            }
        }
       
// checking config
       
if(!isset($config['class'])){
            throw new
db_Exeption('DB: Classname isn\'t set!', 1);
        }
       
$config['host'] = isset($config['host']) ? $config['host'] : false;
       
$config['port'] = isset($config['port']) ? $config['port'] : false;
       
$config['sock'] = isset($config['sock']) ? $config['sock'] : false;
       
$config['user'] = isset($config['user']) ? $config['user'] : false;
       
$config['pass'] = isset($config['pass']) ? $config['pass'] : false;
       
$config['base'] = isset($config['base']) ? $config['base'] : false;
       
$config['coll'] = isset($config['coll']) ? $config['coll'] : false;
       
// load class
       
try{
           
$class = 'db_' . $config['class'];
           
$instance = new $class($config);
            return(
$instance);
        }
        catch(
Exception $e){
            throw new
db_Exeption('DB: Unknown class "' . $config['class'] . '"!', 1);
        }
    }
}
/******************************************************************************
Exceptions
/******************************************************************************/
class db_Exeption extends Exception{

}
/******************************************************************************
abstract class DB
/******************************************************************************/
abstract class db{
   
// descriptor database connection
   
protected $link = false;
   
// number of fields in result from last query
   
protected $fields = 0;
   
// number of rows in result from last query
   
protected $rows = 0;
   
// counter queries
   
protected $counter = 0;
   
// timer queries
   
protected $timer = 0;
/******************************************************************************/
    // connect
   
abstract public function __construct($config);
/******************************************************************************/
    // disconnect
   
abstract public function __destruct();
/******************************************************************************/
    // request to DB
   
abstract public function q($query);
/******************************************************************************/
    // safe input data
   
abstract public function s($data);
/******************************************************************************/
    // choice of encoding
   
abstract public function c($collate);
/******************************************************************************/
    // return last insert id
   
abstract public function l();
/******************************************************************************/
    // status connection
   
public function o(){
        return(!!
$this->link);
    }
/******************************************************************************/
    // return number of fields in result from last query (SELECT)
   
public function f(){
        return(
$this->fields);
    }
/******************************************************************************/
    // return number of rows in result from last query (SELECT) or affected records (INSERT, UPDATE, DELETE)
   
public function r(){
        return(
$this->rows);
    }
/******************************************************************************/
    // return counter
   
public function i(){
        return(
$this->counter);
    }
/******************************************************************************/
    // return timer
   
public function t(){
        return(
$this->timer);
    }
}
/******************************************************************************/