-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnanodbc.cpp
3533 lines (3088 loc) · 105 KB
/
nanodbc.cpp
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
//! \file nanodbc.cpp Implementation details.
#ifndef DOXYGEN
// ASCII art banners are helpful for code editors with a minimap display.
// Generated with http://patorjk.com/software/taag/#p=display&v=0&f=Colossal
#include "nanodbc.h"
#include <algorithm>
#include <clocale>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <map>
#include <sstream>
// User may redefine NANODBC_ASSERT macro in nanodbc.h
#ifndef NANODBC_ASSERT
#include <cassert>
#define NANODBC_ASSERT(expr) assert(expr)
#endif
#ifdef NANODBC_USE_BOOST_CONVERT
#include <boost/locale/encoding_utf.hpp>
#else
#include <codecvt>
#endif
#if defined(_MSC_VER) && _MSC_VER <= 1800
// silence spurious Visual C++ warnings
#pragma warning(disable:4244) // warning about integer conversion issues.
#pragma warning(disable:4312) // warning about 64-bit portability issues.
#pragma warning(disable:4996) // warning about snprintf() deprecated.
#endif
#ifdef __APPLE__
// silence spurious OS X deprecation warnings
#define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_6
#endif
#ifdef _WIN32
// needs to be included above sql.h for windows
#define NOMINMAX
#include <windows.h>
#endif
#include <sql.h>
#include <sqlext.h>
// Default to ODBC version defined by NANODBC_ODBC_VERSION if provided.
#ifndef NANODBC_ODBC_VERSION
#ifdef SQL_OV_ODBC3_80
// Otherwise, use ODBC v3.8 if it's available...
#define NANODBC_ODBC_VERSION SQL_OV_ODBC3_80
#else
// or fallback to ODBC v3.x.
#define NANODBC_ODBC_VERSION SQL_OV_ODBC3
#endif
#endif
// 888 888 d8b 888
// 888 888 Y8P 888
// 888 888 888
// 888 888 88888b. 888 .d8888b .d88b. .d88888 .d88b.
// 888 888 888 "88b 888 d88P" d88""88b d88" 888 d8P Y8b
// 888 888 888 888 888 888 888 888 888 888 88888888
// Y88b. .d88P 888 888 888 Y88b. Y88..88P Y88b 888 Y8b.
// "Y88888P" 888 888 888 "Y8888P "Y88P" "Y88888 "Y8888
// MARK: Unicode -
#if defined(_MSC_VER)
#ifdef NANODBC_USE_UNICODE
#define NANODBC_TEXT(s) L ## s
#define NANODBC_SNPRINTF std::swprintf
#define NANODBC_STRFTIME std::wcsftime
#define NANODBC_STRLEN std::wcslen
#define NANADBC_STRNCMP std::wcsncmp
#define NANODBC_UNICODE(f) f ## W
#define NANODBC_SQLCHAR SQLWCHAR
#else
#define NANODBC_TEXT(s) s
#define NANODBC_SNPRINTF(buffer, count, format, ...) _snprintf_s(buffer, count, _TRUNCATE, format, __VA_ARGS__)
#define NANODBC_STRFTIME std::strftime
#define NANODBC_STRLEN std::strlen
#define NANADBC_STRNCMP std::strncmp
#define NANODBC_UNICODE(f) f
#define NANODBC_SQLCHAR SQLCHAR
// Disable unicode in sqlucode.h on Windows when NANODBC_USE_UNICODE
// is not defined. This is required because unicode is enabled by
// default on many Windows systems.
#define SQL_NOUNICODEMAP
#endif
#else
#ifdef NANODBC_USE_UNICODE
#define NANODBC_TEXT(s) L ## s
#define NANODBC_SNPRINTF std::swprintf
#define NANODBC_STRFTIME std::wcsftime
#define NANODBC_STRLEN std::wcslen
#define NANADBC_STRNCMP std::wcsncmp
#define NANODBC_UNICODE(f) f ## W
#define NANODBC_SQLCHAR SQLWCHAR
#else
#define NANODBC_TEXT(s) s
#define NANODBC_SNPRINTF std::snprintf
#define NANODBC_STRFTIME std::strftime
#define NANODBC_STRLEN std::strlen
#define NANADBC_STRNCMP std::strncmp
#define NANODBC_UNICODE(f) f
#define NANODBC_SQLCHAR SQLCHAR
#endif
#endif
// .d88888b. 8888888b. 888888b. .d8888b. 888b d888
// d88P" "Y88b 888 "Y88b 888 "88b d88P Y88b 8888b d8888
// 888 888 888 888 888 .88P 888 888 88888b.d88888
// 888 888 888 888 8888888K. 888 888Y88888P888 8888b. .d8888b 888d888 .d88b. .d8888b
// 888 888 888 888 888 "Y88b 888 888 Y888P 888 "88b d88P" 888P" d88""88b 88K
// 888 888 888 888 888 888 888 888 888 Y8P 888 .d888888 888 888 888 888 "Y8888b.
// Y88b. .d88P 888 .d88P 888 d88P Y88b d88P 888 " 888 888 888 Y88b. 888 Y88..88P X88
// "Y88888P" 8888888P" 8888888P" "Y8888P" 888 888 "Y888888 "Y8888P 888 "Y88P" 88888P'
// MARK: ODBC Macros -
#define NANODBC_STRINGIZE_I(text) #text
#define NANODBC_STRINGIZE(text) NANODBC_STRINGIZE_I(text)
// By making all calls to ODBC functions through this macro, we can easily get
// runtime debugging information of which ODBC functions are being called,
// in what order, and with what parameters by defining NANODBC_ODBC_API_DEBUG.
#ifdef NANODBC_ODBC_API_DEBUG
#include <iostream>
#define NANODBC_CALL_RC(FUNC, RC, ...) \
do \
{ \
std::cerr << __FILE__ ":" NANODBC_STRINGIZE(__LINE__) " " \
NANODBC_STRINGIZE(FUNC) "(" #__VA_ARGS__ ")" << std::endl; \
RC = FUNC(__VA_ARGS__); \
} while(false) \
/**/
#define NANODBC_CALL(FUNC, ...) \
do \
{ \
std::cerr << __FILE__ ":" NANODBC_STRINGIZE(__LINE__) " " \
NANODBC_STRINGIZE(FUNC) "(" #__VA_ARGS__ ")" << std::endl; \
FUNC(__VA_ARGS__); \
} while(false) \
/**/
#else
#define NANODBC_CALL_RC(FUNC, RC, ...) RC = FUNC(__VA_ARGS__)
#define NANODBC_CALL(FUNC, ...) FUNC(__VA_ARGS__)
#endif
// 8888888888 888 888 888 888 d8b
// 888 888 888 888 888 Y8P
// 888 888 888 888 888
// 8888888 888d888 888d888 .d88b. 888d888 8888888888 8888b. 88888b. .d88888 888 888 88888b. .d88b.
// 888 888P" 888P" d88""88b 888P" 888 888 "88b 888 "88b d88" 888 888 888 888 "88b d88P"88b
// 888 888 888 888 888 888 888 888 .d888888 888 888 888 888 888 888 888 888 888 888
// 888 888 888 Y88..88P 888 888 888 888 888 888 888 Y88b 888 888 888 888 888 Y88b 888
// 8888888888 888 888 "Y88P" 888 888 888 "Y888888 888 888 "Y88888 888 888 888 888 "Y88888
// 888
// Y8b d88P
// "Y88P"
// MARK: Error Handling -
namespace
{
#ifdef NANODBC_ODBC_API_DEBUG
inline nanodbc::string_type return_code(RETCODE rc)
{
switch(rc)
{
case SQL_SUCCESS: return NANODBC_UNICODE("SQL_SUCCESS");
case SQL_SUCCESS_WITH_INFO: return NANODBC_UNICODE("SQL_SUCCESS_WITH_INFO");
case SQL_ERROR: return NANODBC_UNICODE("SQL_ERROR");
case SQL_INVALID_HANDLE: return NANODBC_UNICODE("SQL_INVALID_HANDLE");
case SQL_NO_DATA: return NANODBC_UNICODE("SQL_NO_DATA");
case SQL_NEED_DATA: return NANODBC_UNICODE("SQL_NEED_DATA");
case SQL_STILL_EXECUTING: return NANODBC_UNICODE("SQL_STILL_EXECUTING");
}
NANODBC_ASSERT(0);
return "unknown"; // should never make it here
}
#endif
// Easy way to check if a return code signifies success.
inline bool success(RETCODE rc)
{
#ifdef NANODBC_ODBC_API_DEBUG
std::cerr << "<-- rc: " << return_code(rc) << " | ";
#endif
return rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO;
}
// Returns the array size.
template<typename T, std::size_t N>
inline std::size_t arrlen(T(&)[N])
{
return N;
}
// Operates like strlen() on a character array.
template<typename T, std::size_t N>
inline std::size_t strarrlen(T(&a)[N])
{
const T* s = &a[0];
std::size_t i = 0;
while(*s++ && i < N) i++;
return i;
}
inline void convert(const std::wstring& in, std::string& out)
{
#ifdef NANODBC_USE_BOOST_CONVERT
using boost::locale::conv::utf_to_utf;
out = utf_to_utf<char>(in.c_str(), in.c_str() + in.size());
#else
out = std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(in);
#endif
}
#ifdef NANODBC_USE_UNICODE
inline void convert(const std::string& in, std::wstring& out)
{
#ifdef NANODBC_USE_BOOST_CONVERT
using boost::locale::conv::utf_to_utf;
out = utf_to_utf<wchar_t>(in.c_str(), in.c_str() + in.size());
#else
out = std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(in);
#endif
}
inline void convert(const std::wstring& in, std::wstring & out)
{
out = in;
}
#else
inline void convert(const std::string& in, std::string& out)
{
out = in;
}
#endif
// Attempts to get the most recent ODBC error as a string.
// Always returns std::string, even in unicode mode.
inline std::string recent_error(SQLHANDLE handle, SQLSMALLINT handle_type, long &native, std::string &state)
{
nanodbc::string_type result;
std::string rvalue;
std::vector<NANODBC_SQLCHAR> sql_message(SQL_MAX_MESSAGE_LENGTH);
sql_message[0] = '\0';
SQLINTEGER i = 1;
SQLINTEGER native_error;
SQLSMALLINT total_bytes;
NANODBC_SQLCHAR sql_state[6];
RETCODE rc;
do
{
NANODBC_CALL_RC(
NANODBC_UNICODE(SQLGetDiagRec)
, rc
, handle_type
, handle
, (SQLSMALLINT)i
, sql_state
, &native_error
, 0
, 0
, &total_bytes);
if(success(rc) && total_bytes > 0)
sql_message.resize(total_bytes + 1);
if(rc == SQL_NO_DATA)
break;
NANODBC_CALL_RC(
NANODBC_UNICODE(SQLGetDiagRec)
, rc
, handle_type
, handle
, (SQLSMALLINT)i
, sql_state
, &native_error
, sql_message.data()
, (SQLSMALLINT)sql_message.size()
, &total_bytes);
if(!success(rc))
{
convert(result, rvalue);
return rvalue;
}
if(!result.empty())
result += ' ';
result += nanodbc::string_type(sql_message.begin(), sql_message.end());
i++;
} while(rc != SQL_NO_DATA);
convert(result, rvalue);
state = std::string(&sql_state[0], &sql_state[arrlen(sql_state) - 1]);
native = native_error;
std::string status = state;
status += ": ";
status += rvalue;
// some drivers insert \0 into error messages for unknown reasons
using std::replace;
replace(status.begin(), status.end(), '\0', ' ');
return status;
}
} // namespace
namespace nanodbc
{
type_incompatible_error::type_incompatible_error()
: std::runtime_error("type incompatible") { }
const char* type_incompatible_error::what() const NANODBC_NOEXCEPT
{
return std::runtime_error::what();
}
null_access_error::null_access_error()
: std::runtime_error("null access") { }
const char* null_access_error::what() const NANODBC_NOEXCEPT
{
return std::runtime_error::what();
}
index_range_error::index_range_error()
: std::runtime_error("index out of range") { }
const char* index_range_error::what() const NANODBC_NOEXCEPT
{
return std::runtime_error::what();
}
programming_error::programming_error(const std::string& info)
: std::runtime_error(info.c_str()) { }
const char* programming_error::what() const NANODBC_NOEXCEPT
{
return std::runtime_error::what();
}
database_error::database_error(void* handle, short handle_type, const std::string& info)
: std::runtime_error(info), native_error(0), sql_state("00000")
{
message = std::string(std::runtime_error::what()) + recent_error(handle, handle_type, native_error, sql_state);
}
const char* database_error::what() const NANODBC_NOEXCEPT
{
return message.c_str();
}
const long database_error::native() const NANODBC_NOEXCEPT
{
return native_error;
}
const std::string database_error::state() const NANODBC_NOEXCEPT
{
return sql_state;
}
} // namespace nanodbc
// Throwing exceptions using NANODBC_THROW_DATABASE_ERROR enables file name
// and line numbers to be inserted into the error message. Useful for debugging.
#define NANODBC_THROW_DATABASE_ERROR(handle, handle_type) \
throw nanodbc::database_error( \
handle \
, handle_type \
, __FILE__ ":" NANODBC_STRINGIZE(__LINE__) ": ") \
/**/
// 8888888b. 888 d8b 888
// 888 "Y88b 888 Y8P 888
// 888 888 888 888
// 888 888 .d88b. 888888 8888b. 888 888 .d8888b
// 888 888 d8P Y8b 888 "88b 888 888 88K
// 888 888 88888888 888 .d888888 888 888 "Y8888b.
// 888 .d88P Y8b. Y88b. 888 888 888 888 X88
// 8888888P" "Y8888 "Y888 "Y888888 888 888 88888P'
// MARK: Details -
namespace
{
using namespace std; // if int64_t is in std namespace (in c++11)
// A utility for calculating the ctype from the given type T.
// I essentially create a lookup table based on the MSDN ODBC documentation.
// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms714556(v=vs.85).aspx
template<class T>
struct sql_ctype { };
template<>
struct sql_ctype<nanodbc::string_type::value_type>
{
#ifdef NANODBC_USE_UNICODE
static const SQLSMALLINT value = SQL_C_WCHAR;
#else
static const SQLSMALLINT value = SQL_C_CHAR;
#endif
};
template<>
struct sql_ctype<short>
{
static const SQLSMALLINT value = SQL_C_SSHORT;
};
template<>
struct sql_ctype<unsigned short>
{
static const SQLSMALLINT value = SQL_C_USHORT;
};
template<>
struct sql_ctype<int32_t>
{
static const SQLSMALLINT value = SQL_C_SLONG;
};
template<>
struct sql_ctype<uint32_t>
{
static const SQLSMALLINT value = SQL_C_ULONG;
};
template<>
struct sql_ctype<int64_t>
{
static const SQLSMALLINT value = SQL_C_SBIGINT;
};
template<>
struct sql_ctype<uint64_t>
{
static const SQLSMALLINT value = SQL_C_UBIGINT;
};
template<>
struct sql_ctype<float>
{
static const SQLSMALLINT value = SQL_C_FLOAT;
};
template<>
struct sql_ctype<double>
{
static const SQLSMALLINT value = SQL_C_DOUBLE;
};
template<>
struct sql_ctype<nanodbc::string_type>
{
#ifdef NANODBC_USE_UNICODE
static const SQLSMALLINT value = SQL_C_WCHAR;
#else
static const SQLSMALLINT value = SQL_C_CHAR;
#endif
};
template<>
struct sql_ctype<nanodbc::date>
{
static const SQLSMALLINT value = SQL_C_DATE;
};
template<>
struct sql_ctype<nanodbc::timestamp>
{
static const SQLSMALLINT value = SQL_C_TIMESTAMP;
};
// Encapsulates resources needed for column binding.
class bound_column
{
public:
bound_column(const bound_column& rhs) =delete;
bound_column& operator=(bound_column rhs) =delete;
bound_column()
: name_()
, column_(0)
, sqltype_(0)
, sqlsize_(0)
, scale_(0)
, ctype_(0)
, clen_(0)
, blob_(false)
, cbdata_(0)
, pdata_(0)
{
}
~bound_column()
{
delete[] cbdata_;
delete[] pdata_;
}
public:
nanodbc::string_type name_;
short column_;
SQLSMALLINT sqltype_;
SQLULEN sqlsize_;
SQLSMALLINT scale_;
SQLSMALLINT ctype_;
SQLULEN clen_;
bool blob_;
nanodbc::null_type* cbdata_;
char* pdata_;
};
// Allocates the native ODBC handles.
inline void allocate_handle(SQLHENV& env, SQLHDBC& conn)
{
RETCODE rc;
NANODBC_CALL_RC(
SQLAllocHandle
, rc
, SQL_HANDLE_ENV
, SQL_NULL_HANDLE
, &env);
if(!success(rc))
NANODBC_THROW_DATABASE_ERROR(env, SQL_HANDLE_ENV);
try
{
NANODBC_CALL_RC(
SQLSetEnvAttr
, rc
, env
, SQL_ATTR_ODBC_VERSION
, (SQLPOINTER)NANODBC_ODBC_VERSION
, SQL_IS_UINTEGER);
if(!success(rc))
NANODBC_THROW_DATABASE_ERROR(env, SQL_HANDLE_ENV);
NANODBC_CALL_RC(
SQLAllocHandle
, rc
, SQL_HANDLE_DBC
, env
, &conn);
if(!success(rc))
NANODBC_THROW_DATABASE_ERROR(env, SQL_HANDLE_ENV);
}
catch(...)
{
NANODBC_CALL(
SQLFreeHandle
, SQL_HANDLE_ENV
, env);
throw;
}
}
} // namespace
// .d8888b. 888 d8b 8888888 888
// d88P Y88b 888 Y8P 888 888
// 888 888 888 888 888
// 888 .d88b. 88888b. 88888b. .d88b. .d8888b 888888 888 .d88b. 88888b. 888 88888b.d88b. 88888b. 888
// 888 d88""88b 888 "88b 888 "88b d8P Y8b d88P" 888 888 d88""88b 888 "88b 888 888 "888 "88b 888 "88b 888
// 888 888 888 888 888 888 888 888 88888888 888 888 888 888 888 888 888 888 888 888 888 888 888 888
// Y88b d88P Y88..88P 888 888 888 888 Y8b. Y88b. Y88b. 888 Y88..88P 888 888 888 888 888 888 888 d88P 888
// "Y8888P" "Y88P" 888 888 888 888 "Y8888 "Y8888P "Y888 888 "Y88P" 888 888 8888888 888 888 888 88888P" 888
// 888
// 888
// 888
// MARK: Connection Impl -
namespace nanodbc
{
class connection::connection_impl
{
public:
connection_impl(const connection_impl&) =delete;
connection_impl& operator=(const connection_impl&) =delete;
connection_impl()
: env_(0)
, conn_(0)
, connected_(false)
, transactions_(0)
, rollback_(false)
{
allocate_handle(env_, conn_);
}
connection_impl(
const string_type& dsn
, const string_type& user
, const string_type& pass
, long timeout)
: env_(0)
, conn_(0)
, connected_(false)
, transactions_(0)
, rollback_(false)
{
allocate_handle(env_, conn_);
try
{
connect(dsn, user, pass, timeout);
}
catch(...)
{
NANODBC_CALL(
SQLFreeHandle
, SQL_HANDLE_DBC
, conn_);
NANODBC_CALL(
SQLFreeHandle
, SQL_HANDLE_ENV
, env_);
throw;
}
}
connection_impl(const string_type& connection_string, long timeout)
: env_(0)
, conn_(0)
, connected_(false)
, transactions_(0)
, rollback_(false)
{
allocate_handle(env_, conn_);
try
{
connect(connection_string, timeout);
}
catch(...)
{
NANODBC_CALL(
SQLFreeHandle
, SQL_HANDLE_DBC
, conn_);
NANODBC_CALL(
SQLFreeHandle
, SQL_HANDLE_ENV
, env_);
throw;
}
}
~connection_impl() NANODBC_NOEXCEPT
{
try
{
disconnect();
}
catch(...)
{
// ignore exceptions thrown during disconnect
}
NANODBC_CALL(
SQLFreeHandle
, SQL_HANDLE_DBC
, conn_);
NANODBC_CALL(
SQLFreeHandle
, SQL_HANDLE_ENV
, env_);
}
#ifdef SQL_ATTR_ASYNC_DBC_EVENT
void enable_async(void* event_handle)
{
RETCODE rc;
NANODBC_CALL_RC(
SQLSetConnectAttr
, rc
, conn_
, SQL_ATTR_ASYNC_DBC_FUNCTIONS_ENABLE
, (SQLPOINTER)SQL_ASYNC_DBC_ENABLE_ON
, SQL_IS_INTEGER);
if(!success(rc))
NANODBC_THROW_DATABASE_ERROR(conn_, SQL_HANDLE_DBC);
NANODBC_CALL_RC(
SQLSetConnectAttr
, rc
, conn_
, SQL_ATTR_ASYNC_DBC_EVENT
, event_handle
, SQL_IS_POINTER);
if(!success(rc))
NANODBC_THROW_DATABASE_ERROR(conn_, SQL_HANDLE_DBC);
}
void async_complete()
{
RETCODE rc, arc;
NANODBC_CALL_RC(
SQLCompleteAsync
, rc
, SQL_HANDLE_DBC
, conn_
, &arc);
if(!success(rc) || !success(arc))
NANODBC_THROW_DATABASE_ERROR(conn_, SQL_HANDLE_DBC);
NANODBC_CALL_RC(
SQLSetConnectAttr
, rc
, conn_
, SQL_ATTR_ASYNC_ENABLE
, (SQLPOINTER)SQL_ASYNC_ENABLE_OFF
, SQL_IS_INTEGER);
if(!success(rc))
NANODBC_THROW_DATABASE_ERROR(conn_, SQL_HANDLE_DBC);
connected_ = success(rc);
}
#endif // SQL_ATTR_ASYNC_DBC_EVENT
void connect(
const string_type& dsn
, const string_type& user
, const string_type& pass
, long timeout
, void* event_handle = NULL)
{
disconnect();
RETCODE rc;
NANODBC_CALL_RC(
SQLFreeHandle
, rc
, SQL_HANDLE_DBC
, conn_);
if(!success(rc))
NANODBC_THROW_DATABASE_ERROR(conn_, SQL_HANDLE_DBC);
NANODBC_CALL_RC(
SQLAllocHandle
, rc
, SQL_HANDLE_DBC
, env_
, &conn_);
if(!success(rc))
NANODBC_THROW_DATABASE_ERROR(env_, SQL_HANDLE_ENV);
NANODBC_CALL_RC(
SQLSetConnectAttr
, rc
, conn_
, SQL_LOGIN_TIMEOUT
, (SQLPOINTER)(std::intptr_t)timeout
, 0);
if(!success(rc))
NANODBC_THROW_DATABASE_ERROR(conn_, SQL_HANDLE_DBC);
#ifdef SQL_ATTR_ASYNC_DBC_EVENT
if (event_handle != NULL)
enable_async(event_handle);
#endif
NANODBC_CALL_RC(
NANODBC_UNICODE(SQLConnect)
, rc
, conn_
, (NANODBC_SQLCHAR*)dsn.c_str(), SQL_NTS
, !user.empty() ? (NANODBC_SQLCHAR*)user.c_str() : 0, SQL_NTS
, !pass.empty() ? (NANODBC_SQLCHAR*)pass.c_str() : 0, SQL_NTS);
if(!success(rc) && (event_handle == NULL || rc != SQL_STILL_EXECUTING))
NANODBC_THROW_DATABASE_ERROR(conn_, SQL_HANDLE_DBC);
connected_ = success(rc);
}
void connect(const string_type& connection_string, long timeout, void* event_handle = NULL)
{
disconnect();
RETCODE rc;
NANODBC_CALL_RC(
SQLFreeHandle
, rc
, SQL_HANDLE_DBC
, conn_);
if(!success(rc))
NANODBC_THROW_DATABASE_ERROR(conn_, SQL_HANDLE_DBC);
NANODBC_CALL_RC(
SQLAllocHandle
, rc
, SQL_HANDLE_DBC
, env_
, &conn_);
if(!success(rc))
NANODBC_THROW_DATABASE_ERROR(env_, SQL_HANDLE_ENV);
NANODBC_CALL_RC(
SQLSetConnectAttr
, rc
, conn_
, SQL_LOGIN_TIMEOUT
, (SQLPOINTER)(std::intptr_t)timeout
, 0);
if(!success(rc))
NANODBC_THROW_DATABASE_ERROR(conn_, SQL_HANDLE_DBC);
#ifdef SQL_ATTR_ASYNC_DBC_EVENT
if (event_handle != NULL)
enable_async(event_handle);
#endif
NANODBC_SQLCHAR dsn[1024];
SQLSMALLINT dsn_size = 0;
NANODBC_CALL_RC(
NANODBC_UNICODE(SQLDriverConnect)
, rc
, conn_
, 0
, (NANODBC_SQLCHAR*)connection_string.c_str(), SQL_NTS
, dsn
, sizeof(dsn) / sizeof(NANODBC_SQLCHAR)
, &dsn_size
, SQL_DRIVER_NOPROMPT);
if(!success(rc) && (event_handle == NULL || rc != SQL_STILL_EXECUTING))
NANODBC_THROW_DATABASE_ERROR(conn_, SQL_HANDLE_DBC);
connected_ = success(rc);
}
bool connected() const
{
return connected_;
}
void disconnect()
{
if(connected())
{
RETCODE rc;
NANODBC_CALL_RC(
SQLDisconnect
, rc
, conn_);
if(!success(rc))
NANODBC_THROW_DATABASE_ERROR(conn_, SQL_HANDLE_DBC);
}
connected_ = false;
}
std::size_t transactions() const
{
return transactions_;
}
void* native_dbc_handle() const
{
return conn_;
}
void* native_env_handle() const
{
return env_;
}
string_type driver_name() const
{
NANODBC_SQLCHAR name[1024];
SQLSMALLINT length;
RETCODE rc;
NANODBC_CALL_RC(
NANODBC_UNICODE(SQLGetInfo)
, rc
, conn_
, SQL_DRIVER_NAME
, name
, sizeof(name) / sizeof(NANODBC_SQLCHAR)
, &length);
if(!success(rc))
NANODBC_THROW_DATABASE_ERROR(conn_, SQL_HANDLE_DBC);
return string_type(&name[0], &name[strarrlen(name)]);
}
std::size_t ref_transaction()
{
return --transactions_;
}
std::size_t unref_transaction()
{
return ++transactions_;
}
bool rollback() const
{
return rollback_;
}
void rollback(bool onoff)
{
rollback_ = onoff;
}
private:
HENV env_;
HDBC conn_;
bool connected_;
std::size_t transactions_;
bool rollback_; // if true, this connection is marked for eventual transaction rollback
};
} // namespace nanodbc
// 88888888888 888 d8b 8888888 888
// 888 888 Y8P 888 888
// 888 888 888 888
// 888 888d888 8888b. 88888b. .d8888b 8888b. .d8888b 888888 888 .d88b. 88888b. 888 88888b.d88b. 88888b. 888
// 888 888P" "88b 888 "88b 88K "88b d88P" 888 888 d88""88b 888 "88b 888 888 "888 "88b 888 "88b 888
// 888 888 .d888888 888 888 "Y8888b. .d888888 888 888 888 888 888 888 888 888 888 888 888 888 888 888
// 888 888 888 888 888 888 X88 888 888 Y88b. Y88b. 888 Y88..88P 888 888 888 888 888 888 888 d88P 888
// 888 888 "Y888888 888 888 88888P' "Y888888 "Y8888P "Y888 888 "Y88P" 888 888 8888888 888 888 888 88888P" 888
// 888
// 888
// 888
// MARK: Transaction Impl -
namespace nanodbc
{
class transaction::transaction_impl
{
public:
transaction_impl(const transaction_impl&) =delete;
transaction_impl& operator=(const transaction_impl&) =delete;
transaction_impl(const class connection& conn)
: conn_(conn)
, committed_(false)
{
if(conn_.transactions() == 0 && conn_.connected())
{
RETCODE rc;
NANODBC_CALL_RC(
SQLSetConnectAttr
, rc
, conn_.native_dbc_handle()
, SQL_ATTR_AUTOCOMMIT
, (SQLPOINTER)SQL_AUTOCOMMIT_OFF
, SQL_IS_UINTEGER);
if(!success(rc))
NANODBC_THROW_DATABASE_ERROR(conn_.native_dbc_handle(), SQL_HANDLE_DBC);
}
conn_.ref_transaction();
}
~transaction_impl() NANODBC_NOEXCEPT
{
if(!committed_)
{
conn_.rollback(true);
conn_.unref_transaction();
}
if(conn_.transactions() == 0 && conn_.connected())
{
if(conn_.rollback())
{
NANODBC_CALL(
SQLEndTran
, SQL_HANDLE_DBC
, conn_.native_dbc_handle()
, SQL_ROLLBACK);
conn_.rollback(false);
}
NANODBC_CALL(
SQLSetConnectAttr
, conn_.native_dbc_handle()
, SQL_ATTR_AUTOCOMMIT
, (SQLPOINTER)SQL_AUTOCOMMIT_ON
, SQL_IS_UINTEGER);
}
}
void commit()
{
if(committed_)
return;
committed_ = true;