]>
Commit | Line | Data |
---|---|---|
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: George Tasker | |
8 | // Bart Jourquin | |
9 | // Mark Johnson, wxWindows@mj10777.de | |
10 | // Mods: Dec, 1998: | |
11 | // -Added support for SQL statement logging and database cataloging | |
12 | // April, 1999 | |
13 | // -Added QUERY_ONLY mode support to reduce default number of cursors | |
14 | // -Added additional SQL logging code | |
15 | // -Added DEBUG-ONLY tracking of Ctable objects to detect orphaned DB connections | |
16 | // -Set ODBC option to only read committed writes to the DB so all | |
17 | // databases operate the same in that respect | |
18 | // | |
19 | // Created: 9.96 | |
20 | // RCS-ID: $Id$ | |
21 | // Copyright: (c) 1996 Remstar International, Inc. | |
22 | // Licence: wxWindows licence, plus: | |
23 | // Notice: This class library and its intellectual design are free of charge for use, | |
24 | // modification, enhancement, debugging under the following conditions: | |
25 | // 1) These classes may only be used as part of the implementation of a | |
26 | // wxWindows-based application | |
27 | // 2) All enhancements and bug fixes are to be submitted back to the wxWindows | |
28 | // user groups free of all charges for use with the wxWindows library. | |
29 | // 3) These classes may not be distributed as part of any other class library, | |
30 | // DLL, text (written or electronic), other than a complete distribution of | |
31 | // the wxWindows GUI development toolkit. | |
32 | // | |
33 | /////////////////////////////////////////////////////////////////////////////// | |
34 | ||
35 | /* | |
36 | // SYNOPSIS START | |
37 | // SYNOPSIS STOP | |
38 | */ | |
39 | ||
40 | #ifndef DB_DOT_H | |
41 | #define DB_DOT_H | |
42 | ||
43 | ||
44 | // BJO 20000503: introduce new GetColumns members which are more database independant and | |
45 | // return columns in the order they were created | |
46 | #define OLD_GETCOLUMNS 1 | |
47 | #define EXPERIMENTAL_WXDB_FUNCTIONS 0 | |
48 | ||
49 | // Use this line for wxWindows v1.x | |
50 | //#include "wx_ver.h" | |
51 | // Use this line for wxWindows v2.x | |
52 | #include "wx/version.h" | |
53 | ||
54 | #if wxMAJOR_VERSION == 2 | |
55 | #ifdef __GNUG__ | |
56 | #pragma interface "db.h" | |
57 | #endif | |
58 | #endif | |
59 | ||
60 | #include "wx/setup.h" | |
61 | ||
62 | #if wxMAJOR_VERSION == 2 | |
63 | extern "C" { | |
64 | #include "wx/isql.h" | |
65 | #include "wx/isqlext.h" | |
66 | // If you use the wxCreateDataSource() function with MSW/VC6, | |
67 | // you cannot use the iODBC headers, you must use the VC headers, | |
68 | // plus the odbcinst.h header | |
69 | //#include "sql.h" | |
70 | //#include "sqlext.h" | |
71 | //#include "odbcinst.h" | |
72 | } | |
73 | #else // version == 1 | |
74 | extern "C" { | |
75 | #include "iodbc.h" | |
76 | #include "isqlext.h" | |
77 | } | |
78 | #endif | |
79 | ||
80 | typedef float SFLOAT; | |
81 | typedef double SDOUBLE; | |
82 | typedef unsigned int UINT; | |
83 | #define ULONG UDWORD | |
84 | ||
85 | #ifndef wxODBC_FWD_ONLY_CURSORS | |
86 | #define wxODBC_FWD_ONLY_CURSORS 1 | |
87 | #endif | |
88 | ||
89 | enum enumDummy {enumDum1}; | |
90 | ||
91 | #define SQL_C_BOOLEAN(datatype) (sizeof(datatype) == 1 ? SQL_C_UTINYINT : (sizeof(datatype) == 2 ? SQL_C_USHORT : SQL_C_ULONG)) | |
92 | // #define SQL_C_BOOLEAN (sizeof(Bool) == 2 ? SQL_C_USHORT : SQL_C_ULONG) | |
93 | ||
94 | #define SQL_C_ENUM (sizeof(enumDummy) == 2 ? SQL_C_USHORT : SQL_C_ULONG) | |
95 | ||
96 | #ifndef TRUE | |
97 | #define TRUE 1 | |
98 | #endif | |
99 | ||
100 | #ifndef FALSE | |
101 | #define FALSE 0 | |
102 | #endif | |
103 | ||
104 | const int wxDB_PATH_MAX = 254; | |
105 | ||
106 | extern char const *SQL_LOG_FILENAME; | |
107 | extern char const *SQL_CATALOG_FILENAME; | |
108 | ||
109 | ||
110 | // Database Globals | |
111 | const int DB_TYPE_NAME_LEN = 40; | |
112 | const int DB_MAX_STATEMENT_LEN = 2048; | |
113 | const int DB_MAX_WHERE_CLAUSE_LEN = 1024; | |
114 | const int DB_MAX_ERROR_MSG_LEN = 512; | |
115 | const int DB_MAX_ERROR_HISTORY = 5; | |
116 | const int DB_MAX_TABLE_NAME_LEN = 128; | |
117 | const int DB_MAX_COLUMN_NAME_LEN = 128; | |
118 | ||
119 | const int DB_DATA_TYPE_VARCHAR = 1; | |
120 | const int DB_DATA_TYPE_INTEGER = 2; | |
121 | const int DB_DATA_TYPE_FLOAT = 3; | |
122 | const int DB_DATA_TYPE_DATE = 4; | |
123 | ||
124 | const int DB_SELECT_KEYFIELDS = 1; | |
125 | const int DB_SELECT_WHERE = 2; | |
126 | const int DB_SELECT_MATCHING = 3; | |
127 | const int DB_SELECT_STATEMENT = 4; | |
128 | ||
129 | const int DB_UPD_KEYFIELDS = 1; | |
130 | const int DB_UPD_WHERE = 2; | |
131 | ||
132 | const int DB_DEL_KEYFIELDS = 1; | |
133 | const int DB_DEL_WHERE = 2; | |
134 | const int DB_DEL_MATCHING = 3; | |
135 | ||
136 | const int DB_WHERE_KEYFIELDS = 1; | |
137 | const int DB_WHERE_MATCHING = 2; | |
138 | ||
139 | const int DB_GRANT_SELECT = 1; | |
140 | const int DB_GRANT_INSERT = 2; | |
141 | const int DB_GRANT_UPDATE = 4; | |
142 | const int DB_GRANT_DELETE = 8; | |
143 | const int DB_GRANT_ALL = DB_GRANT_SELECT | DB_GRANT_INSERT | DB_GRANT_UPDATE | DB_GRANT_DELETE; | |
144 | ||
145 | // ODBC Error codes (derived from ODBC SqlState codes) | |
146 | enum wxODBC_ERRORS | |
147 | { | |
148 | DB_FAILURE = 0, | |
149 | DB_SUCCESS = 1, | |
150 | DB_ERR_NOT_IN_USE, | |
151 | DB_ERR_GENERAL_WARNING, // SqlState = '01000' | |
152 | DB_ERR_DISCONNECT_ERROR, // SqlState = '01002' | |
153 | DB_ERR_DATA_TRUNCATED, // SqlState = '01004' | |
154 | DB_ERR_PRIV_NOT_REVOKED, // SqlState = '01006' | |
155 | DB_ERR_INVALID_CONN_STR_ATTR, // SqlState = '01S00' | |
156 | DB_ERR_ERROR_IN_ROW, // SqlState = '01S01' | |
157 | DB_ERR_OPTION_VALUE_CHANGED, // SqlState = '01S02' | |
158 | DB_ERR_NO_ROWS_UPD_OR_DEL, // SqlState = '01S03' | |
159 | DB_ERR_MULTI_ROWS_UPD_OR_DEL, // SqlState = '01S04' | |
160 | DB_ERR_WRONG_NO_OF_PARAMS, // SqlState = '07001' | |
161 | DB_ERR_DATA_TYPE_ATTR_VIOL, // SqlState = '07006' | |
162 | DB_ERR_UNABLE_TO_CONNECT, // SqlState = '08001' | |
163 | DB_ERR_CONNECTION_IN_USE, // SqlState = '08002' | |
164 | DB_ERR_CONNECTION_NOT_OPEN, // SqlState = '08003' | |
165 | DB_ERR_REJECTED_CONNECTION, // SqlState = '08004' | |
166 | DB_ERR_CONN_FAIL_IN_TRANS, // SqlState = '08007' | |
167 | DB_ERR_COMM_LINK_FAILURE, // SqlState = '08S01' | |
168 | DB_ERR_INSERT_VALUE_LIST_MISMATCH, // SqlState = '21S01' | |
169 | DB_ERR_DERIVED_TABLE_MISMATCH, // SqlState = '21S02' | |
170 | DB_ERR_STRING_RIGHT_TRUNC, // SqlState = '22001' | |
171 | DB_ERR_NUMERIC_VALUE_OUT_OF_RNG, // SqlState = '22003' | |
172 | DB_ERR_ERROR_IN_ASSIGNMENT, // SqlState = '22005' | |
173 | DB_ERR_DATETIME_FLD_OVERFLOW, // SqlState = '22008' | |
174 | DB_ERR_DIVIDE_BY_ZERO, // SqlState = '22012' | |
175 | DB_ERR_STR_DATA_LENGTH_MISMATCH, // SqlState = '22026' | |
176 | DB_ERR_INTEGRITY_CONSTRAINT_VIOL, // SqlState = '23000' | |
177 | DB_ERR_INVALID_CURSOR_STATE, // SqlState = '24000' | |
178 | DB_ERR_INVALID_TRANS_STATE, // SqlState = '25000' | |
179 | DB_ERR_INVALID_AUTH_SPEC, // SqlState = '28000' | |
180 | DB_ERR_INVALID_CURSOR_NAME, // SqlState = '34000' | |
181 | DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL, // SqlState = '37000' | |
182 | DB_ERR_DUPLICATE_CURSOR_NAME, // SqlState = '3C000' | |
183 | DB_ERR_SERIALIZATION_FAILURE, // SqlState = '40001' | |
184 | DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL2, // SqlState = '42000' | |
185 | DB_ERR_OPERATION_ABORTED, // SqlState = '70100' | |
186 | DB_ERR_UNSUPPORTED_FUNCTION, // SqlState = 'IM001' | |
187 | DB_ERR_NO_DATA_SOURCE, // SqlState = 'IM002' | |
188 | DB_ERR_DRIVER_LOAD_ERROR, // SqlState = 'IM003' | |
189 | DB_ERR_SQLALLOCENV_FAILED, // SqlState = 'IM004' | |
190 | DB_ERR_SQLALLOCCONNECT_FAILED, // SqlState = 'IM005' | |
191 | DB_ERR_SQLSETCONNECTOPTION_FAILED, // SqlState = 'IM006' | |
192 | DB_ERR_NO_DATA_SOURCE_DLG_PROHIB, // SqlState = 'IM007' | |
193 | DB_ERR_DIALOG_FAILED, // SqlState = 'IM008' | |
194 | DB_ERR_UNABLE_TO_LOAD_TRANSLATION_DLL, // SqlState = 'IM009' | |
195 | DB_ERR_DATA_SOURCE_NAME_TOO_LONG, // SqlState = 'IM010' | |
196 | DB_ERR_DRIVER_NAME_TOO_LONG, // SqlState = 'IM011' | |
197 | DB_ERR_DRIVER_KEYWORD_SYNTAX_ERROR, // SqlState = 'IM012' | |
198 | DB_ERR_TRACE_FILE_ERROR, // SqlState = 'IM013' | |
199 | DB_ERR_TABLE_OR_VIEW_ALREADY_EXISTS, // SqlState = 'S0001' | |
200 | DB_ERR_TABLE_NOT_FOUND, // SqlState = 'S0002' | |
201 | DB_ERR_INDEX_ALREADY_EXISTS, // SqlState = 'S0011' | |
202 | DB_ERR_INDEX_NOT_FOUND, // SqlState = 'S0012' | |
203 | DB_ERR_COLUMN_ALREADY_EXISTS, // SqlState = 'S0021' | |
204 | DB_ERR_COLUMN_NOT_FOUND, // SqlState = 'S0022' | |
205 | DB_ERR_NO_DEFAULT_FOR_COLUMN, // SqlState = 'S0023' | |
206 | DB_ERR_GENERAL_ERROR, // SqlState = 'S1000' | |
207 | DB_ERR_MEMORY_ALLOCATION_FAILURE, // SqlState = 'S1001' | |
208 | DB_ERR_INVALID_COLUMN_NUMBER, // SqlState = 'S1002' | |
209 | DB_ERR_PROGRAM_TYPE_OUT_OF_RANGE, // SqlState = 'S1003' | |
210 | DB_ERR_SQL_DATA_TYPE_OUT_OF_RANGE, // SqlState = 'S1004' | |
211 | DB_ERR_OPERATION_CANCELLED, // SqlState = 'S1008' | |
212 | DB_ERR_INVALID_ARGUMENT_VALUE, // SqlState = 'S1009' | |
213 | DB_ERR_FUNCTION_SEQUENCE_ERROR, // SqlState = 'S1010' | |
214 | DB_ERR_OPERATION_INVALID_AT_THIS_TIME, // SqlState = 'S1011' | |
215 | DB_ERR_INVALID_TRANS_OPERATION_CODE, // SqlState = 'S1012' | |
216 | DB_ERR_NO_CURSOR_NAME_AVAIL, // SqlState = 'S1015' | |
217 | DB_ERR_INVALID_STR_OR_BUF_LEN, // SqlState = 'S1090' | |
218 | DB_ERR_DESCRIPTOR_TYPE_OUT_OF_RANGE, // SqlState = 'S1091' | |
219 | DB_ERR_OPTION_TYPE_OUT_OF_RANGE, // SqlState = 'S1092' | |
220 | DB_ERR_INVALID_PARAM_NO, // SqlState = 'S1093' | |
221 | DB_ERR_INVALID_SCALE_VALUE, // SqlState = 'S1094' | |
222 | DB_ERR_FUNCTION_TYPE_OUT_OF_RANGE, // SqlState = 'S1095' | |
223 | DB_ERR_INF_TYPE_OUT_OF_RANGE, // SqlState = 'S1096' | |
224 | DB_ERR_COLUMN_TYPE_OUT_OF_RANGE, // SqlState = 'S1097' | |
225 | DB_ERR_SCOPE_TYPE_OUT_OF_RANGE, // SqlState = 'S1098' | |
226 | DB_ERR_NULLABLE_TYPE_OUT_OF_RANGE, // SqlState = 'S1099' | |
227 | DB_ERR_UNIQUENESS_OPTION_TYPE_OUT_OF_RANGE, // SqlState = 'S1100' | |
228 | DB_ERR_ACCURACY_OPTION_TYPE_OUT_OF_RANGE, // SqlState = 'S1101' | |
229 | DB_ERR_DIRECTION_OPTION_OUT_OF_RANGE, // SqlState = 'S1103' | |
230 | DB_ERR_INVALID_PRECISION_VALUE, // SqlState = 'S1104' | |
231 | DB_ERR_INVALID_PARAM_TYPE, // SqlState = 'S1105' | |
232 | DB_ERR_FETCH_TYPE_OUT_OF_RANGE, // SqlState = 'S1106' | |
233 | DB_ERR_ROW_VALUE_OUT_OF_RANGE, // SqlState = 'S1107' | |
234 | DB_ERR_CONCURRENCY_OPTION_OUT_OF_RANGE, // SqlState = 'S1108' | |
235 | DB_ERR_INVALID_CURSOR_POSITION, // SqlState = 'S1109' | |
236 | DB_ERR_INVALID_DRIVER_COMPLETION, // SqlState = 'S1110' | |
237 | DB_ERR_INVALID_BOOKMARK_VALUE, // SqlState = 'S1111' | |
238 | DB_ERR_DRIVER_NOT_CAPABLE, // SqlState = 'S1C00' | |
239 | DB_ERR_TIMEOUT_EXPIRED // SqlState = 'S1T00' | |
240 | }; | |
241 | ||
242 | ||
243 | struct wxDbConnectInf | |
244 | { | |
245 | HENV Henv; | |
246 | char Dsn[SQL_MAX_DSN_LENGTH+1]; // Data Source Name | |
247 | char Uid[20+1]; // User ID | |
248 | char AuthStr[20+1]; // Authorization string (password) | |
249 | ||
250 | char description[SQL_MAX_DSN_LENGTH+1]; // Not sure what the max length is | |
251 | char fileType[SQL_MAX_DSN_LENGTH+1]; // Not sure what the max length is | |
252 | ||
253 | // Optionals needed for some databases like dBase | |
254 | char defaultDir[wxDB_PATH_MAX]; // Directory that db file resides in | |
255 | }; | |
256 | ||
257 | struct wxDbSqlTypeInfo | |
258 | { | |
259 | char TypeName[DB_TYPE_NAME_LEN]; | |
260 | int FsqlType; | |
261 | long Precision; | |
262 | short CaseSensitive; | |
263 | // short MinimumScale; | |
264 | short MaximumScale; | |
265 | }; | |
266 | ||
267 | ||
268 | class WXDLLEXPORT wxDbColFor | |
269 | { | |
270 | public: | |
271 | wxString s_Field; // Formated String for Output | |
272 | wxString s_Format[7]; // Formated Objects - TIMESTAMP has the biggest (7) | |
273 | wxString s_Amount[7]; // Formated Objects - amount of things that can be formatted | |
274 | int i_Amount[7]; // Formated Objects - TT MM YYYY HH MM SS m | |
275 | int i_Nation; // 0 = timestamp , 1=EU, 2=UK, 3=International, 4=US | |
276 | int i_dbDataType; // conversion of the 'sqlDataType' to the generic data type used by these classes | |
277 | SWORD i_sqlDataType; | |
278 | ||
279 | wxDbColFor(); | |
280 | ~wxDbColFor(); | |
281 | ||
282 | int Format(int Nation, int dbDataType, SWORD sqlDataType, short columnSize, short decimalDigits); | |
283 | }; | |
284 | ||
285 | ||
286 | class WXDLLEXPORT wxDbColInf | |
287 | { | |
288 | public: | |
289 | char catalog[128+1]; | |
290 | char schema[128+1]; | |
291 | char tableName[DB_MAX_TABLE_NAME_LEN+1]; | |
292 | char colName[DB_MAX_COLUMN_NAME_LEN+1]; | |
293 | SWORD sqlDataType; | |
294 | char typeName[128+1]; | |
295 | SWORD columnSize; | |
296 | SWORD bufferLength; | |
297 | short decimalDigits; | |
298 | short numPrecRadix; | |
299 | short nullable; | |
300 | char remarks[254+1]; | |
301 | int dbDataType; // conversion of the 'sqlDataType' to the generic data type used by these classes | |
302 | // mj10777.19991224 : new | |
303 | int PkCol; // Primary key column 0=No; 1= First Key, 2 = Second Key etc. | |
304 | char PkTableName[DB_MAX_TABLE_NAME_LEN+1]; // Tables that use this PKey as a FKey | |
305 | int FkCol; // Foreign key column 0=No; 1= First Key, 2 = Second Key etc. | |
306 | char FkTableName[DB_MAX_TABLE_NAME_LEN+1]; // Foreign key table name | |
307 | wxDbColFor *pColFor; // How should this columns be formatted | |
308 | ||
309 | wxDbColInf(); | |
310 | ~wxDbColInf(); | |
311 | }; | |
312 | ||
313 | ||
314 | ||
315 | ||
316 | class WXDLLEXPORT wxDbTableInf // Description of a Table | |
317 | { | |
318 | public: | |
319 | char tableName[DB_MAX_TABLE_NAME_LEN+1]; | |
320 | char tableType[254+1]; // "TABLE" or "SYSTEM TABLE" etc. | |
321 | char tableRemarks[254+1]; | |
322 | int numCols; // How many Columns does this Table have: GetColumnCount(..); | |
323 | wxDbColInf *pColInf; // pColInf = NULL ; User can later call GetColumns(..); | |
324 | wxDbTableInf(); | |
325 | ~wxDbTableInf(); | |
326 | }; | |
327 | ||
328 | ||
329 | class WXDLLEXPORT wxDbInf // Description of a Database | |
330 | { | |
331 | public: | |
332 | char catalog[128+1]; | |
333 | char schema[128+1]; | |
334 | int numTables; // How many tables does this database have | |
335 | wxDbTableInf *pTableInf; // pTableInf = new wxDbTableInf[numTables]; | |
336 | ||
337 | wxDbInf(); | |
338 | ~wxDbInf(); | |
339 | }; | |
340 | ||
341 | ||
342 | enum wxDbSqlLogState | |
343 | { | |
344 | sqlLogOFF, | |
345 | sqlLogON | |
346 | }; | |
347 | ||
348 | // These are the databases currently tested and working with these classes | |
349 | // See the comments in wxDb::Dbms() for exceptions/issues with | |
350 | // each of these database engines | |
351 | enum wxDBMS | |
352 | { | |
353 | dbmsUNIDENTIFIED, | |
354 | dbmsORACLE, | |
355 | dbmsSYBASE_ASA, // Adaptive Server Anywhere | |
356 | dbmsSYBASE_ASE, // Adaptive Server Enterprise | |
357 | dbmsMS_SQL_SERVER, | |
358 | dbmsMY_SQL, | |
359 | dbmsPOSTGRES, | |
360 | dbmsACCESS, | |
361 | dbmsDBASE, | |
362 | dbmsINFORMIX, | |
363 | dbmsVIRTUOSO | |
364 | }; | |
365 | ||
366 | ||
367 | // The wxDb::errorList is copied to this variable when the wxDb object | |
368 | // is closed. This way, the error list is still available after the | |
369 | // database object is closed. This is necessary if the database | |
370 | // connection fails so the calling application can show the operator | |
371 | // why the connection failed. Note: as each wxDb object is closed, it | |
372 | // will overwrite the errors of the previously destroyed wxDb object in | |
373 | // this variable. | |
374 | extern char DBerrorList[DB_MAX_ERROR_HISTORY][DB_MAX_ERROR_MSG_LEN]; | |
375 | ||
376 | ||
377 | class WXDLLEXPORT wxDb | |
378 | { | |
379 | private: | |
380 | bool dbIsOpen; | |
381 | char *dsn; // Data source name | |
382 | char *uid; // User ID | |
383 | char *authStr; // Authorization string (password) | |
384 | FILE *fpSqlLog; // Sql Log file pointer | |
385 | wxDbSqlLogState sqlLogState; // On or Off | |
386 | bool fwdOnlyCursors; | |
387 | ||
388 | // Private member functions | |
389 | bool getDbInfo(void); | |
390 | bool getDataTypeInfo(SWORD fSqlType, wxDbSqlTypeInfo &structSQLTypeInfo); | |
391 | bool setConnectionOptions(void); | |
392 | void logError(const char *errMsg, const char *SQLState); | |
393 | #if !wxODBC_BACKWARD_COMPATABILITY | |
394 | // ODBC handles | |
395 | HENV henv; // ODBC Environment handle | |
396 | HDBC hdbc; // ODBC DB Connection handle | |
397 | HSTMT hstmt; // ODBC Statement handle | |
398 | ||
399 | //Error reporting mode | |
400 | bool silent; | |
401 | ||
402 | // Number of Ctable objects connected to this db object. FOR INTERNAL USE ONLY!!! | |
403 | unsigned int nTables; | |
404 | ||
405 | // Information about logical data types VARCHAR, INTEGER, FLOAT and DATE. | |
406 | // | |
407 | // This information is obtained from the ODBC driver by use of the | |
408 | // SQLGetTypeInfo() function. The key piece of information is the | |
409 | // type name the data source uses for each logical data type. | |
410 | // e.g. VARCHAR; Oracle calls it VARCHAR2. | |
411 | wxDbSqlTypeInfo typeInfVarchar; | |
412 | wxDbSqlTypeInfo typeInfInteger; | |
413 | wxDbSqlTypeInfo typeInfFloat; | |
414 | wxDbSqlTypeInfo typeInfDate; | |
415 | #endif | |
416 | ||
417 | public: | |
418 | #if wxODBC_BACKWARD_COMPATABILITY | |
419 | // ODBC handles | |
420 | HENV henv; // ODBC Environment handle | |
421 | HDBC hdbc; // ODBC DB Connection handle | |
422 | HSTMT hstmt; // ODBC Statement handle | |
423 | ||
424 | //Error reporting mode | |
425 | bool silent; | |
426 | ||
427 | // Number of Ctable objects connected to this db object. FOR INTERNAL USE ONLY!!! | |
428 | unsigned int nTables; | |
429 | #endif | |
430 | ||
431 | // The following structure contains database information gathered from the | |
432 | // datasource when the datasource is first opened. | |
433 | struct | |
434 | { | |
435 | char dbmsName[40]; // Name of the dbms product | |
436 | char dbmsVer[64]; // Version # of the dbms product | |
437 | char driverName[40]; // Driver name | |
438 | char odbcVer[60]; // ODBC version of the driver | |
439 | char drvMgrOdbcVer[60]; // ODBC version of the driver manager | |
440 | char driverVer[60]; // Driver version | |
441 | char serverName[80]; // Server Name, typically a connect string | |
442 | char databaseName[128]; // Database filename | |
443 | char outerJoins[2]; // Indicates whether the data source supports outer joins | |
444 | char procedureSupport[2]; // Indicates whether the data source supports stored procedures | |
445 | #if EXPERIMENTAL_WXDB_FUNCTIONS // will be added in 2.4 | |
446 | char accessibleTables[2]; // Indicates whether the data source only reports accessible tables in SQLTables. | |
447 | #endif | |
448 | UWORD maxConnections; // Maximum # of connections the data source supports | |
449 | UWORD maxStmts; // Maximum # of HSTMTs per HDBC | |
450 | UWORD apiConfLvl; // ODBC API conformance level | |
451 | UWORD cliConfLvl; // Indicates whether the data source is SAG compliant | |
452 | UWORD sqlConfLvl; // SQL conformance level | |
453 | UWORD cursorCommitBehavior; // Indicates how cursors are affected by a db commit | |
454 | UWORD cursorRollbackBehavior; // Indicates how cursors are affected by a db rollback | |
455 | UWORD supportNotNullClause; // Indicates if data source supports NOT NULL clause | |
456 | char supportIEF[2]; // Integrity Enhancement Facility (Referential Integrity) | |
457 | UDWORD txnIsolation; // Default transaction isolation level supported by the driver | |
458 | UDWORD txnIsolationOptions; // Transaction isolation level options available | |
459 | UDWORD fetchDirections; // Fetch directions supported | |
460 | UDWORD lockTypes; // Lock types supported in SQLSetPos | |
461 | UDWORD posOperations; // Position operations supported in SQLSetPos | |
462 | UDWORD posStmts; // Position statements supported | |
463 | UDWORD scrollConcurrency; // Concurrency control options supported for scrollable cursors | |
464 | UDWORD scrollOptions; // Scroll Options supported for scrollable cursors | |
465 | UDWORD staticSensitivity; // Indicates if additions, deletions and updates can be detected | |
466 | UWORD txnCapable; // Indicates if the data source supports transactions | |
467 | UDWORD loginTimeout; // Number seconds to wait for a login request | |
468 | } dbInf; | |
469 | ||
470 | // ODBC Error Inf. | |
471 | SWORD cbErrorMsg; | |
472 | int DB_STATUS; | |
473 | wxChar errorList[DB_MAX_ERROR_HISTORY][DB_MAX_ERROR_MSG_LEN]; | |
474 | wxChar errorMsg[SQL_MAX_MESSAGE_LENGTH]; | |
475 | SDWORD nativeError; | |
476 | wxChar sqlState[20]; | |
477 | ||
478 | #if wxODBC_BACKWARD_COMPATABILITY | |
479 | // Information about logical data types VARCHAR, INTEGER, FLOAT and DATE. | |
480 | // | |
481 | // This information is obtained from the ODBC driver by use of the | |
482 | // SQLGetTypeInfo() function. The key piece of information is the | |
483 | // type name the data source uses for each logical data type. | |
484 | // e.g. VARCHAR; Oracle calls it VARCHAR2. | |
485 | wxDbSqlTypeInfo typeInfVarchar; | |
486 | wxDbSqlTypeInfo typeInfInteger; | |
487 | wxDbSqlTypeInfo typeInfFloat; | |
488 | wxDbSqlTypeInfo typeInfDate; | |
489 | #endif | |
490 | ||
491 | // Public member functions | |
492 | wxDb(HENV &aHenv, bool FwdOnlyCursors=(bool)wxODBC_FWD_ONLY_CURSORS); | |
493 | bool Open(char *Dsn, char *Uid, char *AuthStr); // Data Source Name, User ID, Password | |
494 | void Close(void); | |
495 | bool CommitTrans(void); | |
496 | bool RollbackTrans(void); | |
497 | bool DispAllErrors(HENV aHenv, HDBC aHdbc = SQL_NULL_HDBC, HSTMT aHstmt = SQL_NULL_HSTMT); | |
498 | bool GetNextError(HENV aHenv, HDBC aHdbc = SQL_NULL_HDBC, HSTMT aHstmt = SQL_NULL_HSTMT); | |
499 | void DispNextError(void); | |
500 | bool CreateView(const char *viewName, const char *colList, const char *pSqlStmt, bool attemptDrop=TRUE); | |
501 | bool DropView(const char *viewName); | |
502 | bool ExecSql(const char *pSqlStmt); | |
503 | bool GetNext(void); | |
504 | bool GetData(UWORD colNo, SWORD cType, PTR pData, SDWORD maxLen, SDWORD FAR *cbReturned); | |
505 | bool Grant(int privileges, const char *tableName, const char *userList = "PUBLIC"); | |
506 | int TranslateSqlState(const wxChar *SQLState); | |
507 | wxDbInf *GetCatalog(char *userID); | |
508 | bool Catalog(const char *userID=NULL, const char *fileName = SQL_CATALOG_FILENAME); | |
509 | int GetKeyFields(char *tableName, wxDbColInf* colInf, int nocols); | |
510 | ||
511 | wxDbColInf *GetColumns(char *tableName[], const char *userID=NULL); | |
512 | wxDbColInf *GetColumns(char *tableName, int *numCols, const char *userID=NULL); | |
513 | ||
514 | int GetColumnCount(char *tableName, const char *userID=NULL); | |
515 | char *GetDatabaseName(void) {return dbInf.dbmsName;} | |
516 | char *GetDataSource(void) {return dsn;} | |
517 | char *GetUsername(void) {return uid;} | |
518 | char *GetPassword(void) {return authStr;} | |
519 | bool IsOpen(void) {return dbIsOpen;} | |
520 | HENV GetHENV(void) {return henv;} | |
521 | HDBC GetHDBC(void) {return hdbc;} | |
522 | HSTMT GetHSTMT(void) {return hstmt;} | |
523 | int GetTableCount() {return nTables;}; // number of tables using this connection | |
524 | wxDbSqlTypeInfo GetTypeInfVarchar(){return typeInfVarchar;} | |
525 | wxDbSqlTypeInfo GetTypeInfInteger(){return typeInfInteger;} | |
526 | wxDbSqlTypeInfo GetTypeInfFloat() {return typeInfFloat;} | |
527 | wxDbSqlTypeInfo GetTypeInfDate() {return typeInfDate;} | |
528 | ||
529 | bool TableExists(const char *tableName, const char *userID=NULL, const char *path=NULL); // Table name can refer to a table, view, alias or synonym | |
530 | #if EXPERIMENTAL_WXDB_FUNCTIONS // will be added in 2.4 | |
531 | bool TablePrivileges(const char *tableName, const char* priv, const char *userID=NULL, const char *path=NULL); // Table name can refer to a table, view, alias or synonym | |
532 | #endif | |
533 | void LogError(const char *errMsg, const char *SQLState = 0) {logError(errMsg, SQLState);} | |
534 | void SetDebugErrorMessages(bool state) { silent = !state; } | |
535 | bool SetSqlLogging(wxDbSqlLogState state, const wxChar *filename = SQL_LOG_FILENAME, bool append = FALSE); | |
536 | bool WriteSqlLog(const wxChar *logMsg); | |
537 | wxDBMS Dbms(void); | |
538 | bool FwdOnlyCursors(void) {return fwdOnlyCursors;} | |
539 | ||
540 | // These two functions are provided strictly for use by wxDbTable. | |
541 | // DO NOT USE THESE FUNCTIONS, OR MEMORY LEAKS MAY OCCUR | |
542 | void incrementTableCount() { nTables++; return; } | |
543 | void decrementTableCount() { nTables--; return; } | |
544 | ||
545 | }; // wxDb | |
546 | ||
547 | ||
548 | // This structure forms a node in a linked list. The linked list of "DbList" objects | |
549 | // keeps track of allocated database connections. This allows the application to | |
550 | // open more than one database connection through ODBC for multiple transaction support | |
551 | // or for multiple database support. | |
552 | struct wxDbList | |
553 | { | |
554 | wxDbList *PtrPrev; // Pointer to previous item in the list | |
555 | wxChar Dsn[SQL_MAX_DSN_LENGTH+1]; // Data Source Name | |
556 | wxDb *PtrDb; // Pointer to the wxDb object | |
557 | bool Free; // Is item free or in use? | |
558 | wxDbList *PtrNext; // Pointer to next item in the list | |
559 | }; | |
560 | ||
561 | #ifdef __WXDEBUG__ | |
562 | #include "wx/object.h" | |
563 | class wxTablesInUse : public wxObject | |
564 | { | |
565 | public: | |
566 | const char *tableName; | |
567 | ULONG tableID; | |
568 | class wxDb *pDb; | |
569 | }; // wxTablesInUse | |
570 | #endif | |
571 | ||
572 | ||
573 | // The following routines allow a user to get new database connections, free them | |
574 | // for other code segments to use, or close all of them when the application has | |
575 | // completed. | |
576 | wxDb WXDLLEXPORT *wxDbGetConnection(wxDbConnectInf *pDbConfig, bool FwdOnlyCursors=(bool)wxODBC_FWD_ONLY_CURSORS); | |
577 | bool WXDLLEXPORT wxDbFreeConnection(wxDb *pDb); | |
578 | void WXDLLEXPORT wxDbCloseConnections(void); | |
579 | int WXDLLEXPORT wxDbConnectionsInUse(void); | |
580 | ||
581 | ||
582 | // This function sets the sql log state for all open wxDb objects | |
583 | bool wxDbSqlLog(wxDbSqlLogState state, const wxChar *filename = SQL_LOG_FILENAME); | |
584 | ||
585 | ||
586 | #if 0 | |
587 | // MSW/VC6 ONLY!!! Experimental | |
588 | int WXDLLEXPORT wxDbCreateDataSource(const char *driverName, const char *dsn, const char *description="", | |
589 | bool sysDSN=FALSE, const char *defDir="", wxWindow *parent=NULL); | |
590 | #endif | |
591 | ||
592 | // This routine allows you to query a driver manager | |
593 | // for a list of available datasources. Call this routine | |
594 | // the first time using SQL_FETCH_FIRST. Continue to call it | |
595 | // using SQL_FETCH_NEXT until you've exhausted the list. | |
596 | bool WXDLLEXPORT wxDbGetDataSource(HENV henv, char *Dsn, SWORD DsnMax, char *DsDesc, SWORD DsDescMax, | |
597 | UWORD direction = SQL_FETCH_NEXT); | |
598 | ||
599 | ||
600 | // Change this to 0 to remove use of all deprecated functions | |
601 | #if wxODBC_BACKWARD_COMPATABILITY | |
602 | //################################################################################# | |
603 | //############### DEPRECATED functions for backward compatability ################# | |
604 | //################################################################################# | |
605 | ||
606 | // Backward compability structures/classes. This will eventually go away | |
607 | const int DB_PATH_MAX = wxDB_PATH_MAX; | |
608 | ||
609 | typedef wxDb wxDB; | |
610 | typedef wxDbTableInf wxTableInf; | |
611 | typedef wxDbColInf wxColInf; | |
612 | typedef wxDbColInf CcolInf; | |
613 | typedef wxDbColFor wxColFor; | |
614 | typedef wxDbSqlTypeInfo SqlTypeInfo; | |
615 | typedef wxDbSqlTypeInfo wxSqlTypeInfo; | |
616 | typedef enum wxDbSqlLogState sqlLog; | |
617 | typedef enum wxDbSqlLogState wxSqlLogState; | |
618 | typedef enum wxDBMS dbms; | |
619 | typedef enum wxDBMS DBMS; | |
620 | typedef wxODBC_ERRORS ODBC_ERRORS; | |
621 | typedef wxDbConnectInf DbStuff; | |
622 | typedef wxDbList DbList; | |
623 | #ifdef __WXDEBUG__ | |
624 | typedef wxTablesInUse CstructTablesInUse; | |
625 | #endif | |
626 | ||
627 | // Deprecated function names that are replaced by the function names listed above | |
628 | wxDB WXDLLEXPORT *GetDbConnection(DbStuff *pDbStuff, bool FwdOnlyCursors=(bool)wxODBC_FWD_ONLY_CURSORS); | |
629 | bool WXDLLEXPORT FreeDbConnection(wxDB *pDb); | |
630 | void WXDLLEXPORT CloseDbConnections(void); | |
631 | int WXDLLEXPORT NumberDbConnectionsInUse(void); | |
632 | ||
633 | bool SqlLog(sqlLog state, const wxChar *filename = SQL_LOG_FILENAME); | |
634 | ||
635 | bool WXDLLEXPORT GetDataSource(HENV henv, char *Dsn, SWORD DsnMax, char *DsDesc, SWORD DsDescMax, | |
636 | UWORD direction = SQL_FETCH_NEXT); | |
637 | #endif // Deprecated structures/classes/functions | |
638 | ||
639 | #endif |