]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dbtable.h
dropped in new wxMatrix (from wxCanvas)
[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 // 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="");
185 virtual ~wxDbTable();
186
187 bool Open(bool checkPrivileges=FALSE);
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 void From(const wxString& From) { from = From; }
220 void OrderBy(const wxString& OrderBy) { orderBy = OrderBy; }
221 void Where(const wxString& Where) { where = Where; }
222 const char * Where() { return (const char *)where.c_str(); }
223 const char * OrderBy() { return (const char *)orderBy.c_str(); }
224 const char * From() { return (const char *)from.c_str(); }
225 #endif
226 int Insert(void);
227 bool Update(void);
228 bool Update(const char *pSqlStmt);
229 bool UpdateWhere(const char *pWhereClause);
230 bool Delete(void);
231 bool DeleteWhere(const char *pWhereClause);
232 bool DeleteMatching(void);
233 virtual bool Query(bool forUpdate = FALSE, bool distinct = FALSE);
234 bool QueryBySqlStmt(const char *pSqlStmt);
235 bool QueryMatching(bool forUpdate = FALSE, bool distinct = FALSE);
236 bool QueryOnKeyFields(bool forUpdate = FALSE, bool distinct = FALSE);
237 bool Refresh(void);
238 bool GetNext(void) { return(getRec(SQL_FETCH_NEXT)); }
239 bool operator++(int) { return(getRec(SQL_FETCH_NEXT)); }
240
241 /***** These four functions only work with wxDb instances that are defined *****
242 ***** as not being FwdOnlyCursors *****/
243 bool GetPrev(void);
244 bool operator--(int);
245 bool GetFirst(void);
246 bool GetLast(void);
247
248 bool IsCursorClosedOnCommit(void);
249 UWORD GetRowNum(void);
250
251 void BuildSelectStmt(char *pSqlStmt, int typeOfSelect, bool distinct);
252 void BuildDeleteStmt(char *pSqlStmt, int typeOfDel, const char *pWhereClause = 0);
253 void BuildUpdateStmt(char *pSqlStmt, int typeOfUpd, const char *pWhereClause = 0);
254 void BuildWhereClause(char *pWhereClause, int typeOfWhere, const char *qualTableName = 0, bool useLikeComparison=FALSE);
255 #if wxODBC_BACKWARD_COMPATABILITY
256 // The following member functions are deprecated. You should use the BuildXxxxxStmt functions (above)
257 void GetSelectStmt(char *pSqlStmt, int typeOfSelect, bool distinct)
258 { BuildSelectStmt(pSqlStmt,typeOfSelect,distinct); }
259 void GetDeleteStmt(char *pSqlStmt, int typeOfDel, const char *pWhereClause = 0)
260 { BuildDeleteStmt(pSqlStmt,typeOfDel,pWhereClause); }
261 void GetUpdateStmt(char *pSqlStmt, int typeOfUpd, const char *pWhereClause = 0)
262 { BuildUpdateStmt(pSqlStmt,typeOfUpd,pWhereClause); }
263 void GetWhereClause(char *pWhereClause, int typeOfWhere,
264 const char *qualTableName = 0, bool useLikeComparison=FALSE)
265 { BuildWhereClause(pWhereClause,typeOfWhere,qualTableName,useLikeComparison); }
266 #endif
267 bool CanSelectForUpdate(void);
268 bool CanUpdByROWID(void);
269 void ClearMemberVar(int colNo, bool setToNull=FALSE);
270 void ClearMemberVars(bool setToNull=FALSE);
271 bool SetQueryTimeout(UDWORD nSeconds);
272
273 wxDbColDef *GetColDefs() { return colDefs; }
274 void SetColDefs(int index, const char *fieldName, int dataType, void *pData, int cType,
275 int size, bool keyField = FALSE, bool upd = TRUE,
276 bool insAllow = TRUE, bool derivedCol = FALSE);
277 wxDbColDataPtr *SetColDefs(wxDbColInf *colInfs, ULONG numCols);
278
279 bool CloseCursor(HSTMT cursor);
280 bool DeleteCursor(HSTMT *hstmtDel);
281 void SetCursor(HSTMT *hstmtActivate = (void **) wxDB_DEFAULT_CURSOR);
282 HSTMT GetCursor(void) { return(hstmt); }
283 HSTMT *GetNewCursor(bool setCursor = FALSE, bool bindColumns = TRUE);
284 #if wxODBC_BACKWARD_COMPATABILITY
285 // The following member function is deprecated. You should use the GetNewCursor
286 HSTMT *NewCursor(bool setCursor = FALSE, bool bindColumns = TRUE) { return GetNewCursor(setCursor,bindColumns); }
287 #endif
288
289 ULONG Count(const char *args="*");
290 int DB_STATUS(void) { return(pDb->DB_STATUS); }
291
292 bool IsColNull(int colNo);
293 bool SetColNull(int colNo, bool set=TRUE);
294 bool SetColNull(const char *colName, bool set=TRUE);
295 #if wxODBC_BACKWARD_COMPATABILITY
296 // The following member functions are deprecated. You should use the SetColNull()
297 bool SetNull(int colNo, bool set=TRUE) { return (SetNull(colNo,set)); }
298 bool SetNull(const char *colName, bool set=TRUE) { return (SetNull(colName,set)); }
299 #endif
300 #ifdef __WXDEBUG__
301 ULONG GetTableID() { return tableID; }
302 #endif
303
304 }; // wxDbTable
305
306
307 // Change this to 0 to remove use of all deprecated functions
308 #if wxODBC_BACKWARD_COMPATABILITY
309 //#################################################################################
310 //############### DEPRECATED functions for backward compatability #################
311 //#################################################################################
312
313 // Backward compability. These will eventually go away
314 typedef wxDbTable wxTable;
315 typedef wxDbIdxDef wxIdxDef;
316 typedef wxDbIdxDef CidxDef;
317 typedef wxDbColDef wxColDef;
318 typedef wxDbColDef CcolDef;
319 typedef wxDbColDataPtr wxColDataPtr;
320 typedef wxDbColDataPtr CcolDataPtr;
321
322 const int ROWID = wxDB_ROWID_LEN;
323 const int DEFAULT_CURSOR = wxDB_DEFAULT_CURSOR;
324 const bool QUERY_ONLY = wxDB_QUERY_ONLY;
325 const bool DISABLE_VIEW = wxDB_DISABLE_VIEW;
326 #endif
327
328 #endif