fix: crash if relation is one-way

This commit is contained in:
Hamed Masafi 2021-05-31 10:59:23 +04:30
parent 5f36af4bf7
commit e3c77909a3
6 changed files with 30 additions and 13 deletions

View File

@ -115,4 +115,9 @@ void AbstractTableSet::setDatabase(Database *database)
data->database = database;
}
int AbstractTableSet::size() const
{
return data->childs.size();
}
NUT_END_NAMESPACE

View File

@ -48,6 +48,8 @@ public:
Database *database() const;
void setDatabase(Database *database);
int size() const;
protected:
QExplicitlySharedDataPointer<AbstractTableSetData> data;

View File

@ -350,12 +350,14 @@ Q_OUTOFLINE_TEMPLATE RowList<T> Query<T>::toList(int count)
//create table row
Row<Table> row;
if (data.table->className() == d->className) {
row = Nut::create<T>();
Row<T> tmpRow = Nut::create<T>();
row = tmpRow.template objectCast<Table>();
row->init();
#ifdef NUT_RAW_POINTER
returnList.append(dynamic_cast<T*>(table));
#else
returnList.append(row.objectCast<T>());
returnList.append(tmpRow);
#endif
d->tableSet->add(row);

View File

@ -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);
// }

View File

@ -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)

View File

@ -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 \