]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dbtable.h
8d7de7322cbbd5e3ee139ff72fc4ebf7d3afb327
[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
12 ///////////////////////////////////////////////////////////////////////////////
13
14 /*
15 // SYNOPSIS START
16 // SYNOPSIS STOP
17 */
18
19 #ifndef DBTABLE_DOT_H
20 #define DBTABLE_DOT_H
21
22 #include "wx/defs.h"
23
24 #include "wx/db.h"
25
26 #include "wx/variant.h"
27 #include "wx/dbkeyg.h"
28
29 const int wxDB_ROWID_LEN = 24; // 18 is the max, 24 is in case it gets larger
30 const int wxDB_DEFAULT_CURSOR = 0;
31 const bool wxDB_QUERY_ONLY = true;
32 const bool wxDB_DISABLE_VIEW = true;
33
34 // Used to indicate end of a variable length list of
35 // column numbers passed to member functions
36 const int wxDB_NO_MORE_COLUMN_NUMBERS = -1;
37
38 // The following class is used to define a column of a table.
39 // The wxDbTable constructor will dynamically allocate as many of
40 // these as there are columns in the table. The class derived
41 // from wxDbTable must initialize these column definitions in it's
42 // constructor. These column definitions provide inf. to the
43 // wxDbTable class which allows it to create a table in the data
44 // source, exchange data between the data source and the C++
45 // object, and so on.
46 class WXDLLIMPEXP_ODBC wxDbColDef
47 {
48 public:
49 wxChar ColName[DB_MAX_COLUMN_NAME_LEN+1]; // Column Name
50 int DbDataType; // Logical Data Type; e.g. DB_DATA_TYPE_INTEGER
51 SWORD SqlCtype; // C data type; e.g. SQL_C_LONG
52 void *PtrDataObj; // Address of the data object
53 int SzDataObj; // Size, in bytes, of the data object
54 bool KeyField; // true if this column is part of the PRIMARY KEY to the table; Date fields should NOT be KeyFields.
55 bool Updateable; // Specifies whether this column is updateable
56 bool InsertAllowed; // Specifies whether this column should be included in an INSERT statement
57 bool DerivedCol; // Specifies whether this column is a derived value
58 SQLLEN CbValue; // Internal use only!!! For parameter bindings
59 bool Null; // NOT FULLY IMPLEMENTED - Allows NULL values in Inserts and Updates
60 SQLLEN CbValueCol; // Internal use only!!! For column bindings
61
62 wxDbColDef();
63
64 bool Initialize();
65 }; // wxDbColDef
66
67
68 class WXDLLIMPEXP_ODBC wxDbColDataPtr
69 {
70 public:
71 void *PtrDataObj;
72 int SzDataObj;
73 SWORD SqlCtype;
74 }; // wxDbColDataPtr
75
76
77 // This structure is used when creating secondary indexes.
78 class WXDLLIMPEXP_ODBC wxDbIdxDef
79 {
80 public:
81 wxChar ColName[DB_MAX_COLUMN_NAME_LEN+1];
82 bool Ascending;
83 }; // wxDbIdxDef
84
85
86 class WXDLLIMPEXP_ODBC wxDbTable
87 {
88 private:
89 ULONG tableID; // Used for debugging. This can help to match up mismatched constructors/destructors
90
91 // Private member variables
92 UDWORD cursorType;
93 bool insertable;
94
95 // Private member functions
96 bool initialize(wxDb *pwxDb, const wxString &tblName, const UWORD numColumns,
97 const wxString &qryTblName, bool qryOnly, const wxString &tblPath);
98 void cleanup();
99
100 void setCbValueForColumn(int columnIndex);
101 bool bindParams(bool forUpdate); // called by the other 'bind' functions
102 bool bindInsertParams(void);
103 bool bindUpdateParams(void);
104
105 bool bindCols(HSTMT cursor);
106 bool getRec(UWORD fetchType);
107 bool execDelete(const wxString &pSqlStmt);
108 bool execUpdate(const wxString &pSqlStmt);
109 bool query(int queryType, bool forUpdate, bool distinct, const wxString &pSqlStmt=wxEmptyString);
110
111 #if !wxODBC_BACKWARD_COMPATABILITY
112 // these were public
113 // Where, Order By and From clauses
114 wxString where; // Standard SQL where clause, minus the word WHERE
115 wxString orderBy; // Standard SQL order by clause, minus the ORDER BY
116 wxString from; // Allows for joins in a wxDbTable::Query(). Format: ",tbl,tbl..."
117
118 // ODBC Handles
119 HENV henv; // ODBC Environment handle
120 HDBC hdbc; // ODBC DB Connection handle
121 HSTMT hstmt; // ODBC Statement handle
122 HSTMT *hstmtDefault; // Default cursor
123 HSTMT hstmtInsert; // ODBC Statement handle used specifically for inserts
124 HSTMT hstmtDelete; // ODBC Statement handle used specifically for deletes
125 HSTMT hstmtUpdate; // ODBC Statement handle used specifically for updates
126 HSTMT hstmtInternal; // ODBC Statement handle used internally only
127 HSTMT *hstmtCount; // ODBC Statement handle used by Count() function (No binding of columns)
128
129 // Flags
130 bool selectForUpdate;
131
132 // Pointer to the database object this table belongs to
133 wxDb *pDb;
134
135 // Table Inf.
136 wxString tablePath; // needed for dBase tables
137 wxString tableName; // Table name
138 wxString queryTableName; // Query Table Name
139 UWORD m_numCols; // # of columns in the table
140 bool queryOnly; // Query Only, no inserts, updates or deletes
141
142 // Column Definitions
143 wxDbColDef *colDefs; // Array of wxDbColDef structures
144 #endif
145 public:
146 #if wxODBC_BACKWARD_COMPATABILITY
147 // Where, Order By and From clauses
148 char *where; // Standard SQL where clause, minus the word WHERE
149 char *orderBy; // Standard SQL order by clause, minus the ORDER BY
150 char *from; // Allows for joins in a wxDbTable::Query(). Format: ",tbl,tbl..."
151
152 // ODBC Handles
153 HENV henv; // ODBC Environment handle
154 HDBC hdbc; // ODBC DB Connection handle
155 HSTMT hstmt; // ODBC Statement handle
156 HSTMT *hstmtDefault; // Default cursor
157 HSTMT hstmtInsert; // ODBC Statement handle used specifically for inserts
158 HSTMT hstmtDelete; // ODBC Statement handle used specifically for deletes
159 HSTMT hstmtUpdate; // ODBC Statement handle used specifically for updates
160 HSTMT hstmtInternal; // ODBC Statement handle used internally only
161 HSTMT *hstmtCount; // ODBC Statement handle used by Count() function (No binding of columns)
162
163 // Flags
164 bool selectForUpdate;
165
166 // Pointer to the database object this table belongs to
167 wxDb *pDb;
168
169 // Table Inf.
170 char tablePath[wxDB_PATH_MAX]; // needed for dBase tables
171 char tableName[DB_MAX_TABLE_NAME_LEN+1]; // Table name
172 char queryTableName[DB_MAX_TABLE_NAME_LEN+1]; // Query Table Name
173 UWORD m_numCols; // # of columns in the table
174 bool queryOnly; // Query Only, no inserts, updates or deletes
175
176 // Column Definitions
177 wxDbColDef *colDefs; // Array of wxDbColDef structures
178 #endif
179 // Public member functions
180 wxDbTable(wxDb *pwxDb, const wxString &tblName, const UWORD numColumns,
181 const wxString &qryTblName=wxEmptyString, bool qryOnly = !wxDB_QUERY_ONLY,
182 const wxString &tblPath=wxEmptyString);
183
184 virtual ~wxDbTable();
185
186 bool Open(bool checkPrivileges=false, bool checkTableExists=true);
187 bool CreateTable(bool attemptDrop=true);
188 bool DropTable(void);
189 bool CreateIndex(const wxString &indexName, bool unique, UWORD numIndexColumns,
190 wxDbIdxDef *pIndexDefs, bool attemptDrop=true);
191 bool DropIndex(const wxString &indexName);
192
193 // Accessors
194
195 // The member variables returned by these accessors are all
196 // set when the wxDbTable instance is created and cannot be
197 // changed, hence there is no corresponding SetXxxx function
198 wxDb *GetDb() { return pDb; }
199 const wxString &GetTableName() { return tableName; }
200 const wxString &GetQueryTableName() { return queryTableName; }
201 const wxString &GetTablePath() { return tablePath; }
202
203 UWORD GetNumberOfColumns() { return m_numCols; } // number of "defined" columns for this wxDbTable instance
204
205 const wxString &GetFromClause() { return from; }
206 const wxString &GetOrderByClause() { return orderBy; }
207 const wxString &GetWhereClause() { return where; }
208
209 bool IsQueryOnly() { return queryOnly; }
210 #if wxODBC_BACKWARD_COMPATABILITY
211 void SetFromClause(const char *From) { from = (char *)From; }
212 void SetOrderByClause(const char *OrderBy) { orderBy = (char *)OrderBy; }
213 void SetWhereClause(const char *Where) { where = (char *)Where; }
214 #else
215 void SetFromClause(const wxString &From) { from = From; }
216 void SetOrderByClause(const wxString &OrderBy) { orderBy = OrderBy; }
217 bool SetOrderByColNums(UWORD first, ...);
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 wxString &Where() { return where; }
223 const wxString &OrderBy() { return orderBy; }
224 const wxString &From() { return from; }
225 #endif
226 int Insert(void);
227 bool Update(void);
228 bool Update(const wxString &pSqlStmt);
229 bool UpdateWhere(const wxString &pWhereClause);
230 bool Delete(void);
231 bool DeleteWhere(const wxString &pWhereClause);
232 bool DeleteMatching(void);
233 virtual bool Query(bool forUpdate = false, bool distinct = false);
234 bool QueryBySqlStmt(const wxString &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(wxString &pSqlStmt, int typeOfSelect, bool distinct);
252 void BuildSelectStmt(wxChar *pSqlStmt, int typeOfSelect, bool distinct);
253
254 void BuildDeleteStmt(wxString &pSqlStmt, int typeOfDel, const wxString &pWhereClause=wxEmptyString);
255 void BuildDeleteStmt(wxChar *pSqlStmt, int typeOfDel, const wxString &pWhereClause=wxEmptyString);
256
257 void BuildUpdateStmt(wxString &pSqlStmt, int typeOfUpdate, const wxString &pWhereClause=wxEmptyString);
258 void BuildUpdateStmt(wxChar *pSqlStmt, int typeOfUpdate, const wxString &pWhereClause=wxEmptyString);
259
260 void BuildWhereClause(wxString &pWhereClause, int typeOfWhere, const wxString &qualTableName=wxEmptyString, bool useLikeComparison=false);
261 void BuildWhereClause(wxChar *pWhereClause, int typeOfWhere, const wxString &qualTableName=wxEmptyString, bool useLikeComparison=false);
262
263 #if wxODBC_BACKWARD_COMPATABILITY
264 // The following member functions are deprecated. You should use the BuildXxxxxStmt functions (above)
265 void GetSelectStmt(char *pSqlStmt, int typeOfSelect, bool distinct)
266 { BuildSelectStmt(pSqlStmt,typeOfSelect,distinct); }
267 void GetDeleteStmt(char *pSqlStmt, int typeOfDel, const char *pWhereClause = NULL)
268 { BuildDeleteStmt(pSqlStmt,typeOfDel,pWhereClause); }
269 void GetUpdateStmt(char *pSqlStmt, int typeOfUpdate, const char *pWhereClause = NULL)
270 { BuildUpdateStmt(pSqlStmt,typeOfUpdate,pWhereClause); }
271 void GetWhereClause(char *pWhereClause, int typeOfWhere,
272 const char *qualTableName = NULL, bool useLikeComparison=false)
273 { BuildWhereClause(pWhereClause,typeOfWhere,qualTableName,useLikeComparison); }
274 #endif
275 bool CanSelectForUpdate(void);
276 #if wxODBC_BACKWARD_COMPATABILITY
277 bool CanUpdByROWID(void) { return CanUpdateByRowID(); };
278 #endif
279 bool CanUpdateByROWID(void);
280 void ClearMemberVar(UWORD colNumber, bool setToNull=false);
281 void ClearMemberVars(bool setToNull=false);
282 bool SetQueryTimeout(UDWORD nSeconds);
283
284 wxDbColDef *GetColDefs() { return colDefs; }
285 bool SetColDefs(UWORD index, const wxString &fieldName, int dataType,
286 void *pData, SWORD cType,
287 int size, bool keyField = false, bool updateable = true,
288 bool insertAllowed = true, bool derivedColumn = false);
289 wxDbColDataPtr *SetColDefs(wxDbColInf *colInfs, UWORD numCols);
290
291 bool CloseCursor(HSTMT cursor);
292 bool DeleteCursor(HSTMT *hstmtDel);
293 void SetCursor(HSTMT *hstmtActivate = (void **) wxDB_DEFAULT_CURSOR);
294 HSTMT GetCursor(void) { return(hstmt); }
295 HSTMT *GetNewCursor(bool setCursor = false, bool bindColumns = true);
296 #if wxODBC_BACKWARD_COMPATABILITY
297 // The following member function is deprecated. You should use the GetNewCursor
298 HSTMT *NewCursor(bool setCursor = false, bool bindColumns = true) { return GetNewCursor(setCursor,bindColumns); }
299 #endif
300
301 ULONG Count(const wxString &args=_T("*"));
302 int DB_STATUS(void) { return(pDb->DB_STATUS); }
303
304 bool IsColNull(UWORD colNumber) const;
305 bool SetColNull(UWORD colNumber, bool set=true);
306 bool SetColNull(const wxString &colName, bool set=true);
307 #if wxODBC_BACKWARD_COMPATABILITY
308 // The following member functions are deprecated. You should use the SetColNull()
309 bool SetNull(int colNumber, bool set=true) { return (SetNull(colNumber,set)); }
310 bool SetNull(const char *colName, bool set=true) { return (SetNull(colName,set)); }
311 #endif
312 #ifdef __WXDEBUG__
313 ULONG GetTableID() { return tableID; }
314 #endif
315
316 //TODO: Need to Document
317 typedef enum { WX_ROW_MODE_QUERY , WX_ROW_MODE_INDIVIDUAL } rowmode_t;
318 virtual void SetRowMode(const rowmode_t rowmode);
319 #if wxODBC_BACKWARD_COMPATABILITY
320 virtual wxVariant GetCol(const int colNumber) const { return GetColumn(colNumber); };
321 virtual void SetCol(const int colNumber, const wxVariant value) { return SetColumn(colNumber, value); };
322 #endif
323 virtual wxVariant GetColumn(const int colNumber) const ;
324 virtual void SetColumn(const int colNumber, const wxVariant value);
325 virtual GenericKey GetKey(void);
326 virtual void SetKey(const GenericKey &key);
327
328 private:
329 HSTMT *m_hstmtGridQuery;
330 rowmode_t m_rowmode;
331 size_t m_keysize;
332
333 // typedef enum {unmodified=0, UpdatePending, InsertPending } recStatus;
334
335 // recStatus get_ModifiedStatus() { return m_recstatus; }
336
337 // void modify() {
338 // if (m_recstatus==unmodified)
339 // m_recstatus=UpdatePending;
340 // }
341 // protected:
342 // void insertify() {m_recstatus=InsertPending; }
343 // void unmodify() {m_recstatus=unmodified; }
344 // recStatus m_recstatus;
345 //TODO: Need to Document
346 }; // wxDbTable
347
348
349 // Change this to 0 to remove use of all deprecated functions
350 #if wxODBC_BACKWARD_COMPATABILITY
351 //#################################################################################
352 //############### DEPRECATED functions for backward compatibility #################
353 //#################################################################################
354
355 // Backward compability. These will eventually go away
356 typedef wxDbTable wxTable;
357 typedef wxDbIdxDef wxIdxDef;
358 typedef wxDbIdxDef CidxDef;
359 typedef wxDbColDef wxColDef;
360 typedef wxDbColDef CcolDef;
361 typedef wxDbColDataPtr wxColDataPtr;
362 typedef wxDbColDataPtr CcolDataPtr;
363
364 const int ROWID = wxDB_ROWID_LEN;
365 const int DEFAULT_CURSOR = wxDB_DEFAULT_CURSOR;
366 const bool QUERY_ONLY = wxDB_QUERY_ONLY;
367 const bool DISABLE_VIEW = wxDB_DISABLE_VIEW;
368 #endif
369
370 #endif