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