Skip to content

Commit a3166c4

Browse files
committed
Fix broken join feature
Added a Row<Table> property to each table object to being able to pass a QVariant encapsulated Row<Table> through the QObject::setProperty in the toList method.
1 parent 2f43954 commit a3166c4

5 files changed

Lines changed: 45 additions & 22 deletions

File tree

src/defines.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public slots:
100100
keytype m_##name##Id; \
101101
Q_PROPERTY(Nut::Row<type> name READ read WRITE write) \
102102
Q_PROPERTY(keytype name##Id READ read##Id WRITE write##Id) \
103+
Q_PROPERTY(Nut::Row<Table> _##name READ _##read WRITE _##write) \
103104
public: \
104105
Nut::Row<type> read() const; \
105106
keytype read##Id() const; \
@@ -109,9 +110,11 @@ public: \
109110
(staticMetaObject.className(), #name); \
110111
return f; \
111112
} \
112-
public slots: \
113113
void write(Nut::Row<type> name); \
114-
void write##Id(keytype name##Id);
114+
void write##Id(keytype name##Id); \
115+
private: \
116+
Nut::Row<Table> _##read() const; \
117+
void _##write(Nut::Row<Table> name);
115118

116119
#define NUT_FOREIGN_KEY_IMPLEMENT(class, type, keytype, name, read, write) \
117120
\
@@ -132,6 +135,13 @@ public slots: \
132135
m_##name##Id = name##Id; \
133136
m_##name = nullptr; \
134137
propertyChanged(QT_STRINGIFY2(name##Id)); \
138+
} \
139+
Nut::Row<Table> class::_##read() const { return m_##name ; } \
140+
\
141+
void class::_##write(Nut::Row<Table> name) { \
142+
propertyChanged(QT_STRINGIFY2(keyname)); \
143+
m_##name = qSharedPointerCast< type >( name );\
144+
m_##name##Id = m_##name->primaryValue().value<keytype>(); \
135145
}
136146

137147

src/query.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,21 +232,15 @@ Q_OUTOFLINE_TEMPLATE RowList<T> Query<T>::toList(int count)
232232

233233
levels.append(data);
234234
};
235+
236+
// Always add the reference to the query's base table as first item
237+
add_table(0, d->database->model().tableByName(d->tableName));
235238
for (int i = 0; i < d->relations.count(); ++i) {
236239
RelationModel *rel = d->relations[i];
237240
add_table(i, rel->masterTable);
238241
add_table(i, rel->slaveTable);
239242
}
240243

241-
if (!importedTables.count()) {
242-
LevelData data;
243-
data.table = d->database->model().tableByName(d->tableName);
244-
data.keyFiledname = d->tableName + "." + data.table->primaryKey();
245-
data.lastKeyValue = QVariant();
246-
247-
levels.append(data);
248-
}
249-
250244
QVector<bool> checked;
251245
checked.reserve(levels.count());
252246
for (int i = 0; i < levels.count(); ++i)
@@ -290,6 +284,7 @@ Q_OUTOFLINE_TEMPLATE RowList<T> Query<T>::toList(int count)
290284
//create table row
291285
Row<Table> row;
292286
if (data.table->className() == d->className) {
287+
// create a row for the current table
293288
row = Nut::create<T>();
294289
#ifdef NUT_SHARED_POINTER
295290
returnList.append(row.objectCast<T>());
@@ -299,6 +294,7 @@ Q_OUTOFLINE_TEMPLATE RowList<T> Query<T>::toList(int count)
299294
d->tableSet->add(row);
300295

301296
} else {
297+
// create row for a related table
302298
Table *table;
303299
const QMetaObject *childMetaObject
304300
= QMetaType::metaObjectForType(data.table->typeId());
@@ -308,6 +304,20 @@ Q_OUTOFLINE_TEMPLATE RowList<T> Query<T>::toList(int count)
308304
qFatal("Could not create instance of %s",
309305
qPrintable(data.table->name()));
310306
row = createFrom(table);
307+
if (levels[0].lastRow) {
308+
// assign the current row (belongs to a joined table) to the query's base table
309+
foreach (RelationModel *rel, d->relations) {
310+
if (rel->slaveTable->className() == levels[0].table->className()
311+
&& rel->masterTable->className() == data.table->className()) {
312+
// relation found -> assign the row to the query's base table proper field
313+
QString propertyName = "_" + rel->localProperty;
314+
levels[0].lastRow.data()->setProperty(
315+
propertyName.toUtf8().constData(),
316+
QVariant::fromValue(row));
317+
break;
318+
}
319+
}
320+
}
311321
}
312322

313323
QList<FieldModel*> childFields = data.table->fields();

src/table.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ NUT_BEGIN_NAMESPACE
4545

4646
Table::Table(QObject *parent) : QObject(parent),
4747
d(new TablePrivate)
48-
{ }
48+
{
49+
qRegisterMetaType<Nut::Row<Table>>("Nut::Row<Table>");
50+
}
4951

5052
Table::~Table()
5153
{

src/table.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,6 @@ public slots:
9797

9898
NUT_END_NAMESPACE
9999

100+
Q_DECLARE_METATYPE(Nut::Row<Nut::Table>)
101+
100102
#endif // TABLE_H

test/tst_basic/tst_basic.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -227,17 +227,16 @@ void BasicTest::testDate()
227227

228228
void BasicTest::join()
229229
{
230-
// TIC();
231-
// auto q = db.comments()->query()
232-
// ->join<User>()
233-
// ->join<Post>();
234-
235-
// auto comments = q->toList();
230+
TIC();
231+
auto q = db.comments()->query()
232+
->join<User>()
233+
->join<Post>();
236234

237-
// TOC();
238-
// QTEST_ASSERT(comments.length());
239-
// QTEST_ASSERT(comments[0]->author());
240-
// QTEST_ASSERT(comments[0]->author()->username() == "admin");
235+
auto comments = q->toList();
236+
TOC();
237+
QTEST_ASSERT(comments.length());
238+
QTEST_ASSERT(comments[0]->author());
239+
QTEST_ASSERT(comments[0]->author()->username() == "admin");
241240
}
242241

243242

0 commit comments

Comments
 (0)