This commit is contained in:
Hamed Masafi 2019-07-21 11:41:34 +04:30
parent a25e94e930
commit 5dc28f024a
5 changed files with 25 additions and 102 deletions

View File

@ -264,12 +264,12 @@ inline Row<T> create(QObject *parent) {
}
template<class T>
inline T *get(T *row) {
return row;
inline Row<T> createFrom(T *row) {
return QSharedPointer<T>(row);
}
template<class T>
inline T *get(const QSharedPointer<T> row) {
return row.data();
inline Row<T> createFrom(const QSharedPointer<T> row) {
return row;
}
#else
@ -299,32 +299,6 @@ inline T *get(const QSharedPointer<T> row) {
#endif
//template<class C, typename T>
//struct ForeignKeyData {
// Nut::Row<C> _table;
// T _id;
// ForeignKeyData() : _table(nullptr)
// {}
// void setTable(Nut::Row<C> t) {
// _table = t;
// _id = t->primaryValue().value<T>();
// }
// Nut::Row<C> table() const {
// return _table;
// }
// void setValue(const T& val) {
// _table = nullptr;
// _id = val;
// }
// T value() const {
// if (_table)
// return _table->primaryValue().value<T>();
// return _id;
// }
//};
NUT_END_NAMESPACE
#endif // SYNTAX_DEFINES_H

View File

@ -38,10 +38,10 @@ QueryPrivate::~QueryPrivate()
* \brief This class hold a query. A query can be used for getting database rows, editing or deleting without row fetching.
* A query can be used for getting data from database.
* \code
* auto q = db.posts()->createQuery();
* auto q = db.posts()->query();
* q->join(Post::commentsTable());
* q->orderBy(!Post::saveDateField() & Post::bodyField());
* q->setWhere(Post::idField() > 5);
* q->where(Post::idField() > 5);
*
* auto posts = q->toList();
* \endcode
@ -59,7 +59,7 @@ QueryPrivate::~QueryPrivate()
*/
/*!
* \fn Query<T> *Query::setWhere(WherePhrase where)
* \fn Query<T> *Query::where(WherePhrase where)
* Where phrase is a phrase using table's static field methods.
* \code
* q->setWhere(Post::idField() == 4 || Post::titleField().isNull());

View File

@ -288,75 +288,48 @@ Q_OUTOFLINE_TEMPLATE RowList<T> Query<T>::toList(int count)
data.lastKeyValue = q.value(data.keyFiledname);
//create table row
Table *table;
Row<Table> shp;
Row<Table> row;
if (data.table->className() == d->className) {
table = new T();
row = Nut::create<T>();
#ifdef NUT_SHARED_POINTER
shp = QSharedPointer<Table>(table);
returnList.append(shp.objectCast<T>());
d->tableSet->add(shp);
returnList.append(row.objectCast<T>());
#else
returnList.append(dynamic_cast<T*>(table));
#endif
table->setParentTableSet(d->tableSet);
d->tableSet->add(row);
} else {
Table *table;
const QMetaObject *childMetaObject
= QMetaType::metaObjectForType(data.table->typeId());
table = qobject_cast<Table *>(childMetaObject->newInstance());
// table = dynamic_cast<Table *>(QMetaType::create(data.table->typeId()));
if (!table)
qFatal("Could not create instance of %s",
qPrintable(data.table->name()));
shp = QSharedPointer<Table>(table);
row = createFrom(table);
}
const char *className = table->metaObject()->className();
connect(table, &QObject::destroyed, [className](QObject *){
qDebug() << "Destroyed " << className;
});
QList<FieldModel*> childFields = data.table->fields();
foreach (FieldModel *field, childFields)
table->setProperty(field->name.toLatin1().data(),
row->setProperty(field->name.toLatin1().data(),
d->database->sqlGenertor()->unescapeValue(
field->type,
q.value(data.table->name() + "." + field->name)));
for (int i = 0; i < data.masters.count(); ++i) {
int master = data.masters[i];
//#ifdef NUT_SHARED_POINTER
// QString mName = QString("set%1").arg(levels[master].lastRow->metaObject()->className());
// QString type = QString("Nut::Row<%1>").arg(levels[master].lastRow->metaObject()->className());
// bool ok = table->metaObject()->invokeMethod(table,
// mName.toLocal8Bit().data(),
// QGenericArgument(type.toLatin1().data(), levels[master].lastRow));
//// bool ok = table->setProperty(data.masterFields[i].toLocal8Bit().data(),
//// QVariant::fromValue(shp.data()));
//#else
// bool ok = table->setProperty(data.masterFields[i].toLocal8Bit().data(),
// QVariant::fromValue(levels[master].lastRow));
//#endif
// if (!ok)
// qWarning("Unable to set property %s::%s",
// table->metaObject()->className(), data.masterFields[i].toLocal8Bit().data());
auto tableset = levels[master].lastRow->childTableSet(
data.table->className());
// table->setParentTableSet(tableset);
#ifdef NUT_SHARED_POINTER
tableset->add(shp);
#else
tableset->add(table);
#endif
tableset->add(row);
}
table->setStatus(Table::FeatchedFromDB);
table->setParent(this);
table->clear();
row->setStatus(Table::FeatchedFromDB);
row->setParent(this);
row->clear();
//set last created row
data.lastRow = /*QSharedPointer<Table>*/(table);
data.lastRow = row.data();
} //while
} // while

View File

@ -53,11 +53,6 @@ public:
int take;
PhraseList orderPhrase, fieldPhrase;
ConditionalPhrase wherePhrase;
// QList<WherePhrase> wheres;
// QList<WherePhrase> orderPhrases;
// QList<WherePhrase> fields;
// QHash<QString, QString> orders;
};
NUT_END_NAMESPACE

View File

@ -40,9 +40,6 @@ TableSetBase::TableSetBase(Table *parent) : QObject(parent),
TableSetBase::~TableSetBase()
{
// foreach (Table *t, data->tables)
// t->setParentTableSet(nullptr);
foreach (Row<Table> t, data->childs)
if (t)
t->setParentTableSet(nullptr);
@ -66,10 +63,10 @@ int TableSetBase::save(Database *db, bool cleanUp)
|| t->status() == Table::Deleted){
rowsAffected += t->save(db);
if(cleanUp)
#ifndef NUT_SHARED_POINTER
t->deleteLater();
#else
#ifdef NUT_SHARED_POINTER
remove(t);
#else
t->deleteLater();
#endif
}
}
@ -89,22 +86,6 @@ void TableSetBase::clearChilds()
data->childs.clear();
}
//void TableSetBase::add(Table *t)
//{
// if(!data->tables.contains(get(t))){
// data.detach();
// data->tables.insert(get(t));
// data->childRows.append(get(t));
// }
//}
//void TableSetBase::remove(Table *t)
//{
// data.detach();
// data->tables.remove(get(t));
// data->childRows.removeOne(get(t));
//}
void TableSetBase::add(Row<Table> t)
{
data.detach();
@ -115,7 +96,7 @@ void TableSetBase::add(Row<Table> t)
void TableSetBase::remove(Row<Table> t)
{
data.detach();
data->childs.removeOne(t);
data->childs.removeAll(t);
}
QString TableSetBase::childClassName() const