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