1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Declaration of the wxDbTable class.
5 // Modified by: George Tasker
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 ///////////////////////////////////////////////////////////////////////////////
29 // Use this line for wxWindows v1.x
31 // Use this line for wxWindows v2.x
32 #include "wx/version.h"
34 #if wxMAJOR_VERSION == 2
36 #pragma interface "dbtable.h"
40 #if wxMAJOR_VERSION == 2
46 const int wxDB_ROWID_LEN
= 24; // 18 is the max, 24 is in case it gets larger
47 const int wxDB_DEFAULT_CURSOR
= 0;
48 const bool wxDB_QUERY_ONLY
= TRUE
;
49 const bool wxDB_DISABLE_VIEW
= TRUE
;
51 // The following class is used to define a column of a table.
52 // The wxDbTable constructor will dynamically allocate as many of
53 // these as there are columns in the table. The class derived
54 // from wxDbTable must initialize these column definitions in it's
55 // constructor. These column definitions provide inf. to the
56 // wxDbTable class which allows it to create a table in the data
57 // source, exchange data between the data source and the C++
59 class WXDLLEXPORT wxDbColDef
62 char ColName
[DB_MAX_COLUMN_NAME_LEN
+1]; // Column Name
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!!!
72 bool Null
; // NOT FULLY IMPLEMENTED - Allows NULL values in Inserts and Updates
76 class WXDLLEXPORT wxDbColDataPtr
85 // This structure is used when creating secondary indexes.
86 class WXDLLEXPORT wxDbIdxDef
89 char ColName
[DB_MAX_COLUMN_NAME_LEN
+1];
94 class WXDLLEXPORT wxDbTable
97 ULONG tableID
; // Used for debugging. This can help to match up mismatched constructors/destructors
99 // Private member variables
103 // Private member functions
104 bool bindInsertParams(void);
105 bool bindUpdateParams(void);
106 bool bindCols(HSTMT cursor
);
107 bool getRec(UWORD fetchType
);
108 bool execDelete(const char *pSqlStmt
);
109 bool execUpdate(const char *pSqlStmt
);
110 bool query(int queryType
, bool forUpdate
, bool distinct
, const char *pSqlStmt
= 0);
112 #if !wxODBC_BACKWARD_COMPATABILITY
114 // Where, Order By and From clauses
115 wxString where
; // Standard SQL where clause, minus the word WHERE
116 wxString orderBy
; // Standard SQL order by clause, minus the ORDER BY
117 wxString from
; // Allows for joins in a wxDbTable::Query(). Format: ",tbl,tbl..."
120 HENV henv
; // ODBC Environment handle
121 HDBC hdbc
; // ODBC DB Connection handle
122 HSTMT hstmt
; // ODBC Statement handle
123 HSTMT
*hstmtDefault
; // Default cursor
124 HSTMT hstmtInsert
; // ODBC Statement handle used specifically for inserts
125 HSTMT hstmtDelete
; // ODBC Statement handle used specifically for deletes
126 HSTMT hstmtUpdate
; // ODBC Statement handle used specifically for updates
127 HSTMT hstmtInternal
; // ODBC Statement handle used internally only
128 HSTMT
*hstmtCount
; // ODBC Statement handle used by Count() function (No binding of columns)
131 bool selectForUpdate
;
133 // Pointer to the database object this table belongs to
137 char tablePath
[wxDB_PATH_MAX
]; // needed for dBase tables
138 char tableName
[DB_MAX_TABLE_NAME_LEN
+1]; // Table name
139 char queryTableName
[DB_MAX_TABLE_NAME_LEN
+1]; // Query Table Name
140 int noCols
; // # of columns in the table
141 bool queryOnly
; // Query Only, no inserts, updates or deletes
143 // Column Definitions
144 wxDbColDef
*colDefs
; // Array of wxDbColDef structures
147 #if wxODBC_BACKWARD_COMPATABILITY
148 // Where, Order By and From clauses
149 char *where
; // Standard SQL where clause, minus the word WHERE
150 char *orderBy
; // Standard SQL order by clause, minus the ORDER BY
151 char *from
; // Allows for joins in a wxDbTable::Query(). Format: ",tbl,tbl..."
154 HENV henv
; // ODBC Environment handle
155 HDBC hdbc
; // ODBC DB Connection handle
156 HSTMT hstmt
; // ODBC Statement handle
157 HSTMT
*hstmtDefault
; // Default cursor
158 HSTMT hstmtInsert
; // ODBC Statement handle used specifically for inserts
159 HSTMT hstmtDelete
; // ODBC Statement handle used specifically for deletes
160 HSTMT hstmtUpdate
; // ODBC Statement handle used specifically for updates
161 HSTMT hstmtInternal
; // ODBC Statement handle used internally only
162 HSTMT
*hstmtCount
; // ODBC Statement handle used by Count() function (No binding of columns)
165 bool selectForUpdate
;
167 // Pointer to the database object this table belongs to
171 char tablePath
[wxDB_PATH_MAX
]; // needed for dBase tables
172 char tableName
[DB_MAX_TABLE_NAME_LEN
+1]; // Table name
173 char queryTableName
[DB_MAX_TABLE_NAME_LEN
+1]; // Query Table Name
174 int noCols
; // # of columns in the table
175 bool queryOnly
; // Query Only, no inserts, updates or deletes
177 // Column Definitions
178 wxDbColDef
*colDefs
; // Array of wxDbColDef structures
180 // Public member functions
181 wxDbTable(wxDb
*pwxDb
, const char *tblName
, const int nCols
,
182 const char *qryTblName
= 0, bool qryOnly
= !wxDB_QUERY_ONLY
, const char *tblPath
=NULL
);
183 virtual ~wxDbTable();
186 bool CreateTable(bool attemptDrop
=TRUE
);
187 bool DropTable(void);
188 bool CreateIndex(const char * idxName
, bool unique
, int noIdxCols
, wxDbIdxDef
*pIdxDefs
, bool attemptDrop
=TRUE
);
189 bool DropIndex(const char * idxName
);
193 // The member variables returned by these accessors are all
194 // set when the wxDbTable instance is createand cannot be
195 // changed, hence there is no corresponding SetXxxx function
196 wxDb
*GetDb() { return pDb
; }
197 const char *GetTableName() { return tableName
; }
198 const char *GetQueryTableName() { return queryTableName
; }
199 const char *GetTablePath() { return tablePath
; }
201 int GetNumberOfColumns() { return noCols
; } // number of "defined" columns for this wxDbTable instance
204 const char *GetFromClause() { return from
; }
205 const char *GetOrderByClause() { return orderBy
; }
206 const char *GetWhereClause() { return where
; }
208 bool IsQueryOnly() { return queryOnly
; }
209 #if wxODBC_BACKWARD_COMPATABILITY
210 void SetFromClause(const char *From
) { from
= (char *)From
; }
211 void SetOrderByClause(const char *OrderBy
) { orderBy
= (char *)OrderBy
; }
212 void SetWhereClause(const char *Where
) { where
= (char *)Where
; }
214 void SetFromClause(const wxString
& From
) { from
= From
; }
215 void SetOrderByClause(const wxString
& OrderBy
) { orderBy
= OrderBy
; }
216 void SetWhereClause(const wxString
& Where
) { where
= Where
; }
220 bool Update(const char *pSqlStmt
);
221 bool UpdateWhere(const char *pWhereClause
);
223 bool DeleteWhere(const char *pWhereClause
);
224 bool DeleteMatching(void);
225 virtual bool Query(bool forUpdate
= FALSE
, bool distinct
= FALSE
);
226 bool QueryBySqlStmt(const char *pSqlStmt
);
227 bool QueryMatching(bool forUpdate
= FALSE
, bool distinct
= FALSE
);
228 bool QueryOnKeyFields(bool forUpdate
= FALSE
, bool distinct
= FALSE
);
230 bool GetNext(void) { return(getRec(SQL_FETCH_NEXT
)); }
231 bool operator++(int) { return(getRec(SQL_FETCH_NEXT
)); }
233 /***** These four functions only work with wxDb instances that are defined *****
234 ***** as not being FwdOnlyCursors *****/
236 bool operator--(int);
240 bool IsCursorClosedOnCommit(void);
241 UWORD
GetRowNum(void);
243 void BuildSelectStmt(char *pSqlStmt
, int typeOfSelect
, bool distinct
);
244 void BuildDeleteStmt(char *pSqlStmt
, int typeOfDel
, const char *pWhereClause
= 0);
245 void BuildUpdateStmt(char *pSqlStmt
, int typeOfUpd
, const char *pWhereClause
= 0);
246 void BuildWhereClause(char *pWhereClause
, int typeOfWhere
, const char *qualTableName
= 0, bool useLikeComparison
=FALSE
);
247 #if wxODBC_BACKWARD_COMPATABILITY
248 // The following member functions are deprecated. You should use the BuildXxxxxStmt functions (above)
249 void GetSelectStmt(char *pSqlStmt
, int typeOfSelect
, bool distinct
)
250 { BuildSelectStmt(pSqlStmt
,typeOfSelect
,distinct
); }
251 void GetDeleteStmt(char *pSqlStmt
, int typeOfDel
, const char *pWhereClause
= 0)
252 { BuildDeleteStmt(pSqlStmt
,typeOfDel
,pWhereClause
); }
253 void GetUpdateStmt(char *pSqlStmt
, int typeOfUpd
, const char *pWhereClause
= 0)
254 { BuildUpdateStmt(pSqlStmt
,typeOfUpd
,pWhereClause
); }
255 void GetWhereClause(char *pWhereClause
, int typeOfWhere
,
256 const char *qualTableName
= 0, bool useLikeComparison
=FALSE
)
257 { BuildWhereClause(pWhereClause
,typeOfWhere
,qualTableName
,useLikeComparison
); }
259 bool CanSelectForUpdate(void);
260 bool CanUpdByROWID(void);
261 void ClearMemberVars(void);
262 bool SetQueryTimeout(UDWORD nSeconds
);
264 wxDbColDef
*GetColDefs() { return colDefs
; }
265 void SetColDefs(int index
, const char *fieldName
, int dataType
, void *pData
, int cType
,
266 int size
, bool keyField
= FALSE
, bool upd
= TRUE
,
267 bool insAllow
= TRUE
, bool derivedCol
= FALSE
);
268 wxDbColDataPtr
*SetColDefs(wxDbColInf
*colInfs
, ULONG numCols
);
270 bool CloseCursor(HSTMT cursor
);
271 bool DeleteCursor(HSTMT
*hstmtDel
);
272 void SetCursor(HSTMT
*hstmtActivate
= (void **) wxDB_DEFAULT_CURSOR
);
273 HSTMT
GetCursor(void) { return(hstmt
); }
274 HSTMT
*GetNewCursor(bool setCursor
= FALSE
, bool bindColumns
= TRUE
);
275 #if wxODBC_BACKWARD_COMPATABILITY
276 // The following member function is deprecated. You should use the GetNewCursor
277 HSTMT
*NewCursor(bool setCursor
= FALSE
, bool bindColumns
= TRUE
) { return GetNewCursor(setCursor
,bindColumns
); }
280 ULONG
Count(const char *args
="*");
281 int DB_STATUS(void) { return(pDb
->DB_STATUS
); }
283 bool IsColNull(int colNo
);
284 bool SetNull(int colNo
);
285 bool SetNull(const char *colName
);
288 ULONG
GetTableID() { return tableID
; }
294 // Change this to 0 to remove use of all deprecated functions
295 #if wxODBC_BACKWARD_COMPATABILITY
296 //#################################################################################
297 //############### DEPRECATED functions for backward compatability #################
298 //#################################################################################
300 // Backward compability. These will eventually go away
301 typedef wxDbTable wxTable
;
302 typedef wxDbIdxDef wxIdxDef
;
303 typedef wxDbIdxDef CidxDef
;
304 typedef wxDbColDef wxColDef
;
305 typedef wxDbColDef CcolDef
;
306 typedef wxDbColDataPtr wxColDataPtr
;
307 typedef wxDbColDataPtr CcolDataPtr
;
309 const int ROWID
= wxDB_ROWID_LEN
;
310 const int DEFAULT_CURSOR
= wxDB_DEFAULT_CURSOR
;
311 const bool QUERY_ONLY
= wxDB_QUERY_ONLY
;
312 const bool DISABLE_VIEW
= wxDB_DISABLE_VIEW
;