Menu

[r1535]: / cppdb / trunk / odbc_backend.cpp  Maximize  Restore  History

Download this file

943 lines (884 with data), 21.2 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
#include "backend.h"
#include "utils.h"
#include <list>
#include <vector>
#include <sstream>
#include <limits>
#include <iomanip>
#include <string.h>
#if defined(_WIN32) || defined(__WIN32) || defined(WIN32) || defined(__CYGWIN__)
#include <windows.h>
#endif
#include <sqlext.h>
namespace cppdb {
namespace odbc_backend {
typedef unsigned odbc_u32;
typedef unsigned short odbc_u16;
int assert_on_unsigned_is_32[sizeof(unsigned) == 4 ? 1 : -1];
int assert_on_unsigned_short_is_16[sizeof(unsigned short) == 2 ? 1 : -1];
namespace utf {
static const odbc_u32 illegal = 0xFFFFFFFFu;
inline bool valid(odbc_u32 v)
{
if(v>0x10FFFF)
return false;
if(0xD800 <=v && v<= 0xDFFF) // surragates
return false;
return true;
}
}
namespace utf8 {
// See RFC 3629
// Based on: http://www.w3.org/International/questions/qa-forms-utf-8
template<typename Iterator>
odbc_u32 next(Iterator &p,Iterator e)
{
unsigned char c=*p++;
unsigned char seq0,seq1=0,seq2=0,seq3=0;
seq0=c;
int len=1;
if((c & 0xC0) == 0xC0) {
if(p==e)
return utf::illegal;
seq1=*p++;
len=2;
}
if((c & 0xE0) == 0xE0) {
if(p==e)
return utf::illegal;
seq2=*p++;
len=3;
}
if((c & 0xF0) == 0xF0) {
if(p==e)
return utf::illegal;
seq3=*p++;
len=4;
}
switch(len) {
case 1: // ASCII -- remove codes for HTML only
if(seq0 > 0x7F)
return utf::illegal;
break;
case 2: // non-overloading 2 bytes
if(0xC2 <= seq0 && seq0 <= 0xDF) {
if(0x80 <= seq1 && seq1<= 0xBF)
break;
}
return utf::illegal;
case 3:
if(seq0==0xE0) { // exclude overloadings
if(0xA0 <=seq1 && seq1<= 0xBF && 0x80 <=seq2 && seq2<=0xBF)
break;
}
else if( (0xE1 <= seq0 && seq0 <=0xEC) || seq0==0xEE || seq0==0xEF) { // stright 3 bytes
if( 0x80 <=seq1 && seq1<=0xBF &&
0x80 <=seq2 && seq2<=0xBF)
break;
}
else if(seq0 == 0xED) { // exclude surrogates
if( 0x80 <=seq1 && seq1<=0x9F &&
0x80 <=seq2 && seq2<=0xBF)
break;
}
return utf::illegal;
case 4:
switch(seq0) {
case 0xF0: // planes 1-3
if( 0x90 <=seq1 && seq1<=0xBF &&
0x80 <=seq2 && seq2<=0xBF &&
0x80 <=seq3 && seq3<=0xBF)
break;
return utf::illegal;
case 0xF1: // planes 4-15
case 0xF2:
case 0xF3:
if( 0x80 <=seq1 && seq1<=0xBF &&
0x80 <=seq2 && seq2<=0xBF &&
0x80 <=seq3 && seq3<=0xBF)
break;
return utf::illegal;
case 0xF4: // pane 16
if( 0x80 <=seq1 && seq1<=0x8F &&
0x80 <=seq2 && seq2<=0xBF &&
0x80 <=seq3 && seq3<=0xBF)
break;
return utf::illegal;
default:
return utf::illegal;
}
}
switch(len) {
case 1:
return seq0;
case 2:
return ((seq0 & 0x1F) << 6) | (seq1 & 0x3F);
case 3:
return ((seq0 & 0x0F) << 12) | ((seq1 & 0x3F) << 6) | (seq2 & 0x3F) ;
case 4:
return ((seq0 & 0x07) << 18) | ((seq1 & 0x3F) << 12) | ((seq2 & 0x3F) << 6) | (seq3 & 0x3F) ;
}
return utf::illegal;
} // valid
struct seq {
char c[4];
unsigned len;
};
inline seq encode(odbc_u32 value)
{
seq out={ {0} };
if(value <=0x7F) {
out.c[0]=value;
out.len=1;
}
else if(value <=0x7FF) {
out.c[0]=(value >> 6) | 0xC0;
out.c[1]=(value & 0x3F) | 0x80;
out.len=2;
}
else if(value <=0xFFFF) {
out.c[0]=(value >> 12) | 0xE0;
out.c[1]=((value >> 6) & 0x3F) | 0x80;
out.c[2]=(value & 0x3F) | 0x80;
out.len=3;
}
else {
out.c[0]=(value >> 18) | 0xF0;
out.c[1]=((value >> 12) & 0x3F) | 0x80;
out.c[2]=((value >> 6) & 0x3F) | 0x80;
out.c[3]=(value & 0x3F) | 0x80;
out.len=4;
}
return out;
}
} // namespace utf8
namespace utf16 {
// See RFC 2781
inline bool is_first_surrogate(odbc_u16 x)
{
return 0xD800 <=x && x<= 0xDBFF;
}
inline bool is_second_surrogate(odbc_u16 x)
{
return 0xDC00 <=x && x<= 0xDFFF;
}
inline odbc_u32 combine_surrogate(odbc_u16 w1,odbc_u16 w2)
{
return ((odbc_u32(w1 & 0x3FF) << 10) | (w2 & 0x3FF)) + 0x10000;
}
template<typename It>
inline odbc_u32 next(It &current,It last)
{
odbc_u16 w1=*current++;
if(w1 < 0xD800 || 0xDFFF < w1) {
return w1;
}
if(w1 > 0xDBFF)
return utf::illegal;
if(current==last)
return utf::illegal;
odbc_u16 w2=*current++;
if(w2 < 0xDC00 || 0xDFFF < w2)
return utf::illegal;
return combine_surrogate(w1,w2);
}
inline int width(odbc_u32 u)
{
return u>=0x100000 ? 2 : 1;
}
struct seq {
odbc_u16 c[2];
unsigned len;
};
inline seq encode(odbc_u32 u)
{
seq out={ {0} };
if(u<=0xFFFF) {
out.c[0]=u;
out.len=1;
}
else {
u-=0x10000;
out.c[0]=0xD800 | (u>>10);
out.c[1]=0xDC00 | (u & 0x3FF);
out.len=2;
}
return out;
}
} // utf16;
} // odbc_backend
} // cppdb
namespace cppdb {
class connection_info;
class pool;
namespace odbc_backend {
void check_odbc_error(SQLRETURN error,SQLHANDLE h,SQLSMALLINT type)
{
if(SQL_SUCCEEDED(error))
return;
std::string error_message;
int rec=1,r;
for(;;){
SQLCHAR msg[SQL_MAX_MESSAGE_LENGTH + 2] = {0};
SQLCHAR stat[SQL_SQLSTATE_SIZE + 1] = {0};
SQLINTEGER err;
SQLSMALLINT len;
r = SQLGetDiagRec(type,h,rec,stat,&err,msg,sizeof(msg),&len);
rec++;
if(r==SQL_SUCCESS || r==SQL_SUCCESS_WITH_INFO) {
if(!error_message.empty())
error_message+='\n';
error_message +=(char *)msg;
}
else
break;
}
throw cppdb_error("cppdb::odbc::Failed with error `" + error_message +"'");
}
std::string widen(char const *b,char const *e)
{
std::string result;
result.reserve((e-b)*2);
odbc_u32 code_point = 0;
while(b < e && (code_point = utf8::next(b,e))!=utf::illegal) {
utf16::seq sq = utf16::encode(code_point);
char *str = (char *)sq.c;
result.append(str,sq.len * 2);
}
if(b!=e || code_point == utf::illegal) {
throw cppdb_error("cppdb::odbc invalid UTF-8 input");
}
return result;
}
std::string widen(std::string const &s)
{
return widen(s.c_str(),s.c_str()+s.size());
}
std::string narrower(std::string const &wide)
{
if(wide.size() % 2 != 0) {
throw cppdb_error("cppdb::odbc got invalid UTF-16");
}
odbc_u16 const *b = reinterpret_cast<odbc_u16 const *>(wide.c_str());
odbc_u16 const *e = b + wide.size() / 2;
std::string result;
result.reserve((e-b));
odbc_u32 code_point = 0;
while(b < e && (code_point = utf16::next(b,e))!=utf::illegal) {
utf8::seq sq = utf8::encode(code_point);
result.append(sq.c,sq.len);
}
if(b!=e || code_point == utf::illegal) {
throw cppdb_error("cppdb::odbc got invalid UTF-16");
}
return result;
}
std::basic_string<SQLWCHAR> tosqlwide(std::string const &n)
{
std::basic_string<SQLWCHAR> result;
char const *b=n.c_str();
char const *e=b+n.size();
result.reserve(e-b);
odbc_u32 code_point = 0;
while(b < e && (code_point = utf8::next(b,e))!=utf::illegal) {
utf16::seq sq = utf16::encode(code_point);
result.append((SQLWCHAR*)sq.c,sq.len);
}
if(b!=e || code_point == utf::illegal) {
throw cppdb_error("cppdb::odbc invalid UTF-8 input");
}
return result;
}
class result : public backend::result {
public:
typedef std::pair<bool,std::string> cell_type;
typedef std::vector<cell_type> row_type;
typedef std::list<row_type> rows_type;
virtual next_row has_next()
{
rows_type::iterator p=current_;
if(p == rows_.end() || ++p==rows_.end())
return last_row_reached;
else
return next_row_exists;
}
virtual bool next()
{
if(started_ == false) {
current_ = rows_.begin();
started_ = true;
}
else if(current_!=rows_.end()) {
++current_;
}
return current_!=rows_.end();
}
template<typename T>
bool do_fetch(int col,T &v)
{
if(at(col).first)
return false;
std::istringstream ss;
ss.imbue(std::locale::classic());
ss.str(at(col).second);
ss >> v;
if(!ss || !ss.eof()) {
throw bad_value_cast();
}
return true;
}
virtual bool fetch(int col,short &v)
{
return do_fetch(col,v);
}
virtual bool fetch(int col,unsigned short &v)
{
return do_fetch(col,v);
}
virtual bool fetch(int col,int &v)
{
return do_fetch(col,v);
}
virtual bool fetch(int col,unsigned &v)
{
return do_fetch(col,v);
}
virtual bool fetch(int col,long &v)
{
return do_fetch(col,v);
}
virtual bool fetch(int col,unsigned long &v)
{
return do_fetch(col,v);
}
virtual bool fetch(int col,long long &v)
{
return do_fetch(col,v);
}
virtual bool fetch(int col,unsigned long long &v)
{
return do_fetch(col,v);
}
virtual bool fetch(int col,float &v)
{
return do_fetch(col,v);
}
virtual bool fetch(int col,double &v)
{
return do_fetch(col,v);
}
virtual bool fetch(int col,long double &v)
{
return do_fetch(col,v);
}
virtual bool fetch(int col,std::string &v)
{
if(at(col).first)
return false;
v=at(col).second;
return true;
}
virtual bool fetch(int col,std::ostream &v)
{
if(at(col).first)
return false;
v << at(col).second;
return true;
}
virtual bool fetch(int col,std::tm &v)
{
if(at(col).first)
return false;
v = parse_time(at(col).second);
return true;
}
virtual bool is_null(int col)
{
return at(col).first;
}
virtual int cols()
{
if(rows_.begin()!=rows_.end())
return rows_.begin()->size();
return 0;
}
virtual int name_to_column(std::string const &)
{
throw not_supported_by_backend("not supported for now");
}
virtual std::string column_to_name(int)
{
throw not_supported_by_backend("not supported for now");
}
result(rows_type &rows)
{
rows_.swap(rows);
started_ = false;
current_ = rows_.end();
}
cell_type &at(int col)
{
if(current_!=rows_.end() && col >= 0 && col <int(current_->size()))
return current_->at(col);
throw invalid_column();
}
private:
bool started_;
rows_type::iterator current_;
rows_type rows_;
};
class statements_cache;
class statement : public backend::statement {
struct parameter {
parameter() :
null(true),
ctype(SQL_C_CHAR),
sqltype(SQL_C_NUMERIC)
{
}
void set(char const *b,char const *e,bool wide,int sqt)
{
if(!wide) {
value.assign(b,e-b);
null=false;
ctype=SQL_C_CHAR;
sqltype = sqt;
}
else {
std::string tmp = widen(b,e);
null=false;
ctype=SQL_C_WCHAR;
sqltype = sqt;
}
}
void set(std::string &s,bool wide,int sqt)
{
if(!wide) {
value.swap(s);
null=false;
ctype=SQL_C_CHAR;
sqltype = sqt;
}
else {
std::string tmp = widen(s);
null=false;
ctype=SQL_C_WCHAR;
sqltype = sqt;
}
}
void set(char const *b,char const *e,bool wide=false)
{
if(!wide) {
set(b,e,false,SQL_LONGVARCHAR);
}
else {
set(b,e,true,SQL_WLONGVARCHAR);
}
}
void set(std::tm const &v,bool wide=false)
{
std::string s = format_time(v);
set(s,wide,SQL_C_TIMESTAMP);
}
template<typename T>
void set(T v,bool wide=false)
{
std::ostringstream ss;
ss.imbue(std::locale::classic());
if(!std::numeric_limits<T>::is_integer)
ss << std::setprecision(std::numeric_limits<T>::digits10+1);
ss << v;
std::string sv=ss.str();
if(std::numeric_limits<T>::is_integer)
set(sv,wide,SQL_INTEGER);
else
set(sv,wide,SQL_DOUBLE);
}
void bind(int col,SQLHSTMT stmt)
{
int r;
if(null) {
SQLLEN len = SQL_NULL_DATA;
r = SQLBindParameter( stmt,
col,
SQL_PARAM_INPUT,
SQL_C_CHAR, // C type C_CHAR or C_WCHAR
SQL_NUMERIC, // for null
value.size(), // COLUMNSIZE
0, // Presision
0, // string
0, // size
&len);
}
else {
r = SQLBindParameter( stmt,
col,
SQL_PARAM_INPUT,
ctype, // C type C_CHAR or C_WCHAR
sqltype,
value.size(), // COLUMNSIZE
0, // Presision
(SQLCHAR*)value.c_str(), // string
value.size(),
0);
}
check_odbc_error(r,stmt,SQL_HANDLE_STMT);
}
std::string value;
bool null;
SQLSMALLINT ctype;
SQLSMALLINT sqltype;
};
public:
// Begin of API
virtual void reset()
{
SQLFreeStmt(stmt_,SQL_UNBIND);
params_.resize(0);
}
parameter &param_at(int col)
{
if(col < 0)
throw invalid_placeholder();
if(params_.size() < size_t(col))
params_.resize(col);
return params_[col - 1];
}
virtual std::string const &sql_query()
{
return query_;
}
virtual void bind(int col,std::string const &s)
{
bind(col,s.c_str(),s.c_str()+s.size());
}
virtual void bind(int col,char const *s)
{
bind(col,s,s+strlen(s));
}
virtual void bind(int col,char const *b,char const *e)
{
param_at(col).set(b,e,wide_);
}
virtual void bind(int col,std::tm const &s)
{
param_at(col).set(s,wide_);
}
virtual void bind(int col,std::istream const &in)
{
std::ostringstream ss;
ss << in.rdbuf();
bind(col,ss.str());
}
template<typename T>
void do_bind_num(int col,T v)
{
param_at(col).set(v,wide_);
}
virtual void bind(int col,int v)
{
do_bind_num(col,v);
}
virtual void bind(int col,unsigned v)
{
do_bind_num(col,v);
}
virtual void bind(int col,long v)
{
do_bind_num(col,v);
}
virtual void bind(int col,unsigned long v)
{
do_bind_num(col,v);
}
virtual void bind(int col,long long v)
{
do_bind_num(col,v);
}
virtual void bind(int col,unsigned long long v)
{
do_bind_num(col,v);
}
virtual void bind(int col,double v)
{
do_bind_num(col,v);
}
virtual void bind(int col,long double v)
{
do_bind_num(col,v);
}
virtual void bind_null(int col)
{
param_at(col) = parameter();
}
void bind_all()
{
for(unsigned i=0;i<params_.size();i++) {
params_[i].bind(i+1,stmt_);
}
}
virtual long long sequence_last(std::string const &sequence)
{
throw not_supported_by_backend("cppdb::odbc::sequence_last is not supported by odbc backend");
}
virtual unsigned long long affected()
{
SQLLEN rows = 0;
int r = SQLRowCount(stmt_,&rows);
check_error(r);
return rows;
}
virtual result *query()
{
bind_all();
int r=SQLExecute(stmt_);
check_error(r);
char buf[1024];
result::rows_type rows;
result::row_type row;
std::string value;
bool is_null = false;
while((r=SQLFetch(stmt_))==SQL_SUCCESS || r==SQL_SUCCESS_WITH_INFO) {
row.reserve(100);
for(int col=1;;col++) {
SQLLEN len = 0;
value.clear();
is_null=false;
int type = wide_ ? SQL_C_WCHAR : SQL_C_CHAR;
int r = SQLGetData(stmt_,col,type,buf,sizeof(buf),&len);
if(r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) {
if(len == SQL_NULL_DATA) {
is_null = true;
}
else if(len == SQL_NO_TOTAL) {
for(;;) {
if(!wide_)
value.append(buf,sizeof(buf)-1);
else
value.append(buf,sizeof(buf)-2);
r= SQLGetData(stmt_,col,type,buf,sizeof(buf),&len);
if(len == SQL_NO_TOTAL)
continue;
value.append(buf,len);
}
}
else if(len > SQLLEN(sizeof(buf))) {
if(!wide_)
value.append(buf,sizeof(buf)-1);
else
value.append(buf,sizeof(buf)-2);
std::vector<char> all(len-sizeof(buf)+2);
r = SQLGetData(stmt_,col,type,&all.front(),all.size(),&len);
if(len > 0)
value.append(&all.front(),len);
}
else if(len >= 0)// len <= sizeof(buf))
{
value.assign(buf,len);
}
else {
throw cppdb_error("cppdb::odbc::iternal error");
}
}
else {
break;
}
row.resize(row.size()+1);
row.back().first = is_null;
if(wide_) {
std::string tmp=narrower(value);
value.swap(tmp);
}
row.back().second.swap(value);
}
rows.push_back(result::row_type());
rows.back().swap(row);
}
if(r!=SQL_NO_DATA) {
check_error(r);
}
return new result(rows);
}
virtual void exec()
{
bind_all();
int r=SQLExecute(stmt_);
check_error(r);
}
// End of API
statement(std::string const &q,SQLHDBC dbc,bool wide) :
wide_(wide),
query_(q)
{
bool stmt_created = false;
SQLRETURN r = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt_);
check_odbc_error(r,dbc,SQL_HANDLE_DBC);
stmt_created = true;
try {
if(wide_)
r = SQLPrepareW(
stmt_,
(SQLWCHAR*)tosqlwide(query_).c_str(),
SQL_NTS);
else
r = SQLPrepareA(
stmt_,
(SQLCHAR*)query_.c_str(),
SQL_NTS);
check_error(r);
params_.reserve(100);
}
catch(...) {
SQLFreeHandle(SQL_HANDLE_STMT,stmt_);
throw;
}
}
~statement()
{
SQLFreeHandle(SQL_HANDLE_STMT,stmt_);
}
private:
void check_error(int code)
{
check_odbc_error(code,stmt_,SQL_HANDLE_STMT);
}
SQLHSTMT stmt_;
bool wide_;
std::string query_;
std::vector<parameter> params_;
};
class connection : public backend::connection {
public:
connection(connection_info const &ci) :
backend::connection(ci)
{
std::string utf_mode = ci.get("@utf","narrow");
if(utf_mode == "narrow")
wide_ = false;
else if(utf_mode == "wide")
wide_ = true;
else
throw cppdb_error("cppdb::odbc:: @utf property can be either 'narrow' or 'wide'");
bool env_created = false;
bool dbc_created = false;
bool dbc_connected = false;
try {
SQLRETURN r = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env_);
if(!SQL_SUCCEEDED(r)) {
throw cppdb_error("cppdb::odbc::Failed to allocate environment handle");
}
env_created = true;
r = SQLSetEnvAttr(env_,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3, 0);
check_odbc_error(r,env_,SQL_HANDLE_ENV);
r = SQLAllocHandle(SQL_HANDLE_DBC,env_,&dbc_);
check_odbc_error(r,env_,SQL_HANDLE_ENV);
dbc_created = true;
if(ci.get("@utf","narrow")=="wide") {
r = SQLDriverConnectW(dbc_,0,
(SQLWCHAR*)tosqlwide(conn_str(ci)).c_str(),
SQL_NTS,0,0,0,SQL_DRIVER_COMPLETE);
}
else {
r = SQLDriverConnectA(dbc_,0,
(SQLCHAR*)conn_str(ci).c_str(),
SQL_NTS,0,0,0,SQL_DRIVER_COMPLETE);
}
check_odbc_error(r,dbc_,SQL_HANDLE_DBC);
set_autocommit(true);
}
catch(...) {
if(dbc_connected)
SQLDisconnect(dbc_);
if(dbc_created)
SQLFreeHandle(SQL_HANDLE_DBC,dbc_);
if(env_created)
SQLFreeHandle(SQL_HANDLE_ENV,env_);
throw;
}
}
std::string conn_str(connection_info const &ci)
{
std::map<std::string,std::string>::const_iterator p;
std::string str;
for(p=ci.properties.begin();p!=ci.properties.end();p++) {
if(p->first.empty() || p->first[0]=='@')
continue;
str+=p->first;
str+="=";
str+=p->second;
str+=";";
}
return str;
}
~connection()
{
SQLDisconnect(dbc_);
SQLFreeHandle(SQL_HANDLE_DBC,dbc_);
SQLFreeHandle(SQL_HANDLE_ENV,env_);
}
/// API
virtual void begin()
{
set_autocommit(false);
}
virtual void commit()
{
SQLRETURN r = SQLEndTran(SQL_HANDLE_DBC,dbc_,SQL_COMMIT);
check_odbc_error(r,dbc_,SQL_HANDLE_DBC);
set_autocommit(true);
}
virtual void rollback()
{
try {
SQLRETURN r = SQLEndTran(SQL_HANDLE_DBC,dbc_,SQL_ROLLBACK);
check_odbc_error(r,dbc_,SQL_HANDLE_DBC);
}
catch(...) {}
try {
set_autocommit(true);
}
catch(...) {}
}
virtual statement *real_prepare(std::string const &q)
{
return new statement(q,dbc_,wide_);
}
virtual std::string escape(std::string const &s)
{
return escape(s.c_str(),s.c_str()+s.size());
}
virtual std::string escape(char const *s)
{
return escape(s,s+strlen(s));
}
virtual std::string escape(char const *b,char const *e)
{
throw not_supported_by_backend("cppcms::odbc:: string escaping is not supported");
}
virtual std::string name()
{
return "odbc";
}
void set_autocommit(bool on)
{
SQLPOINTER mode = (SQLPOINTER)(on ? SQL_AUTOCOMMIT_ON : SQL_AUTOCOMMIT_OFF);
SQLRETURN r = SQLSetConnectAttr(
dbc_, // handler
SQL_ATTR_AUTOCOMMIT, // option
mode, //value
0);
check_odbc_error(r,dbc_,SQL_HANDLE_DBC);
}
private:
SQLHENV env_;
SQLHDBC dbc_;
bool wide_;
};
} // odbc_backend
} // cppdb
extern "C" {
cppdb::backend::connection *cppdb_odbc_get_connection(cppdb::connection_info const &cs)
{
return new cppdb::odbc_backend::connection(cs);
}
}
MongoDB Logo MongoDB