• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List

E:/E/GEAMP/www/openbiz/openbiz/bin/service/genIdService.php

00001 <?php
00025 class genIdService
00026 {
00027 
00034     public function __construct()
00035     {
00036     }
00037 
00048     public function getNewID($idGeneration, $conn, $dbType, $table=null, $column=null)
00049     {
00050         try
00051         {
00052             if (!$idGeneration || $idGeneration == 'Openbiz')
00053             {
00054                 $newid = $this->getNewSYSID($conn, $table, true);
00055             }
00056             elseif ($idGeneration == 'Identity')
00057             {
00058                 //$newid = $this->getNewIdentity($conn, $dbtype, $table, $column);
00059                 $newid = $conn->lastInsertId($table, $column);   // user zend_db method
00060             }
00061             elseif (strpos($idGeneration, 'Sequence:')===0)
00062             {
00063                 $seqname = substr($idGeneration, 9);
00064                 //$newid = $this->getNewSequence($conn, $dbtype, $seqname);
00065                 $newid = $conn->nextSequenceId($seqname);   // user zend_db method
00066             }
00067             elseif ($idGeneration == 'GUID')
00068             {
00069                 $newid = $this->getNewGUID($conn, $dbType, $table, $column);
00070             }
00071             elseif ($idGeneration == 'UUID')
00072             {
00073                 $newid = $this->getNewUUID();
00074             }
00075             else
00076             {
00077                 throw new Exception("Error in generating new ID: unsupported generation type.");
00078             }
00079         }
00080         catch (Exception $e)
00081         {
00082             throw new Exception($e->getMessage());
00083         }
00084         return $newid;
00085     }
00086 
00096     protected function getNewSYSID($conn, $tableName, $includePrefix=false, $base=-1)
00097     {
00098         $maxRetry = 10;
00099         // try to update the table idbody column
00100         for ($try=1; $try <= $maxRetry; $try++)
00101         {
00102             $sql = "SELECT * FROM ob_sysids WHERE TABLENAME='$tableName'";
00103             try
00104             {
00105                 $rs = $conn->query($sql);
00106             }
00107             catch (Exception $e)
00108             {
00109                 throw new Exception("Error in query: " . $sql . ". " . $e->getMessage());
00110                 return false;
00111             }
00112             $row = $rs->fetch();
00113             unset($rs);
00114             list($tblname, $prefix, $idbody) = $row;
00115             if (!$row)
00116                 throw new Exception("Error in generating new system id: '$tableName' is not in ob_sysids table.");
00117             if ($row)
00118             {
00119                 if ($idbody == null && $prefix) // idbody is empty, return false
00120                     throw new Exception("Error in generating new system id: ob_sysids table does not have a valid sequence for '$tableName'.");
00121             }
00122             // try to update the table idbody column
00123             $sql = "UPDATE ob_sysids SET IDBODY=IDBODY+1 WHERE TABLENAME='$tableName' AND IDBODY=$idbody";
00124             try
00125             {
00126                 $rs = $conn->query($sql);
00127             }
00128             catch (Exception $e)
00129             {
00130                 throw new Exception("Error in query: " . $sql . ". " . $e->getMessage());
00131                 return false;
00132             }
00133             if ($rs->rowCount() > 0)
00134             {
00135                 $idbody += 1;
00136                 break;
00137             }
00138         }
00139         if ($try <= $maxRetry)
00140         {
00141             if ($base>=2 && $base<=36)
00142                 $idbody = dec2base($idbody, $base);
00143             if ($includePrefix)
00144                 return $prefix."_".$idbody;
00145             return $idbody;
00146         }
00147         else
00148             throw new Exception("Error in generating new system id: unable to get a valid id.");
00149         return false;
00150     }
00151 
00162     protected function getNewIdentity($conn, $dbType, $table=null, $column=null)
00163     {
00164         $dbType = strtoupper($dbType);
00165         if ($dbType == 'mysql' || $dbType == 'PDO_MYSQL')
00166         {
00167             $sql = "select last_insert_id()";
00168         }
00169         else if ($dbType == 'mssql' || $dbType == 'PDO_DBLIB')
00170             $sql = "select @@identity";
00171         else if ($dbType == 'sybase' || $dbType == 'PDO_DBLIB')
00172             $sql = "select @@identity";
00173         else if ($dbType == 'db2' || $dbType == 'PDO_ODBC')
00174             $sql = "values identity_val_local()";
00175         else if ($dbType == 'postgresql' || $dbType == 'PDO_PGSQL')
00176             $sql = "select currval('$table_$column_seq')";
00177         else
00178             throw new Exception("Error in generating new identity: unsupported database type.");
00179         // execute sql to get the id
00180         $newid = $this->_getIdWithSql($conn, $sql);
00181         if ($newid === false)
00182             throw new Exception("Error in generating new identity: unable to get a valid id.");
00183         return $newid;
00184     }
00185 
00186 
00196     protected function getNewSequence($conn, $dbtype, $sequenceName)
00197     {
00198         $dbtype = strtoupper($dbtype);
00199         if ($dbtype == 'oracle' || $dbtype == 'oci8' || $dbtype == 'PDO_OCI')
00200             $sql = "select $sequenceName.nextval from dual";
00201         else if ($dbtype == 'db2' || $dbtype == 'PDO_ODBC')
00202             $sql = "values nextval for $sequenceName";
00203         else if ($dbtype == 'postgresql' || $dbtype == 'PDO_PGSQL')
00204             $sql = "select nextval('$sequenceName')";
00205         else if ($dbtype == 'informix' || $dbtype == 'PDO_INFORMIX')
00206             "select $sequenceName.nextval from systables where tabid=1";
00207         else
00208             throw new Exception("Error in generating new sequence: unsupported database type.");
00209         // execute sql to get the id
00210         $newId = $this->_getIdWithSql($conn, $sql);
00211         if ($newId === false)
00212             throw new Exception("Error in generating new sequence: unable to get a valid id.");
00213         return $newId;
00214     }
00215 
00226     protected function getNewGUID($conn, $dbType, $table=null, $column=null)
00227     {
00228         $dbType = strtoupper($dbType);
00229         if ($dbType == 'mysql' || $dbType == 'PDO_DBLIB')
00230             $sql = "select uuid()";
00231         else if ($dbType == 'oracle' || $dbType == 'oci8' || $dbType == 'PDO_OCI')
00232             $sql = "select rawtohex(sys_guid()) from dual";
00233         else if ($dbType == 'msql' || $dbType == 'PDO_MYSQL')
00234             $sql = "select newid()";
00235         else
00236             throw new Exception("Error in generating new GUID: unsupported database type.");
00237         // execute sql to get the id
00238         $newId = $this->_getIdWithSql($conn, $sql);
00239         if ($newId === false)
00240             throw new Exception("Error in generating new GUID: unable to get a valid id.");
00241         return $newId;
00242     }
00243 
00250     protected function getNewUUID()
00251     {
00252         return uniqid();
00253     }
00254 
00262     private function _getIdWithSql($conn, $sql)
00263     {
00264         try
00265         {
00266             $rs = $conn->query($sql);
00267             BizSystem::log(LOG_DEBUG, "DATAOBJ", "Get New Id: $sql");
00268         }
00269         catch (Exception $e)
00270         {
00271             $this->m_ErrorMessage = "Error in query: " . $sql . ". " . $e->getMessage();
00272             return false;
00273         }
00274 
00275         if (($row = $rs->fetch()) != null)
00276         {
00277             //print_r($row);
00278             return $row[0];
00279         }
00280         return false;
00281     }
00282 }
00283 
00284 ?>

Generated on Thu Apr 19 2012 17:09:13 for openbiz by  doxygen 1.7.2