PHP Classes

File: SODA/drivers/driver_mysql.php

Recommend this page to a friend!
  Classes of Salvan Grégory   SODA   SODA/drivers/driver_mysql.php   Download  
File: SODA/drivers/driver_mysql.php
Role: Class source
Content type: text/plain
Description: drivers mysql
Class: SODA
Database abstraction layer that encrypt data
Author: By
Last change: new version 1.1
Date: 15 years ago
Size: 9,979 bytes
 

Contents

Class file image Download
<?php
/**
 * Mysql Driver file
 * @package SODA
 * @ignore
*/
ini_set('display_errors',1);
error_reporting(E_ALL);
/**
 * driver_mysql.php, extends SODA
 * This class permit to manipulate mysql databases
 * @package SODA
 * @subpackage drivers
 * @author Salvan Gregory <dev@europeaweb.com>
 * @version 1.1
 * @copyright Copyright (c) 2008, Gregory Salvan europeaweb.com
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
final class driver_mysql extends SODA
{
/*
 * Protected constructor
 * Because of the singleton pattern $this===parent::$instance
*/
 
protected function __construct($nom='',$pw='',$dbname='',$srv='localhost',$opt=array('_persistant'=>false))
    {
     
$this->init_this();
     
$this->_user=$nom;
     
$this->_pwd=$pw;
     
$this->_server=$srv;
     
$this->_dbname=$dbname;
     
$this->_driver='driver_mysql';
     
$this->_dbtype='mysql';
      if (
is_array($opt)){
        foreach (
$opt as $key=>$val) {
         
$this->$key=$val;
        }
      }
    }
/*---------------------------------------------------------------
 * overloaded functions:
---------------------------------------------------------------*/
/**
  * we override the parent static constructor
  *
 */
 
public static function create($dbtype='mysql',$nom='',$pw='',$dbname='',$srv='localhost',$opt=array('_persistant'=>false))
    {
      return
parent::create($dbtype,$nom,$pw,$dbname,$srv,$opt);
    }
/**
  * Opens the connection to the Data Base
  *
 */
 
public function connect()
    {
     
$srv=$this->_server;
     
$usr=$this->_user;
     
$pwd=$this->_pwd;
     
$this->_connected=false;
      if (
$this->_persistant){
       
$this->_connection=@mysql_pconnect($srv,$usr,$pwd);
      } else {
       
$this->_connection=@mysql_connect($srv,$usr,$pwd);
      }
      if (
$this->_connection==false)$this->_connected=false;
      else {
       
$this->_connected=true;
       
$this->_select_db=@mysql_select_db($this->_dbname,$this->_connection);
      }
     
      if (!
$this->_connected){
         
$this->_errors[]='Connection Error';
      }
      return
$this->_connected;
    }
/**
  * Closes the connection to the Data Base
  *
 */
 
public function close()
    {
      @
mysql_close($this->_connection);
     
$this->_connected=false;
      unset(
$this->_connection);
     
$this->_select_db=false;
    }
/**
  * Selects a Data Base
  *
 */
 
public function select_db($name)
    {
     
$this->_dbname=$name;
     
$this->connect();
      if (!
$this->_select_db) $this->close();
      return
$this->_select_db;
    }
 
/**
  * Execute a query and return datas or false
  * A property with the name or index $rname is created to store the request
  * @param $sql string sql requests
  * @param $rname int|string name or index of the request
  * @param $type string 'array'|'object'|'row'|'field'|'assoc' set the type of returned values
  * @param $len int 0|1|2 set the type of the property _lengths of the request object
 */
 
public function query($sql,$rname=-1,$type='row',$len=0)
    {
     
//$dtype is defined 2 times because of dproto uses facilties
     
$dtype=array('array'=>'mysql_fetch_array',
     
'object'=>'mysql_fetch_object','row'=>'mysql_fetch_row',
     
'field'=>'mysql_fetch_field','assoc'=>'mysql_fetch_assoc');
      if (
array_key_exists($rname,$this->_requests)) {
       
$this->_requests[$rname]->reset();
      } else
$rname=$this->prepare($sql,$rname,$type,$len);
     
//NOTE: take care about _requests because of overloading, $req is binded to it.(= -> &=)
     
$req=$this->_requests[$rname];
     
$this->connect();
     
$req->_result=@mysql_query($req->get_sql(),$this->_connection);
     
//the query return true because of INSERT request type
     
if ($req->_result==1) $req->_rows=@mysql_affected_rows($this->_connection);
      elseif(
$req->_result==0){
       
$req->_errors[]=mysql_error($this->_connection);
      }
     
//the query return values because of SELECT request type
     
else
        {
         
$f=$dtype[$type];
         
//for fields fetch $len is always set to 0
         
if ($type=='field') $len=0;
          if (
$len==1) {
           
$req['_lengths']= new Array_Object();
            while (
$row = @$f($req->_result)){
             
$req['_data'][]=$row;
             
$req['_lengths'][]=@mysql_fetch_lengths($req->_result);
            }
          } elseif(
$len==2) {
         
// this function make the sum of lengths
           
function a_sum (&$item,$key,$ar) {
             
$item=$ar[$key]+$item;
            }
           
$row = @$f($req->_result);
           
$req['_data'][]=$row;
           
$req['_lengths']=@mysql_fetch_lengths($req->_result);
            while (
$row = @$f($req->_result)) {
             
$req['_data'][]=$row;
             
array_walk($req->_lengths,'a_sum',@mysql_fetch_lengths($req->_result));
            }
          } else {
           
//$len=0 :
           
while ($row = @$f($req->_result)) {
             
$req['_data'][]=$row;
             
$l=@mysql_fetch_lengths($req->_result);
             
$req['_lengths']+=is_array($l)?array_sum($l):$l;
            }
          }
         
$req['_fields']=@mysql_num_fields($req->_result);
         
$req['_rows']=@mysql_num_rows($req->_result);
          @
mysql_free_result($req->_result);
         
$req->_result=true;
        }
      if (
$req->_data!=false) $req->_data= new Array_Object($req->_data);
      return
$req->_data;
    }
 
/**
  * Prepare one or more queries and return the index or an array of indexes
  * Parameters can be an array, an object or parameters like query.
  * A property with the name or index $rname is created to store the request
  * @param $sql string sql request
  * @param $rname int|string name or index of the request
  * @param $type string 'array'|'object'|'row'|'field'|'assoc' set the type of returned values
  * @param $len int 0|1|2 set the type of the property _lengths of the request object
 */
 
public function prepare()
    {
     
//$dtype is redefined because of dbproto
     
$dtype=array('array'=>'mysql_fetch_array',
     
'object'=>'mysql_fetch_object','row'=>'mysql_fetch_row',
     
'field'=>'mysql_fetch_field','assoc'=>'mysql_fetch_assoc');
     
//
     
$arg_list = func_get_args();
      if (
is_array($arg_list[0]) && count($arg_list[0])>0) {
       
$r=array();
        foreach (
$arg_list[0] as $key=>$value)
          {
           
$sql= isset($value[0])?$value[0]:'';
           
$rname = isset($value[1])?$value[1]:-1;
           
$type = (isset($value[2]) && array_key_exists($value[2],$dtype))?$value[2]:'row';
           
$len = isset($value[3])?$value[3]:0;
           
$r[$key]=$this->prepare($sql,$rname,$type,$len);
          }
        return
$r;
      } else {
       
$sql= isset($arg_list[0])?$arg_list[0]:'';
       
$rname = isset($arg_list[1])?$arg_list[1]:-1;
       
$type = (isset($arg_list[2])&& array_key_exists($arg_list[2],$dtype))?$arg_list[2]:'row';
       
$len = isset($arg_list[3])?$arg_list[3]:0;
        if (!
is_string($type) || !array_key_exists($type,$dtype)) $type='row';
       
//if $rname isn't set we use an integer index
       
if ($rname==-1)$rname=$this->_requests->count_num_index();
       
//if the request is set we delete it
       
if (isset($this->_requests[$rname]))$this->clean($rname);
       
$req=new request();
       
$req->_sql=$sql;
       
$req->_type=$type;
       
$req->_ltype=$len;
       
$this->_requests[$rname]=$req;
        return
$rname;
      }
    }
/**
  * Execute one or more queries and return true if succeed otherwise false
  * Parameter can be an array, an object of requests indexes or a string or int index.
  * @param $req_id mixed index or indexes of requests if -1 or nothing all prepared requests are executed.
 */
 
public function exec($req_id=-1)
    {
     
//if $req_id == -1 all requests are executed
     
if ($req_id==-1){
        foreach (
$this->_requests as $k=>$v){
         
$ar[$k]['_sql']=$v->_sql;
         
$ar[$k]['_type']=$v->_type;
         
$ar[$k]['_ltype']=$v->_ltype;
        }
       
//As query modify $this->_requests we can't do it all in once
       
foreach($ar as $k=>$v)$this->query($ar[$k]['_sql'],$k,$ar[$k]['_type'],$ar[$k]['_ltype']);
        return
true;
      }
     
//If $req_id is a valid index we return datas or false if the request isn't set
     
elseif (is_int($req_id) || is_string($req_id)) {
        if (isset(
$this->_requests[$req_id])){
          return
$this->query($this->_requests[$req_id]->_sql,$req_id,$this->_requests[$req_id]->_type,$this->_requests[$req_id]->_ltype);
        }
        return
false;
      }
     
//else we execute all requests givens by the array or object $req_id
     
elseif (is_array($req_id) || (is_object($req_id) && (get_class($req_id)=='Array_Object'||get_class($req_id)=='stdClass')))
        {
          foreach (
$req_id as $id) {
           
$this->query($this->_requests[$id]->_sql,$id,$this->_requests[$id]->_type,$this->_requests[$id]->_ltype);
          }
          return
true;
        }
      return
false;
    }
/**
  * Delete one or more queries and return true if succeed otherwise false
  * Parameter can be an array, an object of requests indexes or a string or int index.
  * @param $req_id mixed index or indexes of requests if -1 or nothing all prepared requests are deleted.
 */
 
public function clean($req_id=-1)
    {
     
$r=true;
      if (
$req_id==-1)$this->_requests=new Array_Object();
      elseif (
is_int($req_id)||is_string($req_id)) {
        if (isset(
$this->_requests[$req_id]))unset($this->_requests[$req_id]);
       
$r= !isset($this->_requests[$req_id]);
        if (!
$r) $this->_errors[]="Can't delete the request $req_id";
      }
      elseif (
is_array($req_id))
        {
          foreach (
$req_id as $id) {
           
$v=$this->clean($id);
           
$r=$v==false?false:$r;
          }
        }
      return
$r;
    }
}
//end of the class driver_mysql


?>