Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2011
    Posts
    2
    Plugin Contributions
    0

    Default 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).

  2. #2
    Join Date
    Nov 2011
    Posts
    2
    Plugin Contributions
    0

    Default 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:

    PHP Code:
    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->setAttributePDO::ATTR_STATEMENT_CLASS,
                               
                               array(
    'MyPDOStatement_Wrapper', array( & $this  )) );   
             
        
        }
      
      
      
      
      
      public function 
    exec ($statement)
      {
        
    $this->queryCount++;
        
        
    $args func_get_args();
        
        
    #var_dump($args);
        
        
    $start microtime(true);
        
    $rcall_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:
    PHP Code:
    try { 
    $_pdo = new MyPDO_Wrapper("mysql:dbname=".  DB_DATABASE .";host="DB_SERVER DB_SERVER_USERNAMEDB_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 ); 
    PHP Code:
     #this could go in the footer of the page
    print $_pdo->getQueryCount();
    print 
    $_pdo->printTotalTime(); 

 

 

Similar Threads

  1. Replies: 3
    Last Post: 29 Aug 2012, 02:57 PM
  2. where to put my diy shipping class extends core class ?
    By linjuming in forum General Questions
    Replies: 3
    Last Post: 21 Aug 2012, 02:58 PM
  3. order_total class and class redeclaration
    By s_p_ike in forum General Questions
    Replies: 1
    Last Post: 8 Feb 2007, 11:33 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR