]> git.saurik.com Git - wxWidgets.git/blob - include/wx/db.h
Added defines to allow altered tab placement under gcc-2.95
[wxWidgets.git] / include / wx / db.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: db.h
3 // Purpose: Header file wxDB class. The wxDB class represents a connection
4 // to an ODBC data source. The wxDB class allows operations on the data
5 // source such as opening and closing the data source.
6 // Author: Doug Card
7 // Modified by:
8 // Mods: Dec, 1998: Added support for SQL statement logging and database
9 // cataloging
10 // Created: 9.96
11 // RCS-ID: $Id$
12 // Copyright: (c) 1996 Remstar International, Inc.
13 // Licence: wxWindows licence, plus:
14 // Notice: This class library and its intellectual design are free of charge for use,
15 // modification, enhancement, debugging under the following conditions:
16 // 1) These classes may only be used as part of the implementation of a
17 // wxWindows-based application
18 // 2) All enhancements and bug fixes are to be submitted back to the wxWindows
19 // user groups free of all charges for use with the wxWindows library.
20 // 3) These classes may not be distributed as part of any other class library,
21 // DLL, text (written or electronic), other than a complete distribution of
22 // the wxWindows GUI development toolkit.
23 //
24 ///////////////////////////////////////////////////////////////////////////////
25
26 /*
27 // SYNOPSIS START
28 // SYNOPSIS STOP
29 */
30
31 #ifndef DB_DOT_H
32 #define DB_DOT_H
33
34 #ifdef __GNUG__
35 #pragma interface "db.h"
36 #endif
37
38 #if defined(__WXMSW__) || defined(WIN32)
39 #include <windows.h>
40 #endif
41
42
43 #ifdef __WXGTK__
44
45 extern "C" {
46 #include "../../src/iodbc/isql.h"
47 #include "../../src/iodbc/isqlext.h"
48 typedef float SFLOAT;
49 typedef double SDOUBLE;
50 typedef unsigned int UINT;
51 #define ULONG UDWORD
52
53 }
54
55 #else
56
57 #define ODBCVER 0x0250
58 #include <sql.h>
59 #include <sqlext.h>
60
61 #endif
62
63 enum enumDummy {enumDum1};
64
65 #define SQL_C_BOOLEAN (sizeof(int) == 2 ? SQL_C_USHORT : SQL_C_ULONG)
66 #define SQL_C_ENUM (sizeof(enumDummy) == 2 ? SQL_C_USHORT : SQL_C_ULONG) //glt 2-21-97
67
68 // Database Globals
69 const int DB_TYPE_NAME_LEN = 40;
70 const int DB_MAX_STATEMENT_LEN = 2048;
71 const int DB_MAX_WHERE_CLAUSE_LEN = 1024;
72 const int DB_MAX_ERROR_MSG_LEN = 512;
73 const int DB_MAX_ERROR_HISTORY = 5;
74 const int DB_MAX_TABLE_NAME_LEN = 128;
75 const int DB_MAX_COLUMN_NAME_LEN = 128;
76
77 const int DB_DATA_TYPE_VARCHAR = 1;
78 const int DB_DATA_TYPE_INTEGER = 2;
79 const int DB_DATA_TYPE_FLOAT = 3;
80 const int DB_DATA_TYPE_DATE = 4;
81
82 const int DB_SELECT_KEYFIELDS = 1;
83 const int DB_SELECT_WHERE = 2;
84 const int DB_SELECT_MATCHING = 3;
85 const int DB_SELECT_STATEMENT = 4;
86
87 const int DB_UPD_KEYFIELDS = 1;
88 const int DB_UPD_WHERE = 2;
89
90 const int DB_DEL_KEYFIELDS = 1;
91 const int DB_DEL_WHERE = 2;
92 const int DB_DEL_MATCHING = 3;
93
94 const int DB_WHERE_KEYFIELDS = 1;
95 const int DB_WHERE_MATCHING = 2;
96
97 const int DB_CURSOR0 = 0;
98 const int DB_CURSOR1 = 1;
99 const int DB_CURSOR2 = 2;
100 //const int DB_CURSOR3 = 3;
101 //const int DB_CURSOR4 = 4;
102 //const int DB_CURSOR5 = 5;
103
104 const int DB_GRANT_SELECT = 1;
105 const int DB_GRANT_INSERT = 2;
106 const int DB_GRANT_UPDATE = 4;
107 const int DB_GRANT_DELETE = 8;
108 const int DB_GRANT_ALL = DB_GRANT_SELECT | DB_GRANT_INSERT | DB_GRANT_UPDATE | DB_GRANT_DELETE;
109
110 // ODBC Error codes (derived from ODBC SqlState codes)
111 enum ODBC_ERRORS
112 {
113 DB_FAILURE = 0,
114 DB_SUCCESS = 1,
115 DB_ERR_NOT_IN_USE,
116 DB_ERR_GENERAL_WARNING, // SqlState = '01000'
117 DB_ERR_DISCONNECT_ERROR, // SqlState = '01002'
118 DB_ERR_DATA_TRUNCATED, // SqlState = '01004'
119 DB_ERR_PRIV_NOT_REVOKED, // SqlState = '01006'
120 DB_ERR_INVALID_CONN_STR_ATTR, // SqlState = '01S00'
121 DB_ERR_ERROR_IN_ROW, // SqlState = '01S01'
122 DB_ERR_OPTION_VALUE_CHANGED, // SqlState = '01S02'
123 DB_ERR_NO_ROWS_UPD_OR_DEL, // SqlState = '01S03'
124 DB_ERR_MULTI_ROWS_UPD_OR_DEL, // SqlState = '01S04'
125 DB_ERR_WRONG_NO_OF_PARAMS, // SqlState = '07001'
126 DB_ERR_DATA_TYPE_ATTR_VIOL, // SqlState = '07006'
127 DB_ERR_UNABLE_TO_CONNECT, // SqlState = '08001'
128 DB_ERR_CONNECTION_IN_USE, // SqlState = '08002'
129 DB_ERR_CONNECTION_NOT_OPEN, // SqlState = '08003'
130 DB_ERR_REJECTED_CONNECTION, // SqlState = '08004'
131 DB_ERR_CONN_FAIL_IN_TRANS, // SqlState = '08007'
132 DB_ERR_COMM_LINK_FAILURE, // SqlState = '08S01'
133 DB_ERR_INSERT_VALUE_LIST_MISMATCH, // SqlState = '21S01'
134 DB_ERR_DERIVED_TABLE_MISMATCH, // SqlState = '21S02'
135 DB_ERR_STRING_RIGHT_TRUNC, // SqlState = '22001'
136 DB_ERR_NUMERIC_VALUE_OUT_OF_RNG, // SqlState = '22003'
137 DB_ERR_ERROR_IN_ASSIGNMENT, // SqlState = '22005'
138 DB_ERR_DATETIME_FLD_OVERFLOW, // SqlState = '22008'
139 DB_ERR_DIVIDE_BY_ZERO, // SqlState = '22012'
140 DB_ERR_STR_DATA_LENGTH_MISMATCH, // SqlState = '22026'
141 DB_ERR_INTEGRITY_CONSTRAINT_VIOL, // SqlState = '23000'
142 DB_ERR_INVALID_CURSOR_STATE, // SqlState = '24000'
143 DB_ERR_INVALID_TRANS_STATE, // SqlState = '25000'
144 DB_ERR_INVALID_AUTH_SPEC, // SqlState = '28000'
145 DB_ERR_INVALID_CURSOR_NAME, // SqlState = '34000'
146 DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL, // SqlState = '37000'
147 DB_ERR_DUPLICATE_CURSOR_NAME, // SqlState = '3C000'
148 DB_ERR_SERIALIZATION_FAILURE, // SqlState = '40001'
149 DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL2, // SqlState = '42000'
150 DB_ERR_OPERATION_ABORTED, // SqlState = '70100'
151 DB_ERR_UNSUPPORTED_FUNCTION, // SqlState = 'IM001'
152 DB_ERR_NO_DATA_SOURCE, // SqlState = 'IM002'
153 DB_ERR_DRIVER_LOAD_ERROR, // SqlState = 'IM003'
154 DB_ERR_SQLALLOCENV_FAILED, // SqlState = 'IM004'
155 DB_ERR_SQLALLOCCONNECT_FAILED, // SqlState = 'IM005'
156 DB_ERR_SQLSETCONNECTOPTION_FAILED, // SqlState = 'IM006'
157 DB_ERR_NO_DATA_SOURCE_DLG_PROHIB, // SqlState = 'IM007'
158 DB_ERR_DIALOG_FAILED, // SqlState = 'IM008'
159 DB_ERR_UNABLE_TO_LOAD_TRANSLATION_DLL, // SqlState = 'IM009'
160 DB_ERR_DATA_SOURCE_NAME_TOO_LONG, // SqlState = 'IM010'
161 DB_ERR_DRIVER_NAME_TOO_LONG, // SqlState = 'IM011'
162 DB_ERR_DRIVER_KEYWORD_SYNTAX_ERROR, // SqlState = 'IM012'
163 DB_ERR_TRACE_FILE_ERROR, // SqlState = 'IM013'
164 DB_ERR_TABLE_OR_VIEW_ALREADY_EXISTS, // SqlState = 'S0001'
165 DB_ERR_TABLE_NOT_FOUND, // SqlState = 'S0002'
166 DB_ERR_INDEX_ALREADY_EXISTS, // SqlState = 'S0011'
167 DB_ERR_INDEX_NOT_FOUND, // SqlState = 'S0012'
168 DB_ERR_COLUMN_ALREADY_EXISTS, // SqlState = 'S0021'
169 DB_ERR_COLUMN_NOT_FOUND, // SqlState = 'S0022'
170 DB_ERR_NO_DEFAULT_FOR_COLUMN, // SqlState = 'S0023'
171 DB_ERR_GENERAL_ERROR, // SqlState = 'S1000'
172 DB_ERR_MEMORY_ALLOCATION_FAILURE, // SqlState = 'S1001'
173 DB_ERR_INVALID_COLUMN_NUMBER, // SqlState = 'S1002'
174 DB_ERR_PROGRAM_TYPE_OUT_OF_RANGE, // SqlState = 'S1003'
175 DB_ERR_SQL_DATA_TYPE_OUT_OF_RANGE, // SqlState = 'S1004'
176 DB_ERR_OPERATION_CANCELLED, // SqlState = 'S1008'
177 DB_ERR_INVALID_ARGUMENT_VALUE, // SqlState = 'S1009'
178 DB_ERR_FUNCTION_SEQUENCE_ERROR, // SqlState = 'S1010'
179 DB_ERR_OPERATION_INVALID_AT_THIS_TIME, // SqlState = 'S1011'
180 DB_ERR_INVALID_TRANS_OPERATION_CODE, // SqlState = 'S1012'
181 DB_ERR_NO_CURSOR_NAME_AVAIL, // SqlState = 'S1015'
182 DB_ERR_INVALID_STR_OR_BUF_LEN, // SqlState = 'S1090'
183 DB_ERR_DESCRIPTOR_TYPE_OUT_OF_RANGE, // SqlState = 'S1091'
184 DB_ERR_OPTION_TYPE_OUT_OF_RANGE, // SqlState = 'S1092'
185 DB_ERR_INVALID_PARAM_NO, // SqlState = 'S1093'
186 DB_ERR_INVALID_SCALE_VALUE, // SqlState = 'S1094'
187 DB_ERR_FUNCTION_TYPE_OUT_OF_RANGE, // SqlState = 'S1095'
188 DB_ERR_INF_TYPE_OUT_OF_RANGE, // SqlState = 'S1096'
189 DB_ERR_COLUMN_TYPE_OUT_OF_RANGE, // SqlState = 'S1097'
190 DB_ERR_SCOPE_TYPE_OUT_OF_RANGE, // SqlState = 'S1098'
191 DB_ERR_NULLABLE_TYPE_OUT_OF_RANGE, // SqlState = 'S1099'
192 DB_ERR_UNIQUENESS_OPTION_TYPE_OUT_OF_RANGE, // SqlState = 'S1100'
193 DB_ERR_ACCURACY_OPTION_TYPE_OUT_OF_RANGE, // SqlState = 'S1101'
194 DB_ERR_DIRECTION_OPTION_OUT_OF_RANGE, // SqlState = 'S1103'
195 DB_ERR_INVALID_PRECISION_VALUE, // SqlState = 'S1104'
196 DB_ERR_INVALID_PARAM_TYPE, // SqlState = 'S1105'
197 DB_ERR_FETCH_TYPE_OUT_OF_RANGE, // SqlState = 'S1106'
198 DB_ERR_ROW_VALUE_OUT_OF_RANGE, // SqlState = 'S1107'
199 DB_ERR_CONCURRENCY_OPTION_OUT_OF_RANGE, // SqlState = 'S1108'
200 DB_ERR_INVALID_CURSOR_POSITION, // SqlState = 'S1109'
201 DB_ERR_INVALID_DRIVER_COMPLETION, // SqlState = 'S1110'
202 DB_ERR_INVALID_BOOKMARK_VALUE, // SqlState = 'S1111'
203 DB_ERR_DRIVER_NOT_CAPABLE, // SqlState = 'S1C00'
204 DB_ERR_TIMEOUT_EXPIRED // SqlState = 'S1T00'
205 };
206
207 struct DbStuff
208 {
209 HENV Henv;
210 char Dsn[SQL_MAX_DSN_LENGTH+1]; // Data Source Name
211 char Uid[20]; // User ID
212 char AuthStr[20]; // Authorization string (password)
213 };
214
215 typedef struct
216 {
217 char TypeName[DB_TYPE_NAME_LEN];
218 int FsqlType;
219 long Precision;
220 short CaseSensitive;
221 // short MinimumScale;
222 short MaximumScale;
223 } SqlTypeInfo;
224
225 class WXDLLEXPORT CcolInf
226 {
227 public:
228 char tableName[DB_MAX_TABLE_NAME_LEN+1];
229 char colName[DB_MAX_COLUMN_NAME_LEN+1];
230 int sqlDataType;
231 };
232
233 enum sqlLog
234 {
235 sqlLogOFF,
236 sqlLogON
237 };
238
239 class WXDLLEXPORT wxDB
240 {
241 private:
242
243 // Private data
244 bool dbIsOpen;
245 char *dsn; // Data source name
246 char *uid; // User ID
247 char *authStr; // Authorization string (password)
248 FILE *fpSqlLog; // Sql Log file pointer
249 enum sqlLog sqlLogState; // On or Off
250
251 // Private member functions
252 bool getDbInfo(void);
253 bool getDataTypeInfo(SWORD fSqlType, SqlTypeInfo &structSQLTypeInfo);
254 bool setConnectionOptions(void);
255 void logError(char *errMsg, char *SQLState);
256
257 public:
258
259 // The following structure contains database information gathered from the
260 // datasource when the datasource is first opened.
261 struct
262 {
263 char dbmsName[40]; // Name of the dbms product
264 char dbmsVer[40]; // Version # of the dbms product
265 char driverName[40]; // Driver name
266 char odbcVer[60]; // ODBC version of the driver
267 char drvMgrOdbcVer[60]; // ODBC version of the driver manager
268 char driverVer[60]; // Driver version
269 char serverName[80]; // Server Name, typically a connect string
270 char databaseName[128]; // Database filename
271 char outerJoins[2]; // Indicates whether the data source supports outer joins
272 char procedureSupport[2]; // Indicates whether the data source supports stored procedures
273 UWORD maxConnections; // Maximum # of connections the data source supports
274 UWORD maxStmts; // Maximum # of HSTMTs per HDBC
275 UWORD apiConfLvl; // ODBC API conformance level
276 UWORD cliConfLvl; // Indicates whether the data source is SAG compliant
277 UWORD sqlConfLvl; // SQL conformance level
278 UWORD cursorCommitBehavior; // Indicates how cursors are affected by a db commit
279 UWORD cursorRollbackBehavior; // Indicates how cursors are affected by a db rollback
280 UWORD supportNotNullClause; // Indicates if data source supports NOT NULL clause
281 char supportIEF[2]; // Integrity Enhancement Facility (Referential Integrity)
282 UDWORD txnIsolation; // Default transaction isolation level supported by the driver
283 UDWORD txnIsolationOptions; // Transaction isolation level options available
284 UDWORD fetchDirections; // Fetch directions supported
285 UDWORD lockTypes; // Lock types supported in SQLSetPos
286 UDWORD posOperations; // Position operations supported in SQLSetPos
287 UDWORD posStmts; // Position statements supported
288 UDWORD scrollConcurrency; // Concurrency control options supported for scrollable cursors
289 UDWORD scrollOptions; // Scroll Options supported for scrollable cursors
290 UDWORD staticSensitivity; // Indicates if additions, deletions and updates can be detected
291 UWORD txnCapable; // Indicates if the data source supports transactions
292 UDWORD loginTimeout; // Number seconds to wait for a login request
293 } dbInf;
294
295 // ODBC handles
296 HENV henv; // ODBC Environment handle
297 HDBC hdbc; // ODBC DB Connection handle
298 HSTMT hstmt; // ODBC Statement handle
299
300 // ODBC Error Inf.
301 char sqlState[20];
302 SDWORD nativeError;
303 char errorMsg[SQL_MAX_MESSAGE_LENGTH];
304 SWORD cbErrorMsg;
305 char errorList[DB_MAX_ERROR_HISTORY][DB_MAX_ERROR_MSG_LEN];
306 int DB_STATUS;
307
308 //Error reporting mode
309 bool silent;
310
311 // Inf. about logical data types VARCHAR, INTEGER, FLOAT and DATE.
312 // This inf. is obtained from the ODBC driver by use of the
313 // SQLGetTypeInfo() function. The key piece of inf. is the
314 // type name the data source uses for each logical data type.
315 // e.g. VARCHAR; Oracle calls it VARCHAR2.
316 SqlTypeInfo typeInfVarchar, typeInfInteger, typeInfFloat, typeInfDate;
317
318 // Public member functions
319 wxDB(HENV &aHenv);
320 bool Open(char *Dsn, char *Uid, char *AuthStr); // Data Source Name, User ID, Password
321 void Close(void);
322 bool CommitTrans(void);
323 bool RollbackTrans(void);
324 bool DispAllErrors(HENV aHenv, HDBC aHdbc = SQL_NULL_HDBC, HSTMT aHstmt = SQL_NULL_HSTMT);
325 bool GetNextError(HENV aHenv, HDBC aHdbc = SQL_NULL_HDBC, HSTMT aHstmt = SQL_NULL_HSTMT);
326 void DispNextError(void);
327 bool CreateView(char *viewName, char *colList, char *pSqlStmt);
328 bool ExecSql(char *pSqlStmt);
329 bool Grant(int privileges, char *tableName, char *userList = "PUBLIC");
330 int TranslateSqlState(char *SQLState);
331 bool Catalog(char *userID, char *fileName = "Catalog.txt");
332 CcolInf *GetColumns(char *tableName[]);
333 char *GetDatabaseName(void) {return dbInf.dbmsName;}
334 char *GetDataSource(void) {return dsn;}
335 char *GetUsername(void) {return uid;}
336 char *GetPassword(void) {return authStr;}
337 bool IsOpen(void) {return dbIsOpen;}
338 HENV GetHENV(void) {return henv;}
339 HDBC GetHDBC(void) {return hdbc;}
340 HSTMT GetHSTMT(void) {return hstmt;}
341 bool TableExists(char *tableName); // Table name can refer to a table, view, alias or synonym
342 void LogError(char *errMsg, char *SQLState = 0) {logError(errMsg, SQLState);}
343 bool SqlLog(enum sqlLog state, char *filename = "sqllog.txt", bool append = FALSE);
344 bool WriteSqlLog(char *logMsg);
345
346 }; // wxDB
347
348 // This structure forms a node in a linked list. The linked list of "DbList" objects
349 // keeps track of allocated database connections. This allows the application to
350 // open more than one database connection through ODBC for multiple transaction support
351 // or for multiple database support.
352
353 struct DbList
354 {
355 DbList *PtrPrev; // Pointer to previous item in the list
356 char Dsn[SQL_MAX_DSN_LENGTH+1]; // Data Source Name
357 wxDB *PtrDb; // Pointer to the wxDB object
358 bool Free; // Is item free or in use?
359 DbList *PtrNext; // Pointer to next item in the list
360 };
361
362 // The following routines allow a user to get new database connections, free them
363 // for other code segments to use, or close all of them when the application has
364 // completed.
365
366 wxDB* WXDLLEXPORT GetDbConnection(DbStuff *pDbStuff);
367 bool WXDLLEXPORT FreeDbConnection(wxDB *pDb);
368 void WXDLLEXPORT CloseDbConnections(void);
369 int WXDLLEXPORT NumberDbConnectionsInUse(void);
370
371 // This routine allows you to query a driver manager
372 // for a list of available datasources. Call this routine
373 // the first time using SQL_FETCH_FIRST. Continue to call it
374 // using SQL_FETCH_NEXT until you've exhausted the list.
375 bool WXDLLEXPORT GetDataSource(HENV henv, char *Dsn, SWORD DsnMax, char *DsDesc, SWORD DsDescMax,
376 UWORD direction = SQL_FETCH_NEXT);
377
378 #endif