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