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