Files
speckle-cpp-connectors/SpeckleLib/Speckle/Database/AccountDatabase.cpp
T
Ralph Wessel e148094c81 ModelCardDatabase can retrieve a card by ID
Added numerous Send argument classes:
- ConversionResult (incomplete)
- SendError
- SendObject
- SendViaBrowserArgs (incomplete)
Send method looks up model card and account details, constructs send arguments
AccountDatabase can find an account by ID or server URL
Added DetachedMemoryStore
2024-10-01 17:24:59 +01:00

113 lines
4.1 KiB
C++

#include "Active/Database/Storage/SQLite/SQLiteEngine.h"
#include "Active/Database/Storage/Storage.h"
#include "Active/Serialise/JSON/JSONTransport.h"
#include "Active/Setting/ValueSetting.h"
#include "Active/Setting/Values/StringValue.h"
#include "Speckle/Database/AccountDatabase.h"
using namespace active::container;
using namespace active::database;
using namespace active::serialise::json;
using namespace active::setting;
using namespace speckle::record::cred;
using namespace speckle::database;
using namespace speckle::utility;
namespace {
//Account field indices
enum Fields {
hashID = 0,
contentID,
};
///Internal name of the accounts dbase
const char* accountsDBaseName = "accounts";
///Accounts table name
const char* accountsTableName = "objects";
///Hash field name
const char* hashFieldName = "hash";
///Content field name
const char* contentFieldName = "content";
}
namespace speckle::database {
///Accounts database engine declaration
using AccountsEngine = SQLiteEngine<Account, Account, JSONTransport, active::utility::String, active::utility::String>;
///Accounts database storage declaration
class AccountDatabase::Store : public active::database::Storage<speckle::record::cred::Account, active::serialise::json::JSONTransport,
active::utility::String, active::utility::String, active::utility::String, active::utility::String> {
using base = active::database::Storage<speckle::record::cred::Account, active::serialise::json::JSONTransport,
active::utility::String, active::utility::String, active::utility::String, active::utility::String>;
using base::base;
};
}
/*--------------------------------------------------------------------
Constructor
path: Path to the database file
--------------------------------------------------------------------*/
AccountDatabase::AccountDatabase(const active::file::Path& path) {
//Create accounts database storage (with schema)
m_store = std::make_unique<Store>(
//Engine
std::make_shared<AccountsEngine>(path,
//Schema
DBaseSchema{active::utility::String{accountsDBaseName},
//Tables
{
//Account table
{
accountsTableName, Fields::hashID, Fields::contentID, {
ValueSetting{StringValue{}, hashFieldName},
ValueSetting{StringValue{}, contentFieldName},
}
}
}
}
)
);
} //AccountDatabase::AccountDatabase
/*--------------------------------------------------------------------
Destructor
--------------------------------------------------------------------*/
AccountDatabase::~AccountDatabase() {}
/*--------------------------------------------------------------------
Get a specified account. NB: The server URL is provided as a fallback for the search if the specified accountID is not found
accountID: The account ID (the primary search field)
serverURL: The server URL (a fallback search field if the account ID does not exist)
return: The requested account (nullptr on failure)
--------------------------------------------------------------------*/
std::unique_ptr<Account> AccountDatabase::getAccount(const String& accountID, const String& serverURL) const {
//First attempt to find a matching account ID
auto matchingAccount = m_store->getObjects([&accountID](const auto& acc) { return acc.getID() == accountID; });
if (!matchingAccount.empty())
return matchingAccount.release(matchingAccount.begin());
//Alternatively seek an account with a matching server URL
matchingAccount = m_store->getObjects([&serverURL](const auto& acc) { return acc.getServerURL() == serverURL; });
if (!matchingAccount.empty())
return matchingAccount.release(matchingAccount.begin());
return nullptr;
} //AccountDatabase::getAccount
/*--------------------------------------------------------------------
Get all accounts
return: All the accounts
--------------------------------------------------------------------*/
Vector<Account> AccountDatabase::getAccounts() const {
return m_store->getObjects();
} //AccountDatabase::getAccounts