]>
Commit | Line | Data |
---|---|---|
1 | //---------------------------------------------------------------------------------------- | |
2 | // Name: BrowserDB.h,cpp | |
3 | // Purpose: a wxDB class | |
4 | // Author: Mark Johnson | |
5 | // Modified by: | |
6 | // Created: 19991127.mj10777 | |
7 | // Copyright: (c) Mark Johnson | |
8 | // Licence: wxWindows license | |
9 | // RCS-ID: $Id$ | |
10 | //---------------------------------------------------------------------------------------- | |
11 | //-- 1) | |
12 | //---------------------------------------------------------------------------------------- | |
13 | //-- all #ifdefs that the whole Project needs. ------------------------------------------- | |
14 | //---------------------------------------------------------------------------------------- | |
15 | #ifdef __GNUG__ | |
16 | #pragma implementation | |
17 | #pragma interface | |
18 | #endif | |
19 | //---------------------------------------------------------------------------------------- | |
20 | // For compilers that support precompilation, includes "wx/wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | //---------------------------------------------------------------------------------------- | |
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | //---------------------------------------------------------------------------------------- | |
27 | #ifndef WX_PRECOMP | |
28 | #include "wx/wx.h" | |
29 | #endif | |
30 | //---------------------------------------------------------------------------------------- | |
31 | #include "std.h" | |
32 | //---------------------------------------------------------------------------------------- | |
33 | // Global structure for holding ODBC connection information | |
34 | wxDbConnectInf DbConnectInf; | |
35 | ||
36 | #if !wxUSE_ODBC | |
37 | #error Demo cannot be compiled unless setup.h has wxUSE_ODBC set to 1 | |
38 | #endif | |
39 | ||
40 | //---------------------------------------------------------------------------------------- | |
41 | extern WXDLLEXPORT_DATA(wxDbList*) PtrBegDbList; /* from db.cpp, used in getting back error results from db connections */ | |
42 | ||
43 | //---------------------------------------------------------------------------------------- | |
44 | wxChar *GetExtendedDBErrorMsg(wxChar *ErrFile, int ErrLine) | |
45 | { | |
46 | static wxString msg; | |
47 | wxString tStr; | |
48 | if (ErrFile || ErrLine) | |
49 | { | |
50 | msg += _T("File: "); | |
51 | msg += ErrFile; | |
52 | msg += _T(" Line: "); | |
53 | tStr.Printf(_T("%d"),ErrLine); | |
54 | msg += tStr.GetData(); | |
55 | // msg += _T("\n"); | |
56 | } | |
57 | msg.Append (_T("\nODBC errors:\n")); | |
58 | // msg += _T("\n"); | |
59 | /* Scan through each database connection displaying | |
60 | * any ODBC errors that have occured. */ | |
61 | wxDbList *pDbList; | |
62 | for (pDbList = PtrBegDbList; pDbList; pDbList = pDbList->PtrNext) | |
63 | { | |
64 | // Skip over any free connections | |
65 | if (pDbList->Free) | |
66 | continue; | |
67 | // Display errors for this connection | |
68 | for (int i = 0; i < DB_MAX_ERROR_HISTORY; i++) | |
69 | { | |
70 | if (pDbList->PtrDb->errorList[i]) | |
71 | { | |
72 | msg.Append(pDbList->PtrDb->errorList[i]); | |
73 | if (wxStrcmp(pDbList->PtrDb->errorList[i],_T("")) != 0) | |
74 | msg.Append(_T("\n")); | |
75 | } | |
76 | } | |
77 | } | |
78 | msg += _T("\n"); | |
79 | return (wxChar*) (const wxChar*) msg; | |
80 | } // GetExtendedDBErrorMsg | |
81 | ||
82 | //---------------------------------------------------------------------------------------- | |
83 | BrowserDB::BrowserDB() | |
84 | { | |
85 | PointerToNULL(0); | |
86 | ODBCSource = _T(""); // ODBC data source name (created with ODBC Administrator under Win95/NT) | |
87 | UserName = _T(""); // database username - must already exist in the data source | |
88 | Password = _T(""); // password database username | |
89 | OnFillSqlTyp(); | |
90 | OnFilldbTyp(); | |
91 | } // BrowserDB Constructor | |
92 | ||
93 | //---------------------------------------------------------------------------------------- | |
94 | BrowserDB::~BrowserDB() | |
95 | { | |
96 | PointerToNULL(1); // Clean up Tables and Databases (Commit, Close and delete) | |
97 | } // BrowserDB destructor | |
98 | ||
99 | //---------------------------------------------------------------------------------------- | |
100 | bool BrowserDB::Initialize(int Quiet) | |
101 | { | |
102 | if (!OnStartDB(Quiet)) | |
103 | { | |
104 | wxLogMessage(_("\n\n-E-> BrowserDB::OnStartDB(%s) : Failed ! "),ODBCSource.c_str()); | |
105 | return FALSE; | |
106 | } | |
107 | return TRUE; | |
108 | } // BrowserDB:Initialize | |
109 | ||
110 | //---------------------------------------------------------------------------------------- | |
111 | bool BrowserDB::OnStartDB(int Quiet) | |
112 | { | |
113 | wxStopWatch sw; | |
114 | if (!Quiet) | |
115 | wxLogMessage(_("\n-I-> BrowserDB::OnStartDB(%s) : Begin "),ODBCSource.c_str()); | |
116 | if (db_BrowserDB != NULL) | |
117 | { | |
118 | if (!Quiet) | |
119 | wxLogMessage(_("\n-I-> BrowserDB::OnStartDB() : DB is already open.")); | |
120 | return TRUE; | |
121 | } | |
122 | ||
123 | DbConnectInf.AllocHenv(); | |
124 | ||
125 | //--------------------------------------------------------------------------------------- | |
126 | // Connect to datasource | |
127 | //--------------------------------------------------------------------------------------- | |
128 | DlgUser *p_Dlg; | |
129 | p_Dlg = new DlgUser(pDoc->p_MainFrame,pDoc,_T("")); | |
130 | p_Dlg->s_DSN = ODBCSource; | |
131 | p_Dlg->s_User = UserName; | |
132 | p_Dlg->s_Password = Password; | |
133 | p_Dlg->OnInit(); | |
134 | p_Dlg->Fit(); | |
135 | ||
136 | bool OK = FALSE; | |
137 | if (p_Dlg->ShowModal() == wxID_OK) | |
138 | { | |
139 | (pDoc->p_DSN+i_Which)->Usr = p_Dlg->s_User; | |
140 | (pDoc->p_DSN+i_Which)->Pas = p_Dlg->s_Password; | |
141 | UserName = p_Dlg->s_User; | |
142 | Password = p_Dlg->s_Password; | |
143 | OK = TRUE; | |
144 | } | |
145 | delete p_Dlg; | |
146 | if (OK) | |
147 | { | |
148 | //-------------------------------------------------------------------------------------- | |
149 | DbConnectInf.SetDsn(ODBCSource); // ODBC data source name (created with ODBC Administrator under Win95/NT) | |
150 | DbConnectInf.SetUserID(UserName); // database username - must already exist in the data source | |
151 | DbConnectInf.SetPassword(Password); // password database username | |
152 | db_BrowserDB = wxDbGetConnection(&DbConnectInf); | |
153 | // wxLogMessage(">>>%s<<<>>>%s<<<",UserName.c_str(),Password.c_str()); | |
154 | if (db_BrowserDB == NULL) | |
155 | { | |
156 | DbConnectInf.SetDsn(wxT("")); | |
157 | DbConnectInf.SetUserID(wxT("")); | |
158 | DbConnectInf.SetPassword(wxT("")); | |
159 | if (!Quiet) | |
160 | { | |
161 | wxLogMessage(_("\n-E-> BrowserDB::OnConnectDataSource() DB CONNECTION ERROR : Unable to connect to the data source.\n\nCheck the name of your data source to verify it has been correctly entered/spelled.\n\nWith some databases, the user name and password must\nbe created with full rights to the table prior to making a connection\n(using tools provided by the database manufacturer)")); | |
162 | wxLogMessage(_("-I-> BrowserDB::OnStartDB(%s) : End - Time needed : %ld ms"),ODBCSource.c_str(),sw.Time()); | |
163 | } | |
164 | DbConnectInf.FreeHenv(); | |
165 | return FALSE; | |
166 | } | |
167 | //-------------------------------------------------------------------------------------- | |
168 | if (!Quiet) | |
169 | { | |
170 | Temp1 = db_BrowserDB->GetDatabaseName(); | |
171 | Temp2 = db_BrowserDB->GetDataSource(); | |
172 | wxLogMessage(_("-I-> BrowserDB::OnGetDataSourceODBC() - DatabaseName(%s) ; DataSource(%s)"),Temp1.c_str(),Temp2.c_str()); | |
173 | wxLogMessage(_("-I-> BrowserDB::OnStartDB(%s) : End - Time needed : %ld ms"),ODBCSource.c_str(),sw.Time()); | |
174 | } | |
175 | return TRUE; | |
176 | } | |
177 | else | |
178 | { | |
179 | DbConnectInf.FreeHenv(); | |
180 | return FALSE; | |
181 | } | |
182 | } | |
183 | ||
184 | //---------------------------------------------------------------------------------------- | |
185 | bool BrowserDB::OnCloseDB(int Quiet) | |
186 | { | |
187 | if (!Quiet) | |
188 | wxLogMessage(_("-I-> BrowserDB::OnCloseDB() : Begin ")); | |
189 | if (db_BrowserDB) | |
190 | { | |
191 | // db_BrowserDB->Close(); | |
192 | wxDbFreeConnection(db_BrowserDB); | |
193 | ||
194 | DbConnectInf.FreeHenv(); | |
195 | ||
196 | db_BrowserDB = NULL; | |
197 | } | |
198 | if (!Quiet) | |
199 | wxLogMessage(_("\n-I-> BrowserDB::OnCloseDB() : End ")); | |
200 | return TRUE; | |
201 | } | |
202 | ||
203 | //---------------------------------------------------------------------------------------- | |
204 | bool BrowserDB::OnGetNext(int Cols,int WXUNUSED(Quiet)) | |
205 | { | |
206 | SDWORD cb; | |
207 | int i_dbDataType; | |
208 | wxChar s_temp[1024+1]; | |
209 | long l_temp; | |
210 | double f_temp; | |
211 | int AnzError=0; | |
212 | TIMESTAMP_STRUCT t_temp; | |
213 | wxString Temp0; | |
214 | //----------------------------- | |
215 | if (!db_BrowserDB->GetNext()) | |
216 | { | |
217 | #ifdef __WXDEBUG__ | |
218 | Temp0.Printf(_("\n-E-> BrowserDB::OnGetNext - ODBC-Error with GetNext \n-E-> ")); | |
219 | Temp0 += GetExtendedDBErrorMsg(__TFILE__,__LINE__); | |
220 | wxLogMessage(Temp0); | |
221 | wxMessageBox(Temp0); | |
222 | #endif | |
223 | return FALSE; | |
224 | } | |
225 | else | |
226 | { | |
227 | int i; | |
228 | for (i=0;i<Cols;i++) | |
229 | { | |
230 | wxStrcpy((cl_BrowserDB+i)->tableName,_T("-E->")); | |
231 | i_dbDataType = (cl_BrowserDB+i)->pColFor->i_dbDataType; | |
232 | if (i_dbDataType == 0) // Filter unsupported dbDataTypes | |
233 | { | |
234 | if (((cl_BrowserDB+i)->pColFor->i_sqlDataType == SQL_VARCHAR) || | |
235 | ((cl_BrowserDB+i)->pColFor->i_sqlDataType == SQL_LONGVARCHAR)) | |
236 | i_dbDataType = DB_DATA_TYPE_VARCHAR; | |
237 | if ((cl_BrowserDB+i)->pColFor->i_sqlDataType == SQL_C_DATE) | |
238 | i_dbDataType = DB_DATA_TYPE_DATE; | |
239 | if ((cl_BrowserDB+i)->pColFor->i_sqlDataType == SQL_C_BIT) | |
240 | i_dbDataType = DB_DATA_TYPE_INTEGER; | |
241 | if ((cl_BrowserDB+i)->pColFor->i_sqlDataType == SQL_NUMERIC) | |
242 | i_dbDataType = DB_DATA_TYPE_VARCHAR; | |
243 | if ((cl_BrowserDB+i)->pColFor->i_sqlDataType == SQL_REAL) | |
244 | i_dbDataType = DB_DATA_TYPE_FLOAT; | |
245 | } | |
246 | if ((i_dbDataType == DB_DATA_TYPE_INTEGER) && | |
247 | ((cl_BrowserDB+i)->pColFor->i_sqlDataType == SQL_C_DOUBLE)) | |
248 | { // DBASE Numeric | |
249 | i_dbDataType = DB_DATA_TYPE_FLOAT; | |
250 | } | |
251 | switch(i_dbDataType) | |
252 | { | |
253 | case DB_DATA_TYPE_VARCHAR: | |
254 | wxStrcpy(s_temp,_T("")); | |
255 | if (!db_BrowserDB->GetData(i+1,(cl_BrowserDB+i)->pColFor->i_dbDataType,&s_temp,sizeof(s_temp), &cb)) | |
256 | { | |
257 | Temp0.Printf(_("\n-E-> BrowserDB::OnGetNext - ODBC-Error with GetNext of >%s<.\n-E-> "),(cl_BrowserDB+i)->tableName); | |
258 | Temp0 += GetExtendedDBErrorMsg(__TFILE__,__LINE__); | |
259 | wxLogMessage(Temp0); | |
260 | } | |
261 | Temp0.Printf((cl_BrowserDB+i)->pColFor->s_Field,s_temp); | |
262 | wxStrcpy((cl_BrowserDB+i)->tableName,Temp0.c_str()); | |
263 | break; | |
264 | case DB_DATA_TYPE_INTEGER: | |
265 | l_temp = 0; | |
266 | if (!db_BrowserDB->GetData(i+1,(cl_BrowserDB+i)->pColFor->i_sqlDataType,&l_temp,sizeof(l_temp), &cb)) | |
267 | { | |
268 | Temp0.Printf(_("\n-E-> BrowserDB::OnGetData - ODBC-Error with GetNext \n-E-> ")); | |
269 | Temp0 += GetExtendedDBErrorMsg(__TFILE__,__LINE__); | |
270 | } | |
271 | else | |
272 | { | |
273 | Temp0.Printf((cl_BrowserDB+i)->pColFor->s_Field,l_temp); | |
274 | wxStrcpy((cl_BrowserDB+i)->tableName,Temp0.c_str()); | |
275 | } | |
276 | break; | |
277 | case DB_DATA_TYPE_FLOAT: | |
278 | f_temp = 0; | |
279 | if (!db_BrowserDB->GetData(i+1,(cl_BrowserDB+i)->pColFor->i_sqlDataType,&f_temp,sizeof(f_temp), &cb)) | |
280 | { | |
281 | Temp0.Printf(_("\n-E-> BrowserDB::OnGetData - ODBC-Error with GetNext \n-E-> ")); | |
282 | Temp0 += GetExtendedDBErrorMsg(__TFILE__,__LINE__); | |
283 | wxMessageBox(Temp0); | |
284 | } | |
285 | else | |
286 | { | |
287 | Temp0.Printf((cl_BrowserDB+i)->pColFor->s_Field,f_temp); | |
288 | wxStrcpy((cl_BrowserDB+i)->tableName,Temp0.c_str()); | |
289 | } | |
290 | break; | |
291 | case DB_DATA_TYPE_DATE: | |
292 | t_temp.day = t_temp.month = t_temp.year = t_temp.hour = t_temp.minute = t_temp.second = t_temp.fraction = 0; | |
293 | if (!db_BrowserDB->GetData(i+1,(cl_BrowserDB+i)->pColFor->i_sqlDataType,&t_temp,sizeof(t_temp), &cb)) | |
294 | { | |
295 | Temp0.Printf(_("\n-E-> BrowserDB::OnGetData - ODBC-Error with GetNext \n-E-> ")); | |
296 | Temp0 += GetExtendedDBErrorMsg(__TFILE__,__LINE__); | |
297 | } | |
298 | else | |
299 | { | |
300 | // i_Nation = 0 = timestamp , 1=EU, 2=UK, 3=International, 4=US | |
301 | if (((cl_BrowserDB+i)->pColFor->i_Nation == 0) || // TS YYYY-MM-DD | |
302 | ((cl_BrowserDB+i)->pColFor->i_Nation == 3)) // IT YYYY-MM-DD | |
303 | { | |
304 | Temp0.Printf((cl_BrowserDB+i)->pColFor->s_Field,t_temp.year,t_temp.month,t_temp.day, | |
305 | t_temp.hour, t_temp.minute, t_temp.second, t_temp.fraction); | |
306 | wxStrcpy((cl_BrowserDB+i)->tableName,Temp0.c_str()); | |
307 | } | |
308 | if (((cl_BrowserDB+i)->pColFor->i_Nation == 1) || // EU DD.MM.YYYY | |
309 | ((cl_BrowserDB+i)->pColFor->i_Nation == 2)) // UK DD/MM/YYYY | |
310 | { | |
311 | Temp0.Printf((cl_BrowserDB+i)->pColFor->s_Field,t_temp.day,t_temp.month,t_temp.year, | |
312 | t_temp.hour, t_temp.minute, t_temp.second, t_temp.fraction); | |
313 | wxStrcpy((cl_BrowserDB+i)->tableName,Temp0.c_str()); | |
314 | } | |
315 | if ((cl_BrowserDB+i)->pColFor->i_Nation == 3) // US MM/DD/YYYY | |
316 | { | |
317 | Temp0.Printf((cl_BrowserDB+i)->pColFor->s_Field,t_temp.month,t_temp.day,t_temp.year, | |
318 | t_temp.hour, t_temp.minute, t_temp.second, t_temp.fraction); | |
319 | wxStrcpy((cl_BrowserDB+i)->tableName,Temp0.c_str()); | |
320 | } | |
321 | } | |
322 | break; | |
323 | default: | |
324 | AnzError++; | |
325 | if (AnzError <= 100) | |
326 | { | |
327 | Temp0 = (cl_BrowserDB+i)->colName; | |
328 | wxLogMessage(_("-E-> BrowserDB::OnGetNext - DB_DATA_TYPE_?? (%d) in Col(%s)"),(cl_BrowserDB+i)->pColFor->i_dbDataType,Temp0.c_str()); | |
329 | } | |
330 | else | |
331 | return TRUE; | |
332 | Temp0.Printf(_("-E-> unknown Format(%d) - sql(%d)"),(cl_BrowserDB+i)->pColFor->i_dbDataType,(cl_BrowserDB+i)->pColFor->i_sqlDataType); | |
333 | wxStrcpy((cl_BrowserDB+i)->tableName,Temp0.c_str()); | |
334 | break; | |
335 | }; // switch | |
336 | } // for | |
337 | } // else | |
338 | ||
339 | return TRUE; | |
340 | } | |
341 | ||
342 | //---------------------------------------------------------------------------------------- | |
343 | bool BrowserDB::OnSelect(wxString tb_Name, int Quiet) | |
344 | { | |
345 | wxStopWatch sw; | |
346 | wxString SQLStmt; | |
347 | i_Records = 0; | |
348 | //--------------------------------------------------------------------------------------- | |
349 | wxString tablename = db_BrowserDB->SQLTableName(tb_Name.c_str()); | |
350 | SQLStmt.sprintf(_T("SELECT * FROM %s"),tablename.c_str()); | |
351 | if (!db_BrowserDB->ExecSql((wxChar *)(SQLStmt.GetData()))) | |
352 | { | |
353 | Temp0.Printf(_("\n-E-> BrowserDB::OnSelect - ODBC-Error with ExecSql of >%s<.\n-E-> "),tb_Name.c_str()); | |
354 | Temp0 += GetExtendedDBErrorMsg(__TFILE__,__LINE__); | |
355 | wxLogMessage(Temp0); | |
356 | wxMessageBox(_T("-E-> BrowserDB::OnSelect - GetData()")); | |
357 | return FALSE; | |
358 | } | |
359 | //--------------------------------------------------------------------------------------- | |
360 | while (db_BrowserDB->GetNext()) | |
361 | { | |
362 | i_Records++; | |
363 | } | |
364 | //--------------------------------------------------------------------------------------- | |
365 | if (!db_BrowserDB->ExecSql((wxChar *)(SQLStmt.GetData()))) | |
366 | { | |
367 | Temp0.Printf(_("\n-E-> BrowserDB::OnSelect - ODBC-Error with ExecSql of >%s<.\n-E-> "),tb_Name.c_str()); | |
368 | Temp0 += GetExtendedDBErrorMsg(__TFILE__,__LINE__); | |
369 | wxLogMessage(Temp0); | |
370 | return FALSE; | |
371 | } | |
372 | //--------------------------------------------------------------------------------------- | |
373 | // SetColDefs ( 0,"NAME", DB_DATA_TYPE_VARCHAR, Name, SQL_C_CHAR, sizeof(Name), TRUE, TRUE); // Primary index | |
374 | //--------------------------------------------------------------------------------------- | |
375 | if (!Quiet) | |
376 | { | |
377 | wxLogMessage(_("\n-I-> BrowserDB::OnSelect(%s) Records(%d): End - Time needed : %ld ms"),tb_Name.c_str(),i_Records,sw.Time()); | |
378 | } | |
379 | return TRUE; | |
380 | } | |
381 | ||
382 | //---------------------------------------------------------------------------------------- | |
383 | bool BrowserDB::OnExecSql(wxString SQLStmt, int Quiet) | |
384 | { | |
385 | //--------------------------------------------------------------------------------------- | |
386 | if (!db_BrowserDB->ExecSql((wxChar *)(SQLStmt.GetData()))) | |
387 | { | |
388 | Temp0.Printf(_("\n-E-> BrowserDB::OnExecSQL - ODBC-Error with ExecSql of >%s<.\n-E-> "),SQLStmt.c_str()); | |
389 | Temp0 += GetExtendedDBErrorMsg(__TFILE__,__LINE__); | |
390 | if (!Quiet) | |
391 | wxLogMessage(Temp0); | |
392 | else | |
393 | wxMessageBox(_T("-E-> BrowserDB::OnExecSql - ExecSql()")); | |
394 | return FALSE; | |
395 | } | |
396 | if (!Quiet) | |
397 | { | |
398 | // wxLogMessage(_("\n-I-> BrowserDB::OnExecSql(%s) - End - Time needed : %ld ms"),SQLStmt.c_str(),sw.Time()); | |
399 | } | |
400 | return TRUE; | |
401 | } | |
402 | ||
403 | //---------------------------------------------------------------------------------------- | |
404 | wxDbInf* BrowserDB::OnGetCatalog(int WXUNUSED(Quiet)) | |
405 | { | |
406 | wxChar UName[255]; | |
407 | wxStrcpy(UName,UserName); | |
408 | ct_BrowserDB = db_BrowserDB->GetCatalog(UName); | |
409 | return ct_BrowserDB; | |
410 | } | |
411 | ||
412 | //---------------------------------------------------------------------------------------- | |
413 | wxDbColInf* BrowserDB::OnGetColumns(wxChar *tableName, UWORD numCols, int WXUNUSED(Quiet)) | |
414 | { | |
415 | wxChar UName[255]; | |
416 | int i; | |
417 | wxStrcpy(UName,UserName); | |
418 | cl_BrowserDB = db_BrowserDB->GetColumns(tableName,&numCols,UName); | |
419 | // cl_BrowserDB->pColFor = new wxDbColFor[numCols]; | |
420 | for (i=0;i<numCols;i++) | |
421 | { | |
422 | // (cl_BrowserDB->pColFor+i)->Format(1,(cl_BrowserDB+i)->dbDataType,(cl_BrowserDB+i)->sqlDataType, | |
423 | // (cl_BrowserDB+i)->columnSize, (cl_BrowserDB+i)->decimalDigits); | |
424 | (cl_BrowserDB+i)->pColFor = new wxDbColFor; | |
425 | (cl_BrowserDB+i)->pColFor->Format(1, | |
426 | (cl_BrowserDB+i)->dbDataType, | |
427 | (cl_BrowserDB+i)->sqlDataType, | |
428 | (cl_BrowserDB+i)->columnSize, | |
429 | (cl_BrowserDB+i)->decimalDigits); | |
430 | } | |
431 | return cl_BrowserDB; | |
432 | } | |
433 | ||
434 | //---------------------------------------------------------------------------------------- | |
435 | void BrowserDB::PointerToNULL(int Art) | |
436 | { | |
437 |