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 // Used to indicate end of a variable length list of
54 // column numbers passed to member functions
55 const int wxDB_NO_MORE_COLUMN_NUMBERS
= -1;
57 // The following class is used to define a column of a table.
58 // The wxDbTable constructor will dynamically allocate as many of
59 // these as there are columns in the table. The class derived
60 // from wxDbTable must initialize these column definitions in it's
61 // constructor. These column definitions provide inf. to the
62 // wxDbTable class which allows it to create a table in the data
63 // source, exchange data between the data source and the C++
65 class WXDLLEXPORT wxDbColDef
68 char ColName
[DB_MAX_COLUMN_NAME_LEN
+1]; // Column Name
69 int DbDataType
; // Logical Data Type; e.g. DB_DATA_TYPE_INTEGER
70 int SqlCtype
; // C data type; e.g. SQL_C_LONG
71 void *PtrDataObj
; // Address of the data object
72 int SzDataObj
; // Size, in bytes, of the data object
73 bool KeyField
; // TRUE if this column is part of the PRIMARY KEY to the table; Date fields should NOT be KeyFields.
74 bool Updateable
; // Specifies whether this column is updateable
75 bool InsertAllowed
; // Specifies whether this column should be included in an INSERT statement
76 bool DerivedCol
; // Specifies whether this column is a derived value
77 SDWORD CbValue
; // Internal use only!!!
78 bool Null
; // NOT FULLY IMPLEMENTED - Allows NULL values in Inserts and Updates
82 class WXDLLEXPORT wxDbColDataPtr
91 // This structure is used when creating secondary indexes.
92 class WXDLLEXPORT wxDbIdxDef
95 char ColName
[DB_MAX_COLUMN_NAME_LEN
+1];
100 class WXDLLEXPORT wxDbTable
103 ULONG tableID
; // Used for debugging. This can help to match up mismatched constructors/destructors
105 // Private member variables
109 // Private member functions
110 bool bindInsertParams(void);
111 bool bindUpdateParams(void);
112 bool bindCols(HSTMT cursor
);
113 bool getRec(UWORD fetchType
);
114 bool execDelete(const char *pSqlStmt
);
115 bool execUpdate(const char *pSqlStmt
);
116 bool query(int queryType
, bool forUpdate
, bool distinct
, const char *pSqlStmt
= NULL
);
118 #if !wxODBC_BACKWARD_COMPATABILITY
120 // Where, Order By and From clauses
121 wxString where
; // Standard SQL where clause, minus the word WHERE
122 wxString orderBy
; // Standard SQL order by clause, minus the ORDER BY
123 wxString from
; // Allows for joins in a wxDbTable::Query(). Format: ",tbl,tbl..."
126 HENV henv
; // ODBC Environment handle
127 HDBC hdbc
; // ODBC DB Connection handle
128 HSTMT hstmt
; // ODBC Statement handle
129 HSTMT
*hstmtDefault
; // Default cursor
130 HSTMT hstmtInsert
; // ODBC Statement handle used specifically for inserts
131 HSTMT hstmtDelete
; // ODBC Statement handle used specifically for deletes
132 HSTMT hstmtUpdate
; // ODBC Statement handle used specifically for updates
133 HSTMT hstmtInternal
; // ODBC Statement handle used internally only
134 HSTMT
*hstmtCount
; // ODBC Statement handle used by Count() function (No binding of columns)
137 bool selectForUpdate
;
139 // Pointer to the database object this table belongs to
143 char tablePath
[wxDB_PATH_MAX
]; // needed for dBase tables
144 char tableName
[DB_MAX_TABLE_NAME_LEN
+1]; // Table name
145 char queryTableName
[DB_MAX_TABLE_NAME_LEN
+1]; // Query Table Name
146 int noCols
; // # of columns in the table
147 bool queryOnly
; // Query Only, no inserts, updates or deletes
149 // Column Definitions
150 wxDbColDef
*colDefs
; // Array of wxDbColDef structures
153 #if wxODBC_BACKWARD_COMPATABILITY
154 // Where, Order By and From clauses
155 char *where
; // Standard SQL where clause, minus the word WHERE
156 char *orderBy
; // Standard SQL order by clause, minus the ORDER BY
157 char *from
; // Allows for joins in a wxDbTable::Query(). Format: ",tbl,tbl..."
160 HENV henv
; // ODBC Environment handle
161 HDBC hdbc
; // ODBC DB Connection handle
162 HSTMT hstmt
; // ODBC Statement handle
163 HSTMT
*hstmtDefault
; // Default cursor
164 HSTMT hstmtInsert
; // ODBC Statement handle used specifically for inserts
165 HSTMT hstmtDelete
; // ODBC Statement handle used specifically for deletes
166 HSTMT hstmtUpdate
; // ODBC Statement handle used specifically for updates
167 HSTMT hstmtInternal
; // ODBC Statement handle used internally only
168 HSTMT
*hstmtCount
; // ODBC Statement handle used by Count() function (No binding of columns)
171 bool selectForUpdate
;
173 // Pointer to the database object this table belongs to
177 char tablePath
[wxDB_PATH_MAX
]; // needed for dBase tables
178 char tableName
[DB_MAX_TABLE_NAME_LEN
+1]; // Table name
179 char queryTableName
[DB_MAX_TABLE_NAME_LEN
+1]; // Query Table Name
180 int noCols
; // # of columns in the table
181 bool queryOnly
; // Query Only, no inserts, updates or deletes
183 // Column Definitions
184 wxDbColDef
*colDefs
; // Array of wxDbColDef structures
186 // Public member functions
187 wxDbTable(wxDb
*pwxDb
, const char *tblName
, const int nCols
,
188 const char *qryTblName
= NULL
, bool qryOnly
= !wxDB_QUERY_ONLY
, const char *tblPath
="");
189 virtual ~wxDbTable();
191 bool Open(bool checkPrivileges
=FALSE
);
192 bool CreateTable(bool attemptDrop
=TRUE
);
193 bool DropTable(void);
194 bool CreateIndex(const char * idxName
, bool unique
, int noIdxCols
, wxDbIdxDef
*pIdxDefs
, bool attemptDrop
=TRUE
);
195 bool DropIndex(const char * idxName
);
199 // The member variables returned by these accessors are all
200 // set when the wxDbTable instance is createand cannot be
201 // changed, hence there is no corresponding SetXxxx function
202 wxDb
*GetDb() { return pDb
; }
203 const char *GetTableName() { return tableName
; }
204 const char *GetQueryTableName() { return queryTableName
; }
205 const char *GetTablePath() { return tablePath
; }
207 int GetNumberOfColumns() { return noCols
; } // number of "defined" columns for this wxDbTable instance
210 const char *GetFromClause() { return from
; }
211 const char *GetOrderByClause() { return orderBy
; }
212 const char *GetWhereClause() { return where
; }
214 bool IsQueryOnly() { return queryOnly
; }
215 #if wxODBC_BACKWARD_COMPATABILITY
216 void SetFromClause(const char *From
) { from
= (char *)From
; }
217 void SetOrderByClause(const char *OrderBy
) { orderBy
= (char *)OrderBy
; }
218 void SetWhereClause(const char *Where
) { where
= (char *)Where
; }
220 void SetFromClause(const wxString
& From
) { from
= From
; }
221 void SetOrderByClause(const wxString
& OrderBy
) { orderBy
= OrderBy
; }
222 bool SetOrderByColNums(int first
, ...);
223 void SetWhereClause(const wxString
& Where
) { where
= Where
; }
224 void From(const wxString
& From
) { from
= From
; }
225 void OrderBy(const wxString
& OrderBy
) { orderBy
= OrderBy
; }
226 void Where(const wxString
& Where
) { where
= Where
; }
227 const char * Where() { return (const char *)where
.c_str(); }
228 const char * OrderBy() { return (const char *)orderBy
.c_str(); }
229 const char * From() { return (const char *)from
.c_str(); }
233 bool Update(const char *pSqlStmt
);
234 bool UpdateWhere(const char *pWhereClause
);
236 bool DeleteWhere(const char *pWhereClause
);
237 bool DeleteMatching(void);
238 virtual bool Query(bool forUpdate
= FALSE
, bool distinct
= FALSE
);
239 bool QueryBySqlStmt(const char *pSqlStmt
);
240 bool QueryMatching(bool forUpdate
= FALSE
, bool distinct
= FALSE
);
241 bool QueryOnKeyFields(bool forUpdate
= FALSE
, bool distinct
= FALSE
);
243 bool GetNext(void) { return(getRec(SQL_FETCH_NEXT
)); }
244 bool operator++(int) { return(getRec(SQL_FETCH_NEXT
)); }
246 /***** These four functions only work with wxDb instances that are defined *****
247 ***** as not being FwdOnlyCursors *****/
249 bool operator--(int);
253 bool IsCursorClosedOnCommit(void);
254 UWORD
GetRowNum(void);
256 void BuildSelectStmt(char *pSqlStmt
, int typeOfSelect
, bool distinct
);
257 void BuildDeleteStmt(char *pSqlStmt
, int typeOfDel
, const char *pWhereClause
= NULL
);
258 void BuildUpdateStmt(char *pSqlStmt
, int typeOfUpd
, const char *pWhereClause
= NULL
);
259 void BuildWhereClause(char *pWhereClause
, int typeOfWhere
, const char *qualTableName
= NULL
, bool useLikeComparison
=FALSE
);
260 #if wxODBC_BACKWARD_COMPATABILITY
261 // The following member functions are deprecated. You should use the BuildXxxxxStmt functions (above)
262 void GetSelectStmt(char *pSqlStmt
, int typeOfSelect
, bool distinct
)
263 { BuildSelectStmt(pSqlStmt
,typeOfSelect
,distinct
); }
264 void GetDeleteStmt(char *pSqlStmt
, int typeOfDel
, const char *pWhereClause
= NULL
)
265 { BuildDeleteStmt(pSqlStmt
,typeOfDel
,pWhereClause
); }
266 void GetUpdateStmt(char *pSqlStmt
, int typeOfUpd
, const char *pWhereClause
= NULL
)
267 { BuildUpdateStmt(pSqlStmt
,typeOfUpd
,pWhereClause
); }
268 void GetWhereClause(char *pWhereClause
, int typeOfWhere
,
269 const char *qualTableName
= NULL
, bool useLikeComparison
=FALSE
)
270 { BuildWhereClause(pWhereClause
,typeOfWhere
,qualTableName
,useLikeComparison
); }
272 bool CanSelectForUpdate(void);
273 bool CanUpdByROWID(void);
274 void ClearMemberVar(int colNo
, bool setToNull
=FALSE
);
275 void ClearMemberVars(bool setToNull
=FALSE
);
276 bool SetQueryTimeout(UDWORD nSeconds
);
278 wxDbColDef
*GetColDefs() { return colDefs
; }
279 void SetColDefs(int index
, const char *fieldName
, int dataType
, void *pData
, int cType
,
280 int size
, bool keyField
= FALSE
, bool upd
= TRUE
,
281 bool insAllow
= TRUE
, bool derivedCol
= FALSE
);
282 wxDbColDataPtr
*SetColDefs(wxDbColInf
*colInfs
, ULONG numCols
);
284 bool CloseCursor(HSTMT cursor
);
285 bool DeleteCursor(HSTMT
*hstmtDel
);
286 void SetCursor(HSTMT
*hstmtActivate
= (void **) wxDB_DEFAULT_CURSOR
);
287 HSTMT
GetCursor(void) { return(hstmt
); }
288 HSTMT
*GetNewCursor(bool setCursor
= FALSE
, bool bindColumns
= TRUE
);
289 #if wxODBC_BACKWARD_COMPATABILITY
290 // The following member function is deprecated. You should use the GetNewCursor
291 HSTMT
*NewCursor(bool setCursor
= FALSE
, bool bindColumns
= TRUE
) { return GetNewCursor(setCursor
,bindColumns
); }
294 ULONG
Count(const char *args
="*");
295 int DB_STATUS(void) { return(pDb
->DB_STATUS
); }
297 bool IsColNull(int colNo
);
298 bool SetColNull(int colNo
, bool set
=TRUE
);
299 bool SetColNull(const char *colName
, bool set
=TRUE
);
300 #if wxODBC_BACKWARD_COMPATABILITY
301 // The following member functions are deprecated. You should use the SetColNull()
302 bool SetNull(int colNo
, bool set
=TRUE
) { return (SetNull(colNo
,set
)); }
303 bool SetNull(const char *colName
, bool set
=TRUE
) { return (SetNull(colName
,set
)); }
306 ULONG
GetTableID() { return tableID
; }
312 // Change this to 0 to remove use of all deprecated functions
313 #if wxODBC_BACKWARD_COMPATABILITY
314 //#################################################################################
315 //############### DEPRECATED functions for backward compatability #################
316 //#################################################################################
318 // Backward compability. These will eventually go away
319 typedef wxDbTable wxTable
;
320 typedef wxDbIdxDef wxIdxDef
;
321 typedef wxDbIdxDef CidxDef
;
322 typedef wxDbColDef wxColDef
;
323 typedef wxDbColDef CcolDef
;
324 typedef wxDbColDataPtr wxColDataPtr
;
325 typedef wxDbColDataPtr CcolDataPtr
;
327 const int ROWID
= wxDB_ROWID_LEN
;
328 const int DEFAULT_CURSOR
= wxDB_DEFAULT_CURSOR
;
329 const bool QUERY_ONLY
= wxDB_QUERY_ONLY
;
330 const bool DISABLE_VIEW
= wxDB_DISABLE_VIEW
;