source: svn/trunk/zasc/app/DST_DB_Engine.cpp @ 22

Last change on this file since 22 was 2, checked in by phkim, 11 years ago

1.phkim

  1. revision copy newcon3sk r27
File size: 4.5 KB
Line 
1#include "DST_DB_Engine.h"
2#include "dst_eroum_interface.h"
3#include <sqlite3.h>
4
5#define DB_ENGINE_DEBUG 0
6sqlite3 *g_db =0;
7
8static void Lock(bool bLock)
9{
10        static DS_U32 sema4 = 0;
11        if (sema4== 0) sema4 = DST_OS_CreateLock((char*)"db");
12        bLock ? DST_OS_Lock(sema4) : DST_OS_Unlock(sema4);
13}
14
15CDB::CDB()
16{
17        mRow = 0;
18        mCol = 0;
19        mResult = 0;
20}
21
22CDB::~CDB()
23{
24        if (mResult)
25        {
26                for (int i = 0; i < (mRow+1)*mCol; i++) DST_OS_Free(&mResult[i]);
27                DST_OS_Free(&mResult);
28                mRow = 0;
29                mCol = 0;                         
30        }
31}
32
33int CDB::Query(const char* strSQL, ...)
34{
35        if (g_db == 0 || strSQL == 0) return 0;
36        if (mResult)
37        {
38                for (int i = 0; i < (mRow+1)*mCol; i++) DST_OS_Free(&mResult[i]);
39                DST_OS_Free(&mResult);
40                mRow = 0;
41                mCol = 0;                         
42        }
43
44        Lock(true); 
45        va_list ap;
46        char *z;
47        va_start(ap, strSQL);
48        z = sqlite3_vmprintf(strSQL, ap);
49        va_end(ap);
50#if DB_ENGINE_DEBUG
51        DST_Printf("CT_Query|%s\n", z);
52#endif
53        char *ErrMsg = 0;
54
55        int rc = sqlite3_exec(g_db, z, 0, 0, &ErrMsg);
56       
57        if (rc)
58        {
59                DST_Printf("CT_Query|%s\n", z);
60                DST_Printf("CT_Query|%d|%s\n", rc, ErrMsg);
61                sqlite3_free(ErrMsg);
62        }
63        sqlite3_free(z);
64        Lock(false); 
65  return rc;
66}
67
68int CDB::GetTable(const char* strSQL, ...)
69{
70        if (g_db == 0 || strSQL == 0) return 0;
71        if (mResult)
72        {
73                for (int i = 0; i < (mRow+1)*mCol; i++) DST_OS_Free(&mResult[i]);
74                DST_OS_Free(&mResult);
75                mRow = 0;
76                mCol = 0;                         
77        }
78
79        Lock(true);
80
81        va_list ap;
82        char *z;
83        va_start(ap, strSQL);
84        z = sqlite3_vmprintf(strSQL, ap);
85        va_end(ap);
86#if DB_ENGINE_DEBUG
87                DST_Printf("CT_GetTable|%s\n", z);
88#endif
89        char *ErrMsg = 0;
90       
91        char **mResult1 = 0;   
92        int rc = sqlite3_get_table(g_db, z, &mResult1, &mRow, &mCol, &ErrMsg);
93       
94        if (rc)
95        {
96                DST_Printf("CT_GetTable|%s\n", z);
97                DST_Printf("CT_GetTable|%d|%s\n", rc, ErrMsg);
98                sqlite3_free(ErrMsg);
99        }
100        else
101        {
102                if (mResult1)
103                {
104                        mResult = (char**)DST_OS_Calloc((mRow+1)*mCol, sizeof(char*));   
105                        for (int i = 0; i < (mRow+1)*mCol; i++)                         
106                        { 
107                                unsigned nLen = 0;
108                                if (mResult1[i]) nLen = strlen(mResult1[i]);                                                                 
109                                if (nLen == 0) continue;                                       
110                                mResult[i] = (char*)DST_OS_Calloc(nLen+1,1);                     
111                                strcpy(mResult[i], mResult1[i]);                               
112                        }
113                }                                                               
114        }
115        if (mResult1) sqlite3_free_table(mResult1); 
116        sqlite3_free(z);
117        Lock(false); 
118  return rc;
119}
120
121int CDB::GetRow()
122{
123        return mRow;
124}
125
126int CDB::GetCol()
127{
128        return mCol;
129}
130
131char* CDB::GetResult(int i)
132{
133        static char* strNull =(char*)"";
134        if(i >= (mRow+1)*mCol) 
135        {
136                DST_Printf("%s|%d|Out of range. mRow:%d ,mCol:%d, i:%d\n", __func__, __LINE__,mRow,mCol,i);
137                return (char*)"";
138        }
139        return mResult[i] == 0 ? strNull : mResult[i];
140}
141
142#ifdef SQLITE_OS_OTHER
143
144int dst_sqlite_randomness(sqlite3_vfs*, int nByte, char *zOut)
145{
146        memset(zOut, 0, nByte);
147        return nByte;
148}
149
150extern "C" SQLITE_API int sqlite3_os_init(void)
151{
152  static sqlite3_vfs aVfs = {                       
153    3,                    /* iVersion */                   
154    0,     /* szOsFile */                   
155    255,         /* mxPathname */                 
156    0,                    /* pNext */                       
157    0,              /* zName */                       
158    0,       /* pAppData */                   
159    0,             /* xOpen */                       
160    0,           /* xDelete */                     
161    0,           /* xAccess */                     
162    0,     /* xFullPathname */               
163    0,           /* xDlOpen */                     
164    0,          /* xDlError */                   
165    0,            /* xDlSym */                     
166    0,          /* xDlClose */                   
167    dst_sqlite_randomness,       /* xRandomness */                 
168    0,            /* xSleep */                     
169    0,      /* xCurrentTime */               
170    0,     /* xGetLastError */               
171    0, /* xCurrentTimeInt64 */           
172    0,    /* xSetSystemCall */             
173    0,    /* xGetSystemCall */             
174    0   /* xNextSystemCall */             
175  };
176        sqlite3_vfs_register(&aVfs,1);
177       
178       
179  return SQLITE_OK; 
180}
181
182extern "C" SQLITE_API int sqlite3_os_end(void)
183{ 
184  return SQLITE_OK; 
185}
186
187#endif
Note: See TracBrowser for help on using the repository browser.