1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Declaration of the wxDbTable class.
5 // Modified by: George Tasker
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 ///////////////////////////////////////////////////////////////////////////////
31 // Use this line for wxWindows v1.x
33 // Use this line for wxWindows v2.x
34 #include "wx/version.h"
36 #if wxMAJOR_VERSION == 2
38 #pragma interface "dbtable.h"
42 #if wxMAJOR_VERSION == 2
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
;
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++
61 class WXDLLEXPORT wxDbColDef
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
78 class WXDLLEXPORT wxDbColDataPtr
87 // This structure is used when creating secondary indexes.
88 class WXDLLEXPORT wxDbIdxDef
91 char ColName
[DB_MAX_COLUMN_NAME_LEN
+1];
96 class WXDLLEXPORT wxDbTable
99 ULONG tableID
; // Used for debugging. This can help to match up mismatched constructors/destructors
101 // Private member variables
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);
114 #if !wxODBC_BACKWARD_COMPATABILITY
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..."
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)
133 bool selectForUpdate
;
135 // Pointer to the database object this table belongs to
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
145 // Column Definitions
146 wxDbColDef
*colDefs
; // Array of wxDbColDef structures
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..."
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)
167 bool selectForUpdate
;
169 // Pointer to the database object this table belongs to
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
179 // Column Definitions
180 wxDbColDef
*colDefs
; // Array of wxDbColDef structures
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
=NULL
);
185 virtual ~wxDbTable();
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
);
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
; }
203 int GetNumberOfColumns() { return noCols
; } // number of "defined" columns for this wxDbTable instance
206 const char *GetFromClause() { return from
; }
207 const char *GetOrderByClause() { return orderBy
; }
208 const char *GetWhereClause() { return where
; }
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
; }
216 void SetFromClause(const wxString
& From
) { from
= From
; }
217 void SetOrderByClause(const wxString
& OrderBy
) { orderBy
= OrderBy
; }
218 void SetWhereClause(const wxString
& Where
) { where
= Where
; }
222 bool Update(const char *pSqlStmt
);
223 bool UpdateWhere(const char *pWhereClause
);
225 bool DeleteWhere(const char *pWhereClause
);
226 bool DeleteMatching(void);
227 virtual bool Query(bool forUpdate
= FALSE
, bool distinct
= FALSE
);
228 bool QueryBySqlStmt(const char *pSqlStmt
);
229 bool QueryMatching(bool forUpdate
= FALSE
, bool distinct
= FALSE
);
230 bool QueryOnKeyFields(bool forUpdate
= FALSE
, bool distinct
= FALSE
);
232 bool GetNext(void) { return(getRec(SQL_FETCH_NEXT
)); }
233 bool operator++(int) { return(getRec(SQL_FETCH_NEXT
)); }
235 /***** These four functions only work with wxDb instances that are defined *****
236 ***** as not being FwdOnlyCursors *****/
238 bool operator--(int);
242 bool IsCursorClosedOnCommit(void);
243 UWORD
GetRowNum(void);
245 void BuildSelectStmt(char *pSqlStmt
, int typeOfSelect
, bool distinct
);
246 void BuildDeleteStmt(char *pSqlStmt
, int typeOfDel
, const char *pWhereClause
= 0);
247 void BuildUpdateStmt(char *pSqlStmt
, int typeOfUpd
, const char *pWhereClause
= 0);
248 void BuildWhereClause(char *pWhereClause
, int typeOfWhere
, const char *qualTableName
= 0, bool useLikeComparison
=FALSE
);
249 #if wxODBC_BACKWARD_COMPATABILITY
250 // The following member functions are deprecated. You should use the BuildXxxxxStmt functions (above)
251 void GetSelectStmt(char *pSqlStmt
, int typeOfSelect
, bool distinct
)
252 { BuildSelectStmt(pSqlStmt
,typeOfSelect
,distinct
); }
253 void GetDeleteStmt(char *pSqlStmt
, int typeOfDel
, const char *pWhereClause
= 0)
254 { BuildDeleteStmt(pSqlStmt
,typeOfDel
,pWhereClause
); }
255 void GetUpdateStmt(char *pSqlStmt
, int typeOfUpd
, const char *pWhereClause
= 0)
256 { BuildUpdateStmt(pSqlStmt
,typeOfUpd
,pWhereClause
); }
257 void GetWhereClause(char *pWhereClause
, int typeOfWhere
,
258 const char *qualTableName
= 0, bool useLikeComparison
=FALSE
)
259 { BuildWhereClause(pWhereClause
,typeOfWhere
,qualTableName
,useLikeComparison
); }
261 bool CanSelectForUpdate(void);
262 bool CanUpdByROWID(void);
263 void ClearMemberVars(void);
264 bool SetQueryTimeout(UDWORD nSeconds
);
266 wxDbColDef
*GetColDefs() { return colDefs
; }
267 void SetColDefs(int index
, const char *fieldName
, int dataType
, void *pData
, int cType
,
268 int size
, bool keyField
= FALSE
, bool upd
= TRUE
,
269 bool insAllow
= TRUE
, bool derivedCol
= FALSE
);
270 wxDbColDataPtr
*SetColDefs(wxDbColInf
*colInfs
, ULONG numCols
);
272 bool CloseCursor(HSTMT cursor
);
273 bool DeleteCursor(HSTMT
*hstmtDel
);
274 void SetCursor(HSTMT
*hstmtActivate
= (void **) wxDB_DEFAULT_CURSOR
);
275 HSTMT
GetCursor(void) { return(hstmt
); }
276 HSTMT
*GetNewCursor(bool setCursor
= FALSE
, bool bindColumns
= TRUE
);
277 #if wxODBC_BACKWARD_COMPATABILITY
278 // The following member function is deprecated. You should use the GetNewCursor
279 HSTMT
*NewCursor(bool setCursor
= FALSE
, bool bindColumns
= TRUE
) { return GetNewCursor(setCursor
,bindColumns
); }
282 ULONG
Count(const char *args
="*");
283 int DB_STATUS(void) { return(pDb
->DB_STATUS
); }
285 bool IsColNull(int colNo
);
286 bool SetNull(int colNo
);
287 bool SetNull(const char *colName
);
290 ULONG
GetTableID() { return tableID
; }
296 // Change this to 0 to remove use of all deprecated functions
297 #if wxODBC_BACKWARD_COMPATABILITY
298 //#################################################################################
299 //############### DEPRECATED functions for backward compatability #################
300 //#################################################################################
302 // Backward compability. These will eventually go away
303 typedef wxDbTable wxTable
;
304 typedef wxDbIdxDef wxIdxDef
;
305 typedef wxDbIdxDef CidxDef
;
306 typedef wxDbColDef wxColDef
;
307 typedef wxDbColDef CcolDef
;
308 typedef wxDbColDataPtr wxColDataPtr
;
309 typedef wxDbColDataPtr CcolDataPtr
;
311 const int ROWID
= wxDB_ROWID_LEN
;
312 const int DEFAULT_CURSOR
= wxDB_DEFAULT_CURSOR
;
313 const bool QUERY_ONLY
= wxDB_QUERY_ONLY
;
314 const bool DISABLE_VIEW
= wxDB_DISABLE_VIEW
;