new macro

This commit is contained in:
Hamed Masafi 2018-01-11 13:38:45 +03:30
parent b77fde9358
commit c5e9d5ec2b
7 changed files with 42 additions and 15 deletions

View File

@ -188,16 +188,20 @@ bool DatabasePrivate::getCurrectScheema()
changeLogs = new TableSet<ChangeLogTable>(q);
for (int i = 0; i < q->metaObject()->classInfoCount(); i++) {
QMetaClassInfo ci = q->metaObject()->classInfo(i);
QString ciName = QString(ci.name())
.replace(__nut_NAME_PERFIX, "")
.replace("\"", "");
QString type;
QString name;
QString value;
if (ciName.startsWith(__nut_TABLE))
tables.insert(ciName.split(" ").at(1), ci.value());
if (!checkClassInfo(q->metaObject()->classInfo(i),
type, name, value)) {
continue;
}
if (ciName == __nut_DB_VERSION) {
currentModel.setVersion(QString(ci.value()));
if (type == __nut_TABLE)
tables.insert(name, type);
if (type == __nut_DB_VERSION)
currentModel.setVersion(name);
/* TODO: remove
QStringList version
@ -213,7 +217,6 @@ bool DatabasePrivate::getCurrectScheema()
if (!ok)
qFatal("NUT_DB_VERSION macro accept version in format 'x' or "
"'x[.y]' only, and x,y must be integer values\n");*/
}
}
for (int i = 1; i < q->metaObject()->propertyCount(); i++) {
@ -235,6 +238,23 @@ bool DatabasePrivate::getCurrectScheema()
return true;
}
bool DatabasePrivate::checkClassInfo(const QMetaClassInfo &classInfo, QString &type, QString &name, QString &value)
{
if (!QString(classInfo.name()).startsWith(__nut_NAME_PERFIX)) {
return false;
} else {
QStringList parts = QString(classInfo.value()).split("\n");
if (parts.count() != 3)
return false;
type = parts[0];
name = parts[1];
value = parts[2];
qDebug() << Q_FUNC_INFO << parts;
return true;
}
}
DatabaseModel DatabasePrivate::getLastScheema()
{
ChangeLogTable *u = changeLogs->query()

View File

@ -45,6 +45,8 @@ public:
DatabaseModel getLastScheema();
bool getCurrectScheema();
bool checkClassInfo(const QMetaClassInfo &classInfo,
QString &type, QString &name, QString &value);
QSqlDatabase db;
QString hostName;

View File

@ -79,7 +79,7 @@ TableModel *DatabaseModel::tableByClassName(QString className) const
// qWarning("Table with class name '%s' not found in model",
// qUtf8Printable(className));
// Q_UNREACHABLE();
Q_UNREACHABLE();
return 0;
}

View File

@ -33,10 +33,9 @@
#endif
#define NUT_INFO(type, name, value) \
Q_CLASSINFO(__nut_NAME_PERFIX #type #name #value, #type "\n" #name "\n" #value)
Q_CLASSINFO(__nut_NAME_PERFIX type #name #value, type "\n" #name "\n" #value)
// Database
//TODO: remove minor version
#define NUT_DB_VERSION(version) \
NUT_INFO(__nut_DB_VERSION, version, 0)

View File

@ -68,7 +68,10 @@ QString Table::primaryKey() const
bool Table::isPrimaryKeyAutoIncrement() const
{
return TableModel::findByClassName(metaObject()->className())->field(primaryKey())->isAutoIncrement;
auto m = TableModel::findByClassName(metaObject()->className());
auto pk = m->primaryKey();
auto f = m->field(pk);
return f->isAutoIncrement;
}

View File

@ -144,7 +144,8 @@ bool TableModel::operator !=(const TableModel &t) const
return !(*this == t);
}
bool TableModel::checkClassInfo(const QMetaClassInfo &classInfo, QString type, QString name, QString value)
bool TableModel::checkClassInfo(const QMetaClassInfo &classInfo,
QString &type, QString &name, QString &value)
{
if (!QString(classInfo.name()).startsWith(__nut_NAME_PERFIX)) {
return false;
@ -189,6 +190,7 @@ TableModel::TableModel(int typeId, QString tableName)
continue;
}
qDebug() << "**********" << type << __nut_FIELD << (type == __nut_FIELD);
if(type == __nut_FIELD){
FieldModel *f = new FieldModel;
f->name = name;

View File

@ -117,7 +117,8 @@ private:
QList<FieldModel*> _fields;
QList<RelationModel*> _foregionKeys;
static QSet<TableModel*>_allModels;
bool checkClassInfo(const QMetaClassInfo &classInfo, QString type, QString name, QString value);
bool checkClassInfo(const QMetaClassInfo &classInfo,
QString &type, QString &name, QString &value);
};
NUT_END_NAMESPACE