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