/** * Insert record using given input record array * * @param array $recArr - associated array whose keys are field names of this BizDataObj * @return boolean - if return false, the caller can call getErrorMessage to get the error. **/ public function insertRecord($recArr) { if ($this->_isNeedGenerateId($recArr)) { $recArr["Id"] = $this->generateId(); } // for certain cases, id is generated before insert $this->m_BizRecord->setInputRecord($recArr); if (!$this->validateInput()) { return false; } $db = $this->getDBConnection(); try { $sql = $this->getSQLHelper()->buildInsertSQL($this, $joinValues); if ($sql) { $bindValues = QueryStringParam::getBindValues(); $bindValueString = QueryStringParam::getBindValueString(); BizSystem::log(LOG_DEBUG, "DATAOBJ", "Insert Sql = {$sql}" . "; BIND: {$bindValueString}"); QueryStringParam::reset(); $db->query($sql, $bindValues); } //$mainId = $db->lastInsertId(); if ($this->_isNeedGenerateId($recArr)) { $mainId = $this->generateId(false); $recArr["Id"] = $mainId; } BizSystem::log(LOG_DEBUG, "DATAOBJ", "New record Id is " . $recArr["Id"]); } catch (Exception $e) { BizSystem::log(LOG_ERR, "DATAOBJ", "Query Error : " . $e->getMessage()); $this->m_ErrorMessage = $this->getMessage("DATA_ERROR_QUERY") . ": " . $sql . ". " . $e->getMessage(); throw new BDOException($this->m_ErrorMessage); return null; } $this->m_BizRecord->setInputRecord($recArr); if ($this->_postUpdateLobFields($recArr) === false) { $this->m_ErrorMessage = $db->ErrorMsg(); return false; } $this->cleanCache(); $this->m_RecordId = $recArr["Id"]; $this->m_CurrentRecord = null; $this->_postInsertRecord($recArr); return true; }
/** * Get the number of records according the Select SQL * * @param object $db database connection * @param string $sql SQL string * @return int number of records */ private function _getNumberRecords($db, $sql) { if (preg_match("/^\\s*SELECT\\s+DISTINCT/is", $sql) || preg_match('/\\s+GROUP\\s+BY\\s+/is', $sql)) { // ok, has SELECT DISTINCT or GROUP BY so see if we can use a table alias $rewritesql = preg_replace('/(\\sORDER\\s+BY\\s.*)/is', '', $sql); $rewritesql = "SELECT COUNT(*) FROM ({$rewritesql}) _TABLE_ALIAS_"; } else { // now replace SELECT ... FROM with SELECT COUNT(*) FROM $rewritesql = preg_replace('/^\\s*SELECT\\s.*\\s+FROM\\s/Uis', 'SELECT COUNT(*) FROM ', $sql); // Because count(*) and 'order by' fails with mssql, access and postgresql. // Also a good speedup optimization - skips sorting! $rewritesql = preg_replace('/(\\sORDER\\s+BY\\s.*)/is', '', $rewritesql); } try { $bindValues = QueryStringParam::getBindValues(); $bindValueString = QueryStringParam::getBindValueString(); if ($this->m_CacheLifeTime > 0) { $cache_id = md5($this->m_Name . $rewritesql . serialize($bindValues)); //try to process cache service. $cacheSvc = BizSystem::getService(CACHE_SERVICE); $cacheSvc->init($this->m_Name, $this->m_CacheLifeTime); if ($cacheSvc->test($cache_id)) { //BizSystem::log(LOG_DEBUG, "DATAOBJ", ". Query Sql = ".$rewritesql." BIND: $bindValueString"); $resultArray = $cacheSvc->load($cache_id); } else { BizSystem::log(LOG_DEBUG, "DATAOBJ", "Query Sql = " . $rewritesql . " BIND: {$bindValueString}"); $result = $db->query($rewritesql, $bindValues); $resultArray = $result->fetch(); $cacheSvc->save($resultArray, $cache_id); } } else { $resultSet = $db->query($rewritesql, $bindValues); $resultArray = $resultSet->fetch(); } } catch (Exception $e) { BizSystem::log(LOG_ERR, "DATAOBJ", "Query Error: " . $e->getMessage()); $this->m_ErrorMessage = $this->getMessage("DATA_ERROR_QUERY") . ": " . $sql . ". " . $e->getMessage(); throw new BDOException($this->m_ErrorMessage); return 0; } return $resultArray[0]; }