oxcounter.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxCounter
00008 {
00016     public function getNext( $sIdent )
00017     {
00018         $oDb = oxDb::getDb();
00019         $oDb->startTransaction();
00020 
00021         $sQ = "SELECT `oxcount` FROM `oxcounters` WHERE `oxident` = " . $oDb->quote( $sIdent ) . " FOR UPDATE";
00022 
00023         if ( ( $iCnt = $oDb->getOne( $sQ, false, false ) ) === false ) {
00024             $sQ = "INSERT INTO `oxcounters` (`oxident`, `oxcount`) VALUES (?, '0')";
00025             $oDb->execute( $sQ, array( $sIdent ) );
00026         }
00027 
00028         $iCnt = ( (int) $iCnt ) + 1;
00029         $sQ = "UPDATE `oxcounters` SET `oxcount` = ? WHERE `oxident` = ?";
00030         $oDb->execute( $sQ, array( $iCnt, $sIdent ) );
00031 
00032         $oDb->commitTransaction();
00033 
00034         return $iCnt;
00035     }
00036 
00046     public function update( $sIdent, $iCount )
00047     {
00048         $oDb = oxDb::getDb();
00049         $oDb->startTransaction();
00050 
00051         $sQ = "SELECT `oxcount` FROM `oxcounters` WHERE `oxident` = " . $oDb->quote( $sIdent ) . " FOR UPDATE";
00052 
00053         if ( ( $iCnt = $oDb->getOne( $sQ, false, false ) ) === false ) {
00054             $sQ = "INSERT INTO `oxcounters` (`oxident`, `oxcount`) VALUES (?, ?)";
00055             $blResult = $oDb->execute( $sQ, array( $sIdent, $iCount ) );
00056         } else {
00057             $sQ = "UPDATE `oxcounters` SET `oxcount` = ? WHERE `oxident` = ? AND `oxcount` < ?";
00058             $blResult = $oDb->execute( $sQ, array( $iCount, $sIdent, $iCount ) );
00059         }
00060 
00061         $oDb->commitTransaction();
00062         return $blResult;
00063     }
00064 
00065 
00066 }