1 //----------------------------------------------------------------------------------------
2 // Name: BrowserDB.h,cpp
3 // Purpose: a wxDB class
4 // Author: Mark Johnson
6 // Created: 19991127.mj10777
7 // Copyright: (c) Mark Johnson
8 // Licence: wxWindows license
10 //----------------------------------------------------------------------------------------
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14 //----------------------------------------------------------------------------------------
18 //----------------------------------------------------------------------------------------
22 //----------------------------------------------------------------------------------------
24 //----------------------------------------------------------------------------------------
25 // Global structure for holding ODBC connection information
26 wxDbConnectInf DbConnectInf
;
29 #error Demo cannot be compiled unless setup.h has wxUSE_ODBC set to 1
32 //----------------------------------------------------------------------------------------
33 extern WXDLLEXPORT_DATA(wxDbList
*) PtrBegDbList
; /* from db.cpp, used in getting back error results from db connections */
35 //----------------------------------------------------------------------------------------
36 wxChar
*GetExtendedDBErrorMsg(wxChar
*ErrFile
, int ErrLine
)
40 if (ErrFile
|| ErrLine
)
45 tStr
.Printf(_T("%d"),ErrLine
);
46 msg
+= tStr
.GetData();
49 msg
.Append (_T("\nODBC errors:\n"));
51 /* Scan through each database connection displaying
52 * any ODBC errors that have occurred. */
54 for (pDbList
= PtrBegDbList
; pDbList
; pDbList
= pDbList
->PtrNext
)
56 // Skip over any free connections
59 // Display errors for this connection
60 for (int i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
62 if (pDbList
->PtrDb
->errorList
[i
])
64 msg
.Append(pDbList
->PtrDb
->errorList
[i
]);
65 if (wxStrcmp(pDbList
->PtrDb
->errorList
[i
],wxEmptyString
) != 0)
71 return (wxChar
*) (const wxChar
*) msg
;
72 } // GetExtendedDBErrorMsg
74 //----------------------------------------------------------------------------------------
75 BrowserDB::BrowserDB()
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
83 } // BrowserDB Constructor
85 //----------------------------------------------------------------------------------------
86 BrowserDB::~BrowserDB()
88 PointerToNULL(1); // Clean up Tables and Databases (Commit, Close and delete)
89 } // BrowserDB destructor
91 //----------------------------------------------------------------------------------------
92 bool BrowserDB::Initialize(int Quiet
)
94 if (!OnStartDB(Quiet
))
96 wxLogMessage(_("\n\n-E-> BrowserDB::OnStartDB(%s) : Failed ! "),ODBCSource
.c_str());
100 } // BrowserDB:Initialize
102 //----------------------------------------------------------------------------------------
103 bool BrowserDB::OnStartDB(int Quiet
)
107 wxLogMessage(_("\n-I-> BrowserDB::OnStartDB(%s) : Begin "),ODBCSource
.c_str());
108 if (db_BrowserDB
!= NULL
)
111 wxLogMessage(_("\n-I-> BrowserDB::OnStartDB() : DB is already open."));
115 DbConnectInf
.AllocHenv();
117 //---------------------------------------------------------------------------------------
118 // Connect to datasource
119 //---------------------------------------------------------------------------------------
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
;
129 if (p_Dlg
->ShowModal() == wxID_OK
)
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
;
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
)
148 DbConnectInf
.SetDsn(wxEmptyString
);
149 DbConnectInf
.SetUserID(wxEmptyString
);
150 DbConnectInf
.SetPassword(wxEmptyString
);
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());
156 DbConnectInf
.FreeHenv();
159 //--------------------------------------------------------------------------------------
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());
171 DbConnectInf
.FreeHenv();
176 //----------------------------------------------------------------------------------------
177 bool BrowserDB::OnCloseDB(int Quiet
)
180 wxLogMessage(_("-I-> BrowserDB::OnCloseDB() : Begin "));
183 // db_BrowserDB->Close();
184 wxDbFreeConnection(db_BrowserDB
);
186 DbConnectInf
.FreeHenv();
191 wxLogMessage(_("\n-I-> BrowserDB::OnCloseDB() : End "));
195 //----------------------------------------------------------------------------------------
196 bool BrowserDB::OnGetNext(int Cols
,int WXUNUSED(Quiet
))
200 wxChar s_temp
[1024+1];
204 TIMESTAMP_STRUCT t_temp
;
206 //-----------------------------
207 if (!db_BrowserDB
->GetNext())
210 Temp0
.Printf(_("\n-E-> BrowserDB::OnGetNext - ODBC-Error with GetNext \n-E-> "));
211 Temp0
+= GetExtendedDBErrorMsg(__TFILE__
,__LINE__
);
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
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
;
238 if ((i_dbDataType
== DB_DATA_TYPE_INTEGER
) &&
239 ((cl_BrowserDB
+i
)->pColFor
->i_sqlDataType
== SQL_C_DOUBLE
))
241 i_dbDataType
= DB_DATA_TYPE_FLOAT
;
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
))
249 Temp0
.Printf(_("\n-E-> BrowserDB::OnGetNext - ODBC-Error with GetNext of >%s<.\n-E-> "),(cl_BrowserDB
+i
)->tableName
);
250 Temp0
+= GetExtendedDBErrorMsg(__TFILE__
,__LINE__
);
253 Temp0
.Printf((cl_BrowserDB
+i
)->pColFor
->s_Field
,s_temp
);
254 wxStrcpy((cl_BrowserDB
+i
)->tableName
,Temp0
.c_str());
256 case DB_DATA_TYPE_INTEGER
:
258 if (!db_BrowserDB
->GetData((UWORD
)(i
+1),(SWORD
)((cl_BrowserDB
+i
)->pColFor
->i_sqlDataType
),&l_temp
,sizeof(l_temp
), &cb
))
260 Temp0
.Printf(_("\n-E-> BrowserDB::OnGetData - ODBC-Error with GetNext \n-E-> "));
261 Temp0
+= GetExtendedDBErrorMsg(__TFILE__
,__LINE__
);
265 Temp0
.Printf((cl_BrowserDB
+i
)->pColFor
->s_Field
,l_temp
);
266 wxStrcpy((cl_BrowserDB
+i
)->tableName
,Temp0
.c_str());
269 case DB_DATA_TYPE_FLOAT
:
271 if (!db_BrowserDB
->GetData((UWORD
)(i
+1),(SWORD
)((cl_BrowserDB
+i
)->pColFor
->i_sqlDataType
),&f_temp
,sizeof(f_temp
), &cb
))
273 Temp0
.Printf(_("\n-E-> BrowserDB::OnGetData - ODBC-Error with GetNext \n-E-> "));
274 Temp0
+= GetExtendedDBErrorMsg(__TFILE__
,__LINE__
);
279 Temp0
.Printf((cl_BrowserDB
+i
)->pColFor
->s_Field
,f_temp
);
280 wxStrcpy((cl_BrowserDB
+i
)->tableName
,Temp0
.c_str());
283 case DB_DATA_TYPE_DATE
:
291 if (!db_BrowserDB
->GetData((UWORD
)(i
+1),(SWORD
)((cl_BrowserDB
+i
)->pColFor
->i_sqlDataType
),&t_temp
,sizeof(t_temp
), &cb
))
293 Temp0
.Printf(_("\n-E-> BrowserDB::OnGetData - ODBC-Error with GetNext \n-E-> "));
294 Temp0
+= GetExtendedDBErrorMsg(__TFILE__
,__LINE__
);
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
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());
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
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());
313 if ((cl_BrowserDB
+i
)->pColFor
->i_Nation
== 3) // US MM/DD/YYYY
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());
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());
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());
340 //----------------------------------------------------------------------------------------
341 bool BrowserDB::OnSelect(wxString tb_Name
, int Quiet
)
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())))
351 Temp0
.Printf(_("\n-E-> BrowserDB::OnSelect - ODBC-Error with ExecSql of >%s<.\n-E-> "),tb_Name
.c_str());
352 Temp0
+= GetExtendedDBErrorMsg(__TFILE__
,__LINE__
);
354 wxMessageBox(_T("-E-> BrowserDB::OnSelect - GetData()"));
357 //---------------------------------------------------------------------------------------
358 while (db_BrowserDB
->GetNext())
362 //---------------------------------------------------------------------------------------
363 if (!db_BrowserDB
->ExecSql((wxChar
*)(SQLStmt
.GetData())))
365 Temp0
.Printf(_("\n-E-> BrowserDB::OnSelect - ODBC-Error with ExecSql of >%s<.\n-E-> "),tb_Name
.c_str());
366 Temp0
+= GetExtendedDBErrorMsg(__TFILE__
,__LINE__
);
370 //---------------------------------------------------------------------------------------
371 // SetColDefs ( 0, "NAME", DB_DATA_TYPE_VARCHAR, Name, SQL_C_WXCHAR, sizeof(Name), true, true); // Primary index
372 //---------------------------------------------------------------------------------------
375 wxLogMessage(_("\n-I-> BrowserDB::OnSelect(%s) Records(%d): End - Time needed : %ld ms"),tb_Name
.c_str(),i_Records
,sw
.Time());
380 //----------------------------------------------------------------------------------------
381 bool BrowserDB::OnExecSql(wxString SQLStmt
, int Quiet
)
383 //---------------------------------------------------------------------------------------
384 if (!db_BrowserDB
->ExecSql((wxChar
*)(SQLStmt
.GetData())))
386 Temp0
.Printf(_("\n-E-> BrowserDB::OnExecSQL - ODBC-Error with ExecSql of >%s<.\n-E-> "),SQLStmt
.c_str());
387 Temp0
+= GetExtendedDBErrorMsg(__TFILE__
,__LINE__
);
391 wxMessageBox(_T("-E-> BrowserDB::OnExecSql - ExecSql()"));
396 // wxLogMessage(_("\n-I-> BrowserDB::OnExecSql(%s) - End - Time needed : %ld ms"),SQLStmt.c_str(),sw.Time());
401 //----------------------------------------------------------------------------------------
402 wxDbInf
* BrowserDB::OnGetCatalog(int WXUNUSED(Quiet
))
405 wxStrcpy(UName
,UserName
);
406 ct_BrowserDB
= db_BrowserDB
->GetCatalog(UName
);
410 //----------------------------------------------------------------------------------------
411 wxDbColInf
* BrowserDB::OnGetColumns(wxChar
*tableName
, UWORD numCols
, int WXUNUSED(Quiet
))
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
++)
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
);
432 //----------------------------------------------------------------------------------------
433 void BrowserDB::PointerToNULL(int Art
)
435 if (Art
== 1) // Löschen
437 if (cl_BrowserDB
!= NULL
)
438 { // Destroy the memory
439 delete [] cl_BrowserDB
;
441 if (ct_BrowserDB
!= NULL
)
442 { // Destroy the memory
443 delete [] ct_BrowserDB
;
445 if (db_BrowserDB
!= NULL
)
447 db_BrowserDB
->CommitTrans();
448 db_BrowserDB
->Close();
449 wxDbCloseConnections();
459 //----------------------------------------------------------------------------------------
460 void BrowserDB::OnFillSqlTyp()
462 i_SqlTyp
[1] = SQL_C_BINARY
; s_SqlTyp
[1] = _T("SQL_C_BINARY");
463 i_SqlTyp
[2] = SQL_C_BIT
; s_SqlTyp
[2] = _T("SQL_C_BIT");
464 i_SqlTyp
[3] = SQL_C_BOOKMARK
; s_SqlTyp
[3] = _T("SQL_C_BOOKMARK");
465 i_SqlTyp
[4] = SQL_C_WXCHAR
; s_SqlTyp
[4] = _T("SQL_C_WXCHAR");
466 i_SqlTyp
[5] = SQL_C_DATE
; s_SqlTyp
[5] = _T("SQL_C_DATE");
467 i_SqlTyp
[6] = SQL_C_DEFAULT
; s_SqlTyp
[6] = _T("SQL_C_DEFAULT");
468 i_SqlTyp
[7] = SQL_C_DOUBLE
; s_SqlTyp
[7] = _T("SQL_C_DOUBLE");
469 i_SqlTyp
[8] = SQL_C_FLOAT
; s_SqlTyp
[8] = _T("SQL_C_FLOAT");
470 i_SqlTyp
[9] = SQL_C_LONG
; s_SqlTyp
[9] = _T("SQL_C_LONG");
471 i_SqlTyp
[10] = SQL_C_SHORT
; s_SqlTyp
[10] = _T("SQL_C_SHORT");
472 i_SqlTyp
[11] = SQL_C_SLONG
; s_SqlTyp
[11] = _T("SQL_C_SLONG");
473 i_SqlTyp
[12] = SQL_C_SSHORT
; s_SqlTyp
[12] = _T("SQL_C_SSHORT");
474 i_SqlTyp
[13] = SQL_C_STINYINT
; s_SqlTyp
[13] = _T("SQL_C_STINYINT");
475 i_SqlTyp
[14] = SQL_C_TIME
; s_SqlTyp
[14] = _T("SQL_C_TIME");
476 i_SqlTyp
[15] = SQL_C_TIMESTAMP
; s_SqlTyp
[15] = _T("SQL_C_TIMESTAMP");
477 i_SqlTyp
[16] = SQL_C_TINYINT
; s_SqlTyp
[16] = _T("SQL_C_TINYINT");
478 i_SqlTyp
[17] = SQL_C_ULONG
; s_SqlTyp
[17] = _T("SQL_C_ULONG");
479 i_SqlTyp
[18] = SQL_C_USHORT
; s_SqlTyp
[18] = _T("SQL_C_USHORT");
480 i_SqlTyp
[19] = SQL_C_UTINYINT
; s_SqlTyp
[19] = _T("SQL_C_UTINYINT");
481 i_SqlTyp
[20] = SQL_VARCHAR
; s_SqlTyp
[20] = _T("SQL_VARCHAR");
482 i_SqlTyp
[21] = SQL_NUMERIC
; s_SqlTyp
[21] = _T("SQL_NUMERIC");
483 i_SqlTyp
[22] = SQL_LONGVARCHAR
; s_SqlTyp
[22] = _T("SQL_LONGVARCHAR");
484 i_SqlTyp
[23] = SQL_REAL
; s_SqlTyp
[23] = _T("SQL_REAL");
485 i_SqlTyp
[0] = 23; s_SqlTyp
[0] = wxEmptyString
;
488 //----------------------------------------------------------------------------------------
489 void BrowserDB::OnFilldbTyp()
491 i_dbTyp
[1] = DB_DATA_TYPE_VARCHAR
; s_dbTyp
[1] = _T("DB_DATA_TYPE_VARCHAR");
492 i_dbTyp
[2] = DB_DATA_TYPE_INTEGER
; s_dbTyp
[2] = _T("DB_DATA_TYPE_INTEGER");
493 i_dbTyp
[3] = DB_DATA_TYPE_FLOAT
; s_dbTyp
[3] = _T("DB_DATA_TYPE_FLOAT");
494 i_dbTyp
[4] = DB_DATA_TYPE_DATE
; s_dbTyp
[4] = _T("DB_DATA_TYPE_DATE");
495 i_dbTyp
[0] = 4; s_dbTyp
[0] = wxEmptyString
;
497 //----------------------------------------------------------------------------------------