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