m_strConfigFileName=$strConfigFileName; $this->m_strSectionName=$strSection; $this->m_strLastError=''; $this->m_strDBNameIdent=$strDBNameIdent; } public function Init() { return $this->IsConnected(); } public function GetLastError() { return $this->m_strLastError; } #´ÓÅäÖñíÖжÁȡָ¶¨Êý¾Ý³É¹¦·µ»Øtrue£¬·ñÔò·µ»Øfalse private function GetAppointData( $strSection, $strIdent, &$strRetValue ) { if( !$this->m_config->GetData( $strSection, $strIdent, $strRetValue ) ) { $this->m_strLastError="Get section:$strSection ident:$strIdent error!Error info:".$this->m_config->GetErrInfo(); return false; } return true; } #²é¿´ÊÇ·ñÊý¾Ý¿âÊÇ·ñÁ¬½Ó public function IsConnected() { try { if( $this->GetDB()!=null ) { return true; } \Logging\LogError( 'Á¬½ÓÊý¾Ý¿âʧ°Ü!´íÎó:'.$this->m_strLastError ); } catch( Exception $e ) { \Logging\LogError( $e->getMessage() ); } return false; } #»ñµÃÖ¸¶¨µÄÊý¾Ý¿â public function GetDB() { if( $this->m_db!=null ) { return $this->m_db; } #¶ÁÈ¡Ini $this->m_config = new \ConfigReader\ConfigReader( $this->m_strConfigFileName ); if( !$this->m_config->load() ) { $this->m_strLastError="GetDB load config error!Error info:" .$this->m_config->GetErrInfo(); return null; } #ÔØÈëÅäÖà if( !$this->GetAppointData( $this->m_strSectionName, 'DBIP', $this->m_strDBHost ) ) { return null; } if( !$this->GetAppointData( $this->m_strSectionName, 'DBPort', $this->m_strDBPort ) ) { return null; } #Õ˺ÅÐèÒª½âÃÜ if( !$this->GetAppointData( $this->m_strSectionName, 'DBUser', $this->m_strDBAccount ) ) { return null; } $strSrcDBAccount=$this->m_strDBAccount; $this->m_strDBAccount=\Commfunc\GetDecodePsw( $this->m_strDBAccount ); #ÃÜÂëÐèÒª½âÃÜ if( !$this->GetAppointData( $this->m_strSectionName, 'DBPsw', $this->m_strDBPassword ) ) { return null; } $strSrcDBPsw=$this->m_strDBPassword; $this->m_strDBPassword=\Commfunc\GetDecodePsw( $this->m_strDBPassword ); if( !$this->GetAppointData( $this->m_strSectionName, $this->m_strDBNameIdent, $this->m_strDBName ) ) { return null; } \logging\LogInfo( "¶ÁÈ¡µÄÊý¾Ý¿âhost:$this->m_strDBHost port:$this->m_strDBPort user:$strSrcDBAccount psw:$strSrcDBPsw DataBaseName:$this->m_strDBName" ); $conn = new \MongoClient("mongodb://".$this->m_strDBAccount.":" .$this->m_strDBPassword."@".$this->m_strDBHost.":".$this->m_strDBPort); $strDBName=$this->m_strDBName; $this->m_db=$conn->$strDBName; \Logging\LogInfo( 'Á¬½ÓÊý¾Ý¿â³É¹¦£¡' ); #·µ»ØÄÚ²¿±£´æµÄMongoDB¶ÔÏó return $this->m_db; } #²éѯÊý¾ÝÊýÁ¿ public function GetDataCount( $strCollection, $findArray) { #ÏÈÅжÏÊÇ·ñÁ¬½Ó if( !$this->IsConnected() ) { return false; } #Åжϼ¯ºÏÊÇ·ñ´æÔÚ $collection=$this->m_db->$strCollection; if( !isset($collection) ) { $this->m_strLastError='ÕÒ²»µ½¼¯ºÏ:'.$strCollection; return false; } return $this->m_db->$strCollection->count($findArray); } #²éѯÊý¾Ý,ÒÔarrayÐÎʽ·µ»ØÄÚÈÝ public function GetData( $strCollection, $findArray, &$retArray, $arrayRetColumn=array(), $skip=0, $perPage=10, $sortarry=array()) { #ÏÈÅжÏÊÇ·ñÁ¬½Ó if( !$this->IsConnected() ) { return false; } #Åжϼ¯ºÏÊÇ·ñ´æÔÚ $collection=$this->m_db->$strCollection; if( !isset($collection) ) { $this->m_strLastError='ÕÒ²»µ½¼¯ºÏ:'.$strCollection; return false; } #²éÕÒÊý¾Ý if($sortarry == array()) { $findData=$this->m_db->$strCollection->find( $findArray, $arrayRetColumn )->skip($skip)->limit($perPage); } else { $findData=$this->m_db->$strCollection->find( $findArray, $arrayRetColumn )->skip($skip)->limit($perPage)->sort($sortarry); } $retArray=iterator_to_array($findData); if( !count( $retArray ) ) { #¼Ç¼ʧ°ÜÔ­Òò $strFind=var_export( $findArray, true ); $this->m_strLastError='ÕÒ²»µ½Êý¾Ý,ÔÚ¼¯ºÏ:'.$strCollection.'²éÕÒÊý×é:'.$strFind; return false; } return true; } #²éѯÊý¾ÝÊÇ·ñ´æÔÚ public function IsDataExist( $strCollectionName, $findArray, &$findRet=null) { $collection=$this->m_db->$strCollectionName; if( !isset($collection) ) { return false; } $findRet=$collection->findOne( $findArray ); if( isset( $findRet ) ) { return true; } return false; } #²åÈëÊý¾Ý public function Insert( $strCollectionName, $arrayInsert, $arrayIndex=null ) { $this->m_db->$strCollectionName->insert($arrayInsert); if( $arrayIndex!=null ) { $this->m_db->$strCollectionName->ensureIndex( $arrayIndex ); } } #¸üÐÂÊý¾Ý public function Update( $strCollectionName, $arraySign, $arrayUpdate ) { $this->m_db->$strCollectionName->update( $arraySign, $arrayUpdate ); } #ɾ³ýÊý¾Ý public function Remove( $strCollectionName, $arraySign ) { $this->m_db->$strCollectionName->remove( $arraySign ); } } ?>