204 lines
7.7 KiB
C++
204 lines
7.7 KiB
C++
|
|
|
|
#include "GrantleeWrappers.h"
|
|
|
|
#include <NGLLogger/NGLLoggerBootstrap.h>
|
|
#include <utility>
|
|
|
|
using namespace CodeGen;
|
|
|
|
#ifdef RTI_HEADER_AVAILABLE
|
|
QString IdlStructWrapper::extensibility2String(dds::core::xtypes::ExtensibilityKind extensiblity)
|
|
{
|
|
switch (extensiblity.underlying())
|
|
{
|
|
case dds::core::xtypes::ExtensibilityKind_def::type::FINAL:
|
|
return QStringLiteral("FINAL_EXTENSIBILITY");
|
|
case dds::core::xtypes::ExtensibilityKind_def::type::MUTABLE:
|
|
return QStringLiteral("MUTABLE_EXTENSIBILITY");
|
|
case dds::core::xtypes::ExtensibilityKind_def::type::EXTENSIBLE:
|
|
return QStringLiteral("EXTENSIBLE_EXTENSIBILITY");
|
|
}
|
|
Q_ASSERT_X(false, "IdlStructWrapper::extensibility2String", "Unknown extensiblity kind");
|
|
return QString();
|
|
}
|
|
#else
|
|
QString IdlStructWrapper::extensibility2String(int /*extensiblity*/)
|
|
{
|
|
// RTI not available - return placeholder
|
|
return QStringLiteral("EXTENSIBLE_EXTENSIBILITY");
|
|
}
|
|
#endif
|
|
|
|
QString DataTopicWrapper::typeFq() const
|
|
{
|
|
const QString& t = type();
|
|
const QString& s = serviceName();
|
|
if (s.isEmpty())
|
|
return t;
|
|
if (t.startsWith(s))
|
|
return t;
|
|
return s + QStringLiteral("::") + t;
|
|
}
|
|
|
|
QString MemberInformationWrapper::ddsToQVariant() const
|
|
{
|
|
return QStringLiteral("toQVariant(data.") + cppMemberName() + QStringLiteral("())");
|
|
}
|
|
|
|
QString MemberInformationWrapper::qVariantToDds() const
|
|
{
|
|
const QString ddsType =
|
|
isOptional() ? (QStringLiteral("dds::core::optional<") + cppDdsType() + QLatin1Char('>')) : (cppDdsType());
|
|
|
|
return QStringLiteral("data.") + cppMemberName() + QStringLiteral("() = fromQVariant<") + ddsType +
|
|
QStringLiteral(">(q.value(i++))");
|
|
}
|
|
|
|
QString MemberInformationWrapper::ddsToJsonElement() const
|
|
{
|
|
|
|
if (cppMemberName().isEmpty())
|
|
return QStringLiteral("obj[QStringLiteral(\"") + cppType() + QStringLiteral("\")] = element;");
|
|
|
|
const QString memberAccess = isCppStruct() ? cppMemberName() : cppMemberName() + QStringLiteral("()");
|
|
if (!isOptional())
|
|
return QStringLiteral("obj[QStringLiteral(\"") + cppMemberName() + QStringLiteral("\")] = ") + cppToJsonPre() +
|
|
QStringLiteral("element.") + memberAccess + cppToJsonPost();
|
|
|
|
const QString optionalAccess =
|
|
isCppStruct() ? cppMemberName() + QStringLiteral(".is_set()") : cppMemberName() + QStringLiteral("().is_set()");
|
|
const QString getValue = isCppStruct() ? cppMemberName() : cppMemberName() + QStringLiteral("()");
|
|
return QStringLiteral("if (element.") + optionalAccess + QStringLiteral(") ") +
|
|
QStringLiteral("obj[QStringLiteral(\"") + cppMemberName() + QStringLiteral("\")] = ") + cppToJsonPre() +
|
|
QStringLiteral("*element.") + getValue + cppToJsonPost();
|
|
}
|
|
|
|
QString MemberInformationWrapper::jsonElementToDds() const
|
|
{
|
|
|
|
if (cppMemberName().isEmpty())
|
|
return QStringLiteral("element = ") + cppFromQvPre2() + QStringLiteral("objElement.value(QStringLiteral(\"") +
|
|
cppType() + QStringLiteral("\"))") + cppFromJsonPost();
|
|
|
|
QString ret(QStringLiteral("const QJsonValue val(objElement.value(QStringLiteral(\"") + cppMemberName() +
|
|
QStringLiteral("\"))); "));
|
|
if (isOptional())
|
|
ret += QStringLiteral("if (!val.isNull()) ");
|
|
|
|
if (isCppStruct())
|
|
ret += QStringLiteral("element.") + cppMemberName() + QStringLiteral(" = ") + cppFromJsonPre2() +
|
|
QStringLiteral("val") + cppFromJsonPost();
|
|
else
|
|
ret += QStringLiteral("element.") + cppMemberName() + QStringLiteral("(") + cppFromJsonPre2() +
|
|
QStringLiteral("val") + cppFromJsonPost() + QStringLiteral(")");
|
|
|
|
return ret;
|
|
}
|
|
|
|
QString MemberInformationWrapper::dbToJsonElement() const
|
|
{
|
|
|
|
if (cppMemberName().isEmpty())
|
|
return QStringLiteral("obj[QStringLiteral(\"") + cppType() + QStringLiteral("\")] = element;");
|
|
|
|
if (isOptional() || cppType() == QStringLiteral("QDateTime"))
|
|
{
|
|
QString ret = QStringLiteral("if (") +
|
|
((cppType() == QStringLiteral("QDateTime")) ? QString() : QStringLiteral("!")) +
|
|
QStringLiteral("element.") + cppMemberName() +
|
|
((cppType() == QStringLiteral("QDateTime")) ? QStringLiteral(".isValid()) ")
|
|
: QStringLiteral(".isNull()) ")) +
|
|
QStringLiteral("obj[QStringLiteral(\"") + cppMemberName() + QStringLiteral("\")] = ");
|
|
if (isOptional())
|
|
return ret += cppFromQvPre2() + QStringLiteral("element.") + cppMemberName() + cppFromQvPost();
|
|
}
|
|
|
|
return QStringLiteral("obj[QStringLiteral(\"") + cppMemberName() + QStringLiteral("\")] = ") + cppToJsonPre() +
|
|
QStringLiteral("element.") + cppMemberName() + cppToJsonPost();
|
|
}
|
|
|
|
QString MemberInformationWrapper::jsonElementToDb() const
|
|
{
|
|
|
|
if (cppMemberName().isEmpty())
|
|
return QStringLiteral("element = ") + cppFromQvPre2() + QStringLiteral("objElement.value(QStringLiteral(\"") +
|
|
cppType() + QStringLiteral("\"))") + cppFromJsonPost();
|
|
|
|
QString ret(QStringLiteral("const QJsonValue val(objElement.value(QStringLiteral(\"") + cppMemberName() +
|
|
QStringLiteral("\"))); "));
|
|
if (isOptional())
|
|
ret += QStringLiteral("if (!val.isUndefined()) ");
|
|
|
|
ret += QStringLiteral("element.") + cppMemberName() + QStringLiteral(" = ") + cppFromJsonPre2() +
|
|
QStringLiteral("val") + cppFromJsonPost();
|
|
|
|
return ret;
|
|
}
|
|
|
|
QVariantList DbStructWrapper::insertStructures()
|
|
{
|
|
|
|
QStringList tableNames;
|
|
QMap<QString, QVector<MemberInformationWrapper*>> structures;
|
|
QMap<QString, MemberInformationWrapper*> primaryKeys;
|
|
QMap<QString, MemberInformationWrapper*> fkComplexTableKeys;
|
|
|
|
tableNames.reserve(5);
|
|
for (const QVariant& v : std::as_const(m_member))
|
|
{
|
|
MemberInformationWrapper* mi = v.value<MemberInformationWrapper*>();
|
|
const QString& tableName = mi->info().db.table;
|
|
if (mi->info().db.usedForInsert)
|
|
{
|
|
auto& tnStructure = structures[tableName];
|
|
tnStructure.append(mi);
|
|
if (!tableNames.contains(tableName))
|
|
tableNames.push_back(tableName);
|
|
}
|
|
if (mi->info().fk)
|
|
fkComplexTableKeys[mi->info().fk->destination.tableName] = mi;
|
|
if (mi->info().db.isPrimaryKey)
|
|
{
|
|
if (!primaryKeys.contains(tableName))
|
|
primaryKeys[tableName] = mi;
|
|
}
|
|
if (mi->info().db.isAssignmentForPrimaryTablePk)
|
|
fkComplexTableKeys[mi->info().db.table] = mi;
|
|
}
|
|
|
|
QVariantList vl;
|
|
vl.reserve(tableNames.size());
|
|
for (int i = 0; i < tableNames.size(); ++i)
|
|
{
|
|
const QString& tn = tableNames.at(i);
|
|
MemberInformationWrapper* serialMember = fkComplexTableKeys.value(tn);
|
|
MemberInformationWrapper* baseId = nullptr;
|
|
MemberInformationWrapper* primaryKey = primaryKeys.value(tn);
|
|
if (i == 0 && tableNames.size() > 1)
|
|
baseId = primaryKeys.value(tn);
|
|
|
|
if (tableNames.size() == 1)
|
|
{
|
|
baseId = nullptr;
|
|
|
|
if (!primaryKey)
|
|
{
|
|
NGL::INGLLogger& logger = NGL::NGLLoggerBootstrap::getNGLRootLogger().getNGLLogger("DbStructWrapper");
|
|
WARNING_LOG(logger) << "Table " << tn << " has no primary key!";
|
|
}
|
|
else
|
|
{
|
|
if (primaryKey->dbHasSequence())
|
|
serialMember = primaryKey;
|
|
}
|
|
}
|
|
|
|
const QVector<MemberInformationWrapper*> members = structures.value(tn);
|
|
TableWrapper* tw = new TableWrapper(tn, members, serialMember, baseId, primaryKey, this);
|
|
vl += QVariant::fromValue(tw);
|
|
}
|
|
|
|
return vl;
|
|
}
|