1 ///////////////////////////////////////////////////////////////////////////////
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.
7 // Modified by: George Tasker
9 // Mark Johnson, wxWindows@mj10777.de
11 // -Added support for SQL statement logging and database cataloging
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
21 // Copyright: (c) 1996 Remstar International, Inc.
22 // Licence: wxWindows licence
23 ///////////////////////////////////////////////////////////////////////////////
29 // BJO 20000503: introduce new GetColumns members which are more database independent and
30 // return columns in the order they were created
31 #define OLD_GETCOLUMNS 1
32 #define EXPERIMENTAL_WXDB_FUNCTIONS 1
35 #include "wx/string.h"
37 #if defined(__VISUALC__)
38 // we need to include standard Windows headers but we can't include
39 // <windows.h> directly when using MFC because it includes it itself in a
49 #include "wx/msw/winundef.h"
50 #endif // wxUSE_MFC/!wxUSE_MFC
52 // If you use the wxDbCreateDataSource() function with MSW/VC6,
53 // you cannot use the iODBC headers, you must use the VC headers,
54 // plus the odbcinst.h header - gt Nov 2 2000
56 // Must add "odbccp32.lib" in \wx2\wxWidgets\src\makevc.env to the WINLIBS= line
61 // #include <sqlucode.h>
65 #if defined(__WINDOWS__) && ( defined(HAVE_W32API_H) || defined(__BORLANDC__) )
67 #include "wx/msw/winundef.h"
70 #if defined(wxUSE_BUILTIN_IODBC) && wxUSE_BUILTIN_IODBC
71 // Use the ones from the library
73 #include "wx/isqlext.h"
74 // Not available in v2.x of iODBC
77 typedef wxChar SQLTCHAR
;
79 typedef UCHAR SQLTCHAR
;
83 #if defined( __WXMOTIF__ ) && defined( __VMS )
84 // solves a type definition mismatch between IODBC and MOTIF on OpenVMS
90 // #include <sqlucode.h>
92 #if defined( __WXMOTIF__ ) && defined( __VMS )
100 #define SQL_C_WXCHAR SQL_C_WCHAR
102 #define SQL_C_WXCHAR SQL_C_CHAR
105 #ifdef __DIGITALMARS__
107 typedef wxChar SQLTCHAR
;
109 typedef UCHAR SQLTCHAR
;
113 typedef float SFLOAT
;
114 typedef double SDOUBLE
;
115 typedef unsigned int UINT
;
118 #ifndef wxODBC_FWD_ONLY_CURSORS
119 #define wxODBC_FWD_ONLY_CURSORS 1
122 enum enumDummy
{enumDum1
};
124 #ifndef SQL_C_BOOLEAN
125 #define SQL_C_BOOLEAN(datatype) (sizeof(datatype) == 1 ? SQL_C_UTINYINT : (sizeof(datatype) == 2 ? SQL_C_USHORT : SQL_C_ULONG))
129 #define SQL_C_ENUM (sizeof(enumDummy) == 2 ? SQL_C_USHORT : SQL_C_ULONG)
132 // NOTE: If SQL_C_BLOB is defined, and it is not SQL_C_BINARY, iODBC 2.x
133 // may not function correctly. Likely best to use SQL_C_BINARY direct
136 #define SQL_C_BLOB SQL_C_BINARY
142 #define SQLLEN SQLINTEGER
145 #define SQLULEN SQLUINTEGER
149 const int wxDB_PATH_MAX
= 254;
151 extern WXDLLIMPEXP_DATA_ODBC(wxChar
const *) SQL_LOG_FILENAME
;
152 extern WXDLLIMPEXP_DATA_ODBC(wxChar
const *) SQL_CATALOG_FILENAME
;
155 const int DB_TYPE_NAME_LEN
= 40;
156 const int DB_MAX_STATEMENT_LEN
= 4096;
157 const int DB_MAX_WHERE_CLAUSE_LEN
= 2048;
158 const int DB_MAX_ERROR_MSG_LEN
= 512;
159 const int DB_MAX_ERROR_HISTORY
= 5;
160 const int DB_MAX_TABLE_NAME_LEN
= 128;
161 const int DB_MAX_COLUMN_NAME_LEN
= 128;
163 const int DB_DATA_TYPE_VARCHAR
= 1;
164 const int DB_DATA_TYPE_INTEGER
= 2;
165 const int DB_DATA_TYPE_FLOAT
= 3;
166 const int DB_DATA_TYPE_DATE
= 4;
167 const int DB_DATA_TYPE_BLOB
= 5;
168 const int DB_DATA_TYPE_MEMO
= 6;
170 const int DB_SELECT_KEYFIELDS
= 1;
171 const int DB_SELECT_WHERE
= 2;
172 const int DB_SELECT_MATCHING
= 3;
173 const int DB_SELECT_STATEMENT
= 4;
175 const int DB_UPD_KEYFIELDS
= 1;
176 const int DB_UPD_WHERE
= 2;
178 const int DB_DEL_KEYFIELDS
= 1;
179 const int DB_DEL_WHERE
= 2;
180 const int DB_DEL_MATCHING
= 3;
182 const int DB_WHERE_KEYFIELDS
= 1;
183 const int DB_WHERE_MATCHING
= 2;
185 const int DB_GRANT_SELECT
= 1;
186 const int DB_GRANT_INSERT
= 2;
187 const int DB_GRANT_UPDATE
= 4;
188 const int DB_GRANT_DELETE
= 8;
189 const int DB_GRANT_ALL
= DB_GRANT_SELECT
| DB_GRANT_INSERT
| DB_GRANT_UPDATE
| DB_GRANT_DELETE
;
191 // ODBC Error codes (derived from ODBC SqlState codes)
197 DB_ERR_GENERAL_WARNING
, // SqlState = '01000'
198 DB_ERR_DISCONNECT_ERROR
, // SqlState = '01002'
199 DB_ERR_DATA_TRUNCATED
, // SqlState = '01004'
200 DB_ERR_PRIV_NOT_REVOKED
, // SqlState = '01006'
201 DB_ERR_INVALID_CONN_STR_ATTR
, // SqlState = '01S00'
202 DB_ERR_ERROR_IN_ROW
, // SqlState = '01S01'
203 DB_ERR_OPTION_VALUE_CHANGED
, // SqlState = '01S02'
204 DB_ERR_NO_ROWS_UPD_OR_DEL
, // SqlState = '01S03'
205 DB_ERR_MULTI_ROWS_UPD_OR_DEL
, // SqlState = '01S04'
206 DB_ERR_WRONG_NO_OF_PARAMS
, // SqlState = '07001'
207 DB_ERR_DATA_TYPE_ATTR_VIOL
, // SqlState = '07006'
208 DB_ERR_UNABLE_TO_CONNECT
, // SqlState = '08001'
209 DB_ERR_CONNECTION_IN_USE
, // SqlState = '08002'
210 DB_ERR_CONNECTION_NOT_OPEN
, // SqlState = '08003'
211 DB_ERR_REJECTED_CONNECTION
, // SqlState = '08004'
212 DB_ERR_CONN_FAIL_IN_TRANS
, // SqlState = '08007'
213 DB_ERR_COMM_LINK_FAILURE
, // SqlState = '08S01'
214 DB_ERR_INSERT_VALUE_LIST_MISMATCH
, // SqlState = '21S01'
215 DB_ERR_DERIVED_TABLE_MISMATCH
, // SqlState = '21S02'
216 DB_ERR_STRING_RIGHT_TRUNC
, // SqlState = '22001'
217 DB_ERR_NUMERIC_VALUE_OUT_OF_RNG
, // SqlState = '22003'
218 DB_ERR_ERROR_IN_ASSIGNMENT
, // SqlState = '22005'
219 DB_ERR_DATETIME_FLD_OVERFLOW
, // SqlState = '22008'
220 DB_ERR_DIVIDE_BY_ZERO
, // SqlState = '22012'
221 DB_ERR_STR_DATA_LENGTH_MISMATCH
, // SqlState = '22026'
222 DB_ERR_INTEGRITY_CONSTRAINT_VIOL
, // SqlState = '23000'
223 DB_ERR_INVALID_CURSOR_STATE
, // SqlState = '24000'
224 DB_ERR_INVALID_TRANS_STATE
, // SqlState = '25000'
225 DB_ERR_INVALID_AUTH_SPEC
, // SqlState = '28000'
226 DB_ERR_INVALID_CURSOR_NAME
, // SqlState = '34000'
227 DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL
, // SqlState = '37000'
228 DB_ERR_DUPLICATE_CURSOR_NAME
, // SqlState = '3C000'
229 DB_ERR_SERIALIZATION_FAILURE
, // SqlState = '40001'
230 DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL2
, // SqlState = '42000'
231 DB_ERR_OPERATION_ABORTED
, // SqlState = '70100'
232 DB_ERR_UNSUPPORTED_FUNCTION
, // SqlState = 'IM001'
233 DB_ERR_NO_DATA_SOURCE
, // SqlState = 'IM002'
234 DB_ERR_DRIVER_LOAD_ERROR
, // SqlState = 'IM003'
235 DB_ERR_SQLALLOCENV_FAILED
, // SqlState = 'IM004'
236 DB_ERR_SQLALLOCCONNECT_FAILED
, // SqlState = 'IM005'
237 DB_ERR_SQLSETCONNECTOPTION_FAILED
, // SqlState = 'IM006'
238 DB_ERR_NO_DATA_SOURCE_DLG_PROHIB
, // SqlState = 'IM007'
239 DB_ERR_DIALOG_FAILED
, // SqlState = 'IM008'
240 DB_ERR_UNABLE_TO_LOAD_TRANSLATION_DLL
, // SqlState = 'IM009'
241 DB_ERR_DATA_SOURCE_NAME_TOO_LONG
, // SqlState = 'IM010'
242 DB_ERR_DRIVER_NAME_TOO_LONG
, // SqlState = 'IM011'
243 DB_ERR_DRIVER_KEYWORD_SYNTAX_ERROR
, // SqlState = 'IM012'
244 DB_ERR_TRACE_FILE_ERROR
, // SqlState = 'IM013'
245 DB_ERR_TABLE_OR_VIEW_ALREADY_EXISTS
, // SqlState = 'S0001'
246 DB_ERR_TABLE_NOT_FOUND
, // SqlState = 'S0002'
247 DB_ERR_INDEX_ALREADY_EXISTS
, // SqlState = 'S0011'
248 DB_ERR_INDEX_NOT_FOUND
, // SqlState = 'S0012'
249 DB_ERR_COLUMN_ALREADY_EXISTS
, // SqlState = 'S0021'
250 DB_ERR_COLUMN_NOT_FOUND
, // SqlState = 'S0022'
251 DB_ERR_NO_DEFAULT_FOR_COLUMN
, // SqlState = 'S0023'
252 DB_ERR_GENERAL_ERROR
, // SqlState = 'S1000'
253 DB_ERR_MEMORY_ALLOCATION_FAILURE
, // SqlState = 'S1001'
254 DB_ERR_INVALID_COLUMN_NUMBER
, // SqlState = 'S1002'
255 DB_ERR_PROGRAM_TYPE_OUT_OF_RANGE
, // SqlState = 'S1003'
256 DB_ERR_SQL_DATA_TYPE_OUT_OF_RANGE
, // SqlState = 'S1004'
257 DB_ERR_OPERATION_CANCELLED
, // SqlState = 'S1008'
258 DB_ERR_INVALID_ARGUMENT_VALUE
, // SqlState = 'S1009'
259 DB_ERR_FUNCTION_SEQUENCE_ERROR
, // SqlState = 'S1010'
260 DB_ERR_OPERATION_INVALID_AT_THIS_TIME
, // SqlState = 'S1011'
261 DB_ERR_INVALID_TRANS_OPERATION_CODE
, // SqlState = 'S1012'
262 DB_ERR_NO_CURSOR_NAME_AVAIL
, // SqlState = 'S1015'
263 DB_ERR_INVALID_STR_OR_BUF_LEN
, // SqlState = 'S1090'
264 DB_ERR_DESCRIPTOR_TYPE_OUT_OF_RANGE
, // SqlState = 'S1091'
265 DB_ERR_OPTION_TYPE_OUT_OF_RANGE
, // SqlState = 'S1092'
266 DB_ERR_INVALID_PARAM_NO
, // SqlState = 'S1093'
267 DB_ERR_INVALID_SCALE_VALUE
, // SqlState = 'S1094'
268 DB_ERR_FUNCTION_TYPE_OUT_OF_RANGE
, // SqlState = 'S1095'
269 DB_ERR_INF_TYPE_OUT_OF_RANGE
, // SqlState = 'S1096'
270 DB_ERR_COLUMN_TYPE_OUT_OF_RANGE
, // SqlState = 'S1097'
271 DB_ERR_SCOPE_TYPE_OUT_OF_RANGE
, // SqlState = 'S1098'
272 DB_ERR_NULLABLE_TYPE_OUT_OF_RANGE
, // SqlState = 'S1099'
273 DB_ERR_UNIQUENESS_OPTION_TYPE_OUT_OF_RANGE
, // SqlState = 'S1100'
274 DB_ERR_ACCURACY_OPTION_TYPE_OUT_OF_RANGE
, // SqlState = 'S1101'
275 DB_ERR_DIRECTION_OPTION_OUT_OF_RANGE
, // SqlState = 'S1103'
276 DB_ERR_INVALID_PRECISION_VALUE
, // SqlState = 'S1104'
277 DB_ERR_INVALID_PARAM_TYPE
, // SqlState = 'S1105'
278 DB_ERR_FETCH_TYPE_OUT_OF_RANGE
, // SqlState = 'S1106'
279 DB_ERR_ROW_VALUE_OUT_OF_RANGE
, // SqlState = 'S1107'
280 DB_ERR_CONCURRENCY_OPTION_OUT_OF_RANGE
, // SqlState = 'S1108'
281 DB_ERR_INVALID_CURSOR_POSITION
, // SqlState = 'S1109'
282 DB_ERR_INVALID_DRIVER_COMPLETION
, // SqlState = 'S1110'
283 DB_ERR_INVALID_BOOKMARK_VALUE
, // SqlState = 'S1111'
284 DB_ERR_DRIVER_NOT_CAPABLE
, // SqlState = 'S1C00'
285 DB_ERR_TIMEOUT_EXPIRED
// SqlState = 'S1T00'
292 #ifndef SQL_MAX_AUTHSTR_LEN
293 // There does not seem to be a standard for this, so I am
294 // defaulting to the value that MS uses
295 #define SQL_MAX_AUTHSTR_LEN MAXNAME
298 #ifndef SQL_MAX_CONNECTSTR_LEN
299 // There does not seem to be a standard for this, so I am
300 // defaulting to the value that MS recommends
301 #define SQL_MAX_CONNECTSTR_LEN 1024
305 class WXDLLIMPEXP_ODBC wxDbConnectInf
308 bool freeHenvOnDestroy
;
309 bool useConnectionStr
;
313 wxChar Dsn
[SQL_MAX_DSN_LENGTH
+1]; // Data Source Name
314 wxChar Uid
[SQL_MAX_USER_NAME_LEN
+1]; // User ID
315 wxChar AuthStr
[SQL_MAX_AUTHSTR_LEN
+1]; // Authorization string (password)
316 wxChar ConnectionStr
[SQL_MAX_CONNECTSTR_LEN
+1]; // Connection string (password)
318 wxString Description
; // Not sure what the max length is
319 wxString FileType
; // Not sure what the max length is
321 // Optionals needed for some databases like dBase
322 wxString DefaultDir
; // Directory that db file resides in
327 wxDbConnectInf(HENV henv
, const wxString
&dsn
, const wxString
&userID
=wxEmptyString
,
328 const wxString
&password
=wxEmptyString
, const wxString
&defaultDir
=wxEmptyString
,
329 const wxString
&description
=wxEmptyString
, const wxString
&fileType
=wxEmptyString
);
339 const HENV
&GetHenv() { return Henv
; };
341 const wxChar
*GetDsn() { return Dsn
; };
343 const wxChar
*GetUid() { return Uid
; };
344 const wxChar
*GetUserID() { return Uid
; };
346 const wxChar
*GetAuthStr() { return AuthStr
; };
347 const wxChar
*GetPassword() { return AuthStr
; };
349 const wxChar
*GetConnectionStr() { return ConnectionStr
; };
350 bool UseConnectionStr() { return useConnectionStr
; };
352 const wxChar
*GetDescription() { return Description
; };
353 const wxChar
*GetFileType() { return FileType
; };
354 const wxChar
*GetDefaultDir() { return DefaultDir
; };
356 void SetHenv(const HENV henv
) { Henv
= henv
; };
358 void SetDsn(const wxString
&dsn
);
360 void SetUserID(const wxString
&userID
);
361 void SetUid(const wxString
&uid
) { SetUserID(uid
); };
363 void SetPassword(const wxString
&password
);
364 void SetAuthStr(const wxString
&authstr
) { SetPassword(authstr
); };
366 void SetConnectionStr(const wxString
&connectStr
);
368 void SetDescription(const wxString
&desc
) { Description
= desc
; };
369 void SetFileType(const wxString
&fileType
) { FileType
= fileType
; };
370 void SetDefaultDir(const wxString
&defDir
) { DefaultDir
= defDir
; };
371 }; // class wxDbConnectInf
374 struct WXDLLIMPEXP_ODBC wxDbSqlTypeInfo
384 class WXDLLIMPEXP_ODBC wxDbColFor
387 wxString s_Field
; // Formatted String for Output
388 wxString s_Format
[7]; // Formatted Objects - TIMESTAMP has the biggest (7)
389 wxString s_Amount
[7]; // Formatted Objects - amount of things that can be formatted
390 int i_Amount
[7]; // Formatted Objects - TT MM YYYY HH MM SS m
391 int i_Nation
; // 0 = timestamp , 1=EU, 2=UK, 3=International, 4=US
392 int i_dbDataType
; // conversion of the 'sqlDataType' to the generic data type used by these classes
399 int Format(int Nation
, int dbDataType
, SWORD sqlDataType
, short columnLength
, short decimalDigits
);
403 class WXDLLIMPEXP_ODBC wxDbColInf
406 wxChar catalog
[128+1];
407 wxChar schema
[128+1];
408 wxChar tableName
[DB_MAX_TABLE_NAME_LEN
+1];
409 wxChar colName
[DB_MAX_COLUMN_NAME_LEN
+1];
411 wxChar typeName
[128+1];
417 wxChar remarks
[254+1];
418 int dbDataType
; // conversion of the 'sqlDataType' to the generic data type used by these classes
419 // mj10777.19991224 : new
420 int PkCol
; // Primary key column 0=No; 1= First Key, 2 = Second Key etc.
421 wxChar PkTableName
[DB_MAX_TABLE_NAME_LEN
+1]; // Tables that use this PKey as a FKey
422 int FkCol
; // Foreign key column 0=No; 1= First Key, 2 = Second Key etc.
423 wxChar FkTableName
[DB_MAX_TABLE_NAME_LEN
+1]; // Foreign key table name
424 wxDbColFor
*pColFor
; // How should this columns be formatted
433 class WXDLLIMPEXP_ODBC wxDbTableInf
// Description of a Table
436 wxChar tableName
[DB_MAX_TABLE_NAME_LEN
+1];
437 wxChar tableType
[254+1]; // "TABLE" or "SYSTEM TABLE" etc.
438 wxChar tableRemarks
[254+1];
439 UWORD numCols
; // How many Columns does this Table have: GetColumnCount(..);
440 wxDbColInf
*pColInf
; // pColInf = NULL ; User can later call GetColumns(..);
449 class WXDLLIMPEXP_ODBC wxDbInf
// Description of a Database
452 wxChar catalog
[128+1];
453 wxChar schema
[128+1];
454 int numTables
; // How many tables does this database have
455 wxDbTableInf
*pTableInf
; // pTableInf = new wxDbTableInf[numTables];
470 // These are the databases currently tested and working with these classes
471 // See the comments in wxDb::Dbms() for exceptions/issues with
472 // each of these database engines
477 dbmsSYBASE_ASA
, // Adaptive Server Anywhere
478 dbmsSYBASE_ASE
, // Adaptive Server Enterprise
505 // The wxDb::errorList is copied to this variable when the wxDb object
506 // is closed. This way, the error list is still available after the
507 // database object is closed. This is necessary if the database
508 // connection fails so the calling application can show the operator
509 // why the connection failed. Note: as each wxDb object is closed, it
510 // will overwrite the errors of the previously destroyed wxDb object in
513 extern WXDLLIMPEXP_DATA_ODBC(wxChar
)
514 DBerrorList
[DB_MAX_ERROR_HISTORY
][DB_MAX_ERROR_MSG_LEN
+1];
517 class WXDLLIMPEXP_ODBC wxDb
521 bool dbIsCached
; // Was connection created by caching functions
522 bool dbOpenedWithConnectionString
; // Was the database connection opened with a connection string
523 wxString dsn
; // Data source name
524 wxString uid
; // User ID
525 wxString authStr
; // Authorization string (password)
526 wxString inConnectionStr
; // Connection string used to connect to the database
527 wxString outConnectionStr
;// Connection string returned by the database when a connection is successfully opened
528 FILE *fpSqlLog
; // Sql Log file pointer
529 wxDbSqlLogState sqlLogState
; // On or Off
531 wxDBMS dbmsType
; // Type of datasource - i.e. Oracle, dBase, SQLServer, etc
533 // Private member functions
534 bool getDbInfo(bool failOnDataTypeUnsupported
=true);
535 bool getDataTypeInfo(SWORD fSqlType
, wxDbSqlTypeInfo
&structSQLTypeInfo
);
536 bool setConnectionOptions(void);
537 void logError(const wxString
&errMsg
, const wxString
&SQLState
);
538 const wxChar
*convertUserID(const wxChar
*userID
, wxString
&UserID
);
539 bool determineDataTypes(bool failOnDataTypeUnsupported
);
541 bool open(bool failOnDataTypeUnsupported
=true);
543 #if !wxODBC_BACKWARD_COMPATABILITY
545 HENV henv
; // ODBC Environment handle
546 HDBC hdbc
; // ODBC DB Connection handle
547 HSTMT hstmt
; // ODBC Statement handle
549 //Error reporting mode
552 // Number of Ctable objects connected to this db object. FOR INTERNAL USE ONLY!!!
553 unsigned int nTables
;
555 // Information about logical data types VARCHAR, INTEGER, FLOAT and DATE.
557 // This information is obtained from the ODBC driver by use of the
558 // SQLGetTypeInfo() function. The key piece of information is the
559 // type name the data source uses for each logical data type.
560 // e.g. VARCHAR; Oracle calls it VARCHAR2.
561 wxDbSqlTypeInfo typeInfVarchar
;
562 wxDbSqlTypeInfo typeInfInteger
;
563 wxDbSqlTypeInfo typeInfFloat
;
564 wxDbSqlTypeInfo typeInfDate
;
565 wxDbSqlTypeInfo typeInfBlob
;
566 wxDbSqlTypeInfo typeInfMemo
;
571 void setCached(bool cached
) { dbIsCached
= cached
; }; // This function must only be called by wxDbGetConnection() and wxDbCloseConnections!!!
572 bool IsCached() { return dbIsCached
; };
574 bool GetDataTypeInfo(SWORD fSqlType
, wxDbSqlTypeInfo
&structSQLTypeInfo
)
575 { return getDataTypeInfo(fSqlType
, structSQLTypeInfo
); }
577 #if wxODBC_BACKWARD_COMPATABILITY
579 HENV henv
; // ODBC Environment handle
580 HDBC hdbc
; // ODBC DB Connection handle
581 HSTMT hstmt
; // ODBC Statement handle
583 //Error reporting mode
586 // Number of Ctable objects connected to this db object. FOR INTERNAL USE ONLY!!!
587 unsigned int nTables
;
590 // The following structure contains database information gathered from the
591 // datasource when the datasource is first opened.
594 wxChar dbmsName
[40]; // Name of the dbms product
595 wxChar dbmsVer
[64]; // Version # of the dbms product
596 wxChar driverName
[40]; // Driver name
597 wxChar odbcVer
[60]; // ODBC version of the driver
598 wxChar drvMgrOdbcVer
[60]; // ODBC version of the driver manager
599 wxChar driverVer
[60]; // Driver version
600 wxChar serverName
[80]; // Server Name, typically a connect string
601 wxChar databaseName
[128]; // Database filename
602 wxChar outerJoins
[2]; // Indicates whether the data source supports outer joins
603 wxChar procedureSupport
[2]; // Indicates whether the data source supports stored procedures
604 wxChar accessibleTables
[2]; // Indicates whether the data source only reports accessible tables in SQLTables.
605 UWORD maxConnections
; // Maximum # of connections the data source supports
606 UWORD maxStmts
; // Maximum # of HSTMTs per HDBC
607 UWORD apiConfLvl
; // ODBC API conformance level
608 UWORD cliConfLvl
; // Indicates whether the data source is SAG compliant
609 UWORD sqlConfLvl
; // SQL conformance level
610 UWORD cursorCommitBehavior
; // Indicates how cursors are affected by a db commit
611 UWORD cursorRollbackBehavior
; // Indicates how cursors are affected by a db rollback
612 UWORD supportNotNullClause
; // Indicates if data source supports NOT NULL clause
613 wxChar supportIEF
[2]; // Integrity Enhancement Facility (Referential Integrity)
614 UDWORD txnIsolation
; // Default transaction isolation level supported by the driver
615 UDWORD txnIsolationOptions
; // Transaction isolation level options available
616 UDWORD fetchDirections
; // Fetch directions supported
617 UDWORD lockTypes
; // Lock types supported in SQLSetPos
618 UDWORD posOperations
; // Position operations supported in SQLSetPos
619 UDWORD posStmts
; // Position statements supported
620 UDWORD scrollConcurrency
; // Concurrency control options supported for scrollable cursors
621 UDWORD scrollOptions
; // Scroll Options supported for scrollable cursors
622 UDWORD staticSensitivity
; // Indicates if additions, deletions and updates can be detected
623 UWORD txnCapable
; // Indicates if the data source supports transactions
624 UDWORD loginTimeout
; // Number seconds to wait for a login request
631 // The DECC compiler chokes when in db.cpp the array is accessed outside
632 // its bounds. Maybe this change should also applied for other platforms.
633 wxChar errorList
[DB_MAX_ERROR_HISTORY
][DB_MAX_ERROR_MSG_LEN
+1];
635 wxChar errorList
[DB_MAX_ERROR_HISTORY
][DB_MAX_ERROR_MSG_LEN
];
637 wxChar errorMsg
[SQL_MAX_MESSAGE_LENGTH
];
638 SQLINTEGER nativeError
;
641 #if wxODBC_BACKWARD_COMPATABILITY
642 // Information about logical data types VARCHAR, INTEGER, FLOAT and DATE.
644 // This information is obtained from the ODBC driver by use of the
645 // SQLGetTypeInfo() function. The key piece of information is the
646 // type name the data source uses for each logical data type.
647 // e.g. VARCHAR; Oracle calls it VARCHAR2.
648 wxDbSqlTypeInfo typeInfVarchar
;
649 wxDbSqlTypeInfo typeInfInteger
;
650 wxDbSqlTypeInfo typeInfFloat
;
651 wxDbSqlTypeInfo typeInfDate
;
652 wxDbSqlTypeInfo typeInfBlob
;
655 // Public member functions
656 wxDb(const HENV
&aHenv
, bool FwdOnlyCursors
=(bool)wxODBC_FWD_ONLY_CURSORS
);
659 // Data Source Name, User ID, Password and whether open should fail on data type not supported
660 bool Open(const wxString
& inConnectStr
, bool failOnDataTypeUnsupported
=true);
661 ///This version of Open will open the odbc source selection dialog. Cast a wxWindow::GetHandle() to SQLHWND to use.
662 bool Open(const wxString
& inConnectStr
, SQLHWND parentWnd
, bool failOnDataTypeUnsupported
=true);
663 bool Open(const wxString
&Dsn
, const wxString
&Uid
, const wxString
&AuthStr
, bool failOnDataTypeUnsupported
=true);
664 bool Open(wxDbConnectInf
*dbConnectInf
, bool failOnDataTypeUnsupported
=true);
665 bool Open(wxDb
*copyDb
); // pointer to a wxDb whose connection info should be copied rather than re-queried
667 bool CommitTrans(void);
668 bool RollbackTrans(void);
669 bool DispAllErrors(HENV aHenv
, HDBC aHdbc
= SQL_NULL_HDBC
, HSTMT aHstmt
= SQL_NULL_HSTMT
);
670 bool GetNextError(HENV aHenv
, HDBC aHdbc
= SQL_NULL_HDBC
, HSTMT aHstmt
= SQL_NULL_HSTMT
);
671 void DispNextError(void);
672 bool CreateView(const wxString
&viewName
, const wxString
&colList
, const wxString
&pSqlStmt
, bool attemptDrop
=true);
673 bool DropView(const wxString
&viewName
);
674 bool ExecSql(const wxString
&pSqlStmt
);
675 bool ExecSql(const wxString
&pSqlStmt
, wxDbColInf
** columns
, short& numcols
);
677 bool GetData(UWORD colNo
, SWORD cType
, PTR pData
, SDWORD maxLen
, SQLLEN FAR
*cbReturned
);
678 bool Grant(int privileges
, const wxString
&tableName
, const wxString
&userList
= wxT("PUBLIC"));
679 int TranslateSqlState(const wxString
&SQLState
);
680 wxDbInf
*GetCatalog(const wxChar
*userID
=NULL
);
681 bool Catalog(const wxChar
*userID
=NULL
, const wxString
&fileName
=SQL_CATALOG_FILENAME
);
682 int GetKeyFields(const wxString
&tableName
, wxDbColInf
* colInf
, UWORD noCols
);
684 wxDbColInf
*GetColumns(wxChar
*tableName
[], const wxChar
*userID
=NULL
);
685 wxDbColInf
*GetColumns(const wxString
&tableName
, UWORD
*numCols
, const wxChar
*userID
=NULL
);
687 int GetColumnCount(const wxString
&tableName
, const wxChar
*userID
=NULL
);
688 const wxChar
*GetDatabaseName(void) {return dbInf
.dbmsName
;}
689 const wxString
&GetDataSource(void) {return dsn
;}
690 const wxString
&GetDatasourceName(void){return dsn
;}
691 const wxString
&GetUsername(void) {return uid
;}
692 const wxString
&GetPassword(void) {return authStr
;}
693 const wxString
&GetConnectionInStr(void) {return inConnectionStr
;}
694 const wxString
&GetConnectionOutStr(void) {return outConnectionStr
;}
695 bool IsOpen(void) {return dbIsOpen
;}
696 bool OpenedWithConnectionString(void) {return dbOpenedWithConnectionString
;}
697 HENV
GetHENV(void) {return henv
;}
698 HDBC
GetHDBC(void) {return hdbc
;}
699 HSTMT
GetHSTMT(void) {return hstmt
;}
700 int GetTableCount() {return nTables
;} // number of tables using this connection
701 wxDbSqlTypeInfo
GetTypeInfVarchar() {return typeInfVarchar
;}
702 wxDbSqlTypeInfo
GetTypeInfInteger() {return typeInfInteger
;}
703 wxDbSqlTypeInfo
GetTypeInfFloat() {return typeInfFloat
;}
704 wxDbSqlTypeInfo
GetTypeInfDate() {return typeInfDate
;}
705 wxDbSqlTypeInfo
GetTypeInfBlob() {return typeInfBlob
;}
706 wxDbSqlTypeInfo
GetTypeInfMemo() {return typeInfMemo
;}
708 // tableName can refer to a table, view, alias or synonym
709 bool TableExists(const wxString
&tableName
, const wxChar
*userID
=NULL
,
710 const wxString
&tablePath
=wxEmptyString
);
711 bool TablePrivileges(const wxString
&tableName
, const wxString
&priv
,
712 const wxChar
*userID
=NULL
, const wxChar
*schema
=NULL
,
713 const wxString
&path
=wxEmptyString
);
715 // These two functions return the table name or column name in a form ready
716 // for use in SQL statements. For example, if the datasource allows spaces
717 // in the table name or column name, the returned string will have the
718 // correct enclosing marks around the name to allow it to be properly
719 // included in a SQL statement
720 const wxString
SQLTableName(const wxChar
*tableName
);
721 const wxString
SQLColumnName(const wxChar
*colName
);
723 void LogError(const wxString
&errMsg
, const wxString
&SQLState
= wxEmptyString
)
724 { logError(errMsg
, SQLState
); }
725 void SetDebugErrorMessages(bool state
) { silent
= !state
; }
726 bool SetSqlLogging(wxDbSqlLogState state
, const wxString
&filename
= SQL_LOG_FILENAME
,
727 bool append
= false);
728 bool WriteSqlLog(const wxString
&logMsg
);
731 bool ModifyColumn(const wxString
&tableName
, const wxString
&columnName
,
732 int dataType
, ULONG columnLength
=0,
733 const wxString
&optionalParam
=wxEmptyString
);
735 bool FwdOnlyCursors(void) {return fwdOnlyCursors
;}
737 // These two functions are provided strictly for use by wxDbTable.
738 // DO NOT USE THESE FUNCTIONS, OR MEMORY LEAKS MAY OCCUR
739 void incrementTableCount() { nTables
++; return; }
740 void decrementTableCount() { nTables
--; return; }
745 // This structure forms a node in a linked list. The linked list of "DbList" objects
746 // keeps track of allocated database connections. This allows the application to
747 // open more than one database connection through ODBC for multiple transaction support
748 // or for multiple database support.
751 wxDbList
*PtrPrev
; // Pointer to previous item in the list
752 wxString Dsn
; // Data Source Name
753 wxString Uid
; // User ID
754 wxString AuthStr
; // Authorization string (password)
755 wxString ConnectionStr
; // Connection string used instead of DSN
756 wxDb
*PtrDb
; // Pointer to the wxDb object
757 bool Free
; // Is item free or in use?
758 wxDbList
*PtrNext
; // Pointer to next item in the list
763 #include "wx/object.h"
764 class wxTablesInUse
: public wxObject
767 const wxChar
*tableName
;
774 // The following routines allow a user to get new database connections, free them
775 // for other code segments to use, or close all of them when the application has
777 wxDb WXDLLIMPEXP_ODBC
*wxDbGetConnection(wxDbConnectInf
*pDbConfig
, bool FwdOnlyCursors
=(bool)wxODBC_FWD_ONLY_CURSORS
);
778 bool WXDLLIMPEXP_ODBC
wxDbFreeConnection(wxDb
*pDb
);
779 void WXDLLIMPEXP_ODBC
wxDbCloseConnections(void);
780 int WXDLLIMPEXP_ODBC
wxDbConnectionsInUse(void);
783 // Writes a message to the wxLog window (stdout usually) when an internal error
784 // situation occurs. This function only works in DEBUG builds
785 const wxChar WXDLLIMPEXP_ODBC
*
786 wxDbLogExtendedErrorMsg(const wxChar
*userText
,
788 const wxChar
*ErrFile
,
792 // This function sets the sql log state for all open wxDb objects
793 bool WXDLLIMPEXP_ODBC
794 wxDbSqlLog(wxDbSqlLogState state
, const wxString
&filename
= SQL_LOG_FILENAME
);
798 // MSW/VC6 ONLY!!! Experimental
799 int WXDLLEXPORT
wxDbCreateDataSource(const wxString
&driverName
, const wxString
&dsn
, const wxString
&description
=wxEmptyString
,
800 bool sysDSN
=false, const wxString
&defDir
=wxEmptyString
, wxWindow
*parent
=NULL
);
803 // This routine allows you to query a driver manager
804 // for a list of available datasources. Call this routine
805 // the first time using SQL_FETCH_FIRST. Continue to call it
806 // using SQL_FETCH_NEXT until you've exhausted the list.
807 bool WXDLLIMPEXP_ODBC
808 wxDbGetDataSource(HENV henv
, wxChar
*Dsn
, SWORD DsnMaxLength
, wxChar
*DsDesc
,
809 SWORD DsDescMaxLength
, UWORD direction
= SQL_FETCH_NEXT
);
812 // Change this to 0 to remove use of all deprecated functions
813 #if wxODBC_BACKWARD_COMPATABILITY
814 //#################################################################################
815 //############### DEPRECATED functions for backward compatibility #################
816 //#################################################################################
818 // Backward compability structures/classes. This will eventually go away
819 const int DB_PATH_MAX
= wxDB_PATH_MAX
;
822 typedef wxDbTableInf wxTableInf
;
823 typedef wxDbColInf wxColInf
;
824 typedef wxDbColInf CcolInf
;
825 typedef wxDbColFor wxColFor
;
826 typedef wxDbSqlTypeInfo SqlTypeInfo
;
827 typedef wxDbSqlTypeInfo wxSqlTypeInfo
;
828 typedef enum wxDbSqlLogState sqlLog
;
829 typedef enum wxDbSqlLogState wxSqlLogState
;
830 typedef enum wxDBMS dbms
;
831 typedef enum wxDBMS DBMS
;
832 typedef wxODBC_ERRORS ODBC_ERRORS
;
833 typedef wxDbConnectInf DbStuff
;
834 typedef wxDbList DbList
;
836 typedef wxTablesInUse CstructTablesInUse
;
839 // Deprecated function names that are replaced by the function names listed above
840 wxDB WXDLLIMPEXP_ODBC
841 *GetDbConnection(DbStuff
*pDbStuff
, bool FwdOnlyCursors
=(bool)wxODBC_FWD_ONLY_CURSORS
);
842 bool WXDLLIMPEXP_ODBC
FreeDbConnection(wxDB
*pDb
);
843 void WXDLLIMPEXP_ODBC
CloseDbConnections(void);
844 int WXDLLIMPEXP_ODBC
NumberDbConnectionsInUse(void);
846 bool SqlLog(sqlLog state
, const wxChar
*filename
= SQL_LOG_FILENAME
);
848 bool WXDLLIMPEXP_ODBC
849 GetDataSource(HENV henv
, char *Dsn
, SWORD DsnMaxLength
, char *DsDesc
, SWORD DsDescMaxLength
,
850 UWORD direction
= SQL_FETCH_NEXT
);
852 #endif // Deprecated structures/classes/functions