some polish and #21 [skip ci]
This commit is contained in:
parent
fed16055bc
commit
e4f1c4c266
|
|
@ -203,8 +203,8 @@ bool DatabasePrivate::getCurrectScheema()
|
|||
QString name;
|
||||
QString value;
|
||||
|
||||
if (!checkClassInfo(q->metaObject()->classInfo(i),
|
||||
type, name, value)) {
|
||||
if (!nutClassInfoString(q->metaObject()->classInfo(i),
|
||||
type, name, value)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -249,21 +249,21 @@ 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;
|
||||
//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];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// type = parts[0];
|
||||
// name = parts[1];
|
||||
// value = parts[2];
|
||||
// return true;
|
||||
// }
|
||||
//}
|
||||
|
||||
DatabaseModel DatabasePrivate::getLastScheema()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -45,8 +45,8 @@ public:
|
|||
DatabaseModel getLastScheema();
|
||||
bool getCurrectScheema();
|
||||
|
||||
bool checkClassInfo(const QMetaClassInfo &classInfo,
|
||||
QString &type, QString &name, QString &value);
|
||||
// bool checkClassInfo(const QMetaClassInfo &classInfo,
|
||||
// QString &type, QString &name, QString &value);
|
||||
QSqlDatabase db;
|
||||
|
||||
QString hostName;
|
||||
|
|
|
|||
|
|
@ -26,6 +26,11 @@
|
|||
#include "defines_p.h"
|
||||
#include "qglobal.h"
|
||||
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QVariant>
|
||||
#include <QMetaClassInfo>
|
||||
|
||||
#ifdef NUT_COMPILE_STATIC
|
||||
# define NUT_EXPORT
|
||||
#else
|
||||
|
|
@ -36,21 +41,95 @@
|
|||
Q_CLASSINFO(__nut_NAME_PERFIX type #name #value, \
|
||||
type "\n" #name "\n" #value)
|
||||
|
||||
#define NUT_INFO_STRING(type, name, value) \
|
||||
Q_CLASSINFO(__nut_NAME_PERFIX type #name #value, \
|
||||
type "\n" #name "\n" value)
|
||||
|
||||
inline bool nutClassInfo(const QMetaClassInfo &classInfo,
|
||||
QString &type, QString &name, QVariant &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 = qVariantFromValue(parts[2]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool nutClassInfoString(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];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool nutClassInfoBool(const QMetaClassInfo &classInfo,
|
||||
QString &type, QString &name, bool &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;
|
||||
|
||||
QString buffer = parts[2].toLower();
|
||||
if (buffer != "true" && buffer != "false")
|
||||
return false;
|
||||
|
||||
type = parts[0];
|
||||
name = parts[1];
|
||||
value = (buffer == "true");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool nutClassInfoInt(const QMetaClassInfo &classInfo,
|
||||
QString &type, QString &name, bool &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;
|
||||
bool ok;
|
||||
type = parts[0];
|
||||
name = parts[1];
|
||||
value = parts[2].toInt(&ok);
|
||||
return ok;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Database
|
||||
#define NUT_DB_VERSION(version) \
|
||||
NUT_INFO(__nut_DB_VERSION, version, 0)
|
||||
|
||||
#define NUT_DECLARE_TABLE(type, name) \
|
||||
NUT_INFO(__nut_TABLE, type, name) \
|
||||
Q_PROPERTY(type* name READ name) \
|
||||
Q_PROPERTY(NUT_WRAP_NAMESPACE(TableSet<type>) name##Table READ name##Table)\
|
||||
type* m_##name; \
|
||||
NUT_WRAP_NAMESPACE(TableSet<type>) *m_##name##Table; \
|
||||
Q_PROPERTY(NUT_WRAP_NAMESPACE(TableSet<type>) name READ name) \
|
||||
NUT_WRAP_NAMESPACE(TableSet<type>) *m_##name; \
|
||||
public: \
|
||||
static const type _##name; \
|
||||
type* name() const{ return m_##name; } \
|
||||
NUT_WRAP_NAMESPACE(TableSet<type>) *name##Table() const \
|
||||
{ return m_##name##Table; }
|
||||
static const type *_##name; \
|
||||
NUT_WRAP_NAMESPACE(TableSet<type>) *name() const \
|
||||
{ return m_##name; }
|
||||
|
||||
//Table
|
||||
#define NUT_DECLARE_FIELD(type, name, read, write) \
|
||||
|
|
@ -99,11 +178,9 @@ public: \
|
|||
return m_##n; \
|
||||
}
|
||||
|
||||
|
||||
#define NUT_PRIMARY_KEY(x) NUT_INFO(__nut_PRIMARY_KEY, x, 0)
|
||||
#define NUT_AUTO_INCREMENT(x) NUT_INFO(__nut_AUTO_INCREMENT, x, 0)
|
||||
#define NUT_PRIMARY_AUTO_INCREMENT(x) NUT_PRIMARY_KEY(x) \
|
||||
NUT_AUTO_INCREMENT(x)
|
||||
#define NUT_PRIMARY_AUTO_INCREMENT(x) NUT_INFO(__nut_PRIMARY_KEY_AI, x, 0)
|
||||
#define NUT_DISPLAY_NAME(field, name) NUT_INFO(__nut_DISPLAY, field, name)
|
||||
#define NUT_UNIQUE(x) NUT_INFO(__nut_UNIQUE, x, 0)
|
||||
#define NUT_LEN(field, len) NUT_INFO(__nut_LEN, field, len)
|
||||
|
|
|
|||
|
|
@ -27,10 +27,11 @@
|
|||
#define __FOREIGN_KEYS "foreign_keys"
|
||||
#define __nut_FIELD "field"
|
||||
|
||||
#define __nut_NAME_PERFIX "nut_info::"
|
||||
#define __nut_DB_VERSION "database_version"
|
||||
#define __nut_NAME_PERFIX "nut_db_key::"
|
||||
#define __nut_PRIMARY_KEY "primary_key"
|
||||
#define __nut_AUTO_INCREMENT "auto_increment"
|
||||
#define __nut_PRIMARY_KEY_AI "primary_key_ai"
|
||||
#define __nut_UNIQUE "unique"
|
||||
#define __nut_TABLE "table"
|
||||
#define __nut_TABLE_NAME "table_name"
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ QString PostgreSqlGenerator::fieldType(FieldModel *field)
|
|||
if(field->isAutoIncrement)
|
||||
dbType = "BIGSERIAL";
|
||||
else
|
||||
dbType = "BIGINTEGER";
|
||||
dbType = "BIGINT";
|
||||
break;
|
||||
|
||||
case QVariant::Double:
|
||||
|
|
|
|||
|
|
@ -24,8 +24,6 @@
|
|||
|
||||
NUT_BEGIN_NAMESPACE
|
||||
|
||||
#define LOG(s) qDebug() << __func__ << s
|
||||
|
||||
PhraseData::PhraseData() :
|
||||
className(""), fieldName(""),
|
||||
type(Field), operatorCond(NotAssign),
|
||||
|
|
@ -65,7 +63,6 @@ PhraseData::PhraseData(PhraseData *l, PhraseData::Condition o, QVariant r)
|
|||
|
||||
PhraseData *PhraseData::operator =(PhraseData *other)
|
||||
{
|
||||
LOG("");
|
||||
other->parents++;
|
||||
return other;
|
||||
}
|
||||
|
|
@ -76,20 +73,6 @@ PhraseData &PhraseData::operator =(PhraseData &other)
|
|||
return other;
|
||||
}
|
||||
|
||||
//PhraseData::PhraseData(const PhraseData &other) :
|
||||
// left(other.left), right(other.right), operand(other.operand),
|
||||
// operatorCond(other.operatorCond), className(other.className),
|
||||
// fieldName(other.fieldName), type(other.type), isNot(other.isNot),
|
||||
// parents(other.parents + 1)
|
||||
//{ }
|
||||
|
||||
//PhraseData::PhraseData(const PhraseData *other) :
|
||||
// left(other->left), right(other->right), operand(other->operand),
|
||||
// operatorCond(other->operatorCond), className(other->className),
|
||||
// fieldName(other->fieldName), type(other->type), isNot(other->isNot),
|
||||
// parents(other->parents + 1)
|
||||
//{ }
|
||||
|
||||
QString PhraseData::toString() const
|
||||
{
|
||||
return QString("[%1].%2").arg(className).arg(fieldName);
|
||||
|
|
@ -97,27 +80,10 @@ QString PhraseData::toString() const
|
|||
|
||||
PhraseData::~PhraseData()
|
||||
{
|
||||
// if (type == WithOther) {
|
||||
// delete left;
|
||||
// delete right;
|
||||
// }
|
||||
// if (type == WithVariant) {
|
||||
// if (left)
|
||||
// delete left;
|
||||
// }
|
||||
|
||||
// if (right && !--right->parents)
|
||||
// delete right;
|
||||
|
||||
// if (left && !--left->parents)
|
||||
// delete left;
|
||||
|
||||
LOG("");
|
||||
}
|
||||
|
||||
void PhraseData::cleanUp()
|
||||
{
|
||||
// cleanUp(this);
|
||||
}
|
||||
|
||||
void PhraseData::cleanUp(PhraseData *d)
|
||||
|
|
@ -154,12 +120,6 @@ AbstractFieldPhrase::AbstractFieldPhrase(AbstractFieldPhrase &&other)
|
|||
|
||||
AbstractFieldPhrase::~AbstractFieldPhrase()
|
||||
{
|
||||
if (data) {
|
||||
LOG(data->toString()) << data->parents;
|
||||
} else {
|
||||
LOG("");
|
||||
}
|
||||
|
||||
if (data) {
|
||||
--data->parents;
|
||||
if (data->parents <= 0)
|
||||
|
|
@ -232,14 +192,12 @@ PhraseList::PhraseList() : isValid(false)
|
|||
|
||||
PhraseList::PhraseList(const PhraseList &other) : isValid(true)
|
||||
{
|
||||
LOG("");
|
||||
data = qMove(other.data);
|
||||
const_cast<PhraseList&>(other).data.clear();
|
||||
}
|
||||
|
||||
PhraseList::PhraseList(PhraseList &&other)
|
||||
{
|
||||
LOG("");
|
||||
data = other.data;
|
||||
}
|
||||
|
||||
|
|
@ -277,7 +235,6 @@ PhraseList::PhraseList(PhraseList *left, const AbstractFieldPhrase *right)
|
|||
|
||||
PhraseList::~PhraseList()
|
||||
{
|
||||
LOG("");
|
||||
}
|
||||
|
||||
PhraseList &PhraseList::operator =(const PhraseList &other)
|
||||
|
|
@ -484,7 +441,6 @@ ConditionalPhrase::ConditionalPhrase(ConditionalPhrase *l,
|
|||
|
||||
ConditionalPhrase::~ConditionalPhrase()
|
||||
{
|
||||
LOG("");
|
||||
if (data) {
|
||||
data->cleanUp();
|
||||
if (!--data->parents)
|
||||
|
|
|
|||
|
|
@ -156,22 +156,22 @@ bool TableModel::operator !=(const TableModel &t) const
|
|||
return !(*this == t);
|
||||
}
|
||||
|
||||
bool TableModel::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;
|
||||
//bool TableModel::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];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// type = parts[0];
|
||||
// name = parts[1];
|
||||
// value = parts[2];
|
||||
// return true;
|
||||
// }
|
||||
//}
|
||||
|
||||
TableModel::TableModel(int typeId, QString tableName)
|
||||
{
|
||||
|
|
@ -196,7 +196,7 @@ TableModel::TableModel(int typeId, QString tableName)
|
|||
QString name;
|
||||
QString value;
|
||||
|
||||
if (!checkClassInfo(tableMetaObject->classInfo(j),
|
||||
if (!nutClassInfoString(tableMetaObject->classInfo(j),
|
||||
type, name, value)) {
|
||||
continue;
|
||||
}
|
||||
|
|
@ -227,7 +227,7 @@ TableModel::TableModel(int typeId, QString tableName)
|
|||
QString name;
|
||||
QString value;
|
||||
|
||||
if (!checkClassInfo(tableMetaObject->classInfo(j),
|
||||
if (!nutClassInfoString(tableMetaObject->classInfo(j),
|
||||
type, name, value)) {
|
||||
continue;
|
||||
}
|
||||
|
|
@ -248,23 +248,27 @@ TableModel::TableModel(int typeId, QString tableName)
|
|||
|
||||
|
||||
FieldModel *f = field(name);
|
||||
if(!f)
|
||||
if (!f)
|
||||
continue;
|
||||
|
||||
if(type == __nut_LEN)
|
||||
if (type == __nut_LEN)
|
||||
f->length = value.toInt();
|
||||
else if(type == __nut_NOT_NULL)
|
||||
else if (type == __nut_NOT_NULL)
|
||||
f->notNull = true;
|
||||
else if(type == __nut_DEFAULT_VALUE)
|
||||
else if (type == __nut_DEFAULT_VALUE)
|
||||
f->defaultValue = value;
|
||||
else if(type == __nut_PRIMARY_KEY)
|
||||
else if (type == __nut_PRIMARY_KEY)
|
||||
f->isPrimaryKey = true;
|
||||
else if(type == __nut_AUTO_INCREMENT)
|
||||
else if (type == __nut_AUTO_INCREMENT)
|
||||
f->isAutoIncrement = true;
|
||||
else if(type == __nut_UNIQUE)
|
||||
else if (type == __nut_UNIQUE)
|
||||
f->isUnique = true;
|
||||
else if(type == __nut_DISPLAY)
|
||||
else if (type == __nut_DISPLAY)
|
||||
f->displayName = value.mid(1, value.length() - 2);
|
||||
else if (type == __nut_PRIMARY_KEY_AI) {
|
||||
f->isPrimaryKey = true;
|
||||
f->isAutoIncrement = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(!findByTypeId(typeId) && !tableName.isNull())
|
||||
|
|
|
|||
|
|
@ -138,8 +138,8 @@ private:
|
|||
QList<FieldModel*> _fields;
|
||||
QList<RelationModel*> _foreignKeys;
|
||||
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
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <QtCore/qglobal.h>
|
||||
#include <QtCore/QDateTime>
|
||||
#include <QtCore/QUuid>
|
||||
#include "table.h"
|
||||
|
||||
#ifdef NUT_NAMESPACE
|
||||
|
|
@ -16,13 +17,13 @@ class Comment : public Table
|
|||
Q_OBJECT
|
||||
|
||||
NUT_PRIMARY_AUTO_INCREMENT(id)
|
||||
NUT_DECLARE_FIELD(int, id, id, setId)
|
||||
NUT_DECLARE_FIELD(QUuid, id, id, setId)
|
||||
NUT_DECLARE_FIELD(QString, message, message, setMessage)
|
||||
NUT_DECLARE_FIELD(QDateTime, saveDate, saveDate, setSaveDate)
|
||||
NUT_DECLARE_FIELD(qreal, point, point, setPoint)
|
||||
|
||||
NUT_FOREGION_KEY(Post, int, post, post, setPost)
|
||||
NUT_FOREGION_KEY(User, int, author, author, setAuthor)
|
||||
NUT_FOREGION_KEY(User, QUuid, author, author, setAuthor)
|
||||
|
||||
public:
|
||||
Q_INVOKABLE explicit Comment(QObject *parentTableSet = 0);
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
#define DRIVER "QSQLITE"
|
||||
#define HOST "127.0.0.1"
|
||||
#define DATABASE "nutdb"
|
||||
#define DATABASE "nutdb1"
|
||||
#define USERNAME "root"
|
||||
#define PASSWORD "onlyonlyi"
|
||||
|
||||
|
|
|
|||
|
|
@ -17,8 +17,8 @@ class User : public Nut::Table
|
|||
{
|
||||
Q_OBJECT
|
||||
|
||||
NUT_PRIMARY_AUTO_INCREMENT(id)
|
||||
NUT_DECLARE_FIELD(int, id, id, setId)
|
||||
NUT_PRIMARY_KEY(id)
|
||||
NUT_DECLARE_FIELD(QUuid, id, id, setId)
|
||||
|
||||
NUT_NOT_NULL(username)
|
||||
NUT_LEN(username, 50)
|
||||
|
|
|
|||
Loading…
Reference in New Issue