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; data->database = database;
} }
int AbstractTableSet::size() const
{
return data->childs.size();
}
NUT_END_NAMESPACE NUT_END_NAMESPACE

View File

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

View File

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

View File

@ -258,14 +258,14 @@ QStringList AbstractSqlGenerator::diff(TableModel *oldTable, TableModel *newTabl
columnSql << declare; columnSql << declare;
} }
} }
// Q_FOREACH (QString fieldName, relations) { // for (auto &fieldName: relations) {
// RelationModel *newRelation = newTable->foregionKeyByField(fieldName); // RelationModel *newRelation = newTable->foreignKeyByField(fieldName);
// if (oldTable) { // if (oldTable) {
// RelationModel *oldRelation = oldTable->foregionKeyByField(fieldName); // RelationModel *oldRelation = oldTable->foreignKeyByField(fieldName);
// QString buffer = diff(oldRelation, newRelation); // auto buffer = diff(oldRelation, newRelation);
// if (!buffer.isNull()) // if (buffer.size())
// columnSql << buffer; // columnSql.append(buffer);
// } else { // } else {
// columnSql << relationDeclare(newRelation); // columnSql << relationDeclare(newRelation);
// } // }

View File

@ -176,16 +176,23 @@ QStringList SqliteGenerator::diff(TableModel *oldTable, TableModel *newTable)
SELECT id, SELECT id,
t, t,
m m
FROM sqlitestudio_temp_table; FROM nut_orm_temp_table;
DROP TABLE sqlitestudio_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(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)); .arg(newTable->name(), columns));
ret.append(QStringLiteral("DROP TABLE sqlitestudio_temp_table;")); ret.append(QStringLiteral("DROP TABLE nut_orm_temp_table;"));
return ret; return ret;
} }
void SqliteGenerator::appendSkipTake(QString &sql, int skip, int take) void SqliteGenerator::appendSkipTake(QString &sql, int skip, int take)

View File

@ -4,7 +4,8 @@ TARGET = tst_upgrades
TEMPLATE = app TEMPLATE = app
CONFIG += warn_on c++11 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 += \ SOURCES += \
tst_json.cpp \ tst_json.cpp \