forked from CUBRID/cubrid-oledb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataSource.cpp
388 lines (324 loc) · 12.5 KB
/
DataSource.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
/*
* 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 "CUBRIDProvider.h"
#include "DataSource.h"
#include "Error.h"
#include "ProviderInfo.h"
//const OLEDBDECLSPEC GUID DBPROPSET_UNIPROVIDER_DBINIT = {0x7f555b1d,0xc6d2,0x40ce,{0x9a,0xb4,0x49,0x62,0x78,0x1e,0xb6,0x6c}};
//extern "C" {
//char cci_client_name[8] = "ODBC";
//}
CCUBRIDDataSource *CCUBRIDDataSource::GetDataSourcePtr(IObjectWithSite *pSite)
{
CComPtr<IDBCreateSession> spCom;
HRESULT hr = pSite->GetSite(__uuidof(IDBCreateSession), (void **)&spCom);
// 제대로 프로그래밍 됐을때, 실패하는 경우가 있을까?
ATLASSERT(SUCCEEDED(hr));
// 굳이 오버헤드를 감수해가며 dynamic_cast를 쓸 필요는 없을 듯
return static_cast<CCUBRIDDataSource *>((IDBCreateSession *)spCom);
}
STDMETHODIMP CCUBRIDDataSource::Initialize(void)
{
ATLTRACE2(atlTraceDBProvider, 2, "CCUBRIDDataSource::Initialize\n");
// 현재 오류객체를 제거한다.
ClearError();
// 연결정보들(ID, 암호등)이 올바른지 확인한다.
char dbmsver[16];
{
CConnectionProperties props;
Util::GetConnectionProperties((IDBProperties*) this, props);
T_CCI_ERROR error;
int hConn = cci_connect_ex(CW2A(props.strAddr), props.nPort, CW2A(props.strDB), CW2A(props.strUser), CW2A(props.strPass),&error);
if(hConn<0)
{
if(hConn==CCI_ER_NO_MORE_MEMORY)
return E_OUTOFMEMORY;
// wrong hostname
CComVariant var;
var = DBPROPVAL_CS_COMMUNICATIONFAILURE;
SetPropValue(&DBPROPSET_DATASOURCEINFO, DBPROP_CONNECTIONSTATUS, &var);
return RaiseError(E_FAIL, 1, __uuidof(IDBInitialize), CA2W(error.err_msg));
}
char buf[16];
int rc = cci_get_db_version(hConn, buf, sizeof(buf));
if(rc<0)
{
ATLASSERT(rc!=CCI_ER_CON_HANDLE);
// rc==CCI_ER_CONNECT -> 주소나 포트가 틀림
// rc==CAS_ER_DBMS -> DB 이름이나 ID, 암호가 틀림
ATLTRACE2(atlTraceDBProvider, 0, "CCUBRIDDataSource::Initialize : cci_get_db_version failed with rc=%d\n", rc);
cci_disconnect(hConn, &error);
CComVariant var;
var = DBPROPVAL_CS_COMMUNICATIONFAILURE;
SetPropValue(&DBPROPSET_DATASOURCEINFO, DBPROP_CONNECTIONSTATUS, &var);
return RaiseError(DB_SEC_E_AUTH_FAILED, 0, __uuidof(IDBInitialize), (LPWSTR)0, L"42000");
}
rc = cci_get_db_parameter(hConn, CCI_PARAM_MAX_STRING_LENGTH, &PARAM_MAX_STRING_LENGTH, &error);
if (rc < 0)
{
/*
cci_disconnect(hConn, &error);
CComVariant var;
var = DBPROPVAL_CS_COMMUNICATIONFAILURE;
SetPropValue(&DBPROPSET_DATASOURCEINFO, DBPROP_CONNECTIONSTATUS, &var);
return RaiseError(E_FAIL, 1, __uuidof(IDBInitialize), CA2W(error.err_msg));
*/
PARAM_MAX_STRING_LENGTH=1073741823;
}
cci_disconnect(hConn, &error);
switch(props.nCommitLevel)
{
case ISOLATIONLEVEL_READUNCOMMITTED:
case ISOLATIONLEVEL_READCOMMITTED:
case ISOLATIONLEVEL_REPEATABLEREAD:
case ISOLATIONLEVEL_SERIALIZABLE:
break;
default:
return XACT_E_ISOLATIONLEVEL;
}
int a=0, b=0, c=0;
sscanf(buf, "%2d.%2d.%2d", &a, &b, &c);
sprintf(dbmsver, "%02d.%02d.%04d", a, b, c);
}
// ATL의 초기화 루틴을 호출
{
HRESULT hr = IDBInitializeImpl<CCUBRIDDataSource>::Initialize();
if(FAILED(hr)) return hr;
}
// set properties
{
CComVariant var;
// 읽기 전용 속성이므로 IDBProperties::SetProperties를 이용할 수 없다.
// 대신 내부적으로 IDBProperties에 이용된 CUtlProps::SetPropValue를 사용한다.
// 외부적으론 변경할 수 없지만, 내부적으로 변경할 수 있도록
// DBPROPFLAGS_CHANGE flag를 추가한다.
var = dbmsver;
SetPropValue(&DBPROPSET_DATASOURCEINFO, DBPROP_DBMSVER, &var);
VariantClear(&var);
VariantInit(&var);
var = "2.0.01.004";
SetPropValue(&DBPROPSET_DATASOURCEINFO, DBPROP_PROVIDERVER, &var);
GetPropValue(&DBPROPSET_DBINIT, DBPROP_INIT_LOCATION, &var);
SetPropValue(&DBPROPSET_DATASOURCEINFO, DBPROP_DATASOURCENAME, &var);
GetPropValue(&DBPROPSET_DBINIT, DBPROP_SESS_AUTOCOMMITISOLEVELS, &var);
SetPropValue(&DBPROPSET_SESSION, DBPROP_SESS_AUTOCOMMITISOLEVELS, &var);
var = DBPROPVAL_CS_INITIALIZED;
SetPropValue(&DBPROPSET_DATASOURCEINFO, DBPROP_CONNECTIONSTATUS, &var);
}
return S_OK;
}
STDMETHODIMP CCUBRIDDataSource::Uninitialize(void)
{
ATLTRACE2(atlTraceDBProvider, 2, "CCUBRIDDataSource::Uninitialize\n");
CComVariant var;
// 현재 오류객체를 제거한다.
ClearError();
// ATL의 루틴을 호출
{
HRESULT hr = IDBInitializeImpl<CCUBRIDDataSource>::Uninitialize();
if(FAILED(hr)) return hr;
}
var = DBPROPVAL_CS_UNINITIALIZED;
SetPropValue(&DBPROPSET_DATASOURCEINFO, DBPROP_CONNECTIONSTATUS, &var);
return S_OK;
}
STDMETHODIMP CCUBRIDDataSource::CreateSession(IUnknown *pUnkOuter, REFIID riid, IUnknown **ppDBSession)
{
if(ppDBSession==NULL) return E_INVALIDARG;
*ppDBSession = NULL;
// DBPROP_ACTIVESESSIONS 개수 이상의 session을 열 수 없다.
{
CComVariant var;
HRESULT hr = GetPropValue(&DBPROPSET_DATASOURCEINFO, DBPROP_ACTIVESESSIONS, &var);
if(FAILED(hr)) return hr;
ATLASSERT(var.vt==VT_I4);
int cActSessions = V_I4(&var);
if(cActSessions!=0 && this->m_cSessionsOpen>=cActSessions)
return DB_E_OBJECTCREATIONLIMITREACHED;
}
// DBPROP_MULTIPLECONNECTIONS==FALSE면
// 여러 개의 connection handle을 여는 것을 허용하지 않음
/*
{
CComVariant var;
HRESULT hr = GetPropValue(&DBPROPSET_DATASOURCE, DBPROP_MULTIPLECONNECTIONS, &var);
if(FAILED(hr)) return hr;
ATLASSERT(var.vt==VT_BOOL);
bool bMulSessions = V_BOOL(&var);
if(!bMulSessions && this->m_cSessionsOpen!=0)
return DB_E_OBJECTOPEN;
}
*/
HRESULT hr = IDBCreateSessionImpl<CCUBRIDDataSource, CCUBRIDSession>::CreateSession(pUnkOuter, riid, ppDBSession);
if (FAILED(hr)) return hr;
ICUBRIDSession* pCUBRIDSession = NULL;
hr = (*ppDBSession)->QueryInterface(__uuidof(ICUBRIDSession), (void**) &pCUBRIDSession);
if (FAILED(hr))
{
(*ppDBSession)->Release();
return hr;
}
hr = pCUBRIDSession->Connect();
if (FAILED(hr))
{
(*ppDBSession)->Release();
(*ppDBSession)->Release();
return hr;
}
(*ppDBSession)->Release();
return S_OK;
}
STDMETHODIMP CCUBRIDDataSource::GetLiteralInfo(ULONG cLiterals, const DBLITERAL rgLiterals[],
ULONG *pcLiteralInfo, DBLITERALINFO **prgLiteralInfo,
OLECHAR **ppCharBuffer)
{
ATLTRACE(atlTraceDBProvider, 2, _T("CCUBRIDDataSource::GetLiteralInfo\n"));
ObjectLock lock(this);
// 현재 오류객체를 제거한다.
ClearError();
// 초기화
if( pcLiteralInfo )
*pcLiteralInfo = 0;
if( prgLiteralInfo )
*prgLiteralInfo = NULL;
if( ppCharBuffer )
*ppCharBuffer = NULL;
// 파라미터 체크
if (!pcLiteralInfo || !prgLiteralInfo || !ppCharBuffer)
return E_INVALIDARG;
if( cLiterals != 0 && rgLiterals == NULL )
return E_INVALIDARG;
// Data Source가 초기화 되었는지 확인
if (!(m_dwStatus & DSF_INITIALIZED))
return E_UNEXPECTED;
*ppCharBuffer = (WCHAR *)CoTaskMemAlloc(ProvInfo::size_wszAllStrings);
if(*ppCharBuffer==NULL) return E_OUTOFMEMORY;
memcpy(*ppCharBuffer, ProvInfo::wszAllStrings, ProvInfo::size_wszAllStrings);
// 제공되는 모두 literal 정보 수
const UINT numLiteralInfos = ProvInfo::size_LiteralInfos;
// cLiterals가 0이면 모든 literal 정보 리턴
*pcLiteralInfo = ( cLiterals==0 ? numLiteralInfos : cLiterals );
// *pcLiteralInfo 만큼의 DBLITERALINFO 구조체 배열 할당
*prgLiteralInfo = (DBLITERALINFO *)CoTaskMemAlloc(*pcLiteralInfo * sizeof(DBLITERALINFO));
if (!*prgLiteralInfo)
{
::CoTaskMemFree(*ppCharBuffer);
*pcLiteralInfo = 0;
*ppCharBuffer = 0;
return E_OUTOFMEMORY;
}
ULONG ulSucceeded = 0;
if(cLiterals)
{ // 일부 literal 정보만 반환
for(ULONG i=0;i<*pcLiteralInfo;i++)
{
ULONG j;
// 요청된 literal이 LiteralInfos에 있는지 찾아본다.
for(j=0;j<numLiteralInfos;j++)
{
if(ProvInfo::LiteralInfos[j].lt==rgLiterals[i])
{
(*prgLiteralInfo)[i] = ProvInfo::LiteralInfos[j];
ulSucceeded++;
break;
}
}
if(j==numLiteralInfos)
{ // LiteralInfos에 없는 literal
// fSupported도 자동으로 FALSE가 된다.
ZeroMemory((*prgLiteralInfo)+i, sizeof(DBLITERALINFO));
(*prgLiteralInfo)[i].lt = rgLiterals[i];
}
}
}
else
{ // 모든 literal 정보를 반환
for( ; ulSucceeded<numLiteralInfos ; ulSucceeded++ )
(*prgLiteralInfo)[ulSucceeded] = ProvInfo::LiteralInfos[ulSucceeded];
}
if(ulSucceeded==*pcLiteralInfo)
return S_OK;
else if(ulSucceeded!=0)
return DB_S_ERRORSOCCURRED;
else
{
// 스펙에 따라서 string buffer는 free하고, infos buffer는 놔둔다.
::CoTaskMemFree(*ppCharBuffer);
*ppCharBuffer = NULL;
return DB_E_ERRORSOCCURRED;
}
}
STDMETHODIMP CCUBRIDDataSource::GetKeywords(LPOLESTR *ppwszKeywords)
{
ATLTRACE(atlTraceDBProvider, 2, _T("CCUBRIDDataSource::GetKeywords\n"));
ObjectLock lock(this);
// 현재 오류객체를 제거한다.
ClearError();
// check params
if (ppwszKeywords == NULL)
return E_INVALIDARG;
*ppwszKeywords = NULL;
// check if data source object is initialized
if (!(m_dwStatus & DSF_INITIALIZED))
return E_UNEXPECTED;
// 키워드 리스트를 comma를 seperate 문자로 하여 continuous한 배열로 선언
OLECHAR Keywords[] = L"ABORT,ACTIVE,ADD_MONTHS,AFTER,ALIAS,ASYNC,ATTACH,ATTRIBUTE,"
L"BEFORE,BOOLEAN,BREADTH,CALL,CHANGE,CLASS,CLASSES,CLUSTER,"
L"COMMITTED,COMPLETION,COST,CYCLE,DATA,DATA_TYPE___,DECAY_CONSTANT,"
L"DEFINED,DEPTH,DICTIONARY,DIFFERENCE,DIRECTORY,DRAND,EACH,"
L"ELSEIF,EQUALS,EVALUATE,EVENT,EXCLUDE,FILE,FUNCTION,GDB,GENERAL,"
L"GROUPBY_NUM,GROUPS,HOST,IDENTIFIED,IF,IGNORE,INACTIVE,INCREMENT,"
L"INDEX,INFINITE,INHERIT,INOUT,INSTANCES,INST_NUM,"
L"INTERSECTION,INTRINSIC,INVALIDATE,LAST_DAY,LDB,LEAVE,LESS,LIMIT,LIST,"
L"LOCK,LOOP,LPAD,LTRIM,MAXIMUM,MAXVALUE,MAX_ACTIVE,"
L"MEMBERS,METHOD,MINVALUE,MIN_ACTIVE,MODIFY,MOD,MONETARY,"
L"MONTHS_BETWEEN,MULTISET,MULTISET_OF,NA,NAME,"
L"NEW,NOCYCLE,NOMAXVALUE,NOMINVALUE,NONE,OBJECT,OBJECT_ID,OFF,OID,OLD,"
L"OPERATION,OPERATORS,OPTIMIZATION,ORDERBY_NUM,OTHERS,OUT,PARAMETERS,"
L"PASSWORD,PENDANT,PREORDER,PRINT,PRIORITY,PRIVATE,PROXY,PROTECTED,"
L"QUERY,RAND,RECURSIVE,REF,REFERENCING,REGISTER,REJECT,RENAME,REPEATABLE,"
L"REPLACE,RESIGNAL,RETURN,RETURNS,ROLE,ROUTINE,ROW,ROWNUM,"
L"RPAD,RTRIM,SAVEPOINT,SCOPE___,SEARCH,SENSITIVE,SEQUENCE,"
L"SEQUENCE_OF,SERIAL,SERIALIZABLE,SETEQ,SETNEQ,SET_OF,SHARED,SHORT,SIGNAL,"
L"SIMILAR,SQLEXCEPTION,SQLWARNING,STABILITY,START,STATEMENT,STATISTICS,STATUS,"
L"STDDEV,STRING,STRUCTURE,SUBCLASS,SUBSET,SUBSETEQ,SUPERCLASS,"
L"SUPERSET,SUPERSETEQ,SYS_DATE,SYS_TIME,SYS_TIMESTAMP,SYS_USER,"
L"TEST,THERE,TIMEOUT,TO_CHAR,TO_DATE,TO_NUMBER,TO_TIMETO_TIMESTAMP,"
L"TRACE,TRIGGERS,TYPE,UNCOMMITTED,UNDER,USE,UTIME,VARIABLE,VARIANCE,"
L"VCLASS,VIRTUAL,VISIBLE,WAIT,WHILE,WITHOUT";
// 버퍼를 생성한다
*ppwszKeywords = (LPWSTR)CoTaskMemAlloc(sizeof(Keywords));
if( *ppwszKeywords == NULL )
return E_OUTOFMEMORY;
// Copy keywords
wcscpy( *ppwszKeywords, Keywords );
return S_OK;
}