forked from CUBRID/cubrid-oledb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RowsetRow.cpp
1287 lines (1158 loc) · 36.4 KB
/
RowsetRow.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
/*
* Copyright (C) 2008 Search Solution Corporation. All rights reserved by Search Solution.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - Neither the name of the <ORGANIZATION> nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
*/
#include "stdafx.h"
#include "type.h"
#include "RowsetRow.h"
#include "Error.h"
#include "Rowset.h"
#include "Session.h"
#include "CUBRIDStream.h"
// RowsetRow의 각 컬럼의 데이터를 가지는 class
// 데이터는 int, time등의 타입도 포함해서 모두 string으로 가진다.
class CCUBRIDRowsetRowColumn
{
public:
CCUBRIDRowsetRowColumn(UINT uCodepage, INT con = -1) : m_uCodepage(uCodepage), m_strData(0), m_cbDataLen(0), m_dwStatus(DBSTATUS_S_ISNULL), m_con(con)
{
ATLTRACE(atlTraceDBProvider, 3, "CCUBRIDRowsetRowColumn::CCUBRIDRowsetRowColumn\n");
}
~CCUBRIDRowsetRowColumn()
{
ATLTRACE(atlTraceDBProvider, 3, "CCUBRIDRowsetRowColumn::~CCUBRIDRowsetRowColumn\n");
if(m_strData) free(m_strData);
}
UINT m_uCodepage;
INT m_con;
// 메모리는 new가 아닌 malloc에 의해 할당된다.
PWSTR m_strData;
char *m_byteData;
// string의 length. type의 length(DBTYPE_U4는 4)가 아님.
DBLENGTH m_cbDataLen;
// DBSTATUS_S_OK : 제대로 된 값
// DBSTATUS_S_ISNULL : null value
// DBSTATUS_E_UNAVAILABLE : 어떤 경우?
DBSTATUS m_dwStatus;
// 가지고 있는 데이터를 pData로 복사한다.
// pData는 cbMaxLen 만큼의 공간을 가지고 있다.
// 복사후 상태와 길이를 *pdwStatus, *pcbDataLen에 반환한다.
HRESULT TransferData(CComPtr<IDataConvert> &spConvert, int iOrdinal, DBTYPE wType,
BYTE bPrecision, BYTE bScale, void *pData, DBLENGTH cbMaxLen,
DBSTATUS *pdwStatus, DBLENGTH *pcbDataLen, INT u_type = -1);
// wType의 pData로 부터 m_strData의 데이터를 채운다.
HRESULT ReadData(CComPtr<IDataConvert> &spConvert, DBTYPE wType,
BYTE *pData, DBLENGTH cbLength, ATLCOLUMNINFO *pInfo, UINT uCodepage);
// storage로 부터 m_strData를 채운다.
HRESULT ReadData(int hReq, int iOrdinal, DBTYPE wType, INT u_type = -1);
// storage에 데이터를 기록
HRESULT WriteData(int hReq, int nPos, ATLCOLUMNINFO *pInfo);
};
HRESULT CCUBRIDRowsetRowColumn::TransferData(CComPtr<IDataConvert> &spConvert, int /*iOrdinal*/,
DBTYPE wType, BYTE bPrecision, BYTE bScale, void *pData,
DBLENGTH cbMaxLen, DBSTATUS *pdwStatus, DBLENGTH *pcbDataLen, INT u_type)
{
ATLTRACE(atlTraceDBProvider, 2, "CCUBRIDRowsetRowColumn::TransferData\n");
if(m_dwStatus==DBSTATUS_E_UNAVAILABLE)
{
*pdwStatus = DBSTATUS_E_UNAVAILABLE;
*pcbDataLen = 0;
if(pData)
memset(pData, 0,cbMaxLen);
return E_FAIL; // TODO: S_OK를 반환해 주는게 좋을까?
}
if(m_dwStatus==DBSTATUS_S_ISNULL)
{ // null value
*pdwStatus = DBSTATUS_S_ISNULL;
*pcbDataLen = 0;
if(pData)
memset(pData, 0,cbMaxLen);
return S_OK;
}
DBSTATUS dbStat = DBSTATUS_S_OK;
DBLENGTH cbDst = m_cbDataLen;
HRESULT hr;
if(u_type == CCI_U_TYPE_BLOB ||
u_type == CCI_U_TYPE_CLOB)
{
hr = spConvert->DataConvert(DBTYPE_BYTES, wType, m_cbDataLen, &cbDst,
m_byteData, pData, cbMaxLen, dbStat, &dbStat, bPrecision, bScale, 0);
delete[] m_byteData;
}
else
{
hr = spConvert->DataConvert(DBTYPE_WSTR, wType, m_cbDataLen, &cbDst,
m_strData, pData, cbMaxLen, dbStat, &dbStat, bPrecision, bScale, 0);
}
//modified by swseo
if (hr == DB_E_UNSUPPORTEDCONVERSION)
{
*pdwStatus = DBSTATUS_E_CANTCONVERTVALUE;
*pcbDataLen = 0;
//if(pData)
// memset(pData, 0,cbMaxLen);
return hr;
}
*pdwStatus = dbStat;
*pcbDataLen = ( dbStat==DBSTATUS_S_ISNULL ? 0 : cbDst );
return hr;
}
HRESULT CCUBRIDRowsetRowColumn::ReadData(CComPtr<IDataConvert> &spConvert, DBTYPE wType,
BYTE *pData, DBLENGTH cbLength, ATLCOLUMNINFO *pInfo, UINT uCodepage)
{
ATLTRACE(atlTraceDBProvider, 2, "CCUBRIDRowsetRowColumn::ReadData(1)\n");
// TODO: 가능하면 spConvert를 이용하도록 수정
PWSTR strDataNew = 0;
// TODO: ADO에서 BSTR으로 넘겨주는데 BYREF가 없어도 문자열의 포인터가 넘어온다.
// BSTR은 원래 그런걸까?
DBTYPE wPureType = wType & ~DBTYPE_BYREF;
if( pData && ((wType&DBTYPE_BYREF) || wType==DBTYPE_BSTR) )
pData = *(BYTE **)pData;
switch(wPureType)
{
case DBTYPE_STR:
if(cbLength>pInfo->ulColumnSize)
return DB_E_CANTCONVERTVALUE;
// TODO: 고정 문자열 컬럼을 고려?
// Storage에서 읽어들이는 ReadData는 뒤의 스페이스를 모두 제거하고 있다.
strDataNew = _wcsdup((PCWSTR)pData);
break;
case DBTYPE_BSTR:
case DBTYPE_WSTR:
if(cbLength>pInfo->ulColumnSize*2)
return DB_E_CANTCONVERTVALUE;
// TODO: codepage를 고려해서 변환?
strDataNew = _wcsdup((LPCWSTR)pData);
break;
case DBTYPE_I2:
strDataNew = (PWSTR)malloc(7 * sizeof(WCHAR));
swprintf(strDataNew, L"%d", *(SHORT *)pData);
break;
case DBTYPE_UI2:
strDataNew = (PWSTR)malloc(7 * sizeof(WCHAR));
swprintf(strDataNew, L"%d", *(USHORT *)pData);
break;
case DBTYPE_I4:
strDataNew = (PWSTR)malloc(12 * sizeof(WCHAR));
swprintf(strDataNew, L"%d", *(LONG *)pData);
break;
case DBTYPE_UI4:
strDataNew = (PWSTR)malloc(12 * sizeof(WCHAR));
swprintf(strDataNew, L"%d", *(ULONG *)pData);
break;
case DBTYPE_DBTIME:
{
strDataNew = (PWSTR)malloc(9 * sizeof(WCHAR));
DBTIME time = *(DBTIME *)pData;
swprintf(strDataNew, L"%02d:%02d:%02d", time.hour, time.minute, time.second);
}
break;
case DBTYPE_DBDATE:
{
strDataNew = (PWSTR)malloc(11 * sizeof(WCHAR));
DBDATE date = *(DBDATE *)pData;
swprintf(strDataNew, L"%04d-%02d-%02d", date.year, date.month, date.day);
}
break;
case DBTYPE_DBTIMESTAMP:
{
strDataNew = (PWSTR)malloc(20 * sizeof(WCHAR));
DBTIMESTAMP ts = *(DBTIMESTAMP *)pData;
swprintf(strDataNew, L"%04d-%02d-%02d %02d:%02d:%02d.%03d",
ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.fraction/1000000);
}
break;
case DBTYPE_R4:
{
strDataNew = (PWSTR)malloc(50 * sizeof(WCHAR));
swprintf(strDataNew, L"%f", *(float *)pData);
}
break;
case DBTYPE_R8:
{
strDataNew = (PWSTR)malloc(50 * sizeof(WCHAR));
swprintf(strDataNew, L"%f", *(double *)pData);
}
break;
case DBTYPE_BYTES:
{
if(cbLength>pInfo->ulColumnSize)
return DB_E_CANTCONVERTVALUE;
strDataNew = (PWSTR)malloc((cbLength * 2 + 1) * sizeof(WCHAR));
for(DBLENGTH i=0;i<cbLength;i++)
{
swprintf(strDataNew+i*2, L"%02X", pData[i]);
}
}
break;
case DBTYPE_NUMERIC:
{
DB_NUMERIC &tmp = *(DB_NUMERIC *)pData;
strDataNew = (PWSTR)malloc((tmp.precision + 5) * sizeof(WCHAR)); // -, . 포함 좀 넉넉하게
spConvert->DataConvert(DBTYPE_NUMERIC, DBTYPE_WSTR, sizeof(DB_NUMERIC), NULL,
pData, strDataNew, tmp.precision+4, DBSTATUS_S_OK, NULL, 0, 0, 0);
}
break;
default:
return DB_E_CANTCONVERTVALUE;
}
ATLTRACE(atlTraceDBProvider, 3, L"CCUBRIDRowsetRowColumn::ReadData Result(type=%d) : %s\n", wType, m_strData);
if(m_strData)
{
free(m_strData);
}
m_strData = strDataNew;
m_cbDataLen = (m_strData ? (DBLENGTH) wcslen(m_strData) * sizeof(WCHAR) : 0);
m_dwStatus = DBSTATUS_S_OK;
return S_OK;
}
HRESULT CCUBRIDRowsetRowColumn::ReadData(int hReq, int iOrdinal, DBTYPE wType, INT u_type)
{
ATLTRACE(atlTraceDBProvider, 2, "CCUBRIDRowsetRowColumn::ReadData(2)\n");
if(m_strData) { free(m_strData); m_strData = 0; }
if(iOrdinal==0)
{ // Bookmark Column
return S_OK;
}
// Non-Bookmark Columns
char *value; int ind;
// time 타입을 STR로 받으면 CCI는 '1:2:3'과 같이 반환한다.
// LTM은 '01:02:03'과 같은 형태여야만 성공한다.
// 그냥 쓰기에는 상관없을 듯 하지만, 일단은 따로 처리한다.
if(wType==DBTYPE_DBTIME || wType==DBTYPE_DBTIMESTAMP)
{
T_CCI_DATE date;
if(cci_get_data(hReq, iOrdinal, CCI_A_TYPE_DATE, &date, &ind) < 0)
{
m_dwStatus = DBSTATUS_E_UNAVAILABLE;
return S_OK;
}
if(ind==-1)
{ // null value
m_dwStatus = DBSTATUS_S_ISNULL;
return S_OK;
}
if(wType==DBTYPE_DBTIME)
{
m_strData = (PWSTR)malloc(9 * sizeof(WCHAR));
swprintf(m_strData, L"%02d:%02d:%02d", date.hh, date.mm, date.ss);
m_cbDataLen = 8 * sizeof(WCHAR);
}
else // DBTYPE_DBTIMESTAMP
{
m_strData = (PWSTR)malloc(24 * sizeof(WCHAR));
swprintf(m_strData, L"%04d-%02d-%02d %02d:%02d:%02d.%03d",
date.yr, date.mon, date.day, date.hh, date.mm, date.ss, date.ms);
m_cbDataLen = 23 * sizeof(WCHAR);
}
m_dwStatus = DBSTATUS_S_OK;
return S_OK;
}
if (u_type == CCI_U_TYPE_BLOB)
{
T_CCI_BLOB blob;
T_CCI_ERROR error;
int res = cci_get_data (hReq, iOrdinal, CCI_A_TYPE_BLOB, (void *)&blob, &ind);
if(res < 0)
{
m_dwStatus = DBSTATUS_E_UNAVAILABLE;
return S_OK;
}
DBLENGTH len = (DBLENGTH)cci_blob_size(blob);
m_byteData = new char[len];
res = cci_blob_read (this->m_con, blob, 0, (int)len, m_byteData, &error);
if (res < 0)
{
delete[] m_byteData;
m_dwStatus = DBSTATUS_E_UNAVAILABLE;
return S_OK;
}
m_dwStatus = DBSTATUS_S_OK;
m_cbDataLen = len;
cci_blob_free(blob);
return S_OK;
}
else if (u_type == CCI_U_TYPE_CLOB)
{
T_CCI_CLOB clob;
T_CCI_ERROR error;
int res = cci_get_data (hReq, iOrdinal, CCI_A_TYPE_CLOB, (void *)&clob, &ind);
if(res < 0)
{
m_dwStatus = DBSTATUS_E_UNAVAILABLE;
return S_OK;
}
DBLENGTH len = (DBLENGTH)cci_clob_size(clob);
m_byteData = new char[len];
res = cci_clob_read (this->m_con, clob, 0, (int)len, m_byteData, &error);
if(res < 0)
{
delete[] m_byteData;
m_dwStatus = DBSTATUS_E_UNAVAILABLE;
return S_OK;
}
m_dwStatus = DBSTATUS_S_OK;
m_cbDataLen = len;
cci_clob_free(clob);
return S_OK;
}
if (cci_get_data(hReq, iOrdinal, CCI_A_TYPE_STR, &value, &ind) < 0)
{
m_dwStatus = DBSTATUS_E_UNAVAILABLE;
return S_OK;
}
if(ind==-1)
{ // null value
m_dwStatus = DBSTATUS_S_ISNULL;
return S_OK;
}
// 고정 크기 문자열의 경우 뒤에 필요없는 값이 붙어서 나온다.
// 이때문에 NCHAR의 경우 문제가 생길 수 있다.
// (NCHAR와 WSTR이 정확히 일치하는 타입이 아니기 때문에
// 변환시 DBSTATUS_S_TRUNCATED가 나올 수 있다.)
// 어짜피 OLE DB는 고정 크기 문자열 타입이 없기 때문에
// 제거하는 쪽을 택했다.
// TODO: 다음과 같이 제거하면 될까?
// while(value[ind-1]==' ' || (unsigned char)value[ind-1]==0xA1)
// 두번째 조건을 포함할 경우 한글이 잘릴수 있다. -- cgkang
while(value[ind-1]==' ')
{
ind--;
value[ind] = 0;
}
// TODO: BLOB일 경우도 괜찮을까?
m_strData = _wcsdup(CA2W(value, m_uCodepage));
m_dwStatus = DBSTATUS_S_OK;
m_cbDataLen = wcslen(m_strData) * sizeof(WCHAR);
return S_OK;
}
HRESULT CCUBRIDRowsetRowColumn::WriteData(int hReq, int iRowset, ATLCOLUMNINFO *pInfo)
{
ATLTRACE(atlTraceDBProvider, 2, "CCUBRIDRowsetRowColumn::WriteData\n");
T_CCI_ERROR err_buf;
int rc;
if(m_dwStatus==DBSTATUS_S_ISNULL)
rc = cci_cursor_update(hReq, iRowset+1, (int) pInfo->iOrdinal, CCI_A_TYPE_STR, NULL, &err_buf);
//else if(m_dwStatus==DBSTATUS_S_DEFAULT) //Do nothing
// rc = 0;
else
{
switch(pInfo->wType)
{
case DBTYPE_R8:
{
double val = _wtof(m_strData);
rc = cci_cursor_update(hReq, iRowset+1, (int) pInfo->iOrdinal, CCI_A_TYPE_DOUBLE, &val, &err_buf);
}
break;
case DBTYPE_BYTES:
{
T_CCI_BIT val;
val.size = (int)wcslen(m_strData)/2;
val.buf = (char *)malloc(val.size);
for(int i=0;i<val.size;i++)
{
int t;
swscanf(m_strData+i*2, L"%02X", &t);
val.buf[i] = t;
}
rc = cci_cursor_update(hReq, iRowset+1, (int) pInfo->iOrdinal, CCI_A_TYPE_BIT, &val, &err_buf);
free(val.buf);
}
break;
case DBTYPE_DBDATE:
{
T_CCI_DATE val;
int y,m,d;
swscanf(m_strData, L"%d-%d-%d", &y, &m, &d);
val.yr = y; val.mon = m; val.day = d;
rc = cci_cursor_update(hReq, iRowset+1, (int) pInfo->iOrdinal, CCI_A_TYPE_DATE, &val, &err_buf);
}
break;
case DBTYPE_DBTIMESTAMP:
{
T_CCI_DATE val;
int y,m,d,H,M,S,MS;
swscanf(m_strData, L"%d-%d-%d %d:%d:%d.%03d", &y, &m, &d, &H, &M, &S, &MS);
val.yr = y; val.mon = m; val.day = d;
val.hh = H; val.mm = M; val.ss = S;
val.ms = MS;
rc = cci_cursor_update(hReq, iRowset+1, (int) pInfo->iOrdinal, CCI_A_TYPE_DATE, &val, &err_buf);
}
break;
default:
rc = cci_cursor_update(hReq, iRowset+1, (int) pInfo->iOrdinal, CCI_A_TYPE_STR, m_strData, &err_buf);
break;
}
}
if(rc==0)
return S_OK;
else if(err_buf.err_code==-670)
return DB_E_INTEGRITYVIOLATION;
else
return E_FAIL;
}
void CCUBRIDRowsetRow::FreeData()
{
for (int i = 0; i < m_cColumns; i++)
m_rgColumns[i].~CCUBRIDRowsetRowColumn();
free(m_rgColumns);
m_rgColumns = 0;
}
HRESULT CCUBRIDRowsetRow::ReadData(int hReq, bool bOIDOnly, bool bSensitive)
{
ATLTRACE(atlTraceDBProvider, 3, "CCUBRIDRowsetRow::ReadData(1)\n");
// 컬럼 배열 생성
CCUBRIDRowsetRowColumn *pColumns; // = m_rgColumns
{
if(m_rgColumns)
{ // refresh(undo) row
pColumns = m_rgColumns;
}
else
{ // create row
pColumns = (CCUBRIDRowsetRowColumn*) malloc(m_cColumns * sizeof(CCUBRIDRowsetRowColumn));
if(pColumns==NULL) return E_OUTOFMEMORY;
for (int i = 0; i < m_cColumns; i++)
pColumns[i].CCUBRIDRowsetRowColumn::CCUBRIDRowsetRowColumn(m_uCodepage, this->m_con);
m_rgColumns = pColumns;
}
}
// cursor를 이동하고 fetch
{
// deferred delete는 메모리에 남아 있으므로
// iRowset이 storage상의 위치와 같다.
T_CCI_ERROR err_buf;
int res = cci_cursor(hReq, (int) m_iRowset+1, CCI_CURSOR_FIRST, &err_buf);
if(res==CCI_ER_NO_MORE_DATA) return DB_S_ENDOFROWSET;
if(res<0) return E_FAIL;
if(bSensitive)
res = cci_fetch_sensitive(hReq, &err_buf);
else
res = cci_fetch(hReq, &err_buf);
if(res==CCI_ER_DELETED_TUPLE) return DB_E_DELETEDROW;
if(res==CCI_ER_NO_MORE_DATA) return DB_S_ENDOFROWSET;
if(res<0) return E_FAIL;
}
cci_get_cur_oid(hReq, m_szOID);
if(bOIDOnly) return S_OK;
for(DBORDINAL i=0;i<m_cColumns;i++)
{
CCUBRIDRowsetRowColumn *pColCur = &pColumns[i];
pColCur->ReadData(hReq, (int) m_pInfo[i].iOrdinal, m_pInfo[i].wType, m_pInfo[i].u_type);
}
return S_OK;
}
HRESULT CCUBRIDRowsetRow::ReadData(ATLBINDINGS *pBinding, void *pData, UINT uCodepage)
{
ATLTRACE(atlTraceDBProvider, 3, "CCUBRIDRowsetRow::ReadData(2)\n");
// 컬럼 배열 생성
CCUBRIDRowsetRowColumn *pColumns; // = m_rgColumns
{
if(m_rgColumns)
{ // update row
pColumns = m_rgColumns;
}
else
{ // insert row
pColumns = (CCUBRIDRowsetRowColumn*) malloc(m_cColumns * sizeof(CCUBRIDRowsetRowColumn));
if(pColumns==NULL) return E_OUTOFMEMORY;
for (int i = 0; i < m_cColumns; i++)
pColumns[i].CCUBRIDRowsetRowColumn::CCUBRIDRowsetRowColumn(m_uCodepage);
m_rgColumns = pColumns;
}
}
bool bFailed = false;
bool bSucceeded = false;
for(DBORDINAL i=0;i<pBinding->cBindings;i++)
{
DBBINDING *pBindCur = &pBinding->pBindings[i];
if(pBindCur->iOrdinal==0)
{ // Bookmark Column
if(m_pInfo[0].iOrdinal!=0)
return DB_E_BADORDINAL; // 북마크가 없는데 요청했다.
// 북마크 값은 IRowset::GetData가 채운다.
continue;
}
// BINDING에 대응하는 COLUMN을 찾는다.
CCUBRIDRowsetRowColumn *pColCur;
ATLCOLUMNINFO *pInfoCur;
{
DBORDINAL j;
for( j=0; j<m_cColumns && pBindCur->iOrdinal!=m_pInfo[j].iOrdinal; j++);
if(j==m_cColumns)
{
ATLTRACE(atlTraceDBProvider, 0, "Invalid binding\n");
return DB_E_BADORDINAL;
continue;
}
pColCur = &pColumns[j];
pInfoCur = &m_pInfo[j];
}
DBSTATUS dwSrcStatus = DBSTATUS_S_OK;
DBSTATUS dwRetStatus = (DBSTATUS)-1;
if(pBindCur->dwPart & DBPART_STATUS)
dwSrcStatus = *(DBSTATUS *)((BYTE *)pData + pBindCur->obStatus);
if(pBindCur->dwPart & DBPART_VALUE)
{
BYTE *pSrcData = (BYTE *)pData+pBindCur->obValue;
if( (pBindCur->dwPart & DBPART_LENGTH) || pInfoCur->wType!=DBTYPE_BYTES)
{ // string 타입이면 문자열의 길이를 length로 삼을 수 있다.
DBLENGTH cbLength;
if(pBindCur->dwPart & DBPART_LENGTH)
cbLength = *(DBLENGTH *)((BYTE *)pData+pBindCur->obLength);
/*{
if (pInfoCur->wType==DBTYPE_STR || pInfoCur->wType==DBTYPE_WSTR)
cbLength = *(DBLENGTH *)((BYTE *)pData+pBindCur->obLength);
else
cbLength = 0;
}*/
else if(pInfoCur->wType==DBTYPE_STR)
cbLength = (DBLENGTH)strlen((const char *)pSrcData);
else if(pInfoCur->wType==DBTYPE_WSTR)
cbLength = (DBLENGTH)wcslen((const wchar_t *)pSrcData);
else
cbLength = 0; // fixed-length column?
if(dwSrcStatus==DBSTATUS_S_ISNULL)
{
pColCur->m_dwStatus = DBSTATUS_S_ISNULL;
bSucceeded = true;
}
else if(dwSrcStatus==DBSTATUS_S_DEFAULT)
{
if (m_defaultVal && m_defaultVal->GetAt(i).GetLength() > 0)
{
pColCur->m_strData = _wcsdup(CA2W(m_defaultVal->GetAt(i), m_uCodepage));
pColCur->m_cbDataLen = (DBLENGTH) wcslen(pColCur->m_strData) * sizeof(WCHAR);
pColCur->m_dwStatus = DBSTATUS_S_DEFAULT;
dwRetStatus = DBSTATUS_S_DEFAULT;
bSucceeded = true;
} else
{
pColCur->m_dwStatus = DBSTATUS_E_UNAVAILABLE;
dwRetStatus = DBSTATUS_E_UNAVAILABLE;
bSucceeded = true;
}
}
else if(dwSrcStatus==DBSTATUS_S_OK)
{
HRESULT hr = pColCur->ReadData(m_spConvert, pBindCur->wType, pSrcData, cbLength, pInfoCur, uCodepage);
if(FAILED(hr))
{
bFailed = true;
if(hr==DB_E_CANTCONVERTVALUE)
dwRetStatus = DBSTATUS_E_CANTCONVERTVALUE;
else
dwRetStatus = DBSTATUS_E_UNAVAILABLE;
}
else
bSucceeded = true;
}
else if(dwSrcStatus==DBSTATUS_S_IGNORE)
{
bSucceeded = true;
}
else
{
bFailed = true;
dwRetStatus = DBSTATUS_E_BADSTATUS;
}
}
else
{ // length 정보가 없고 string 타입도 아님
bFailed = true;
dwRetStatus = DBSTATUS_E_UNAVAILABLE;
}
}
else
{
if(pBindCur->dwPart & DBPART_LENGTH)
{
if(pInfoCur->dwFlags & DBCOLUMNFLAGS_ISLONG)
{
// 별다른 일은 하지 않는다.
bSucceeded = true;
}
else
{
bFailed = true;
dwRetStatus = DBSTATUS_E_UNAVAILABLE;
}
}
else
{
if(dwSrcStatus==DBSTATUS_S_ISNULL)
{
pColCur->m_dwStatus = DBSTATUS_S_ISNULL;
bSucceeded = true;
}
else if(dwSrcStatus==DBSTATUS_S_DEFAULT)
{
if (m_defaultVal && m_defaultVal->GetAt(i).GetLength() > 0)
{
pColCur->m_strData = _wcsdup(CA2W(m_defaultVal->GetAt(i), m_uCodepage));
pColCur->m_cbDataLen = (DBLENGTH) wcslen(pColCur->m_strData) * sizeof(WCHAR);
pColCur->m_dwStatus = DBSTATUS_S_OK;
dwRetStatus = DBSTATUS_S_DEFAULT;
bSucceeded = true;
} else
{
dwRetStatus = DBSTATUS_E_UNAVAILABLE;
bFailed = true;
}
}
else if(dwSrcStatus==DBSTATUS_S_OK)
{
bFailed = true;
dwRetStatus = DBSTATUS_E_UNAVAILABLE;
}
else if(dwSrcStatus==DBSTATUS_S_IGNORE)
{
bSucceeded = true;
}
else
{
bFailed = true;
dwRetStatus = DBSTATUS_E_BADSTATUS;
}
}
}
if( (pBindCur->dwPart & DBPART_STATUS) && dwRetStatus!=(DBSTATUS)-1 )
*(DBSTATUS *)((BYTE *)pData + pBindCur->obStatus) = dwRetStatus;
}
if(bFailed)
{ // partially succeeded?
return bSucceeded ? DB_S_ERRORSOCCURRED : DB_E_ERRORSOCCURRED;
}
else
{
return S_OK;
}
}
//Row 객체에서 GetColumns시 호출된다.
//Row의 데이터를 채운다.
//by risensh1ne
HRESULT CCUBRIDRowsetRow::ReadData(int hReq, char* szOID)
{
ATLTRACE(atlTraceDBProvider, 3, "CCUBRIDRowsetRow::ReadData(3)\n");
int res, ind;
T_CCI_SET* set;
char* strVal;
// 컬럼 배열 생성
CCUBRIDRowsetRowColumn *pColumns; // = m_rgColumns
{
if(m_rgColumns)
{ // refresh(undo) row
pColumns = m_rgColumns;
}
else
{ // create row
pColumns = (CCUBRIDRowsetRowColumn*) malloc(sizeof(CCUBRIDRowsetRowColumn));
if(pColumns==NULL) return E_OUTOFMEMORY;
pColumns->CCUBRIDRowsetRowColumn::CCUBRIDRowsetRowColumn(m_uCodepage);
m_rgColumns = pColumns;
}
}
//OID Copy
strcpy(m_szOID, szOID);
res = cci_get_data(hReq, 1, CCI_A_TYPE_SET, &set, &ind);
if(res<0) return RaiseError(E_FAIL, 0, __uuidof(IRow), L"CCUBRIDRowsetRow::ReadData(3) cci_get_data failed");
CCUBRIDRowsetRowColumn *pColCur = &pColumns[0];
if (cci_set_get(set, 1, CCI_A_TYPE_STR, &strVal, &ind) < 0)
return RaiseError(E_FAIL, 0, __uuidof(IRow), L"CCUBRIDRowsetRow::ReadData(3) cci_set_data failed");
pColCur->m_strData = _wcsdup(CA2W(strVal, m_uCodepage));
pColCur->m_dwStatus = DBSTATUS_S_OK;
pColCur->m_cbDataLen = wcslen(pColCur->m_strData) * sizeof(WCHAR);
return S_OK;
}
static CComBSTR BuildColumnValue(ATLCOLUMNINFO *m_pInfoCur, CCUBRIDRowsetRowColumn *pColCur)
{
if(pColCur->m_dwStatus==DBSTATUS_S_ISNULL)
return "NULL";
// TODO: type.*의 prefix, suffix 정보를 이용해서 구현
CComBSTR str;
CString temp(pColCur->m_strData);
switch(m_pInfoCur->wType)
{
case DBTYPE_I2:
case DBTYPE_I4:
case DBTYPE_R4:
case DBTYPE_R8:
case DBTYPE_NUMERIC:
str.Append(pColCur->m_strData);
break;
case DBTYPE_WSTR: // NCHAR?
str.Append("N'");
temp.Replace("'", "''");
str.Append(temp);
str.Append("'");
break;
case DBTYPE_BYTES:
str.Append("X'");
str.Append(pColCur->m_strData);
str.Append("'");
break;
default:
str.Append("'");
temp.Replace("'", "''");
str.Append(temp);
str.Append("'");
break;
}
return str;
}
// NOTICE: 가능하면 이 함수에서 에러가 발생하면 안 된다.
// query 중 에러가 발생할 만한 여지가 있는 것은 ReadData 시에
// 모두 찾아내야 한다.
// 그렇지 않으면 deferred update 모드에서 SetData()는 성공했는데
// Update()가 실패하게 된다.
HRESULT CCUBRIDRowsetRow::WriteData(int hConn, UINT uCodepage, int hReq, CComBSTR &strTableName)
{
ATLTRACE(atlTraceDBProvider, 3, "CCUBRIDRowsetRow::WriteData(1)\n");
HRESULT hr = S_OK;
// query 생성
CComBSTR query;
{
switch(m_status)
{
case DBPENDINGSTATUS_DELETED:
// delete from <table> where <table>=?
query.Append("delete from ");
query.Append("[");
query.Append(strTableName);
query.Append("]");
query.Append(" where ");
query.Append("[");
query.Append(strTableName);
query.Append("]");
query.Append("=?");
break;
case DBPENDINGSTATUS_NEW:
// insert into <table>(<column>,<column>) values(<value>,<value>)
{
CComBSTR column;
CComBSTR value;
for(DBORDINAL i=0;i<m_cColumns;i++)
{
ATLCOLUMNINFO *m_pInfoCur = &m_pInfo[i];
if(m_pInfoCur->iOrdinal==0) continue; // skip bookmark column
column.Append("[");
column.Append(m_pInfoCur->pwszName);
column.Append("]");
value.Append(BuildColumnValue(m_pInfoCur, m_rgColumns+i));
if(i!=m_cColumns-1) { column.Append(","); value.Append(","); }
}
query.Append("insert into ");
query.Append("[");
query.Append(strTableName);
query.Append("]");
query.Append("(");
query.Append(column);
query.Append(") values(");
query.Append(value);
query.Append(")");
}
break;
case DBPENDINGSTATUS_CHANGED:
{
// TODO: 중간에 실패하면 되돌리지를 못한다.
for(DBORDINAL i=0;i<m_cColumns;i++)
{
ATLCOLUMNINFO *pInfoCur = &m_pInfo[i];
if(pInfoCur->iOrdinal==0) continue; // skip bookmark column
CCUBRIDRowsetRowColumn *pCol = m_rgColumns+i;
HRESULT hr = pCol->WriteData(hReq, (int) m_iRowset, pInfoCur);
if(FAILED(hr))
return hr;
}
return S_OK;
}
/* Query Base: view에 적용불가
// update <table> set <column>=<value>, <column>=<value> where <table>=?
{
CComBSTR set;
for(DBORDINAL i=0;i<m_cColumns;i++)
{
ATLCOLUMNINFO *m_pInfoCur = &m_pInfo[i];
if(m_pInfoCur->iOrdinal==0) continue; // skip bookmark column
set.Append(m_pInfoCur->pwszName);
set.Append("=");
set.Append(BuildColumnValue(m_pInfoCur, m_rgColumns+i));
if(i!=m_cColumns-1) set.Append(",");
}
query.Append("update ");
query.Append("[");
query.Append(strTableName);
query.Append("]");
query.Append(" set ");
query.Append(set);
query.Append(" where ");
query.Append("[");
query.Append(strTableName);
query.Append("]");
query.Append("=?");
}
*/
break;
default:
return DB_E_ERRORSOCCURRED;
}
}
//#ifdef _DEBUG
// {
// // ATLTRACE는 한번에 1024문자까지 밖에 출력하지 못한다.
// // 그래서 이렇게 출력한다.
// ATLTRACE(atlTraceDBProvider, 2, "Execute Query = ");
// CW2A local(query);
// for(size_t i=0;i<strlen(local);)
// {
// char buf[513]; memcpy(buf, local+i, 512); buf[512] = 0;
// ATLTRACE(atlTraceDBProvider, 2, "%s", buf);
// i += 512;
// }
// ATLTRACE(atlTraceDBProvider, 2, "\n");
// }
//#endif
// prepare query
int hLocalReq;
{
T_CCI_ERROR err_buf;
hLocalReq = cci_prepare(hConn, CW2A(query, uCodepage), 0, &err_buf);
if (hLocalReq<0) return RaiseError(DB_E_ERRORSOCCURRED, 1, __uuidof(IRowset), CA2W(err_buf.err_msg, uCodepage));
}
// execute query
{
if(m_status==DBPENDINGSTATUS_DELETED || m_status==DBPENDINGSTATUS_CHANGED)
cci_bind_param(hLocalReq, 1, CCI_A_TYPE_STR, m_szOID, CCI_U_TYPE_OBJECT, 0);
T_CCI_ERROR err_buf;
int rc = cci_execute(hLocalReq, 0, 0, &err_buf);
if(rc<0)
{
cci_close_req_handle(hLocalReq);
if(err_buf.err_code==-670) // constraint violation code 맞나?
return RaiseError(DB_E_INTEGRITYVIOLATION, 0, __uuidof(IRowset));
else
return RaiseError(DB_E_ERRORSOCCURRED, 1, __uuidof(IRowset), CA2W(err_buf.err_msg, uCodepage));
}
else if(rc==0)
{
if(m_status==DBPENDINGSTATUS_DELETED)
hr = DB_E_DELETEDROW;
}
else
{
ATLASSERT(rc==1);
}
}
cci_close_req_handle(hLocalReq);
return hr;
}
HRESULT CCUBRIDRowsetRow::WriteData(ATLBINDINGS *pBinding, void *pData, DBROWCOUNT dwBookmark, CCUBRIDRowset* pRowset)
{
ATLTRACE(atlTraceDBProvider, 3, "CCUBRIDRowsetRow::WriteData(2)\n");
bool bFailed = false;
bool bSucceeded = false;
for(DBCOUNTITEM i=0;i<pBinding->cBindings;i++)
{
DBBINDING *pBindCur = &(pBinding->pBindings[i]);
if(pBindCur->iOrdinal==0)
{ // Bookmark Column
DBSTATUS dbStat = DBSTATUS_S_OK;
DBLENGTH cbDst = 4;
if(pBindCur->dwPart & DBPART_VALUE)
{
// deferred validation
// DBLENGTH는 unsigned이므로 obStatus<obValue는 고려하지 않아도 됨
// DataConvert 함수에서 DBSTATUS_S_TRUNCATED 반환
DBLENGTH cbMaxLen = pBindCur->cbMaxLen;
if(pBindCur->dwPart & DBPART_STATUS)
{
DBLENGTH cbLen = pBindCur->obStatus - pBindCur->obValue;
if(cbLen<cbMaxLen)
cbMaxLen = cbLen;
}
if(pBindCur->dwPart & DBPART_LENGTH)
{
DBLENGTH cbLen = pBindCur->obLength - pBindCur->obValue;
if(cbLen<cbMaxLen)