1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Declaration of the wxDbTable class.
5 // Modified by: George Tasker
10 // Copyright: (c) 1996 Remstar International, Inc.
11 // Licence: wxWindows licence
12 ///////////////////////////////////////////////////////////////////////////////
26 #include "wx/variant.h"
27 #include "wx/dbkeyg.h"
29 const int wxDB_ROWID_LEN
= 24; // 18 is the max, 24 is in case it gets larger
30 const int wxDB_DEFAULT_CURSOR
= 0;
31 const bool wxDB_QUERY_ONLY
= true;
32 const bool wxDB_DISABLE_VIEW
= true;
34 // Used to indicate end of a variable length list of
35 // column numbers passed to member functions
36 const int wxDB_NO_MORE_COLUMN_NUMBERS
= -1;
38 // The following class is used to define a column of a table.
39 // The wxDbTable constructor will dynamically allocate as many of
40 // these as there are columns in the table. The class derived
41 // from wxDbTable must initialize these column definitions in it's
42 // constructor. These column definitions provide inf. to the
43 // wxDbTable class which allows it to create a table in the data
44 // source, exchange data between the data source and the C++
46 class WXDLLIMPEXP_ODBC wxDbColDef
49 wxChar ColName
[DB_MAX_COLUMN_NAME_LEN
+1]; // Column Name
50 int DbDataType
; // Logical Data Type; e.g. DB_DATA_TYPE_INTEGER
51 SWORD SqlCtype
; // C data type; e.g. SQL_C_LONG
52 void *PtrDataObj
; // Address of the data object
53 int SzDataObj
; // Size, in bytes, of the data object
54 bool KeyField
; // true if this column is part of the PRIMARY KEY to the table; Date fields should NOT be KeyFields.
55 bool Updateable
; // Specifies whether this column is updateable
56 bool InsertAllowed
; // Specifies whether this column should be included in an INSERT statement
57 bool DerivedCol
; // Specifies whether this column is a derived value
58 SQLLEN CbValue
; // Internal use only!!! For parameter bindings
59 bool Null
; // NOT FULLY IMPLEMENTED - Allows NULL values in Inserts and Updates
60 SQLLEN CbValueCol
; // Internal use only!!! For column bindings
68 class WXDLLIMPEXP_ODBC wxDbColDataPtr
77 // This structure is used when creating secondary indexes.
78 class WXDLLIMPEXP_ODBC wxDbIdxDef
81 wxChar ColName
[DB_MAX_COLUMN_NAME_LEN
+1];
86 class WXDLLIMPEXP_ODBC wxDbTable
89 ULONG tableID
; // Used for debugging. This can help to match up mismatched constructors/destructors
91 // Private member variables
95 // Private member functions
96 bool initialize(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
97 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
);
100 void setCbValueForColumn(int columnIndex
);
101 bool bindParams(bool forUpdate
); // called by the other 'bind' functions
102 bool bindInsertParams(void);
103 bool bindUpdateParams(void);
105 bool bindCols(HSTMT cursor
);
106 bool getRec(UWORD fetchType
);
107 bool execDelete(const wxString
&pSqlStmt
);
108 bool execUpdate(const wxString
&pSqlStmt
);
109 bool query(int queryType
, bool forUpdate
, bool distinct
, const wxString
&pSqlStmt
=wxEmptyString
);
111 #if !wxODBC_BACKWARD_COMPATABILITY
113 // Where, Order By and From clauses
114 wxString where
; // Standard SQL where clause, minus the word WHERE
115 wxString orderBy
; // Standard SQL order by clause, minus the ORDER BY
116 wxString from
; // Allows for joins in a wxDbTable::Query(). Format: ",tbl,tbl..."
119 HENV henv
; // ODBC Environment handle
120 HDBC hdbc
; // ODBC DB Connection handle
121 HSTMT hstmt
; // ODBC Statement handle
122 HSTMT
*hstmtDefault
; // Default cursor
123 HSTMT hstmtInsert
; // ODBC Statement handle used specifically for inserts
124 HSTMT hstmtDelete
; // ODBC Statement handle used specifically for deletes
125 HSTMT hstmtUpdate
; // ODBC Statement handle used specifically for updates
126 HSTMT hstmtInternal
; // ODBC Statement handle used internally only
127 HSTMT
*hstmtCount
; // ODBC Statement handle used by Count() function (No binding of columns)
130 bool selectForUpdate
;
132 // Pointer to the database object this table belongs to
136 wxString tablePath
; // needed for dBase tables
137 wxString tableName
; // Table name
138 wxString queryTableName
; // Query Table Name
139 UWORD m_numCols
; // # of columns in the table
140 bool queryOnly
; // Query Only, no inserts, updates or deletes
142 // Column Definitions
143 wxDbColDef
*colDefs
; // Array of wxDbColDef structures
146 #if wxODBC_BACKWARD_COMPATABILITY
147 // Where, Order By and From clauses
148 char *where
; // Standard SQL where clause, minus the word WHERE
149 char *orderBy
; // Standard SQL order by clause, minus the ORDER BY
150 char *from
; // Allows for joins in a wxDbTable::Query(). Format: ",tbl,tbl..."
153 HENV henv
; // ODBC Environment handle
154 HDBC hdbc
; // ODBC DB Connection handle
155 HSTMT hstmt
; // ODBC Statement handle
156 HSTMT
*hstmtDefault
; // Default cursor
157 HSTMT hstmtInsert
; // ODBC Statement handle used specifically for inserts
158 HSTMT hstmtDelete
; // ODBC Statement handle used specifically for deletes
159 HSTMT hstmtUpdate
; // ODBC Statement handle used specifically for updates
160 HSTMT hstmtInternal
; // ODBC Statement handle used internally only
161 HSTMT
*hstmtCount
; // ODBC Statement handle used by Count() function (No binding of columns)
164 bool selectForUpdate
;
166 // Pointer to the database object this table belongs to
170 char tablePath
[wxDB_PATH_MAX
]; // needed for dBase tables
171 char tableName
[DB_MAX_TABLE_NAME_LEN
+1]; // Table name
172 char queryTableName
[DB_MAX_TABLE_NAME_LEN
+1]; // Query Table Name
173 UWORD m_numCols
; // # of columns in the table
174 bool queryOnly
; // Query Only, no inserts, updates or deletes
176 // Column Definitions
177 wxDbColDef
*colDefs
; // Array of wxDbColDef structures
179 // Public member functions
180 wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
181 const wxString
&qryTblName
=wxEmptyString
, bool qryOnly
= !wxDB_QUERY_ONLY
,
182 const wxString
&tblPath
=wxEmptyString
);
184 virtual ~wxDbTable();
186 bool Open(bool checkPrivileges
=false, bool checkTableExists
=true);
187 bool CreateTable(bool attemptDrop
=true);
188 bool DropTable(void);
189 bool CreateIndex(const wxString
&indexName
, bool unique
, UWORD numIndexColumns
,
190 wxDbIdxDef
*pIndexDefs
, bool attemptDrop
=true);
191 bool DropIndex(const wxString
&indexName
);
195 // The member variables returned by these accessors are all
196 // set when the wxDbTable instance is created and cannot be
197 // changed, hence there is no corresponding SetXxxx function
198 wxDb
*GetDb() { return pDb
; }
199 const wxString
&GetTableName() { return tableName
; }
200 const wxString
&GetQueryTableName() { return queryTableName
; }
201 const wxString
&GetTablePath() { return tablePath
; }
203 UWORD
GetNumberOfColumns() { return m_numCols
; } // number of "defined" columns for this wxDbTable instance
205 const wxString
&GetFromClause() { return from
; }
206 const wxString
&GetOrderByClause() { return orderBy
; }
207 const wxString
&GetWhereClause() { return where
; }
209 bool IsQueryOnly() { return queryOnly
; }
210 #if wxODBC_BACKWARD_COMPATABILITY
211 void SetFromClause(const char *From
) { from
= (char *)From
; }
212 void SetOrderByClause(const char *OrderBy
) { orderBy
= (char *)OrderBy
; }
213 void SetWhereClause(const char *Where
) { where
= (char *)Where
; }
215 void SetFromClause(const wxString
&From
) { from
= From
; }
216 void SetOrderByClause(const wxString
&OrderBy
) { orderBy
= OrderBy
; }
217 bool SetOrderByColNums(UWORD first
, ...);
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 wxString
&Where() { return where
; }
223 const wxString
&OrderBy() { return orderBy
; }
224 const wxString
&From() { return from
; }
228 bool Update(const wxString
&pSqlStmt
);
229 bool UpdateWhere(const wxString
&pWhereClause
);
231 bool DeleteWhere(const wxString
&pWhereClause
);
232 bool DeleteMatching(void);
233 virtual bool Query(bool forUpdate
= false, bool distinct
= false);
234 bool QueryBySqlStmt(const wxString
&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(wxString
&pSqlStmt
, int typeOfSelect
, bool distinct
);
252 void BuildSelectStmt(wxChar
*pSqlStmt
, int typeOfSelect
, bool distinct
);
254 void BuildDeleteStmt(wxString
&pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
=wxEmptyString
);
255 void BuildDeleteStmt(wxChar
*pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
=wxEmptyString
);
257 void BuildUpdateStmt(wxString
&pSqlStmt
, int typeOfUpdate
, const wxString
&pWhereClause
=wxEmptyString
);
258 void BuildUpdateStmt(wxChar
*pSqlStmt
, int typeOfUpdate
, const wxString
&pWhereClause
=wxEmptyString
);
260 void BuildWhereClause(wxString
&pWhereClause
, int typeOfWhere
, const wxString
&qualTableName
=wxEmptyString
, bool useLikeComparison
=false);
261 void BuildWhereClause(wxChar
*pWhereClause
, int typeOfWhere
, const wxString
&qualTableName
=wxEmptyString
, bool useLikeComparison
=false);
263 #if wxODBC_BACKWARD_COMPATABILITY
264 // The following member functions are deprecated. You should use the BuildXxxxxStmt functions (above)
265 void GetSelectStmt(char *pSqlStmt
, int typeOfSelect
, bool distinct
)
266 { BuildSelectStmt(pSqlStmt
,typeOfSelect
,distinct
); }
267 void GetDeleteStmt(char *pSqlStmt
, int typeOfDel
, const char *pWhereClause
= NULL
)
268 { BuildDeleteStmt(pSqlStmt
,typeOfDel
,pWhereClause
); }
269 void GetUpdateStmt(char *pSqlStmt
, int typeOfUpdate
, const char *pWhereClause
= NULL
)
270 { BuildUpdateStmt(pSqlStmt
,typeOfUpdate
,pWhereClause
); }
271 void GetWhereClause(char *pWhereClause
, int typeOfWhere
,
272 const char *qualTableName
= NULL
, bool useLikeComparison
=false)
273 { BuildWhereClause(pWhereClause
,typeOfWhere
,qualTableName
,useLikeComparison
); }
275 bool CanSelectForUpdate(void);
276 #if wxODBC_BACKWARD_COMPATABILITY
277 bool CanUpdByROWID(void) { return CanUpdateByRowID(); };
279 bool CanUpdateByROWID(void);
280 void ClearMemberVar(UWORD colNumber
, bool setToNull
=false);
281 void ClearMemberVars(bool setToNull
=false);
282 bool SetQueryTimeout(UDWORD nSeconds
);
284 wxDbColDef
*GetColDefs() { return colDefs
; }
285 bool SetColDefs(UWORD index
, const wxString
&fieldName
, int dataType
,
286 void *pData
, SWORD cType
,
287 int size
, bool keyField
= false, bool updateable
= true,
288 bool insertAllowed
= true, bool derivedColumn
= false);
289 wxDbColDataPtr
*SetColDefs(wxDbColInf
*colInfs
, UWORD numCols
);
291 bool CloseCursor(HSTMT cursor
);
292 bool DeleteCursor(HSTMT
*hstmtDel
);
293 void SetCursor(HSTMT
*hstmtActivate
= (void **) wxDB_DEFAULT_CURSOR
);
294 HSTMT
GetCursor(void) { return(hstmt
); }
295 HSTMT
*GetNewCursor(bool setCursor
= false, bool bindColumns
= true);
296 #if wxODBC_BACKWARD_COMPATABILITY
297 // The following member function is deprecated. You should use the GetNewCursor
298 HSTMT
*NewCursor(bool setCursor
= false, bool bindColumns
= true) { return GetNewCursor(setCursor
,bindColumns
); }
301 ULONG
Count(const wxString
&args
=_T("*"));
302 int DB_STATUS(void) { return(pDb
->DB_STATUS
); }
304 bool IsColNull(UWORD colNumber
) const;
305 bool SetColNull(UWORD colNumber
, bool set
=true);
306 bool SetColNull(const wxString
&colName
, bool set
=true);
307 #if wxODBC_BACKWARD_COMPATABILITY
308 // The following member functions are deprecated. You should use the SetColNull()
309 bool SetNull(int colNumber
, bool set
=true) { return (SetNull(colNumber
,set
)); }
310 bool SetNull(const char *colName
, bool set
=true) { return (SetNull(colName
,set
)); }
313 ULONG
GetTableID() { return tableID
; }
316 //TODO: Need to Document
317 typedef enum { WX_ROW_MODE_QUERY
, WX_ROW_MODE_INDIVIDUAL
} rowmode_t
;
318 virtual void SetRowMode(const rowmode_t rowmode
);
319 #if wxODBC_BACKWARD_COMPATABILITY
320 virtual wxVariant
GetCol(const int colNumber
) const { return GetColumn(colNumber
); };
321 virtual void SetCol(const int colNumber
, const wxVariant value
) { return SetColumn(colNumber
, value
); };
323 virtual wxVariant
GetColumn(const int colNumber
) const ;
324 virtual void SetColumn(const int colNumber
, const wxVariant value
);
325 virtual GenericKey
GetKey(void);
326 virtual void SetKey(const GenericKey
&key
);
329 HSTMT
*m_hstmtGridQuery
;
333 // typedef enum {unmodified=0, UpdatePending, InsertPending } recStatus;
335 // recStatus get_ModifiedStatus() { return m_recstatus; }
338 // if (m_recstatus==unmodified)
339 // m_recstatus=UpdatePending;
342 // void insertify() {m_recstatus=InsertPending; }
343 // void unmodify() {m_recstatus=unmodified; }
344 // recStatus m_recstatus;
345 //TODO: Need to Document
349 // Change this to 0 to remove use of all deprecated functions
350 #if wxODBC_BACKWARD_COMPATABILITY
351 //#################################################################################
352 //############### DEPRECATED functions for backward compatibility #################
353 //#################################################################################
355 // Backward compability. These will eventually go away
356 typedef wxDbTable wxTable
;
357 typedef wxDbIdxDef wxIdxDef
;
358 typedef wxDbIdxDef CidxDef
;
359 typedef wxDbColDef wxColDef
;
360 typedef wxDbColDef CcolDef
;
361 typedef wxDbColDataPtr wxColDataPtr
;
362 typedef wxDbColDataPtr CcolDataPtr
;
364 const int ROWID
= wxDB_ROWID_LEN
;
365 const int DEFAULT_CURSOR
= wxDB_DEFAULT_CURSOR
;
366 const bool QUERY_ONLY
= wxDB_QUERY_ONLY
;
367 const bool DISABLE_VIEW
= wxDB_DISABLE_VIEW
;