//수행도중 에러가 나도 S_OK 리턴 //에러시 cardinality는 0이 된다. static HRESULT GetCardinality(int hConn, WCHAR* tableName, ULARGE_INTEGER* cardinality) { CComBSTR query; T_CCI_ERROR error; int hReq, res, ind; query.Append("select count(*) from "); query.Append(tableName); hReq = cci_prepare(hConn, CW2A(query.m_str), 0, &error); if (hReq < 0) return S_OK; res = cci_execute(hReq, CCI_EXEC_QUERY_ALL, 0, &error); if (res < 0) return S_OK; res = cci_cursor(hReq, 1, CCI_CURSOR_FIRST, &error); if(res<0) return S_OK; res = cci_fetch(hReq, &error); if(res<0) return S_OK; res = cci_get_data(hReq, 1, CCI_A_TYPE_INT, &cardinality->QuadPart, &ind); if(res<0) return S_OK; cci_close_req_handle(hReq); return S_OK; }
bool DbCursor(int qid,int n){ int ret = cci_cursor(qid, n, CCI_CURSOR_CURRENT, &cciErr); if(ret == CCI_ER_NO_MORE_DATA) return false; return true; }
int DbResultCount(int qid){ int len = 1; cci_cursor(qid, 0, CCI_CURSOR_FIRST, &cciErr); while(true){ if (!DbCursor(qid,1) ) break; if(!DbFetch(qid)) break; len ++; } cci_cursor(qid, -len, CCI_CURSOR_CURRENT, &cciErr); return len-1; }
HRESULT CCUBRIDRowsetRow::ReadData(int hReq, bool bOIDOnly, bool bSensitive) { ATLTRACE(atlTraceDBProvider, 3, "CCUBRIDRowsetRow::ReadData(1)\n"); // 컬럼 배열 생성 CCUBRIDRowsetRowColumn *pColumns; // = m_rgColumns { if(m_rgColumns) { // refresh(undo) row pColumns = m_rgColumns; } else { // create row pColumns = new CCUBRIDRowsetRowColumn[m_cColumns]; if(pColumns==NULL) return E_OUTOFMEMORY; m_rgColumns = pColumns; } } // cursor를 이동하고 fetch { // deferred delete는 메모리에 남아 있으므로 // iRowset이 storage상의 위치와 같다. T_CCI_ERROR err_buf; int res = cci_cursor(hReq, (int) m_iRowset+1, CCI_CURSOR_FIRST, &err_buf); if(res==CCI_ER_NO_MORE_DATA) return DB_S_ENDOFROWSET; if(res<0) return E_FAIL; if(bSensitive) res = cci_fetch_sensitive(hReq, &err_buf); else res = cci_fetch(hReq, &err_buf); if(res==CCI_ER_DELETED_TUPLE) return DB_E_DELETEDROW; if(res==CCI_ER_NO_MORE_DATA) return DB_S_ENDOFROWSET; if(res<0) return E_FAIL; } cci_get_cur_oid(hReq, m_szOID); if(bOIDOnly) return S_OK; for(DBORDINAL i=0;i<m_cColumns;i++) { CCUBRIDRowsetRowColumn *pColCur = &pColumns[i]; pColCur->ReadData(hReq, (int) m_pInfo[i].iOrdinal, m_pInfo[i].wType); } return S_OK; }
int CubTest(){ int nColCount = 0, nLen = 0; int i = 0, nDB = 0, nRet = 0, nReq = 0; char *pszBuff = NULL; T_CCI_COL_INFO *pcciCol; T_CCI_SQLX_CMD cciCmdType; assert((nDB = cci_connect(DB_SERVER, DB_PORT, DB_NAME, DB_USER_ID, DB_USER_PW)) >= 0); assert((nReq = cci_prepare(nDB, "SELECT * FROM athlete", 0, &cciErr)) >= 0); assert((pcciCol = cci_get_result_info(nReq, &cciCmdType, &nColCount)) != NULL); for (i = 1; i <= nColCount; i++) { printf("%s\t", CCI_GET_RESULT_INFO_NAME(pcciCol, i)); } printf("\n"); assert(cci_execute(nReq, 0, 0, &cciErr) >= 0); while (1) { if ( cci_cursor(nReq, 1, CCI_CURSOR_CURRENT, &cciErr) == CCI_ER_NO_MORE_DATA ) break; assert(cci_fetch(nReq, &cciErr) >= 0); for (i = 1; i<= nColCount; i++) { assert(cci_get_data(nReq, i, CCI_A_TYPE_STR, &pszBuff, &nLen) >= 0); printf("%s\t", pszBuff); } printf("\n"); } assert(cci_close_req_handle(nReq) >= 0); assert(cci_disconnect(nDB, &cciErr) >= 0); return 0; }
HRESULT CSRTables::Execute(LONG * /*pcRowsAffected*/, ULONG cRestrictions, const VARIANT *rgRestrictions) { ATLTRACE2(atlTraceDBProvider, 2, "CSRTables::Execute\n"); ClearError(); int hConn = -1; HRESULT hr = CCUBRIDSession::GetSessionPtr(this)->GetConnectionHandle(&hConn); if(FAILED(hr)) return hr; char table_name[1024]; table_name[0] = 0; int table_type = TABLE_TYPE_TABLE | TABLE_TYPE_VIEW | TABLE_TYPE_SYSTEM; GetRestrictions(cRestrictions, rgRestrictions, table_name, &table_type); { T_CCI_ERROR err_buf; int hReq = cci_schema_info(hConn, CCI_SCH_CLASS, (table_name[0]?table_name:NULL), NULL, CCI_CLASS_NAME_PATTERN_MATCH, &err_buf); if(hReq<0) { ATLTRACE2("cci_schema_info fail\n"); return RaiseError(E_FAIL, 1, __uuidof(IDBSchemaRowset), err_buf.err_msg); } 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) { CTABLESRow trData; hr = FetchData(hReq, trData, table_type); if(FAILED(hr)) { cci_close_req_handle(hReq); return hr; } if(hr==S_OK) // S_FALSE면 추가하지 않음 { _ATLTRY { // TABLE_TYPE, 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_szType, -1, trData.m_szType, -1); if(res==CSTR_GREATER_THAN) break; if(res==CSTR_EQUAL) { res = CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNOREKANATYPE | NORM_IGNOREWIDTH | SORT_STRINGSORT, m_rgRowData[nPos].m_szTable, -1, trData.m_szTable, -1); if(res==CSTR_GREATER_THAN) break; } } m_rgRowData.InsertAt(nPos, trData); } _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), err_buf.err_msg); done: cci_close_req_handle(hReq); }
HRESULT CSRColumns::Execute(LONG * /*pcRowsAffected*/, ULONG cRestrictions, const VARIANT *rgRestrictions) { ATLTRACE2(atlTraceDBProvider, 2, "CSRColumns::Execute\n"); ClearError(); int hConn = -1; HRESULT hr = CCUBRIDSession::GetSessionPtr(this)->GetConnectionHandle(&hConn); if(FAILED(hr)) return hr; ULONG ulMaxLen = CCUBRIDSession::GetSessionPtr(this)->GetDataSourcePtr()->PARAM_MAX_STRING_LENGTH; char table_name[1024]; table_name[0] = 0; char column_name[1024]; column_name[0] = 0; GetRestrictions(cRestrictions, rgRestrictions, table_name, column_name); { T_CCI_ERROR err_buf; int hReq = cci_schema_info(hConn, CCI_SCH_ATTRIBUTE, (table_name[0]?table_name:NULL), (column_name[0]?column_name: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; hr = FetchData(hReq, 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), err_buf.err_msg); done: cci_close_req_handle(hReq); } return S_OK; }
HRESULT CSRKeyColumnUsage::Execute(LONG *pcRowsAffected, ULONG cRestrictions, const VARIANT *rgRestrictions) { ATLTRACE2(atlTraceDBProvider, 2, "CSRKeyColumnUsage::Execute()\n"); //// int cci_connection_handle = -999; WCHAR tableName[128] = {'\0', }; HRESULT hr = E_FAIL; //// cci_connection_handle = CCUBRIDSession::GetSessionPtr(this)->GetConnection(); UINT uCodepage = CCUBRIDSession::GetSessionPtr(this)->GetCodepage(); GetRestrictions(cRestrictions, rgRestrictions, tableName); //// int cci_return_code = -999; int cci_request_handle = 0; T_CCI_ERROR cci_error_buffer; //// #if 1 CW2A _tableName(tableName, uCodepage); cci_return_code = cci_schema_info ( cci_connection_handle, CCI_SCH_PRIMARY_KEY, (tableName[0]? (PSTR) _tableName : NULL), NULL, // no attribute name is specified. CCI_CLASS_NAME_PATTERN_MATCH, &cci_error_buffer ); #endif if (cci_return_code < 0) { ATLTRACE2(atlTraceDBProvider, 2, "CSRPrimaryKeys::cci_schema_info() FAILED! \n"); return RaiseError(E_FAIL, 1, __uuidof(IDBSchemaRowset), CA2W(cci_error_buffer.err_msg, uCodepage)); } cci_request_handle = cci_return_code; cci_return_code = cci_cursor(cci_request_handle, 1, CCI_CURSOR_FIRST, &cci_error_buffer); if (cci_return_code < 0) { if (cci_return_code == CCI_ER_NO_MORE_DATA) { cci_close_req_handle(cci_request_handle); return S_OK; } else { cci_close_req_handle(cci_request_handle); return RaiseError(E_FAIL, 1, __uuidof(IDBSchemaRowset), CA2W(cci_error_buffer.err_msg, uCodepage)); } } while (1) { CKeyColumnUsageRow row; hr = FetchData(cci_request_handle, uCodepage, row); if (FAILED(hr)) return hr; if (hr == S_OK) { _ATLTRY { size_t position = 0; for (position = 0; position < m_rgRowData.GetCount() ; position++) { int result = CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNOREKANATYPE | NORM_IGNOREWIDTH | SORT_STRINGSORT, m_rgRowData[position].GetConstraintName(), -1, row.GetConstraintName(), -1); if ( result == CSTR_GREATER_THAN ) { break; } else if (result == CSTR_EQUAL) { result = CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNOREKANATYPE | NORM_IGNOREWIDTH | SORT_STRINGSORT, m_rgRowData[position].GetTableName(), -1, row.GetTableName(), -1); if (result == CSTR_GREATER_THAN) break; else if (result == CSTR_EQUAL) { if (m_rgRowData[position].GetOrdinalPosition() >= row.GetOrdinalPosition()) break; } } } m_rgRowData.InsertAt(position, row); } _ATLCATCHALL() { cci_close_req_handle(cci_request_handle); return E_OUTOFMEMORY; } } cci_return_code = cci_cursor(cci_request_handle, 1, CCI_CURSOR_CURRENT, &cci_error_buffer); if (cci_return_code < 0) { if (cci_return_code == CCI_ER_NO_MORE_DATA) { cci_close_req_handle(cci_request_handle); return S_OK; } else { cci_close_req_handle(cci_request_handle); return RaiseError(E_FAIL, 1, __uuidof(IDBSchemaRowset), CA2W(cci_error_buffer.err_msg, uCodepage)); } } }
HRESULT CSRViewColumnUsage::Execute(LONG* pcRowsAffected, ULONG cRestrictions, const VARIANT *rgRestrictions) { ATLTRACE2(atlTraceDBProvider, 2, "CSRViewColumnUsage::Execute\n"); ClearError(); if (pcRowsAffected) *pcRowsAffected = 0; int hConn = CCUBRIDSession::GetSessionPtr(this)->GetConnection(); UINT uCodepage = CCUBRIDSession::GetSessionPtr(this)->GetCodepage(); WCHAR view_name[256]; view_name[0] = 0; GetRestrictions(cRestrictions, rgRestrictions, view_name); { T_CCI_ERROR err_buf; char current_view_name[256]; char* buffer; int res, ind; int hReq, hReq2; CW2A _viewName(view_name, uCodepage); hReq = cci_schema_info(hConn, CCI_SCH_VCLASS, (view_name[0] ? (PSTR) _viewName : NULL), NULL, CCI_CLASS_NAME_PATTERN_MATCH, &err_buf); if(hReq<0) { ATLTRACE2("cci_schema_info fail\n"); return RaiseError(E_FAIL, 1, __uuidof(IDBSchemaRowset), CA2W(err_buf.err_msg, uCodepage)); } 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) { res = cci_fetch(hReq, &err_buf); if(res<0) goto error; res = cci_get_data(hReq, 1, CCI_A_TYPE_STR, &buffer, &ind); if(res<0) goto error; strcpy(current_view_name, buffer); hReq2 = cci_schema_info(hConn, CCI_SCH_ATTRIBUTE, current_view_name, NULL, CCI_ATTR_NAME_PATTERN_MATCH, &err_buf); if(hReq2<0) { ATLTRACE2("cci_schema_info fail\n"); goto error; } res = cci_cursor(hReq2, 1, CCI_CURSOR_FIRST, &err_buf); if(res==CCI_ER_NO_MORE_DATA) goto next_view; if(res<0) goto error; while(1) { CViewColumnUsageRow tprData; wcscpy(tprData.m_szViewName, CA2W(current_view_name, uCodepage)); res = cci_fetch(hReq2, &err_buf); if(res<0) goto error; res = cci_get_data(hReq2, 1, CCI_A_TYPE_STR, &buffer, &ind); if(res<0) goto error; wcscpy(tprData.m_szColumnName, CA2W(buffer, uCodepage)); res = cci_get_data(hReq2, 11, CCI_A_TYPE_STR, &buffer, &ind); if(res<0) goto error; wcscpy(tprData.m_szTableName, CA2W(buffer, uCodepage)); _ATLTRY { // VIEW_NAME, TABLE_NAME, COLUMN_NAME 순으로 정렬해야 한다. size_t nPos; for( nPos=0 ; nPos<m_rgRowData.GetCount() ; nPos++ ) { int res = wcscmp(m_rgRowData[nPos].m_szViewName, tprData.m_szViewName); if(res>0) break; if (res == 0) { res = wcscmp(m_rgRowData[nPos].m_szTableName, tprData.m_szTableName); if (res>0) break; if (res == 0) { res = wcscmp(m_rgRowData[nPos].m_szColumnName, tprData.m_szColumnName); if (res>0) break; } } } m_rgRowData.InsertAt(nPos, tprData); } _ATLCATCHALL() { ATLTRACE2("out of memory\n"); cci_close_req_handle(hReq); return E_OUTOFMEMORY; } if (pcRowsAffected) (*pcRowsAffected)++; res = cci_cursor(hReq2, 1, CCI_CURSOR_CURRENT, &err_buf); if(res==CCI_ER_NO_MORE_DATA) break; if(res<0) goto error; } next_view: cci_close_req_handle(hReq2); 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); cci_close_req_handle(hReq2); return RaiseError(E_FAIL, 1, __uuidof(IDBSchemaRowset), CA2W(err_buf.err_msg, uCodepage)); done: cci_close_req_handle(hReq); } return S_OK; }