/TGame/TUtil/Sqlite/DB_SQLite.cpp

http://awoe.googlecode.com/ · C++ · 416 lines · 341 code · 52 blank · 23 comment · 35 complexity · 9a000d66409f95f6886e92f10b195ce9 MD5 · raw file

  1. #include "stdafx.h"
  2. #include "DB_SQLite.h"
  3. #include "Log_SQLite.h"
  4. #include "comutil.h"
  5. #include "win32/CharacterSet.h"
  6. #define DEFAULT_LOGFILE_NAME _T("Log_SQLite.log")
  7. #define DEFAULT_LOGDIRECTORY _T("db")
  8. #define MAX_BUFFER_NUM 512
  9. ////////////////////////////////////////////////////////////////////////////////////////////////////
  10. CSQLiteBase::CSQLiteBase()
  11. {
  12. m_LogCallBack = NULL;
  13. m_WorkDirectory.Empty();
  14. m_LogFileName.Empty();
  15. m_IsOpen = false;
  16. SetWorkDirectory((DEFAULT_LOGDIRECTORY));
  17. SetLiteLogName((DEFAULT_LOGFILE_NAME));
  18. }
  19. ////////////////////////////////////////////////////////////////////////////////////////////////////
  20. CSQLiteBase::~CSQLiteBase()
  21. {
  22. }
  23. ////////////////////////////////////////////////////////////////////////////////////////////////////
  24. bool CSQLiteBase::OpenFullPath(const char* FileName)
  25. {
  26. if(FileName==NULL)
  27. {
  28. WriteSQLLog(1,CString("filename null error!"));
  29. return false;
  30. }
  31. try
  32. {
  33. m_db.close();
  34. m_db.open(FileName);
  35. m_IsOpen = true;
  36. }
  37. catch(CppSQLite3Exception ex)
  38. {
  39. m_IsOpen = false;
  40. CString Log;
  41. Log.Format("Open Error!");
  42. WriteSQLLog(1,Log);
  43. return false;
  44. }
  45. return m_IsOpen;
  46. }
  47. ////////////////////////////////////////////////////////////////////////////////////////////////////
  48. bool CSQLiteBase::Open(const char* FileName)
  49. {
  50. if(FileName==NULL)
  51. {
  52. WriteSQLLog(1,CString("filename null error!"));
  53. return false;
  54. }
  55. try
  56. {
  57. m_db.open(FileName);
  58. m_IsOpen = m_db.dbExists();
  59. }
  60. catch(CppSQLite3Exception ex)
  61. {
  62. m_IsOpen = false;
  63. CString Log;
  64. Log.Format("Open Error!");
  65. WriteSQLLog(1,Log);
  66. return false;
  67. }
  68. return m_IsOpen;
  69. }
  70. ////////////////////////////////////////////////////////////////////////////////////////////////////
  71. void CSQLiteBase::Close()
  72. {
  73. m_db.close();
  74. }
  75. ////////////////////////////////////////////////////////////////////////////////////////////////////
  76. void CSQLiteBase::SetWorkDirectory(const char* Directory)
  77. {
  78. m_WorkDirectory = Directory;
  79. }
  80. ////////////////////////////////////////////////////////////////////////////////////////////////////
  81. void CSQLiteBase::SetLiteLogName(const char* LogName)
  82. {
  83. if(LogName==NULL)
  84. return;
  85. if(m_WorkDirectory.IsEmpty())
  86. {
  87. m_LogFileName += DEFAULT_LOGDIRECTORY;
  88. }
  89. else
  90. {
  91. m_LogFileName += m_WorkDirectory;
  92. }
  93. m_LogFileName +="\\";
  94. m_LogFileName += LogName;
  95. }
  96. ////////////////////////////////////////////////////////////////////////////////////////////////////
  97. void CSQLiteBase::WriteSQLLog(int i , const char* szLogError)
  98. {
  99. if(m_LogCallBack!=NULL)
  100. m_LogCallBack(i,szLogError);
  101. }
  102. ////////////////////////////////////////////////////////////////////////////////////////////////////
  103. bool CSQLiteBase::IsOpen()
  104. {
  105. return this->m_IsOpen;
  106. }
  107. ////////////////////////////////////////////////////////////////////////////////////////////////////
  108. bool CSQLiteBase::BeginSqlCommit(SQLiteList& test)
  109. {
  110. int i = 0;
  111. try
  112. {
  113. GetDB()->execDML("begin transaction;");
  114. for (i = 0; i <(int)test.size(); i++)
  115. {
  116. CString str = test[i];
  117. GetDB()->execDML((const char*)test[i]);
  118. }
  119. GetDB()->execDML("commit transaction;");
  120. return true;
  121. }
  122. catch(CppSQLite3Exception& ex)
  123. {
  124. GetDB()->execDML("commit transaction;");
  125. WriteSQLLog(1,ex.errorMessage());
  126. throw i;// throw out the error sql command , so out
  127. return false;
  128. }
  129. }
  130. ////////////////////////////////////////////////////////////////////////////////////////////////////
  131. bool CSQLiteBase::Transcode(const char* Source, unsigned short& numRet)
  132. {
  133. CString strRet;
  134. if (Transcode(Source, strRet) != NULL)
  135. {
  136. numRet = atoi(strRet.GetBuffer());
  137. return true;
  138. }
  139. else
  140. {
  141. numRet = 0;
  142. }
  143. return false;
  144. }
  145. ////////////////////////////////////////////////////////////////////////////////////////////////////
  146. bool CSQLiteBase::Transcode(const char* Source, short& numRet)
  147. {
  148. CString strRet;
  149. if (Transcode(Source, strRet) != NULL)
  150. {
  151. numRet = atoi(strRet.GetBuffer());
  152. return true;
  153. }
  154. else
  155. {
  156. numRet = 0;
  157. }
  158. return false;
  159. }
  160. ////////////////////////////////////////////////////////////////////////////////////////////////////
  161. bool CSQLiteBase::Transcode(const char* Source, int& numRet)
  162. {
  163. CString strRet;
  164. if (Transcode(Source, strRet) != NULL)
  165. {
  166. numRet = atoi(strRet.GetBuffer());
  167. return true;
  168. }
  169. else
  170. {
  171. numRet = 0;
  172. }
  173. return false;
  174. }
  175. ////////////////////////////////////////////////////////////////////////////////////////////////////
  176. bool CSQLiteBase::Transcode(const char* Source, unsigned int& numRet)
  177. {
  178. CString strRet;
  179. if (Transcode(Source, strRet) != NULL)
  180. {
  181. numRet = atoi(strRet.GetBuffer());
  182. return true;
  183. }
  184. else
  185. {
  186. numRet = 0;
  187. }
  188. return false;
  189. }
  190. ////////////////////////////////////////////////////////////////////////////////////////////////////
  191. bool CSQLiteBase::Transcode(const char* Source, bool& flagRet)
  192. {
  193. int num;
  194. if (Transcode(Source, num) == true)
  195. {
  196. return (num != 0);
  197. }
  198. return false;
  199. }
  200. const char* CSQLiteBase::NoTranscode(const char* Source,CString& strRet)
  201. {
  202. int len =0;
  203. strRet.Empty();
  204. if (Source == NULL)
  205. {
  206. return NULL;
  207. }
  208. try
  209. {
  210. strRet = Source ;
  211. }
  212. catch(...)
  213. {
  214. return NULL;
  215. }
  216. return (const char*)strRet;
  217. }
  218. ////////////////////////////////////////////////////////////////////////////////////////////////////
  219. const char* CSQLiteBase::Transcode(const char* Source,CString& strRet)
  220. {
  221. int len =0;
  222. strRet.Empty();
  223. if (Source == NULL)
  224. {
  225. return NULL;
  226. }
  227. try
  228. {
  229. std::string newStr;
  230. CharacterSet::ConvertUTF8ToASCII(Source, newStr);
  231. strRet = newStr.c_str();
  232. }
  233. catch(...)
  234. {
  235. return NULL;
  236. }
  237. return (const char*)strRet;
  238. }
  239. bool CSQLiteBase::Transcode(const char* Source,string& strRet)
  240. {
  241. if (Source == NULL)
  242. {
  243. return false;
  244. }
  245. try
  246. {
  247. CharacterSet::ConvertUTF8ToASCII(Source, strRet);
  248. return true;
  249. }
  250. catch(...)
  251. {
  252. return false;
  253. }
  254. return false;
  255. }
  256. ////////////////////////////////////////////////////////////////////////////////////////////////////
  257. CDBSQLite::CDBSQLite(void)
  258. {
  259. }
  260. ////////////////////////////////////////////////////////////////////////////////////////////////////
  261. CDBSQLite::~CDBSQLite(void)
  262. {
  263. }
  264. ////////////////////////////////////////////////////////////////////////////////////////////////////
  265. bool CDBSQLite::DBQueryTableField(const char* sFieldName, const char* sCondition, CString& strRet)
  266. {
  267. if(!IsOpen())
  268. return false;
  269. if(m_TableName.IsEmpty())
  270. return false;
  271. CString strSQL ;
  272. if(sCondition==NULL)
  273. strSQL.Format("select %s from %s",sFieldName,(const char*)m_TableName);
  274. else
  275. strSQL.Format("select %s from %s where %s",sFieldName,(const char*)m_TableName,sCondition);
  276. try
  277. {
  278. SQLiteDBQuery q = GetDB()->execQuery((const char*)strSQL);
  279. if(q.eof())
  280. return false;
  281. else
  282. {
  283. Transcode(q.fieldValue(0),strRet);
  284. return true;
  285. }
  286. }
  287. catch(CppSQLite3Exception& ex)
  288. {
  289. this->WriteSQLLog(1,ex.errorMessage());
  290. return false;
  291. }
  292. }
  293. ////////////////////////////////////////////////////////////////////////////////////////////////////
  294. bool CDBSQLite::DBQueryTableFieldList(const char* sFieldName, const char* sCondition, CDBSQLite::SQLiteList& strRetList)
  295. {
  296. if(!IsOpen())
  297. return false;
  298. if(m_TableName.IsEmpty())
  299. return false;
  300. CString strSQL ;
  301. if(sCondition==NULL)
  302. strSQL.Format("select %s from %s",sFieldName,(const char*)m_TableName);
  303. else
  304. strSQL.Format("select %s from %s where %s",sFieldName,(const char*)m_TableName,sCondition);
  305. SQLiteDBQuery q = GetDB()->execQuery((const char*)strSQL);
  306. if(q.eof())
  307. return false;
  308. else
  309. {
  310. CString strRet;
  311. while(!q.eof())
  312. {
  313. Transcode(q.fieldValue(0),strRet);
  314. strRetList.push_back(strRet);
  315. q.nextRow();
  316. }
  317. return true;
  318. }
  319. }
  320. ////////////////////////////////////////////////////////////////////////////////////////////////////
  321. unsigned int CDBSQLite::DBQueryTableCount()
  322. {
  323. if(!IsOpen())
  324. return false;
  325. if(m_TableName.IsEmpty())
  326. return false;
  327. const char * sTemp = "select count(*) from %s";
  328. CString strSQL;
  329. strSQL.Format(sTemp,(const char*)m_TableName);
  330. return GetDB()->execScalar((const char*)strSQL);
  331. }
  332. ////////////////////////////////////////////////////////////////////////////////////////////////////
  333. bool CDBSQLite::SetTableName(const char* szTableName)
  334. {
  335. m_TableName = szTableName;
  336. try
  337. {
  338. if(GetDB()->tableExists(szTableName))
  339. return true;
  340. else
  341. return false;
  342. }
  343. catch(...)
  344. {
  345. m_TableName.Empty();
  346. return false;
  347. }
  348. return true;
  349. }
  350. ////////////////////////////////////////////////////////////////////////////////////////////////////
  351. bool CDBSQLite::ExecuteSQL(const char* szSQLCommand)
  352. {
  353. int retValue = 0;
  354. try
  355. {
  356. retValue = GetDB()->execDML(szSQLCommand);
  357. return retValue>0;
  358. }
  359. catch(CppSQLite3Exception& ex)
  360. {
  361. WriteSQLLog(1,ex.errorMessage());
  362. return false;
  363. }
  364. }