]>
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, plus: | |
12 | // Notice: This class library and its intellectual design are free of charge for use, | |
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 | ||
28 | #ifndef DBTABLE_DOT_H | |
29 | #define DBTABLE_DOT_H | |
30 | ||
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 | |
37 | #ifdef __GNUG__ | |
38 | #pragma interface "dbtable.h" | |
39 | #endif | |
40 | #endif | |
41 | ||
42 | #if wxMAJOR_VERSION == 2 | |
43 | #include "wx/db.h" | |
44 | #else | |
45 | #include "db.h" | |
46 | #endif | |
47 | ||
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; | |
52 | ||
53 | // The following class is used to define a column of a table. | |
54 | // The wxDbTable constructor will dynamically allocate as many of | |
55 | // these as there are columns in the table. The class derived | |
56 | // from wxDbTable must initialize these column definitions in it's | |
57 | // constructor. These column definitions provide inf. to the | |
58 | // wxDbTable class which allows it to create a table in the data | |
59 | // source, exchange data between the data source and the C++ | |
60 | // object, and so on. | |
61 | class WXDLLEXPORT wxDbColDef | |
62 | { | |
63 | public: | |
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 | |
75 | }; // wxDbColDef | |
76 | ||
77 | ||
78 | class WXDLLEXPORT wxDbColDataPtr | |
79 | { | |
80 | public: | |
81 | void *PtrDataObj; | |
82 | int SzDataObj; | |
83 | int SqlCtype; | |
84 | }; // wxDbColDataPtr | |
85 | ||
86 | ||
87 | // This structure is used when creating secondary indexes. | |
88 | class WXDLLEXPORT wxDbIdxDef | |
89 | { | |
90 | public: | |
91 | char ColName[DB_MAX_COLUMN_NAME_LEN+1]; | |
92 | bool Ascending; | |
93 | }; // wxDbIdxDef | |
94 | ||
95 | ||
96 | class WXDLLEXPORT wxDbTable | |
97 | { | |
98 | private: | |
99 | ULONG tableID; // Used for debugging. This can help to match up mismatched constructors/destructors | |
100 | ||
101 | // Private member variables | |
102 | UDWORD cursorType; | |
103 | bool insertable; | |
104 | ||
105 | // Private member functions | |
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); | |
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 | 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 | |
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 | 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 | |
182 | // Public member functions | |
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 | ||
193 | // Accessors | |
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 | |
198 | wxDb *GetDb() { return pDb; } | |
199 | const char *GetTableName() { return tableName; } | |
200 | const char *GetQueryTableName() { return queryTableName; } | |
201 | const char *GetTablePath() { return tablePath; } | |
202 | ||
203 | int GetNumberOfColumns() { return noCols; } // number of "defined" columns for this wxDbTable instance | |
204 | ||
205 | ||
206 | const char *GetFromClause() { return from; } | |
207 | const char *GetOrderByClause() { return orderBy; } | |
208 | const char *GetWhereClause() { return where; } | |
209 | ||
210 | bool IsQueryOnly() { return queryOnly; } | |
211 | #if wxODBC_BACKWARD_COMPATABILITY | |
212 | void SetFromClause(const char *From) { from = (char *)From; } | |
213 | void SetOrderByClause(const char *OrderBy) { orderBy = (char *)OrderBy; } | |
214 | void SetWhereClause(const char *Where) { where = (char *)Where; } | |
215 | #else | |
216 | void SetFromClause(const wxString& From) { from = From; } | |
217 | void SetOrderByClause(const wxString& OrderBy) { orderBy = OrderBy; } | |
218 | void SetWhereClause(const wxString& Where) { where = Where; } | |
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 ***** | |
236 | ***** as not being FwdOnlyCursors *****/ | |
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); | |
288 | ||
289 | #ifdef __WXDEBUG__ | |
290 | ULONG GetTableID() { return tableID; } | |
291 | #endif | |
292 | ||
293 | }; // wxDbTable | |
294 | ||
295 | ||
296 | // Change this to 0 to remove use of all deprecated functions | |
297 | #if wxODBC_BACKWARD_COMPATABILITY | |
298 | //################################################################################# | |
299 | //############### DEPRECATED functions for backward compatability ################# | |
300 | //################################################################################# | |
301 | ||
302 | // Backward compability. These will eventually go away | |
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; | |
315 | #endif | |
316 | ||
317 | #endif |