MySQL PDO class?

Started by calm_man on 3 Nov 2011, 8:09 PM · Views: 2,593

Results 1 to 2 of 2
3 Nov 2011, 8:09 PM
#1
calm_man avatar

calm_man

New Zenner

Join Date:
Nov 2011
Posts:
2
Plugin Contributions:
0

MySQL PDO class?

Hi

I dont know if this is the best forum-section to post in but here goes. Appology if not.
I usually use PDO to connect to mysql, i would like to use pdo in zencart too.
Is there any alreadymade addon for this, or should I just go ahead and create my own new addon ?

I dont want to replace the default zencart connection-code, i just want another another class. (I suppose people here have good PHP skills and know what PDO mysql is).

22 Nov 2011, 2:21 PM
#2
calm_man avatar

calm_man

New Zenner

Join Date:
Nov 2011
Posts:
2
Plugin Contributions:
0

Re: MySQL PDO class?

Just replying to myself, any advice is welcome from anyone. Here is a little hack i put together from various examples:

class MyPDO_Wrapper extends PDO
{
  
	public static $log = array();   
  
  
  
  public $queryCount;             
  
  
	public function __construct( /*$dsn   , $username = null, $password = null */   ) 
  {
    
    
      $args = func_get_args();
      $r = call_user_func_array(array($this, 'parent::__construct'), $args);
      
      
      $this->queryCount = 0;
      ## PDO::ATTR_STATEMENT_CLASS: Set user-supplied statement class derived from PDOStatement. 
      ## Cannot be used with persistent PDO instances. Requires array(string classname, array(mixed constructor_args)).
      
      $this->setAttribute( PDO::ATTR_STATEMENT_CLASS,
                           
                           array('MyPDOStatement_Wrapper', array( & $this  )) );   
         
    
	}
  
  
  
  
  
  public function exec ($statement)
  {
    $this->queryCount++;
    
    $args = func_get_args();
    
    #var_dump($args);
    
    $start = microtime(true);
    $r= call_user_func_array(array($this, 'parent::exec'), $args);
    $time = microtime(true) - $start;
    self::$log[] = array('query' => $args[1], 'time' => sprintf('%01.4f', $time ));
    
    return $r;
    
  }
  
  
  
  
  
  /**
  *
  */
	public function query($query) 
  {
	 
      $this->queryCount++;
  
      $args = func_get_args();  
      #var_dump($args);
  
      $start = microtime(true);
      $result = call_user_func_array(array($this, 'parent::query'),  $args    );
      $time = microtime(true) - $start;
  
      self::$log[] = array('query' => $query, 'time' => sprintf('%01.4f', $time ) ) ;
  		
      return $result;
	}
  
  
  
  public static function printLog() 
  {
		$totalTime = 0;
		echo '<table border=1><tr><th>Query</th><th>Time (ms)</th></tr>';
		foreach(self::$log as $entry) 
    {
			   $totalTime += $entry['time'];
			   echo '<tr><td>' . $entry['query'] . '</td><td>' . $entry['time'] . "</td></tr>\n";
		}
    
		echo '<tr><th>' . count(self::$log) . ' queries</th><th>' . $totalTime . "</th></tr>\n";
		echo '</table>';
	}
  
  
  
  public static function printTotalTime() 
  {
        
        $totalTime = 0;
        foreach(self::$log as $entry) 
        {
             $totalTime += $entry['time'];
             
        }
          
        return $totalTime;
    
  } 
  
  
  public function getQueryCount()
  {
      return $this->queryCount;
  }
  
  
}#end class





class MyPDOStatement_Wrapper extends PDOStatement
{
  
 private $queryCount;
 
 protected function __construct (   & $this_pdo      )    
 {

    $this->queryCount = & $this_pdo->queryCount ;   
    $this->ptr_mypdo  = & $this_pdo;
 }



 public function execute ($input_parameters = array())
 {
  $this->queryCount++;
 
  $args = func_get_args();
  
  $start = microtime(true);
  $ret   =   call_user_func_array(  array($this, 'parent::execute'), $args);
  $time  = microtime(true) - $start;
  
  
  
  $this->ptr_mypdo-> log[] = array('query' => '[PS] ' . $this->queryString,
			                             'time'  =>   sprintf('%01.4f', $time )    );
  
  
  
  return $ret;
 }

}#end class.

It should work like:

try { 
$_pdo = new MyPDO_Wrapper("mysql:dbname=".  DB_DATABASE .";host=". DB_SERVER , DB_SERVER_USERNAME, DB_SERVER_PASSWORD ,
                     array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
} catch (PDOException $exception) { 
        printf("Failed to obtain database handle %s", $exception->getMessage());
        die();
}

$r = $_pdo->query("SELECT ..."  );
$ar_r = $r->fetchAll(PDO::FETCH_ASSOC );
 #this could go in the footer of the page
print $_pdo->getQueryCount();
print $_pdo->printTotalTime();