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 // - darf nur einmal im Projekte definiert werden ?? Extra Databasse Klasse ?
35 wxDbConnectInf DbConnectInf
; // Für DBase
37 //----------------------------------------------------------------------------------------
38 extern WXDLLEXPORT_DATA(wxDbList
*) PtrBegDbList
; /* from db.cpp, used in getting back error results from db connections */
40 //----------------------------------------------------------------------------------------
41 char *GetExtendedDBErrorMsg(char *ErrFile
, int ErrLine
)
45 if (ErrFile
|| ErrLine
)
50 tStr
.Printf("%d",ErrLine
);
51 msg
+= tStr
.GetData();
54 msg
.Append ("\nODBC errors:\n");
56 /* Scan through each database connection displaying
57 * any ODBC errors that have occured. */
59 for (pDbList
= PtrBegDbList
; pDbList
; pDbList
= pDbList
->PtrNext
)
61 // Skip over any free connections
64 // Display errors for this connection
65 for (int i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
67 if (pDbList
->PtrDb
->errorList
[i
])
69 msg
.Append(pDbList
->PtrDb
->errorList
[i
]);
70 if (strcmp(pDbList
->PtrDb
->errorList
[i
],"") != 0)
76 return (char*) (const char*) msg
;
77 } // GetExtendedDBErrorMsg
79 //----------------------------------------------------------------------------------------
80 BrowserDB::BrowserDB()
83 ODBCSource
= ""; // ODBC data source name (created with ODBC Administrator under Win95/NT)
84 UserName
= ""; // database username - must already exist in the data source
85 Password
= ""; // password database username
88 } // BrowserDB Constructor
90 //----------------------------------------------------------------------------------------
91 BrowserDB::~BrowserDB()
93 PointerToNULL(1); // Clean up Tables and Databases (Commit, Close and delete)
94 } // BrowserDB destructor
96 //----------------------------------------------------------------------------------------
97 bool BrowserDB::Initialize(int Quiet
)
99 if (!OnStartDB(Quiet
))
101 wxLogMessage(_("\n\n-E-> BrowserDB::OnStartDB(%s) : Failed ! "),ODBCSource
.c_str());
105 } // BrowserDB:Initialize
107 //----------------------------------------------------------------------------------------
108 bool BrowserDB::OnStartDB(int Quiet
)
112 wxLogMessage(_("\n-I-> BrowserDB::OnStartDB(%s) : Begin "),ODBCSource
.c_str());
113 if (db_BrowserDB
!= NULL
)
116 wxLogMessage(_("\n-I-> BrowserDB::OnStartDB() : DB is already open."));
120 DbConnectInf
.AllocHenv();
122 //---------------------------------------------------------------------------------------
123 // Connect to datasource
124 //---------------------------------------------------------------------------------------
126 p_Dlg
= new DlgUser(pDoc
->p_MainFrame
,pDoc
,"");
127 p_Dlg
->s_DSN
= ODBCSource
;
128 p_Dlg
->s_User
= UserName
;
129 p_Dlg
->s_Password
= Password
;
134 if (p_Dlg
->ShowModal() == wxID_OK
)
136 (pDoc
->p_DSN
+i_Which
)->Usr
= p_Dlg
->s_User
;
137 (pDoc
->p_DSN
+i_Which
)->Pas
= p_Dlg
->s_Password
;
138 UserName
= p_Dlg
->s_User
;
139 Password
= p_Dlg
->s_Password
;
145 //--------------------------------------------------------------------------------------
146 DbConnectInf
.SetDsn(ODBCSource
); // ODBC data source name (created with ODBC Administrator under Win95/NT)
147 DbConnectInf
.SetUserID(UserName
); // database username - must already exist in the data source
148 DbConnectInf
.SetPassword(Password
); // password database username
149 db_BrowserDB
= wxDbGetConnection(&DbConnectInf
);
150 // wxLogMessage(">>>%s<<<>>>%s<<<",UserName.c_str(),Password.c_str());
151 if (db_BrowserDB
== NULL
)
153 DbConnectInf
.SetDsn(wxT(""));
154 DbConnectInf
.SetUserID(wxT(""));
155 DbConnectInf
.SetPassword(wxT(""));
158 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)"));
159 wxLogMessage(_("-I-> BrowserDB::OnStartDB(%s) : End - Time needed : %ld ms"),ODBCSource
.c_str(),sw
.Time());
161 DbConnectInf
.FreeHenv();
164 //--------------------------------------------------------------------------------------
167 Temp1
= db_BrowserDB
->GetDatabaseName();
168 Temp2
= db_BrowserDB
->GetDataSource();
169 wxLogMessage(_("-I-> BrowserDB::OnGetDataSourceODBC() - DatabaseName(%s) ; DataSource(%s)"),Temp1
.c_str(),Temp2
.c_str());
170 wxLogMessage(_("-I-> BrowserDB::OnStartDB(%s) : End - Time needed : %ld ms"),ODBCSource
.c_str(),sw
.Time());
176 DbConnectInf
.FreeHenv();
181 //----------------------------------------------------------------------------------------
182 bool BrowserDB::OnCloseDB(int Quiet
)
185 wxLogMessage(_("-I-> BrowserDB::OnCloseDB() : Begin "));
188 // db_BrowserDB->Close();
189 wxDbFreeConnection(db_BrowserDB
);
191 DbConnectInf
.FreeHenv();
196 wxLogMessage(_("\n-I-> BrowserDB::OnCloseDB() : End "));
200 //----------------------------------------------------------------------------------------
201 bool BrowserDB::OnGetNext(int Cols
,int Quiet
)
210 TIMESTAMP_STRUCT t_temp
;
212 //-----------------------------
213 if (!db_BrowserDB
->GetNext())
216 Temp0
.Printf(_("\n-E-> BrowserDB::OnGetNext - ODBC-Error with GetNext \n-E-> "));
217 Temp0
+= GetExtendedDBErrorMsg(__FILE__
,__LINE__
);
225 strcpy((cl_BrowserDB
+i
)->tableName
,"-E->");
226 i_dbDataType
= (cl_BrowserDB
->pColFor
+i
)->i_dbDataType
;
227 if (i_dbDataType
== 0) // Filter unsupported dbDataTypes
229 if (((cl_BrowserDB
->pColFor
+i
)->i_sqlDataType
== SQL_VARCHAR
) || ((cl_BrowserDB
->pColFor
+i
)->i_sqlDataType
== SQL_LONGVARCHAR
))
230 i_dbDataType
= DB_DATA_TYPE_VARCHAR
;
231 if ((cl_BrowserDB
->pColFor
+i
)->i_sqlDataType
== SQL_C_DATE
)
232 i_dbDataType
= DB_DATA_TYPE_DATE
;
233 if ((cl_BrowserDB
->pColFor
+i
)->i_sqlDataType
== SQL_C_BIT
)
234 i_dbDataType
= DB_DATA_TYPE_INTEGER
;
235 if ((cl_BrowserDB
->pColFor
+i
)->i_sqlDataType
== SQL_NUMERIC
)
236 i_dbDataType
= DB_DATA_TYPE_VARCHAR
;
237 if ((cl_BrowserDB
->pColFor
+i
)->i_sqlDataType
== SQL_REAL
)
238 i_dbDataType
= DB_DATA_TYPE_FLOAT
;
240 if ((i_dbDataType
== DB_DATA_TYPE_INTEGER
) && ((cl_BrowserDB
->pColFor
+i
)->i_sqlDataType
== SQL_C_DOUBLE
))
242 i_dbDataType
= DB_DATA_TYPE_FLOAT
;
246 case DB_DATA_TYPE_VARCHAR
:
248 if (!db_BrowserDB
->GetData(i
+1,(cl_BrowserDB
->pColFor
+i
)->i_dbDataType
,&s_temp
,sizeof(s_temp
), &cb
))
250 Temp0
.Printf(_("\n-E-> BrowserDB::OnGetNext - ODBC-Error with GetNext of >%s<.\n-E-> "),(cl_BrowserDB
+i
)->tableName
);
251 Temp0
+= GetExtendedDBErrorMsg(__FILE__
,__LINE__
);
254 Temp0
.Printf((cl_BrowserDB
->pColFor
+i
)->s_Field
,s_temp
);
255 strcpy((cl_BrowserDB
+i
)->tableName
,Temp0
.c_str());
257 case DB_DATA_TYPE_INTEGER
:
259 if (!db_BrowserDB
->GetData(i
+1,(cl_BrowserDB
->pColFor
+i
)->i_sqlDataType
,&l_temp
,sizeof(l_temp
), &cb
))
261 Temp0
.Printf(_("\n-E-> BrowserDB::OnGetData - ODBC-Error with GetNext \n-E-> "));
262 Temp0
+= GetExtendedDBErrorMsg(__FILE__
,__LINE__
);
266 Temp0
.Printf((cl_BrowserDB
->pColFor
+i
)->s_Field
,l_temp
);
267 strcpy((cl_BrowserDB
+i
)->tableName
,Temp0
.c_str());
270 case DB_DATA_TYPE_FLOAT
:
272 if (!db_BrowserDB
->GetData(i
+1,(cl_BrowserDB
->pColFor
+i
)->i_sqlDataType
,&f_temp
,sizeof(f_temp
), &cb
))
274 Temp0
.Printf(_("\n-E-> BrowserDB::OnGetData - ODBC-Error with GetNext \n-E-> "));
275 Temp0
+= GetExtendedDBErrorMsg(__FILE__
,__LINE__
);
280 Temp0
.Printf((cl_BrowserDB
->pColFor
+i
)->s_Field
,f_temp
);
281 strcpy((cl_BrowserDB
+i
)->tableName
,Temp0
.c_str());
284 case DB_DATA_TYPE_DATE
:
285 t_temp
.day
= t_temp
.month
= t_temp
.year
= t_temp
.hour
= t_temp
.minute
= t_temp
.second
= t_temp
.fraction
= 0;
286 if (!db_BrowserDB
->GetData(i
+1,(cl_BrowserDB
->pColFor
+i
)->i_sqlDataType
,&t_temp
,sizeof(t_temp
), &cb
))
288 Temp0
.Printf(_("\n-E-> BrowserDB::OnGetData - ODBC-Error with GetNext \n-E-> "));
289 Temp0
+= GetExtendedDBErrorMsg(__FILE__
,__LINE__
);
293 // i_Nation = 0 = timestamp , 1=EU, 2=UK, 3=International, 4=US
294 if (((cl_BrowserDB
->pColFor
+i
)->i_Nation
== 0) || // TS YYYY-MM-DD
295 ((cl_BrowserDB
->pColFor
+i
)->i_Nation
== 3)) // IT YYYY-MM-DD
297 Temp0
.Printf((cl_BrowserDB
->pColFor
+i
)->s_Field
,t_temp
.year
,t_temp
.month
,t_temp
.day
,
298 t_temp
.hour
, t_temp
.minute
, t_temp
.second
, t_temp
.fraction
);
299 strcpy((cl_BrowserDB
+i
)->tableName
,Temp0
.c_str());
301 if (((cl_BrowserDB
->pColFor
+i
)->i_Nation
== 1) || // EU DD.MM.YYYY
302 ((cl_BrowserDB
->pColFor
+i
)->i_Nation
== 2)) // UK DD/MM/YYYY
304 Temp0
.Printf((cl_BrowserDB
->pColFor
+i
)->s_Field
,t_temp
.day
,t_temp
.month
,t_temp
.year
,
305 t_temp
.hour
, t_temp
.minute
, t_temp
.second
, t_temp
.fraction
);
306 strcpy((cl_BrowserDB
+i
)->tableName
,Temp0
.c_str());
308 if ((cl_BrowserDB
->pColFor
+i
)->i_Nation
== 3) // US MM/DD/YYYY
310 Temp0
.Printf((cl_BrowserDB
->pColFor
+i
)->s_Field
,t_temp
.month
,t_temp
.day
,t_temp
.year
,
311 t_temp
.hour
, t_temp
.minute
, t_temp
.second
, t_temp
.fraction
);
312 strcpy((cl_BrowserDB
+i
)->tableName
,Temp0
.c_str());
320 Temp0
= (cl_BrowserDB
+i
)->colName
;
321 wxLogMessage(_("-E-> BrowserDB::OnGetNext - DB_DATA_TYPE_?? (%d) in Col(%s)"),(cl_BrowserDB
->pColFor
+i
)->i_dbDataType
,Temp0
.c_str());
325 Temp0
.Printf(_("-E-> unknown Format(%d) - sql(%d)"),(cl_BrowserDB
->pColFor
+i
)->i_dbDataType
,(cl_BrowserDB
->pColFor
+i
)->i_sqlDataType
);
326 strcpy((cl_BrowserDB
+i
)->tableName
,Temp0
.c_str());
335 //----------------------------------------------------------------------------------------
336 bool BrowserDB::OnSelect(wxString tb_Name
, int Quiet
)
341 //---------------------------------------------------------------------------------------
342 SQLStmt
.sprintf("SELECT * FROM %s",tb_Name
.c_str());
343 if (!db_BrowserDB
->ExecSql((char *)(SQLStmt
.GetData())))
345 Temp0
.Printf(_("\n-E-> BrowserDB::OnSelect - ODBC-Error with ExecSql of >%s<.\n-E-> "),tb_Name
.c_str());
346 Temp0
+= GetExtendedDBErrorMsg(__FILE__
,__LINE__
);
348 wxMessageBox("-E-> BrowserDB::OnSelect - GetData()");
351 //---------------------------------------------------------------------------------------
352 while (db_BrowserDB
->GetNext())
356 //---------------------------------------------------------------------------------------
357 if (!db_BrowserDB
->ExecSql((char *)(SQLStmt
.GetData())))
359 Temp0
.Printf(_("\n-E-> BrowserDB::OnSelect - ODBC-Error with ExecSql of >%s<.\n-E-> "),tb_Name
.c_str());
360 Temp0
+= GetExtendedDBErrorMsg(__FILE__
,__LINE__
);
364 //---------------------------------------------------------------------------------------
365 // SetColDefs ( 0,"NAME", DB_DATA_TYPE_VARCHAR, Name, SQL_C_CHAR, sizeof(Name), TRUE, TRUE); // Primary index
366 //---------------------------------------------------------------------------------------
369 wxLogMessage(_("\n-I-> BrowserDB::OnSelect(%s) Records(%d): End - Time needed : %ld ms"),tb_Name
.c_str(),i_Records
,sw
.Time());
374 //----------------------------------------------------------------------------------------
375 bool BrowserDB::OnExecSql(wxString SQLStmt
, int Quiet
)
377 //---------------------------------------------------------------------------------------
378 if (!db_BrowserDB
->ExecSql((char *)(SQLStmt
.GetData())))
380 Temp0
.Printf(_("\n-E-> BrowserDB::OnExecSQL - ODBC-Error with ExecSql of >%s<.\n-E-> "),SQLStmt
.c_str());
381 Temp0
+= GetExtendedDBErrorMsg(__FILE__
,__LINE__
);
385 wxMessageBox("-E-> BrowserDB::OnExecSql - ExecSql()");
390 // wxLogMessage(_("\n-I-> BrowserDB::OnExecSql(%s) - End - Time needed : %ld ms"),SQLStmt.c_str(),sw.Time());
395 //----------------------------------------------------------------------------------------
396 wxDbInf
* BrowserDB::OnGetCatalog(int Quiet
)
399 strcpy(UName
,UserName
);
400 ct_BrowserDB
= db_BrowserDB
->GetCatalog(UName
);
404 //----------------------------------------------------------------------------------------
405 wxDbColInf
* BrowserDB::OnGetColumns(char *tableName
, int numCols
, int Quiet
)
409 strcpy(UName
,UserName
);
410 cl_BrowserDB
= db_BrowserDB
->GetColumns(tableName
,&numCols
,UName
);
411 cl_BrowserDB
->pColFor
= new wxDbColFor
[numCols
];
412 for (i
=0;i
<numCols
;i
++)
414 (cl_BrowserDB
->pColFor
+i
)->Format(1,(cl_BrowserDB
+i
)->dbDataType
,(cl_BrowserDB
+i
)->sqlDataType
,
415 (cl_BrowserDB
+i
)->columnSize
, (cl_BrowserDB
+i
)->decimalDigits
);
420 //----------------------------------------------------------------------------------------
421 void BrowserDB::PointerToNULL(int Art
)
423 if (Art
== 1) // Löschen
425 if (cl_BrowserDB
!= NULL
)
426 { // Destroy the memory
427 delete [] cl_BrowserDB
;
429 if (ct_BrowserDB
!= NULL
)
430 { // Destroy the memory
431 delete [] ct_BrowserDB
;
433 if (db_BrowserDB
!= NULL
)
435 db_BrowserDB
->CommitTrans();
436 db_BrowserDB
->Close();
437 wxDbCloseConnections();
447 //----------------------------------------------------------------------------------------
448 void BrowserDB::OnFillSqlTyp()
450 i_SqlTyp
[1] = SQL_C_BINARY
; s_SqlTyp
[1] = "SQL_C_BINARY";
451 i_SqlTyp
[2] = SQL_C_BIT
; s_SqlTyp
[2] = "SQL_C_BIT";
452 i_SqlTyp
[3] = SQL_C_BOOKMARK
; s_SqlTyp
[3] = "SQL_C_BOOKMARK";
453 i_SqlTyp
[4] = SQL_C_CHAR
; s_SqlTyp
[4] = "SQL_C_CHAR";
454 i_SqlTyp
[5] = SQL_C_DATE
; s_SqlTyp
[5] = "SQL_C_DATE";
455 i_SqlTyp
[6] = SQL_C_DEFAULT
; s_SqlTyp
[6] = "SQL_C_DEFAULT";
456 i_SqlTyp
[7] = SQL_C_DOUBLE
; s_SqlTyp
[7] = "SQL_C_DOUBLE";
457 i_SqlTyp
[8] = SQL_C_FLOAT
; s_SqlTyp
[8] = "SQL_C_FLOAT";
458 i_SqlTyp
[9] = SQL_C_LONG
; s_SqlTyp
[9] = "SQL_C_LONG";
459 i_SqlTyp
[10] = SQL_C_SHORT
; s_SqlTyp
[10] = "SQL_C_SHORT";
460 i_SqlTyp
[11] = SQL_C_SLONG
; s_SqlTyp
[11] = "SQL_C_SLONG";
461 i_SqlTyp
[12] = SQL_C_SSHORT
; s_SqlTyp
[12] = "SQL_C_SSHORT";
462 i_SqlTyp
[13] = SQL_C_STINYINT
; s_SqlTyp
[13] = "SQL_C_STINYINT";
463 i_SqlTyp
[14] = SQL_C_TIME
; s_SqlTyp
[14] = "SQL_C_TIME";
464 i_SqlTyp
[15] = SQL_C_TIMESTAMP
; s_SqlTyp
[15] = "SQL_C_TIMESTAMP";
465 i_SqlTyp
[16] = SQL_C_TINYINT
; s_SqlTyp
[16] = "SQL_C_TINYINT";
466 i_SqlTyp
[17] = SQL_C_ULONG
; s_SqlTyp
[17] = "SQL_C_ULONG";
467 i_SqlTyp
[18] = SQL_C_USHORT
; s_SqlTyp
[18] = "SQL_C_USHORT";
468 i_SqlTyp
[19] = SQL_C_UTINYINT
; s_SqlTyp
[19] = "SQL_C_UTINYINT";
469 i_SqlTyp
[20] = SQL_VARCHAR
; s_SqlTyp
[20] = "SQL_VARCHAR";
470 i_SqlTyp
[21] = SQL_NUMERIC
; s_SqlTyp
[21] = "SQL_NUMERIC";
471 i_SqlTyp
[22] = SQL_LONGVARCHAR
; s_SqlTyp
[22] = "SQL_LONGVARCHAR";
472 i_SqlTyp
[23] = SQL_REAL
; s_SqlTyp
[23] = "SQL_REAL";
473 i_SqlTyp
[0] = 23; s_SqlTyp
[0] = "";
476 //----------------------------------------------------------------------------------------
477 void BrowserDB::OnFilldbTyp()
479 i_dbTyp
[1] = DB_DATA_TYPE_VARCHAR
; s_dbTyp
[1] = "DB_DATA_TYPE_VARCHAR";
480 i_dbTyp
[2] = DB_DATA_TYPE_INTEGER
; s_dbTyp
[2] = "DB_DATA_TYPE_INTEGER";
481 i_dbTyp
[3] = DB_DATA_TYPE_FLOAT
; s_dbTyp
[3] = "DB_DATA_TYPE_FLOAT";
482 i_dbTyp
[4] = DB_DATA_TYPE_DATE
; s_dbTyp
[4] = "DB_DATA_TYPE_DATE";
483 i_dbTyp
[0] = 4; s_dbTyp
[0] = "";
485 //----------------------------------------------------------------------------------------