-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdb.cpp
More file actions
4524 lines (3864 loc) · 159 KB
/
db.cpp
File metadata and controls
4524 lines (3864 loc) · 159 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////////////////////
// Name: src/common/db.cpp
// Purpose: Implementation of the wxDb class. The wxDb class represents a connection
// to an ODBC data source. The wxDb class allows operations on the data
// source such as opening and closing the data source.
// Author: Doug Card
// Modified by: George Tasker
// Bart Jourquin
// Mark Johnson, wxWindows@mj10777.de
// Mods: Dec, 1998:
// -Added support for SQL statement logging and database cataloging
// Mods: April, 1999
// -Added QUERY_ONLY mode support to reduce default number of cursors
// -Added additional SQL logging code
// -Added DEBUG-ONLY tracking of wxTable objects to detect orphaned DB connections
// -Set ODBC option to only read committed writes to the DB so all
// databases operate the same in that respect
// Created: 9.96
// RCS-ID: $Id: db.cpp 52489 2008-03-14 14:14:57Z JS $
// Copyright: (c) 1996 Remstar International, Inc.
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#if wxUSE_ODBC
#ifndef WX_PRECOMP
#include "wx/object.h"
#include "wx/list.h"
#include "wx/string.h"
#include "wx/utils.h"
#include "wx/log.h"
#include "wx/app.h"
#endif
#ifdef DBDEBUG_CONSOLE
#include "wx/ioswrap.h"
#endif
#include "wx/filefn.h"
#include "wx/wxchar.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <ctype.h>
#include "wx/db.h"
// DLL options compatibility check:
WX_CHECK_BUILD_OPTIONS("wxODBC")
WXDLLIMPEXP_DATA_ODBC(wxDbList*) PtrBegDbList = 0;
wxChar const *SQL_LOG_FILENAME = wxT("sqllog.txt");
wxChar const *SQL_CATALOG_FILENAME = wxT("catalog.txt");
#ifdef __WXDEBUG__
#include "wx/thread.h"
extern wxList TablesInUse;
#if wxUSE_THREADS
extern wxCriticalSection csTablesInUse;
#endif // wxUSE_THREADS
#endif
// SQL Log defaults to be used by GetDbConnection
wxDbSqlLogState SQLLOGstate = sqlLogOFF;
static wxString SQLLOGfn = SQL_LOG_FILENAME;
// The wxDb::errorList is copied to this variable when the wxDb object
// is closed. This way, the error list is still available after the
// database object is closed. This is necessary if the database
// connection fails so the calling application can show the operator
// why the connection failed. Note: as each wxDb object is closed, it
// will overwrite the errors of the previously destroyed wxDb object in
// this variable. NOTE: This occurs during a CLOSE, not a FREEing of the
// connection
wxChar DBerrorList[DB_MAX_ERROR_HISTORY][DB_MAX_ERROR_MSG_LEN+1];
// This type defines the return row-struct form
// SQLTablePrivileges, and is used by wxDB::TablePrivileges.
typedef struct
{
wxChar tableQual[128+1];
wxChar tableOwner[128+1];
wxChar tableName[128+1];
wxChar grantor[128+1];
wxChar grantee[128+1];
wxChar privilege[128+1];
wxChar grantable[3+1];
} wxDbTablePrivilegeInfo;
/********** wxDbConnectInf Constructor - form 1 **********/
wxDbConnectInf::wxDbConnectInf()
{
Henv = 0;
freeHenvOnDestroy = false;
Initialize();
} // Constructor
/********** wxDbConnectInf Constructor - form 2 **********/
wxDbConnectInf::wxDbConnectInf(HENV henv, const wxString &dsn, const wxString &userID,
const wxString &password, const wxString &defaultDir,
const wxString &fileType, const wxString &description)
{
Henv = 0;
freeHenvOnDestroy = false;
Initialize();
if (henv)
SetHenv(henv);
else
AllocHenv();
SetDsn(dsn);
SetUserID(userID);
SetPassword(password);
SetDescription(description);
SetFileType(fileType);
SetDefaultDir(defaultDir);
} // wxDbConnectInf Constructor
wxDbConnectInf::~wxDbConnectInf()
{
if (freeHenvOnDestroy)
{
FreeHenv();
}
} // wxDbConnectInf Destructor
/********** wxDbConnectInf::Initialize() **********/
bool wxDbConnectInf::Initialize()
{
freeHenvOnDestroy = false;
if (freeHenvOnDestroy && Henv)
FreeHenv();
Henv = 0;
Dsn[0] = 0;
Uid[0] = 0;
AuthStr[0] = 0;
ConnectionStr[0] = 0;
Description.Empty();
FileType.Empty();
DefaultDir.Empty();
useConnectionStr = false;
return true;
} // wxDbConnectInf::Initialize()
/********** wxDbConnectInf::AllocHenv() **********/
bool wxDbConnectInf::AllocHenv()
{
// This is here to help trap if you are getting a new henv
// without releasing an existing henv
wxASSERT(!Henv);
// Initialize the ODBC Environment for Database Operations
if (SQLAllocEnv(&Henv) != SQL_SUCCESS)
{
wxLogDebug(wxT("A problem occurred while trying to get a connection to the data source"));
return false;
}
freeHenvOnDestroy = true;
return true;
} // wxDbConnectInf::AllocHenv()
void wxDbConnectInf::FreeHenv()
{
wxASSERT(Henv);
if (Henv)
SQLFreeEnv(Henv);
Henv = 0;
freeHenvOnDestroy = false;
} // wxDbConnectInf::FreeHenv()
void wxDbConnectInf::SetDsn(const wxString &dsn)
{
wxASSERT(dsn.length() < WXSIZEOF(Dsn));
wxStrncpy(Dsn, dsn, WXSIZEOF(Dsn)-1);
Dsn[WXSIZEOF(Dsn)-1] = 0; // Prevent buffer overrun
} // wxDbConnectInf::SetDsn()
void wxDbConnectInf::SetUserID(const wxString &uid)
{
wxASSERT(uid.length() < WXSIZEOF(Uid));
wxStrncpy(Uid, uid, WXSIZEOF(Uid)-1);
Uid[WXSIZEOF(Uid)-1] = 0; // Prevent buffer overrun
} // wxDbConnectInf::SetUserID()
void wxDbConnectInf::SetPassword(const wxString &password)
{
wxASSERT(password.length() < WXSIZEOF(AuthStr));
wxStrncpy(AuthStr, password, WXSIZEOF(AuthStr)-1);
AuthStr[WXSIZEOF(AuthStr)-1] = 0; // Prevent buffer overrun
} // wxDbConnectInf::SetPassword()
void wxDbConnectInf::SetConnectionStr(const wxString &connectStr)
{
wxASSERT(connectStr.length() < WXSIZEOF(ConnectionStr));
useConnectionStr = wxStrlen(connectStr) > 0;
wxStrncpy(ConnectionStr, connectStr, WXSIZEOF(ConnectionStr)-1);
ConnectionStr[WXSIZEOF(ConnectionStr)-1] = 0; // Prevent buffer overrun
} // wxDbConnectInf::SetConnectionStr()
/********** wxDbColFor Constructor **********/
wxDbColFor::wxDbColFor()
{
Initialize();
} // wxDbColFor::wxDbColFor()
/********** wxDbColFor::Initialize() **********/
void wxDbColFor::Initialize()
{
s_Field.Empty();
int i;
for (i=0; i<7; i++)
{
s_Format[i].Empty();
s_Amount[i].Empty();
i_Amount[i] = 0;
}
i_Nation = 0; // 0=EU, 1=UK, 2=International, 3=US
i_dbDataType = 0;
i_sqlDataType = 0;
Format(1,DB_DATA_TYPE_VARCHAR,0,0,0); // the Function that does the work
} // wxDbColFor::Initialize()
/********** wxDbColFor::Format() **********/
int wxDbColFor::Format(int Nation, int dbDataType, SWORD sqlDataType,
short columnLength, short decimalDigits)
{
// ----------------------------------------------------------------------------------------
// -- 19991224 : mj10777 : Create
// There is still a lot of work to do here, but it is a start
// It handles all the basic data-types that I have run into up to now
// The main work will have be with Dates and float Formatting
// (US 1,000.00 ; EU 1.000,00)
// There are wxWindow plans for locale support and the new wxDateTime. If
// they define some constants (wxEUROPEAN) that can be gloably used,
// they should be used here.
// ----------------------------------------------------------------------------------------
// There should also be a function to scan in a string to fill the variable
// ----------------------------------------------------------------------------------------
wxString tempStr;
i_Nation = Nation; // 0 = timestamp , 1=EU, 2=UK, 3=International, 4=US
i_dbDataType = dbDataType;
i_sqlDataType = sqlDataType;
s_Field.Printf(wxT("%s%d"),s_Amount[1].c_str(),i_Amount[1]); // OK for VARCHAR, INTEGER and FLOAT
if (i_dbDataType == 0) // Filter unsupported dbDataTypes
{
if ((i_sqlDataType == SQL_VARCHAR)
#if wxUSE_UNICODE
#if defined(SQL_WCHAR)
|| (i_sqlDataType == SQL_WCHAR)
#endif
#if defined(SQL_WVARCHAR)
|| (i_sqlDataType == SQL_WVARCHAR)
#endif
#endif
|| (i_sqlDataType == SQL_LONGVARCHAR))
i_dbDataType = DB_DATA_TYPE_VARCHAR;
if ((i_sqlDataType == SQL_C_DATE) || (i_sqlDataType == SQL_C_TIMESTAMP))
i_dbDataType = DB_DATA_TYPE_DATE;
if (i_sqlDataType == SQL_C_BIT)
i_dbDataType = DB_DATA_TYPE_INTEGER;
if (i_sqlDataType == SQL_NUMERIC)
i_dbDataType = DB_DATA_TYPE_VARCHAR; // glt - ??? is this right?
if (i_sqlDataType == SQL_REAL)
i_dbDataType = DB_DATA_TYPE_FLOAT;
if (i_sqlDataType == SQL_C_BINARY)
i_dbDataType = DB_DATA_TYPE_BLOB;
}
if ((i_dbDataType == DB_DATA_TYPE_INTEGER) && (i_sqlDataType == SQL_C_DOUBLE))
{ // DBASE Numeric
i_dbDataType = DB_DATA_TYPE_FLOAT;
}
switch(i_dbDataType) // TBD: Still a lot of proper formatting to do
{
case DB_DATA_TYPE_VARCHAR:
s_Field = wxT("%s");
break;
case DB_DATA_TYPE_INTEGER:
s_Field = wxT("%d");
break;
case DB_DATA_TYPE_FLOAT:
if (decimalDigits == 0)
decimalDigits = 2;
tempStr.Printf(wxT("%%%d.%d"), columnLength, decimalDigits);
s_Field.Printf(wxT("%sf"), tempStr.c_str());
break;
case DB_DATA_TYPE_DATE:
if (i_Nation == 0) // timestamp YYYY-MM-DD HH:MM:SS.SSS (tested for SYBASE)
{
s_Field = wxT("%04d-%02d-%02d %02d:%02d:%02d.%03d");
}
if (i_Nation == 1) // European DD.MM.YYYY HH:MM:SS.SSS
{
s_Field = wxT("%02d.%02d.%04d %02d:%02d:%02d.%03d");
}
if (i_Nation == 2) // UK DD/MM/YYYY HH:MM:SS.SSS
{
s_Field = wxT("%02d/%02d/%04d %02d:%02d:%02d.%03d");
}
if (i_Nation == 3) // International YYYY-MM-DD HH:MM:SS.SSS
{
s_Field = wxT("%04d-%02d-%02d %02d:%02d:%02d.%03d");
}
if (i_Nation == 4) // US MM/DD/YYYY HH:MM:SS.SSS
{
s_Field = wxT("%02d/%02d/%04d %02d:%02d:%02d.%03d");
}
break;
case DB_DATA_TYPE_BLOB:
s_Field.Printf(wxT("Unable to format(%d)-SQL(%d)"), dbDataType,sqlDataType); //
break;
default:
s_Field.Printf(wxT("Unknown Format(%d)-SQL(%d)"), dbDataType,sqlDataType); //
break;
};
return TRUE;
} // wxDbColFor::Format()
/********** wxDbColInf Constructor **********/
wxDbColInf::wxDbColInf()
{
Initialize();
} // wxDbColInf::wxDbColInf()
/********** wxDbColInf Destructor ********/
wxDbColInf::~wxDbColInf()
{
if (pColFor)
delete pColFor;
pColFor = NULL;
} // wxDbColInf::~wxDbColInf()
bool wxDbColInf::Initialize()
{
catalog[0] = 0;
schema[0] = 0;
tableName[0] = 0;
colName[0] = 0;
sqlDataType = 0;
typeName[0] = 0;
columnLength = 0;
bufferSize = 0;
decimalDigits = 0;
numPrecRadix = 0;
nullable = 0;
remarks[0] = 0;
dbDataType = 0;
PkCol = 0;
PkTableName[0] = 0;
FkCol = 0;
FkTableName[0] = 0;
pColFor = NULL;
return true;
} // wxDbColInf::Initialize()
/********** wxDbTableInf Constructor ********/
wxDbTableInf::wxDbTableInf()
{
Initialize();
} // wxDbTableInf::wxDbTableInf()
/********** wxDbTableInf Constructor ********/
wxDbTableInf::~wxDbTableInf()
{
if (pColInf)
delete [] pColInf;
pColInf = NULL;
} // wxDbTableInf::~wxDbTableInf()
bool wxDbTableInf::Initialize()
{
tableName[0] = 0;
tableType[0] = 0;
tableRemarks[0] = 0;
numCols = 0;
pColInf = NULL;
return true;
} // wxDbTableInf::Initialize()
/********** wxDbInf Constructor *************/
wxDbInf::wxDbInf()
{
Initialize();
} // wxDbInf::wxDbInf()
/********** wxDbInf Destructor *************/
wxDbInf::~wxDbInf()
{
if (pTableInf)
delete [] pTableInf;
pTableInf = NULL;
} // wxDbInf::~wxDbInf()
/********** wxDbInf::Initialize() *************/
bool wxDbInf::Initialize()
{
catalog[0] = 0;
schema[0] = 0;
numTables = 0;
pTableInf = NULL;
return true;
} // wxDbInf::Initialize()
/********** wxDb Constructor **********/
wxDb::wxDb(const HENV &aHenv, bool FwdOnlyCursors)
{
// Copy the HENV into the db class
henv = aHenv;
fwdOnlyCursors = FwdOnlyCursors;
initialize();
} // wxDb::wxDb()
/********** wxDb Destructor **********/
wxDb::~wxDb()
{
wxASSERT_MSG(!IsCached(),wxT("Cached connections must not be manually deleted, use\nwxDbFreeConnection() or wxDbCloseConnections()."));
if (IsOpen())
{
Close();
}
} // wxDb destructor
/********** PRIVATE! wxDb::initialize PRIVATE! **********/
/********** wxDb::initialize() **********/
void wxDb::initialize()
/*
* Private member function that sets all wxDb member variables to
* known values at creation of the wxDb
*/
{
int i;
fpSqlLog = 0; // Sql Log file pointer
sqlLogState = sqlLogOFF; // By default, logging is turned off
nTables = 0;
dbmsType = dbmsUNIDENTIFIED;
wxStrcpy(sqlState,wxEmptyString);
wxStrcpy(errorMsg,wxEmptyString);
nativeError = cbErrorMsg = 0;
for (i = 0; i < DB_MAX_ERROR_HISTORY; i++)
wxStrcpy(errorList[i], wxEmptyString);
// Init typeInf structures
typeInfVarchar.TypeName.Empty();
typeInfVarchar.FsqlType = 0;
typeInfVarchar.Precision = 0;
typeInfVarchar.CaseSensitive = 0;
typeInfVarchar.MaximumScale = 0;
typeInfInteger.TypeName.Empty();
typeInfInteger.FsqlType = 0;
typeInfInteger.Precision = 0;
typeInfInteger.CaseSensitive = 0;
typeInfInteger.MaximumScale = 0;
typeInfFloat.TypeName.Empty();
typeInfFloat.FsqlType = 0;
typeInfFloat.Precision = 0;
typeInfFloat.CaseSensitive = 0;
typeInfFloat.MaximumScale = 0;
typeInfDate.TypeName.Empty();
typeInfDate.FsqlType = 0;
typeInfDate.Precision = 0;
typeInfDate.CaseSensitive = 0;
typeInfDate.MaximumScale = 0;
typeInfBlob.TypeName.Empty();
typeInfBlob.FsqlType = 0;
typeInfBlob.Precision = 0;
typeInfBlob.CaseSensitive = 0;
typeInfBlob.MaximumScale = 0;
typeInfMemo.TypeName.Empty();
typeInfMemo.FsqlType = 0;
typeInfMemo.Precision = 0;
typeInfMemo.CaseSensitive = 0;
typeInfMemo.MaximumScale = 0;
// Error reporting is turned OFF by default
silent = true;
// Allocate a data source connection handle
if (SQLAllocConnect(henv, &hdbc) != SQL_SUCCESS)
DispAllErrors(henv);
// Initialize the db status flag
DB_STATUS = 0;
// Mark database as not open as of yet
dbIsOpen = false;
dbIsCached = false;
dbOpenedWithConnectionString = false;
} // wxDb::initialize()
/********** PRIVATE! wxDb::convertUserID PRIVATE! **********/
//
// NOTE: Return value from this function MUST be copied
// immediately, as the value is not good after
// this function has left scope.
//
const wxChar *wxDb::convertUserID(const wxChar *userID, wxString &UserID)
{
if (userID)
{
if (!wxStrlen(userID))
UserID = uid;
else
UserID = userID;
}
else
UserID.Empty();
// dBase does not use user names, and some drivers fail if you try to pass one
if ( Dbms() == dbmsDBASE
|| Dbms() == dbmsXBASE_SEQUITER )
UserID.Empty();
// Some databases require user names to be specified in uppercase,
// so force the name to uppercase
if ((Dbms() == dbmsORACLE) ||
(Dbms() == dbmsMAXDB))
UserID = UserID.Upper();
return UserID.c_str();
} // wxDb::convertUserID()
bool wxDb::determineDataTypes(bool failOnDataTypeUnsupported)
{
size_t iIndex;
// These are the possible SQL types we check for use against the datasource we are connected
// to for the purpose of determining which data type to use for the basic character strings
// column types
//
// NOTE: The first type in this enumeration that is determined to be supported by the
// datasource/driver is the one that will be used.
SWORD PossibleSqlCharTypes[] = {
#if wxUSE_UNICODE && defined(SQL_WVARCHAR)
SQL_WVARCHAR,
#endif
SQL_VARCHAR,
#if wxUSE_UNICODE && defined(SQL_WVARCHAR)
SQL_WCHAR,
#endif
SQL_CHAR
};
// These are the possible SQL types we check for use against the datasource we are connected
// to for the purpose of determining which data type to use for the basic non-floating point
// column types
//
// NOTE: The first type in this enumeration that is determined to be supported by the
// datasource/driver is the one that will be used.
SWORD PossibleSqlIntegerTypes[] = {
SQL_INTEGER
};
// These are the possible SQL types we check for use against the datasource we are connected
// to for the purpose of determining which data type to use for the basic floating point number
// column types
//
// NOTE: The first type in this enumeration that is determined to be supported by the
// datasource/driver is the one that will be used.
SWORD PossibleSqlFloatTypes[] = {
SQL_DOUBLE,
SQL_REAL,
SQL_FLOAT,
SQL_DECIMAL,
SQL_NUMERIC
};
// These are the possible SQL types we check for use agains the datasource we are connected
// to for the purpose of determining which data type to use for the date/time column types
//
// NOTE: The first type in this enumeration that is determined to be supported by the
// datasource/driver is the one that will be used.
SWORD PossibleSqlDateTypes[] = {
SQL_TIMESTAMP,
SQL_DATE,
#ifdef SQL_DATETIME
SQL_DATETIME
#endif
};
// These are the possible SQL types we check for use agains the datasource we are connected
// to for the purpose of determining which data type to use for the BLOB column types.
//
// NOTE: The first type in this enumeration that is determined to be supported by the
// datasource/driver is the one that will be used.
SWORD PossibleSqlBlobTypes[] = {
SQL_LONGVARBINARY,
SQL_VARBINARY
};
// These are the possible SQL types we check for use agains the datasource we are connected
// to for the purpose of determining which data type to use for the MEMO column types
// (a type which allow to store large strings; like VARCHAR just with a bigger precision)
//
// NOTE: The first type in this enumeration that is determined to be supported by the
// datasource/driver is the one that will be used.
SWORD PossibleSqlMemoTypes[] = {
SQL_LONGVARCHAR,
};
// Query the data source regarding data type information
//
// The way it was determined which SQL data types to use was by calling SQLGetInfo
// for all of the possible SQL data types to see which ones were supported. If
// a type is not supported, the SQLFetch() that's called from getDataTypeInfo()
// fails with SQL_NO_DATA_FOUND. This is ugly because I'm sure the three SQL data
// types I've selected below will not always be what we want. These are just
// what happened to work against an Oracle 7/Intersolv combination. The following is
// a complete list of the results I got back against the Oracle 7 database:
//
// SQL_BIGINT SQL_NO_DATA_FOUND
// SQL_BINARY SQL_NO_DATA_FOUND
// SQL_BIT SQL_NO_DATA_FOUND
// SQL_CHAR type name = 'CHAR', Precision = 255
// SQL_DATE SQL_NO_DATA_FOUND
// SQL_DECIMAL type name = 'NUMBER', Precision = 38
// SQL_DOUBLE type name = 'NUMBER', Precision = 15
// SQL_FLOAT SQL_NO_DATA_FOUND
// SQL_INTEGER SQL_NO_DATA_FOUND
// SQL_LONGVARBINARY type name = 'LONG RAW', Precision = 2 billion
// SQL_LONGVARCHAR type name = 'LONG', Precision = 2 billion
// SQL_NUMERIC SQL_NO_DATA_FOUND
// SQL_REAL SQL_NO_DATA_FOUND
// SQL_SMALLINT SQL_NO_DATA_FOUND
// SQL_TIME SQL_NO_DATA_FOUND
// SQL_TIMESTAMP type name = 'DATE', Precision = 19
// SQL_VARBINARY type name = 'RAW', Precision = 255
// SQL_VARCHAR type name = 'VARCHAR2', Precision = 2000
// =====================================================================
// Results from a Microsoft Access 7.0 db, using a driver from Microsoft
//
// SQL_VARCHAR type name = 'TEXT', Precision = 255
// SQL_TIMESTAMP type name = 'DATETIME'
// SQL_DECIMAL SQL_NO_DATA_FOUND
// SQL_NUMERIC type name = 'CURRENCY', Precision = 19
// SQL_FLOAT SQL_NO_DATA_FOUND
// SQL_REAL type name = 'SINGLE', Precision = 7
// SQL_DOUBLE type name = 'DOUBLE', Precision = 15
// SQL_INTEGER type name = 'LONG', Precision = 10
// Query the data source for info about itself
if (!getDbInfo(failOnDataTypeUnsupported))
return false;
// --------------- Varchar - (Variable length character string) ---------------
for (iIndex = 0; iIndex < WXSIZEOF(PossibleSqlCharTypes) &&
!getDataTypeInfo(PossibleSqlCharTypes[iIndex], typeInfVarchar); ++iIndex)
{}
if (iIndex < WXSIZEOF(PossibleSqlCharTypes))
typeInfVarchar.FsqlType = PossibleSqlCharTypes[iIndex];
else if (failOnDataTypeUnsupported)
return false;
// --------------- Float ---------------
for (iIndex = 0; iIndex < WXSIZEOF(PossibleSqlFloatTypes) &&
!getDataTypeInfo(PossibleSqlFloatTypes[iIndex], typeInfFloat); ++iIndex)
{}
if (iIndex < WXSIZEOF(PossibleSqlFloatTypes))
typeInfFloat.FsqlType = PossibleSqlFloatTypes[iIndex];
else if (failOnDataTypeUnsupported)
return false;
// --------------- Integer -------------
for (iIndex = 0; iIndex < WXSIZEOF(PossibleSqlIntegerTypes) &&
!getDataTypeInfo(PossibleSqlIntegerTypes[iIndex], typeInfInteger); ++iIndex)
{}
if (iIndex < WXSIZEOF(PossibleSqlIntegerTypes))
typeInfInteger.FsqlType = PossibleSqlIntegerTypes[iIndex];
else if (failOnDataTypeUnsupported)
{
// If no non-floating point data types are supported, we'll
// use the type assigned for floats to store integers as well
if (!getDataTypeInfo(typeInfFloat.FsqlType, typeInfInteger))
{
if (failOnDataTypeUnsupported)
return false;
}
else
typeInfInteger.FsqlType = typeInfFloat.FsqlType;
}
// --------------- Date/Time ---------------
for (iIndex = 0; iIndex < WXSIZEOF(PossibleSqlDateTypes) &&
!getDataTypeInfo(PossibleSqlDateTypes[iIndex], typeInfDate); ++iIndex)
{}
if (iIndex < WXSIZEOF(PossibleSqlDateTypes))
typeInfDate.FsqlType = PossibleSqlDateTypes[iIndex];
else if (failOnDataTypeUnsupported)
return false;
// --------------- BLOB ---------------
for (iIndex = 0; iIndex < WXSIZEOF(PossibleSqlBlobTypes) &&
!getDataTypeInfo(PossibleSqlBlobTypes[iIndex], typeInfBlob); ++iIndex)
{}
if (iIndex < WXSIZEOF(PossibleSqlBlobTypes))
typeInfBlob.FsqlType = PossibleSqlBlobTypes[iIndex];
else if (failOnDataTypeUnsupported)
return false;
// --------------- MEMO ---------------
for (iIndex = 0; iIndex < WXSIZEOF(PossibleSqlMemoTypes) &&
!getDataTypeInfo(PossibleSqlMemoTypes[iIndex], typeInfMemo); ++iIndex)
{}
if (iIndex < WXSIZEOF(PossibleSqlMemoTypes))
typeInfMemo.FsqlType = PossibleSqlMemoTypes[iIndex];
else if (failOnDataTypeUnsupported)
return false;
return true;
} // wxDb::determineDataTypes
bool wxDb::open(bool failOnDataTypeUnsupported)
{
/*
If using Intersolv branded ODBC drivers, this is the place where you would substitute
your branded driver license information
SQLSetConnectOption(hdbc, 1041, (UDWORD) wxEmptyString);
SQLSetConnectOption(hdbc, 1042, (UDWORD) wxEmptyString);
*/
// Mark database as open
dbIsOpen = true;
// Allocate a statement handle for the database connection
if (SQLAllocStmt(hdbc, &hstmt) != SQL_SUCCESS)
return(DispAllErrors(henv, hdbc));
// Set Connection Options
if (!setConnectionOptions())
return false;
if (!determineDataTypes(failOnDataTypeUnsupported))
return false;
#ifdef DBDEBUG_CONSOLE
cout << wxT("VARCHAR DATA TYPE: ") << typeInfVarchar.TypeName << endl;
cout << wxT("INTEGER DATA TYPE: ") << typeInfInteger.TypeName << endl;
cout << wxT("FLOAT DATA TYPE: ") << typeInfFloat.TypeName << endl;
cout << wxT("DATE DATA TYPE: ") << typeInfDate.TypeName << endl;
cout << wxT("BLOB DATA TYPE: ") << typeInfBlob.TypeName << endl;
cout << wxT("MEMO DATA TYPE: ") << typeInfMemo.TypeName << endl;
cout << endl;
#endif
// Completed Successfully
return true;
}
bool wxDb::Open(const wxString& inConnectStr, bool failOnDataTypeUnsupported)
{
wxASSERT(inConnectStr.length());
return Open(inConnectStr, NULL, failOnDataTypeUnsupported);
}
bool wxDb::Open(const wxString& inConnectStr, SQLHWND parentWnd, bool failOnDataTypeUnsupported)
{
dsn = wxEmptyString;
uid = wxEmptyString;
authStr = wxEmptyString;
RETCODE retcode;
if (!FwdOnlyCursors())
{
// Specify that the ODBC cursor library be used, if needed. This must be
// specified before the connection is made.
retcode = SQLSetConnectOption(hdbc, SQL_ODBC_CURSORS, SQL_CUR_USE_IF_NEEDED);
#ifdef DBDEBUG_CONSOLE
if (retcode == SQL_SUCCESS)
cout << wxT("SQLSetConnectOption(CURSOR_LIB) successful") << endl;
else
cout << wxT("SQLSetConnectOption(CURSOR_LIB) failed") << endl;
#else
wxUnusedVar(retcode);
#endif
}
// Connect to the data source
SQLTCHAR outConnectBuffer[SQL_MAX_CONNECTSTR_LEN+1]; // MS recommends at least 1k buffer
short outConnectBufferLen;
inConnectionStr = inConnectStr;
retcode = SQLDriverConnect(hdbc, parentWnd, (SQLTCHAR FAR *)inConnectionStr.c_str(),
(SWORD)inConnectionStr.length(), (SQLTCHAR FAR *)outConnectBuffer,
WXSIZEOF(outConnectBuffer), &outConnectBufferLen, SQL_DRIVER_COMPLETE );
if ((retcode != SQL_SUCCESS) &&
(retcode != SQL_SUCCESS_WITH_INFO))
return(DispAllErrors(henv, hdbc));
outConnectBuffer[outConnectBufferLen] = 0;
outConnectionStr = outConnectBuffer;
dbOpenedWithConnectionString = true;
return open(failOnDataTypeUnsupported);
}
/********** wxDb::Open() **********/
bool wxDb::Open(const wxString &Dsn, const wxString &Uid, const wxString &AuthStr, bool failOnDataTypeUnsupported)
{
wxASSERT(!Dsn.empty());
dsn = Dsn;
uid = Uid;
authStr = AuthStr;
inConnectionStr = wxEmptyString;
outConnectionStr = wxEmptyString;
RETCODE retcode;
if (!FwdOnlyCursors())
{
// Specify that the ODBC cursor library be used, if needed. This must be
// specified before the connection is made.
retcode = SQLSetConnectOption(hdbc, SQL_ODBC_CURSORS, SQL_CUR_USE_IF_NEEDED);
#ifdef DBDEBUG_CONSOLE
if (retcode == SQL_SUCCESS)
cout << wxT("SQLSetConnectOption(CURSOR_LIB) successful") << endl;
else
cout << wxT("SQLSetConnectOption(CURSOR_LIB) failed") << endl;
#else
wxUnusedVar( retcode );
#endif
}
// Connect to the data source
retcode = SQLConnect(hdbc, (SQLTCHAR FAR *) dsn.c_str(), SQL_NTS,
(SQLTCHAR FAR *) uid.c_str(), SQL_NTS,
(SQLTCHAR FAR *) authStr.c_str(), SQL_NTS);
if ((retcode != SQL_SUCCESS) &&
(retcode != SQL_SUCCESS_WITH_INFO))
return(DispAllErrors(henv, hdbc));
return open(failOnDataTypeUnsupported);
} // wxDb::Open()
bool wxDb::Open(wxDbConnectInf *dbConnectInf, bool failOnDataTypeUnsupported)
{
wxASSERT(dbConnectInf);
// Use the connection string if one is present
if (dbConnectInf->UseConnectionStr())
return Open(dbConnectInf->GetConnectionStr(), failOnDataTypeUnsupported);
else
return Open(dbConnectInf->GetDsn(), dbConnectInf->GetUserID(),
dbConnectInf->GetPassword(), failOnDataTypeUnsupported);
} // wxDb::Open()
bool wxDb::Open(wxDb *copyDb)
{
dsn = copyDb->GetDatasourceName();
uid = copyDb->GetUsername();
authStr = copyDb->GetPassword();
inConnectionStr = copyDb->GetConnectionInStr();
outConnectionStr = copyDb->GetConnectionOutStr();
RETCODE retcode;
if (!FwdOnlyCursors())
{
// Specify that the ODBC cursor library be used, if needed. This must be
// specified before the connection is made.
retcode = SQLSetConnectOption(hdbc, SQL_ODBC_CURSORS, SQL_CUR_USE_IF_NEEDED);
#ifdef DBDEBUG_CONSOLE
if (retcode == SQL_SUCCESS)
cout << wxT("SQLSetConnectOption(CURSOR_LIB) successful") << endl;
else
cout << wxT("SQLSetConnectOption(CURSOR_LIB) failed") << endl;
#else
wxUnusedVar( retcode );
#endif
}
if (copyDb->OpenedWithConnectionString())
{
// Connect to the data source
SQLTCHAR outConnectBuffer[SQL_MAX_CONNECTSTR_LEN+1];
short outConnectBufferLen;
inConnectionStr = copyDb->GetConnectionInStr();
retcode = SQLDriverConnect(hdbc, NULL, (SQLTCHAR FAR *)inConnectionStr.c_str(),
(SWORD)inConnectionStr.length(), (SQLTCHAR FAR *)outConnectBuffer,
WXSIZEOF(outConnectBuffer), &outConnectBufferLen, SQL_DRIVER_COMPLETE);
if ((retcode != SQL_SUCCESS) &&
(retcode != SQL_SUCCESS_WITH_INFO))
return(DispAllErrors(henv, hdbc));
outConnectBuffer[outConnectBufferLen] = 0;
outConnectionStr = outConnectBuffer;
dbOpenedWithConnectionString = true;
}
else
{
// Connect to the data source
retcode = SQLConnect(hdbc, (SQLTCHAR FAR *) dsn.c_str(), SQL_NTS,
(SQLTCHAR FAR *) uid.c_str(), SQL_NTS,
(SQLTCHAR FAR *) authStr.c_str(), SQL_NTS);
}
if ((retcode != SQL_SUCCESS) &&
(retcode != SQL_SUCCESS_WITH_INFO))
return(DispAllErrors(henv, hdbc));
/*
If using Intersolv branded ODBC drivers, this is the place where you would substitute
your branded driver license information
SQLSetConnectOption(hdbc, 1041, (UDWORD) wxEmptyString);
SQLSetConnectOption(hdbc, 1042, (UDWORD) wxEmptyString);
*/