00001 <?php
00017 include_once(OPENBIZ_BIN.'data/BizDataObj_Abstract.php');
00018 include_once(OPENBIZ_BIN."util/QueryStringParam.php");
00019 include_once(OPENBIZ_BIN."data/DataSet.php");
00020
00021
00022 define('CK_CONNECTOR', "#");
00023
00032 class BizDataObj_Lite extends BizDataObj_Abstract
00033 {
00039 protected $m_RecordId = null;
00040
00046 protected $m_CurrentRecord = null;
00047
00053 protected $m_ErrorMessage = "";
00054
00060 protected $m_ErrorFields = array();
00061
00062 protected $_fetch4countQuery = null;
00063
00070 public function getSessionVars($sessionContext)
00071 {
00072 if ($this->m_Stateless == "Y")
00073 return;
00074 $sessionContext->getObjVar($this->m_Name, "RecordId", $this->m_RecordId);
00075 $sessionContext->getObjVar($this->m_Name, "SearchRule", $this->m_SearchRule);
00076 $sessionContext->getObjVar($this->m_Name, "SortRule", $this->m_SortRule);
00077 $sessionContext->getObjVar($this->m_Name, "OtherSqlRule", $this->m_OtherSQLRule);
00078 $sessionContext->getObjVar($this->m_Name, "Association", $this->m_Association);
00079
00080
00081 foreach($this->m_BizRecord as $fieldName=>$bizField)
00082 $bizField->setValue($this->m_CurrentRecord[$fieldName]);
00083 }
00084
00091 public function setSessionVars($sessionContext)
00092 {
00093 if ($this->m_Stateless == "Y")
00094 return;
00095 $sessionContext->setObjVar($this->m_Name, "RecordId", $this->m_RecordId);
00096 $sessionContext->setObjVar($this->m_Name, "SearchRule", $this->m_SearchRule);
00097 $sessionContext->setObjVar($this->m_Name, "SortRule", $this->m_SortRule);
00098 $sessionContext->setObjVar($this->m_Name, "OtherSqlRule", $this->m_OtherSQLRule);
00099 if(is_array($this->m_Association)){
00100 $sessionContext->setObjVar($this->m_Name, "Association", $this->m_Association);
00101 }
00102 }
00103
00109 public function getErrorMessage()
00110 {
00111 return $this->m_ErrorMessage;
00112 }
00113
00119 public function getErrorFields()
00120 {
00121 return $this->m_ErrorFields;
00122 }
00123
00130 public function getField($fieldName)
00131 {
00132 return $this->m_BizRecord->get($fieldName);
00133 }
00134
00141 public function getProperty($propertyName)
00142 {
00143 $ret = parent::getProperty($propertyName);
00144 if ($ret !== null) return $ret;
00145
00146
00147 $pos1 = strpos($propertyName, "[");
00148 $pos2 = strpos($propertyName, "]");
00149
00150 if ($pos1>0 && $pos2>$pos1)
00151 {
00152 $propType = substr($propertyName, 0, $pos1);
00153 $fieldName = substr($propertyName, $pos1+1,$pos2-$pos1-1);
00154
00155
00156
00157 return $this->getField($fieldName);
00158 }
00159 }
00160
00167 public function getFieldNameByColumn($column)
00168 {
00169
00170
00171 return $this->m_BizRecord->getFieldByColumn($column);
00172 }
00173
00180 public function getFieldValue($fieldName)
00181 {
00182 $rec = $this->getActiveRecord();
00183 return $rec[$fieldName];
00184 }
00185
00192 public function setActiveRecord($currentRecord)
00193 {
00194 $this->m_CurrentRecord = $currentRecord;
00195 $this->m_RecordId = $this->m_CurrentRecord['Id'];
00196 }
00197
00204 public function getActiveRecord()
00205 {
00206 if ($this->m_RecordId == null || $this->m_RecordId == "")
00207 return null;
00208 if ($this->m_CurrentRecord == null)
00209 {
00210
00211 $records = $this->directFetch("[Id]='".$this->m_RecordId."'", 1);
00212 if (count($records) == 1)
00213 {
00214 $this->m_CurrentRecord = $records[0];
00215 $this->m_RecordId = $this->m_CurrentRecord["Id"];
00216 }
00217 else
00218 $this->m_CurrentRecord = null;
00219 }
00220
00221 return $this->m_CurrentRecord;
00222 }
00223
00224 public function getRecordId(){
00225 return $this->m_RecordId;
00226 }
00227
00233 public function setActiveRecordId($recordId)
00234 {
00235 if ($this->m_RecordId != $recordId)
00236 {
00237 $this->m_RecordId = $recordId;
00238 $this->m_CurrentRecord = null;
00239 }
00240 }
00241
00247 public function fetch()
00248 {
00249 $dataSet = new DataSet($this);
00250 $this->_fetch4countQuery = null;
00251 $resultSet = $this->_run_search($this->m_Limit);
00252 if ($resultSet !== null)
00253 {
00254 $i = 0;
00255 while ($recArray = $this->_fetch_record($resultSet))
00256 {
00257 $dataSet[$i++] = $recArray;
00258 }
00259 }
00260 else
00261 return null;
00262
00263 return $dataSet;
00264 }
00265
00274 public function directFetch($searchRule="", $count=-1, $offset=0,$sortRule="")
00275 {
00276 $curRecord = $this->m_CurrentRecord;
00277 $recId = $this->m_RecordId;
00278 $this->m_CurrentRecord = null;
00279
00280 $oldSearchRule = $this->m_SearchRule;
00281 $this->clearSearchRule();
00282 $this->setSearchRule($searchRule);
00283
00284 $oldSortRule = $this->m_SortRule;
00285 $this->clearSortRule();
00286 if($sortRule){
00287 $this->setSortRule($sortRule);
00288 }else{
00289 $this->setSortRule($this->m_SortRule);
00290 }
00291
00292 $limit = ($count == -1) ? null : array('count'=>$count, 'offset'=>$offset);
00293
00294 $dataSet = new DataSet($this);
00295 $resultSet = $this->_run_search($limit);
00296 if ($resultSet !== null)
00297 {
00298 $i = 0;
00299 while ($recArray = $this->_fetch_record($resultSet))
00300 {
00301 $dataSet[$i++] = $recArray;
00302 }
00303 }
00304
00305 $this->m_SortRule = $oldSortRule;
00306 $this->m_SearchRule = $oldSearchRule;
00307 $this->m_CurrentRecord = $curRecord;
00308 $this->m_RecordId = $recId;
00309
00310 if (count($dataSet) == 0)
00311 return new DataSet(null);
00312 return $dataSet;
00313 }
00314
00320 public function fetchById($id)
00321 {
00322 $searchRule = "[Id] = '$id'";
00323 return $this->fetchOne($searchRule);
00324 }
00325
00326 public function fetchByName($nameVal)
00327 {
00328 $searchRule = "[name] = '$nameVal'";
00329 return $this->fetchOne($searchRule);
00330 }
00331
00332 public function fetchOne($searchRule,$sortRule="")
00333 {
00334 $recordList = $this->directFetch($searchRule, 1,0,$sortRule);
00335 if (count($recordList) >= 1)
00336 return new DataRecord($recordList[0], $this);
00337 else
00338 return null;
00339 }
00340
00353 public function fetchRecords($searchRule, &$resultRecords, $count=-1,
00354 $offset=0, $clearSearchRule=true, $noAssociation=false)
00355 {
00356 if ($count == 0) return;
00357 $curRecord = $this->m_CurrentRecord;
00358 $recId = $this->m_RecordId;
00359 $oldSearchRule = $this->m_SearchRule;
00360 $this->m_CurrentRecord = null;
00361 if ($clearSearchRule)
00362 $this->clearSearchRule();
00363 $this->setSearchRule($searchRule);
00364 if ($noAssociation)
00365 {
00366 $oldAssociation = $this->m_Association;
00367 $this->m_Association = null;
00368 }
00369 $limit = ($count == -1) ? null : array('count'=>$count, 'offset'=>$offset);
00370
00371 $resultRecords = array();
00372
00373 $resultSet = $this->_run_search($limit);
00374 if ($resultSet !== null)
00375 {
00376 while ($recArray = $this->_fetch_record($resultSet))
00377 {
00378 $resultRecords[] = $recArray;
00379 }
00380 }
00381 if ($noAssociation)
00382 $this->m_Association = $oldAssociation;
00383 $this->m_SearchRule = $oldSearchRule;
00384 $this->m_CurrentRecord = $curRecord;
00385 $this->m_RecordId = $recId;
00386 return true;
00387 }
00388
00394 public function find()
00395 {
00396 return $this->_run_search($this->m_Limit);
00397 }
00398
00404 protected function getSQLHelper()
00405 {
00406 return BizDataObj_SQLHelper::instance();
00407 }
00408
00415 public function count()
00416 {
00417
00418 $db = $this->getDBConnection("READ");
00419 if ($this->_fetch4countQuery)
00420 $querySQL = $this->_fetch4countQuery;
00421 else
00422 $querySQL = $this->getSQLHelper()->buildQuerySQL($this);
00423 $this->_fetch4countQuery = null;
00424 return $this->_getNumberRecords($db, $querySQL);
00425 }
00426
00433 protected function _run_search($limit=null)
00434 {
00435
00436 $db = $this->getDBConnection("READ");
00437 $querySQL = $this->getSQLHelper()->buildQuerySQL($this);
00438 $this->_fetch4countQuery = $querySQL;
00439 if ($limit && count($limit) > 0 && $limit['count'] > 0)
00440 $sql = $db->limit($querySQL, $limit['count'], $limit['offset']);
00441 else
00442 $sql = $querySQL;
00443
00444 try
00445 {
00446 $bindValues = QueryStringParam::getBindValues();
00447 $bindValueString = QueryStringParam::getBindValueString();
00448 if($this->m_CacheLifeTime>0)
00449 {
00450 $cache_id = md5($this->m_Name . $sql . serialize($bindValues));
00451
00452 $cacheSvc = BizSystem::getService(CACHE_SERVICE,1);
00453 $cacheSvc->init($this->m_Name,$this->m_CacheLifeTime);
00454 if($cacheSvc->test($cache_id))
00455 {
00456
00457 $resultSetArray = $cacheSvc->load($cache_id);
00458 }
00459 else
00460 {
00461 BizSystem::log(LOG_DEBUG, "DATAOBJ", "Query Sql = ".$sql." BIND: $bindValueString");
00462 $resultSet = $db->query($sql, $bindValues);
00463 $resultSetArray = $resultSet->fetchAll();
00464 $cacheSvc->save($resultSetArray,$cache_id);
00465 }
00466 }
00467 else
00468 {
00469 BizSystem::log(LOG_DEBUG, "DATAOBJ", "Query Sql = ".$sql." BIND: $bindValueString");
00470 $resultSet = $db->query($sql, $bindValues);
00471 $resultSetArray = $resultSet->fetchAll();
00472 }
00473 }
00474 catch (Exception $e)
00475 {
00476 BizSystem::log(LOG_ERR, "DATAOBJ", "Query Error: ".$e->getMessage());
00477 $this->m_ErrorMessage = $this->getMessage("DATA_ERROR_QUERY").": ".$sql.". ".$e->getMessage();
00478 throw new BDOException($this->m_ErrorMessage);
00479 return null;
00480 }
00481 return $resultSetArray;
00482 }
00483
00491 private function _getNumberRecords($db, $sql)
00492 {
00493 if (preg_match("/^\s*SELECT\s+DISTINCT/is", $sql) || preg_match('/\s+GROUP\s+BY\s+/is',$sql))
00494 {
00495
00496 $rewritesql = preg_replace('/(\sORDER\s+BY\s.*)/is','',$sql);
00497 $rewritesql = "SELECT COUNT(*) FROM ($rewritesql) _TABLE_ALIAS_";
00498 }
00499 else
00500 {
00501
00502 $rewritesql = preg_replace('/^\s*SELECT\s.*\s+FROM\s/Uis','SELECT COUNT(*) FROM ',$sql);
00503
00504
00505
00506 $rewritesql = preg_replace('/(\sORDER\s+BY\s.*)/is','',$rewritesql);
00507 }
00508
00509 try
00510 {
00511 $bindValues = QueryStringParam::getBindValues();
00512 $bindValueString = QueryStringParam::getBindValueString();
00513 if($this->m_CacheLifeTime>0)
00514 {
00515 $cache_id = md5($this->m_Name . $rewritesql . serialize($bindValues));
00516
00517 $cacheSvc = BizSystem::getService(CACHE_SERVICE);
00518 $cacheSvc->init($this->m_Name,$this->m_CacheLifeTime);
00519 if($cacheSvc->test($cache_id))
00520 {
00521
00522 $resultArray = $cacheSvc->load($cache_id);
00523 }
00524 else
00525 {
00526 BizSystem::log(LOG_DEBUG, "DATAOBJ", "Query Sql = ".$rewritesql." BIND: $bindValueString");
00527 $result = $db->query($rewritesql, $bindValues);
00528 $resultArray = $result->fetch();
00529 $cacheSvc->save($resultArray,$cache_id);
00530 }
00531 }
00532 else
00533 {
00534 BizSystem::log(LOG_DEBUG, "DATAOBJ", "Query Sql = ".$rewritesql." BIND: $bindValueString");
00535 $resultSet = $db->query($rewritesql, $bindValues);
00536 $resultArray = $resultSet->fetch();
00537 }
00538 }
00539 catch (Exception $e)
00540 {
00541 BizSystem::log(LOG_ERR, "DATAOBJ", "Query Error: ".$e->getMessage());
00542 $this->m_ErrorMessage = $this->getMessage("DATA_ERROR_QUERY").": ".$sql.". ".$e->getMessage();
00543 throw new BDOException($this->m_ErrorMessage);
00544 return 0;
00545 }
00546
00547 return $resultArray[0];
00548 }
00549
00555 protected function _fetch_record(&$resultSet)
00556 {
00557 if(!is_array($resultSet)){
00558 return null;
00559 }
00560 if ($sqlArr = current($resultSet))
00561 {
00562 $this->m_CurrentRecord = $this->m_BizRecord->convertSqlArrToRecArr($sqlArr);
00563 $this->m_CurrentRecord = $this->m_BizRecord->getRecordArr($sqlArr);
00564 $this->m_RecordId = $this->m_CurrentRecord["Id"];
00565 next($resultSet);
00566 }
00567 else
00568 {
00569 return null;
00570 }
00571 return $this->m_CurrentRecord;
00572 }
00573
00579 public function validateInput()
00580 {
00581
00582 }
00583
00589 public function newRecord()
00590 {
00591
00592 }
00593
00600 public function insertRecord($recArr)
00601 {
00602
00603 }
00604
00612 public function updateRecord($recArr, $oldRec=null)
00613 {
00614
00615 }
00616
00623 public function deleteRecord($recArr)
00624 {
00625
00626 }
00627 }
00628
00629 ?>