1+ #include " pch.h"
2+ #include " Database_Client.h"
3+ /* *******************************************************************
4+ // Created: 2022/03/29 15:36:56
5+ // File Name: D:\XEngine_Storage\XEngine_Source\StorageModule_Database\Database_Client\Database_Client.cpp
6+ // File Path: D:\XEngine_Storage\XEngine_Source\StorageModule_Database\Database_Client
7+ // File Base: Database_Client
8+ // File Ext: cpp
9+ // Project: XEngine(网络通信引擎)
10+ // Author: qyt
11+ // Purpose: 客户端数据库操作类
12+ // History:
13+ *********************************************************************/
14+ CDatabase_Client::CDatabase_Client ()
15+ {
16+ }
17+ CDatabase_Client::~CDatabase_Client ()
18+ {
19+
20+ }
21+ // ////////////////////////////////////////////////////////////////////////
22+ // 公有函数
23+ // ////////////////////////////////////////////////////////////////////////
24+ /* *******************************************************************
25+ 函数名称:Database_Client_Init
26+ 函数功能:初始化客户端数据库系统
27+ 参数.一:lpszSQLFile
28+ In/Out:In
29+ 类型:常量字符指针
30+ 可空:N
31+ 意思:输入要操作的SQL文件
32+ 返回值
33+ 类型:逻辑型
34+ 意思:是否成功
35+ 备注:
36+ *********************************************************************/
37+ BOOL CDatabase_Client::Database_Client_Init (LPCTSTR lpszSQLFile)
38+ {
39+ Database_IsErrorOccur = FALSE ;
40+
41+ memset (tszTableName, ' \0 ' , sizeof (tszTableName));
42+ _tcscpy (tszTableName, _T (" FileList" ));
43+ // 创建数据库
44+ if (DataBase_SQLite_Create (lpszSQLFile))
45+ {
46+ Database_Client_CreateTable (tszTableName);
47+ }
48+ else
49+ {
50+ // 如果不是这个错误,说明创建数据库失败了
51+ if (ERROR_HELPCOMPONENTS_DATABASE_SQLITE_CREATE_ISEXIST != DataBase_GetLastError ())
52+ {
53+ Database_IsErrorOccur = TRUE ;
54+ Database_dwErrorCode = DataBase_GetLastError ();
55+ return FALSE ;
56+ }
57+ }
58+ // 打开数据库
59+ if (!DataBase_SQLite_Open (&xhSQL, lpszSQLFile))
60+ {
61+ Database_IsErrorOccur = TRUE ;
62+ Database_dwErrorCode = DataBase_GetLastError ();
63+ return FALSE ;
64+ }
65+ return TRUE ;
66+ }
67+ /* *******************************************************************
68+ 函数名称:Database_Client_Destory
69+ 函数功能:销毁
70+ 返回值
71+ 类型:逻辑型
72+ 意思:是否成功
73+ 备注:
74+ *********************************************************************/
75+ BOOL CDatabase_Client::Database_Client_Destory ()
76+ {
77+ Database_IsErrorOccur = FALSE ;
78+
79+ DataBase_SQLite_Close (xhSQL);
80+ return TRUE ;
81+ }
82+ /* *******************************************************************
83+ 函数名称:Database_Client_FileInsert
84+ 函数功能:插入一个文件数据到数据库中
85+ 参数.一:pSt_DBManage
86+ In/Out:In
87+ 类型:数据结构指针
88+ 可空:N
89+ 意思:要插入的数据信息
90+ 返回值
91+ 类型:逻辑型
92+ 意思:是否成功
93+ 备注:这个结构所有值都必须填充
94+ *********************************************************************/
95+ BOOL CDatabase_Client::Database_Client_FileInsert (XSTORAGECORE_DBFILE* pSt_DBFile)
96+ {
97+ Database_IsErrorOccur = FALSE ;
98+
99+ if (NULL == pSt_DBFile)
100+ {
101+ Database_IsErrorOccur = TRUE ;
102+ Database_dwErrorCode = ERROR_XENGINE_XSTROGE_CORE_DB_INSERTFILE_PARAMENT;
103+ return FALSE ;
104+ }
105+ int nListCount = 0 ;
106+ XSTORAGECORE_DBFILE** ppSt_ListFile;
107+ if (Database_Client_FileQuery (&ppSt_ListFile, &nListCount, NULL , NULL , NULL , pSt_DBFile->st_ProtocolFile .tszFileHash ))
108+ {
109+ BaseLib_OperatorMemory_Free ((void ***)&ppSt_ListFile, nListCount);
110+ return TRUE ;
111+ }
112+ BaseLib_OperatorMemory_Free ((void ***)&ppSt_ListFile, nListCount);
113+
114+ TCHAR tszSQLStatement[2048 ];
115+ memset (tszSQLStatement, ' \0 ' , sizeof (tszSQLStatement));
116+
117+ Database_Help_Insert (tszSQLStatement, pSt_DBFile);
118+ if (!DataBase_SQLite_Exec (xhSQL, tszSQLStatement))
119+ {
120+ Database_IsErrorOccur = TRUE ;
121+ Database_dwErrorCode = DataBase_GetLastError ();
122+ return FALSE ;
123+ }
124+ return TRUE ;
125+ }
126+ /* *******************************************************************
127+ 函数名称:Database_Client_FileDelete
128+ 函数功能:删除一个数据库文件信息
129+ 参数.一:lpszBuckKey
130+ In/Out:In
131+ 类型:常量字符指针
132+ 可空:Y
133+ 意思:所属BUCK名称
134+ 参数.二:lpszFile
135+ In/Out:In
136+ 类型:常量字符指针
137+ 可空:Y
138+ 意思:要删除的文件全路径
139+ 参数.三:lpszHash
140+ In/Out:In
141+ 类型:常量字符指针
142+ 可空:Y
143+ 意思:要删除的文件HASH
144+ 返回值
145+ 类型:逻辑型
146+ 意思:是否成功
147+ 备注:参数不能全为空,不会删除文件
148+ *********************************************************************/
149+ BOOL CDatabase_Client::Database_Client_FileDelete (LPCTSTR lpszBuckKey /* = NULL */ , LPCTSTR lpszFile /* = NULL */ , LPCTSTR lpszHash /* = NULL */ )
150+ {
151+ Database_IsErrorOccur = FALSE ;
152+
153+ if ((NULL == lpszFile) && (NULL == lpszHash))
154+ {
155+ Database_IsErrorOccur = TRUE ;
156+ Database_dwErrorCode = ERROR_XENGINE_XSTROGE_CORE_DB_DELETEFILE_PARAMENT;
157+ return FALSE ;
158+ }
159+ int nListCount = 0 ;
160+ XSTORAGECORE_DBFILE** ppSt_ListFile;
161+ if (!Database_Client_FileQuery (&ppSt_ListFile, &nListCount, NULL , NULL , lpszBuckKey, lpszFile, lpszHash))
162+ {
163+ return FALSE ;
164+ }
165+ // 轮训查找删除
166+ for (int i = 0 ; i < nListCount; i++)
167+ {
168+ TCHAR tszSQLStatement[1024 ];
169+ memset (tszSQLStatement, ' \0 ' , sizeof (tszSQLStatement));
170+ Database_Help_Delete (tszSQLStatement, ppSt_ListFile[i]->tszTableName , lpszBuckKey, lpszFile, lpszHash);
171+
172+ if (!DataBase_SQLite_Exec (xhSQL, tszSQLStatement))
173+ {
174+ Database_IsErrorOccur = TRUE ;
175+ Database_dwErrorCode = DataBase_GetLastError ();
176+ return FALSE ;
177+ }
178+ }
179+
180+ return TRUE ;
181+ }
182+ /* *******************************************************************
183+ 函数名称:Database_Client_FileQuery
184+ 函数功能:查询文件信息
185+ 参数.一:pppSt_ListFile
186+ In/Out:Out
187+ 类型:三级指针
188+ 可空:N
189+ 意思:导出查询到的文件列表,此函数需要调用基础库的内存释放函数
190+ 参数.二:pInt_ListCount
191+ In/Out:Out
192+ 类型:三级指针
193+ 可空:N
194+ 意思:导出文件个数
195+ 参数.三:lpszTimeStart
196+ In/Out:In
197+ 类型:常量字符指针
198+ 可空:Y
199+ 意思:查找开始时间,20190701
200+ 参数.四:lpszTimeEnd
201+ In/Out:In
202+ 类型:常量字符指针
203+ 可空:Y
204+ 意思:查找结束时间,20190730
205+ 参数.五:lpszBuckKey
206+ In/Out:In
207+ 类型:常量字符指针
208+ 可空:Y
209+ 意思:查询的BUCK名
210+ 参数.六:lpszFile
211+ In/Out:In
212+ 类型:常量字符指针
213+ 可空:Y
214+ 意思:要查询的名称
215+ 参数.七:lpszHash
216+ In/Out:In
217+ 类型:常量字符指针
218+ 可空:Y
219+ 意思:要查询的文件HASH
220+ 参数.八:lpszTableName
221+ In/Out:In
222+ 类型:常量字符指针
223+ 可空:Y
224+ 意思:输入要查询的表明,为NULL所有
225+ 返回值
226+ 类型:逻辑型
227+ 意思:是否成功
228+ 备注:返回假可能没有查找到,这条记录不存在.参数lpszFile和lpszHash不能全为空
229+ *********************************************************************/
230+ BOOL CDatabase_Client::Database_Client_FileQuery (XSTORAGECORE_DBFILE*** pppSt_ListFile, int * pInt_ListCount, LPCTSTR lpszTimeStart /* = NULL */ , LPCTSTR lpszTimeEnd /* = NULL */ , LPCTSTR lpszBuckKey /* = NULL */ , LPCTSTR lpszFile /* = NULL */ , LPCTSTR lpszHash /* = NULL */ )
231+ {
232+ Database_IsErrorOccur = FALSE ;
233+
234+ if ((NULL == lpszHash) && (NULL == lpszFile))
235+ {
236+ Database_IsErrorOccur = TRUE ;
237+ Database_dwErrorCode = ERROR_XENGINE_XSTROGE_CORE_DB_QUERYFILE_PARAMENT;
238+ return FALSE ;
239+ }
240+ // 查询
241+ int nLine = 0 ;
242+ int nRow = 0 ;
243+ TCHAR** pptszResult;
244+ TCHAR tszSQLStatement[1024 ];
245+ list<XSTORAGECORE_DBFILE> stl_ListFile;
246+
247+ memset (tszSQLStatement, ' \0 ' , sizeof (tszSQLStatement));
248+
249+ Database_Help_Query (tszSQLStatement, tszTableName, lpszBuckKey, NULL , lpszFile, lpszHash, NULL , lpszTimeStart, lpszTimeEnd);
250+ if (DataBase_SQLite_GetTable (xhSQL, tszSQLStatement, &pptszResult, &nLine, &nRow))
251+ {
252+ int nFiled = nRow;
253+ // 循环获取所有查找到的文件
254+ for (int i = 0 ; i < nLine; i++)
255+ {
256+ XSTORAGECORE_DBFILE st_DBFile;
257+ memset (&st_DBFile, ' \0 ' , sizeof (XSTORAGECORE_DBFILE));
258+
259+ _tcscpy (st_DBFile.tszTableName , tszTableName);
260+ nFiled++;
261+
262+ _tcscpy (st_DBFile.tszBuckKey , pptszResult[nFiled]);
263+ nFiled++;
264+
265+ _tcscpy (st_DBFile.st_ProtocolFile .tszFilePath , pptszResult[nFiled]);
266+ nFiled++;
267+
268+ _tcscpy (st_DBFile.st_ProtocolFile .tszFileName , pptszResult[nFiled]);
269+ nFiled++;
270+
271+ _tcscpy (st_DBFile.st_ProtocolFile .tszFileHash , pptszResult[nFiled]);
272+ nFiled++;
273+
274+ _tcscpy (st_DBFile.st_ProtocolFile .tszFileUser , pptszResult[nFiled]);
275+ nFiled++;
276+
277+ st_DBFile.st_ProtocolFile .nFileSize = _ttoi64 (pptszResult[nFiled]);
278+ nFiled++;
279+
280+ _tcscpy (st_DBFile.st_ProtocolFile .tszFileTime , pptszResult[nFiled]);
281+ nFiled++;
282+ stl_ListFile.push_back (st_DBFile);
283+ }
284+ DataBase_SQLite_FreeTable (pptszResult);
285+ }
286+ if (stl_ListFile.empty ())
287+ {
288+ Database_IsErrorOccur = TRUE ;
289+ Database_dwErrorCode = ERROR_XENGINE_XSTROGE_CORE_DB_QUERYFILE_EMPTY;
290+ return FALSE ;
291+ }
292+ BaseLib_OperatorMemory_Malloc ((XPPPMEM)pppSt_ListFile, stl_ListFile.size (), sizeof (XSTORAGECORE_DBFILE));
293+
294+ list<XSTORAGECORE_DBFILE>::const_iterator stl_ListIterator = stl_ListFile.begin ();
295+ for (int i = 0 ; stl_ListIterator != stl_ListFile.end (); stl_ListIterator++, i++)
296+ {
297+ *(*pppSt_ListFile)[i] = *stl_ListIterator;
298+ }
299+ *pInt_ListCount = stl_ListFile.size ();
300+ stl_ListFile.clear ();
301+ return TRUE ;
302+ }
303+ // ////////////////////////////////////////////////////////////////////////
304+ // 保护函数
305+ // ////////////////////////////////////////////////////////////////////////
306+ /* *******************************************************************
307+ 函数名称:Database_Client_CreateTable
308+ 函数功能:创建表
309+ 参数.一:lpszTableName
310+ In/Out:In
311+ 类型:常量字符指针
312+ 可空:N
313+ 意思:输入表名称
314+ 返回值
315+ 类型:逻辑型
316+ 意思:是否成功
317+ 备注:
318+ *********************************************************************/
319+ BOOL CDatabase_Client::Database_Client_CreateTable (LPCTSTR lpszTableName)
320+ {
321+ Database_IsErrorOccur = FALSE ;
322+
323+ TCHAR tszSQLQuery[2048 ];
324+ memset (tszSQLQuery, ' \0 ' , sizeof (tszSQLQuery));
325+
326+ _stprintf_s (tszSQLQuery, _T (" PRAGMA foreign_keys = false;"
327+ " CREATE TABLE IF NOT EXISTS \" %s\" ("
328+ " \" ID\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
329+ " \" BuckKey\" TEXT,"
330+ " \" FilePath\" TEXT NOT NULL,"
331+ " \" FileName\" TEXT NOT NULL,"
332+ " \" FileHash\" TEXT,"
333+ " \" FileUser\" TEXT,"
334+ " \" FileSize\" integer NOT NULL,"
335+ " \" FileTime\" TEXT NOT NULL"
336+ " );"
337+ " PRAGMA foreign_keys = true;"
338+ ), lpszTableName);
339+
340+ if (!DataBase_SQLite_Exec (xhSQL, tszSQLQuery))
341+ {
342+ Database_IsErrorOccur = TRUE ;
343+ Database_dwErrorCode = DataBase_GetLastError ();
344+ return FALSE ;
345+ }
346+ return TRUE ;
347+ }
0 commit comments