]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dbtable.h
Added optional param to Count() function to allow for counting DISTINCT columns rathe...
[wxWidgets.git] / include / wx / dbtable.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: dbtable.h
3 // Purpose: Declaration of the wxTable class.
4 // Author: Doug Card
5 // Modified by:
6 // Created: 9.96
7 // RCS-ID: $Id$
8 // Copyright: (c) 1996 Remstar International, Inc.
9 // Licence: wxWindows licence, plus:
10 // Notice: This class library and its intellectual design are free of charge for use,
11 // modification, enhancement, debugging under the following conditions:
12 // 1) These classes may only be used as part of the implementation of a
13 // wxWindows-based application
14 // 2) All enhancements and bug fixes are to be submitted back to the wxWindows
15 // user groups free of all charges for use with the wxWindows library.
16 // 3) These classes may not be distributed as part of any other class library,
17 // DLL, text (written or electronic), other than a complete distribution of
18 // the wxWindows GUI development toolkit.
19 ///////////////////////////////////////////////////////////////////////////////
20
21 /*
22 // SYNOPSIS START
23 // SYNOPSIS STOP
24 */
25
26 #ifndef DBTABLE_DOT_H
27 #define DBTABLE_DOT_H
28
29 // Use this line for wxWindows v1.x
30 //#include "wx_ver.h"
31 // Use this line for wxWindows v2.x
32 #include "wx/version.h"
33
34 #if wxMAJOR_VERSION == 2
35 #ifdef __GNUG__
36 #pragma interface "dbtable.h"
37 #endif
38 #endif
39
40 #if wxMAJOR_VERSION == 2
41 #include "wx/db.h"
42 #else
43 #include "db.h"
44 #endif
45
46 const int ROWID_LEN = 24; // 18 is the max, 24 is in case it gets larger
47 const int DEFAULT_CURSOR = 0;
48 const bool QUERY_ONLY = TRUE;
49 const bool DISABLE_VIEW = TRUE;
50
51 // The following class is used to define a column of a table.
52 // The wxTable constructor will dynamically allocate as many of
53 // these as there are columns in the table. The class derived
54 // from wxTable must initialize these column definitions in it's
55 // constructor. These column definitions provide inf. to the
56 // wxTable class which allows it to create a table in the data
57 // source, exchange data between the data source and the C++
58 // object, and so on.
59
60 class WXDLLEXPORT CcolDef
61 {
62 public:
63 char ColName[DB_MAX_COLUMN_NAME_LEN+1]; // Column Name
64 int DbDataType; // Logical Data Type; e.g. DB_DATA_TYPE_INTEGER
65 int SqlCtype; // C data type; e.g. SQL_C_LONG
66 void *PtrDataObj; // Address of the data object
67 int SzDataObj; // Size, in bytes, of the data object
68 bool KeyField; // TRUE if this column is part of the PRIMARY KEY to the table; Date fields should NOT be KeyFields.
69 bool Updateable; // Specifies whether this column is updateable
70 bool InsertAllowed; // Specifies whether this column should be included in an INSERT statement
71 bool DerivedCol; // Specifies whether this column is a derived value
72 SDWORD CbValue; // Internal use only!!!
73 bool Null; // NOT FULLY IMPLEMENTED - Allows NULL values in Inserts and Updates
74 }; // CcolDef
75
76 // This structure is used when creating secondary indexes.
77 class WXDLLEXPORT CidxDef
78 {
79 public:
80 char ColName[DB_MAX_COLUMN_NAME_LEN+1];
81 bool Ascending;
82 }; // CidxDef
83
84 class WXDLLEXPORT wxTable
85 {
86 private:
87
88 ULONG tableID; // Used for debugging. This can help to match up mismatched constructors/destructors
89
90 // Private member variables
91 UDWORD cursorType;
92
93 // Private member functions
94 bool bindInsertParams(void);
95 bool bindUpdateParams(void);
96 bool bindCols(HSTMT cursor);
97 bool getRec(UWORD fetchType);
98 bool execDelete(char *pSqlStmt);
99 bool execUpdate(char *pSqlStmt);
100 bool query(int queryType, bool forUpdate, bool distinct, char *pSqlStmt = 0);
101
102 public:
103
104 // Pointer to the database object this table belongs to
105 wxDB *pDb;
106
107 // ODBC Handles
108 HENV henv; // ODBC Environment handle
109 HDBC hdbc; // ODBC DB Connection handle
110 HSTMT hstmt; // ODBC Statement handle
111 HSTMT *hstmtDefault; // Default cursor
112 HSTMT hstmtInsert; // ODBC Statement handle used specifically for inserts
113 HSTMT hstmtDelete; // ODBC Statement handle used specifically for deletes
114 HSTMT hstmtUpdate; // ODBC Statement handle used specifically for updates
115 HSTMT hstmtInternal; // ODBC Statement handle used internally only
116 HSTMT *hstmtCount; // ODBC Statement handle used by Count() function (No binding of columns)
117
118 // Table Inf.
119 char tableName[DB_MAX_TABLE_NAME_LEN+1]; // Table name
120 char queryTableName[DB_MAX_TABLE_NAME_LEN+1]; // Query Table Name
121 int noCols; // # of columns in the table
122 bool queryOnly; // Query Only, no inserts, updates or deletes
123
124 char tablePath[DB_PATH_MAX]; // needed for dBase tables
125
126 // Column Definitions
127 CcolDef *colDefs; // Array of CcolDef structures
128
129 // Where, Order By and From clauses
130 char *where; // Standard SQL where clause, minus the word WHERE
131 char *orderBy; // Standard SQL order by clause, minus the ORDER BY
132 char *from; // Allows for joins in a wxTable::Query(). Format: ",tbl,tbl..."
133
134 // Flags
135 bool selectForUpdate;
136
137 // Public member functions
138 wxTable(wxDB *pwxDB, const char *tblName, const int nCols,
139 const char *qryTblName = 0, bool qryOnly = !QUERY_ONLY, char *tblPath=NULL);
140 virtual ~wxTable();
141 bool Open(void);
142 bool CreateTable(bool attemptDrop=TRUE);
143 bool DropTable(void);
144 bool CreateIndex(char * idxName, bool unique, int noIdxCols, CidxDef *pIdxDefs, bool attemptDrop=TRUE);
145 bool DropIndex(char * idxName);
146 bool CloseCursor(HSTMT cursor);
147 int Insert(void);
148 bool Update(void);
149 bool Update(char *pSqlStmt);
150 bool UpdateWhere(char *pWhereClause);
151 bool Delete(void);
152 bool DeleteWhere(char *pWhereClause);
153 bool DeleteMatching(void);
154 virtual bool Query(bool forUpdate = FALSE, bool distinct = FALSE);
155 bool QueryBySqlStmt(char *pSqlStmt);
156 bool QueryMatching(bool forUpdate = FALSE, bool distinct = FALSE);
157 bool QueryOnKeyFields(bool forUpdate = FALSE, bool distinct = FALSE);
158 bool GetNext(void) { return(getRec(SQL_FETCH_NEXT)); }
159 bool operator++(int) { return(getRec(SQL_FETCH_NEXT)); }
160 #if !wxODBC_FWD_ONLY_CURSORS
161 bool GetPrev(void) { return(getRec(SQL_FETCH_PRIOR)); }
162 bool operator--(int) { return(getRec(SQL_FETCH_PRIOR)); }
163 bool GetFirst(void) { return(getRec(SQL_FETCH_FIRST)); }
164 bool GetLast(void) { return(getRec(SQL_FETCH_LAST)); }
165 #endif
166 bool IsCursorClosedOnCommit(void);
167 bool IsColNull(int colNo);
168 UWORD GetRowNum(void);
169 void GetSelectStmt(char *pSqlStmt, int typeOfSelect, bool distinct);
170 void GetDeleteStmt(char *pSqlStmt, int typeOfDel, char *pWhereClause = 0);
171 void GetUpdateStmt(char *pSqlStmt, int typeOfUpd, char *pWhereClause = 0);
172 void GetWhereClause(char *pWhereClause, int typeOfWhere, char *qualTableName = 0);
173 bool CanSelectForUpdate(void);
174 bool CanUpdByROWID(void);
175 void ClearMemberVars(void);
176 bool SetQueryTimeout(UDWORD nSeconds);
177 void SetColDefs (int index, char *fieldName, int dataType, void *pData, int cType,
178 int size, bool keyField = FALSE, bool upd = TRUE,
179 bool insAllow = TRUE, bool derivedCol = FALSE);
180 HSTMT *NewCursor(bool setCursor = FALSE, bool bindColumns = TRUE);
181 bool DeleteCursor(HSTMT *hstmtDel);
182 void SetCursor(HSTMT *hstmtActivate = (void **) DEFAULT_CURSOR);
183 HSTMT GetCursor(void) { return(hstmt); }
184 ULONG Count(const char *args="*");
185 int DB_STATUS(void) { return(pDb->DB_STATUS); }
186 bool Refresh(void);
187 bool SetNull(int colNo);
188 bool SetNull(char *colName);
189
190 #ifdef __WXDEBUG__
191 ULONG GetTableID() { return tableID; };
192 #endif
193
194 }; // wxTable
195
196 #endif