forked from CUBRID/cubrid-oledb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Session.cpp
1350 lines (1158 loc) · 41.8 KB
/
Session.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 "DataSource.h"
#include "Session.h"
#include "Rowset.h"
#include "Row.h"
#include "util.h"
#include "type.h"
#include "ProviderInfo.h"
#include "Error.h"
CCUBRIDDataSource *CCUBRIDSession::GetDataSourcePtr()
{
return CCUBRIDDataSource::GetDataSourcePtr(this);
}
CCUBRIDSession *CCUBRIDSession::GetSessionPtr(IObjectWithSite *pSite)
{
CComPtr<IGetDataSource> spCom;
HRESULT hr = pSite->GetSite(__uuidof(IGetDataSource), (void **)&spCom);
// 제대로 프로그래밍 됐을때, 실패하는 경우가 있을까?
ATLASSERT(SUCCEEDED(hr));
// 굳이 오버헤드를 감수해가며 dynamic_cast를 쓸 필요는 없을 듯
return static_cast<CCUBRIDSession *>((IGetDataSource *)spCom);
}
HRESULT CCUBRIDSession::RegisterTxnCallback(Util::ITxnCallback *pTxnCallback, bool bRegister)
{
ATLASSERT(pTxnCallback);
if(bRegister)
{
ATLASSERT(m_grpTxnCallbacks.Find(pTxnCallback)==NULL); // 이미 있는지 검사한다.
m_grpTxnCallbacks.AddTail(pTxnCallback);
}
else
{
POSITION pos = m_grpTxnCallbacks.Find(pTxnCallback);
if(pos!=NULL) // 없을 수도 있다(생성하다 실패한 경우)
m_grpTxnCallbacks.RemoveAt(pos);
}
return S_OK;
}
int CCUBRIDSession::GetConnection()
{
return m_hConn;
}
UINT CCUBRIDSession::GetCodepage()
{
return m_uCodepage;
}
HRESULT CCUBRIDSession::FinalConstruct()
{
ATLTRACE2(atlTraceDBProvider, 3, "CCUBRIDSession::FinalConstruct\n");
return FInit();
}
void CCUBRIDSession::FinalRelease()
{
ATLTRACE2(atlTraceDBProvider, 3, "CCUBRIDSession::FinalRelease\n");
if(m_hConn>0)
{
T_CCI_ERROR err_buf;
cci_disconnect(m_hConn, &err_buf);
m_hConn = 0;
}
}
STDMETHODIMP CCUBRIDSession::Connect()
{
ATLTRACE2(atlTraceDBProvider, 3, "CCUBRIDSession::FinalConstruct\n");
CConnectionProperties props;
int ret;
CComPtr<IDBProperties> spDBProps;
HRESULT hr = GetDataSource(__uuidof(IDBProperties), (IUnknown**)&spDBProps);
if (FAILED(hr)) return hr;
hr = Util::GetConnectionProperties(spDBProps, props);
if (FAILED(hr)) return hr;
if (m_hConn != 0)
{
RaiseError(DB_E_ALREADYINITIALIZED, 0, IID_NULL, L"Connection already inited");
return DB_E_ALREADYINITIALIZED;
}
m_uCodepage = props.uCodepage;
ATLTRACE2(atlTraceDBProvider, 2, L"Location=%s;DataSource=%s;"
L"UserID=%s;Password=%s;Port=%d;\n", props.strAddr, props.strDB, props.strUser, props.strPass, props.nPort);
int rc = cci_connect(CW2A(props.strAddr), props.nPort, CW2A(props.strDB), CW2A(props.strUser), CW2A(props.strPass));
ATLTRACE2(atlTraceDBProvider, 3, "cci_connect returned %d\n", rc);
if(rc<0)
{
if(rc==CCI_ER_NO_MORE_MEMORY)
return E_OUTOFMEMORY;
// wrong hostname
return DB_SEC_E_AUTH_FAILED;
}
ret = cci_set_autocommit(rc, CCI_AUTOCOMMIT_FALSE);
if(ret!=0)
{
return CCI_ER_CON_HANDLE;
}
m_hConn = rc;
hr= EnterAutoCommitMode(props.nCommitLevel);
if (FAILED(hr)) return DB_SEC_E_AUTH_FAILED;
return S_OK;
}
HRESULT CCUBRIDSession::IsValidValue(ULONG /*iCurSet*/, DBPROP* pDBProp)
{
// 다른 속성은 없다.
ATLASSERT(pDBProp->dwPropertyID==DBPROP_SESS_AUTOCOMMITISOLEVELS);
HRESULT hr = SetCASCCIIsoLevel(V_I4(&pDBProp->vValue), true);
if(FAILED(hr))
return S_FALSE;
return S_OK;
}
HRESULT CCUBRIDSession::OnPropertyChanged(ULONG /*iCurSet*/, DBPROP *pDBProp)
{
// 다른 속성은 없다.
ATLASSERT(pDBProp->dwPropertyID==DBPROP_SESS_AUTOCOMMITISOLEVELS);
if(m_bAutoCommit)
{
HRESULT hr = SetCASCCIIsoLevel(V_I4(&pDBProp->vValue));
if(FAILED(hr))
return DB_E_ERRORSOCCURRED;
}
return S_OK;
}
STDMETHODIMP CCUBRIDSession::OpenRowset(IUnknown *pUnk, DBID *pTID, DBID *pInID, REFIID riid,
ULONG cSets, DBPROPSET rgSets[], IUnknown **ppRowset)
{
ATLTRACE2(atlTraceDBProvider, 2, "CCUBRIDSession::OpenRowset");
HRESULT hr = S_OK, _hr = S_OK;
CComVariant var;
char prepareFlag = 0;
bool bCreateRow = false;
if(ppRowset)
*ppRowset = NULL;
if (pUnk && riid != IID_IUnknown)
return RaiseError(DB_E_NOAGGREGATION, 0, __uuidof(IOpenRowset));
if (pInID != NULL)
return RaiseError(DB_E_NOINDEX, 0, _uuidof(IOpenRowset));
if (!pTID)
return RaiseError(E_INVALIDARG, 0, __uuidof(IOpenRowset));
//NAME만을 지원함
if (pTID->eKind != DBKIND_NAME &&
pTID->eKind != DBKIND_GUID_NAME &&
pTID->eKind != DBKIND_PGUID_NAME)
return RaiseError(DB_E_NOTABLE, 0, __uuidof(IOpenRowset));
//DBPROP_IRow 처리
if (rgSets && InlineIsEqualGUID(rgSets->guidPropertySet, DBPROPSET_ROWSET))
{
for (ULONG i = 0; i < rgSets->cProperties; i++)
{
if(rgSets->rgProperties) // else면 INVALIDARG지만 일단은 패스. 후에 에러 반환
{
if (rgSets->rgProperties[i].dwPropertyID == DBPROP_IRow &&
V_BOOL(&rgSets->rgProperties[i].vValue) == ATL_VARIANT_TRUE )
bCreateRow = true;
// DBPROPSTATUS* status = &rgSets->rgProperties[i].dwStatus;
// DBPROPOPTIONS options = rgSets->rgProperties[i].dwOptions;
// *status = DBPROPSTATUS_OK;
// if (rgSets->rgProperties[i].dwPropertyID == DBPROP_IRow)
// {
// //DBPROP_IRow가 세팅되고 riid가 IID_IRow가 아니면
// //DBPROPSTATUS_CONFLICTING status를 세팅한다.
// if (riid == IID_IRow || riid == IID_IUnknown)
// {
// if (V_BOOL(&rgSets->rgProperties[i].vValue) == ATL_VARIANT_TRUE)
// bCreateRow = true;
// } else
// {
// if (riid == IID_IRowset || riid == IID_IRowsetChange || riid == IID_IRowsetInfo)
// {
// *status = DBPROPSTATUS_CONFLICTING;
// } else
// {
// *status = DBPROPSTATUS_NOTSUPPORTED;
// }
// if (options == DBPROPOPTIONS_REQUIRED)
// hr = DB_E_ERRORSOCCURRED;
// else if (options == DBPROPOPTIONS_OPTIONAL)
// hr = DB_S_ERRORSOCCURRED;
// }
// } else if (rgSets->rgProperties[i].dwPropertyID == DBPROP_ISupportErrorInfo)
// {
// *status = DBPROPSTATUS_NOTSETTABLE;
// if (options == DBPROPOPTIONS_REQUIRED)
// hr = DB_E_ERRORSOCCURRED;
// else if (options == DBPROPOPTIONS_OPTIONAL)
// hr = DB_S_ERRORSOCCURRED;
// else
// *status = DBPROPSTATUS_BADOPTION;
// } else if (rgSets->rgProperties[i].dwPropertyID == DBPROP_IRowSchemaChange ||
// rgSets->rgProperties[i].dwPropertyID == DBPROP_IRowChange ||
// rgSets->rgProperties[i].dwPropertyID == DBPROP_ICreateRow ||
// rgSets->rgProperties[i].dwPropertyID == DBPROP_IGetSourceRow)
// {
// *status = DBPROPSTATUS_NOTSUPPORTED;
// if (options == DBPROPOPTIONS_REQUIRED)
// hr = DB_E_ERRORSOCCURRED;
// else if (options == DBPROPOPTIONS_OPTIONAL)
// hr = DB_S_ERRORSOCCURRED;
// else
// *status = DBPROPSTATUS_BADOPTION;
// }
}
}
//if (FAILED(hr)) return hr;
}
//prepare시 사용할 flag를 결정한다.
//if (Util::RequestedRIIDNeedsOID(riid))
// prepareFlag |= CCI_PREPARE_INCLUDE_OID;
if (Util::RequestedRIIDNeedsUpdatability(riid))
prepareFlag |= (CCI_PREPARE_INCLUDE_OID | CCI_PREPARE_UPDATABLE);
if (cSets > 0 && rgSets)
{
//if (Util::CheckOIDFromProperties(cSets, rgSets))
// prepareFlag |= CCI_PREPARE_INCLUDE_OID;
if (Util::CheckUpdatabilityFromProperties(cSets, rgSets))
prepareFlag |= (CCI_PREPARE_INCLUDE_OID | CCI_PREPARE_UPDATABLE);
}
//prepareFlag = CCI_PREPARE_INCLUDE_OID | CCI_PREPARE_UPDATABLE;
HRESULT originalHRESULT = hr;
if (riid == IID_IRow || bCreateRow)
{
int hReq, cResult;
{
_hr = Util::OpenTable(m_hConn, m_uCodepage, pTID->uName.pwszName, &hReq, &cResult, prepareFlag);
if(FAILED(_hr)) return _hr;
}
hr = _hr;
if(cResult==0) return DB_E_NOTFOUND;
CComPolyObject<CCUBRIDRow> *pRow;
_hr = CComPolyObject<CCUBRIDRow>::CreateInstance(pUnk, &pRow);
if(FAILED(_hr))
return _hr;
// 생성된 COM 객체를 참조해서, 실패시 자동 해제하도록 한다.
CComPtr<IUnknown> spUnk;
_hr = pRow->QueryInterface(&spUnk);
if(FAILED(_hr))
{
delete pRow; // 참조되지 않았기 때문에 수동으로 지운다.
return _hr;
}
// Session object의 IUnknown을 Row의 Site로 설정한다.
CComPtr<IUnknown> spOuterUnk;
QueryInterface(__uuidof(IUnknown), (void **)&spOuterUnk);
pRow->m_contained.SetSite(spOuterUnk, CCUBRIDRow::FromSession);
//Row object initialize
_hr = pRow->m_contained.Initialize(hReq);
if(FAILED(_hr))
{
return _hr;
}
if (cResult > 1) {
if (originalHRESULT != DB_S_ERRORSOCCURRED)
hr = DB_S_NOTSINGLETON;
}
//Selection 결과가 1 이상일 때
if (cResult > 1)
{
if (hr != DB_S_ERRORSOCCURRED)
hr = DB_S_NOTSINGLETON;
}
//생성된 Row 객체의 IRow 인터페이스 반환
_hr = pRow->QueryInterface(riid, (void **)ppRowset);
if(FAILED(_hr))
{
return _hr;
}
} else
{
CComPtr<IUnknown> spRowset;
CCUBRIDRowset* pRowset = 0;
_hr = CreateRowset(pUnk, pTID, pInID, riid, cSets, rgSets,
(ppRowset?&spRowset:NULL), pRowset);
if(FAILED(_hr)) return _hr;
// Rowset Object를 생성하지 않았다.
if(!ppRowset) return _hr;
hr = _hr;
_hr = pRowset->InitFromSession(pTID, prepareFlag);
if(FAILED(_hr)) return _hr;
if (ppRowset) *ppRowset = spRowset.Detach();
}
if (FAILED(originalHRESULT))
return originalHRESULT;
else if (originalHRESULT == DB_S_ERRORSOCCURRED)
return originalHRESULT;
return hr; // CreateRowset이 DB_S_ERRORSOCCURRED를 반환할 수도 있으니까 hr을 써야 한다.
}
// 각 Schema에 대해 지원하는 restriction을 지정한다.
void CCUBRIDSession::SetRestrictions(ULONG cRestrictions, GUID* rguidSchema, ULONG* rgRestrictions)
{
ATLTRACE2(atlTraceDBProvider, 2, "CCUBRIDSession::SetRestrictions\n");
for (ULONG l=0; l<cRestrictions; l++)
{
if (InlineIsEqualGUID(rguidSchema[l], DBSCHEMA_TABLES))
rgRestrictions[l] = 0x0c; // TABLE_NAME | TABLE_TYPE
else if (InlineIsEqualGUID(rguidSchema[l], DBSCHEMA_COLUMNS))
rgRestrictions[l] = 0x0c; // TABLE_NAME | COLUMN_NAME
else if (InlineIsEqualGUID(rguidSchema[l], DBSCHEMA_PROVIDER_TYPES))
rgRestrictions[l] = 0x00; // nothing
else if (InlineIsEqualGUID(rguidSchema[l], DBSCHEMA_TABLE_PRIVILEGES))
rgRestrictions[l] = 0x04; // TABLE_NAME
else if (InlineIsEqualGUID(rguidSchema[l], DBSCHEMA_COLUMN_PRIVILEGES))
rgRestrictions[l] = 0x0c; // TABLE_NAME | COLUMN_NAME
else if (InlineIsEqualGUID(rguidSchema[l], DBSCHEMA_TABLE_CONSTRAINTS))
rgRestrictions[l] = 0x60; // TABLE_NAME | CONSTRAINT_TYPE
else if (InlineIsEqualGUID(rguidSchema[l], DBSCHEMA_TABLES_INFO))
rgRestrictions[l] = 0x0c; // TABLE_NAME | TABLE_TYPE
else if (InlineIsEqualGUID(rguidSchema[l], DBSCHEMA_STATISTICS))
rgRestrictions[l] = 0x04; // TABLE_NAME
else if (InlineIsEqualGUID(rguidSchema[l], DBSCHEMA_INDEXES))
rgRestrictions[l] = 0x14; // TABLE_NAME | INDEX_NAME
else if (InlineIsEqualGUID(rguidSchema[l], DBSCHEMA_VIEW_COLUMN_USAGE))
rgRestrictions[l] = 0x04; // VIEW_NAME
else if (InlineIsEqualGUID(rguidSchema[l], DBSCHEMA_VIEWS))
rgRestrictions[l] = 0x04; // TABLE_NAME
}
}
/* atldb.h의 루틴은 기본 schema rowset 만 지원한다.
* 새로운 schema 삽입이 힘든 구조라 전체를 복사해왔다.
*/
// 각 restriction의 타입을 검사한다.
HRESULT CCUBRIDSession::CheckRestrictions(REFGUID rguidSchema, ULONG cRestrictions,
const VARIANT rgRestrictions[])
{
// Use this function to help check the validity of restrictions
// against a schema rowset.
const ULONG ulSchemaRowset = 11;
const GUID *rgGUIDSchema[ulSchemaRowset] = {
&DBSCHEMA_TABLES,
&DBSCHEMA_COLUMNS,
&DBSCHEMA_PROVIDER_TYPES,
&DBSCHEMA_TABLE_PRIVILEGES,
&DBSCHEMA_COLUMN_PRIVILEGES,
&DBSCHEMA_TABLE_CONSTRAINTS,
&DBSCHEMA_TABLES_INFO,
&DBSCHEMA_STATISTICS,
&DBSCHEMA_INDEXES,
&DBSCHEMA_VIEW_COLUMN_USAGE,
&DBSCHEMA_VIEWS,
};
const VARTYPE rgRestrictionTypes[ulSchemaRowset][7] = {
{ VT_BSTR, VT_BSTR, VT_BSTR, VT_BSTR, VT_EMPTY, VT_EMPTY, VT_EMPTY }, // DBSCHEMA_TABLES
{ VT_BSTR, VT_BSTR, VT_BSTR, VT_BSTR, VT_EMPTY, VT_EMPTY, VT_EMPTY }, // DBSCHEMA_COLUMNS
{ VT_UI2, VT_BOOL, VT_EMPTY, VT_EMPTY, VT_EMPTY, VT_EMPTY, VT_EMPTY }, // DBSCHEMA_PROVIDER_TYPES
{ VT_BSTR, VT_BSTR, VT_BSTR, VT_BSTR, VT_BSTR, VT_EMPTY, VT_EMPTY }, // DBSCHEMA_TABLE_PRIVILEGES
{ VT_BSTR, VT_BSTR, VT_BSTR, VT_BSTR, VT_BSTR, VT_BSTR, VT_EMPTY }, // DBSCHEMA_COLUMN_PRIVILEGES
{ VT_BSTR, VT_BSTR, VT_BSTR, VT_BSTR, VT_BSTR, VT_BSTR, VT_BSTR }, // DBSCHEMA_TABLE_CONSTRAINTS
{ VT_BSTR, VT_BSTR, VT_BSTR, VT_BSTR, VT_EMPTY, VT_EMPTY, VT_EMPTY }, // DBSCHEMA_TABLES_INFO
{ VT_BSTR, VT_BSTR, VT_BSTR, VT_EMPTY, VT_EMPTY, VT_EMPTY, VT_EMPTY }, // DBSCHEMA_STATISTICS
{ VT_BSTR, VT_BSTR, VT_BSTR, VT_BSTR, VT_BSTR, VT_EMPTY, VT_EMPTY }, // DBSCHEMA_INDEXES
{ VT_BSTR, VT_BSTR, VT_BSTR, VT_EMPTY, VT_EMPTY, VT_EMPTY, VT_EMPTY }, // DBSCHEMA_VIEW_COLUMN_USAGE
{ VT_BSTR, VT_BSTR, VT_BSTR, VT_EMPTY, VT_EMPTY, VT_EMPTY, VT_EMPTY }, // DBSCHEMA_VIEWS
};
const ULONG ulMaxRestrictions[ulSchemaRowset] = { 4, 4, 2, 5, 6, 7, 4, 3, 5, 3, 3 };
ULONG ulType;
for(ulType=0;ulType<ulSchemaRowset;ulType++)
{
if(InlineIsEqualGUID(rguidSchema, *rgGUIDSchema[ulType]))
break;
}
if (ulType < ulSchemaRowset) // I.E. we found one of our supported rowsets
{
ULONG ulCurrentRestrictions = 0x00;
ULONG ulCurrentMask = 0x01;
// Ask the provider's session object for its list of restrictions
SetRestrictions(1, (GUID*)&rguidSchema, &ulCurrentRestrictions);
// We allow VT_EMPTY through in case the consumer wanted to ignore this
// restriction (basically a way to pass in 'NULL').
if (cRestrictions > ulMaxRestrictions[ulType])
return E_INVALIDARG;
for (ULONG ulRes = 0; ulRes < cRestrictions; ulRes++)
{
// Check for obviously invalid types
if (rgRestrictions[ulRes].vt != rgRestrictionTypes[ulType][ulRes] &&
rgRestrictions[ulRes].vt != DBTYPE_EMPTY &&
rgRestrictions[ulRes].vt != VT_NULL )
return E_INVALIDARG;
// Check for restrictions the provider doesn't support.
if (!(ulCurrentMask & ulCurrentRestrictions) &&
(rgRestrictions[ulRes].vt != DBTYPE_EMPTY))
return DB_E_NOTSUPPORTED;
ulCurrentMask <<= 1; // Increase mask by * 2;
}
}
return S_OK;
}
STDMETHODIMP CCUBRIDSession::CreateTable(IUnknown *pUnkOuter,
DBID *pTableID,
DBORDINAL cColumnDescs,
const DBCOLUMNDESC rgColumnDescs[],
REFIID riid,
ULONG cPropertySets,
DBPROPSET rgPropertySets[],
DBID **ppTableID,
IUnknown **ppRowset)
{
HRESULT hr = S_OK;
CComBSTR tableName, temp_table_name;
CComBSTR strQuery;
int hReq, rc, len;
T_CCI_ERROR error;
if (ppTableID)
*ppTableID = NULL;
ClearError();
if ((!pTableID && !ppTableID) ||
(!cColumnDescs) || //컬럼 없는 테이블 지원안함
(!rgColumnDescs) ||
(cPropertySets && !rgPropertySets))
return E_INVALIDARG;
if (pUnkOuter && riid != IID_IUnknown)
return DB_E_NOAGGREGATION;
//테이블 프로퍼티 관련
for (ULONG i = 0; i < cPropertySets; i++)
{
if (rgPropertySets[i].guidPropertySet == DBPROPSET_ROWSET)
{
if (rgPropertySets[i].cProperties && !rgPropertySets[i].rgProperties)
return E_INVALIDARG;
for (ULONG j = 0; j < rgPropertySets[i].cProperties; j++)
{
DBPROPOPTIONS option = rgPropertySets[i].rgProperties[j].dwOptions;
DBPROPID propID = rgPropertySets[i].rgProperties[j].dwPropertyID;
DBPROPSTATUS* status = &rgPropertySets[i].rgProperties[j].dwStatus;
VARIANT value = rgPropertySets[i].rgProperties[j].vValue;
if (propID == DBPROP_MAYWRITECOLUMN)
{
*status = DBPROPSTATUS_NOTSUPPORTED;
if (option == DBPROPOPTIONS_REQUIRED)
return DB_E_ERRORSOCCURRED;
else
hr = DB_S_ERRORSOCCURRED;
}
}
}
}
if (pTableID)
{
if (IsValidDBID(pTableID) != S_OK)
return DB_E_BADTABLEID;
if (CompareDBIDs(pTableID, &DB_NULLID) == S_OK)
return DB_E_BADTABLEID;
if (pTableID->eKind != DBKIND_NAME)
return DB_E_BADTABLEID;
if (!pTableID->uName.pwszName || !wcslen(pTableID->uName.pwszName))
return DB_E_BADTABLEID;
//quoted table name인 경우 quotation을 벗긴다.
temp_table_name.Append(pTableID->uName.pwszName);
len = temp_table_name.Length();
if (temp_table_name.m_str[0] == '\"' && temp_table_name.m_str[len - 1] == '\"')
{
temp_table_name.Empty();
temp_table_name.Append(pTableID->uName.pwszName + 1);
temp_table_name.m_str[len - 2] = '\0';
}
//Maximum Table Length를 넘는지 체크
if (wcslen(temp_table_name) > ProvInfo::LiteralInfos[8].cchMaxLen)
return DB_E_BADTABLEID;
//Invalid character를 포함하는지 체크
if (wcspbrk(temp_table_name, ProvInfo::LiteralInfos[8].pwszInvalidChars))
return DB_E_BADTABLEID;
//테이블 이름 중복검사
HRESULT hr = Util::DoesTableExist(m_hConn, CW2A(temp_table_name, m_uCodepage));
if(FAILED(hr))
return hr;
if(hr==S_OK)
return DB_E_DUPLICATETABLEID;
tableName = pTableID->uName.pwszName;
}
else
{
CComBSTR strTableName;
Util::GetUniqueTableName(strTableName);
tableName = strTableName;
}
//SQL문 생성 시작
strQuery.Append(L"CREATE TABLE ");
strQuery.Append(tableName);
strQuery.Append(L" (");
//cColumnDescs 갯수 만큼 컬럼에 대한 SQL 문을 생성하고 이를 Query string에 이어 붙인다.
for (DBORDINAL i = 0; i < cColumnDescs; i++)
{
CComBSTR queryLine;
CComBSTR typeDef;
HRESULT _hr = S_OK;
if (IsValidDBID(&rgColumnDescs[i].dbcid) != S_OK)
return DB_E_BADCOLUMNID;
if (CompareDBIDs(&rgColumnDescs[i].dbcid, &DB_NULLID) == S_OK)
return DB_E_BADCOLUMNID;
if (rgColumnDescs[i].dbcid.eKind != DBKIND_NAME)
return DB_E_BADCOLUMNID;
if (rgColumnDescs[i].dbcid.eKind == DBKIND_NAME &&
(!rgColumnDescs[i].dbcid.uName.pwszName || !wcslen(rgColumnDescs[i].dbcid.uName.pwszName)))
return DB_E_BADCOLUMNID;
//Maximum Column Length를 넘는지 체크
if (wcslen(rgColumnDescs[i].dbcid.uName.pwszName) > ProvInfo::LiteralInfos[2].cchMaxLen)
return DB_E_BADCOLUMNID;
//Invalid character를 포함하는지 체크
if (wcspbrk(rgColumnDescs[i].dbcid.uName.pwszName, ProvInfo::LiteralInfos[2].pwszInvalidChars))
return DB_E_BADCOLUMNID;
//컬럼 이름 중복 체크
for (DBORDINAL j = i+1; j < cColumnDescs; j++)
{
if (rgColumnDescs[j].dbcid.uName.pwszName &&
!wcscmp(rgColumnDescs[i].dbcid.uName.pwszName, rgColumnDescs[j].dbcid.uName.pwszName))
return DB_E_DUPLICATECOLUMNID;
}
queryLine.Append(rgColumnDescs[i].dbcid.uName.pwszName);
queryLine.Append(L" ");
_hr = Type::GetColumnDefinitionString(rgColumnDescs[i], typeDef);
if (FAILED(_hr))
return _hr;
if (_hr != S_OK)
hr = _hr;
queryLine.Append(typeDef);
strQuery.Append(queryLine);
if (i+1 != cColumnDescs)
strQuery.Append(L",");
}
//SQL문 생성 종료
strQuery.Append(L")");
char flag = 0;
if (Util::RequestedRIIDNeedsUpdatability(riid))
flag |= (CCI_PREPARE_INCLUDE_OID | CCI_PREPARE_UPDATABLE);
for (ULONG i = 0; i < cPropertySets; i++)
{
if (rgPropertySets[i].guidPropertySet == DBPROPSET_ROWSET)
{
ULONG cProp = rgPropertySets[i].cProperties;
for (ULONG j = 0; j < cProp; j++)
{
DBPROPID propID = rgPropertySets[i].rgProperties[j].dwPropertyID;
if (propID == DBPROP_IRowsetChange ||
propID == DBPROP_IRowsetUpdate ||
propID == DBPROP_IRow ||
propID == DBPROP_IGetRow ||
propID == DBPROP_IGetSession ||
propID == DBPROP_OTHERUPDATEDELETE)
{
flag |= (CCI_PREPARE_INCLUDE_OID | CCI_PREPARE_UPDATABLE);
break;
}
}
}
}
hReq = cci_prepare(m_hConn, CW2A(strQuery, m_uCodepage), flag, &error);
if (hReq < 0)
{
ATLTRACE2(atlTraceDBProvider, 3, "%s\n", error.err_msg);
return RaiseError(E_FAIL, 1, __uuidof(ITableDefinition), CA2W(error.err_msg, m_uCodepage));
}
rc = cci_execute(hReq, CCI_EXEC_QUERY_ALL, 0, &error);
if (rc < 0)
{
ATLTRACE2(atlTraceDBProvider, 3, "%s\n", error.err_msg);
return RaiseError(E_FAIL, 1, __uuidof(ITableDefinition), CA2W(error.err_msg, m_uCodepage));
}
AutoCommit(0);
DBID tmp;
tmp.eKind = DBKIND_NAME;
tmp.uName.pwszName = tableName;
//ppRowset이 NULL이 아닌 경우 Rowset을 연다.
if (ppRowset)
{
HRESULT _hr = OpenRowset(pUnkOuter, &tmp, NULL, riid, cPropertySets, rgPropertySets, ppRowset);
if (FAILED(_hr))
{
return _hr;
}
//위에서 DB_S_ERRORSOCCURRED등이 발생하지 않았었으면 hr을 다시 세팅한다.
if (hr == S_OK) hr = _hr;
}
//ppTableID에 생성된 테이블 이름을 넘겨준다.
if(ppTableID)
{
*ppTableID = (DBID *)CoTaskMemAlloc(sizeof(DBID));
CopyDBIDs(*ppTableID, &tmp);
}
return hr;
}
STDMETHODIMP CCUBRIDSession::DropTable(DBID *pTableID)
{
HRESULT hr = S_OK;
int hReq, rc, len;
CComBSTR strQuery = NULL;
CComBSTR table_name;
T_CCI_ERROR error;
CDBIDOps op;
ClearError();
if (!pTableID)
return E_INVALIDARG;
if (op.IsValidDBID(pTableID) != S_OK)
return DB_E_NOTABLE;
if (op.CompareDBIDs(pTableID, &DB_NULLID) == S_OK)
return DB_E_NOTABLE;
if (pTableID->eKind != DBKIND_NAME)
return DB_E_NOTABLE;
if (!pTableID->uName.pwszName || !wcslen(pTableID->uName.pwszName))
return DB_E_NOTABLE;
//quoted table name인 경우 quotation을 삭제한다.
table_name.Append(pTableID->uName.pwszName);
len = table_name.Length();
if (table_name.m_str[0] == '\"' && table_name.m_str[len - 1] == '\"')
{
table_name.Empty();
table_name.Append(pTableID->uName.pwszName + 1);
table_name.m_str[len - 2] = '\0';
}
//Maximum Table Length를 넘는지 체크
if (wcslen(table_name) > ProvInfo::LiteralInfos[8].cchMaxLen)
return DB_E_NOTABLE;
//Invalid character를 포함하는지 체크
if (wcspbrk(table_name, ProvInfo::LiteralInfos[8].pwszInvalidChars))
return DB_E_NOTABLE;
//테이블 이름이 있는지 검사
hr = Util::DoesTableExist(m_hConn, CW2A(table_name, m_uCodepage));
if(FAILED(hr))
return hr;
if(hr==S_FALSE)
return DB_E_NOTABLE;
strQuery.Append(L"DROP TABLE ");
strQuery.Append(pTableID->uName.pwszName);
hReq = cci_prepare(m_hConn, CW2A(strQuery, m_uCodepage), 0, &error);
if (hReq < 0)
{
return RaiseError(E_FAIL, 1, __uuidof(ITableDefinition), CA2W(error.err_msg, m_uCodepage));
}
rc = cci_execute(hReq, CCI_EXEC_QUERY_ALL, 0, &error);
if (rc < 0)
{
return RaiseError(E_FAIL, 1, __uuidof(ITableDefinition), CA2W(error.err_msg, m_uCodepage));
}
AutoCommit(0);
return hr;
}
STDMETHODIMP CCUBRIDSession::AddColumn(DBID *pTableID, DBCOLUMNDESC *pColumnDesc, DBID **ppColumnID)
{
HRESULT hr = S_OK;
int hReq, rc, len;
CComBSTR strQuery, colDef, table_name, column_name;
T_CCI_ERROR error;
ClearError();
if ((!pTableID || !pColumnDesc) ||
(pColumnDesc->cPropertySets > 0 && !pColumnDesc->rgPropertySets))
return E_INVALIDARG;
if (IsValidDBID(pTableID) != S_OK)
return DB_E_NOTABLE;
if (CompareDBIDs(pTableID, &DB_NULLID) == S_OK)
return DB_E_NOTABLE;
if (pTableID->eKind != DBKIND_NAME)
return DB_E_NOTABLE;
if (!pTableID->uName.pwszName || !wcslen(pTableID->uName.pwszName))
return DB_E_NOTABLE;
//quoted table name인 경우 quotation을 삭제한다.
table_name.Append(pTableID->uName.pwszName);
len = table_name.Length();
if (table_name.m_str[0] == '\"' && table_name.m_str[len - 1] == '\"')
{
table_name.Empty();
table_name.Append(pTableID->uName.pwszName + 1);
table_name.m_str[len - 2] = '\0';
}
//Maximum Table Length를 넘는지 체크
if (wcslen(table_name) > ProvInfo::LiteralInfos[8].cchMaxLen)
return DB_E_NOTABLE;
//Invalid character를 포함하는지 체크
if (wcspbrk(table_name, ProvInfo::LiteralInfos[8].pwszInvalidChars))
return DB_E_NOTABLE;
{
//테이블 이름이 있는지 검사
hReq = cci_schema_info(m_hConn, CCI_SCH_CLASS, CW2A(table_name, m_uCodepage),
NULL, 0, &error);
if(hReq<0)
{
ATLTRACE2("cci_schema_info fail\n");
return RaiseError(E_FAIL, 1, __uuidof(ITableDefinition), CA2W(error.err_msg, m_uCodepage));
}
rc = cci_cursor(hReq, 1, CCI_CURSOR_FIRST, &error);
if(rc==CCI_ER_NO_MORE_DATA)
return DB_E_NOTABLE;
else if (rc<0) return RaiseError(E_FAIL, 1, __uuidof(ITableDefinition), CA2W(error.err_msg, m_uCodepage));
cci_close_req_handle(hReq);
//컬럼 이름이 있는지 검사
hReq = cci_schema_info(m_hConn, CCI_SCH_ATTRIBUTE, CW2A(table_name, m_uCodepage),
CW2A(pColumnDesc->dbcid.uName.pwszName, m_uCodepage), 0, &error);
if(hReq<0)
{
ATLTRACE2("cci_schema_info fail\n");
return RaiseError(E_FAIL, 1, __uuidof(ITableDefinition), CA2W(error.err_msg, m_uCodepage));
}
rc = cci_cursor(hReq, 1, CCI_CURSOR_FIRST, &error);
if(rc >= 0) //modified 2003.07.09
return DB_E_DUPLICATECOLUMNID;
cci_close_req_handle(hReq);
}
strQuery.Append(L"ALTER ");
strQuery.Append(pTableID->uName.pwszName);
strQuery.Append(L" ADD ATTRIBUTE ");
if (IsValidDBID(&pColumnDesc->dbcid) != S_OK)
return DB_E_BADCOLUMNID;
if (CompareDBIDs(&pColumnDesc->dbcid, &DB_NULLID) == S_OK)
return DB_E_BADCOLUMNID;
if (pColumnDesc->dbcid.eKind != DBKIND_NAME)
return DB_E_BADCOLUMNID;
if (!pColumnDesc->dbcid.uName.pwszName || !wcslen(pColumnDesc->dbcid.uName.pwszName))
return DB_E_BADCOLUMNID;
column_name.Append(pColumnDesc->dbcid.uName.pwszName);
//Maximum Column Length를 넘는지 체크
if (wcslen(pColumnDesc->dbcid.uName.pwszName) > ProvInfo::LiteralInfos[2].cchMaxLen)
return DB_E_BADCOLUMNID;
//Invalid character를 포함하는지 체크
if (wcspbrk(pColumnDesc->dbcid.uName.pwszName, ProvInfo::LiteralInfos[2].pwszInvalidChars))
return DB_E_BADCOLUMNID;
strQuery.Append(column_name);
strQuery.Append(L" ");
hr = Type::GetColumnDefinitionString(*pColumnDesc, colDef);
if (FAILED(hr))
return hr;
strQuery.Append(colDef);
hReq = cci_prepare(m_hConn, CW2A(strQuery, m_uCodepage), 0, &error);
if (hReq < 0)
{
return RaiseError(E_FAIL, 1, __uuidof(ITableDefinition), CA2W(error.err_msg, m_uCodepage));
}
rc = cci_execute(hReq, 0, 0, &error);
if (rc < 0)
{
return RaiseError(E_FAIL, 1, __uuidof(ITableDefinition), CA2W(error.err_msg, m_uCodepage));
}
AutoCommit(0);
//ppColumnID에 생성된컬럼 이름을 넘겨준다.
if(ppColumnID)
{
*ppColumnID = (DBID *)CoTaskMemAlloc(sizeof(DBID));
DBID colID;
colID.eKind = DBKIND_NAME;
colID.uName.pwszName = column_name;
CopyDBIDs(*ppColumnID, &colID);
}
return hr;
}
STDMETHODIMP CCUBRIDSession::DropColumn(DBID *pTableID, DBID *pColumnID)
{
HRESULT hr = S_OK;
int hReq, rc, len;
CComBSTR strQuery = NULL, table_name;
T_CCI_ERROR error;
CDBIDOps op;
ClearError();
if (!pTableID || !pColumnID)
return E_INVALIDARG;
if (op.IsValidDBID(pTableID) != S_OK)
return DB_E_NOTABLE;
if (op.CompareDBIDs(pTableID, &DB_NULLID) == S_OK)
return DB_E_NOTABLE;
if (pTableID->eKind != DBKIND_NAME)
return DB_E_NOTABLE;
if (!pTableID->uName.pwszName || !wcslen(pTableID->uName.pwszName))
return DB_E_NOTABLE;
if (op.IsValidDBID(pColumnID) != S_OK)
return DB_E_NOCOLUMN;
if (op.CompareDBIDs(pColumnID, &DB_NULLID) == S_OK)
return DB_E_NOCOLUMN;
if (pColumnID->eKind != DBKIND_NAME)
return DB_E_NOCOLUMN;
if (!pColumnID->uName.pwszName || !wcslen(pColumnID->uName.pwszName))
return DB_E_NOCOLUMN;
//quoted table name인 경우 quotation을 삭제한다.
table_name.Append(pTableID->uName.pwszName);
len = table_name.Length();
if (table_name.m_str[0] == '\"' && table_name.m_str[len - 1] == '\"')
{
table_name.Empty();
table_name.Append(pTableID->uName.pwszName + 1);
table_name.m_str[len - 2] = '\0';
}
//Maximum Table Length를 넘는지 체크
if (wcslen(table_name) > ProvInfo::LiteralInfos[8].cchMaxLen)
return DB_E_NOTABLE;
//Table이 Invalid character를 포함하는지 체크
if (wcspbrk(table_name, ProvInfo::LiteralInfos[8].pwszInvalidChars))
return DB_E_NOTABLE;
//Maximum Column Length를 넘는지 체크
if (wcslen(pColumnID->uName.pwszName) > ProvInfo::LiteralInfos[2].cchMaxLen)
return DB_E_NOCOLUMN;
//Column이 Invalid character를 포함하는지 체크
if (wcspbrk(pColumnID->uName.pwszName, ProvInfo::LiteralInfos[2].pwszInvalidChars))
return DB_E_NOCOLUMN;
//테이블 이름이 있는지 검사
hReq = cci_schema_info(m_hConn, CCI_SCH_CLASS, CW2A(table_name, m_uCodepage),
NULL, CCI_CLASS_NAME_PATTERN_MATCH, &error);
if(hReq<0)
{
ATLTRACE2("cci_schema_info fail\n");
return RaiseError(E_FAIL, 1, __uuidof(ITableDefinition), CA2W(error.err_msg, m_uCodepage));
}
rc = cci_cursor(hReq, 1, CCI_CURSOR_FIRST, &error);
if(rc==CCI_ER_NO_MORE_DATA)
return DB_E_NOTABLE;
else if (rc<0) return RaiseError(E_FAIL, 1, __uuidof(ITableDefinition), CA2W(error.err_msg, m_uCodepage));
cci_close_req_handle(hReq);
hReq = cci_schema_info(m_hConn, CCI_SCH_ATTRIBUTE, CW2A(table_name, m_uCodepage),
CW2A(pColumnID->uName.pwszName, m_uCodepage), 0, &error);
if(hReq<0)
{
ATLTRACE2("cci_schema_info fail\n");
return E_FAIL;
}
rc = cci_cursor(hReq, 1, CCI_CURSOR_FIRST, &error);
if(rc==CCI_ER_NO_MORE_DATA)
return DB_E_NOCOLUMN;
else if (rc<0) return RaiseError(E_FAIL, 1, __uuidof(ITableDefinition), CA2W(error.err_msg, m_uCodepage));
cci_close_req_handle(hReq);
strQuery.Append(L"ALTER ");
strQuery.Append(pTableID->uName.pwszName);
strQuery.Append(L" DROP ");
strQuery.Append(pColumnID->uName.pwszName);
hReq = cci_prepare(m_hConn, CW2A(strQuery, m_uCodepage), 0, &error);
if (hReq < 0)
{
return RaiseError(E_FAIL, 1, __uuidof(ITableDefinition), CA2W(error.err_msg, m_uCodepage));
}
rc = cci_execute(hReq, CCI_EXEC_QUERY_ALL, 0, &error);
if (rc < 0)
{
return RaiseError(E_FAIL, 1, __uuidof(ITableDefinition), CA2W(error.err_msg, m_uCodepage));
}
AutoCommit(0);
return hr;
}
STDMETHODIMP CCUBRIDSession::CreateIndex(
/* [in] */ DBID *pTableID,
/* [in] */ DBID *pIndexID,
/* [in] */ DBORDINAL cIndexColumnDescs,
/* [size_is][in] */ const DBINDEXCOLUMNDESC rgIndexColumnDescs[],
/* [in] */ ULONG cPropertySets,