//-------------------------------------------------------------- void FillSelect(HWND hWnd) { CBase base(ParametersBase().name()); if(base.IsOpen()) { ADODB::_RecordsetPtr rec; if(base.OpenTable(ParametersTable().name(), rec)) { while (!rec->EndOfFile) { wchar_t *c = _bstr_t(rec->Fields->GetItem(NameParam().name())->GetValue()); ComboBox_AddString(hWnd, c); rec->MoveNext(); } //CurrentParametersTable param; CurrentParametersTable ¶m = Singleton<CurrentParametersTable>::Instance(); Select<CurrentParametersTable>(base).ID(1).Execute(param); int id = param.items.get<CurrentID>().value; ParametersTable typeSizeParam; Select<ParametersTable>(base).ID(id).Execute(typeSizeParam); int num = ComboBox_FindStringExact(hWnd, 0, typeSizeParam.items.get<NameParam>().value); if(CB_ERR != num) ComboBox_SetCurSel(hWnd, num); } } }
/** * Executes the passed in command on the database. The caller is responsible for deleting * the created RecordSet. * * @param CommandString Command to execute * @param RecordSet Reference to recordset pointer that is going to hold result * * @return TRUE if execution was successful, FALSE otherwise */ virtual UBOOL Execute( const TCHAR* CommandString, FDataBaseRecordSet*& RecordSet ) { // Initialize return value. RecordSet = NULL; try { // Create instance of record set. ADODB::_RecordsetPtr ADORecordSet = NULL; ADORecordSet.CreateInstance(__uuidof(ADODB::Recordset) ); // Execute the passed in command on the record set. The recordset returned will be in open state so you can call Get* on it directly. ADORecordSet->Open( CommandString, _variant_t((IDispatch *) DataBaseConnection), ADODB::adOpenStatic, ADODB::adLockReadOnly, ADODB::adCmdText ); // Create record set from returned data. RecordSet = new FADODataBaseRecordSet( ADORecordSet ); } catch(_com_error &Error) { // Log error and return. This will not log if called before appInit due to use of debugf. TCHAR* ErrorDescription = (TCHAR*) Error.Description(); warnf(NAME_DevDataBase,TEXT("Failure executing command [%s] [%s]"),CommandString,ErrorDescription); // Delete record set if it has already been created. delete RecordSet; RecordSet = NULL; } return RecordSet != NULL; }
void CSiteDir::BuildFromDatabase() { WCHAR sql[256]; swprintf(sql, L"SELECT id,filename,type,file_size,file_date FROM files WHERE parent_id = %d", m_dbid); _variant_t va; ADODB::_RecordsetPtr recordset = m_pWebSite->m_siteDataConnection->Execute(sql, &va, ADODB::adCmdText); CArray<CSiteDir*,CSiteDir*> dirs; while (recordset->EndOfFile == false) { CSiteItem* pItem; BYTE type = recordset->Fields->Item[_bstr_t("type")]->Value.lVal; if (type == 1) // Directory { CSiteDir* pDir = new CSiteDir; pItem = pDir; strcpy(pDir->m_wfd.cFileName, _bstr_t(recordset->Fields->Item[_bstr_t("filename")]->Value)); _makepath(pDir->m_pathName.GetBuffer(512), NULL, m_pathName, pDir->m_wfd.cFileName, NULL); pDir->m_pathName.ReleaseBuffer(); dirs.Add(pDir); } else if (type == 2) // File { CSiteFile* pFile = new CSiteFile; pItem = pFile; strcpy(pFile->m_wfd.cFileName, _bstr_t(recordset->Fields->Item[_bstr_t("filename")]->Value)); pFile->m_wfd.nFileSizeLow = recordset->Fields->Item[_bstr_t("file_size")]->Value.lVal; } else ATLASSERT(0); if (pItem) { pItem->m_pWebSite = m_pWebSite; pItem->m_dbid = recordset->Fields->Item[_bstr_t("id")]->Value.lVal; // pItem->m_name = pItem->m_wfd.cFileName; // ?? // lastWriteDate double lastWriteTime = recordset->Fields->Item[_bstr_t("file_date")]->Value; SYSTEMTIME systime; VariantTimeToSystemTime(lastWriteTime, &systime); SystemTimeToFileTime(&systime, &pItem->m_wfd.ftLastWriteTime); AddChildTail(pItem); } recordset->MoveNext(); } for (int i = 0; i < dirs.GetSize(); i++) { dirs[i]->BuildFromDatabase(); // Recurse } }
bool CDataStorage::OpenRecordset(ADODB::_RecordsetPtr& spRsNew, string sSQL, long lMaxRecords, long eCursorType, long eLockType, bool bDisconnect) { bool bRtn = false; try { ADODB::_RecordsetPtr loRsNew; HRESULT hr = loRsNew.CreateInstance(__uuidof(ADODB::Recordset)); if (SUCCEEDED(hr)) { //set rs parameters loRsNew->put_CacheSize( lMaxRecords > 0 ? lMaxRecords : 1 ); loRsNew->put_MaxRecords( lMaxRecords > 0 ? lMaxRecords : 0 ); loRsNew->put_CursorType( (ADODB::CursorTypeEnum)eCursorType ); loRsNew->put_LockType( (ADODB::LockTypeEnum)eLockType ); //open the rs if( m_AdoConnection != NULL ) { // BSTR sSQL string to variant CComVariant vSQL( CComBSTR(sSQL.c_str()) ); CComVariant voAdoConn( m_AdoConnection.GetInterfacePtr() ); loRsNew->Open(vSQL, voAdoConn, ( (ADODB::CursorTypeEnum)eCursorType ), ( (ADODB::LockTypeEnum)eLockType ), ADODB::adCmdText); } //If disconnection was requested disconnect the rs if( bDisconnect ) loRsNew->put_ActiveConnection( vtMissing ); hr = loRsNew->QueryInterface(loRsNew.GetIID(),(void**)&spRsNew ); if( hr == S_OK ) bRtn = true; } } catch(_com_error &e) { //Handle errors char ErrorStr[1000]; _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description()); sprintf_s(ErrorStr, 1000, "OpenRecordset Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n", e.Error(), e.ErrorMessage(), (LPCSTR)bstrSource, (LPCSTR)bstrDescription ); OutputDebugString(ErrorStr); } return bRtn; }
/** * Returns a float associated with the passed in field/ column for the current row. * * @param Column Name of column to retrieve data for in current row */ virtual FLOAT GetFloat( const TCHAR* Column ) const { FLOAT ReturnValue = 0; try { // Retrieve specified column field value for selected row. _variant_t Value = ADORecordSet->GetCollect( Column ); // Check variant type for validity and cast to specified type. _variant_t has overloaded cast operators. if( Value.vt != VT_NULL ) { ReturnValue = (FLOAT)Value; } // Unknown column. else { debugf(NAME_DevDataBase,TEXT("Failure retrieving FLOAT value for column [%s]"),Column); } } // Error handling. Will return default value of 0. catch( _com_error& Error ) { debugf(NAME_DevDataBase,TEXT("Failure retrieving FLOAT value for column [%s] [%s]"),Column,(TCHAR*) Error.Description()); } return ReturnValue; }
/** * Returns a string associated with the passed in field/ column for the current row. * * @param Column Name of column to retrieve data for in current row */ virtual FString GetString( const TCHAR* Column ) const { FString ReturnString; try { // Retrieve specified column field value for selected row. _variant_t Value = ADORecordSet->GetCollect( Column ); // Check variant type for validity and cast to specified type. _variant_t has overloaded cast operators. if( Value.vt != VT_NULL ) { ReturnString = (TCHAR*)_bstr_t(Value); } // Unknown column. else { ReturnString = TEXT("Unknown Column"); } } // Error handling. Will return string with error message. catch( _com_error& Error ) { ReturnString = FString::Printf(TEXT("Failure retrieving string value for column [%s] [%s]"),Column,(TCHAR*) Error.Description()); debugf(NAME_DevDataBase,TEXT("%s"),*ReturnString); } return ReturnString; }
/** Moves to the next record in the set. */ virtual void MoveToNext() { if( !ADORecordSet->ADOEOF ) { ADORecordSet->MoveNext(); } }
/** Moves to the first record in the set. */ virtual void MoveToFirst() { if( !ADORecordSet->BOF || !ADORecordSet->ADOEOF ) { ADORecordSet->MoveFirst(); } }
/** * Returns the set of column names for this Recordset. This is useful for determining * what you can actually ask the record set for without having to hard code those ahead of time. */ virtual TArray<FDatabaseColumnInfo> GetColumnNames() const { TArray<FDatabaseColumnInfo> Retval; if( !ADORecordSet->BOF || !ADORecordSet->ADOEOF ) { ADORecordSet->MoveFirst(); for( SWORD i = 0; i < ADORecordSet->Fields->Count; ++i ) { _bstr_t bstrName = ADORecordSet->Fields->Item[i]->Name; _variant_t varValue = ADORecordSet->Fields->Item[i]->Value; ADODB::DataTypeEnum DataType = ADORecordSet->Fields->Item[i]->Type; FDatabaseColumnInfo NewInfo; NewInfo.ColumnName = FString((TCHAR*)_bstr_t(bstrName)); // from http://www.w3schools.com/ado/prop_field_type.asp#datatypeenum switch( DataType ) { case ADODB::adInteger: case ADODB::adBigInt: NewInfo.DataType = DBT_INT; break; case ADODB::adSingle: case ADODB::adDouble: NewInfo.DataType = DBT_FLOAT; break; case ADODB::adWChar: case ADODB::adVarWChar: NewInfo.DataType = DBT_STRING; break; default: warnf( NAME_DevDataBase, TEXT("Unable to find a EDataBaseUE3Types (%s) from DODB::DataTypeEnum DataType: %d "), *NewInfo.ColumnName, static_cast<INT>(DataType) ); NewInfo.DataType = DBT_UNKOWN; break; } Retval.AddItem( NewInfo ); } } // here for debugging as this code is new. for( INT i = 0; i < Retval.Num(); ++ i ) { warnf( TEXT( "ColumnName %d: Name: %s Type: %d"), i, *Retval(i).ColumnName, static_cast<INT>(Retval(i).DataType) ); } return Retval; }
/** Destructor, cleaning up ADO record set. */ virtual ~FADODataBaseRecordSet() { // We're using smart pointers so all we need to do is close and assign NULL. ADORecordSet->Close(); ADORecordSet = NULL; }
void PerformSearch(JNIEnv *env, jobject obj, jobject callback, std::wstring searchText, std::list<std::wstring>& patterns,int maximumResults, jobject monitor) { ADODB::_ConnectionPtr connection = NULL; ADODB::_RecordsetPtr recordset = NULL; try { HRESULT hr = ::CoInitialize(NULL); if (!SUCCEEDED(hr)) return; hr = connection.CreateInstance(__uuidof(ADODB::Connection)); if (!SUCCEEDED(hr)) return; hr = recordset.CreateInstance(__uuidof(ADODB::Recordset)); if (!SUCCEEDED(hr)) return; connection->CursorLocation = ADODB::adUseClient; hr = connection->Open(L"Provider=Search.CollatorDSO;Extended Properties='Application=Windows';", L"", L"", ADODB::adConnectUnspecified); if (!SUCCEEDED(hr)) return; searchText = ReplaceCharWithString(searchText, '\'', L"''"); std::wstring filenameMatcher = searchText; filenameMatcher = ReplaceCharWithString(filenameMatcher, '%', L"\\%"); filenameMatcher = ReplaceCharWithString(filenameMatcher, '*', L"%"); // see FREETEXT http://msdn.microsoft.com/en-us/library/bb231268(v=vs.85).aspx // see CONTAINS http://msdn.microsoft.com/en-us/library/bb231270(v=vs.85).aspx std::wstring query = L"SELECT TOP "; query += IntToString(maximumResults); query += L" System.ItemPathDisplay from SystemIndex WHERE "; // limit to documents, pictures and video query += L"(System.Kind = 'document' or System.Kind = 'picture' or System.Kind = 'video' or System.Kind is null) AND "; // content search query += L"(FREETEXT('\""; query += searchText; query += L"\"') OR "; // filename search query += L"(System.ItemName LIKE '%"; query += filenameMatcher; query += L"%'))"; if (patterns.size() > 0) { bool hasFilenameSearch = true; std::wstring anyFile(L"*"); for(std::list<std::wstring>::iterator iterator = patterns.begin(); iterator != patterns.end(); iterator++) { std::wstring pattern = *iterator; if (pattern == anyFile) { hasFilenameSearch = false; break; } } if (hasFilenameSearch) { query += L" AND ("; for(std::list<std::wstring>::iterator iterator = patterns.begin(); iterator != patterns.end(); iterator++) { std::wstring pattern = *iterator; if (pattern.size() == 0) { continue; } pattern = ReplaceCharWithString(pattern, '*', L"%"); if (iterator != patterns.begin()) { query += L" OR "; } query += L"(System.ItemName LIKE '"; if (pattern[0] != '%') { query += L"%"; } query += pattern; query += L"')"; } query += L")"; } } //std::cout << "Query:\n"; //std::cout << query; //std::cout << "\n"; //std::cout << std::flush; hr = recordset->Open(query.c_str(), connection.GetInterfacePtr(), ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, ADODB::adCmdText); if (!SUCCEEDED(hr)) { std::cout << "Open Failed\n" << std::flush; return; } int count = maximumResults; while(!recordset->ADOEOF) { _variant_t var = recordset->Fields->GetItem(L"System.ItemPathDisplay")->GetValue(); std::wstring filename = (const wchar_t*)_bstr_t(var.bstrVal); InvokeCallback(env, obj, callback, filename); if (IsProgressMonitorCancelled(env,monitor)) { break; } if (--count < 0) { break; } hr = recordset->MoveNext(); if (!SUCCEEDED(hr)) { std::cout << "Move Next Failed\n" << std::flush; break; } } } catch (_com_error &e) { _tprintf(_T("\tCOM Error code = %08lx\n"), e.Error()); } if (recordset != NULL && recordset->State == ADODB::adStateOpen) recordset->Close(); if (connection != NULL && connection->State == ADODB::adStateOpen) connection->Close(); ::CoUninitialize(); }