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