join OK
This commit is contained in:
parent
f0717cc05b
commit
7e437fd805
|
|
@ -71,7 +71,7 @@ public: \
|
|||
#define NUT_FOREGION_KEY(type, keytype, name, read, write) \
|
||||
Q_PROPERTY(type* name READ read WRITE write) \
|
||||
NUT_DECLARE_FIELD(keytype, name##Id, read##Id, write##Id) \
|
||||
NUT_INFO(__nut_FOREGION_KEY, name##Id, type) \
|
||||
NUT_INFO(__nut_FOREGION_KEY, name, type) \
|
||||
type *m_##name; \
|
||||
public: \
|
||||
type *read() const { return m_##name ; } \
|
||||
|
|
|
|||
39
src/query.h
39
src/query.h
|
|
@ -154,6 +154,7 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
|||
struct LevelData{
|
||||
QList<int> masters;
|
||||
QList<int> slaves;
|
||||
QList<QString> masterFields;
|
||||
QString keyFiledname;
|
||||
QVariant lastKeyValue;
|
||||
TableModel *table;
|
||||
|
|
@ -171,19 +172,25 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
|||
data.keyFiledname = data.table->name() + "." + data.table->primaryKey();
|
||||
data.lastKeyValue = QVariant();
|
||||
|
||||
QSet<QString> masters;
|
||||
QHash<QString, QString> masters;
|
||||
foreach (RelationModel *rel, d->relations)
|
||||
if (rel->slaveTable->name() == table->name())
|
||||
masters.insert(rel->masterTable->name());
|
||||
masters.insert(rel->masterTable->name(), rel->localProperty);
|
||||
|
||||
for (int j = 0; j < levels.count(); ++j) {
|
||||
LevelData &dt = levels[j];
|
||||
qDebug() <<"[check]"<<table->name() << dt.table->name();
|
||||
foreach (QString m, masters)
|
||||
if (dt.table->name() == m) {
|
||||
|
||||
QHashIterator<QString, QString> it(masters);
|
||||
while (it.hasNext()) {
|
||||
it.next();
|
||||
|
||||
if (dt.table->name() == it.key()) {
|
||||
data.masters.append(j);
|
||||
data.masterFields.append(it.value());
|
||||
dt.slaves.append(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
qDebug() << data.table->name() <<"added";
|
||||
levels.append(data);
|
||||
|
|
@ -194,6 +201,15 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
|||
add_table(i, rel->slaveTable);
|
||||
}
|
||||
|
||||
if (!importedTables.count()) {
|
||||
LevelData data;
|
||||
data.table = d->database->model().tableByName(d->tableName);
|
||||
data.keyFiledname = d->tableName + "." + data.table->primaryKey();
|
||||
data.lastKeyValue = QVariant();
|
||||
|
||||
levels.append(data);
|
||||
}
|
||||
|
||||
QVector<bool> checked;
|
||||
checked.reserve(levels.count());
|
||||
for (int i = 0; i < levels.count(); ++i)
|
||||
|
|
@ -243,24 +259,31 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
|||
= QMetaType::metaObjectForType(data.table->typeId());
|
||||
table = qobject_cast<Table *>(childMetaObject->newInstance());
|
||||
|
||||
qDebug() << data.table->name() <<"created";
|
||||
}
|
||||
qDebug() << "table created" << table;
|
||||
|
||||
QStringList childFields = data.table->fieldsNames();
|
||||
foreach (QString field, childFields)
|
||||
table->setProperty(field.toLatin1().data(),
|
||||
q.value(data.table->name() + "." + field));
|
||||
|
||||
foreach (int master, data.masters) {
|
||||
for (int i = 0; i < data.masters.count(); ++i) {
|
||||
int master = data.masters[i];
|
||||
table->setProperty(data.masterFields[i].toLocal8Bit().data(),
|
||||
QVariant::fromValue(levels[master].lastRow));
|
||||
|
||||
table->setParentTableSet(levels[master].lastRow->childTableSet(data.table->className()));
|
||||
qDebug() << data.table->name() << "added to" << levels[master].table->name();
|
||||
TableSetBase *ts = levels[master].lastRow->childTableSet(data.table->className());
|
||||
qDebug() << table << "added to"
|
||||
<< levels[master].lastRow
|
||||
<< ts->childClassName()
|
||||
<< data.masterFields[i];
|
||||
}
|
||||
|
||||
table->setStatus(Table::FeatchedFromDB);
|
||||
table->setParent(this);
|
||||
table->clear();
|
||||
|
||||
qDebug() << "table created" << table;
|
||||
//set last created row
|
||||
data.lastRow = table;
|
||||
|
||||
|
|
|
|||
|
|
@ -223,7 +223,8 @@ TableModel::TableModel(int typeId, QString tableName)
|
|||
if(type == __nut_FOREGION_KEY){
|
||||
RelationModel *fk = new RelationModel;
|
||||
fk->slaveTable = this;
|
||||
fk->localColumn = name;
|
||||
fk->localColumn = name + "Id";
|
||||
fk->localProperty = name;
|
||||
fk->foregionColumn = value;
|
||||
fk->masterClassName = value;
|
||||
_foregionKeys.append(fk);
|
||||
|
|
|
|||
|
|
@ -67,6 +67,7 @@ struct FieldModel{
|
|||
struct RelationModel{
|
||||
//slave
|
||||
QString localColumn;
|
||||
QString localProperty;
|
||||
TableModel *slaveTable;
|
||||
//master
|
||||
QString foregionColumn;
|
||||
|
|
|
|||
|
|
@ -118,20 +118,23 @@ void MainTest::selectPosts()
|
|||
|
||||
PRINT(posts.length());
|
||||
PRINT(posts.at(0)->comments()->length());
|
||||
// QTEST_ASSERT(posts.length() == 1);
|
||||
// QTEST_ASSERT(posts.at(0)->comments()->length() == 3);
|
||||
// QTEST_ASSERT(posts.at(0)->title() == "post title");
|
||||
QTEST_ASSERT(posts.length() == 1);
|
||||
QTEST_ASSERT(posts.at(0)->comments()->length() == 3);
|
||||
QTEST_ASSERT(posts.at(0)->title() == "post title");
|
||||
|
||||
// QTEST_ASSERT(posts.at(0)->comments()->at(0)->message() == "comment #0");
|
||||
// QTEST_ASSERT(posts.at(0)->comments()->at(1)->message() == "comment #1");
|
||||
// QTEST_ASSERT(posts.at(0)->comments()->at(2)->message() == "comment #2");
|
||||
QTEST_ASSERT(posts.at(0)->comments()->at(0)->message() == "comment #0");
|
||||
QTEST_ASSERT(posts.at(0)->comments()->at(1)->message() == "comment #1");
|
||||
QTEST_ASSERT(posts.at(0)->comments()->at(2)->message() == "comment #2");
|
||||
db.cleanUp();
|
||||
}
|
||||
|
||||
void MainTest::selectFirst()
|
||||
{
|
||||
auto posts = db.posts()->query()
|
||||
->first();
|
||||
auto q = db.posts()->query();
|
||||
|
||||
auto posts = q->first();
|
||||
|
||||
qDebug() << q->sqlCommand();
|
||||
QTEST_ASSERT(posts != Q_NULLPTR);
|
||||
}
|
||||
|
||||
|
|
@ -194,13 +197,6 @@ void MainTest::selectWithInvalidRelation()
|
|||
q->toList();
|
||||
}
|
||||
|
||||
void MainTest::select10NewstPosts()
|
||||
{
|
||||
auto q = db.posts()->query();
|
||||
q->orderBy(!Post::saveDateField());
|
||||
q->toList(10);
|
||||
}
|
||||
|
||||
void MainTest::modifyPost()
|
||||
{
|
||||
auto q = db.posts()->query();
|
||||
|
|
|
|||
Loading…
Reference in New Issue