ConfigurationInfo

////ConfigurationInfo.h

 

#ifndef CONFIGURATION_H
#define CONFIGURATION_H

#pragma once
//配置文件路徑
#define  CONFIG_FILE_PATH  "conf\\jdbc.properties";

//定義一個用於匹配配置文件的數據類型
typedef struct _conf_info
{
 UINT    port;//數據庫端口
 char host[20];//數據庫名 
 char dbname[50];//使用的數據庫名
 char userName[50];//用戶名
 char userPassword[50];//用戶密碼

}ConfInfo;


//typedef struct _conf_info
//{
// UINT    port;//數據庫端口
// CString host;//數據庫名
// CString dbname;//使用的數據庫名
// CString userName;//用戶名
// CString userPassword;//用戶密碼
//}ConfInfo;

class CConfigurationInfo
{
public:
 CConfigurationInfo(void);
 ~CConfigurationInfo(void);

public:
 //獲得文件處理路徑
 CString GetExeFilePath();

 //獲取系統配置文件信息
 void GetSystemConfInfo(ConfInfo *info);

 /* 將路徑中的\轉換爲\\ */
 void TransformPath(CString &strFilePath);
private:
 //讀取配置爲文件信息
 CString CConfigurationInfo::GetConfigFileInfo(const CString &confFilePath,//配置文件路徑
  const CString &confSectionName,//單元
  const CString &confKeyValue,//鍵值
  const CString &confDefault);//默認值)

};
#endif

 

 

 

 

 

//////ConfigurationInfo.cpp

 


#include "StdAfx.h"
#include "ConfigurationInfo.h"


CConfigurationInfo::CConfigurationInfo(void)
{

}


CConfigurationInfo::~CConfigurationInfo(void)
{
}


CString CConfigurationInfo::GetExeFilePath()
{
 char chConfPath[MAX_PATH] = {0};

 //獲取程序運行路徑
 GetModuleFileName(NULL, chConfPath, MAX_PATH);
 CString strPath =chConfPath;
 strPath.MakeReverse();
 strPath = strPath.Mid(strPath.Find(TEXT("\\")));
 return strPath.MakeReverse() + CONFIG_FILE_PATH;
}


CString CConfigurationInfo::GetConfigFileInfo(const CString &confFilePath,//配置文件路徑
             const CString &confSectionName,//單元
             const CString &confKeyValue,//鍵值
             const CString &confDefault)//默認值
{
 char chTemp[MAX_PATH] = {0};
 CString strTemp;
 int rtncode;
 rtncode = GetPrivateProfileString(confSectionName, confKeyValue, confDefault, chTemp, MAX_PATH, confFilePath);
 strTemp = chTemp;
 if (rtncode <= 0)
 {
  strTemp = "";
 }

 return strTemp;
}


void CConfigurationInfo::GetSystemConfInfo(ConfInfo *info)
{
 CString strResult;
 CString strPath = GetExeFilePath();
 TransformPath(strPath);

 //MYSQL值
 strResult = GetConfigFileInfo(strPath, TEXT("MYSQL"), TEXT("SERVER"), TEXT("localhost"));
 //info->host = strResult;
 strcpy(info->host,strResult);

 //PORT值
 strResult = GetConfigFileInfo(strPath, TEXT("MYSQL"), TEXT("PORT"), TEXT("3306"));
 info->port = atoi(strResult);

 //用戶名值
 strResult = GetConfigFileInfo(strPath, TEXT("MYSQL"), TEXT("USER"), TEXT("root"));
 //info->userName = strResult;
 strcpy(info->userName,strResult);

 //用戶密碼值
 strResult = GetConfigFileInfo(strPath, TEXT("MYSQL"), TEXT("Password"), TEXT("123456"));
 //info->userPassword = strResult;
 strcpy(info->userPassword,strResult);
 //數據庫名
 strResult = GetConfigFileInfo(strPath, TEXT("MYSQL"), TEXT("Database"), TEXT("lda"));
 strcpy(info->dbname,strResult);
 //info->dbname = strResult;
}


void CConfigurationInfo::TransformPath(CString &strFilePath)
{
 int i,iLen;
 iLen = strFilePath.GetLength();
 CString csRetFileName;
 char ch;

 csRetFileName = "";
 for (i=0;i<iLen;i++)
 {
  ch = strFilePath.GetAt(i);

  if (i == 636)
  {
   int a = 0;
  }
  if ( '\\' == ch)
  {
   csRetFileName += '\\';
   i++;
   if(strFilePath.GetAt(i) == '\\')
   {
    csRetFileName += '\\';
   }
   else
   {
    csRetFileName += '\\';
    csRetFileName += strFilePath.GetAt(i);
   }
  }
  else
  {
   csRetFileName += ch;
  }
 }

 strFilePath = csRetFileName;
}

////DealData.h

 

#ifndef DEALDATA_H
#define DEALDATA_H
#pragma once
#include <map>
#include <vector>
#include <queue>
#include <iostream>
#include <iterator>
#include "MySqlHelper.h"
#include "ConfigurationInfo.h"
//結構體,用於保存maxbatch中的相關信息
typedef struct maxbatchinfo
{
 UINT directoryType;
 CString directoryPath;
}MaxBatchInfo;

//定義最大批次的文件路徑容器
typedef vector<CString> VecMaxPath;

//定義一個MAP類型用來聲明MapRunPath類型變量,存放表run_path中的數據對<Test_Project_Id,runPath>
//typedef map<CString, CString> MapRunPath;

//定義一個MapOfDirAndProjectId類型,用來聲明MapOfDirAndProjectId的變量,
//存放表test_project表中的<Test_Project_Id,Project_src_Dir>
//typedef multimap<CString, UINT> MapOfDirAndProjectId;
typedef multimap<CString, MaxBatchInfo> MapOfDirAndProjectId;
class DealData
{
public:
 DealData(void);
 ~DealData(void);
public:
 //獲取test_project最大批次數據
 void GetMaxBatch();

 //獲取run_path所有數據
 //void GetRunPathData();

 //判斷傳入路徑是否在run_path中已經存在
 //bool IsExistInRunPath(const CString strPath);
 bool IsExistInRunPath(const CString projectPath,const CString strPath);
 //獲取對應路徑的ProjectId
 UINT GetIDThroughPath(CString strPath);
 UINT  GetbatchThroughPath(CString strPath);
 //獲得最大批次的文件路徑緩存
 VecMaxPath*  ReturnMaxBatchPath(void);

 //返回runpath所有文件目錄
 //MapRunPath* ReturnRunpath(void);

 //入庫
 void ControlDataInsertOrRead(const CString strSQL);

 //判斷最大批次文件路徑中是否已經存在該記錄
 bool IsExistMaxPathPath(const CString &strPath);

public:
 //數據庫操作類
 MySqlHelper DBHelper;

 //存放最大批次的文件路徑
 VecMaxPath m_vecMaxPath;

 //存放最大批次的文件路徑對應的ProjectId
 MapOfDirAndProjectId m_mapMaxProjectId;

 //存放run_path數據表中所有數據
 //初始化時從數據庫中讀取數據,之後每次向數據庫中插入數據的同時向MAP中插入
 //MapRunPath m_mapRunPath;

};
#endif

 

 

////DealData.cpp


#include "StdAfx.h"
#include "DealData.h"
#include "LogFile.h"

DealData::DealData()
{
 ConfInfo *m_info;
 m_info = new ConfInfo;
 CConfigurationInfo conInfo;
 memset(m_info,0,sizeof(ConfInfo));
 conInfo.GetSystemConfInfo(m_info); 
 //連接數據庫
 int iResult = DBHelper.FunConnectData(m_info->host, m_info->userName, m_info->userPassword,
  m_info->dbname, m_info->port, CLIENT_MULTI_STATEMENTS);
 if (iResult == -1)
 {
  return ;//數據庫連接失敗,此處應失敗的原因告知用戶,否則不方便調試,找不到掃描沒執行的原因
 }
}


DealData::~DealData()
{
 m_vecMaxPath.clear();
 m_mapMaxProjectId.clear();
 //m_mapRunPath.clear(); 
 DBHelper.CloseDataConnect();
}


bool DealData::IsExistMaxPathPath(const CString &strPath)
{
 vector<CString>::iterator itercsVec = m_vecMaxPath.begin();
 while (itercsVec != m_vecMaxPath.end())
 {
  if (strPath == *itercsVec)
  {
   return true;
  }

  itercsVec++;
 }
 return false;
}


void DealData::GetMaxBatch()
{
 m_vecMaxPath.clear();
 m_mapMaxProjectId.clear();
 CString strSQL;
 //獲取最大批次的數據庫語句
 //strSQL.Format(TEXT("SELECT Project_Src_Dir,Test_Project_Id from test_project where batch = (select max(batch) from test_project)"));
 strSQL.Format(TEXT("select directory,type from MonitorDirectory"));
 //typedef pair <UINT, CString> Int_Pair;
 //typedef pair <CString, UINT> Int_PairOne;
 typedef pair <CString, MaxBatchInfo> Int_PairOne;
 MYSQL_RES *SQL_RES  = DBHelper.QuerySQL(strSQL);
 MYSQL_ROW Row;
 CString strRes;
 UINT directoryType = 0;
 MaxBatchInfo tmpMaxBatchInfo;
 while((Row = DBHelper.fetch_QuerySQL(SQL_RES)) !=NULL)
 {
  strRes = Row[0];//路徑
  CConfigurationInfo tmp;
  tmp.TransformPath(strRes);
  if (strRes != "")
  {
   if (!IsExistMaxPathPath(strRes))
   {
    //目錄類型
    directoryType = atoi(Row[1]);
    tmpMaxBatchInfo.directoryPath=strRes;
    tmpMaxBatchInfo.directoryType=directoryType;

    //保存最大年批次文件路徑
    m_vecMaxPath.push_back(strRes);

    //將所有對應文件路徑及工程ID存入m_mapProjectId
    //m_mapMaxProjectId.insert(Int_PairOne(strRes, iProjectId));
    m_mapMaxProjectId.insert(Int_PairOne(strRes, tmpMaxBatchInfo));
   }
  }
 }
 DBHelper.ReleaseAfterQuerySQL(SQL_RES);
}


bool DealData::IsExistInRunPath(const CString projectPath,const CString strPath)
{
 CString strTmp=strPath;
 CConfigurationInfo tmp;
 tmp.TransformPath(strTmp);
  CString strTempProjectPath=projectPath;
  int tmpProjectId=GetIDThroughPath(strTempProjectPath);
  CString strSQL;
  strSQL.Format(TEXT("SELECT runPath from run_path where runPath=\'%s\' and Test_Project_Id=%d"),strTmp,tmpProjectId);
  MYSQL_RES *SQL_RES  = DBHelper.QuerySQL(strSQL);
  MYSQL_ROW Row;
  if((Row = DBHelper.fetch_QuerySQL(SQL_RES)) !=NULL)
  {
   return true;
  }
  DBHelper.ReleaseAfterQuerySQL(SQL_RES); 
 return false;
}

 

 

UINT DealData::GetIDThroughPath(CString strPath)
{
 //GetMaxBatch();
 //multimap<CString, UINT>::iterator iterId = m_mapMaxProjectId.find(strPath);

 //if (iterId != m_mapMaxProjectId.end())
 //{
 // return iterId->second;
 //}
 //return -1;
 //if(m_mapMaxProjectId.empty())
 //{
 // GetMaxBatch();
 //}
 //MaxBatchInfo tmpMaxBatchInfo;
 //multimap<CString, MaxBatchInfo>::iterator iterId = m_mapMaxProjectId.find(strPath);

 //if (iterId != m_mapMaxProjectId.end())
 //{
 // tmpMaxBatchInfo = iterId->second;
 // return tmpMaxBatchInfo.Test_Project_Id;
 //}
 return -1;
}


UINT DealData::GetbatchThroughPath(CString strPath)
{
 //MaxBatchInfo tmpMaxBatchInfo;
 //multimap<CString, MaxBatchInfo>::iterator iterId = m_mapMaxProjectId.find(strPath);

 //if (iterId != m_mapMaxProjectId.end())
 //{
 // tmpMaxBatchInfo = iterId->second;
 // return tmpMaxBatchInfo.batch;
 //}
 return -1;
}


VecMaxPath*  DealData::ReturnMaxBatchPath(void)
{
 return &m_vecMaxPath;
}


void DealData::ControlDataInsertOrRead(const CString strSQL)
{
 try
 {
  //UINT iProjectId = GetIDThroughPath(strSou);
  //UINT ibatch=GetbatchThroughPath(strSou);
  //CString str = strData;
  //CConfigurationInfo tmp;
  //tmp.TransformPath(str);
  //CString strSQL;
  //strSQL.Format(TEXT("insert into run_path(Test_Project_Id,runPath,isDone,batch)"\
  // "values(%d,\'%s\',%d,%d);"), iProjectId, str, 0,ibatch); 
  DBHelper.Start_Transaction();//開始事務
  DBHelper.MultiInsert(strSQL);
  DBHelper.Commit();   //提交事務
 }
 catch (...)
 {
  CTime m_currentTime=CTime::GetCurrentTime();
  CString currentTimeStr;
  currentTimeStr.Format(_T("%04d%02d%02d %02d:%02d:%02d"),
   m_currentTime.GetYear(),
   m_currentTime.GetMonth(),
   m_currentTime.GetDay(),
   m_currentTime.GetHour(),
   m_currentTime.GetMinute(),
   m_currentTime.GetSecond());
  WRITELOG("ERR:"+currentTimeStr+"insert into mysql failed!");
 }
}

 

 

發佈了56 篇原創文章 · 獲贊 5 · 訪問量 81萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章