]> git.saurik.com Git - wxWidgets.git/blame - include/wx/dbtable.h
ODBC database class documentation updates
[wxWidgets.git] / include / wx / dbtable.h
CommitLineData
108106cf 1///////////////////////////////////////////////////////////////////////////////
a2115c88 2// Name: dbtable.h
f6bcfd97 3// Purpose: Declaration of the wxDbTable class.
108106cf 4// Author: Doug Card
30a6d2d2 5// Modified by: George Tasker
3ca6a5f0
BP
6// Bart Jourquin
7// Mark Johnson
108106cf
JS
8// Created: 9.96
9// RCS-ID: $Id$
10// Copyright: (c) 1996 Remstar International, Inc.
11// Licence: wxWindows licence, plus:
4fdae997 12// Notice: This class library and its intellectual design are free of charge for use,
108106cf
JS
13// modification, enhancement, debugging under the following conditions:
14// 1) These classes may only be used as part of the implementation of a
15// wxWindows-based application
16// 2) All enhancements and bug fixes are to be submitted back to the wxWindows
17// user groups free of all charges for use with the wxWindows library.
18// 3) These classes may not be distributed as part of any other class library,
19// DLL, text (written or electronic), other than a complete distribution of
20// the wxWindows GUI development toolkit.
21///////////////////////////////////////////////////////////////////////////////
22
23/*
24// SYNOPSIS START
25// SYNOPSIS STOP
26*/
27
a2115c88
GT
28#ifndef DBTABLE_DOT_H
29#define DBTABLE_DOT_H
108106cf 30
a2115c88
GT
31// Use this line for wxWindows v1.x
32//#include "wx_ver.h"
33// Use this line for wxWindows v2.x
34#include "wx/version.h"
35
36#if wxMAJOR_VERSION == 2
95ca6a20 37 #ifdef __GNUG__
3ca6a5f0 38 #pragma interface "dbtable.h"
95ca6a20 39 #endif
108106cf
JS
40#endif
41
a2115c88 42#if wxMAJOR_VERSION == 2
95ca6a20 43 #include "wx/db.h"
a2115c88 44#else
95ca6a20 45 #include "db.h"
a2115c88 46#endif
108106cf 47
f6bcfd97
BP
48const int wxDB_ROWID_LEN = 24; // 18 is the max, 24 is in case it gets larger
49const int wxDB_DEFAULT_CURSOR = 0;
50const bool wxDB_QUERY_ONLY = TRUE;
51const bool wxDB_DISABLE_VIEW = TRUE;
108106cf 52
2b63ff5d
GT
53// Used to indicate end of a variable length list of
54// column numbers passed to member functions
55const int wxDB_NO_MORE_COLUMN_NUMBERS = -1;
56
108106cf 57// The following class is used to define a column of a table.
f6bcfd97 58// The wxDbTable constructor will dynamically allocate as many of
108106cf 59// these as there are columns in the table. The class derived
f6bcfd97 60// from wxDbTable must initialize these column definitions in it's
108106cf 61// constructor. These column definitions provide inf. to the
f6bcfd97 62// wxDbTable class which allows it to create a table in the data
108106cf
JS
63// source, exchange data between the data source and the C++
64// object, and so on.
f6bcfd97 65class WXDLLEXPORT wxDbColDef
108106cf
JS
66{
67public:
4fdae997 68 wxChar ColName[DB_MAX_COLUMN_NAME_LEN+1]; // Column Name
95ca6a20
GT
69 int DbDataType; // Logical Data Type; e.g. DB_DATA_TYPE_INTEGER
70 int SqlCtype; // C data type; e.g. SQL_C_LONG
71 void *PtrDataObj; // Address of the data object
72 int SzDataObj; // Size, in bytes, of the data object
73 bool KeyField; // TRUE if this column is part of the PRIMARY KEY to the table; Date fields should NOT be KeyFields.
74 bool Updateable; // Specifies whether this column is updateable
75 bool InsertAllowed; // Specifies whether this column should be included in an INSERT statement
76 bool DerivedCol; // Specifies whether this column is a derived value
77 SDWORD CbValue; // Internal use only!!!
78 bool Null; // NOT FULLY IMPLEMENTED - Allows NULL values in Inserts and Updates
f6bcfd97 79}; // wxDbColDef
30a6d2d2 80
95ca6a20 81
f6bcfd97 82class WXDLLEXPORT wxDbColDataPtr
30a6d2d2
GT
83{
84public:
95ca6a20
GT
85 void *PtrDataObj;
86 int SzDataObj;
87 int SqlCtype;
f6bcfd97 88}; // wxDbColDataPtr
30a6d2d2
GT
89
90
108106cf 91// This structure is used when creating secondary indexes.
f6bcfd97 92class WXDLLEXPORT wxDbIdxDef
108106cf
JS
93{
94public:
4fdae997
GT
95 wxChar ColName[DB_MAX_COLUMN_NAME_LEN+1];
96 bool Ascending;
f6bcfd97 97}; // wxDbIdxDef
108106cf 98
95ca6a20 99
f6bcfd97 100class WXDLLEXPORT wxDbTable
108106cf
JS
101{
102private:
3ca6a5f0 103 ULONG tableID; // Used for debugging. This can help to match up mismatched constructors/destructors
108106cf 104
95ca6a20 105 // Private member variables
3ca6a5f0
BP
106 UDWORD cursorType;
107 bool insertable;
a2115c88 108
95ca6a20 109 // Private member functions
4fdae997
GT
110 bool initialize(wxDb *pwxDb, const wxString &tblName, const int nCols,
111 const wxString &qryTblName, bool qryOnly, const wxString &tblPath);
112 void cleanup();
113
114 bool bindParams(bool forUpdate); // called by the other 'bind' functions
3ca6a5f0
BP
115 bool bindInsertParams(void);
116 bool bindUpdateParams(void);
4fdae997 117
3ca6a5f0
BP
118 bool bindCols(HSTMT cursor);
119 bool getRec(UWORD fetchType);
4fdae997
GT
120 bool execDelete(const wxString &pSqlStmt);
121 bool execUpdate(const wxString &pSqlStmt);
122 bool query(int queryType, bool forUpdate, bool distinct, const wxString &pSqlStmt=wxT(""));
108106cf 123
f6bcfd97
BP
124#if !wxODBC_BACKWARD_COMPATABILITY
125// these were public
126 // Where, Order By and From clauses
127 wxString where; // Standard SQL where clause, minus the word WHERE
128 wxString orderBy; // Standard SQL order by clause, minus the ORDER BY
129 wxString from; // Allows for joins in a wxDbTable::Query(). Format: ",tbl,tbl..."
95ca6a20
GT
130
131 // ODBC Handles
f6bcfd97
BP
132 HENV henv; // ODBC Environment handle
133 HDBC hdbc; // ODBC DB Connection handle
134 HSTMT hstmt; // ODBC Statement handle
135 HSTMT *hstmtDefault; // Default cursor
136 HSTMT hstmtInsert; // ODBC Statement handle used specifically for inserts
137 HSTMT hstmtDelete; // ODBC Statement handle used specifically for deletes
138 HSTMT hstmtUpdate; // ODBC Statement handle used specifically for updates
139 HSTMT hstmtInternal; // ODBC Statement handle used internally only
140 HSTMT *hstmtCount; // ODBC Statement handle used by Count() function (No binding of columns)
95ca6a20 141
f6bcfd97
BP
142 // Flags
143 bool selectForUpdate;
95ca6a20 144
f6bcfd97
BP
145 // Pointer to the database object this table belongs to
146 wxDb *pDb;
95ca6a20 147
f6bcfd97 148 // Table Inf.
4fdae997
GT
149 wxString tablePath; // needed for dBase tables
150 wxString tableName; // Table name
151 wxString queryTableName; // Query Table Name
f6bcfd97
BP
152 int noCols; // # of columns in the table
153 bool queryOnly; // Query Only, no inserts, updates or deletes
95ca6a20 154
f6bcfd97
BP
155 // Column Definitions
156 wxDbColDef *colDefs; // Array of wxDbColDef structures
157#endif
158public:
159#if wxODBC_BACKWARD_COMPATABILITY
95ca6a20 160 // Where, Order By and From clauses
f6bcfd97
BP
161 char *where; // Standard SQL where clause, minus the word WHERE
162 char *orderBy; // Standard SQL order by clause, minus the ORDER BY
163 char *from; // Allows for joins in a wxDbTable::Query(). Format: ",tbl,tbl..."
164
165 // ODBC Handles
166 HENV henv; // ODBC Environment handle
167 HDBC hdbc; // ODBC DB Connection handle
168 HSTMT hstmt; // ODBC Statement handle
169 HSTMT *hstmtDefault; // Default cursor
170 HSTMT hstmtInsert; // ODBC Statement handle used specifically for inserts
171 HSTMT hstmtDelete; // ODBC Statement handle used specifically for deletes
172 HSTMT hstmtUpdate; // ODBC Statement handle used specifically for updates
173 HSTMT hstmtInternal; // ODBC Statement handle used internally only
174 HSTMT *hstmtCount; // ODBC Statement handle used by Count() function (No binding of columns)
95ca6a20
GT
175
176 // Flags
f6bcfd97 177 bool selectForUpdate;
95ca6a20 178
f6bcfd97
BP
179 // Pointer to the database object this table belongs to
180 wxDb *pDb;
181
182 // Table Inf.
183 char tablePath[wxDB_PATH_MAX]; // needed for dBase tables
184 char tableName[DB_MAX_TABLE_NAME_LEN+1]; // Table name
185 char queryTableName[DB_MAX_TABLE_NAME_LEN+1]; // Query Table Name
186 int noCols; // # of columns in the table
187 bool queryOnly; // Query Only, no inserts, updates or deletes
188
189 // Column Definitions
190 wxDbColDef *colDefs; // Array of wxDbColDef structures
191#endif
95ca6a20 192 // Public member functions
4fdae997 193 wxDbTable(wxDb *pwxDb, const wxString &tblName, const int nCols,
e7e5298c
GT
194 const wxString &qryTblName="", bool qryOnly = !wxDB_QUERY_ONLY,
195 const wxString &tblPath="");
4fdae997
GT
196
197 // DEPRECATED
198 wxDbTable(wxDb *pwxDb, const wxString &tblName, const int nCols,
e7e5298c
GT
199 const wxChar *qryTblName="", bool qryOnly = !wxDB_QUERY_ONLY,
200 const wxString &tblPath="");
4fdae997 201
f6bcfd97
BP
202 virtual ~wxDbTable();
203
f02d4a64 204 bool Open(bool checkPrivileges=FALSE);
f6bcfd97
BP
205 bool CreateTable(bool attemptDrop=TRUE);
206 bool DropTable(void);
e7e5298c
GT
207 bool CreateIndex(const wxString &idxName, bool unique, int noIdxCols,
208 wxDbIdxDef *pIdxDefs, bool attemptDrop=TRUE);
4fdae997 209 bool DropIndex(const wxString &idxName);
f6bcfd97 210
3ca6a5f0 211 // Accessors
f6bcfd97
BP
212
213 // The member variables returned by these accessors are all
214 // set when the wxDbTable instance is createand cannot be
215 // changed, hence there is no corresponding SetXxxx function
3ca6a5f0 216 wxDb *GetDb() { return pDb; }
4fdae997
GT
217 const wxString &GetTableName() { return tableName; }
218 const wxString &GetQueryTableName() { return queryTableName; }
219 const wxString &GetTablePath() { return tablePath; }
f6bcfd97 220
3ca6a5f0 221 int GetNumberOfColumns() { return noCols; } // number of "defined" columns for this wxDbTable instance
f6bcfd97
BP
222
223
4fdae997
GT
224 const wxString &GetFromClause() { return from; }
225 const wxString &GetOrderByClause() { return orderBy; }
226 const wxString &GetWhereClause() { return where; }
f6bcfd97 227
3ca6a5f0 228 bool IsQueryOnly() { return queryOnly; }
f6bcfd97
BP
229#if wxODBC_BACKWARD_COMPATABILITY
230 void SetFromClause(const char *From) { from = (char *)From; }
3ca6a5f0
BP
231 void SetOrderByClause(const char *OrderBy) { orderBy = (char *)OrderBy; }
232 void SetWhereClause(const char *Where) { where = (char *)Where; }
f6bcfd97 233#else
e7e5298c
GT
234 void SetFromClause(const wxString &From) { from = From; }
235 void SetOrderByClause(const wxString &OrderBy) { orderBy = OrderBy; }
2b63ff5d 236 bool SetOrderByColNums(int first, ...);
e7e5298c
GT
237 void SetWhereClause(const wxString &Where) { where = Where; }
238 void From(const wxString &From) { from = From; }
239 void OrderBy(const wxString &OrderBy) { orderBy = OrderBy; }
240 void Where(const wxString &Where) { where = Where; }
4fdae997
GT
241 const wxString &Where() { return where; }
242 const wxString &OrderBy() { return orderBy; }
243 const wxString &From() { return from; }
f6bcfd97
BP
244#endif
245 int Insert(void);
246 bool Update(void);
4fdae997
GT
247 bool Update(const wxString &pSqlStmt);
248 bool UpdateWhere(const wxString &pWhereClause);
f6bcfd97 249 bool Delete(void);
4fdae997 250 bool DeleteWhere(const wxString &pWhereClause);
f6bcfd97
BP
251 bool DeleteMatching(void);
252 virtual bool Query(bool forUpdate = FALSE, bool distinct = FALSE);
4fdae997 253 bool QueryBySqlStmt(const wxString &pSqlStmt);
f6bcfd97
BP
254 bool QueryMatching(bool forUpdate = FALSE, bool distinct = FALSE);
255 bool QueryOnKeyFields(bool forUpdate = FALSE, bool distinct = FALSE);
256 bool Refresh(void);
257 bool GetNext(void) { return(getRec(SQL_FETCH_NEXT)); }
258 bool operator++(int) { return(getRec(SQL_FETCH_NEXT)); }
259
260 /***** These four functions only work with wxDb instances that are defined *****
95ca6a20 261 ***** as not being FwdOnlyCursors *****/
f6bcfd97
BP
262 bool GetPrev(void);
263 bool operator--(int);
264 bool GetFirst(void);
265 bool GetLast(void);
266
267 bool IsCursorClosedOnCommit(void);
268 UWORD GetRowNum(void);
269
4fdae997
GT
270 void BuildSelectStmt(wxString &pSqlStmt, int typeOfSelect, bool distinct);
271 void BuildSelectStmt(wxChar *pSqlStmt, int typeOfSelect, bool distinct);
272
273 void BuildDeleteStmt(wxString &pSqlStmt, int typeOfDel, const wxString &pWhereClause="");
274 void BuildDeleteStmt(wxChar *pSqlStmt, int typeOfDel, const wxString &pWhereClause="");
275
276 void BuildUpdateStmt(wxString &pSqlStmt, int typeOfUpd, const wxString &pWhereClause="");
277 void BuildUpdateStmt(wxChar *pSqlStmt, int typeOfUpd, const wxString &pWhereClause="");
278
279 void BuildWhereClause(wxString &pWhereClause, int typeOfWhere, const wxString &qualTableName="", bool useLikeComparison=FALSE);
280 void BuildWhereClause(wxChar *pWhereClause, int typeOfWhere, const wxString &qualTableName="", bool useLikeComparison=FALSE);
281
f6bcfd97
BP
282#if wxODBC_BACKWARD_COMPATABILITY
283// The following member functions are deprecated. You should use the BuildXxxxxStmt functions (above)
284 void GetSelectStmt(char *pSqlStmt, int typeOfSelect, bool distinct)
285 { BuildSelectStmt(pSqlStmt,typeOfSelect,distinct); }
2b63ff5d 286 void GetDeleteStmt(char *pSqlStmt, int typeOfDel, const char *pWhereClause = NULL)
f6bcfd97 287 { BuildDeleteStmt(pSqlStmt,typeOfDel,pWhereClause); }
2b63ff5d 288 void GetUpdateStmt(char *pSqlStmt, int typeOfUpd, const char *pWhereClause = NULL)
f6bcfd97
BP
289 { BuildUpdateStmt(pSqlStmt,typeOfUpd,pWhereClause); }
290 void GetWhereClause(char *pWhereClause, int typeOfWhere,
2b63ff5d 291 const char *qualTableName = NULL, bool useLikeComparison=FALSE)
f6bcfd97
BP
292 { BuildWhereClause(pWhereClause,typeOfWhere,qualTableName,useLikeComparison); }
293#endif
294 bool CanSelectForUpdate(void);
295 bool CanUpdByROWID(void);
f02d4a64
GT
296 void ClearMemberVar(int colNo, bool setToNull=FALSE);
297 void ClearMemberVars(bool setToNull=FALSE);
f6bcfd97
BP
298 bool SetQueryTimeout(UDWORD nSeconds);
299
300 wxDbColDef *GetColDefs() { return colDefs; }
e7e5298c
GT
301 void SetColDefs(int index, const wxString &fieldName, int dataType,
302 void *pData, int cType,
f6bcfd97
BP
303 int size, bool keyField = FALSE, bool upd = TRUE,
304 bool insAllow = TRUE, bool derivedCol = FALSE);
305 wxDbColDataPtr *SetColDefs(wxDbColInf *colInfs, ULONG numCols);
306
307 bool CloseCursor(HSTMT cursor);
308 bool DeleteCursor(HSTMT *hstmtDel);
309 void SetCursor(HSTMT *hstmtActivate = (void **) wxDB_DEFAULT_CURSOR);
310 HSTMT GetCursor(void) { return(hstmt); }
311 HSTMT *GetNewCursor(bool setCursor = FALSE, bool bindColumns = TRUE);
312#if wxODBC_BACKWARD_COMPATABILITY
313// The following member function is deprecated. You should use the GetNewCursor
314 HSTMT *NewCursor(bool setCursor = FALSE, bool bindColumns = TRUE) { return GetNewCursor(setCursor,bindColumns); }
315#endif
316
4fdae997 317 ULONG Count(const wxString &args="*");
f6bcfd97
BP
318 int DB_STATUS(void) { return(pDb->DB_STATUS); }
319
320 bool IsColNull(int colNo);
f02d4a64 321 bool SetColNull(int colNo, bool set=TRUE);
4fdae997 322 bool SetColNull(const wxString &colName, bool set=TRUE);
f02d4a64
GT
323#if wxODBC_BACKWARD_COMPATABILITY
324// The following member functions are deprecated. You should use the SetColNull()
325 bool SetNull(int colNo, bool set=TRUE) { return (SetNull(colNo,set)); }
326 bool SetNull(const char *colName, bool set=TRUE) { return (SetNull(colName,set)); }
327#endif
154f22b3 328#ifdef __WXDEBUG__
f6bcfd97 329 ULONG GetTableID() { return tableID; }
a2115c88 330#endif
108106cf 331
f6bcfd97 332}; // wxDbTable
108106cf 333
4a8fc2c8
GT
334
335// Change this to 0 to remove use of all deprecated functions
f6bcfd97 336#if wxODBC_BACKWARD_COMPATABILITY
4a8fc2c8
GT
337//#################################################################################
338//############### DEPRECATED functions for backward compatability #################
339//#################################################################################
340
341// Backward compability. These will eventually go away
f6bcfd97
BP
342typedef wxDbTable wxTable;
343typedef wxDbIdxDef wxIdxDef;
344typedef wxDbIdxDef CidxDef;
345typedef wxDbColDef wxColDef;
346typedef wxDbColDef CcolDef;
347typedef wxDbColDataPtr wxColDataPtr;
348typedef wxDbColDataPtr CcolDataPtr;
349
350const int ROWID = wxDB_ROWID_LEN;
351const int DEFAULT_CURSOR = wxDB_DEFAULT_CURSOR;
352const bool QUERY_ONLY = wxDB_QUERY_ONLY;
353const bool DISABLE_VIEW = wxDB_DISABLE_VIEW;
4a8fc2c8
GT
354#endif
355
108106cf 356#endif