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