forked from CUBRID/cubrid-oledb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SRColumns.cpp
278 lines (244 loc) · 9.52 KB
/
SRColumns.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
/*
* 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 "Session.h"
#include "type.h"
#include "Error.h"
#include "DataSource.h"
CSRColumns::~CSRColumns()
{
if(m_spUnkSite)
{
CCUBRIDSession* pSession = CCUBRIDSession::GetSessionPtr(this);
if (pSession->m_cSessionsOpen == 1) pSession->RowsetCommit();
}
}
static void GetRestrictions(ULONG cRestrictions, const VARIANT *rgRestrictions,
PWSTR table_name, PWSTR column_name)
{
// restriction이 없다고 항상 cRestrictions==0은 아니다.
// 따라서 vt!=VT_EMPTY인지 등도 검사해줘야 한다.
if(cRestrictions>=3 && V_VT(&rgRestrictions[2])==VT_BSTR && V_BSTR(&rgRestrictions[2])!=NULL)
{ // TABLE_NAME restriction
wcsncpy(table_name, V_BSTR(&rgRestrictions[2]), 1023);
ATLTRACE2(L"\tTable Name = %s\n", V_BSTR(&rgRestrictions[2]));
table_name[1023] = 0; // ensure zero-terminated string
}
if(cRestrictions>=4 && V_VT(&rgRestrictions[3])==VT_BSTR && V_BSTR(&rgRestrictions[3])!=NULL)
{ // COLUMN_NAME restriction
wcsncpy(column_name, V_BSTR(&rgRestrictions[3]), 1023);
ATLTRACE2("\tColumn Name = %s\n", V_BSTR(&rgRestrictions[3]));
column_name[1023] = 0; // ensure zero-terminated string
}
}
// S_OK : 성공
// E_FAIL : 실패
static HRESULT FetchData(int hReq, UINT uCodepage, CCOLUMNSRow &crData)
{
char *cvalue;
int ivalue, ind, res;
T_CCI_ERROR err_buf;
int nCCIUType, nPrecision, nScale;
bool bNullable;
res = cci_fetch(hReq, &err_buf);
if(res<0) return RaiseError(E_FAIL, 1, __uuidof(IDBSchemaRowset), CA2W(err_buf.err_msg, uCodepage));
res = cci_get_data(hReq, 11, CCI_A_TYPE_STR, &cvalue, &ind);
if(res<0) return RaiseError(E_FAIL, 0, __uuidof(IDBSchemaRowset));
wcsncpy(crData.m_szTableName, CA2W(cvalue, uCodepage), 128);
crData.m_szTableName[128] = 0;
_wcsupr(crData.m_szTableName);
res = cci_get_data(hReq, 1, CCI_A_TYPE_STR, &cvalue, &ind);
if(res<0) return RaiseError(E_FAIL, 0, __uuidof(IDBSchemaRowset));
wcsncpy(crData.m_szColumnName, CA2W(cvalue, uCodepage), 128);
crData.m_szColumnName[128] = 0;
_wcsupr(crData.m_szColumnName);
res = cci_get_data(hReq, 2, CCI_A_TYPE_INT, &ivalue, &ind);
if(res<0) return RaiseError(E_FAIL, 0, __uuidof(IDBSchemaRowset));
nCCIUType = ivalue;
if(nCCIUType==-1)
crData.m_nDataType = Type::GetStaticTypeInfo(CCI_U_TYPE_STRING).nOLEDBType;
else
crData.m_nDataType = Type::GetStaticTypeInfo(ivalue).nOLEDBType;
res = cci_get_data(hReq, 3, CCI_A_TYPE_INT, &ivalue, &ind);
if(res<0) return RaiseError(E_FAIL, 0, __uuidof(IDBSchemaRowset));
nScale = ivalue;
res = cci_get_data(hReq, 4, CCI_A_TYPE_INT, &ivalue, &ind);
if(res<0) return RaiseError(E_FAIL, 0, __uuidof(IDBSchemaRowset));
nPrecision = ivalue;
res = cci_get_data(hReq, 6, CCI_A_TYPE_INT, &ivalue, &ind);
if(res<0) return RaiseError(E_FAIL, 0, __uuidof(IDBSchemaRowset));
crData.m_bIsNullable = ( ivalue==0 ? ATL_VARIANT_TRUE : ATL_VARIANT_FALSE );
bNullable = (ivalue==0);
res = cci_get_data(hReq, 10, CCI_A_TYPE_INT, &ivalue, &ind);
if(res<0) return RaiseError(E_FAIL, 0, __uuidof(IDBSchemaRowset));
crData.m_ulOrdinalPosition = ivalue;
res = cci_get_data(hReq, 9, CCI_A_TYPE_STR, &cvalue, &ind);
if(res<0) return RaiseError(E_FAIL, 0, __uuidof(IDBSchemaRowset));
if(ind==-1)
{
crData.m_bColumnHasDefault = ATL_VARIANT_FALSE;
crData.m_szColumnDefault[0] = L'\x0';
}
else
{
crData.m_bColumnHasDefault = ATL_VARIANT_TRUE;
wcsncpy(crData.m_szColumnDefault, CA2W(cvalue, uCodepage), 128);
crData.m_szColumnDefault[128] = 0;
}
Type::DynamicTypeInfo dyn_info =
( nCCIUType==-1 ? Type::GetDynamicTypeInfo(CCI_U_TYPE_STRING, 0, 0, true) :
Type::GetDynamicTypeInfo(nCCIUType, nPrecision, nScale, bNullable) );
crData.m_ulColumnFlags = dyn_info.ulFlags;
crData.m_nNumericPrecision = dyn_info.nNumericPrecision;
crData.m_nNumericScale = dyn_info.nNumericScale;
crData.m_ulDateTimePrecision = dyn_info.ulDateTimePrecision;
// TODO: CCI_PARAM_MAX_STRING_LENGTH 적용?
crData.m_ulCharMaxLength = dyn_info.ulCharMaxLength;
crData.m_ulCharOctetLength = dyn_info.ulCharOctetLength;
return S_OK;
}
HRESULT CSRColumns::Execute(LONG * /*pcRowsAffected*/,
ULONG cRestrictions, const VARIANT *rgRestrictions)
{
ATLTRACE2(atlTraceDBProvider, 2, "CSRColumns::Execute\n");
ClearError();
int hConn = CCUBRIDSession::GetSessionPtr(this)->GetConnection();
UINT uCodepage = CCUBRIDSession::GetSessionPtr(this)->GetCodepage();
ULONG ulMaxLen = CCUBRIDSession::GetSessionPtr(this)->GetDataSourcePtr()->PARAM_MAX_STRING_LENGTH;
WCHAR table_name[1024]; table_name[0] = 0;
WCHAR column_name[1024]; column_name[0] = 0;
GetRestrictions(cRestrictions, rgRestrictions, table_name, column_name);
{
T_CCI_ERROR err_buf;
CW2A _tableName(table_name, uCodepage);
CW2A _columnName(column_name, uCodepage);
int hReq = cci_schema_info(hConn, CCI_SCH_ATTRIBUTE,
(table_name[0] ? (PSTR) _tableName : NULL),
(column_name[0]? (PSTR) _columnName : NULL),
CCI_CLASS_NAME_PATTERN_MATCH | CCI_ATTR_NAME_PATTERN_MATCH,
&err_buf);
if(hReq<0)
{
ATLTRACE2("cci_schema_info fail\n");
return E_FAIL;
}
int res = cci_cursor(hReq, 1, CCI_CURSOR_FIRST, &err_buf);
if(res==CCI_ER_NO_MORE_DATA) goto done;
if(res<0) goto error;
while(1)
{
CCOLUMNSRow crData;
HRESULT hr = FetchData(hReq, uCodepage, crData);
if(FAILED(hr)) goto error;
//MAX STRING LENGTH 반영
if (crData.m_ulCharMaxLength != (ULONG)~0 && crData.m_ulCharMaxLength > ulMaxLen)
crData.m_ulCharMaxLength = ulMaxLen;
if (crData.m_ulCharMaxLength != (ULONG)~0 && crData.m_ulCharOctetLength > ulMaxLen)
crData.m_ulCharOctetLength = ulMaxLen;
_ATLTRY
{
// TABLE_NAME 순으로 정렬한다.
size_t nPos;
for( nPos=0 ; nPos<m_rgRowData.GetCount() ; nPos++ )
{
int res = CompareStringW(LOCALE_USER_DEFAULT,
NORM_IGNOREKANATYPE | NORM_IGNOREWIDTH | SORT_STRINGSORT,
m_rgRowData[nPos].m_szTableName, -1,
crData.m_szTableName, -1);
if(res==CSTR_GREATER_THAN) break;
}
m_rgRowData.InsertAt(nPos, crData);
}
_ATLCATCHALL()
{
ATLTRACE2("out of memory\n");
cci_close_req_handle(hReq);
return E_OUTOFMEMORY;
}
res = cci_cursor(hReq, 1, CCI_CURSOR_CURRENT, &err_buf);
if(res==CCI_ER_NO_MORE_DATA) goto done;
if(res<0) goto error;
}
error:
ATLTRACE2("fail to fetch data\n");
cci_close_req_handle(hReq);
return RaiseError(E_FAIL, 1, __uuidof(IDBSchemaRowset), CA2W(err_buf.err_msg, uCodepage));
done:
cci_close_req_handle(hReq);
}
return S_OK;
}
DBSTATUS CSRColumns::GetDBStatus(CSimpleRow *pRow, ATLCOLUMNINFO* pInfo)
{
ATLTRACE2(atlTraceDBProvider, 3, "CSRColumns::GetDBStatus\n");
switch(pInfo->iOrdinal)
{
case 3: // TABLE_NAME
case 4: // COLUMN_NAME
case 7: // ORDINAL_POSITION
case 8: // COLUMN_HASDEFAULT
case 10: // COLUMN_FLAGS
case 11: // IS_NULLABLE
case 12: // DATA_TYPE
return DBSTATUS_S_OK;
case 9: // COLUMN_DEFAULT
if(m_rgRowData[pRow->m_iRowset].m_bColumnHasDefault==ATL_VARIANT_FALSE)
return DBSTATUS_S_ISNULL;
else
return DBSTATUS_S_OK;
case 14: // CHARACTER_MAXIMUM_LENGTH
if(m_rgRowData[pRow->m_iRowset].m_ulCharMaxLength==(ULONG)~0)
return DBSTATUS_S_ISNULL;
else
return DBSTATUS_S_OK;
case 15: // CHARACTER_OCTET_LENGTH
if(m_rgRowData[pRow->m_iRowset].m_ulCharOctetLength==(ULONG)~0)
return DBSTATUS_S_ISNULL;
else
return DBSTATUS_S_OK;
case 16: // NUMERIC_PRECISION
if(m_rgRowData[pRow->m_iRowset].m_nNumericPrecision==(USHORT)~0)
return DBSTATUS_S_ISNULL;
else
return DBSTATUS_S_OK;
case 17: // NUMERIC_SCALE
if(m_rgRowData[pRow->m_iRowset].m_nNumericScale==-1)
return DBSTATUS_S_ISNULL;
else
return DBSTATUS_S_OK;
case 18: // DATETIME_PRECISION
if(m_rgRowData[pRow->m_iRowset].m_ulDateTimePrecision==(ULONG)~0)
return DBSTATUS_S_ISNULL;
else
return DBSTATUS_S_OK;
default:
return DBSTATUS_S_ISNULL;
}
}