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