]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: dbtable.h | |
3 | // Purpose: Declaration of the wxDbTable class. | |
4 | // Author: Doug Card | |
5 | // Modified by: George Tasker | |
6 | // Bart Jourquin | |
7 | // Mark Johnson | |
8 | // Created: 9.96 | |
9 | // RCS-ID: $Id$ | |
10 | // Copyright: (c) 1996 Remstar International, Inc. | |
11 | // Licence: wxWindows licence | |
12 | /////////////////////////////////////////////////////////////////////////////// | |
13 | ||
14 | /* | |
15 | // SYNOPSIS START | |
16 | // SYNOPSIS STOP | |
17 | */ | |
18 | ||
19 | #ifndef DBTABLE_DOT_H | |
20 | #define DBTABLE_DOT_H | |
21 | ||
22 | #include "wx/defs.h" | |
23 | ||
24 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
25 | #pragma interface "dbtable.h" | |
26 | #endif | |
27 | ||
28 | #include "wx/db.h" | |
29 | ||
30 | #include "wx/variant.h" | |
31 | #include "wx/dbkeyg.h" | |
32 | ||
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; | |
35 | const bool wxDB_QUERY_ONLY = true; | |
36 | const bool wxDB_DISABLE_VIEW = true; | |
37 | ||
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 | ||
42 | // The following class is used to define a column of a table. | |
43 | // The wxDbTable constructor will dynamically allocate as many of | |
44 | // these as there are columns in the table. The class derived | |
45 | // from wxDbTable must initialize these column definitions in it's | |
46 | // constructor. These column definitions provide inf. to the | |
47 | // wxDbTable class which allows it to create a table in the data | |
48 | // source, exchange data between the data source and the C++ | |
49 | // object, and so on. | |
50 | class WXDLLIMPEXP_ODBC wxDbColDef | |
51 | { | |
52 | public: | |
53 | wxChar ColName[DB_MAX_COLUMN_NAME_LEN+1]; // Column Name | |
54 | int DbDataType; // Logical Data Type; e.g. DB_DATA_TYPE_INTEGER | |
55 | SWORD SqlCtype; // C data type; e.g. SQL_C_LONG | |
56 | void *PtrDataObj; // Address of the data object | |
57 | int SzDataObj; // Size, in bytes, of the data object | |
58 | bool KeyField; // true if this column is part of the PRIMARY KEY to the table; Date fields should NOT be KeyFields. | |
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 | |
64 | ||
65 | wxDbColDef(); | |
66 | ||
67 | bool Initialize(); | |
68 | }; // wxDbColDef | |
69 | ||
70 | ||
71 | class WXDLLIMPEXP_ODBC wxDbColDataPtr | |
72 | { | |
73 | public: | |
74 | void *PtrDataObj; | |
75 | int SzDataObj; | |
76 | SWORD SqlCtype; | |
77 | }; // wxDbColDataPtr | |
78 | ||
79 | ||
80 | // This structure is used when creating secondary indexes. | |
81 | class WXDLLIMPEXP_ODBC wxDbIdxDef | |
82 | { | |
83 | public: | |
84 | wxChar ColName[DB_MAX_COLUMN_NAME_LEN+1]; | |
85 | bool Ascending; | |
86 | }; // wxDbIdxDef | |
87 | ||
88 | ||
89 | class WXDLLIMPEXP_ODBC wxDbTable | |
90 | { | |
91 | private: | |
92 | ULONG tableID; // Used for debugging. This can help to match up mismatched constructors/destructors | |
93 | ||
94 | // Private member variables | |
95 | UDWORD cursorType; | |
96 | bool insertable; | |
97 | ||
98 | // Private member functions | |
99 | bool initialize(wxDb *pwxDb, const wxString &tblName, const UWORD numColumns, | |
100 | const wxString &qryTblName, bool qryOnly, const wxString &tblPath); | |
101 | void cleanup(); | |
102 | ||
103 | void setCbValueForColumn(int columnIndex); | |
104 | bool bindParams(bool forUpdate); // called by the other 'bind' functions | |
105 | bool bindInsertParams(void); | |
106 | bool bindUpdateParams(void); | |
107 | ||
108 | bool bindCols(HSTMT cursor); | |
109 | bool getRec(UWORD fetchType); | |
110 | bool execDelete(const wxString &pSqlStmt); | |
111 | bool execUpdate(const wxString &pSqlStmt); | |
112 | bool query(int queryType, bool forUpdate, bool distinct, const wxString &pSqlStmt=wxEmptyString); | |
113 | ||
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..." | |
120 | ||
121 | // ODBC Handles | |
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) | |
131 | ||
132 | // Flags | |
133 | bool selectForUpdate; | |
134 | ||
135 | // Pointer to the database object this table belongs to | |
136 | wxDb *pDb; | |
137 | ||
138 | // Table Inf. | |
139 | wxString tablePath; // needed for dBase tables | |
140 | wxString tableName; // Table name | |
141 | wxString queryTableName; // Query Table Name | |
142 | UWORD m_numCols; // # of columns in the table | |
143 | bool queryOnly; // Query Only, no inserts, updates or deletes | |
144 | ||
145 | // Column Definitions | |
146 | wxDbColDef *colDefs; // Array of wxDbColDef structures | |
147 | #endif | |
148 | public: | |
149 | #if wxODBC_BACKWARD_COMPATABILITY | |
150 | // Where, Order By and From clauses | |
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) | |
165 | ||
166 | // Flags | |
167 | bool selectForUpdate; | |
168 | ||
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 | |
176 | UWORD m_numCols; // # of columns in the table | |
177 | bool queryOnly; // Query Only, no inserts, updates or deletes | |
178 | ||
179 | // Column Definitions | |
180 | wxDbColDef *colDefs; // Array of wxDbColDef structures | |
181 | #endif | |
182 | // Public member functions | |
183 | wxDbTable(wxDb *pwxDb, const wxString &tblName, const UWORD numColumns, | |
184 | const wxString &qryTblName=wxEmptyString, bool qryOnly = !wxDB_QUERY_ONLY, | |
185 | const wxString &tblPath=wxEmptyString); | |
186 | ||
187 | #if WXWIN_COMPATIBILITY_2_4 | |
188 | wxDEPRECATED( | |
189 | wxDbTable(wxDb *pwxDb, const wxString &tblName, const UWORD numColumns, | |
190 | const wxChar *qryTblName, bool qryOnly, | |
191 | const wxString &tblPath) | |
192 | ); | |
193 | #endif // WXWIN_COMPATIBILITY_2_4 | |
194 | ||
195 | virtual ~wxDbTable(); | |
196 | ||
197 | bool Open(bool checkPrivileges=false, bool checkTableExists=true); | |
198 | bool CreateTable(bool attemptDrop=true); | |
199 | bool DropTable(void); | |
200 | bool CreateIndex(const wxString &indexName, bool unique, UWORD numIndexColumns, | |
201 | wxDbIdxDef *pIndexDefs, bool attemptDrop=true); | |
202 | bool DropIndex(const wxString &indexName); | |
203 | ||
204 | // Accessors | |
205 | ||
206 | // The member variables returned by these accessors are all | |
207 | // set when the wxDbTable instance is created and cannot be | |
208 | // changed, hence there is no corresponding SetXxxx function | |
209 | wxDb *GetDb() { return pDb; } | |
210 | const wxString &GetTableName() { return tableName; } | |
211 | const wxString &GetQueryTableName() { return queryTableName; } | |
212 | const wxString &GetTablePath() { return tablePath; } | |
213 | ||
214 | UWORD GetNumberOfColumns() { return m_numCols; } // number of "defined" columns for this wxDbTable instance | |
215 | ||
216 | const wxString &GetFromClause() { return from; } | |
217 | const wxString &GetOrderByClause() { return orderBy; } | |
218 | const wxString &GetWhereClause() { return where; } | |
219 | ||
220 | bool IsQueryOnly() { return queryOnly; } | |
221 | #if wxODBC_BACKWARD_COMPATABILITY | |
222 | void SetFromClause(const char *From) { from = (char *)From; } | |
223 | void SetOrderByClause(const char *OrderBy) { orderBy = (char *)OrderBy; } | |
224 | void SetWhereClause(const char *Where) { where = (char *)Where; } | |
225 | #else | |
226 | void SetFromClause(const wxString &From) { from = From; } | |
227 | void SetOrderByClause(const wxString &OrderBy) { orderBy = OrderBy; } | |
228 | bool SetOrderByColNums(UWORD first, ...); | |
229 | void SetWhereClause(const wxString &Where) { where = Where; } | |
230 | void From(const wxString &From) { from = From; } | |
231 | void OrderBy(const wxString &OrderBy) { orderBy = OrderBy; } | |
232 | void Where(const wxString &Where) { where = Where; } | |
233 | const wxString &Where() { return where; } | |
234 | const wxString &OrderBy() { return orderBy; } | |
235 | const wxString &From() { return from; } | |
236 | #endif | |
237 | int Insert(void); | |
238 | bool Update(void); | |
239 | bool Update(const wxString &pSqlStmt); | |
240 | bool UpdateWhere(const wxString &pWhereClause); | |
241 | bool Delete(void); | |
242 | bool DeleteWhere(const wxString &pWhereClause); | |
243 | bool DeleteMatching(void); | |
244 | virtual bool Query(bool forUpdate = false, bool distinct = false); | |
245 | bool QueryBySqlStmt(const wxString &pSqlStmt); | |
246 | bool QueryMatching(bool forUpdate = false, bool distinct = false); | |
247 | bool QueryOnKeyFields(bool forUpdate = false, bool distinct = false); | |
248 | bool Refresh(void); | |
249 | bool GetNext(void) { return(getRec(SQL_FETCH_NEXT)); } | |
250 | bool operator++(int) { return(getRec(SQL_FETCH_NEXT)); } | |
251 | ||
252 | /***** These four functions only work with wxDb instances that are defined ***** | |
253 | ***** as not being FwdOnlyCursors *****/ | |
254 | bool GetPrev(void); | |
255 | bool operator--(int); | |
256 | bool GetFirst(void); | |
257 | bool GetLast(void); | |
258 | ||
259 | bool IsCursorClosedOnCommit(void); | |
260 | UWORD GetRowNum(void); | |
261 | ||
262 | void BuildSelectStmt(wxString &pSqlStmt, int typeOfSelect, bool distinct); | |
263 | void BuildSelectStmt(wxChar *pSqlStmt, int typeOfSelect, bool distinct); | |
264 | ||
265 | void BuildDeleteStmt(wxString &pSqlStmt, int typeOfDel, const wxString &pWhereClause=wxEmptyString); | |
266 | void BuildDeleteStmt(wxChar *pSqlStmt, int typeOfDel, const wxString &pWhereClause=wxEmptyString); | |
267 | ||
268 | void BuildUpdateStmt(wxString &pSqlStmt, int typeOfUpdate, const wxString &pWhereClause=wxEmptyString); | |
269 | void BuildUpdateStmt(wxChar *pSqlStmt, int typeOfUpdate, const wxString &pWhereClause=wxEmptyString); | |
270 | ||
271 | void BuildWhereClause(wxString &pWhereClause, int typeOfWhere, const wxString &qualTableName=wxEmptyString, bool useLikeComparison=false); | |
272 | void BuildWhereClause(wxChar *pWhereClause, int typeOfWhere, const wxString &qualTableName=wxEmptyString, bool useLikeComparison=false); | |
273 | ||
274 | #if wxODBC_BACKWARD_COMPATABILITY | |
275 | // The following member functions are deprecated. You should use the BuildXxxxxStmt functions (above) | |
276 | void GetSelectStmt(char *pSqlStmt, int typeOfSelect, bool distinct) | |
277 | { BuildSelectStmt(pSqlStmt,typeOfSelect,distinct); } | |
278 | void GetDeleteStmt(char *pSqlStmt, int typeOfDel, const char *pWhereClause = NULL) | |
279 | { BuildDeleteStmt(pSqlStmt,typeOfDel,pWhereClause); } | |
280 | void GetUpdateStmt(char *pSqlStmt, int typeOfUpdate, const char *pWhereClause = NULL) | |
281 | { BuildUpdateStmt(pSqlStmt,typeOfUpdate,pWhereClause); } | |
282 | void GetWhereClause(char *pWhereClause, int typeOfWhere, | |
283 | const char *qualTableName = NULL, bool useLikeComparison=false) | |
284 | { BuildWhereClause(pWhereClause,typeOfWhere,qualTableName,useLikeComparison); } | |
285 | #endif | |
286 | bool CanSelectForUpdate(void); | |
287 | #if wxODBC_BACKWARD_COMPATABILITY | |
288 | bool CanUpdByROWID(void) { return CanUpdateByRowID(); }; | |
289 | #endif | |
290 | bool CanUpdateByROWID(void); | |
291 | void ClearMemberVar(UWORD colNumber, bool setToNull=false); | |
292 | void ClearMemberVars(bool setToNull=false); | |
293 | bool SetQueryTimeout(UDWORD nSeconds); | |
294 | ||
295 | wxDbColDef *GetColDefs() { return colDefs; } | |
296 | bool SetColDefs(UWORD index, const wxString &fieldName, int dataType, | |
297 | void *pData, SWORD cType, | |
298 | int size, bool keyField = false, bool updateable = true, | |
299 | bool insertAllowed = true, bool derivedColumn = false); | |
300 | wxDbColDataPtr *SetColDefs(wxDbColInf *colInfs, UWORD numCols); | |
301 | ||
302 | bool CloseCursor(HSTMT cursor); | |
303 | bool DeleteCursor(HSTMT *hstmtDel); | |
304 | void SetCursor(HSTMT *hstmtActivate = (void **) wxDB_DEFAULT_CURSOR); | |
305 | HSTMT GetCursor(void) { return(hstmt); } | |
306 | HSTMT *GetNewCursor(bool setCursor = false, bool bindColumns = true); | |
307 | #if wxODBC_BACKWARD_COMPATABILITY | |
308 | // The following member function is deprecated. You should use the GetNewCursor | |
309 | HSTMT *NewCursor(bool setCursor = false, bool bindColumns = true) { return GetNewCursor(setCursor,bindColumns); } | |
310 | #endif | |
311 | ||
312 | ULONG Count(const wxString &args=_T("*")); | |
313 | int DB_STATUS(void) { return(pDb->DB_STATUS); } | |
314 | ||
315 | bool IsColNull(UWORD colNumber) const; | |
316 | bool SetColNull(UWORD colNumber, bool set=true); | |
317 | bool SetColNull(const wxString &colName, bool set=true); | |
318 | #if wxODBC_BACKWARD_COMPATABILITY | |
319 | // The following member functions are deprecated. You should use the SetColNull() | |
320 | bool SetNull(int colNumber, bool set=true) { return (SetNull(colNumber,set)); } | |
321 | bool SetNull(const char *colName, bool set=true) { return (SetNull(colName,set)); } | |
322 | #endif | |
323 | #ifdef __WXDEBUG__ | |
324 | ULONG GetTableID() { return tableID; } | |
325 | #endif | |
326 | ||
327 | //TODO: Need to Document | |
328 | typedef enum { WX_ROW_MODE_QUERY , WX_ROW_MODE_INDIVIDUAL } rowmode_t; | |
329 | virtual void SetRowMode(const rowmode_t rowmode); | |
330 | #if wxODBC_BACKWARD_COMPATABILITY | |
331 | virtual wxVariant GetCol(const int colNumber) const { return GetColumn(colNumber); }; | |
332 | virtual void SetCol(const int colNumber, const wxVariant value) { return SetColumn(colNumber, value); }; | |
333 | #endif | |
334 | virtual wxVariant GetColumn(const int colNumber) const ; | |
335 | virtual void SetColumn(const int colNumber, const wxVariant value); | |
336 | virtual GenericKey GetKey(void); | |
337 | virtual void SetKey(const GenericKey &key); | |
338 | ||
339 | private: | |
340 | HSTMT *m_hstmtGridQuery; | |
341 | rowmode_t m_rowmode; | |
342 | size_t m_keysize; | |
343 | ||
344 | // typedef enum {unmodified=0, UpdatePending, InsertPending } recStatus; | |
345 | ||
346 | // recStatus get_ModifiedStatus() { return m_recstatus; } | |
347 | ||
348 | // void modify() { | |
349 | // if (m_recstatus==unmodified) | |
350 | // m_recstatus=UpdatePending; | |
351 | // } | |
352 | // protected: | |
353 | // void insertify() {m_recstatus=InsertPending; } | |
354 | // void unmodify() {m_recstatus=unmodified; } | |
355 | // recStatus m_recstatus; | |
356 | //TODO: Need to Document | |
357 | }; // wxDbTable | |
358 | ||
359 | ||
360 | // Change this to 0 to remove use of all deprecated functions | |
361 | #if wxODBC_BACKWARD_COMPATABILITY | |
362 | //################################################################################# | |
363 | //############### DEPRECATED functions for backward compatability ################# | |
364 | //################################################################################# | |
365 | ||
366 | // Backward compability. These will eventually go away | |
367 | typedef wxDbTable wxTable; | |
368 | typedef wxDbIdxDef wxIdxDef; | |
369 | typedef wxDbIdxDef CidxDef; | |
370 | typedef wxDbColDef wxColDef; | |
371 | typedef wxDbColDef CcolDef; | |
372 | typedef wxDbColDataPtr wxColDataPtr; | |
373 | typedef wxDbColDataPtr CcolDataPtr; | |
374 | ||
375 | const int ROWID = wxDB_ROWID_LEN; | |
376 | const int DEFAULT_CURSOR = wxDB_DEFAULT_CURSOR; | |
377 | const bool QUERY_ONLY = wxDB_QUERY_ONLY; | |
378 | const bool DISABLE_VIEW = wxDB_DISABLE_VIEW; | |
379 | #endif | |
380 | ||
381 | #endif |