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