]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dbtable.h
::Initialize() methods added to the helper classes
[wxWidgets.git] / include / wx / dbtable.h
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 // 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
57 // The following class is used to define a column of a table.
58 // The wxDbTable constructor will dynamically allocate as many of
59 // these as there are columns in the table. The class derived
60 // from wxDbTable must initialize these column definitions in it's
61 // constructor. These column definitions provide inf. to the
62 // wxDbTable class which allows it to create a table in the data
63 // source, exchange data between the data source and the C++
64 // object, and so on.
65 class WXDLLEXPORT wxDbColDef
66 {
67 public:
68 wxChar ColName[DB_MAX_COLUMN_NAME_LEN+1]; // Column Name
69 int DbDataType; // Logical Data Type; e.g. DB_DATA_TYPE_INTEGER
70 int SqlCtype; // C data type; e.g. SQL_C_LONG
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
79
80 wxDbColDef();
81
82 bool Initialize();
83 }; // wxDbColDef
84
85
86 class WXDLLEXPORT wxDbColDataPtr
87 {
88 public:
89 void *PtrDataObj;
90 int SzDataObj;
91 int SqlCtype;
92 }; // wxDbColDataPtr
93
94
95 // This structure is used when creating secondary indexes.
96 class WXDLLEXPORT wxDbIdxDef
97 {
98 public:
99 wxChar ColName[DB_MAX_COLUMN_NAME_LEN+1];
100 bool Ascending;
101 }; // wxDbIdxDef
102
103
104 class WXDLLEXPORT wxDbTable
105 {
106 private:
107 ULONG tableID; // Used for debugging. This can help to match up mismatched constructors/destructors
108
109 // Private member variables
110 UDWORD cursorType;
111 bool insertable;
112
113 // Private member functions
114 bool initialize(wxDb *pwxDb, const wxString &tblName, const int nCols,
115 const wxString &qryTblName, bool qryOnly, const wxString &tblPath);
116 void cleanup();
117
118 bool bindParams(bool forUpdate); // called by the other 'bind' functions
119 bool bindInsertParams(void);
120 bool bindUpdateParams(void);
121
122 bool bindCols(HSTMT cursor);
123 bool getRec(UWORD fetchType);
124 bool execDelete(const wxString &pSqlStmt);
125 bool execUpdate(const wxString &pSqlStmt);
126 bool query(int queryType, bool forUpdate, bool distinct, const wxString &pSqlStmt=wxEmptyString);
127
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..."
134
135 // ODBC Handles
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)
145
146 // Flags
147 bool selectForUpdate;
148
149 // Pointer to the database object this table belongs to
150 wxDb *pDb;
151
152 // Table Inf.
153 wxString tablePath; // needed for dBase tables
154 wxString tableName; // Table name
155 wxString queryTableName; // Query Table Name
156 int noCols; // # of columns in the table
157 bool queryOnly; // Query Only, no inserts, updates or deletes
158
159 // Column Definitions
160 wxDbColDef *colDefs; // Array of wxDbColDef structures
161 #endif
162 public:
163 #if wxODBC_BACKWARD_COMPATABILITY
164 // Where, Order By and From clauses
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)
179
180 // Flags
181 bool selectForUpdate;
182
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
190 int noCols; // # of columns in the table
191 bool queryOnly; // Query Only, no inserts, updates or deletes
192
193 // Column Definitions
194 wxDbColDef *colDefs; // Array of wxDbColDef structures
195 #endif
196 // Public member functions
197 wxDbTable(wxDb *pwxDb, const wxString &tblName, const int nCols,
198 const wxString &qryTblName="", bool qryOnly = !wxDB_QUERY_ONLY,
199 const wxString &tblPath="");
200
201 // DEPRECATED
202 wxDbTable(wxDb *pwxDb, const wxString &tblName, const int nCols,
203 const wxChar *qryTblName="", bool qryOnly = !wxDB_QUERY_ONLY,
204 const wxString &tblPath="");
205
206 virtual ~wxDbTable();
207
208 bool Open(bool checkPrivileges=FALSE);
209 bool CreateTable(bool attemptDrop=TRUE);
210 bool DropTable(void);
211 bool CreateIndex(const wxString &idxName, bool unique, int noIdxCols,
212 wxDbIdxDef *pIdxDefs, bool attemptDrop=TRUE);
213 bool DropIndex(const wxString &idxName);
214
215 // Accessors
216
217 // The member variables returned by these accessors are all
218 // set when the wxDbTable instance is createand cannot be
219 // changed, hence there is no corresponding SetXxxx function
220 wxDb *GetDb() { return pDb; }
221 const wxString &GetTableName() { return tableName; }
222 const wxString &GetQueryTableName() { return queryTableName; }
223 const wxString &GetTablePath() { return tablePath; }
224
225 int GetNumberOfColumns() { return noCols; } // number of "defined" columns for this wxDbTable instance
226
227
228 const wxString &GetFromClause() { return from; }
229 const wxString &GetOrderByClause() { return orderBy; }
230 const wxString &GetWhereClause() { return where; }
231
232 bool IsQueryOnly() { return queryOnly; }
233 #if wxODBC_BACKWARD_COMPATABILITY
234 void SetFromClause(const char *From) { from = (char *)From; }
235 void SetOrderByClause(const char *OrderBy) { orderBy = (char *)OrderBy; }
236 void SetWhereClause(const char *Where) { where = (char *)Where; }
237 #else
238 void SetFromClause(const wxString &From) { from = From; }
239 void SetOrderByClause(const wxString &OrderBy) { orderBy = OrderBy; }
240 bool SetOrderByColNums(int first, ...);
241 void SetWhereClause(const wxString &Where) { where = Where; }
242 void From(const wxString &From) { from = From; }
243 void OrderBy(const wxString &OrderBy) { orderBy = OrderBy; }
244 void Where(const wxString &Where) { where = Where; }
245 const wxString &Where() { return where; }
246 const wxString &OrderBy() { return orderBy; }
247 const wxString &From() { return from; }
248 #endif
249 int Insert(void);
250 bool Update(void);
251 bool Update(const wxString &pSqlStmt);
252 bool UpdateWhere(const wxString &pWhereClause);
253 bool Delete(void);
254 bool DeleteWhere(const wxString &pWhereClause);
255 bool DeleteMatching(void);
256 virtual bool Query(bool forUpdate = FALSE, bool distinct = FALSE);
257 bool QueryBySqlStmt(const wxString &pSqlStmt);
258 bool QueryMatching(bool forUpdate = FALSE, bool distinct = FALSE);
259 bool QueryOnKeyFields(bool forUpdate = FALSE, bool distinct = FALSE);
260 bool Refresh(void);
261 bool GetNext(void) { return(getRec(SQL_FETCH_NEXT)); }
262 bool operator++(int) { return(getRec(SQL_FETCH_NEXT)); }
263
264 /***** These four functions only work with wxDb instances that are defined *****
265 ***** as not being FwdOnlyCursors *****/
266 bool GetPrev(void);
267 bool operator--(int);
268 bool GetFirst(void);
269 bool GetLast(void);
270
271 bool IsCursorClosedOnCommit(void);
272 UWORD GetRowNum(void);
273
274 void BuildSelectStmt(wxString &pSqlStmt, int typeOfSelect, bool distinct);
275 void BuildSelectStmt(wxChar *pSqlStmt, int typeOfSelect, bool distinct);
276
277 void BuildDeleteStmt(wxString &pSqlStmt, int typeOfDel, const wxString &pWhereClause="");
278 void BuildDeleteStmt(wxChar *pSqlStmt, int typeOfDel, const wxString &pWhereClause="");
279
280 void BuildUpdateStmt(wxString &pSqlStmt, int typeOfUpd, const wxString &pWhereClause="");
281 void BuildUpdateStmt(wxChar *pSqlStmt, int typeOfUpd, const wxString &pWhereClause="");
282
283 void BuildWhereClause(wxString &pWhereClause, int typeOfWhere, const wxString &qualTableName="", bool useLikeComparison=FALSE);
284 void BuildWhereClause(wxChar *pWhereClause, int typeOfWhere, const wxString &qualTableName="", bool useLikeComparison=FALSE);
285
286 #if wxODBC_BACKWARD_COMPATABILITY
287 // The following member functions are deprecated. You should use the BuildXxxxxStmt functions (above)
288 void GetSelectStmt(char *pSqlStmt, int typeOfSelect, bool distinct)
289 { BuildSelectStmt(pSqlStmt,typeOfSelect,distinct); }
290 void GetDeleteStmt(char *pSqlStmt, int typeOfDel, const char *pWhereClause = NULL)
291 { BuildDeleteStmt(pSqlStmt,typeOfDel,pWhereClause); }
292 void GetUpdateStmt(char *pSqlStmt, int typeOfUpd, const char *pWhereClause = NULL)
293 { BuildUpdateStmt(pSqlStmt,typeOfUpd,pWhereClause); }
294 void GetWhereClause(char *pWhereClause, int typeOfWhere,
295 const char *qualTableName = NULL, bool useLikeComparison=FALSE)
296 { BuildWhereClause(pWhereClause,typeOfWhere,qualTableName,useLikeComparison); }
297 #endif
298 bool CanSelectForUpdate(void);
299 bool CanUpdByROWID(void);
300 void ClearMemberVar(int colNo, bool setToNull=FALSE);
301 void ClearMemberVars(bool setToNull=FALSE);
302 bool SetQueryTimeout(UDWORD nSeconds);
303
304 wxDbColDef *GetColDefs() { return colDefs; }
305 void SetColDefs(int index, const wxString &fieldName, int dataType,
306 void *pData, int cType,
307 int size, bool keyField = FALSE, bool upd = TRUE,
308 bool insAllow = TRUE, bool derivedCol = FALSE);
309 wxDbColDataPtr *SetColDefs(wxDbColInf *colInfs, ULONG numCols);
310
311 bool CloseCursor(HSTMT cursor);
312 bool DeleteCursor(HSTMT *hstmtDel);
313 void SetCursor(HSTMT *hstmtActivate = (void **) wxDB_DEFAULT_CURSOR);
314 HSTMT GetCursor(void) { return(hstmt); }
315 HSTMT *GetNewCursor(bool setCursor = FALSE, bool bindColumns = TRUE);
316 #if wxODBC_BACKWARD_COMPATABILITY
317 // The following member function is deprecated. You should use the GetNewCursor
318 HSTMT *NewCursor(bool setCursor = FALSE, bool bindColumns = TRUE) { return GetNewCursor(setCursor,bindColumns); }
319 #endif
320
321 ULONG Count(const wxString &args="*");
322 int DB_STATUS(void) { return(pDb->DB_STATUS); }
323
324 bool IsColNull(int colNo);
325 bool SetColNull(int colNo, bool set=TRUE);
326 bool SetColNull(const wxString &colName, bool set=TRUE);
327 #if wxODBC_BACKWARD_COMPATABILITY
328 // The following member functions are deprecated. You should use the SetColNull()
329 bool SetNull(int colNo, bool set=TRUE) { return (SetNull(colNo,set)); }
330 bool SetNull(const char *colName, bool set=TRUE) { return (SetNull(colName,set)); }
331 #endif
332 #ifdef __WXDEBUG__
333 ULONG GetTableID() { return tableID; }
334 #endif
335
336 }; // wxDbTable
337
338
339 // Change this to 0 to remove use of all deprecated functions
340 #if wxODBC_BACKWARD_COMPATABILITY
341 //#################################################################################
342 //############### DEPRECATED functions for backward compatability #################
343 //#################################################################################
344
345 // Backward compability. These will eventually go away
346 typedef wxDbTable wxTable;
347 typedef wxDbIdxDef wxIdxDef;
348 typedef wxDbIdxDef CidxDef;
349 typedef wxDbColDef wxColDef;
350 typedef wxDbColDef CcolDef;
351 typedef wxDbColDataPtr wxColDataPtr;
352 typedef wxDbColDataPtr CcolDataPtr;
353
354 const int ROWID = wxDB_ROWID_LEN;
355 const int DEFAULT_CURSOR = wxDB_DEFAULT_CURSOR;
356 const bool QUERY_ONLY = wxDB_QUERY_ONLY;
357 const bool DISABLE_VIEW = wxDB_DISABLE_VIEW;
358 #endif
359
360 #endif