Added ODBC database classes and sample from RemStar (sample needs work for wxWin 2)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@385 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
2041
samples/db/dbtest.cpp
Normal file
2041
samples/db/dbtest.cpp
Normal file
File diff suppressed because it is too large
Load Diff
8
samples/db/dbtest.def
Normal file
8
samples/db/dbtest.def
Normal file
@@ -0,0 +1,8 @@
|
||||
NAME DBTEST
|
||||
DESCRIPTION 'Database wxWindows application'
|
||||
EXETYPE WINDOWS
|
||||
STUB 'WINSTUB.EXE'
|
||||
CODE PRELOAD MOVEABLE DISCARDABLE
|
||||
DATA PRELOAD MOVEABLE MULTIPLE
|
||||
HEAPSIZE 1024
|
||||
STACKSIZE 8192
|
286
samples/db/dbtest.h
Normal file
286
samples/db/dbtest.h
Normal file
@@ -0,0 +1,286 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Name: dbtest.h
|
||||
// Purpose: wxWindows database demo app
|
||||
// Author: George Tasker
|
||||
// Modified by:
|
||||
// Created: 1998
|
||||
// RCS-ID: $Id$
|
||||
// Copyright: (c) 1998 Remstar International, Inc.
|
||||
// Licence: wxWindows licence
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma interface "dbtest.h"
|
||||
|
||||
#include <wx/string.h>
|
||||
#include <wx/dbtable.h>
|
||||
|
||||
enum DialogModes {mView,mCreate,mEdit,mSearch};
|
||||
|
||||
// ID for the menu quit command
|
||||
#define FILE_CREATE 100
|
||||
#define FILE_EXIT 199
|
||||
#define EDIT_PARAMETERS 200
|
||||
#define ABOUT_DEMO 300
|
||||
|
||||
|
||||
// Name of the table to be created/opened
|
||||
const char CONTACT_TABLE_NAME[] = "CONTACTS";
|
||||
|
||||
// Nuber of columns in the above table
|
||||
const int CONTACT_NO_COLS = 12; // 0-11
|
||||
|
||||
// Global structure for holding ODBC connection information
|
||||
struct DbStuff DbConnectInf;
|
||||
|
||||
enum Language {langENGLISH, langFRENCH, langGERMAN, langSPANISH, langOTHER};
|
||||
|
||||
// Forward class declarations
|
||||
class CeditorDlg;
|
||||
class CparameterDlg;
|
||||
|
||||
const char paramFilename[] = "database.cfg";
|
||||
|
||||
|
||||
/*
|
||||
* This class contains the actual data members that are used for transferring
|
||||
* data back and forth from the database to the program.
|
||||
*
|
||||
* NOTE: The object described in this class is just for example purposes, and has no
|
||||
* real meaning other than to show each type of field being used by the database
|
||||
*/
|
||||
class CstructContact : public wxObject
|
||||
{
|
||||
public:
|
||||
char Name[ 50+1 ]; // Contact's name
|
||||
char Addr1[ 50+1 ];
|
||||
char Addr2[ 50+1 ];
|
||||
char City[ 25+1 ];
|
||||
char State[ 25+1 ];
|
||||
char PostalCode[ 15+1 ];
|
||||
char Country[ 20+1 ];
|
||||
TIMESTAMP_STRUCT JoinDate; // Date on which this person joined the wxWindows project
|
||||
Language NativeLanguage; // Enumerated type indicating person's native language
|
||||
bool IsDeveloper; // Is this person a developer for wxWindows, or just a subscriber
|
||||
int Contributions; // Something to show off an integer field
|
||||
ULONG LinesOfCode; // Something to show off a 'long' field
|
||||
}; // CstructContact
|
||||
|
||||
|
||||
//
|
||||
// NOTE: Ccontact inherits wxTable, which gives access to all the database functionality
|
||||
//
|
||||
class Ccontact : public wxTable, public CstructContact
|
||||
{
|
||||
private:
|
||||
bool freeDbConn;
|
||||
void SetupColumns();
|
||||
|
||||
public:
|
||||
wxString whereStr;
|
||||
wxString qryWhereStr; // Where string returned from the query dialog
|
||||
|
||||
Ccontact(wxDB *pwxDB=NULL);
|
||||
~Ccontact();
|
||||
|
||||
void Initialize();
|
||||
bool CreateIndexes(void);
|
||||
bool FetchByName(char *name);
|
||||
|
||||
}; // Ccontact class definition
|
||||
|
||||
|
||||
typedef struct Cparameters
|
||||
{
|
||||
// The length of these strings were arbitrarily picked, and are
|
||||
// dependent on the OS and database engine you will be using.
|
||||
char ODBCSource[100+1];
|
||||
char UserName[25+1];
|
||||
char Password[25+1];
|
||||
} Cparameters;
|
||||
|
||||
|
||||
// Define a new application type
|
||||
class DatabaseDemoApp: public wxApp
|
||||
{
|
||||
public:
|
||||
Cparameters params;
|
||||
wxFrame *OnInit(void);
|
||||
}; // DatabaseDemoApp
|
||||
|
||||
DECLARE_APP(DatabaseDemoApp)
|
||||
|
||||
// Define a new frame type
|
||||
class DatabaseDemoFrame: public wxFrame
|
||||
{
|
||||
private:
|
||||
CeditorDlg *pEditorDlg;
|
||||
CparameterDlg *pParamDlg;
|
||||
|
||||
public:
|
||||
DatabaseDemoFrame(wxFrame *frame, char *title, int x, int y, int w, int h);
|
||||
|
||||
void OnMenuCommand(int id);
|
||||
bool OnClose(void);
|
||||
|
||||
void CreateDataTable();
|
||||
void BuildEditorDialog();
|
||||
void BuildParameterDialog(wxWindow *parent);
|
||||
}; // DatabaseDemoFrame
|
||||
|
||||
|
||||
|
||||
// *************************** CeditorDlg ***************************
|
||||
|
||||
class CeditorDlg : public wxPanel
|
||||
{
|
||||
private:
|
||||
bool widgetPtrsSet;
|
||||
wxString saveName;
|
||||
|
||||
// Pointers to all widgets on the dialog
|
||||
wxButton *pCreateBtn, *pEditBtn, *pDeleteBtn, *pCopyBtn, *pSaveBtn, *pCancelBtn;
|
||||
wxButton *pPrevBtn, *pNextBtn, *pQueryBtn, *pResetBtn, *pDoneBtn, *pHelpBtn;
|
||||
wxButton *pNameListBtn;
|
||||
wxText *pNameTxt, *pAddress1Txt, *pAddress2Txt,*pCityTxt, *pStateTxt, *pCountryTxt,*pPostalCodeTxt;
|
||||
wxMessage *pNameMsg, *pAddress1Msg, *pAddress2Msg,*pCityMsg, *pStateMsg, *pCountryMsg,*pPostalCodeMsg;
|
||||
wxText *pJoinDateTxt,*pContribTxt, *pLinesTxt;
|
||||
wxMessage *pJoinDateMsg,*pContribMsg, *pLinesMsg;
|
||||
wxRadioBox *pDeveloperRadio;
|
||||
wxChoice *pNativeLangChoice;
|
||||
wxMessage *pNativeLangMsg;
|
||||
|
||||
public:
|
||||
enum DialogModes mode;
|
||||
Ccontact *Contact; // this is the table object that will be being manipulated
|
||||
|
||||
CeditorDlg(wxWindow *parent);
|
||||
bool OnClose(void);
|
||||
void OnCommand(wxWindow& win, wxCommandEvent& event);
|
||||
void OnActivate(bool) {}; // necessary for hot keys
|
||||
|
||||
void FieldsEditable();
|
||||
void SetMode(enum DialogModes m);
|
||||
bool PutData();
|
||||
bool GetData();
|
||||
bool Save();
|
||||
bool GetNextRec();
|
||||
bool GetPrevRec();
|
||||
bool GetRec(char *whereStr);
|
||||
}; // CeditorDlg
|
||||
|
||||
|
||||
// *************************** CparameterDlg ***************************
|
||||
|
||||
class CparameterDlg : public wxDialogBox
|
||||
{
|
||||
private:
|
||||
bool widgetPtrsSet;
|
||||
enum DialogModes mode;
|
||||
bool saved;
|
||||
Cparameters savedParamSettings;
|
||||
|
||||
// Pointers to all widgets on the dialog
|
||||
wxMessage *pParamODBCSourceMsg;
|
||||
wxListBox *pParamODBCSourceList;
|
||||
wxMessage *pParamUserNameMsg, *pParamPasswordMsg;
|
||||
wxText *pParamUserNameTxt, *pParamPasswordTxt;
|
||||
wxButton *pParamSaveBtn, *pParamCancelBtn;
|
||||
|
||||
public:
|
||||
CparameterDlg(wxWindow *parent);
|
||||
bool OnClose(void);
|
||||
void OnCommand(wxWindow& win, wxCommandEvent& event);
|
||||
void OnActivate(bool) {}; // necessary for hot keys
|
||||
|
||||
bool PutData();
|
||||
bool GetData();
|
||||
bool Save();
|
||||
void FillDataSourceList();
|
||||
|
||||
}; // CparameterDlg
|
||||
|
||||
|
||||
// *************************** CqueryDlg ***************************
|
||||
|
||||
|
||||
// QUERY DIALOG
|
||||
enum qryOp
|
||||
{
|
||||
qryOpEQ,
|
||||
qryOpLT,
|
||||
qryOpGT,
|
||||
qryOpLE,
|
||||
qryOpGE,
|
||||
qryOpBEGINS,
|
||||
qryOpCONTAINS,
|
||||
qryOpLIKE,
|
||||
qryOpBETWEEN
|
||||
};
|
||||
|
||||
|
||||
// Query strings
|
||||
char * const langQRY_EQ = "column = column | value";
|
||||
char * const langQRY_LT = "column < column | value";
|
||||
char * const langQRY_GT = "column > column | value";
|
||||
char * const langQRY_LE = "column <= column | value";
|
||||
char * const langQRY_GE = "column >= column | value";
|
||||
char * const langQRY_BEGINS = "columns that BEGIN with the string entered";
|
||||
char * const langQRY_CONTAINS = "columns that CONTAIN the string entered";
|
||||
char * const langQRY_LIKE = "% matches 0 or more of any char; _ matches 1 char";
|
||||
char * const langQRY_BETWEEN = "column BETWEEN value AND value";
|
||||
|
||||
|
||||
class CqueryDlg : public wxDialogBox
|
||||
{
|
||||
private:
|
||||
CcolInf *colInf; // Column inf. returned by db->GetColumns()
|
||||
wxTable *dbTable;
|
||||
char *masterTableName;
|
||||
char *pWhere; // A pointer to the storage for the resulting where clause
|
||||
wxDB *pDB;
|
||||
|
||||
public:
|
||||
bool widgetPtrsSet;
|
||||
|
||||
// Widget pointers
|
||||
wxMessage *pQueryCol1Msg;
|
||||
wxChoice *pQueryCol1Choice;
|
||||
wxMessage *pQueryNotMsg;
|
||||
wxCheckBox *pQueryNotCheck;
|
||||
wxMessage *pQueryOperatorMsg;
|
||||
wxChoice *pQueryOperatorChoice;
|
||||
wxMessage *pQueryCol2Msg;
|
||||
wxChoice *pQueryCol2Choice;
|
||||
wxMessage *pQueryValue1Msg;
|
||||
wxText *pQueryValue1Txt;
|
||||
wxMessage *pQueryValue2Msg;
|
||||
wxText *pQueryValue2Txt;
|
||||
wxMessage *pQuerySqlWhereMsg;
|
||||
wxMultiText *pQuerySqlWhereMtxt;
|
||||
wxButton *pQueryAddBtn;
|
||||
wxButton *pQueryAndBtn;
|
||||
wxButton *pQueryOrBtn;
|
||||
wxButton *pQueryLParenBtn;
|
||||
wxButton *pQueryRParenBtn;
|
||||
wxButton *pQueryDoneBtn;
|
||||
wxButton *pQueryClearBtn;
|
||||
wxButton *pQueryCountBtn;
|
||||
wxButton *pQueryHelpBtn;
|
||||
wxGroupBox *pQueryHintGrp;
|
||||
wxMessage *pQueryHintMsg;
|
||||
|
||||
wxText *pFocusTxt;
|
||||
|
||||
CqueryDlg(wxWindow *parent, wxDB *pDb, char *tblName[], char *pWhereArg);
|
||||
|
||||
void OnCommand(wxWindow& win, wxCommandEvent& event);
|
||||
bool OnClose();
|
||||
void OnActivate(bool) {}; // necessary for hot keys
|
||||
|
||||
// bool SetWidgetPtrs();
|
||||
void AppendToWhere(char *s);
|
||||
void ProcessAddBtn();
|
||||
void ProcessCountBtn();
|
||||
bool ValidateWhereClause();
|
||||
|
||||
}; // CqueryDlg
|
BIN
samples/db/dbtest.ico
Normal file
BIN
samples/db/dbtest.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 766 B |
3
samples/db/dbtest.rc
Normal file
3
samples/db/dbtest.rc
Normal file
@@ -0,0 +1,3 @@
|
||||
db_icon ICON "dbtest.ico"
|
||||
#include "wx.rc"
|
||||
|
412
samples/db/listdb.cpp
Normal file
412
samples/db/listdb.cpp
Normal file
@@ -0,0 +1,412 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Name: listdb.cpp
|
||||
// Purpose: Data table lookup listbox code
|
||||
// Author: George Tasker/Doug Card
|
||||
// Modified by:
|
||||
// Created: 1996
|
||||
// RCS-ID: $Id$
|
||||
// Copyright: (c) 1996 Remstar International, Inc.
|
||||
// Licence: wxWindows licence
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
// SYNOPSIS START
|
||||
|
||||
Member functions for the classes defined in LISTDB.H
|
||||
|
||||
This class is used to present a generic ListBox lookup window for
|
||||
use with any of the object creation/selection choice widgets. This
|
||||
dialog window will present a (possibly) scrolling list of values
|
||||
that come from a data table source. Based on the object type passed
|
||||
in the constructor, a ListBox is built to present the user with a
|
||||
single selection listbox.
|
||||
|
||||
The string selected from the list box is stored in the Global variable
|
||||
"ListDB_Seclection", and will remain set until another interation of this
|
||||
routine is called.
|
||||
|
||||
For each object (database) type that is to be used, an overridden
|
||||
constructor should be written to appropriately link to the proper
|
||||
data table/object for building the list.
|
||||
|
||||
The data table record access is all handled through the routines
|
||||
in this module, interfacing with the methods defined in wxTable.
|
||||
|
||||
All objects which use data table access must be initialized and
|
||||
have opened the table prior to passing them in the dialog
|
||||
constructor, and the 'where' query should already have been set
|
||||
and performed before creating this dialog instance.
|
||||
|
||||
// SYNOPSIS STOP
|
||||
*/
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation "listdb.h"
|
||||
#endif
|
||||
|
||||
#include "wx/wxprec.h"
|
||||
|
||||
#ifdef __BORLANDC__
|
||||
#pragma hdrstop
|
||||
#endif //__BORLANDC__
|
||||
|
||||
#ifndef WX_PRECOMP
|
||||
#include <wx/wx.h>
|
||||
#endif //WX_PRECOMP
|
||||
|
||||
#include <wx/dbtable.h>
|
||||
|
||||
#include "listdb.h"
|
||||
|
||||
// Global structure for holding ODBC connection information
|
||||
extern DbStuff DbConnectInf;
|
||||
|
||||
// Global database connection
|
||||
extern wxDB *READONLY_DB;
|
||||
|
||||
|
||||
// Used for passing the selected listbox selection back to the calling
|
||||
// routine. This variable must be declared as 'extern' in the calling
|
||||
// source module
|
||||
char ListDB_Selection[LOOKUP_COL_LEN+1];
|
||||
|
||||
// If the listbox contains two columns of data, the second column is
|
||||
// returned in this variable.
|
||||
char ListDB_Selection2[LOOKUP_COL_LEN+1];
|
||||
|
||||
// Constants
|
||||
const LISTDB_NO_SPACES_BETWEEN_COLS = 3;
|
||||
|
||||
|
||||
// Clookup constructor
|
||||
Clookup::Clookup(char *tblName, char *colName) : wxTable(READONLY_DB, tblName, 1)
|
||||
{
|
||||
|
||||
SetColDefs (0, colName, DB_DATA_TYPE_VARCHAR, lookupCol, SQL_C_CHAR, LOOKUP_COL_LEN+1, FALSE, FALSE);
|
||||
|
||||
} // Clookup()
|
||||
|
||||
|
||||
// Clookup2 constructor
|
||||
Clookup2::Clookup2(char *tblName, char *colName1, char *colName2, wxDB *pDb)
|
||||
: wxTable(pDb, tblName, (1 + (strlen(colName2) > 0)))
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
SetColDefs (i, colName1, DB_DATA_TYPE_VARCHAR, lookupCol1, SQL_C_CHAR, LOOKUP_COL_LEN+1, FALSE, FALSE);
|
||||
|
||||
if (strlen(colName2) > 0)
|
||||
SetColDefs (++i, colName2, DB_DATA_TYPE_VARCHAR, lookupCol2, SQL_C_CHAR, LOOKUP_COL_LEN+1, FALSE, FALSE);
|
||||
|
||||
} // Clookup2()
|
||||
|
||||
|
||||
// This is a generic lookup constructor that will work with any table and any column
|
||||
ClookUpDlg::ClookUpDlg(wxWindow *parent, char *windowTitle, char *tableName, char *colName,
|
||||
char *where, char *orderBy) : wxDialogBox (parent, "Select...", 1, -1, -1, 400, 290)
|
||||
{
|
||||
wxBeginBusyCursor();
|
||||
|
||||
strcpy(ListDB_Selection,"");
|
||||
widgetPtrsSet = FALSE;
|
||||
lookup = 0;
|
||||
lookup2 = 0;
|
||||
noDisplayCols = 1;
|
||||
col1Len = 0;
|
||||
|
||||
// Build the dialog
|
||||
SetLabelPosition(wxVERTICAL);
|
||||
|
||||
wxFont *ButtonFont = new wxFont(12,wxSWISS,wxNORMAL,wxBOLD);
|
||||
wxFont *TextFont = new wxFont(12,wxSWISS,wxNORMAL,wxNORMAL);
|
||||
|
||||
SetButtonFont(ButtonFont);
|
||||
SetLabelFont(TextFont);
|
||||
SetLabelPosition(wxVERTICAL);
|
||||
|
||||
pLookUpSelectList = new wxListBox(this, NULL, "", wxSINGLE|wxALWAYS_SB, 5, 15, 384, 195, 0, 0, 0, "LookUpSelectList");
|
||||
pLookUpOkBtn = new wxButton(this, NULL, "&Ok", 113, 222, 70, 35, 0, "LookUpOkBtn");
|
||||
pLookUpCancelBtn = new wxButton(this, NULL, "C&ancel", 212, 222, 70, 35, 0, "LookUpCancelBtn");
|
||||
|
||||
widgetPtrsSet = TRUE;
|
||||
|
||||
// Query the lookup table and display the result set
|
||||
if (!(lookup = new Clookup(tableName, colName)))
|
||||
{
|
||||
wxMessageBox("Error allocating memory for 'Clookup'object.","Error...");
|
||||
Close();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!lookup->Open())
|
||||
{
|
||||
wxString tStr;
|
||||
tStr.sprintf("Unable to open the table '%s'.",tableName);
|
||||
wxMessageBox(tStr.GetData(),"ODBC Error...");
|
||||
Close();
|
||||
return;
|
||||
}
|
||||
|
||||
lookup->orderBy = orderBy;
|
||||
lookup->where = where;
|
||||
if (!lookup->Query())
|
||||
{
|
||||
wxMessageBox("ODBC error during Query()","ODBC Error...");
|
||||
Close();
|
||||
return;
|
||||
}
|
||||
|
||||
// Fill in the list box from the query result set
|
||||
while (lookup->GetNext())
|
||||
pLookUpSelectList->Append(lookup->lookupCol);
|
||||
|
||||
// Highlight the first list item
|
||||
pLookUpSelectList->SetSelection(0);
|
||||
|
||||
// Make the OK activate by pressing Enter
|
||||
if (pLookUpSelectList->Number())
|
||||
pLookUpOkBtn->SetDefault();
|
||||
else
|
||||
{
|
||||
pLookUpCancelBtn->SetDefault();
|
||||
pLookUpOkBtn->Enable(FALSE);
|
||||
}
|
||||
|
||||
// Display the dialog window
|
||||
SetTitle(windowTitle);
|
||||
Centre(wxBOTH);
|
||||
wxEndBusyCursor();
|
||||
Show(TRUE);
|
||||
|
||||
} // Generic lookup constructor
|
||||
|
||||
|
||||
//
|
||||
// This is a generic lookup constructor that will work with any table and any column.
|
||||
// It extends the capabilites of the lookup dialog in the following ways:
|
||||
//
|
||||
// 1) 2 columns rather than one
|
||||
// 2) The ability to select DISTINCT column values
|
||||
//
|
||||
// Only set distinctValues equal to true if necessary. In many cases, the constraints
|
||||
// of the index(es) will enforce this uniqueness. Selecting DISTINCT does require
|
||||
// overhead by the database to ensure that all values returned are distinct. Therefore,
|
||||
// use this ONLY when you need it.
|
||||
//
|
||||
// For complicated queries, you can pass in the sql select statement. This would be
|
||||
// necessary if joins are involved since by default both columns must come from the
|
||||
// same table.
|
||||
//
|
||||
// If you do query by sql statement, you must pass in the maximum lenght of column1,
|
||||
// since it cannot be derived when you query using your own sql statement.
|
||||
//
|
||||
// The optional database connection can be used if you'd like the lookup class
|
||||
// to use a database pointer other than the global READONLY_DB. This is necessary if
|
||||
// records are being saved, but not committed to the db, yet should be included
|
||||
// in the lookup window.
|
||||
//
|
||||
ClookUpDlg::ClookUpDlg(wxWindow *parent, char *windowTitle, char *tableName,
|
||||
char *dispCol1, char *dispCol2, char *where, char *orderBy, bool distinctValues,
|
||||
char *selectStmt, int maxLenCol1, wxDB *pDb, bool allowOk) : wxDialogBox (parent, "Select...", 1, -1, -1, 400, 290)
|
||||
{
|
||||
wxBeginBusyCursor();
|
||||
|
||||
strcpy(ListDB_Selection,"");
|
||||
strcpy(ListDB_Selection2,"");
|
||||
widgetPtrsSet = FALSE;
|
||||
lookup = 0;
|
||||
lookup2 = 0;
|
||||
noDisplayCols = (strlen(dispCol2) ? 2 : 1);
|
||||
col1Len = 0;
|
||||
|
||||
// Build the dialog
|
||||
SetLabelPosition(wxVERTICAL);
|
||||
|
||||
wxFont *ButtonFont = new wxFont(12,wxSWISS,wxNORMAL,wxBOLD);
|
||||
wxFont *TextFont = new wxFont(12,wxSWISS,wxNORMAL,wxNORMAL);
|
||||
wxFont *FixedFont = new wxFont(12,wxMODERN,wxNORMAL,wxNORMAL);
|
||||
|
||||
SetButtonFont(ButtonFont);
|
||||
SetLabelFont(TextFont);
|
||||
SetLabelPosition(wxVERTICAL);
|
||||
|
||||
// this is done with fixed font so that the second column (if any) will be left
|
||||
// justified in the second column
|
||||
SetButtonFont(FixedFont);
|
||||
pLookUpSelectList = new wxListBox(this, NULL, "", wxSINGLE|wxALWAYS_SB, 5, 15, 384, 195, 0, 0, 0, "LookUpSelectList");
|
||||
SetButtonFont(ButtonFont);
|
||||
pLookUpOkBtn = new wxButton(this, NULL, "&Ok", 113, 222, 70, 35, 0, "LookUpOkBtn");
|
||||
pLookUpCancelBtn = new wxButton(this, NULL, "C&ancel", 212, 222, 70, 35, 0, "LookUpCancelBtn");
|
||||
|
||||
widgetPtrsSet = TRUE;
|
||||
|
||||
// Query the lookup table and display the result set
|
||||
if (!(lookup2 = new Clookup2(tableName, dispCol1, dispCol2, pDb)))
|
||||
{
|
||||
wxMessageBox("Error allocating memory for 'Clookup2'object.","Error...");
|
||||
Close();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!lookup2->Open())
|
||||
{
|
||||
wxString tStr;
|
||||
tStr.sprintf("Unable to open the table '%s'.",tableName);
|
||||
wxMessageBox(tStr.GetData(),"ODBC Error...");
|
||||
Close();
|
||||
return;
|
||||
}
|
||||
|
||||
// If displaying 2 columns, determine the maximum length of column1
|
||||
int maxColLen;
|
||||
if (maxLenCol1)
|
||||
maxColLen = col1Len = maxLenCol1; // user passed in max col length for column 1
|
||||
else
|
||||
{
|
||||
maxColLen = LOOKUP_COL_LEN;
|
||||
if (strlen(dispCol2))
|
||||
{
|
||||
wxString q = "SELECT MAX({fn LENGTH(";
|
||||
q += dispCol1;
|
||||
q += ")}), NULL";
|
||||
q += " FROM ";
|
||||
q += tableName;
|
||||
if (strlen(where))
|
||||
{
|
||||
q += " WHERE ";
|
||||
q += where;
|
||||
}
|
||||
if (!lookup2->QueryBySqlStmt(q.GetData()))
|
||||
{
|
||||
wxMessageBox("ODBC error during QueryBySqlStmt()","ODBC Error...");
|
||||
Close();
|
||||
return;
|
||||
}
|
||||
if (lookup2->GetNext())
|
||||
maxColLen = col1Len = atoi(lookup2->lookupCol1);
|
||||
else
|
||||
wxMessageBox("ODBC error during GetNext()","ODBC Error...");
|
||||
}
|
||||
}
|
||||
|
||||
// Query the actual record set
|
||||
if (selectStmt && strlen(selectStmt)) // Query by sql stmt passed in
|
||||
{
|
||||
if (!lookup2->QueryBySqlStmt(selectStmt))
|
||||
{
|
||||
wxMessageBox("ODBC error during QueryBySqlStmt()","ODBC Error...");
|
||||
Close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
else // Query using where and order by clauses
|
||||
{
|
||||
lookup2->orderBy = orderBy;
|
||||
lookup2->where = where;
|
||||
if (!lookup2->Query(FALSE, distinctValues))
|
||||
{
|
||||
wxMessageBox("ODBC error during Query()","ODBC Error...");
|
||||
Close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Fill in the list box from the query result set
|
||||
wxString s;
|
||||
while (lookup2->GetNext())
|
||||
{
|
||||
s = lookup2->lookupCol1;
|
||||
if (strlen(dispCol2)) // Append the optional column 2
|
||||
{
|
||||
s.Append(' ', (maxColLen + LISTDB_NO_SPACES_BETWEEN_COLS - strlen(lookup2->lookupCol1)));
|
||||
s.Append(lookup2->lookupCol2);
|
||||
}
|
||||
pLookUpSelectList->Append(s.GetData());
|
||||
}
|
||||
|
||||
// Highlight the first list item
|
||||
pLookUpSelectList->SetSelection(0);
|
||||
|
||||
// Make the OK activate by pressing Enter
|
||||
if (pLookUpSelectList->Number())
|
||||
pLookUpOkBtn->SetDefault();
|
||||
else
|
||||
{
|
||||
pLookUpCancelBtn->SetDefault();
|
||||
pLookUpOkBtn->Enable(FALSE);
|
||||
}
|
||||
|
||||
pLookUpOkBtn->Enable(allowOk);
|
||||
|
||||
// Display the dialog window
|
||||
SetTitle(windowTitle);
|
||||
Centre(wxBOTH);
|
||||
wxEndBusyCursor();
|
||||
Show(TRUE);
|
||||
|
||||
} // Generic lookup constructor 2
|
||||
|
||||
|
||||
bool ClookUpDlg::OnClose(void)
|
||||
{
|
||||
widgetPtrsSet = FALSE;
|
||||
GetParent()->Enable(TRUE);
|
||||
|
||||
if (lookup)
|
||||
delete lookup;
|
||||
if (lookup2)
|
||||
delete lookup2;
|
||||
|
||||
wxEndBusyCursor();
|
||||
return TRUE;
|
||||
|
||||
} // ClookUpDlg::OnClose
|
||||
|
||||
|
||||
void ClookUpDlg::OnCommand(wxWindow& win, wxCommandEvent& event)
|
||||
{
|
||||
wxString widgetName = win.GetName();
|
||||
|
||||
if (widgetPtrsSet)
|
||||
{
|
||||
// OK Button
|
||||
if (widgetName == pLookUpOkBtn->GetName())
|
||||
{
|
||||
if (pLookUpSelectList->GetSelection() != -1)
|
||||
{
|
||||
if (noDisplayCols == 1)
|
||||
strcpy (ListDB_Selection, pLookUpSelectList->GetStringSelection());
|
||||
else // 2 display columns
|
||||
{
|
||||
wxString s = pLookUpSelectList->GetStringSelection();
|
||||
// Column 1
|
||||
s = s.SubString(0, col1Len-1);
|
||||
s = s.Strip();
|
||||
strcpy(ListDB_Selection, s.GetData());
|
||||
// Column 2
|
||||
s = pLookUpSelectList->GetStringSelection();
|
||||
s = s.From(col1Len + LISTDB_NO_SPACES_BETWEEN_COLS);
|
||||
s = s.Strip();
|
||||
strcpy(ListDB_Selection2, s.GetData());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
strcpy(ListDB_Selection,"");
|
||||
strcpy(ListDB_Selection2,"");
|
||||
}
|
||||
Close();
|
||||
} // OK Button
|
||||
|
||||
// Cancel Button
|
||||
if (widgetName == pLookUpCancelBtn->GetName())
|
||||
{
|
||||
strcpy (ListDB_Selection,"");
|
||||
strcpy (ListDB_Selection2,"");
|
||||
Close();
|
||||
} // Cancel Button
|
||||
}
|
||||
|
||||
}; // ClookUpDlg::OnCommand
|
||||
|
||||
// *********************************** listdb.cpp **********************************
|
125
samples/db/listdb.h
Normal file
125
samples/db/listdb.h
Normal file
@@ -0,0 +1,125 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Name: listdb.h
|
||||
// Purpose: wxWindows database demo app
|
||||
// Author: George Tasker
|
||||
// Modified by:
|
||||
// Created: 1996
|
||||
// RCS-ID: $Id$
|
||||
// Copyright: (c) 1996 Remstar International, Inc.
|
||||
// Licence: wxWindows licence
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma interface "listdb.h"
|
||||
|
||||
/*
|
||||
/*
|
||||
// SYNOPSIS START
|
||||
|
||||
Contains dialog class for creating a data table lookup listbox
|
||||
|
||||
// SYNOPSIS STOP
|
||||
*/
|
||||
|
||||
#ifndef LISTDB_DOT_H
|
||||
#define LISTDB_DOT_H
|
||||
|
||||
|
||||
#include <wx/dbtable.h>
|
||||
|
||||
const LOOKUP_COL_LEN = 250;
|
||||
|
||||
// Global database connection
|
||||
extern wxDB *READONLY_DB;
|
||||
|
||||
// Clookup class
|
||||
class Clookup : public wxTable
|
||||
{
|
||||
public:
|
||||
|
||||
char lookupCol[LOOKUP_COL_LEN+1];
|
||||
|
||||
Clookup(char *tblName, char *colName);
|
||||
|
||||
}; // Clookup
|
||||
|
||||
// Clookup2 class
|
||||
class Clookup2 : public wxTable
|
||||
{
|
||||
public:
|
||||
|
||||
char lookupCol1[LOOKUP_COL_LEN+1];
|
||||
char lookupCol2[LOOKUP_COL_LEN+1];
|
||||
|
||||
Clookup2(char *tblName, char *colName1, char *colName2, wxDB *pDb);
|
||||
|
||||
}; // Clookup2
|
||||
|
||||
class ClookUpDlg : public wxDialogBox
|
||||
{
|
||||
private:
|
||||
bool widgetPtrsSet;
|
||||
int currentCursor;
|
||||
Clookup *lookup;
|
||||
Clookup2 *lookup2;
|
||||
int noDisplayCols;
|
||||
int col1Len;
|
||||
|
||||
wxListBox *pLookUpSelectList;
|
||||
wxButton *pLookUpOkBtn;
|
||||
wxButton *pLookUpCancelBtn;
|
||||
|
||||
public:
|
||||
|
||||
// This is a generic lookup constructor that will work with any table and any column
|
||||
ClookUpDlg(wxWindow *parent,
|
||||
char *windowTitle,
|
||||
char *tableName,
|
||||
char *colName,
|
||||
char *where,
|
||||
char *orderBy);
|
||||
|
||||
//
|
||||
// This is a generic lookup constructor that will work with any table and any column.
|
||||
// It extends the capabilites of the lookup dialog in the following ways:
|
||||
//
|
||||
// 1) 2 columns rather than one
|
||||
// 2) The ability to select DISTINCT column values
|
||||
//
|
||||
// Only set distinctValues equal to true if necessary. In many cases, the constraints
|
||||
// of the index(es) will enforce this uniqueness. Selecting DISTINCT does require
|
||||
// overhead by the database to ensure that all values returned are distinct. Therefore,
|
||||
// use this ONLY when you need it.
|
||||
//
|
||||
// For complicated queries, you can pass in the sql select statement. This would be
|
||||
// necessary if joins are involved since by default both columns must come from the
|
||||
// same table.
|
||||
//
|
||||
// If you do query by sql statement, you must pass in the maximum length of column1,
|
||||
// since it cannot be derived when you query using your own sql statement.
|
||||
//
|
||||
// The optional database connection can be used if you'd like the lookup class
|
||||
// to use a database pointer other than the global READONLY_DB. This is necessary if
|
||||
// records are being saved, but not committed to the db, yet should be included
|
||||
// in the lookup window.
|
||||
//
|
||||
ClookUpDlg(wxWindow *parent,
|
||||
char *windowTitle,
|
||||
char *tableName,
|
||||
char *dispCol1, // Must have at least 1 display column
|
||||
char *dispCol2, // Optional
|
||||
char *where,
|
||||
char *orderBy,
|
||||
bool distinctValues, // e.g. SELECT DISTINCT ...
|
||||
char *selectStmt = 0, // If you wish to query by SQLstmt (complicated lookups)
|
||||
int maxLenCol1 = 0, // Mandatory if querying by SQLstmt
|
||||
wxDB *pDb = READONLY_DB, // Database connection pointer
|
||||
bool allowOk = TRUE); // is the OK button enabled
|
||||
|
||||
void OnCommand(wxWindow& win, wxCommandEvent& event);
|
||||
bool OnClose();
|
||||
void OnActivate(bool) {}; // necessary for hot keys
|
||||
};
|
||||
|
||||
#endif // LISTDB_DOT_H
|
||||
|
||||
// ************************************ listdb.h *********************************
|
97
samples/db/makefile.nt
Normal file
97
samples/db/makefile.nt
Normal file
@@ -0,0 +1,97 @@
|
||||
#
|
||||
# File: makefile.nt
|
||||
# Author: George Tasker
|
||||
# Created: 1998
|
||||
# Updated:
|
||||
#
|
||||
# "%W% %G%"
|
||||
#
|
||||
# Makefile : Builds database example (MS VC++).
|
||||
|
||||
!if "$(FINAL)" == ""
|
||||
FINAL=0
|
||||
!endif
|
||||
|
||||
|
||||
!if "$(MSVCDIR)" == ""
|
||||
MSVCDIR=c:\devstudio\vc
|
||||
!endif
|
||||
|
||||
# Set WXDIR for your system
|
||||
WXDIR = $(WXWIN)
|
||||
THISDIR = $(WXDIR)\samples\database
|
||||
WXODBCDIR = $(WXDIR)\utils\wxodbc
|
||||
|
||||
!if "$(MSVCDIR)" == ""
|
||||
DBLIBS=$(MSDEVDIR)\lib\odbc32.lib
|
||||
!else
|
||||
DBLIBS=$(MSVCDIR)\lib\odbc32.lib
|
||||
!endif
|
||||
|
||||
EXTRAINC = -I$(WXODBCDIR)\src
|
||||
EXTRALIBS = $(DBLIBS) $(WXODBCDIR)\lib\wxodbc.lib
|
||||
|
||||
!include $(WXDIR)\src\ntwxwin.mak
|
||||
|
||||
PROGRAM=database
|
||||
|
||||
OBJECTS = $(PROGRAM).$(OBJSUFF) listdb.$(OBJSUFF)
|
||||
|
||||
all: wxodbc $(PROGRAM).exe
|
||||
|
||||
$(PROGRAM): $(PROGRAM).exe
|
||||
|
||||
gt:
|
||||
cd $(CPPFLAGS)
|
||||
|
||||
wxodbc:
|
||||
cd $(WXODBCDIR)\src
|
||||
nmake -f makefile.nt FINAL=$(FINAL)
|
||||
cd $(THISDIR)
|
||||
|
||||
wx:
|
||||
cd $(WXDIR)\src\msw
|
||||
nmake -f makefile.nt FINAL=$(FINAL)
|
||||
cd $(THISDIR)
|
||||
|
||||
wxclean:
|
||||
cd $(WXDIR)\src\msw
|
||||
nmake -f makefile.nt clean
|
||||
cd $(THISDIR)
|
||||
cd $(WXODBCDIR)\src
|
||||
nmake -f makefile.nt clean
|
||||
cd $(THISDIR)
|
||||
|
||||
|
||||
$(PROGRAM).exe: $(DUMMYOBJ) $(OBJECTS) $(PROGRAM).res
|
||||
$(link) @<<
|
||||
-out:$(PROGRAM).exe
|
||||
$(LINKFLAGS)
|
||||
$(DUMMYOBJ) $(OBJECTS) $(PROGRAM).res
|
||||
$(LIBS)
|
||||
<<
|
||||
|
||||
|
||||
listdb.$(OBJSUFF): $(*B).$(SRCSUFF) $(*B).h
|
||||
$(cc) @<<
|
||||
$(CPPFLAGS) /c /Fo$(*B).$(OBJSUFF) /Tp $(*B).$(SRCSUFF)
|
||||
<<
|
||||
|
||||
|
||||
$(PROGRAM).$(OBJSUFF): $(PROGRAM).$(SRCSUFF) $(PROGRAM).h listdb.h
|
||||
$(cc) @<<
|
||||
$(CPPFLAGS) /c /Fo$(*B).$(OBJSUFF) /Tp $(*B).$(SRCSUFF)
|
||||
<<
|
||||
|
||||
|
||||
$(PROGRAM).res: $(PROGRAM).rc $(WXDIR)\include\wx\msw\wx.rc
|
||||
$(rc) -r /i$(WXDIR)\include -fo$@ $(PROGRAM).rc
|
||||
|
||||
|
||||
clean:
|
||||
-erase *.obj
|
||||
-erase *.exe
|
||||
-erase *.res
|
||||
-erase *.map
|
||||
-erase *.sbr
|
||||
-erase *.pdb
|
71
samples/db/makefile.unx
Normal file
71
samples/db/makefile.unx
Normal file
@@ -0,0 +1,71 @@
|
||||
#
|
||||
# File: makefile.unx
|
||||
# Author: Terry Tompkins
|
||||
# Created: 1998
|
||||
# Updated:
|
||||
# Copyright: (c) 1998, Remstar International
|
||||
#
|
||||
# Makefile for wxDB (UNIX).
|
||||
|
||||
OBJDIR=database
|
||||
OBJSUFF=.o
|
||||
SRCSUFF=.cpp
|
||||
WXDIR = $(WXWIN)
|
||||
|
||||
# All common UNIX compiler flags and options are now in
|
||||
# this central makefile.
|
||||
include $(WXDIR)/src/make.env
|
||||
|
||||
PROGRAM=database
|
||||
|
||||
OBJECTS = $(OBJDIR)/$(PROGRAM).$(OBJSUFF) $(OBJDIR)/table.$(OBJSUFF) $(OBJDIR)/db.$(OBJSUFF) $(OBJDIR)/listdb.$(OBJSUFF)
|
||||
|
||||
.SUFFIXES:
|
||||
|
||||
all: $(OBJDIR) $(PROGRAM)$(GUISUFFIX)
|
||||
|
||||
wx:
|
||||
|
||||
|
||||
motif:
|
||||
$(MAKE) -f makefile.unx GUISUFFIX=_motif GUI=-Dwx_motif GUISUFFIX=_motif OPT='$(OPT)' LDLIBS='$(MOTIFLDLIBS)' WXLIB=$(WXDIR)/lib/libwx_motif.a OPTIONS='$(OPTIONS)' DEBUG='$(DEBUG)' WARN='$(WARN)' XLIB='$(XLIB)' XINCLUDE='$(XINCLUDE)' XVIEW_LINK=
|
||||
|
||||
xview:
|
||||
cd $(WXDIR)/src/x; $(MAKE) -f makefile.unx xview
|
||||
$(MAKE) -f makefile.unx GUI=-Dwx_xview GUISUFFIX=_ol CC=$(CC) OPTIONS='$(OPTIONS)' DEBUG='$(DEBUG)' WARN='$(WARN)' XLIB='$(XLIB)' XINCLUDE='$(XINCLUDE)'
|
||||
|
||||
hp:
|
||||
cd $(WXDIR)/src/x; $(MAKE) -f makefile.unx hp
|
||||
$(MAKE) -f makefile.unx GUI=-Dwx_motif GUISUFFIX=_hp CC=CC DEBUG='$(DEBUG)' WARN='-w' \
|
||||
XINCLUDE='$(HPXINCLUDE)' XLIB='$(HPXLIB)' XVIEW_LINK='' LDLIBS='$(HPLDLIBS)'
|
||||
|
||||
$(OBJDIR):
|
||||
mkdir $(OBJDIR)
|
||||
|
||||
$(PROGRAM)$(GUISUFFIX): $(DUMMYOBJ) $(DBLIBS) $(OBJECTS) $(WXLIB)
|
||||
$(CC) $(LDFLAGS) -o $(PROGRAM)$(GUISUFFIX) $(OBJECTS) $(XVIEW_LINK) $(LDLIBS)
|
||||
|
||||
$(OBJDIR)/$(PROGRAM).$(OBJSUFF): $(PROGRAM).$(SRCSUFF)
|
||||
$(CC) -c $(CPPFLAGS) -o $@ $(PROGRAM).$(SRCSUFF)
|
||||
|
||||
$(OBJDIR)/table.$(OBJSUFF): table.$(SRCSUFF)
|
||||
$(CC) -c $(CPPFLAGS) -o $@ table.$(SRCSUFF)
|
||||
|
||||
$(OBJDIR)/db.$(OBJSUFF): db.$(SRCSUFF)
|
||||
$(CC) -c $(CPPFLAGS) -o $@ db.$(SRCSUFF)
|
||||
|
||||
$(OBJDIR)/listdb.$(OBJSUFF): listdb.$(SRCSUFF)
|
||||
$(CC) -c $(CPPFLAGS) -o $@ listdb.$(SRCSUFF)
|
||||
|
||||
clean_motif:
|
||||
$(MAKE) -f makefile.unx GUISUFFIX=_motif cleanany
|
||||
|
||||
clean_ol:
|
||||
$(MAKE) -f makefile.unx GUISUFFIX=_ol cleanany
|
||||
|
||||
clean_hp:
|
||||
$(MAKE) -f makefile.unx GUISUFFIX=_hp cleanany
|
||||
|
||||
cleanany:
|
||||
rm -f $(OBJECTS) $(PROGRAM)$(GUISUFFIX) core
|
||||
|
Reference in New Issue
Block a user