CVariant CVariant::LongChar(const char *p, size_t len) { return CVariant(p ? new CDB_LongChar(len, p) : new CDB_LongChar(len)); }
CVariant CVariant::Double(double *p) { return CVariant(p ? new CDB_Double(*p) : new CDB_Double()); }
CVariant CVariant::Bit(bool *p) { return CVariant(p ? new CDB_Bit(*p) : new CDB_Bit()); }
CVariant CVariant::TinyInt(Uint1 *p) { return CVariant(p ? new CDB_TinyInt(*p) : new CDB_TinyInt()); }
CVariant CVariant::Float(float *p) { return CVariant(p ? new CDB_Float(*p) : new CDB_Float()); }
void CVideoInfoTag::Serialize(CVariant& value) const { value["director"] = m_director; value["writer"] = m_writingCredits; value["genre"] = m_genre; value["country"] = m_country; value["tagline"] = m_strTagLine; value["plotoutline"] = m_strPlotOutline; value["plot"] = m_strPlot; value["title"] = m_strTitle; value["votes"] = StringUtils::Format("%i", GetRating().votes); value["studio"] = m_studio; value["trailer"] = m_strTrailer; value["cast"] = CVariant(CVariant::VariantTypeArray); for (unsigned int i = 0; i < m_cast.size(); ++i) { CVariant actor; actor["name"] = m_cast[i].strName; actor["role"] = m_cast[i].strRole; actor["order"] = m_cast[i].order; if (!m_cast[i].thumb.empty()) actor["thumbnail"] = CTextureUtils::GetWrappedImageURL(m_cast[i].thumb); value["cast"].push_back(actor); } value["set"] = m_strSet; value["setid"] = m_iSetId; value["setoverview"] = m_strSetOverview; value["tag"] = m_tags; value["runtime"] = GetDuration(); value["file"] = m_strFile; value["path"] = m_strPath; value["imdbnumber"] = GetUniqueID(); value["mpaa"] = m_strMPAARating; value["filenameandpath"] = m_strFileNameAndPath; value["originaltitle"] = m_strOriginalTitle; value["sorttitle"] = m_strSortTitle; value["episodeguide"] = m_strEpisodeGuide; value["premiered"] = m_premiered.IsValid() ? m_premiered.GetAsDBDate() : StringUtils::Empty; value["status"] = m_strStatus; value["productioncode"] = m_strProductionCode; value["firstaired"] = m_firstAired.IsValid() ? m_firstAired.GetAsDBDate() : StringUtils::Empty; value["showtitle"] = m_strShowTitle; value["album"] = m_strAlbum; value["artist"] = m_artist; value["playcount"] = GetPlayCount(); value["lastplayed"] = m_lastPlayed.IsValid() ? m_lastPlayed.GetAsDBDateTime() : StringUtils::Empty; value["top250"] = m_iTop250; value["year"] = m_premiered.GetYear(); value["season"] = m_iSeason; value["episode"] = m_iEpisode; for (const auto& i : m_uniqueIDs) value["uniqueid"][i.first] = i.second; value["rating"] = GetRating().rating; CVariant ratings = CVariant(CVariant::VariantTypeObject); for (const auto& i : m_ratings) { CVariant rating; rating["rating"] = i.second.rating; rating["votes"] = i.second.votes; rating["default"] = i.first == m_strDefaultRating; ratings[i.first] = rating; } value["ratings"] = ratings; value["userrating"] = m_iUserRating; value["dbid"] = m_iDbId; value["fileid"] = m_iFileId; value["track"] = m_iTrack; value["showlink"] = m_showLink; m_streamDetails.Serialize(value["streamdetails"]); CVariant resume = CVariant(CVariant::VariantTypeObject); resume["position"] = (float)m_resumePoint.timeInSeconds; resume["total"] = (float)m_resumePoint.totalTimeInSeconds; value["resume"] = resume; value["tvshowid"] = m_iIdShow; value["dateadded"] = m_dateAdded.IsValid() ? m_dateAdded.GetAsDBDateTime() : StringUtils::Empty; value["type"] = m_type; value["seasonid"] = m_iIdSeason; value["specialsortseason"] = m_iSpecialSortSeason; value["specialsortepisode"] = m_iSpecialSortEpisode; }
CVariant CVariant::VarChar(const char *p, size_t len) { return CVariant(p ? (len ? new CDB_VarChar(p, len) : new CDB_VarChar(p)) : new CDB_VarChar()); }
CVariant CVariant::DateTime(CTime *p) { return CVariant(p ? new CDB_DateTime(*p) : new CDB_DateTime()); }
//================================================================== CVariant CVariant::BigInt(Int8 *p) { return CVariant(p ? new CDB_BigInt(*p) : new CDB_BigInt()); }
CVariant CVariant::Binary(size_t size, const void *p, size_t len) { return CVariant(p ? new CDB_Binary(size, p, len) : new CDB_Binary(size)); }
CVariant CVariant::SmallDateTime(CTime *p) { return CVariant(p ? new CDB_SmallDateTime(*p) : new CDB_SmallDateTime()); }
CVariant CVariant::VarBinary(const void *p, size_t len) { return CVariant(p ? new CDB_VarBinary(p, len) : new CDB_VarBinary()); }
CVariant CVariant::LongBinary(size_t maxSize, const void *p, size_t len) { return CVariant(p ? new CDB_LongBinary(maxSize, p, len) : new CDB_LongBinary(maxSize)); }
CVariant CVariant::Char(size_t size, const char *p) { return CVariant(p ? new CDB_Char(size, p) : new CDB_Char(size)); }
void CKadLookup::InitLookupID() { CAbstractKey LookupID(KEY_64BIT, true); m_LookupID = CVariant(LookupID.GetKey(), LookupID.GetSize()); }
CVariant CVariant::Int(Int4 *p) { return CVariant(p ? new CDB_Int(*p) : new CDB_Int()); }
CVariant CKadOperator::GetResponses() { CVariant Responses = m_Responses; m_Responses = CVariant(CVariant::EList); return Responses; }
CVariant CVariant::SmallInt(Int2 *p) { return CVariant(p ? new CDB_SmallInt(*p) : new CDB_SmallInt()); }
BEGIN_NCBI_SCOPE /////////////////////////////////////////////////////////////////////////////// BOOST_AUTO_TEST_CASE(Test_Procedure) { try { // Test a regular IStatement with "exec" // Parameters are not allowed with this construction. { auto_ptr<IStatement> auto_stmt( GetConnection().GetStatement() ); // Execute it first time ... auto_stmt->SendSql( "exec sp_databases" ); while( auto_stmt->HasMoreResults() ) { if( auto_stmt->HasRows() ) { auto_ptr<IResultSet> rs( auto_stmt->GetResultSet() ); switch ( rs->GetResultType() ) { case eDB_RowResult: while( rs->Next() ) { // int col1 = rs->GetVariant(1).GetInt4(); } break; case eDB_ParamResult: while( rs->Next() ) { // int col1 = rs->GetVariant(1).GetInt4(); } break; case eDB_StatusResult: while( rs->Next() ) { int status = rs->GetVariant(1).GetInt4(); status = status; } break; case eDB_ComputeResult: case eDB_CursorResult: break; } } } // Execute it second time ... auto_stmt->SendSql( "exec sp_databases" ); while( auto_stmt->HasMoreResults() ) { if( auto_stmt->HasRows() ) { auto_ptr<IResultSet> rs( auto_stmt->GetResultSet() ); switch ( rs->GetResultType() ) { case eDB_RowResult: while( rs->Next() ) { // int col1 = rs->GetVariant(1).GetInt4(); } break; case eDB_ParamResult: while( rs->Next() ) { // int col1 = rs->GetVariant(1).GetInt4(); } break; case eDB_StatusResult: while( rs->Next() ) { int status = rs->GetVariant(1).GetInt4(); status = status; } break; case eDB_ComputeResult: case eDB_CursorResult: break; } } } // Same as before but do not retrieve data ... auto_stmt->SendSql( "exec sp_databases" ); auto_stmt->SendSql( "exec sp_databases" ); } // Test ICallableStatement // No parameters at this time. { // Execute it first time ... auto_ptr<ICallableStatement> auto_stmt( GetConnection().GetCallableStatement("sp_databases") ); auto_stmt->Execute(); while(auto_stmt->HasMoreResults()) { if( auto_stmt->HasRows() ) { auto_ptr<IResultSet> rs( auto_stmt->GetResultSet() ); switch( rs->GetResultType() ) { case eDB_RowResult: while(rs->Next()) { // retrieve row results } break; case eDB_ParamResult: while(rs->Next()) { // Retrieve parameter row } break; default: break; } } } // Get status auto_stmt->GetReturnStatus(); // Execute it second time ... auto_stmt.reset( GetConnection().GetCallableStatement("sp_databases") ); auto_stmt->Execute(); while(auto_stmt->HasMoreResults()) { if( auto_stmt->HasRows() ) { auto_ptr<IResultSet> rs( auto_stmt->GetResultSet() ); switch( rs->GetResultType() ) { case eDB_RowResult: while(rs->Next()) { // retrieve row results } break; case eDB_ParamResult: while(rs->Next()) { // Retrieve parameter row } break; default: break; } } } // Get status auto_stmt->GetReturnStatus(); // Same as before but do not retrieve data ... auto_stmt.reset( GetConnection().GetCallableStatement("sp_databases") ); auto_stmt->Execute(); auto_stmt.reset( GetConnection().GetCallableStatement("sp_databases") ); auto_stmt->Execute(); } // Temporary test ... // !!! This is a bug ... if (false) { auto_ptr<IConnection> conn( GetDS().CreateConnection( CONN_OWNERSHIP ) ); BOOST_CHECK( conn.get() != NULL ); conn->Connect( "anyone", "allowed", "PUBSEQ_OS_LXA", "" ); auto_ptr<ICallableStatement> auto_stmt( conn->GetCallableStatement("id_seqid4gi") ); auto_stmt->SetParam( CVariant(1), "@gi" ); auto_stmt->Execute(); while(auto_stmt->HasMoreResults()) { if( auto_stmt->HasRows() ) { auto_ptr<IResultSet> rs( auto_stmt->GetResultSet() ); switch( rs->GetResultType() ) { case eDB_RowResult: while(rs->Next()) { // retrieve row results } break; case eDB_ParamResult: _ASSERT(false); while(rs->Next()) { // Retrieve parameter row } break; default: break; } } } // Get status int status = auto_stmt->GetReturnStatus(); status = status; // Get rid of warnings. } if (false) { const string query("[db_alias] is not null"); auto_ptr<IConnection> conn( GetDS().CreateConnection( CONN_OWNERSHIP ) ); BOOST_CHECK( conn.get() != NULL ); conn->Connect( "*****", "******", "MSSQL31", "AlignDb_Info" ); auto_ptr<ICallableStatement> auto_stmt( conn->PrepareCall("[dbo].[FindAttributesEx]") ); auto_stmt->SetParam( CVariant(1), "@userid" ); auto_stmt->SetParam( CVariant("ALIGNDB"), "@application" ); auto_stmt->SetParam( CVariant("AlignDbMasterInfo"), "@classname" ); // LongChar doesn't work. // auto_stmt->SetParam( CVariant(new CDB_LongChar(query.length(), query)), "@query" ); // auto_stmt->SetParam( CVariant::LongChar(query.data(), query.length()), "@query" ); auto_stmt->SetParam( CVariant(query), "@query" ); auto_stmt->SetParam( CVariant(1), "@max_results" ); auto_stmt->Execute(); while(auto_stmt->HasMoreResults()) { if( auto_stmt->HasRows() ) { auto_ptr<IResultSet> rs( auto_stmt->GetResultSet() ); switch( rs->GetResultType() ) { case eDB_RowResult: while(rs->Next()) { // retrieve row results } break; case eDB_ParamResult: _ASSERT(false); while(rs->Next()) { // Retrieve parameter row } break; default: break; } } } // Get status int status = auto_stmt->GetReturnStatus(); status = status; // Get rid of warnings. } // Test returned recordset ... { // In case of MS SQL 2005 sp_databases returns empty result set. // It is not a bug. It is a difference in setiings for MS SQL // 2005. if (GetArgs().GetServerType() != CDBConnParams::eMSSqlServer) { int num = 0; // Execute it first time ... auto_ptr<ICallableStatement> auto_stmt( GetConnection().GetCallableStatement("sp_databases") ); auto_stmt->Execute(); BOOST_CHECK(auto_stmt->HasMoreResults()); BOOST_CHECK(auto_stmt->HasRows()); auto_ptr<IResultSet> rs(auto_stmt->GetResultSet()); BOOST_CHECK(rs.get() != NULL); while (rs->Next()) { BOOST_CHECK(rs->GetVariant(1).GetString().size() > 0); BOOST_CHECK(rs->GetVariant(2).GetInt4() > 0); BOOST_CHECK_EQUAL(rs->GetVariant(3).IsNull(), true); ++num; } BOOST_CHECK(num > 0); DumpResults(auto_stmt.get()); } { int num = 0; auto_ptr<ICallableStatement> auto_stmt( GetConnection().GetCallableStatement("sp_server_info") ); auto_stmt->Execute(); BOOST_CHECK(auto_stmt->HasMoreResults()); BOOST_CHECK(auto_stmt->HasRows()); auto_ptr<IResultSet> rs(auto_stmt->GetResultSet()); BOOST_CHECK(rs.get() != NULL); while (rs->Next()) { BOOST_CHECK(rs->GetVariant(1).GetInt4() > 0); BOOST_CHECK(rs->GetVariant(2).GetString().size() > 0); BOOST_CHECK(rs->GetVariant(3).GetString().size() > 0); ++num; } BOOST_CHECK(num > 0); DumpResults(auto_stmt.get()); } } // Test ICallableStatement // With parameters. { { auto_ptr<ICallableStatement> auto_stmt( GetConnection().GetCallableStatement("sp_server_info") ); // Set parameter to NULL ... auto_stmt->SetParam( CVariant(eDB_Int), "@attribute_id" ); auto_stmt->Execute(); if (GetArgs().GetServerType() == CDBConnParams::eSybaseSQLServer) { BOOST_CHECK_EQUAL( size_t(30), GetNumOfRecords(auto_stmt) ); } else { BOOST_CHECK_EQUAL( size_t(29), GetNumOfRecords(auto_stmt) ); } // Set parameter to 1 ... auto_stmt->SetParam( CVariant( Int4(1) ), "@attribute_id" ); auto_stmt->Execute(); BOOST_CHECK_EQUAL( size_t(1), GetNumOfRecords(auto_stmt) ); } // NULL value with CVariant ... { auto_ptr<ICallableStatement> auto_stmt( GetConnection().GetCallableStatement("sp_statistics") ); auto_stmt->SetParam(CVariant((const char*) NULL), "@table_name"); auto_stmt->Execute(); DumpResults(auto_stmt.get()); } // Doesn't work for some reason ... if (false) { // Execute it first time ... auto_ptr<ICallableStatement> auto_stmt( GetConnection().GetCallableStatement("sp_statistics") ); auto_stmt->SetParam(CVariant(GetTableName()), "@table_name"); auto_stmt->Execute(); { BOOST_CHECK(auto_stmt->HasMoreResults()); BOOST_CHECK(auto_stmt->HasRows()); auto_ptr<IResultSet> rs(auto_stmt->GetResultSet()); BOOST_CHECK(rs.get() != NULL); BOOST_CHECK(rs->Next()); DumpResults(auto_stmt.get()); } // Execute it second time ... auto_stmt->SetParam(CVariant("#bulk_insert_table"), "@table_name"); auto_stmt->Execute(); { BOOST_CHECK(auto_stmt->HasMoreResults()); BOOST_CHECK(auto_stmt->HasRows()); auto_ptr<IResultSet> rs(auto_stmt->GetResultSet()); BOOST_CHECK(rs.get() != NULL); BOOST_CHECK(rs->Next()); DumpResults(auto_stmt.get()); } } if (false) { auto_ptr<ICallableStatement> auto_stmt( GetConnection().GetCallableStatement("DBAPI_Sample..TestBigIntProc") ); auto_stmt->SetParam(CVariant(Int8(1234567890)), "@num"); auto_stmt->ExecuteUpdate(); } } // Test output parameters ... if (false) { CRef<CDB_UserHandler_Diag> handler(new CDB_UserHandler_Diag()); I_DriverContext* drv_context = GetDS().GetDriverContext(); drv_context->PushDefConnMsgHandler(handler); auto_ptr<ICallableStatement> auto_stmt( GetConnection().GetCallableStatement("DBAPI_Sample..SampleProc3") ); auto_stmt->SetParam(CVariant(1), "@id"); auto_stmt->SetParam(CVariant(2.0), "@f"); auto_stmt->SetOutputParam(CVariant(eDB_Int), "@o"); auto_stmt->Execute(); // auto_stmt->SendSql( "exec DBAPI_Sample..TestProc4 @test_out output" ); while(auto_stmt->HasMoreResults()) { if( auto_stmt->HasRows() ) { auto_ptr<IResultSet> rs( auto_stmt->GetResultSet() ); switch( rs->GetResultType() ) { case eDB_RowResult: while(rs->Next()) { // retrieve row results } break; case eDB_ParamResult: BOOST_CHECK(rs->Next()); NcbiCout << "Output param: " << rs->GetVariant(1).GetInt4() << endl; break; case eDB_ComputeResult: break; case eDB_StatusResult: break; case eDB_CursorResult: break; default: break; } } } // BOOST_CHECK(auto_stmt->HasMoreResults()); // BOOST_CHECK(auto_stmt->HasRows()); // auto_ptr<IResultSet> rs(auto_stmt->GetResultSet()); // BOOST_CHECK(rs.get() != NULL); // // while (rs->Next()) { // BOOST_CHECK(rs->GetVariant(1).GetString().size() > 0); // BOOST_CHECK(rs->GetVariant(2).GetInt4() > 0); // BOOST_CHECK_EQUAL(rs->GetVariant(3).IsNull(), true); // ++num; // } // // BOOST_CHECK(num > 0); DumpResults(auto_stmt.get()); drv_context->PopDefConnMsgHandler(handler); } // Temporary test ... if (false) { auto_ptr<IConnection> conn( GetDS().CreateConnection( CONN_OWNERSHIP ) ); BOOST_CHECK( conn.get() != NULL ); conn->Connect( "anyone", "allowed", "", "GenomeHits" ); auto_ptr<ICallableStatement> auto_stmt( conn->GetCallableStatement("NewSub") ); auto_stmt->SetParam(CVariant("tsub2"), "@name"); auto_stmt->SetParam(CVariant("tst"), "@center"); auto_stmt->SetParam(CVariant("9606"), "@taxid"); auto_stmt->SetParam(CVariant("H**o sapiens"), "@organism"); auto_stmt->SetParam(CVariant(""), "@notes"); auto_stmt->Execute(); while(auto_stmt->HasMoreResults()) { if( auto_stmt->HasRows() ) { auto_ptr<IResultSet> rs( auto_stmt->GetResultSet() ); switch( rs->GetResultType() ) { case eDB_RowResult: while(rs->Next()) { // retrieve row results } break; case eDB_ParamResult: _ASSERT(false); while(rs->Next()) { // Retrieve parameter row } break; default: break; } } } // Get status int status = auto_stmt->GetReturnStatus(); status = status; // Get rid of warnings. } // Temporary test ... if (false && GetArgs().GetServerType() != CDBConnParams::eSybaseSQLServer) { auto_ptr<IConnection> conn( GetDS().CreateConnection( CONN_OWNERSHIP ) ); BOOST_CHECK( conn.get() != NULL ); conn->Connect( "pmcupdate", "*******", "PMC3QA", "PMC3QA" ); auto_ptr<ICallableStatement> auto_stmt( conn->PrepareCall("id_new_id") ); auto_stmt->SetParam(CVariant("tsub2"), "@IdName"); auto_stmt->Execute(); while(auto_stmt->HasMoreResults()) { if( auto_stmt->HasRows() ) { auto_ptr<IResultSet> rs( auto_stmt->GetResultSet() ); switch( rs->GetResultType() ) { case eDB_RowResult: while(rs->Next()) { // retrieve row results } break; case eDB_ParamResult: _ASSERT(false); while(rs->Next()) { // Retrieve parameter row } break; default: break; } } } // Get status int status = auto_stmt->GetReturnStatus(); status = status; // Get rid of warnings. } } catch(const CException& ex) { DBAPI_BOOST_FAIL(ex); } }
void CKadHandler::HandleCryptoRequest(const CVariant& Request, CKadNode* pNode, CComChannel* pChannel) { SKadData* pData = pChannel->GetData<SKadData>(); if(GetParent<CKademlia>()->Cfg()->GetBool("DebugTL")) LogLine(LOG_DEBUG, L"Recived 'CryptoRequest' to %s", pNode->GetID().ToHex().c_str()); CVariant Response(CVariant::EMap); CScoped<CSymmetricKey> pCryptoKey; try { CScoped<CAbstractKey> pSessionKey; if(Request.Has("EK")) { UINT eExAlgorithm = 0; string Param; if(Request.Has("KA")) eExAlgorithm = ParseKA(Request["KA"], Param); UINT eAlgorithm = PrepKA(eExAlgorithm, Param); CScoped<CKeyExchange> pKeyExchange = NewKeyExchange(eAlgorithm, Param); CScoped<CAbstractKey> pKey = pKeyExchange->InitialsieKeyExchange(); Response["EK"] = CVariant(pKey->GetKey(), pKey->GetSize()); CScoped<CAbstractKey> pRemKey = new CAbstractKey(Request["EK"].GetData(), Request["EK"].GetSize()); pSessionKey = pKeyExchange->FinaliseKeyExchange(pRemKey); if(!pSessionKey) throw "ExchangeFailed"; bool bAuthenticated = Request.IsSigned(); if(bAuthenticated) { CPublicKey* pPubKey; if(Request.Has("PK")) pPubKey = pNode->SetIDKey(Request); else pPubKey = pNode->GetID().GetKey(); if(!pPubKey) throw "MissingKey"; if(!Request.Verify(pPubKey)) throw "InvalidSign"; } pData->Authenticated = bAuthenticated; pNode->SetTrustedKey(new CTrustedKey(pSessionKey->GetKey(), pSessionKey->GetSize(), bAuthenticated, eAlgorithm & CAbstractKey::eHashFunkt)); } else if(Request.Has("FP")) { CTrustedKey* pTrustedKey = pNode->GetTrustedKey(); if(!pTrustedKey || pTrustedKey->GetFingerPrint() != Request["FP"].To<uint64>()) throw "UnknownKey"; pData->Authenticated = pTrustedKey->IsAuthenticated(); pSessionKey = new CAbstractKey(pTrustedKey->GetKey(), pTrustedKey->GetSize()); } else throw "UnknownMethod"; if(Request.Has("IV")) { CAbstractKey OutIV(KEY_128BIT, true); Response["IV"] = CVariant(OutIV.GetKey(), OutIV.GetSize()); UINT eSymAlgorithm = 0; if(Request.Has("SC")) eSymAlgorithm = ParseSC(Request["SC"]); UINT eAlgorithm = PrepSC(eSymAlgorithm); CAbstractKey InIV(Request["IV"].GetData(), Request["IV"].GetSize()); pCryptoKey = NewSymmetricKey(eAlgorithm, pSessionKey, &InIV, &OutIV); } if(Request.IsSigned() && pData->Authenticated) { const CMyKadID& MyID = GetParent<CKademlia>()->Root()->GetID(); Response["PK"] = CVariant(MyID.GetKey()->GetKey(), MyID.GetKey()->GetSize()); if((MyID.GetKey()->GetAlgorithm() & CAbstractKey::eHashFunkt) != 0) Response["HK"] = CAbstractKey::Algorithm2Str((MyID.GetKey()->GetAlgorithm() & CAbstractKey::eHashFunkt)); Response.Sign(MyID.GetPrivateKey()); } } catch(const char* Error) { delete pCryptoKey.Detache(); Response["ERR"] = Error; } if(GetParent<CKademlia>()->Cfg()->GetBool("DebugTL")) LogLine(LOG_DEBUG, L"Sending 'CryptoResponse' to %s", pNode->GetID().ToHex().c_str()); pChannel->SendPacket(KAD_CRYPTO_RESPONSE, Response); if(pCryptoKey) { pChannel->SetQueueLock(true); pChannel->Encrypt(pCryptoKey.Detache()); pChannel->SetQueueLock(false); } }