]>
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: | |
95ca6a20 | 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 JS |
52 | |
53 | // The following class is used to define a column of a table. | |
f6bcfd97 | 54 | // The wxDbTable constructor will dynamically allocate as many of |
108106cf | 55 | // these as there are columns in the table. The class derived |
f6bcfd97 | 56 | // from wxDbTable must initialize these column definitions in it's |
108106cf | 57 | // constructor. These column definitions provide inf. to the |
f6bcfd97 | 58 | // wxDbTable class which allows it to create a table in the data |
108106cf JS |
59 | // source, exchange data between the data source and the C++ |
60 | // object, and so on. | |
f6bcfd97 | 61 | class WXDLLEXPORT wxDbColDef |
108106cf JS |
62 | { |
63 | public: | |
95ca6a20 GT |
64 | char ColName[DB_MAX_COLUMN_NAME_LEN+1]; // Column Name |
65 | int DbDataType; // Logical Data Type; e.g. DB_DATA_TYPE_INTEGER | |
66 | int SqlCtype; // C data type; e.g. SQL_C_LONG | |
67 | void *PtrDataObj; // Address of the data object | |
68 | int SzDataObj; // Size, in bytes, of the data object | |
69 | bool KeyField; // TRUE if this column is part of the PRIMARY KEY to the table; Date fields should NOT be KeyFields. | |
70 | bool Updateable; // Specifies whether this column is updateable | |
71 | bool InsertAllowed; // Specifies whether this column should be included in an INSERT statement | |
72 | bool DerivedCol; // Specifies whether this column is a derived value | |
73 | SDWORD CbValue; // Internal use only!!! | |
74 | bool Null; // NOT FULLY IMPLEMENTED - Allows NULL values in Inserts and Updates | |
f6bcfd97 | 75 | }; // wxDbColDef |
30a6d2d2 | 76 | |
95ca6a20 | 77 | |
f6bcfd97 | 78 | class WXDLLEXPORT wxDbColDataPtr |
30a6d2d2 GT |
79 | { |
80 | public: | |
95ca6a20 GT |
81 | void *PtrDataObj; |
82 | int SzDataObj; | |
83 | int SqlCtype; | |
f6bcfd97 | 84 | }; // wxDbColDataPtr |
30a6d2d2 GT |
85 | |
86 | ||
108106cf | 87 | // This structure is used when creating secondary indexes. |
f6bcfd97 | 88 | class WXDLLEXPORT wxDbIdxDef |
108106cf JS |
89 | { |
90 | public: | |
95ca6a20 GT |
91 | char ColName[DB_MAX_COLUMN_NAME_LEN+1]; |
92 | bool Ascending; | |
f6bcfd97 | 93 | }; // wxDbIdxDef |
108106cf | 94 | |
95ca6a20 | 95 | |
f6bcfd97 | 96 | class WXDLLEXPORT wxDbTable |
108106cf JS |
97 | { |
98 | private: | |
3ca6a5f0 | 99 | ULONG tableID; // Used for debugging. This can help to match up mismatched constructors/destructors |
108106cf | 100 | |
95ca6a20 | 101 | // Private member variables |
3ca6a5f0 BP |
102 | UDWORD cursorType; |
103 | bool insertable; | |
a2115c88 | 104 | |
95ca6a20 | 105 | // Private member functions |
3ca6a5f0 BP |
106 | bool bindInsertParams(void); |
107 | bool bindUpdateParams(void); | |
108 | bool bindCols(HSTMT cursor); | |
109 | bool getRec(UWORD fetchType); | |
110 | bool execDelete(const char *pSqlStmt); | |
111 | bool execUpdate(const char *pSqlStmt); | |
112 | bool query(int queryType, bool forUpdate, bool distinct, const char *pSqlStmt = 0); | |
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 BP |
138 | // Table Inf. |
139 | char tablePath[wxDB_PATH_MAX]; // needed for dBase tables | |
140 | char tableName[DB_MAX_TABLE_NAME_LEN+1]; // Table name | |
141 | char queryTableName[DB_MAX_TABLE_NAME_LEN+1]; // Query Table Name | |
142 | int noCols; // # of columns in the table | |
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 | |
176 | int noCols; // # 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 | |
95ca6a20 | 182 | // Public member functions |
f6bcfd97 BP |
183 | wxDbTable(wxDb *pwxDb, const char *tblName, const int nCols, |
184 | const char *qryTblName = 0, bool qryOnly = !wxDB_QUERY_ONLY, const char *tblPath=NULL); | |
185 | virtual ~wxDbTable(); | |
186 | ||
187 | bool Open(void); | |
188 | bool CreateTable(bool attemptDrop=TRUE); | |
189 | bool DropTable(void); | |
190 | bool CreateIndex(const char * idxName, bool unique, int noIdxCols, wxDbIdxDef *pIdxDefs, bool attemptDrop=TRUE); | |
191 | bool DropIndex(const char * idxName); | |
192 | ||
3ca6a5f0 | 193 | // Accessors |
f6bcfd97 BP |
194 | |
195 | // The member variables returned by these accessors are all | |
196 | // set when the wxDbTable instance is createand cannot be | |
197 | // changed, hence there is no corresponding SetXxxx function | |
3ca6a5f0 BP |
198 | wxDb *GetDb() { return pDb; } |
199 | const char *GetTableName() { return tableName; } | |
200 | const char *GetQueryTableName() { return queryTableName; } | |
201 | const char *GetTablePath() { return tablePath; } | |
f6bcfd97 | 202 | |
3ca6a5f0 | 203 | int GetNumberOfColumns() { return noCols; } // number of "defined" columns for this wxDbTable instance |
f6bcfd97 BP |
204 | |
205 | ||
3ca6a5f0 BP |
206 | const char *GetFromClause() { return from; } |
207 | const char *GetOrderByClause() { return orderBy; } | |
208 | const char *GetWhereClause() { return where; } | |
f6bcfd97 | 209 | |
3ca6a5f0 | 210 | bool IsQueryOnly() { return queryOnly; } |
f6bcfd97 BP |
211 | #if wxODBC_BACKWARD_COMPATABILITY |
212 | void SetFromClause(const char *From) { from = (char *)From; } | |
3ca6a5f0 BP |
213 | void SetOrderByClause(const char *OrderBy) { orderBy = (char *)OrderBy; } |
214 | void SetWhereClause(const char *Where) { where = (char *)Where; } | |
f6bcfd97 | 215 | #else |
3ca6a5f0 BP |
216 | void SetFromClause(const wxString& From) { from = From; } |
217 | void SetOrderByClause(const wxString& OrderBy) { orderBy = OrderBy; } | |
218 | void SetWhereClause(const wxString& Where) { where = Where; } | |
f6bcfd97 BP |
219 | #endif |
220 | int Insert(void); | |
221 | bool Update(void); | |
222 | bool Update(const char *pSqlStmt); | |
223 | bool UpdateWhere(const char *pWhereClause); | |
224 | bool Delete(void); | |
225 | bool DeleteWhere(const char *pWhereClause); | |
226 | bool DeleteMatching(void); | |
227 | virtual bool Query(bool forUpdate = FALSE, bool distinct = FALSE); | |
228 | bool QueryBySqlStmt(const char *pSqlStmt); | |
229 | bool QueryMatching(bool forUpdate = FALSE, bool distinct = FALSE); | |
230 | bool QueryOnKeyFields(bool forUpdate = FALSE, bool distinct = FALSE); | |
231 | bool Refresh(void); | |
232 | bool GetNext(void) { return(getRec(SQL_FETCH_NEXT)); } | |
233 | bool operator++(int) { return(getRec(SQL_FETCH_NEXT)); } | |
234 | ||
235 | /***** These four functions only work with wxDb instances that are defined ***** | |
95ca6a20 | 236 | ***** as not being FwdOnlyCursors *****/ |
f6bcfd97 BP |
237 | bool GetPrev(void); |
238 | bool operator--(int); | |
239 | bool GetFirst(void); | |
240 | bool GetLast(void); | |
241 | ||
242 | bool IsCursorClosedOnCommit(void); | |
243 | UWORD GetRowNum(void); | |
244 | ||
245 | void BuildSelectStmt(char *pSqlStmt, int typeOfSelect, bool distinct); | |
246 | void BuildDeleteStmt(char *pSqlStmt, int typeOfDel, const char *pWhereClause = 0); | |
247 | void BuildUpdateStmt(char *pSqlStmt, int typeOfUpd, const char *pWhereClause = 0); | |
248 | void BuildWhereClause(char *pWhereClause, int typeOfWhere, const char *qualTableName = 0, bool useLikeComparison=FALSE); | |
249 | #if wxODBC_BACKWARD_COMPATABILITY | |
250 | // The following member functions are deprecated. You should use the BuildXxxxxStmt functions (above) | |
251 | void GetSelectStmt(char *pSqlStmt, int typeOfSelect, bool distinct) | |
252 | { BuildSelectStmt(pSqlStmt,typeOfSelect,distinct); } | |
253 | void GetDeleteStmt(char *pSqlStmt, int typeOfDel, const char *pWhereClause = 0) | |
254 | { BuildDeleteStmt(pSqlStmt,typeOfDel,pWhereClause); } | |
255 | void GetUpdateStmt(char *pSqlStmt, int typeOfUpd, const char *pWhereClause = 0) | |
256 | { BuildUpdateStmt(pSqlStmt,typeOfUpd,pWhereClause); } | |
257 | void GetWhereClause(char *pWhereClause, int typeOfWhere, | |
258 | const char *qualTableName = 0, bool useLikeComparison=FALSE) | |
259 | { BuildWhereClause(pWhereClause,typeOfWhere,qualTableName,useLikeComparison); } | |
260 | #endif | |
261 | bool CanSelectForUpdate(void); | |
262 | bool CanUpdByROWID(void); | |
263 | void ClearMemberVars(void); | |
264 | bool SetQueryTimeout(UDWORD nSeconds); | |
265 | ||
266 | wxDbColDef *GetColDefs() { return colDefs; } | |
267 | void SetColDefs(int index, const char *fieldName, int dataType, void *pData, int cType, | |
268 | int size, bool keyField = FALSE, bool upd = TRUE, | |
269 | bool insAllow = TRUE, bool derivedCol = FALSE); | |
270 | wxDbColDataPtr *SetColDefs(wxDbColInf *colInfs, ULONG numCols); | |
271 | ||
272 | bool CloseCursor(HSTMT cursor); | |
273 | bool DeleteCursor(HSTMT *hstmtDel); | |
274 | void SetCursor(HSTMT *hstmtActivate = (void **) wxDB_DEFAULT_CURSOR); | |
275 | HSTMT GetCursor(void) { return(hstmt); } | |
276 | HSTMT *GetNewCursor(bool setCursor = FALSE, bool bindColumns = TRUE); | |
277 | #if wxODBC_BACKWARD_COMPATABILITY | |
278 | // The following member function is deprecated. You should use the GetNewCursor | |
279 | HSTMT *NewCursor(bool setCursor = FALSE, bool bindColumns = TRUE) { return GetNewCursor(setCursor,bindColumns); } | |
280 | #endif | |
281 | ||
282 | ULONG Count(const char *args="*"); | |
283 | int DB_STATUS(void) { return(pDb->DB_STATUS); } | |
284 | ||
285 | bool IsColNull(int colNo); | |
286 | bool SetNull(int colNo); | |
287 | bool SetNull(const char *colName); | |
a2115c88 | 288 | |
154f22b3 | 289 | #ifdef __WXDEBUG__ |
f6bcfd97 | 290 | ULONG GetTableID() { return tableID; } |
a2115c88 | 291 | #endif |
108106cf | 292 | |
f6bcfd97 | 293 | }; // wxDbTable |
108106cf | 294 | |
4a8fc2c8 GT |
295 | |
296 | // Change this to 0 to remove use of all deprecated functions | |
f6bcfd97 | 297 | #if wxODBC_BACKWARD_COMPATABILITY |
4a8fc2c8 GT |
298 | //################################################################################# |
299 | //############### DEPRECATED functions for backward compatability ################# | |
300 | //################################################################################# | |
301 | ||
302 | // Backward compability. These will eventually go away | |
f6bcfd97 BP |
303 | typedef wxDbTable wxTable; |
304 | typedef wxDbIdxDef wxIdxDef; | |
305 | typedef wxDbIdxDef CidxDef; | |
306 | typedef wxDbColDef wxColDef; | |
307 | typedef wxDbColDef CcolDef; | |
308 | typedef wxDbColDataPtr wxColDataPtr; | |
309 | typedef wxDbColDataPtr CcolDataPtr; | |
310 | ||
311 | const int ROWID = wxDB_ROWID_LEN; | |
312 | const int DEFAULT_CURSOR = wxDB_DEFAULT_CURSOR; | |
313 | const bool QUERY_ONLY = wxDB_QUERY_ONLY; | |
314 | const bool DISABLE_VIEW = wxDB_DISABLE_VIEW; | |
4a8fc2c8 GT |
315 | #endif |
316 | ||
108106cf | 317 | #endif |