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
="");
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
; }
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 char * Where() { return (const char *)where
.c_str(); }
223 const char * OrderBy() { return (const char *)orderBy
.c_str(); }
224 const char * From() { return (const char *)from
.c_str(); }
228 bool Update(const char *pSqlStmt
);
229 bool UpdateWhere(const char *pWhereClause
);
231 bool DeleteWhere(const char *pWhereClause
);
232 bool DeleteMatching(void);
233 virtual bool Query(bool forUpdate
= FALSE
, bool distinct
= FALSE
);
234 bool QueryBySqlStmt(const char *pSqlStmt
);
235 bool QueryMatching(bool forUpdate
= FALSE
, bool distinct
= FALSE
);
236 bool QueryOnKeyFields(bool forUpdate
= FALSE
, bool distinct
= FALSE
);
238 bool GetNext(void) { return(getRec(SQL_FETCH_NEXT
)); }
239 bool operator++(int) { return(getRec(SQL_FETCH_NEXT
)); }
241 /***** These four functions only work with wxDb instances that are defined *****
242 ***** as not being FwdOnlyCursors *****/
244 bool operator--(int);
248 bool IsCursorClosedOnCommit(void);
249 UWORD
GetRowNum(void);
251 void BuildSelectStmt(char *pSqlStmt
, int typeOfSelect
, bool distinct
);
252 void BuildDeleteStmt(char *pSqlStmt
, int typeOfDel
, const char *pWhereClause
= 0);
253 void BuildUpdateStmt(char *pSqlStmt
, int typeOfUpd
, const char *pWhereClause
= 0);
254 void BuildWhereClause(char *pWhereClause
, int typeOfWhere
, const char *qualTableName
= 0, bool useLikeComparison
=FALSE
);
255 #if wxODBC_BACKWARD_COMPATABILITY
256 // The following member functions are deprecated. You should use the BuildXxxxxStmt functions (above)
257 void GetSelectStmt(char *pSqlStmt
, int typeOfSelect
, bool distinct
)
258 { BuildSelectStmt(pSqlStmt
,typeOfSelect
,distinct
); }
259 void GetDeleteStmt(char *pSqlStmt
, int typeOfDel
, const char *pWhereClause
= 0)
260 { BuildDeleteStmt(pSqlStmt
,typeOfDel
,pWhereClause
); }
261 void GetUpdateStmt(char *pSqlStmt
, int typeOfUpd
, const char *pWhereClause
= 0)
262 { BuildUpdateStmt(pSqlStmt
,typeOfUpd
,pWhereClause
); }
263 void GetWhereClause(char *pWhereClause
, int typeOfWhere
,
264 const char *qualTableName
= 0, bool useLikeComparison
=FALSE
)
265 { BuildWhereClause(pWhereClause
,typeOfWhere
,qualTableName
,useLikeComparison
); }
267 bool CanSelectForUpdate(void);
268 bool CanUpdByROWID(void);
269 void ClearMemberVars(void);
270 bool SetQueryTimeout(UDWORD nSeconds
);
272 wxDbColDef
*GetColDefs() { return colDefs
; }
273 void SetColDefs(int index
, const char *fieldName
, int dataType
, void *pData
, int cType
,
274 int size
, bool keyField
= FALSE
, bool upd
= TRUE
,
275 bool insAllow
= TRUE
, bool derivedCol
= FALSE
);
276 wxDbColDataPtr
*SetColDefs(wxDbColInf
*colInfs
, ULONG numCols
);
278 bool CloseCursor(HSTMT cursor
);
279 bool DeleteCursor(HSTMT
*hstmtDel
);
280 void SetCursor(HSTMT
*hstmtActivate
= (void **) wxDB_DEFAULT_CURSOR
);
281 HSTMT
GetCursor(void) { return(hstmt
); }
282 HSTMT
*GetNewCursor(bool setCursor
= FALSE
, bool bindColumns
= TRUE
);
283 #if wxODBC_BACKWARD_COMPATABILITY
284 // The following member function is deprecated. You should use the GetNewCursor
285 HSTMT
*NewCursor(bool setCursor
= FALSE
, bool bindColumns
= TRUE
) { return GetNewCursor(setCursor
,bindColumns
); }
288 ULONG
Count(const char *args
="*");
289 int DB_STATUS(void) { return(pDb
->DB_STATUS
); }
291 bool IsColNull(int colNo
);
292 bool SetNull(int colNo
);
293 bool SetNull(const char *colName
);
296 ULONG
GetTableID() { return tableID
; }
302 // Change this to 0 to remove use of all deprecated functions
303 #if wxODBC_BACKWARD_COMPATABILITY
304 //#################################################################################
305 //############### DEPRECATED functions for backward compatability #################
306 //#################################################################################
308 // Backward compability. These will eventually go away
309 typedef wxDbTable wxTable
;
310 typedef wxDbIdxDef wxIdxDef
;
311 typedef wxDbIdxDef CidxDef
;
312 typedef wxDbColDef wxColDef
;
313 typedef wxDbColDef CcolDef
;
314 typedef wxDbColDataPtr wxColDataPtr
;
315 typedef wxDbColDataPtr CcolDataPtr
;
317 const int ROWID
= wxDB_ROWID_LEN
;
318 const int DEFAULT_CURSOR
= wxDB_DEFAULT_CURSOR
;
319 const bool QUERY_ONLY
= wxDB_QUERY_ONLY
;
320 const bool DISABLE_VIEW
= wxDB_DISABLE_VIEW
;