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