Skip to content

Commit cc1c7af

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 cc1c7af

5 files changed

Lines changed: 45 additions & 33 deletions

File tree

src/defines.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,14 @@ public: \
8282
propertyChanged(#name); \
8383
}
8484

85-
#define NUT_FOREIGN_KEY(type, keytype, name, read, write) \
86-
Q_PROPERTY(Nut::Row<type> name READ read WRITE write) \
87-
NUT_DECLARE_FIELD(keytype, name##Id, read##Id, write##Id) \
88-
NUT_INFO(__nut_FOREIGN_KEY, name, type) \
89-
Nut::Row<type> m_##name; \
90-
public slots: \
91-
Nut::Row<type> read() const { return m_##name ; } \
92-
Q_INVOKABLE void write(Nut::Row<type> name){ \
93-
m_##name = name; \
94-
}
95-
9685
#define NUT_FOREIGN_KEY_DECLARE(type, keytype, name, read, write) \
9786
NUT_INFO(__nut_FIELD, name##Id, 0) \
9887
NUT_INFO(__nut_FOREIGN_KEY, name, type) \
9988
Nut::Row<type> m_##name; \
10089
keytype m_##name##Id; \
10190
Q_PROPERTY(Nut::Row<type> name READ read WRITE write) \
10291
Q_PROPERTY(keytype name##Id READ read##Id WRITE write##Id) \
92+
Q_PROPERTY(Nut::Row<Table> _##name READ _##read WRITE _##write) \
10393
public: \
10494
Nut::Row<type> read() const; \
10595
keytype read##Id() const; \
@@ -109,9 +99,11 @@ public: \
10999
(staticMetaObject.className(), #name); \
110100
return f; \
111101
} \
112-
public slots: \
113102
void write(Nut::Row<type> name); \
114-
void write##Id(keytype name##Id);
103+
void write##Id(keytype name##Id); \
104+
private: \
105+
Nut::Row<Table> _##read() const; \
106+
void _##write(Nut::Row<Table> name);
115107

116108
#define NUT_FOREIGN_KEY_IMPLEMENT(class, type, keytype, name, read, write) \
117109
\
@@ -132,6 +124,13 @@ public slots: \
132124
m_##name##Id = name##Id; \
133125
m_##name = nullptr; \
134126
propertyChanged(QT_STRINGIFY2(name##Id)); \
127+
} \
128+
Nut::Row<Table> class::_##read() const { return m_##name ; } \
129+
\
130+
void class::_##write(Nut::Row<Table> name) { \
131+
propertyChanged(QT_STRINGIFY2(keyname)); \
132+
m_##name = qSharedPointerCast< type >( name );\
133+
m_##name##Id = m_##name->primaryValue().value<keytype>(); \
135134
}
136135

137136

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)