diff --git a/src/nut/core/abstracttableset.cpp b/src/nut/core/abstracttableset.cpp index 49477bb..df0c380 100644 --- a/src/nut/core/abstracttableset.cpp +++ b/src/nut/core/abstracttableset.cpp @@ -115,4 +115,9 @@ void AbstractTableSet::setDatabase(Database *database) data->database = database; } +int AbstractTableSet::size() const +{ + return data->childs.size(); +} + NUT_END_NAMESPACE diff --git a/src/nut/core/abstracttableset.h b/src/nut/core/abstracttableset.h index f5f8f45..cd155b1 100644 --- a/src/nut/core/abstracttableset.h +++ b/src/nut/core/abstracttableset.h @@ -48,6 +48,8 @@ public: Database *database() const; void setDatabase(Database *database); + int size() const; + protected: QExplicitlySharedDataPointer data; diff --git a/src/nut/core/query.h b/src/nut/core/query.h index ce488c2..bd74d31 100644 --- a/src/nut/core/query.h +++ b/src/nut/core/query.h @@ -350,12 +350,14 @@ Q_OUTOFLINE_TEMPLATE RowList Query::toList(int count) //create table row Row row; if (data.table->className() == d->className) { - row = Nut::create(); + Row tmpRow = Nut::create(); + row = tmpRow.template objectCast
(); + row->init(); #ifdef NUT_RAW_POINTER returnList.append(dynamic_cast(table)); #else - returnList.append(row.objectCast()); + returnList.append(tmpRow); #endif d->tableSet->add(row); diff --git a/src/nut/generators/abstractsqlgenerator.cpp b/src/nut/generators/abstractsqlgenerator.cpp index d176b24..bc6b806 100644 --- a/src/nut/generators/abstractsqlgenerator.cpp +++ b/src/nut/generators/abstractsqlgenerator.cpp @@ -258,14 +258,14 @@ QStringList AbstractSqlGenerator::diff(TableModel *oldTable, TableModel *newTabl columnSql << declare; } } -// Q_FOREACH (QString fieldName, relations) { -// RelationModel *newRelation = newTable->foregionKeyByField(fieldName); +// for (auto &fieldName: relations) { +// RelationModel *newRelation = newTable->foreignKeyByField(fieldName); // if (oldTable) { -// RelationModel *oldRelation = oldTable->foregionKeyByField(fieldName); +// RelationModel *oldRelation = oldTable->foreignKeyByField(fieldName); -// QString buffer = diff(oldRelation, newRelation); -// if (!buffer.isNull()) -// columnSql << buffer; +// auto buffer = diff(oldRelation, newRelation); +// if (buffer.size()) +// columnSql.append(buffer); // } else { // columnSql << relationDeclare(newRelation); // } diff --git a/src/nut/generators/sqlitegenerator.cpp b/src/nut/generators/sqlitegenerator.cpp index d27252f..6a01311 100644 --- a/src/nut/generators/sqlitegenerator.cpp +++ b/src/nut/generators/sqlitegenerator.cpp @@ -176,16 +176,23 @@ QStringList SqliteGenerator::diff(TableModel *oldTable, TableModel *newTable) SELECT id, t, m - FROM sqlitestudio_temp_table; + FROM nut_orm_temp_table; DROP TABLE sqlitestudio_temp_table; */ - ret.append(QStringLiteral("ALTER TABLE ") + newTable->name() + QStringLiteral(" RENAME TO sqlitestudio_temp_table;")); + QString foreignKeys; + for (auto &f: newTable->foreignKeys()) { + if (!foreignKeys.isEmpty()) + foreignKeys.append(QStringLiteral(", ")); + foreignKeys.append(QStringLiteral("FOREIGN KEY(%1) REFERENCES %2(id)") + .arg(f->localColumn, f->masterTable->name())); + } + ret.append(QStringLiteral("ALTER TABLE ") + newTable->name() + QStringLiteral(" RENAME TO nut_orm_temp_table;")); ret.append(newTableSql); - ret.append(QStringLiteral("INSERT INTO %1 ( %2 ) SELECT %2 FROM sqlitestudio_temp_table;") + ret.append(QStringLiteral("INSERT INTO %1 ( %2 ) SELECT %2 FROM nut_orm_temp_table;") .arg(newTable->name(), columns)); - ret.append(QStringLiteral("DROP TABLE sqlitestudio_temp_table;")); + ret.append(QStringLiteral("DROP TABLE nut_orm_temp_table;")); return ret; } void SqliteGenerator::appendSkipTake(QString &sql, int skip, int take) diff --git a/tests/auto/tst_json/tst_json.pro b/tests/auto/tst_json/tst_json.pro index f224c0b..c60c9a4 100644 --- a/tests/auto/tst_json/tst_json.pro +++ b/tests/auto/tst_json/tst_json.pro @@ -4,7 +4,8 @@ TARGET = tst_upgrades TEMPLATE = app CONFIG += warn_on c++11 -include(../common/nut-lib.pri) +#include(../common/nut-lib.pri) +include(/doc/dev/qt/Nut/src/nut/nut.pri) SOURCES += \ tst_json.cpp \