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 //----------------------------------------------------------------------------------------
13 //-- all #ifdefs that the whole Project needs. -------------------------------------------
14 //----------------------------------------------------------------------------------------
16 #pragma implementation
19 //----------------------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22 //----------------------------------------------------------------------------------------
26 //----------------------------------------------------------------------------------------
30 //----------------------------------------------------------------------------------------
32 //----------------------------------------------------------------------------------------
33 // Global structure for holding ODBC connection information
34 wxDbConnectInf DbConnectInf
;
37 #error Demo cannot be compiled unless setup.h has wxUSE_ODBC set to 1
40 //----------------------------------------------------------------------------------------
41 extern WXDLLEXPORT_DATA(wxDbList
*) PtrBegDbList
; /* from db.cpp, used in getting back error results from db connections */
43 //----------------------------------------------------------------------------------------
44 wxChar
*GetExtendedDBErrorMsg(wxChar
*ErrFile
, int ErrLine
)
48 if (ErrFile
|| ErrLine
)
53 tStr
.Printf(_T("%d"),ErrLine
);
54 msg
+= tStr
.GetData();
57 msg
.Append (_T("\nODBC errors:\n"));
59 /* Scan through each database connection displaying
60 * any ODBC errors that have occured. */
62 for (pDbList
= PtrBegDbList
; pDbList
; pDbList
= pDbList
->PtrNext
)
64 // Skip over any free connections
67 // Display errors for this connection
68 for (int i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
70 if (pDbList
->PtrDb
->errorList
[i
])
72 msg
.Append(pDbList
->PtrDb
->errorList
[i
]);
73 if (wxStrcmp(pDbList
->PtrDb
->errorList
[i
],_T("")) != 0)
79 return (wxChar
*) (const wxChar
*) msg
;
80 } // GetExtendedDBErrorMsg
82 //----------------------------------------------------------------------------------------
83 BrowserDB::BrowserDB()
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
91 } // BrowserDB Constructor
93 //----------------------------------------------------------------------------------------
94 BrowserDB::~BrowserDB()
96 PointerToNULL(1); // Clean up Tables and Databases (Commit, Close and delete)
97 } // BrowserDB destructor
99 //----------------------------------------------------------------------------------------
100 bool BrowserDB::Initialize(int Quiet
)
102 if (!OnStartDB(Quiet
))
104 wxLogMessage(_("\n\n-E-> BrowserDB::OnStartDB(%s) : Failed ! "),ODBCSource
.c_str());
108 } // BrowserDB:Initialize
110 //----------------------------------------------------------------------------------------
111 bool BrowserDB::OnStartDB(int Quiet
)
115 wxLogMessage(_("\n-I-> BrowserDB::OnStartDB(%s) : Begin "),ODBCSource
.c_str());
116 if (db_BrowserDB
!= NULL
)
119 wxLogMessage(_("\n-I-> BrowserDB::OnStartDB() : DB is already open."));
123 DbConnectInf
.AllocHenv();
125 //---------------------------------------------------------------------------------------
126 // Connect to datasource
127 //---------------------------------------------------------------------------------------
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
;
137 if (p_Dlg
->ShowModal() == wxID_OK
)
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
;
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
)
156 DbConnectInf
.SetDsn(wxT(""));
157 DbConnectInf
.SetUserID(wxT(""));
158 DbConnectInf
.SetPassword(wxT(""));
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());
164 DbConnectInf
.FreeHenv();
167 //--------------------------------------------------------------------------------------
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());
179 DbConnectInf
.FreeHenv();
184 //----------------------------------------------------------------------------------------
185 bool BrowserDB::OnCloseDB(int Quiet
)
188 wxLogMessage(_("-I-> BrowserDB::OnCloseDB() : Begin "));
191 // db_BrowserDB->Close();
192 wxDbFreeConnection(db_BrowserDB
);
194 DbConnectInf
.FreeHenv();
199 wxLogMessage(_("\n-I-> BrowserDB::OnCloseDB() : End "));
203 //----------------------------------------------------------------------------------------
204 bool BrowserDB::OnGetNext(int Cols
,int WXUNUSED(Quiet
))
208 wxChar s_temp
[1024+1];
212 TIMESTAMP_STRUCT t_temp
;
214 //-----------------------------
215 if (!db_BrowserDB
->GetNext())
218 Temp0
.Printf(_("\n-E-> BrowserDB::OnGetNext - ODBC-Error with GetNext \n-E-> "));
219 Temp0
+= GetExtendedDBErrorMsg(__TFILE__
,__LINE__
);
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
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
;
246 if ((i_dbDataType
== DB_DATA_TYPE_INTEGER
) &&
247 ((cl_BrowserDB
+i
)->pColFor
->i_sqlDataType
== SQL_C_DOUBLE
))
249 i_dbDataType
= DB_DATA_TYPE_FLOAT
;
253 case DB_DATA_TYPE_VARCHAR
:
254 wxStrcpy(s_temp
,_T(""));
255 if (!db_BrowserDB
->GetData((UWORD
)(i
+1),(SWORD
)((cl_BrowserDB
+i
)->pColFor
->i_dbDataType
),&s_temp
[0],sizeof(s_temp
), &cb
))
257 Temp0
.Printf(_("\n-E-> BrowserDB::OnGetNext - ODBC-Error with GetNext of >%s<.\n-E-> "),(cl_BrowserDB
+i
)->tableName
);
258 Temp0
+= GetExtendedDBErrorMsg(__TFILE__
,__LINE__
);
261 Temp0
.Printf((cl_BrowserDB
+i
)->pColFor
->s_Field
,s_temp
);
262 wxStrcpy((cl_BrowserDB
+i
)->tableName
,Temp0
.c_str());
264 case DB_DATA_TYPE_INTEGER
:
266 if (!db_BrowserDB
->GetData((UWORD
)(i
+1),(SWORD
)((cl_BrowserDB
+i
)->pColFor
->i_sqlDataType
),&l_temp
,sizeof(l_temp
), &cb
))
268 Temp0
.Printf(_("\n-E-> BrowserDB::OnGetData - ODBC-Error with GetNext \n-E-> "));
269 Temp0
+= GetExtendedDBErrorMsg(__TFILE__
,__LINE__
);
273 Temp0
.Printf((cl_BrowserDB
+i
)->pColFor
->s_Field
,l_temp
);
274 wxStrcpy((cl_BrowserDB
+i
)->tableName
,Temp0
.c_str());
277 case DB_DATA_TYPE_FLOAT
:
279 if (!db_BrowserDB
->GetData((UWORD
)(i
+1),(SWORD
)((cl_BrowserDB
+i
)->pColFor
->i_sqlDataType
),&f_temp
,sizeof(f_temp
), &cb
))
281 Temp0
.Printf(_("\n-E-> BrowserDB::OnGetData - ODBC-Error with GetNext \n-E-> "));
282 Temp0
+= GetExtendedDBErrorMsg(__TFILE__
,__LINE__
);
287 Temp0
.Printf((cl_BrowserDB
+i
)->pColFor
->s_Field
,f_temp
);
288 wxStrcpy((cl_BrowserDB
+i
)->tableName
,Temp0
.c_str());
291 case DB_DATA_TYPE_DATE
:
299 if (!db_BrowserDB
->GetData((UWORD
)(i
+1),(SWORD
)((cl_BrowserDB
+i
)->pColFor
->i_sqlDataType
),&t_temp
,sizeof(t_temp
), &cb
))
301 Temp0
.Printf(_("\n-E-> BrowserDB::OnGetData - ODBC-Error with GetNext \n-E-> "));
302 Temp0
+= GetExtendedDBErrorMsg(__TFILE__
,__LINE__
);
306 // i_Nation = 0 = timestamp , 1=EU, 2=UK, 3=International, 4=US
307 if (((cl_BrowserDB
+i
)->pColFor
->i_Nation
== 0) || // TS YYYY-MM-DD
308 ((cl_BrowserDB
+i
)->pColFor
->i_Nation
== 3)) // IT YYYY-MM-DD
310 Temp0
.Printf((cl_BrowserDB
+i
)->pColFor
->s_Field
,t_temp
.year
,t_temp
.month
,t_temp
.day
,
311 t_temp
.hour
, t_temp
.minute
, t_temp
.second
, t_temp
.fraction
);
312 wxStrcpy((cl_BrowserDB
+i
)->tableName
,Temp0
.c_str());
314 if (((cl_BrowserDB
+i
)->pColFor
->i_Nation
== 1) || // EU DD.MM.YYYY
315 ((cl_BrowserDB
+i
)->pColFor
->i_Nation
== 2)) // UK DD/MM/YYYY
317 Temp0
.Printf((cl_BrowserDB
+i
)->pColFor
->s_Field
,t_temp
.day
,t_temp
.month
,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());
321 if ((cl_BrowserDB
+i
)->pColFor
->i_Nation
== 3) // US MM/DD/YYYY
323 Temp0
.Printf((cl_BrowserDB
+i
)->pColFor
->s_Field
,t_temp
.month
,t_temp
.day
,t_temp
.year
,
324 t_temp
.hour
, t_temp
.minute
, t_temp
.second
, t_temp
.fraction
);
325 wxStrcpy((cl_BrowserDB
+i
)->tableName
,Temp0
.c_str());
333 Temp0
= (cl_BrowserDB
+i
)->colName
;
334 wxLogMessage(_("-E-> BrowserDB::OnGetNext - DB_DATA_TYPE_?? (%d) in Col(%s)"),(cl_BrowserDB
+i
)->pColFor
->i_dbDataType
,Temp0
.c_str());
338 Temp0
.Printf(_("-E-> unknown Format(%d) - sql(%d)"),(cl_BrowserDB
+i
)->pColFor
->i_dbDataType
,(cl_BrowserDB
+i
)->pColFor
->i_sqlDataType
);
339 wxStrcpy((cl_BrowserDB
+i
)->tableName
,Temp0
.c_str());
348 //----------------------------------------------------------------------------------------
349 bool BrowserDB::OnSelect(wxString tb_Name
, int Quiet
)
354 //---------------------------------------------------------------------------------------
355 wxString tablename
= db_BrowserDB
->SQLTableName(tb_Name
.c_str());
356 SQLStmt
.sprintf(_T("SELECT * FROM %s"),tablename
.c_str());
357 if (!db_BrowserDB
->ExecSql((wxChar
*)(SQLStmt
.GetData())))
359 Temp0
.Printf(_("\n-E-> BrowserDB::OnSelect - ODBC-Error with ExecSql of >%s<.\n-E-> "),tb_Name
.c_str());
360 Temp0
+= GetExtendedDBErrorMsg(__TFILE__
,__LINE__
);
362 wxMessageBox(_T("-E-> BrowserDB::OnSelect - GetData()"));
365 //---------------------------------------------------------------------------------------
366 while (db_BrowserDB
->GetNext())
370 //---------------------------------------------------------------------------------------
371 if (!db_BrowserDB
->ExecSql((wxChar
*)(SQLStmt
.GetData())))
373 Temp0
.Printf(_("\n-E-> BrowserDB::OnSelect - ODBC-Error with ExecSql of >%s<.\n-E-> "),tb_Name
.c_str());
374 Temp0
+= GetExtendedDBErrorMsg(__TFILE__
,__LINE__
);
378 //---------------------------------------------------------------------------------------
379 // SetColDefs ( 0, "NAME", DB_DATA_TYPE_VARCHAR, Name, SQL_C_WXCHAR, sizeof(Name), true, true); // Primary index
380 //---------------------------------------------------------------------------------------
383 wxLogMessage(_("\n-I-> BrowserDB::OnSelect(%s) Records(%d): End - Time needed : %ld ms"),tb_Name
.c_str(),i_Records
,sw
.Time());
388 //----------------------------------------------------------------------------------------
389 bool BrowserDB::OnExecSql(wxString SQLStmt
, int Quiet
)
391 //---------------------------------------------------------------------------------------
392 if (!db_BrowserDB
->ExecSql((wxChar
*)(SQLStmt
.GetData())))
394 Temp0
.Printf(_("\n-E-> BrowserDB::OnExecSQL - ODBC-Error with ExecSql of >%s<.\n-E-> "),SQLStmt
.c_str());
395 Temp0
+= GetExtendedDBErrorMsg(__TFILE__
,__LINE__
);
399 wxMessageBox(_T("-E-> BrowserDB::OnExecSql - ExecSql()"));
404 // wxLogMessage(_("\n-I-> BrowserDB::OnExecSql(%s) - End - Time needed : %ld ms"),SQLStmt.c_str(),sw.Time());
409 //----------------------------------------------------------------------------------------
410 wxDbInf
* BrowserDB::OnGetCatalog(int WXUNUSED(Quiet
))
413 wxStrcpy(UName
,UserName
);
414 ct_BrowserDB
= db_BrowserDB
->GetCatalog(UName
);
418 //----------------------------------------------------------------------------------------
419 wxDbColInf
* BrowserDB::OnGetColumns(wxChar
*tableName
, UWORD numCols
, int WXUNUSED(Quiet
))
423 wxStrcpy(UName
,UserName
);
424 cl_BrowserDB
= db_BrowserDB
->GetColumns(tableName
,&numCols
,UName
);
425 // cl_BrowserDB->pColFor = new wxDbColFor[numCols];
426 for (i
=0;i
<numCols
;i
++)
428 // (cl_BrowserDB->pColFor+i)->Format(1,(cl_BrowserDB+i)->dbDataType,(cl_BrowserDB+i)->sqlDataType,
429 // (cl_BrowserDB+i)->columnSize, (cl_BrowserDB+i)->decimalDigits);
430 (cl_BrowserDB
+i
)->pColFor
= new wxDbColFor
;
431 (cl_BrowserDB
+i
)->pColFor
->Format(1,
432 (cl_BrowserDB
+i
)->dbDataType
,
433 (cl_BrowserDB
+i
)->sqlDataType
,
434 (cl_BrowserDB
+i
)->columnLength
,
435 (cl_BrowserDB
+i
)->decimalDigits
);
440 //----------------------------------------------------------------------------------------
441 void BrowserDB::PointerToNULL(int Art
)
443 if (Art
== 1) // Löschen
445 if (cl_BrowserDB
!= NULL
)
446 { // Destroy the memory
447 delete [] cl_BrowserDB
;
449 if (ct_BrowserDB
!= NULL
)
450 { // Destroy the memory
451 delete [] ct_BrowserDB
;
453 if (db_BrowserDB
!= NULL
)
455 db_BrowserDB
->CommitTrans();
456 db_BrowserDB
->Close();
457 wxDbCloseConnections();
467 //----------------------------------------------------------------------------------------
468 void BrowserDB::OnFillSqlTyp()
470 i_SqlTyp
[1] = SQL_C_BINARY
; s_SqlTyp
[1] = _T("SQL_C_BINARY");
471 i_SqlTyp
[2] = SQL_C_BIT
; s_SqlTyp
[2] = _T("SQL_C_BIT");
472 i_SqlTyp
[3] = SQL_C_BOOKMARK
; s_SqlTyp
[3] = _T("SQL_C_BOOKMARK");
473 i_SqlTyp
[4] = SQL_C_WXCHAR
; s_SqlTyp
[4] = _T("SQL_C_WXCHAR");
474 i_SqlTyp
[5] = SQL_C_DATE
; s_SqlTyp
[5] = _T("SQL_C_DATE");
475 i_SqlTyp
[6] = SQL_C_DEFAULT
; s_SqlTyp
[6] = _T("SQL_C_DEFAULT");
476 i_SqlTyp
[7] = SQL_C_DOUBLE
; s_SqlTyp
[7] = _T("SQL_C_DOUBLE");
477 i_SqlTyp
[8] = SQL_C_FLOAT
; s_SqlTyp
[8] = _T("SQL_C_FLOAT");
478 i_SqlTyp
[9] = SQL_C_LONG
; s_SqlTyp
[9] = _T("SQL_C_LONG");
479 i_SqlTyp
[10] = SQL_C_SHORT
; s_SqlTyp
[10] = _T("SQL_C_SHORT");
480 i_SqlTyp
[11] = SQL_C_SLONG
; s_SqlTyp
[11] = _T("SQL_C_SLONG");
481 i_SqlTyp
[12] = SQL_C_SSHORT
; s_SqlTyp
[12] = _T("SQL_C_SSHORT");
482 i_SqlTyp
[13] = SQL_C_STINYINT
; s_SqlTyp
[13] = _T("SQL_C_STINYINT");
483 i_SqlTyp
[14] = SQL_C_TIME
; s_SqlTyp
[14] = _T("SQL_C_TIME");
484 i_SqlTyp
[15] = SQL_C_TIMESTAMP
; s_SqlTyp
[15] = _T("SQL_C_TIMESTAMP");
485 i_SqlTyp
[16] = SQL_C_TINYINT
; s_SqlTyp
[16] = _T("SQL_C_TINYINT");
486 i_SqlTyp
[17] = SQL_C_ULONG
; s_SqlTyp
[17] = _T("SQL_C_ULONG");
487 i_SqlTyp
[18] = SQL_C_USHORT
; s_SqlTyp
[18] = _T("SQL_C_USHORT");
488 i_SqlTyp
[19] = SQL_C_UTINYINT
; s_SqlTyp
[19] = _T("SQL_C_UTINYINT");
489 i_SqlTyp
[20] = SQL_VARCHAR
; s_SqlTyp
[20] = _T("SQL_VARCHAR");
490 i_SqlTyp
[21] = SQL_NUMERIC
; s_SqlTyp
[21] = _T("SQL_NUMERIC");
491 i_SqlTyp
[22] = SQL_LONGVARCHAR
; s_SqlTyp
[22] = _T("SQL_LONGVARCHAR");
492 i_SqlTyp
[23] = SQL_REAL
; s_SqlTyp
[23] = _T("SQL_REAL");
493 i_SqlTyp
[0] = 23; s_SqlTyp
[0] = _T("");
496 //----------------------------------------------------------------------------------------
497 void BrowserDB::OnFilldbTyp()
499 i_dbTyp
[1] = DB_DATA_TYPE_VARCHAR
; s_dbTyp
[1] = _T("DB_DATA_TYPE_VARCHAR");
500 i_dbTyp
[2] = DB_DATA_TYPE_INTEGER
; s_dbTyp
[2] = _T("DB_DATA_TYPE_INTEGER");
501 i_dbTyp
[3] = DB_DATA_TYPE_FLOAT
; s_dbTyp
[3] = _T("DB_DATA_TYPE_FLOAT");
502 i_dbTyp
[4] = DB_DATA_TYPE_DATE
; s_dbTyp
[4] = _T("DB_DATA_TYPE_DATE");
503 i_dbTyp
[0] = 4; s_dbTyp
[0] = _T("");
505 //----------------------------------------------------------------------------------------