]>
Commit | Line | Data |
---|---|---|
108106cf | 1 | /////////////////////////////////////////////////////////////////////////////// |
a2115c88 | 2 | // Name: dbtable.h |
108106cf JS |
3 | // Purpose: Declaration of the wxTable class. |
4 | // Author: Doug Card | |
30a6d2d2 | 5 | // Modified by: George Tasker |
108106cf JS |
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 | ||
a2115c88 GT |
26 | #ifndef DBTABLE_DOT_H |
27 | #define DBTABLE_DOT_H | |
108106cf | 28 | |
a2115c88 GT |
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 | |
108106cf JS |
38 | #endif |
39 | ||
a2115c88 GT |
40 | #if wxMAJOR_VERSION == 2 |
41 | #include "wx/db.h" | |
42 | #else | |
43 | #include "db.h" | |
44 | #endif | |
108106cf | 45 | |
a2115c88 GT |
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; | |
108106cf JS |
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. | |
30a6d2d2 | 59 | class WXDLLEXPORT wxColDef |
108106cf JS |
60 | { |
61 | public: | |
a2115c88 | 62 | char ColName[DB_MAX_COLUMN_NAME_LEN+1]; // Column Name |
1fc5dd6f JS |
63 | int DbDataType; // Logical Data Type; e.g. DB_DATA_TYPE_INTEGER |
64 | int SqlCtype; // C data type; e.g. SQL_C_LONG | |
65 | void *PtrDataObj; // Address of the data object | |
66 | int SzDataObj; // Size, in bytes, of the data object | |
67 | bool KeyField; // TRUE if this column is part of the PRIMARY KEY to the table; Date fields should NOT be KeyFields. | |
68 | bool Updateable; // Specifies whether this column is updateable | |
69 | bool InsertAllowed; // Specifies whether this column should be included in an INSERT statement | |
70 | bool DerivedCol; // Specifies whether this column is a derived value | |
71 | SDWORD CbValue; // Internal use only!!! | |
a2115c88 | 72 | bool Null; // NOT FULLY IMPLEMENTED - Allows NULL values in Inserts and Updates |
30a6d2d2 GT |
73 | }; // wxColDef |
74 | ||
75 | class WXDLLEXPORT wxColDataPtr | |
76 | { | |
77 | public: | |
78 | void *PtrDataObj; | |
79 | int SzDataObj; | |
80 | int SqlCtype; | |
81 | }; // wxColDataPtr | |
82 | ||
83 | ||
84 | // Backward compability for Remstar classes. These | |
85 | // will eventually go away, so the wxColXxxx classes | |
86 | // should be used | |
87 | typedef wxColDef CcolDef; | |
88 | typedef wxColDataPtr CcolDataPtr; | |
89 | ||
108106cf JS |
90 | |
91 | // This structure is used when creating secondary indexes. | |
9f8de18f | 92 | class WXDLLEXPORT CidxDef |
108106cf JS |
93 | { |
94 | public: | |
a2115c88 | 95 | char ColName[DB_MAX_COLUMN_NAME_LEN+1]; |
1fc5dd6f | 96 | bool Ascending; |
108106cf JS |
97 | }; // CidxDef |
98 | ||
9f8de18f | 99 | class WXDLLEXPORT wxTable |
108106cf JS |
100 | { |
101 | private: | |
102 | ||
a2115c88 GT |
103 | ULONG tableID; // Used for debugging. This can help to match up mismatched constructors/destructors |
104 | ||
108106cf | 105 | // Private member variables |
a2115c88 | 106 | UDWORD cursorType; |
108106cf JS |
107 | |
108 | // Private member functions | |
109 | bool bindInsertParams(void); | |
110 | bool bindUpdateParams(void); | |
111 | bool bindCols(HSTMT cursor); | |
112 | bool getRec(UWORD fetchType); | |
0b0ca94c GT |
113 | bool execDelete(const char *pSqlStmt); |
114 | bool execUpdate(const char *pSqlStmt); | |
108106cf JS |
115 | bool query(int queryType, bool forUpdate, bool distinct, char *pSqlStmt = 0); |
116 | ||
117 | public: | |
118 | ||
119 | // Pointer to the database object this table belongs to | |
120 | wxDB *pDb; | |
121 | ||
122 | // ODBC Handles | |
123 | HENV henv; // ODBC Environment handle | |
124 | HDBC hdbc; // ODBC DB Connection handle | |
125 | HSTMT hstmt; // ODBC Statement handle | |
a2115c88 | 126 | HSTMT *hstmtDefault; // Default cursor |
108106cf JS |
127 | HSTMT hstmtInsert; // ODBC Statement handle used specifically for inserts |
128 | HSTMT hstmtDelete; // ODBC Statement handle used specifically for deletes | |
129 | HSTMT hstmtUpdate; // ODBC Statement handle used specifically for updates | |
a2115c88 GT |
130 | HSTMT hstmtInternal; // ODBC Statement handle used internally only |
131 | HSTMT *hstmtCount; // ODBC Statement handle used by Count() function (No binding of columns) | |
108106cf JS |
132 | |
133 | // Table Inf. | |
134 | char tableName[DB_MAX_TABLE_NAME_LEN+1]; // Table name | |
135 | char queryTableName[DB_MAX_TABLE_NAME_LEN+1]; // Query Table Name | |
136 | int noCols; // # of columns in the table | |
a2115c88 GT |
137 | bool queryOnly; // Query Only, no inserts, updates or deletes |
138 | ||
139 | char tablePath[DB_PATH_MAX]; // needed for dBase tables | |
108106cf JS |
140 | |
141 | // Column Definitions | |
30a6d2d2 | 142 | wxColDef *colDefs; // Array of wxColDef structures |
108106cf | 143 | |
1fc5dd6f | 144 | // Where, Order By and From clauses |
108106cf JS |
145 | char *where; // Standard SQL where clause, minus the word WHERE |
146 | char *orderBy; // Standard SQL order by clause, minus the ORDER BY | |
a2115c88 | 147 | char *from; // Allows for joins in a wxTable::Query(). Format: ",tbl,tbl..." |
108106cf JS |
148 | |
149 | // Flags | |
150 | bool selectForUpdate; | |
151 | ||
152 | // Public member functions | |
a2115c88 | 153 | wxTable(wxDB *pwxDB, const char *tblName, const int nCols, |
0b0ca94c | 154 | const char *qryTblName = 0, bool qryOnly = !QUERY_ONLY, const char *tblPath=NULL); |
7e616b10 | 155 | virtual ~wxTable(); |
1fc5dd6f | 156 | bool Open(void); |
a2115c88 GT |
157 | bool CreateTable(bool attemptDrop=TRUE); |
158 | bool DropTable(void); | |
0b0ca94c GT |
159 | bool CreateIndex(const char * idxName, bool unique, int noIdxCols, CidxDef *pIdxDefs, bool attemptDrop=TRUE); |
160 | bool DropIndex(const char * idxName); | |
1fc5dd6f JS |
161 | bool CloseCursor(HSTMT cursor); |
162 | int Insert(void); | |
163 | bool Update(void); | |
0b0ca94c GT |
164 | bool Update(const char *pSqlStmt); |
165 | bool UpdateWhere(const char *pWhereClause); | |
1fc5dd6f | 166 | bool Delete(void); |
0b0ca94c | 167 | bool DeleteWhere(const char *pWhereClause); |
1fc5dd6f JS |
168 | bool DeleteMatching(void); |
169 | virtual bool Query(bool forUpdate = FALSE, bool distinct = FALSE); | |
170 | bool QueryBySqlStmt(char *pSqlStmt); | |
171 | bool QueryMatching(bool forUpdate = FALSE, bool distinct = FALSE); | |
172 | bool QueryOnKeyFields(bool forUpdate = FALSE, bool distinct = FALSE); | |
173 | bool GetNext(void) { return(getRec(SQL_FETCH_NEXT)); } | |
174 | bool operator++(int) { return(getRec(SQL_FETCH_NEXT)); } | |
a3439c7d GT |
175 | |
176 | /***** These four functions only work with wxDB instances that are defined ***** | |
177 | ***** as not being FwdOnlyCursors *****/ | |
178 | bool GetPrev(void); | |
179 | bool operator--(int); | |
180 | bool GetFirst(void); | |
181 | bool GetLast(void); | |
182 | ||
1fc5dd6f JS |
183 | bool IsCursorClosedOnCommit(void); |
184 | bool IsColNull(int colNo); | |
185 | UWORD GetRowNum(void); | |
186 | void GetSelectStmt(char *pSqlStmt, int typeOfSelect, bool distinct); | |
0b0ca94c GT |
187 | void GetDeleteStmt(char *pSqlStmt, int typeOfDel, const char *pWhereClause = 0); |
188 | void GetUpdateStmt(char *pSqlStmt, int typeOfUpd, const char *pWhereClause = 0); | |
189 | void GetWhereClause(char *pWhereClause, int typeOfWhere, const char *qualTableName = 0); | |
1fc5dd6f JS |
190 | bool CanSelectForUpdate(void); |
191 | bool CanUpdByROWID(void); | |
192 | void ClearMemberVars(void); | |
193 | bool SetQueryTimeout(UDWORD nSeconds); | |
0b0ca94c | 194 | void SetColDefs (int index, const char *fieldName, int dataType, void *pData, int cType, |
1fc5dd6f JS |
195 | int size, bool keyField = FALSE, bool upd = TRUE, |
196 | bool insAllow = TRUE, bool derivedCol = FALSE); | |
30a6d2d2 | 197 | bool SetColDefs (wxColInf *colInfs, ULONG numCols, wxColDataPtr *pColDataPtrs); |
a2115c88 GT |
198 | HSTMT *NewCursor(bool setCursor = FALSE, bool bindColumns = TRUE); |
199 | bool DeleteCursor(HSTMT *hstmtDel); | |
200 | void SetCursor(HSTMT *hstmtActivate = (void **) DEFAULT_CURSOR); | |
201 | HSTMT GetCursor(void) { return(hstmt); } | |
e9a145df | 202 | ULONG Count(const char *args="*"); |
1fc5dd6f JS |
203 | int DB_STATUS(void) { return(pDb->DB_STATUS); } |
204 | bool Refresh(void); | |
a2115c88 | 205 | bool SetNull(int colNo); |
0b0ca94c | 206 | bool SetNull(const char *colName); |
a2115c88 | 207 | |
154f22b3 | 208 | #ifdef __WXDEBUG__ |
a2115c88 GT |
209 | ULONG GetTableID() { return tableID; }; |
210 | #endif | |
108106cf JS |
211 | |
212 | }; // wxTable | |
213 | ||
214 | #endif |