forked from CUBRID/cubrid-oledb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultipleResult.cpp
296 lines (254 loc) · 7.85 KB
/
MultipleResult.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
/*
* 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.
*
*/
// MultipleResult.cpp : Implementation of CMultipleResult
#include "stdafx.h"
#include "MultipleResult.h"
#include "Rowset.h"
#include "Session.h"
#include "Row.h"
#include "Error.h"
CCUBRIDSession *CMultipleResult::GetSessionPtr()
{
return GetCommandPtr()->GetSessionPtr();
}
CCUBRIDCommand *CMultipleResult::GetCommandPtr()
{
return m_command;
}
CMultipleResult::~CMultipleResult()
{
ATLTRACE2(atlTraceDBProvider, 2, _T("CMultipleResult::~CMultipleResult()\n"));
this->GetSessionPtr()->AutoCommit(NULL);
if(m_spUnkSite)
GetSessionPtr()->RegisterTxnCallback(this, false);
}
HRESULT CMultipleResult::SetSite(IUnknown *pUnkSite)
{
HRESULT hr = IObjectWithSiteImpl<CMultipleResult>::SetSite(pUnkSite);
GetSessionPtr()->RegisterTxnCallback(this, true);
return hr;
}
void CMultipleResult::TxnCallback(const ITxnCallback *pOwner)
{
if(pOwner!=this)
{
cci_close_req_handle(m_hReq);
m_hReq = 0;
m_bInvalidated = true;
}
}
HRESULT CMultipleResult::GetResult(IUnknown *pUnkOuter, DBRESULTFLAG lResultFlag,
REFIID riid, DBROWCOUNT *pcRowsAffected, IUnknown **ppRowset)
{
ATLTRACE2(atlTraceDBProvider, 2, _T("CMultipleResult::GetResult\n"));
HRESULT hr = S_OK;
CCUBRIDCommand* cmd = NULL;
CCUBRIDRowset* pRowset = NULL;
int result_count = 0, rc;
T_CCI_CUBRID_STMT cmd_type;
T_CCI_ERROR error;
ClearError();
error.err_msg[0] = 0;
//E_INVALIDARG 처리
if (!(lResultFlag == DBRESULTFLAG_DEFAULT || lResultFlag == DBRESULTFLAG_ROWSET
|| lResultFlag == DBRESULTFLAG_ROW))
{
hr = E_INVALIDARG;
goto error;
}
//E_NOINTERFACE 처리
if (lResultFlag == DBRESULTFLAG_ROWSET)
{
if (riid == IID_IRow)
hr = E_NOINTERFACE;
} else if (lResultFlag == DBRESULTFLAG_ROW)
{
if (riid == IID_IRowset)
hr = E_NOINTERFACE;
}
if ( riid == IID_IRowsetUpdate )
hr = E_NOINTERFACE;
else if ( riid == IID_IMultipleResults)
hr = E_NOINTERFACE;
if (hr == E_NOINTERFACE)
goto error;
if (pUnkOuter && riid != IID_IUnknown)
{
hr = DB_E_NOAGGREGATION;
goto error;
}
if (pcRowsAffected)
*pcRowsAffected = DB_COUNTUNAVAILABLE;
//모든 쿼리가 다 수행되었으면 m_qr 해제 후 DB_S_NORESULT 리턴
if (m_resultIndex > m_numQuery)
{
ATLTRACE2(atlTraceDBProvider, 2, _T("DB_S_NORESULT\n"));
cci_query_result_free(m_qr, m_numQuery);
m_qr = NULL;
if (ppRowset)
*ppRowset = NULL;
if (pcRowsAffected)
*pcRowsAffected = DB_COUNTUNAVAILABLE;
return DB_S_NORESULT;
}
if (m_bInvalidated)
{
hr = E_UNEXPECTED;
goto error;
}
//Rowset이 열려있는 경우 DB_E_OBJECTOPEN을 리턴
if (m_command->m_cRowsetsOpen > 0)
{
hr = DB_E_OBJECTOPEN;
goto error;
}
//다음번 결과셋을 가져온다
if (m_resultIndex != 1 && !m_bCommitted)
{
rc = cci_next_result(m_hReq, &error);
if (rc < 0)
{
hr = E_FAIL;
goto error;
}
}
result_count = CCI_QUERY_RESULT_RESULT(m_qr, m_resultIndex);
cmd_type = (T_CCI_CUBRID_STMT)CCI_QUERY_RESULT_STMT_TYPE(m_qr, m_resultIndex);
//쿼리 인덱스 증가
m_resultIndex++;
//CCUBRIDCommand 객체에 대한 레퍼런스를 받아옴
cmd = m_command;
if (riid == IID_IRow)
{
if (ppRowset && (cmd_type==CUBRID_STMT_SELECT ||
cmd_type==CUBRID_STMT_GET_STATS ||
cmd_type==CUBRID_STMT_CALL ||
cmd_type==CUBRID_STMT_EVALUATE))
{
CComPolyObject<CCUBRIDRow> *pRow;
HRESULT hr = CComPolyObject<CCUBRIDRow>::CreateInstance(pUnkOuter, &pRow);
if(FAILED(hr)) goto error;
// 생성된 COM 객체를 참조해서, 실패시 자동 해제하도록 한다.
CComPtr<IUnknown> spUnk;
hr = pRow->QueryInterface(&spUnk);
if(FAILED(hr))
{
delete pRow; // 참조되지 않았기 때문에 수동으로 지운다.
goto error;
}
//Command object의 IUnknown을 Row의 Site로 설정한다.
CComPtr<IUnknown> spOuterUnk;
GetCommandPtr()->QueryInterface(__uuidof(IUnknown), (void **)&spOuterUnk);
hr = pRow->m_contained.SetSite(spOuterUnk, CCUBRIDRow::FromCommand);
if(FAILED(hr)) goto error;
hr = pRow->m_contained.Initialize(m_hReq);
if (FAILED(hr))
{
if (ppRowset)
*ppRowset = NULL;
if (pcRowsAffected)
*pcRowsAffected = DB_COUNTUNAVAILABLE;
return RaiseError(hr, 0, __uuidof(IMultipleResults));
}
//생성된 Row 객체의 IRow 인터페이스 반환
hr = pRow->QueryInterface(riid, (void **)ppRowset);
if(FAILED(hr)) goto error;
//if (result_count > 1)
// return DB_S_NOTSINGLETON;
} else
{
if (cmd_type == CUBRID_STMT_SELECT)
{
if (pcRowsAffected)
*pcRowsAffected = -1;
}
else
{
if (pcRowsAffected)
*pcRowsAffected = result_count;
}
if (m_resultIndex > m_numQuery) //마지막 쿼리인 경우
{
GetSessionPtr()->AutoCommit(this);
m_hReq = 0;
m_bCommitted = true;
}
if (ppRowset != NULL)
*ppRowset = NULL;
}
}
//IID_IRowset이 아닌 경우 Rowset을 생성
//IID_IRow 처럼 다르게 처리해 주어야 하는 경우가 또 있을까?
else
{
if(cmd_type==CUBRID_STMT_SELECT ||
cmd_type==CUBRID_STMT_GET_STATS ||
cmd_type==CUBRID_STMT_CALL ||
cmd_type==CUBRID_STMT_EVALUATE)
{
if (ppRowset)
{
if (riid != IID_NULL)
{
ATLTRACE2(atlTraceDBProvider, 2, _T("CMultipleResult::CreateRowset\n"));
//Rowset object 생성
hr = cmd->CreateRowset<CCUBRIDRowset>(pUnkOuter, riid, m_pParams, pcRowsAffected, ppRowset, pRowset);
if (FAILED(hr)) goto error;
pRowset->InitFromCommand(m_hReq, m_uCodepage, result_count);
} else
*ppRowset = NULL;
}
} else
{
if (pcRowsAffected)
*pcRowsAffected = result_count;
//커밋을 하지 않으므로 다음번 쿼리에서 현재 쿼리의 업데이트 내용을 볼 수 없다!!
//마지막 쿼리인 경우는 커밋
if (m_resultIndex > m_numQuery && m_numQuery > 1) //질의수가 2개 이상이면서 마지막 질의인 경우 커밋
{
GetSessionPtr()->AutoCommit(this);
m_hReq = 0;
m_bCommitted = true;
}
*ppRowset = NULL;
}
}
return S_OK;
error:
if (ppRowset)
*ppRowset = NULL;
if (pcRowsAffected)
*pcRowsAffected = DB_COUNTUNAVAILABLE;
m_hReq = 0;
if (strlen(error.err_msg) > 0)
return RaiseError(hr, 1, __uuidof(IMultipleResults), CA2W(error.err_msg, m_uCodepage));
else
return RaiseError(hr, 0, __uuidof(IMultipleResults));
}