forked from CUBRID/cubrid-oledb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rowset.cpp
1315 lines (1117 loc) · 39.3 KB
/
Rowset.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.
*
*/
// Rowset.cpp : Implementation of CCUBRIDCommand
#include "stdafx.h"
#include "Rowset.h"
#include "Row.h"
#include "DataSource.h"
#include "Error.h"
#include "CUBRIDStream.h"
CCUBRIDDataSource *CCUBRIDRowset::GetDataSourcePtr()
{
return GetSessionPtr()->GetDataSourcePtr();
}
CCUBRIDSession *CCUBRIDRowset::GetSessionPtr()
{
switch(m_eType)
{
case FromSession:
return CCUBRIDSession::GetSessionPtr(this);
case FromCommand:
return GetCommandPtr()->GetSessionPtr();
case FromRow:
return NULL; // TODO
default: // Invalid
return NULL;
}
}
CCUBRIDCommand *CCUBRIDRowset::GetCommandPtr()
{
return CCUBRIDCommand::GetCommandPtr(this);
}
CCUBRIDRowset *CCUBRIDRowset::GetRowsetPtr(IObjectWithSite *pSite)
{
CComPtr<IRowset> spCom;
HRESULT hr = pSite->GetSite(__uuidof(IRowset), (void **)&spCom);
// 제대로 프로그래밍 됐을때, 실패하는 경우가 있을까?
ATLASSERT(SUCCEEDED(hr));
// 굳이 오버헤드를 감수해가며 dynamic_cast를 쓸 필요는 없을 듯
return static_cast<CCUBRIDRowset *>((IRowset *)spCom);
}
int CCUBRIDRowset::GetRequestHandle()
{
switch(m_eType)
{
case FromSession:
return m_hReq;
case FromCommand:
return GetCommandPtr()->m_hReq;
case FromRow:
return 0; // TODO
default: // Invalid
return 0;
}
}
CCUBRIDRowset::CCUBRIDRowset()
: m_eType(Invalid), m_hReq(0), m_bAsynch(false), m_nStatus(0), m_bFindForward(true)
{
ATLTRACE(atlTraceDBProvider, 3, "CCUBRIDRowset::CCUBRIDRowset\n");
// RegisterTxnCallback은 m_eType이 정해진 후에야 가능하다.
}
CCUBRIDRowset::~CCUBRIDRowset()
{
ATLTRACE(atlTraceDBProvider, 3, "CCUBRIDRowset::~CCUBRIDRowset\n");
CCUBRIDSession *pSession = GetSessionPtr();
if(pSession)
{
//pSession->RowsetCommit();
#if 1
if(m_bIsChangeable) pSession->RowsetCommit();
#endif
pSession->RegisterTxnCallback(this, false);
}
}
HRESULT CCUBRIDRowset::ValidateCommandID(DBID *pTableID, DBID *pIndexID)
{
HRESULT hr = _RowsetBaseClass::ValidateCommandID(pTableID, pIndexID);
if (hr != S_OK)
return hr;
if(pIndexID)
return RaiseError(DB_E_NOINDEX, 0, __uuidof(IOpenRowset)); // 인덱스를 지원하지 않는다.
if(pTableID && (pTableID->uName.pwszName==NULL || wcslen(pTableID->uName.pwszName)==0))
return RaiseError(DB_E_NOTABLE, 0, __uuidof(IOpenRowset)); // 테이블 이름이 없다.
return S_OK;
}
// cci_fetch_size를 설정한다.
static HRESULT SetFetchSize(CCUBRIDRowset *pRowset)
{
CCUBRIDDataSource *pDS = pRowset->GetDataSourcePtr();
CComVariant var;
pDS->GetPropValue(&DBPROPSET_UNIPROVIDER_DBINIT, DBPROP_UNIPROVIDER_FETCH_SIZE, &var);
int rc = cci_fetch_size(pRowset->GetRequestHandle(), V_I4(&var));
if(rc<0)
RaiseError(E_FAIL, 1, __uuidof(IRowset), L"Failed to set the fetch size");
return S_OK;
}
HRESULT CCUBRIDRowset::InitCommon(int cResult, bool bRegist)
{
m_rgRowData.SetCount(cResult);
// 1과 2는 DBBMK_FIRST, DBBMK_LAST를 위해 비워놓는다.
// cbBookmark가 1과 4로 다르긴 한데
// IRowsetLocateImpl은 구현이 잘못되서 구별을 잘 못한다.
m_rgBookmarks.SetCount(cResult+3);
m_rgBookmarks[0] = m_rgBookmarks[1] = m_rgBookmarks[2] = -1;
for(int i=3;i<cResult+3;i++)
{
m_rgBookmarks[i] = i-2;
}
HRESULT hr = SetFetchSize(this);
if(FAILED(hr)) return hr;
if(bRegist)
GetSessionPtr()->RegisterTxnCallback(this, true);
return S_OK;
}
HRESULT CCUBRIDRowset::InitFromSession(DBID *pTID, char flag)
{
int hConn = CCUBRIDSession::GetSessionPtr(this)->GetConnection();
m_uCodepage = CCUBRIDSession::GetSessionPtr(this)->GetCodepage();
/* 아직 미지원
CComVariant var;
GetPropValue(&DBPROPSET_ROWSET, DBPROP_ROWSET_ASYNCH, &var);
ATLASSERT(V_VT(&var)==VT_I4);
m_bAsynch = ( (V_I4(&var)&DBPROPVAL_ASYNCH_POPULATEONDEMAND) != 0 );
*/
int hReq, cResult;
{
CComVariant var;
GetPropValue(&DBPROPSET_ROWSET, DBPROP_MAXROWS, &var);
HRESULT hr = Util::OpenTable(hConn, m_uCodepage, pTID->uName.pwszName, &hReq, &cResult, flag, m_bAsynch, V_I4(&var));
if(FAILED(hr)) return hr;
}
m_hReq = hReq;
m_eType = FromSession;
m_strTableName = m_strCommandText;
return InitCommon(cResult);
}
HRESULT CCUBRIDRowset::InitFromCommand(int hReq, UINT uCodepage, int cResult, bool bAsynch)
{
/* 아직 미지원
m_bAsynch = bAsynch;
*/
m_uCodepage = uCodepage;
m_eType = FromCommand;
Util::ExtractTableName(m_strCommandText, m_strTableName);
return InitCommon(cResult);
}
/*
HRESULT CCUBRIDRowset::InitFromRow(int hReq, int cResult)
{
m_eType = FromRow;
return InitCommon(hReq, cResult);
}
*/
HRESULT CCUBRIDRowset::Reexecute()
{
int hConn = GetSessionPtr()->GetConnection();
m_uCodepage = GetSessionPtr()->GetCodepage();
int cResult = 0;
char flag = CCI_PREPARE_INCLUDE_OID | CCI_PREPARE_UPDATABLE;
{
if(m_eType==FromCommand)
{
int &hReq = GetCommandPtr()->m_hReq;
cci_close_req_handle(hReq);
hReq = 0;
T_CCI_ERROR err_buf;
hReq = cci_prepare(hConn, CW2A(m_strCommandText.m_str, m_uCodepage), flag, &err_buf);
if(hReq>0)
{
CComVariant var;
GetPropValue(&DBPROPSET_ROWSET, DBPROP_MAXROWS, &var);
//cci_set_max_row(hReq, V_I4(&var));
cResult = cci_execute(hReq, 0, 0, &err_buf);
}
else
hReq = 0;
}
else if(m_eType==FromSession)
{
CComVariant var;
GetPropValue(&DBPROPSET_ROWSET, DBPROP_MAXROWS, &var);
int hReq = 0;
HRESULT hr = Util::OpenTable(hConn, m_uCodepage, m_strCommandText, &hReq, &cResult, flag, false, V_I4(&var));
if(FAILED(hr)) return E_FAIL;
m_hReq = hReq;
}
}
InitCommon(cResult, false);
m_nStatus = 0;
return S_OK;
}
void CCUBRIDRowset::TxnCallback(const ITxnCallback *pOwner)
{
ATLTRACE(atlTraceDBProvider, 2, "CCUBRIDRowset::TxnCallback\n");
ITxnCallback *pMyOwner = this;
if(m_eType==FromCommand)
pMyOwner = GetCommandPtr();
if(pOwner!=pMyOwner)
{
cci_close_req_handle(m_hReq);
m_hReq = 0;
m_nStatus = 1;
}
else
{
//m_nStatus = 2; // RestartPosition시 질의를 재실행
m_nStatus = 0; // RestartPosition시 질의를 재실행하지 않음
}
}
STDMETHODIMP CCUBRIDRowset::AddRefAccessor(HACCESSOR hAccessor, DBREFCOUNT *pcRefCount)
{
ClearError();
if(m_nStatus==1) return RaiseError(E_UNEXPECTED, 1, __uuidof(IAccessor), L"This object is in a zombie state");
HRESULT hr = _RowsetBaseClass::AddRefAccessor(hAccessor, pcRefCount);
if(FAILED(hr))
return RaiseError(hr, 0, __uuidof(IAccessor));
else
return hr;
}
STDMETHODIMP CCUBRIDRowset::CreateAccessor(DBACCESSORFLAGS dwAccessorFlags, DBCOUNTITEM cBindings,
const DBBINDING rgBindings[], DBLENGTH cbRowSize,
HACCESSOR *phAccessor, DBBINDSTATUS rgStatus[])
{
ClearError();
if(m_nStatus==1) return RaiseError(E_UNEXPECTED, 1, __uuidof(IAccessor), L"This object is in a zombie state");
HRESULT hr = _RowsetBaseClass::CreateAccessor(dwAccessorFlags, cBindings,
rgBindings, cbRowSize, phAccessor, rgStatus);
if(FAILED(hr))
return RaiseError(hr, 0, __uuidof(IAccessor));
else
return hr;
}
STDMETHODIMP CCUBRIDRowset::GetBindings(HACCESSOR hAccessor, DBACCESSORFLAGS *pdwAccessorFlags,
DBCOUNTITEM *pcBindings, DBBINDING **prgBindings)
{
ClearError();
if(m_nStatus==1) return RaiseError(E_UNEXPECTED, 1, __uuidof(IAccessor), L"This object is in a zombie state");
HRESULT hr = _RowsetBaseClass::GetBindings(hAccessor, pdwAccessorFlags,
pcBindings, prgBindings);
if(FAILED(hr))
return RaiseError(hr, 0, __uuidof(IAccessor));
else
return hr;
}
STDMETHODIMP CCUBRIDRowset::ReleaseAccessor(HACCESSOR hAccessor, DBREFCOUNT *pcRefCount)
{
ClearError();
HRESULT hr = _RowsetBaseClass::ReleaseAccessor(hAccessor, pcRefCount);
if(FAILED(hr))
return RaiseError(hr, 0, __uuidof(IAccessor));
else
return hr;
}
STDMETHODIMP CCUBRIDRowset::GetColumnDefaultValue(CCUBRIDRowset* pv)
{
HRESULT hr = S_OK;
int hConn, hReq, res;
T_CCI_ERROR error;
if (!pv->m_Columns.m_defaultVal)
{
hConn = GetSessionPtr()->GetConnection();
res = cci_schema_info(hConn, CCI_SCH_ATTRIBUTE, CW2A(m_strTableName.m_str, m_uCodepage), NULL,
CCI_ATTR_NAME_PATTERN_MATCH, &error);
if (res<0) return RaiseError(E_FAIL, 0, __uuidof(IColumnsInfo));
hReq = res;
res = cci_cursor(hReq, 1, CCI_CURSOR_FIRST, &error);
if(pv->m_Columns.m_cColumns > 0 && res==CCI_ER_NO_MORE_DATA)
return RaiseError(E_FAIL, 0, __uuidof(IColumnsInfo));
if(res<0) return RaiseError(E_FAIL, 0, __uuidof(IColumnsInfo));
pv->m_Columns.m_defaultVal = new CAtlArray<CStringA>();
while(1)
{
char* buffer;
int ind;
res = cci_fetch(hReq, &error);
if(res<0) return RaiseError(E_FAIL, 0, __uuidof(IColumnsInfo));
res = cci_get_data(hReq, 9, CCI_A_TYPE_STR, &buffer, &ind);
if(res<0) return RaiseError(E_FAIL, 0, __uuidof(IColumnsInfo));
if (ind == -1)
pv->m_Columns.m_defaultVal->Add("");
else
{
ATLASSERT(buffer);
pv->m_Columns.m_defaultVal->Add(buffer);
}
res = cci_cursor(hReq, 1, CCI_CURSOR_CURRENT, &error);
if(res==CCI_ER_NO_MORE_DATA) break;
}
cci_close_req_handle(hReq);
}
return hr;
}
ATLCOLUMNINFO* CCUBRIDRowset::GetColumnInfo(CCUBRIDRowset *pv, DBORDINAL *pcCols)
{
if(!pv->m_Columns.m_pInfo)
{
CComVariant var;
pv->GetPropValue(&DBPROPSET_ROWSET, DBPROP_BOOKMARKS, &var);
HRESULT hr = pv->m_Columns.GetColumnInfo(pv->GetRequestHandle(), pv->m_uCodepage,
V_BOOL(&var)==ATL_VARIANT_TRUE,
pv->GetDataSourcePtr()->PARAM_MAX_STRING_LENGTH);
if(FAILED(hr))
return NULL;
//Changable일 경우만 default 값을 가져온다.
//CComVariant varChange, varUpdate;
//pv->GetPropValue(&DBPROPSET_ROWSET, DBPROP_IRowsetChange, &varChange);
//pv->GetPropValue(&DBPROPSET_ROWSET, DBPROP_IRowsetUpdate, &varUpdate);
//if (varChange.boolVal == ATL_VARIANT_TRUE || varUpdate.boolVal == ATL_VARIANT_TRUE)
//{
// pv->GetColumnDefaultValue(pv);
// //실패해도 에러 리턴 안함
//}
// TODO: Command 쪽에도 추가?
// 컬럼별 FINDCOMPAREOPS 등록
{
ULONG iCurSet, iCurProp;
pv->GetIndexofPropSet(&DBPROPSET_ROWSET, &iCurSet);
pv->GetIndexofPropIdinPropSet(iCurSet, DBPROP_FINDCOMPAREOPS, &iCurProp);
DBPROP prop;
prop.dwPropertyID = DBPROP_FINDCOMPAREOPS;
prop.dwStatus = DBSTATUS_S_OK;
prop.dwOptions = 0;
prop.vValue.vt = VT_I4;
for(int i=0;i<pv->m_Columns.m_cColumns;i++)
{
pv->CDBIDOps::CopyDBIDs(&prop.colid, &pv->m_Columns.m_pInfo[i].columnid);
prop.vValue.lVal = ::Type::GetFindCompareOps(pv->m_Columns.m_pInfo[i].wType);
pv->SetProperty(iCurSet, iCurProp, &prop);
}
}
}
if(pcCols)
*pcCols = pv->m_Columns.m_cColumns;
return pv->m_Columns.m_pInfo;
}
STDMETHODIMP CCUBRIDRowset::GetColumnInfo(DBORDINAL *pcColumns, DBCOLUMNINFO **prgInfo,
OLECHAR **ppStringsBuffer)
{
ClearError();
if(m_nStatus==1) return RaiseError(E_UNEXPECTED, 1, __uuidof(IColumnsInfo), L"This object is in a zombie state");
m_uCodepage = GetSessionPtr()->GetCodepage();
HRESULT hr = IColumnsInfoImpl<CCUBRIDRowset>::GetColumnInfo(pcColumns, prgInfo, ppStringsBuffer);
if(FAILED(hr)) return RaiseError(hr, 0, __uuidof(IColumnsInfo));
return hr;
}
STDMETHODIMP CCUBRIDRowset::MapColumnIDs(DBORDINAL cColumnIDs, const DBID rgColumnIDs[],
DBORDINAL rgColumns[])
{
ClearError();
if(m_nStatus==1) return RaiseError(E_UNEXPECTED, 1, __uuidof(IColumnsInfo), L"This object is in a zombie state");
HRESULT hr = _RowsetBaseClass::MapColumnIDs(cColumnIDs, rgColumnIDs, rgColumns);
if(FAILED(hr))
return RaiseError(hr, 0, __uuidof(IColumnsInfo));
else
return hr;
}
STDMETHODIMP CCUBRIDRowset::CanConvert(DBTYPE wFromType, DBTYPE wToType, DBCONVERTFLAGS dwConvertFlags)
{
ClearError();
HRESULT hr = _RowsetBaseClass::CanConvert(wFromType, wToType, dwConvertFlags);
if(FAILED(hr))
return RaiseError(hr, 0, __uuidof(IConvertType));
else
return hr;
}
// MapClass의 key와 RowClass의 초기화(ReadData 호출) 때문에 override
HRESULT CCUBRIDRowset::CreateRow(DBROWOFFSET lRowsOffset, DBCOUNTITEM &cRowsObtained, HROW *rgRows)
{
CCUBRIDRowsetRow *pRow = NULL;
ATLASSERT(lRowsOffset >= 0);
CCUBRIDRowsetRow::KeyType key = lRowsOffset+1;
ATLASSERT(key > 0);
bool bFound = m_rgRowHandles.Lookup(key,pRow);
if (!bFound || pRow == NULL)
{
DBORDINAL cCols;
ATLCOLUMNINFO *pInfo = GetColumnInfo(this, &cCols);
CCUBRIDSession *ps = this->GetSessionPtr();
ATLTRY(pRow = new CCUBRIDRowsetRow(m_uCodepage, lRowsOffset, cCols, pInfo, m_spConvert, m_Columns.m_defaultVal, ps->GetConnection()))
if (pRow == NULL)
return E_OUTOFMEMORY;
bool bSensitive = false;
{
CComVariant var;
GetPropValue(&DBPROPSET_ROWSET, DBPROP_OWNUPDATEDELETE, &var);
bSensitive = (V_BOOL(&var)==VARIANT_TRUE);
}
HRESULT hr = pRow->ReadData(GetRequestHandle(), false, bSensitive);
if(FAILED(hr))
{
delete pRow;
pRow = NULL;
return hr;
}
_ATLTRY
{
m_rgRowHandles.SetAt(key, pRow);
}
_ATLCATCH( e )
{
_ATLDELETEEXCEPTION( e );
delete pRow;
pRow = NULL;
return E_OUTOFMEMORY;
}
}
else // found pRow
{
if(pRow->m_status==DBPENDINGSTATUS_INVALIDROW)
return DB_E_DELETEDROW;
}
pRow->AddRefRow();
m_bReset = false;
rgRows[cRowsObtained++] = (HROW)key;
return S_OK;
}
STDMETHODIMP CCUBRIDRowset::AddRefRows(DBCOUNTITEM cRows, const HROW rghRows[],
DBREFCOUNT rgRefCounts[], DBROWSTATUS rgRowStatus[])
{
ClearError();
if(m_nStatus==1) return RaiseError(E_UNEXPECTED, 1, __uuidof(IRowset), L"This object is in a zombie state");
HRESULT hr = _RowsetBaseClass::AddRefRows(cRows, rghRows, rgRefCounts, rgRowStatus);
if(FAILED(hr))
return RaiseError(hr, 0, __uuidof(IRowset));
else
return hr;
}
STDMETHODIMP CCUBRIDRowset::GetData(HROW hRow, HACCESSOR hAccessor, void *pDstData)
{
ClearError();
if(m_nStatus==1) return RaiseError(E_UNEXPECTED, 1, __uuidof(IRowset), L"This object is in a zombie state");
ATLTRACE(atlTraceDBProvider, 2, _T("IRowset::GetData\n"));
HRESULT hr;
CCUBRIDRowsetRow *pRow = 0;
ATLBINDINGS *pBinding = 0;
{ // check arguments and prepare data
if(hRow==NULL || !m_rgRowHandles.Lookup((ULONG)hRow, pRow) || pRow==NULL)
return RaiseError(DB_E_BADROWHANDLE, 0, __uuidof(IRowset));
if(!m_rgBindings.Lookup((ULONG)hAccessor, pBinding) || pBinding==NULL)
return RaiseError(DB_E_BADACCESSORHANDLE, 0, __uuidof(IRowset));
if(pDstData==NULL && pBinding->cBindings!=0)
return RaiseError(E_INVALIDARG, 0, __uuidof(IRowset));
}
if(pRow->m_status==DBPENDINGSTATUS_INVALIDROW || pRow->m_status==DBPENDINGSTATUS_DELETED)
return RaiseError(DB_E_DELETEDROW, 0, __uuidof(IRowset));
DBROWCOUNT dwBookmark = pRow->m_iRowset+3;//Util::FindBookmark(m_rgBookmarks, (LONG)pRow->m_iRowset+1);
hr = pRow->WriteData(pBinding, pDstData, dwBookmark, this);
if(FAILED(hr))
return RaiseError(hr, 0, __uuidof(IRowset));
else
return hr;
}
HRESULT CCUBRIDRowset::GetNextRowsAsynch(HCHAPTER hReserved, DBROWOFFSET lRowsOffset,
DBROWCOUNT cRows, DBCOUNTITEM *pcRowsObtained, HROW **prghRows)
{
// TODO: backward fetch 미테스트(아마 잘 안 될듯)
// bookmark 미구현
ATLTRACE(atlTraceDBProvider, 2, "CCUBRIDRowset::GetNextRowsAsynch\n");
if(prghRows==NULL || pcRowsObtained==NULL) return E_INVALIDARG;
if(cRows==0) return S_OK;
if(lRowsOffset<0 && !m_bCanScrollBack)
return DB_E_CANTSCROLLBACKWARDS;
if(cRows<0 && !m_bCanFetchBack)
return DB_E_CANTFETCHBACKWARDS;
// In the case where the user is moving backwards after moving forwards,
// we do not wrap around to the end of the rowset.
if(m_iRowset==0 && !m_bReset && cRows<0)
return DB_S_ENDOFROWSET;
if(lRowsOffset<0 && m_bReset)
return DB_E_CANTSCROLLBACKWARDS; // 아직 데이터가 다 없는데 backward가 가능한가?
int iStepSize = ( cRows >= 0 ? 1 : -1 );
cRows = AbsVal(cRows); // if cRows==MINLONG_PTR?
DBROWOFFSET lTmpRows = lRowsOffset;
lRowsOffset += m_iRowset;
CComHeapPtr<HROW> rghRowsAllocated;
if(*prghRows==NULL)
{
rghRowsAllocated.Allocate(cRows); // 일단 최대로 잡는다.
if(rghRowsAllocated==NULL)
return E_OUTOFMEMORY;
*prghRows = rghRowsAllocated;
}
HRESULT hr = S_OK;
while(lRowsOffset>=0 && cRows!=0)
{
// cRows > cRowsInSet && iStepSize < 0
if (lRowsOffset == 0 && cRows > 0 && iStepSize < 0)
break;
hr = CreateRow(lRowsOffset, *pcRowsObtained, *prghRows);
if(FAILED(hr))
{
RefRows(*pcRowsObtained, *prghRows, NULL, NULL, FALSE);
for(ULONG iRowDel=0;iRowDel<*pcRowsObtained;iRowDel++)
(*prghRows)[iRowDel] = NULL;
*pcRowsObtained = 0;
return hr;
}
// TODO: hr==DB_S_ENDOFROWSET이면 다 읽었다는 표시인데
// m_bAsynch=false로 바꾸는게 좋을까?
// 이 경우 Commit에 의해 다시 테이블을 읽어들일때
// 문제가 될지도 모르겠다.
if(hr!=S_OK) break;
if(m_rgRowData.GetCount()<=(size_t)lRowsOffset)
m_rgRowData.SetCount(lRowsOffset+1);
if(m_rgBookmarks.GetCount()<=(size_t)lRowsOffset+3)
m_rgBookmarks.SetCount(lRowsOffset+4);
m_rgBookmarks[lRowsOffset+3] = lRowsOffset+1;
cRows--;
lRowsOffset += iStepSize;
}
m_iRowset = lRowsOffset;
if(SUCCEEDED(hr) && *pcRowsObtained>0)
rghRowsAllocated.Detach();
else
{
if(rghRowsAllocated)
*prghRows = 0; // 입력시 *prghRows==NULL이고 에러가 발생했으면 다시 NULL로 만든다.
}
return hr;
}
STDMETHODIMP CCUBRIDRowset::GetNextRows(HCHAPTER hReserved, DBROWOFFSET lRowsOffset,
DBROWCOUNT cRows, DBCOUNTITEM *pcRowsObtained, HROW **prghRows)
{
ClearError();
if(pcRowsObtained) *pcRowsObtained = 0;
if(m_nStatus==1) return RaiseError(E_UNEXPECTED, 1, __uuidof(IRowset), L"This object is in a zombie state");
if(cRows==0) return S_OK;
CHECK_RESTART(__uuidof(IRowset));
if(!m_bExternalFetch)
CHECK_CANHOLDROWS(__uuidof(IRowset));
if(m_bAsynch)
return GetNextRowsAsynch(hReserved, lRowsOffset, cRows, pcRowsObtained, prghRows);
bool bProvAlloc = (pcRowsObtained && prghRows && *prghRows==0);
HRESULT hr;
hr = _RowsetBaseClass::GetNextRows(hReserved, lRowsOffset, cRows, pcRowsObtained, prghRows);
if(bProvAlloc && *pcRowsObtained==0)
{ // ATL Provider Templates의 버그
*prghRows = 0;
}
if(FAILED(hr))
return RaiseError(hr, 0, __uuidof(IRowset));
else
return hr;
}
STDMETHODIMP CCUBRIDRowset::ReleaseRows(DBCOUNTITEM cRows, const HROW rghRows[],
DBROWOPTIONS rgRowOptions[], DBREFCOUNT rgRefCounts[],
DBROWSTATUS rgRowStatus[])
{
ClearError();
HRESULT hr = _RowsetBaseClass::ReleaseRows(cRows, rghRows, rgRowOptions, rgRefCounts, rgRowStatus);
if(FAILED(hr))
return RaiseError(hr, 0, __uuidof(IRowset));
else
return hr;
}
STDMETHODIMP CCUBRIDRowset::RestartPosition(HCHAPTER hReserved)
{
ClearError();
if(m_nStatus==1) return RaiseError(E_UNEXPECTED, 1, __uuidof(IRowset), L"This object is in a zombie state");
HRESULT hr = _RowsetBaseClass::RestartPosition(hReserved);
if(FAILED(hr))
return RaiseError(hr, 0, __uuidof(IRowset));
cci_fetch_buffer_clear(GetRequestHandle());
if(m_nStatus==2)
{
hr = Reexecute();
if(FAILED(hr))
return RaiseError(E_FAIL, 1, __uuidof(IRowset), L"Failed to reexecute the command");
return DB_S_COMMANDREEXECUTED;
}
return S_OK;
}
// Provider Templates의 구현은 DELETED 상태인 ROW에 대해 DB_E_DELETEDROW를 반환한다.
// spec은 INVALIDROW에 대해서 반환하게 되어 있다.
STDMETHODIMP CCUBRIDRowset::IsSameRow(HROW hThisRow, HROW hThatRow)
{
ClearError();
if(m_nStatus==1) return RaiseError(E_UNEXPECTED, 1, __uuidof(IRowsetIdentity), L"This object is in a zombie state");
ATLTRACE(atlTraceDBProvider, 2, "CCUBRIDRowset::IsSameRow\n");
// Validate row handles
CCUBRIDRowsetRow *pRow1;
if( ! m_rgRowHandles.Lookup((CCUBRIDRowsetRow::KeyType)hThisRow, pRow1) )
return DB_E_BADROWHANDLE;
CCUBRIDRowsetRow *pRow2;
if( ! m_rgRowHandles.Lookup((CCUBRIDRowsetRow::KeyType)hThatRow, pRow2) )
return DB_E_BADROWHANDLE;
if (pRow1->m_status == DBPENDINGSTATUS_NEW ||
pRow2->m_status == DBPENDINGSTATUS_NEW)
return DB_E_NEWLYINSERTED;
if (pRow1->m_status == DBPENDINGSTATUS_INVALIDROW ||
pRow2->m_status == DBPENDINGSTATUS_INVALIDROW)
return DB_E_DELETEDROW;
return pRow1->Compare(pRow2);
}
HRESULT CCUBRIDRowset::OnPropertyChanged(ULONG iCurSet, DBPROP* pDBProp)
{
HRESULT hr = CUBRIDOnPropertyChanged(this, iCurSet, pDBProp);
if(hr==S_FALSE)
return _RowsetBaseClass::OnPropertyChanged(iCurSet, pDBProp);
else
return hr;
}
HRESULT CCUBRIDRowset::IsValidValue(ULONG iCurSet, DBPROP* pDBProp)
{
ATLASSERT(pDBProp);
if(pDBProp->dwPropertyID==DBPROP_ROWSET_ASYNCH)
{
// TODO: PREPOPULATE와 POPULATEONDEMAND 둘 다 세팅하는 건 에러겠지?
LONG val = V_I4(&pDBProp->vValue);
if(val==0 || val==DBPROPVAL_ASYNCH_PREPOPULATE // --> synchronous
|| val==DBPROPVAL_ASYNCH_POPULATEONDEMAND) // --> asynchronous
return S_OK;
return S_FALSE;
}
return _RowsetBaseClass::IsValidValue(iCurSet, pDBProp);
}
STDMETHODIMP CCUBRIDRowset::GetProperties(const ULONG cPropertyIDSets, const DBPROPIDSET rgPropertyIDSets[],
ULONG *pcPropertySets, DBPROPSET **prgPropertySets)
{
ClearError();
if(m_nStatus==1) return RaiseError(E_UNEXPECTED, 1, __uuidof(IRowsetInfo), L"This object is in a zombie state");
HRESULT hr = _RowsetBaseClass::GetProperties(cPropertyIDSets, rgPropertyIDSets,
pcPropertySets, prgPropertySets);
if(FAILED(hr))
return RaiseError(hr, 0, __uuidof(IRowsetInfo));
else
return hr;
}
STDMETHODIMP CCUBRIDRowset::GetReferencedRowset(DBORDINAL iOrdinal, REFIID riid,
IUnknown **ppReferencedRowset)
{
ClearError();
if(m_nStatus==1) return RaiseError(E_UNEXPECTED, 1, __uuidof(IRowsetInfo), L"This object is in a zombie state");
HRESULT hr = _RowsetBaseClass::GetReferencedRowset(iOrdinal, riid, ppReferencedRowset);
if(FAILED(hr))
return RaiseError(hr, 0, __uuidof(IRowsetInfo));
else
return hr;
}
STDMETHODIMP CCUBRIDRowset::GetSpecification(REFIID riid, IUnknown **ppSpecification)
{
ClearError();
if(m_nStatus==1) return RaiseError(E_UNEXPECTED, 1, __uuidof(IRowsetInfo), L"This object is in a zombie state");
HRESULT hr = _RowsetBaseClass::GetSpecification(riid, ppSpecification);
if(FAILED(hr))
return RaiseError(hr, 0, __uuidof(IRowsetInfo));
else
return hr;
}
STDMETHODIMP CCUBRIDRowset::GetRowFromHROW(IUnknown *pUnkOuter, HROW hRow,
REFIID riid, IUnknown **ppUnk)
{
ClearError();
if(m_nStatus==1) return RaiseError(E_UNEXPECTED, 1, __uuidof(IGetRow), L"This object is in a zombie state");
ATLTRACE(atlTraceDBProvider, 2, "CCUBRIDRowset::GetRowFromHROW\n");
DBCOUNTITEM iRowset;
{ // check arguments
CCUBRIDRowsetRow *pRow = 0;
if(hRow==NULL || !m_rgRowHandles.Lookup((ULONG)hRow, pRow) || pRow==NULL)
return DB_E_BADROWHANDLE;
if(pRow->m_status==DBPENDINGSTATUS_INVALIDROW
|| pRow->m_status==DBPENDINGSTATUS_DELETED)
return DB_E_DELETEDROW;
iRowset = pRow->m_iRowset;
}
if(!ppUnk)
return E_INVALIDARG;
if(pUnkOuter && !InlineIsEqualUnknown(riid))
return DB_E_NOAGGREGATION;
CComPolyObject<CCUBRIDRow> *pRow;
HRESULT hr = CComPolyObject<CCUBRIDRow>::CreateInstance(pUnkOuter, &pRow);
if(FAILED(hr))
return hr;
// 생성된 COM 객체를 참조해서, 실패시 자동 해제하도록 한다.
CComPtr<IUnknown> spUnk;
hr = pRow->QueryInterface(&spUnk);
if(FAILED(hr))
{
delete pRow; // 참조되지 않았기 때문에 수동으로 지운다.
return hr;
}
// Rowset object의 IUnknown을 Row의 Site로 설정한다.
CComPtr<IUnknown> spOuterUnk;
QueryInterface(__uuidof(IUnknown), (void **)&spOuterUnk);
pRow->m_contained.SetSite(spOuterUnk, CCUBRIDRow::FromRowset);
CComVariant var;
GetPropValue(&DBPROPSET_ROWSET, DBPROP_BOOKMARKS, &var);
hr = pRow->m_contained.Initialize(GetRequestHandle(), V_BOOL(&var)==ATL_VARIANT_TRUE, hRow, iRowset);
if (FAILED(hr))
return E_FAIL;
return pRow->QueryInterface(riid, (void **)ppUnk);
}
STDMETHODIMP CCUBRIDRowset::GetURLFromHROW(HROW hRow, LPOLESTR *ppwszURL)
{
ClearError();
ATLTRACE(atlTraceDBProvider, 2, "CCUBRIDRowset::GetURLFromHROW\n");
return RaiseError(DB_E_NOTSUPPORTED, 0, __uuidof(IGetRow));
}
STDMETHODIMP CCUBRIDRowset::FindNextRow(HCHAPTER hChapter, HACCESSOR hAccessor,
void *pFindValue, DBCOMPAREOP CompareOp, DBBKMARK cbBookmark,
const BYTE *pBookmark, DBROWOFFSET lRowsOffset, DBROWCOUNT cRows,
DBCOUNTITEM *pcRowsObtained, HROW **prghRows)
{
ATLTRACE(atlTraceDBProvider, 2, "CCUBRIDRowset::FindNextRow\n");
ClearError();
if(pcRowsObtained) *pcRowsObtained = 0;
if(m_nStatus==1) return RaiseError(E_UNEXPECTED, 1, __uuidof(IRowsetFind), L"This object is in a zombie state");
CHECK_RESTART(__uuidof(IRowsetFind));
if(pcRowsObtained==NULL || prghRows==NULL)
return E_INVALIDARG;
if(cbBookmark!=0 && pBookmark==NULL)
return E_INVALIDARG;
if(hChapter!=DB_NULL_HCHAPTER)
return DB_E_BADCHAPTER;
CHECK_CANHOLDROWS(__uuidof(IRowsetFind));
// 바인딩 정보를 구함
ATLBINDINGS *pBinding;
{
// 스펙에 DB_E_BADACCESSORHANDLE, DB_E_BADACCESSORTYPE, E_INVALIDARG를
// 반환하는 경우는 정의되어 있지 않지만 해주는게 맞는 것 같다.
bool bFound = m_rgBindings.Lookup((ULONG)hAccessor, pBinding);
if(!bFound || pBinding==NULL)
return DB_E_BADACCESSORHANDLE;
if(!(pBinding->dwAccessorFlags & DBACCESSOR_ROWDATA))
return DB_E_BADACCESSORTYPE; // row accessor 가 아니다.
if(pBinding->cBindings!=1)
return DB_E_BADBINDINFO;
}
// 나중에 체크하지만 LTM을 위해 중복
{
DBCOMPAREOP LocalOp = CompareOp & ~DBCOMPAREOPS_CASESENSITIVE & ~DBCOMPAREOPS_CASEINSENSITIVE;
if(LocalOp<0 || LocalOp>DBCOMPAREOPS_NOTCONTAINS)
return DB_E_BADCOMPAREOP;
if((CompareOp & DBCOMPAREOPS_CASESENSITIVE) && (CompareOp & DBCOMPAREOPS_CASEINSENSITIVE))
return DB_E_BADCOMPAREOP;
}
if(cRows==0 && cbBookmark!=0)
return S_OK;
DBROWOFFSET iDir = 1;
if(cRows<0) { iDir = -1; cRows = -cRows; }
if(cRows==0) { iDir = (m_bFindForward?1:-1); }
m_bFindForward = (iDir==1);
CComHeapPtr<HROW> rghRowsAllocated;
if(*prghRows==NULL)
{
rghRowsAllocated.Allocate(cRows?cRows:1);
if(rghRowsAllocated==NULL)
return E_OUTOFMEMORY;
*prghRows = rghRowsAllocated;
}
DBROWOFFSET iRowsetTemp = -1;
if(cbBookmark!=0)
{
if(!m_bCanScrollBack)
return DB_E_CANTSCROLLBACKWARDS;
HRESULT hr = ValidateBookmark(cbBookmark, pBookmark);
if(FAILED(hr)) return hr;
iRowsetTemp = m_iRowset; // cache the current rowset
// 시작 점을 찾는다.
if(cbBookmark==1)
{
if(*pBookmark==DBBMK_FIRST)
m_iRowset = 1;
else // *pBookmark==DBBMK_LAST
m_iRowset = (DBCOUNTITEM)m_rgRowData.GetCount();
}
else
{
m_iRowset = m_rgBookmarks[*pBookmark];
}
if(iDir==1) m_iRowset--;
}
while(1)
{
DBCOUNTITEM cTmp;
HROW *phRow = &(*prghRows)[0];
m_bExternalFetch = true;
HRESULT hr = GetNextRows(hChapter, lRowsOffset, iDir, &cTmp, &phRow);
m_bExternalFetch = false;
lRowsOffset = 0; // 첫 fetch 때만 lRowsOffset을 적용
if(FAILED(hr) || hr==DB_S_ENDOFROWSET) goto error;
// 조건 검사
bool bMatch = true;
if(CompareOp!=DBCOMPAREOPS_IGNORE)
{
CCUBRIDRowsetRow *pRow;
{
bool bFound = m_rgRowHandles.Lookup((ULONG)*phRow, pRow);
ATLASSERT(bFound && pRow!=NULL);
}
DBBINDING &rBinding = pBinding->pBindings[0];
hr = pRow->Compare(pFindValue, CompareOp, rBinding);
if(hr==S_FALSE)
bMatch = false;
else if(hr!=S_OK)
goto error;
}
if(bMatch)
{
(*pcRowsObtained)++;
break;
}
else
ReleaseRows(1, phRow, NULL, NULL, NULL);
continue;
error:
if(rghRowsAllocated) // 메모리 해제는 자동적으로 이루어진다.
*prghRows = 0;
if(iRowsetTemp!=-1)
m_iRowset = iRowsetTemp;
return hr;
}
if(cRows>1)
{ // fetch last rows
DBCOUNTITEM cTmp;
HROW *phRow = &(*prghRows)[1];
m_bExternalFetch = true;
GetNextRows(hChapter, 0, (cRows-1)*iDir, &cTmp, &phRow); // TODO: return value 검사?
m_bExternalFetch = false;
*pcRowsObtained += cTmp;
}
else if(cRows==0)
{ // handle은 반환하지 않고 fetch position만 변경되는 효과를 낸다.
HROW *phRow = &(*prghRows)[0];
ReleaseRows(1, phRow, NULL, NULL, NULL);
*pcRowsObtained = 0;
if(rghRowsAllocated) // 메모리 해제는 자동적으로 이루어진다.
*prghRows = 0;