revisited TinyBuilder

- better moves
 - early returns if size is 0
 - better reserve
This commit is contained in:
silverqx
2022-07-30 15:06:53 +02:00
parent 6509db48ea
commit 739ad82979
9 changed files with 35 additions and 27 deletions
+1
View File
@@ -49,6 +49,7 @@ namespace Orm::Constants
SHAREDLIB_EXPORT extern const QString UPDATED_AT;
SHAREDLIB_EXPORT extern const QString PARENTH_ONE;
SHAREDLIB_EXPORT extern const QString NEWLINE;
SHAREDLIB_EXPORT extern const QString DOT_IN;
SHAREDLIB_EXPORT extern const QString SPACE_IN;
SHAREDLIB_EXPORT extern const QString NOSPACE;
SHAREDLIB_EXPORT extern const QString EMPTY;
+1
View File
@@ -48,6 +48,7 @@ namespace Orm::Constants
inline const QString UPDATED_AT = QStringLiteral("updated_at");
inline const QString PARENTH_ONE = QStringLiteral("(%1)");
inline const QString NEWLINE = QStringLiteral("\n");
inline const QString DOT_IN = QStringLiteral("%1.%2");
inline const QString SPACE_IN = QStringLiteral("%1 %2");
inline const QString NOSPACE = QStringLiteral("%1%2");
inline const QString EMPTY = QLatin1String("");
@@ -513,8 +513,7 @@ namespace Concerns
localKey = basemodel().getKeyName();
return newHasOne<Related>(std::move(instance), model(),
QStringLiteral("%1.%2").arg(instance->getTable(),
foreignKey),
DOT_IN.arg(instance->getTable(), foreignKey),
localKey);
}
@@ -565,8 +564,7 @@ namespace Concerns
localKey = basemodel().getKeyName();
return newHasMany<Related>(std::move(instance), model(),
QStringLiteral("%1.%2").arg(instance->getTable(),
foreignKey),
DOT_IN.arg(instance->getTable(), foreignKey),
localKey);
}
+1 -1
View File
@@ -1008,7 +1008,7 @@ namespace Orm::Tiny
if (column.contains(DOT))
return column;
return QStringLiteral("%1.%2").arg(model().getTable(), column);
return DOT_IN.arg(model().getTable(), column);
}
/* protected */
+2 -2
View File
@@ -197,7 +197,7 @@ namespace Orm::Tiny::Relations
of the related models matching on the foreign key that's on a parent. */
const auto &table = this->m_related->getTable();
this->m_query->where(QStringLiteral("%1.%2").arg(table, m_ownerKey), EQ,
this->m_query->where(DOT_IN.arg(table, m_ownerKey), EQ,
m_child.getAttribute(m_foreignKey));
}
@@ -209,7 +209,7 @@ namespace Orm::Tiny::Relations
a non-standard name and not "id". We will then construct the constraint for
our eagerly loading query so it returns the proper models from execution. */
this->m_query->getQuery().whereIn(
QStringLiteral("%1.%2").arg(this->m_related->getTable(), m_ownerKey),
DOT_IN.arg(this->m_related->getTable(), m_ownerKey),
getEagerModelKeys(models));
}
+1 -1
View File
@@ -627,7 +627,7 @@ namespace Orm::Tiny::Relations
if (column.contains(DOT))
return column;
return QStringLiteral("%1.%2").arg(m_table, column);
return DOT_IN.arg(m_table, column);
}
template<class Model, class Related, class PivotType>
+24 -18
View File
@@ -921,7 +921,7 @@ namespace Orm::Tiny
the relationship with its own key in the vector of eager-load names. */
addNestedWiths(relation.name, results);
results.append(std::move(relation));
results << std::move(relation);
}
return results;
@@ -934,48 +934,51 @@ namespace Orm::Tiny
auto relation = splitted.at(0).trimmed();
auto &columns = splitted[1];
/* Get the Related model table name if the relation is BelongsToMany, otherwise
return an empty std::optional. */
auto belongsToManyRelatedTable =
m_model.getRelatedTableForBelongsToManyWithVisitor(relation);
// Move the relation and also values to the lambda, to avoid dangling references
return {
std::move(relation),
[columns = std::move(columns),
belongsToManyRelatedTable = std::move(belongsToManyRelatedTable)]
belongsToManyRelatedTable = std::move(belongsToManyRelatedTable)]
(auto &query)
{
auto columnsSplitted = columns.split(COMMA_C, Qt::SkipEmptyParts);
// Nothing to do
if (columnsSplitted.isEmpty())
return;
QVector<Column> columnsList;
columnsList.reserve(columns.count(COMMA_C) + 1);
columnsList.reserve(columnsSplitted.size());
// Avoid 'clazy might detach' warning
for (const auto columns_ = columns.split(COMMA_C);
auto column : columns_)
for (auto &&column : columnsSplitted)
{
column = column.trimmed();
column = std::move(column).trimmed();
// Fully qualified column passed, not needed to process
// Nothing to process (already a fully qualified column name)
if (column.contains(DOT)) {
columnsList << std::move(column);
continue;
}
/* Generate fully qualified column name for the BelongsToMany
/* Generate the fully qualified column name for the BelongsToMany
relation. */
if (belongsToManyRelatedTable) {
#ifdef __GNUG__
columnsList << QString("%1.%2")
.arg(*belongsToManyRelatedTable, column);
#else
columnsList << QStringLiteral("%1.%2")
.arg(*belongsToManyRelatedTable, column);
#endif
columnsList << DOT_IN.arg(*belongsToManyRelatedTable, column);
continue;
}
// All other column names are unqualified (it's ok)
columnsList << std::move(column);
}
// TODO move, query.select() silverqx
query.select(std::move(columnsList));
query.select(columnsList);
}
};
}
@@ -984,13 +987,16 @@ namespace Orm::Tiny
void Builder<Model>::addNestedWiths(const QString &name,
QVector<WithItem> &results) const
{
QStringList progress;
/* If the relation has already been set on the result vector, we will not set it
again, since that would override any constraints that were already placed
on the relationships. We will only set the ones that are not specified. */
auto names = name.split(DOT, Qt::SkipEmptyParts, Qt::CaseSensitive);
// Nothing to do (no nested relations)
if (names.isEmpty())
return;
QStringList progress;
progress.reserve(names.size());
for (auto &&segment : names) {
+1
View File
@@ -38,6 +38,7 @@ namespace Orm::Constants
const QString UPDATED_AT = QStringLiteral("updated_at");
const QString PARENTH_ONE = QStringLiteral("(%1)");
const QString NEWLINE = QStringLiteral("\n");
const QString DOT_IN = QStringLiteral("%1.%2");
const QString SPACE_IN = QStringLiteral("%1 %2");
const QString NOSPACE = QStringLiteral("%1%2");
const QString EMPTY = QLatin1String("");
+2 -1
View File
@@ -347,6 +347,7 @@ bool Builder::doesntExistOr(const std::function<void()> &callback)
return false;
}
// TODO move, select() silverqx
Builder &Builder::select(const QVector<Column> &columns)
{
clearColumns();
@@ -1284,7 +1285,7 @@ Builder &Builder::prependDatabaseNameIfCrossDatabaseQuery(Builder &query) const
!queryFrom.startsWith(queryDatabaseName) &&
!queryFrom.contains(DOT)
)
query.from(QStringLiteral("%1.%2").arg(queryDatabaseName, std::move(queryFrom)));
query.from(DOT_IN.arg(queryDatabaseName, std::move(queryFrom)));
return query;
}