polish alpha [skip ci]

This commit is contained in:
Hamed Masafi 2019-06-07 12:49:20 +04:30
parent 370d5234e0
commit d2822e3ef3
16 changed files with 103 additions and 170 deletions

View File

@ -23,7 +23,7 @@ void Nut::BulkInserter::setFields(const Nut::PhraseList &ph)
void Nut::BulkInserter::insert(std::initializer_list<QVariant> vars)
{
if (vars.size() != _fieldCount) {
qInfo("Number of rows mstake");
qInfo("Number of rows mistake");
return;
}

View File

@ -45,7 +45,9 @@
#include <iostream>
#include <cstdarg>
#define __CHANGE_LOG_TABLE_NAME "__change_logs"
#ifndef __CHANGE_LOG_TABLE_NAME
# define __CHANGE_LOG_TABLE_NAME "__change_logs"
#endif
NUT_BEGIN_NAMESPACE
@ -196,6 +198,7 @@ bool DatabasePrivate::getCurrectScheema()
return false;
}
QMap<QString, QString> tables;
tables.clear();
// TODO: change logs must not be in model
@ -240,21 +243,6 @@ bool DatabasePrivate::getCurrectScheema()
if (!ok)
qFatal("NUT_DB_VERSION macro accept version in format 'x'");
currentModel.setVersion(version);
/* TODO: remove
QStringList version
= QString(ci.value()).replace("\"", "").split('.');
bool ok = false;
if (version.length() == 1) {
currentModel.setVersion(version.at(0).toInt(&ok));
} else if (version.length() == 2) {
currentModel.setVersionMajor(version.at(0).toInt(&ok));
currentModel.setVersionMinor(version.at(1).toInt(&ok));
}
if (!ok)
qFatal("NUT_DB_VERSION macro accept version in format 'x' or "
"'x[.y]' only, and x,y must be integer values\n");*/
}
}
@ -288,22 +276,6 @@ 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];
// return true;
// }
//}
DatabaseModel DatabasePrivate::getLastScheema()
{
ChangeLogTable *u = changeLogs->query()
@ -386,6 +358,7 @@ void DatabasePrivate::createChangeLogs()
Database::Database(QObject *parent)
: QObject(parent), d_ptr(new DatabasePrivate(this))
{
// _d = new QSharedDataPointer<DatabasePrivate>(new DatabasePrivate(this));
DatabasePrivate::lastId++;
}
@ -393,6 +366,7 @@ Database::Database(const Database &other)
: QObject(other.parent()), d_ptr(new DatabasePrivate(this))
{
DatabasePrivate::lastId++;
// _d = other._d;
setDriver(other.driver());
setHostName(other.hostName());
@ -592,7 +566,6 @@ bool Database::open(bool updateDatabase)
if (!d->sqlGenertor) {
qFatal("Sql generator for driver %s not found",
driver().toLatin1().constData());
return false;
}
return d->open(updateDatabase);

View File

@ -24,6 +24,7 @@
#include <QtCore/qglobal.h>
#include <QtCore/QList>
#include <QtSql/QSqlDatabase>
#include <QSharedDataPointer>
#include "defines.h"
#include "tableset.h"
@ -39,6 +40,7 @@ class NUT_EXPORT Database : public QObject
{
Q_OBJECT
// QSharedDataPointer<DatabasePrivate> *_d;
DatabasePrivate *d_ptr;
Q_DECLARE_PRIVATE(Database)

View File

@ -25,11 +25,12 @@
#include "databasemodel.h"
#include <QDebug>
#include <QSharedData>
NUT_BEGIN_NAMESPACE
class ChangeLogTable;
class DatabasePrivate
class DatabasePrivate //: public QSharedData
{
Database *q_ptr;
Q_DECLARE_PUBLIC(Database)
@ -45,8 +46,6 @@ public:
DatabaseModel getLastScheema();
bool getCurrectScheema();
// bool checkClassInfo(const QMetaClassInfo &classInfo,
// QString &type, QString &name, QString &value);
QSqlDatabase db;
QString hostName;
@ -62,8 +61,6 @@ public:
TableSet<ChangeLogTable> *changeLogs;
QT_DEPRECATED
QMap<QString, QString> tables;
static QMap<QString, DatabaseModel> allTableMaps;
static qulonglong lastId;

View File

@ -163,13 +163,15 @@ QString SqlServerGenerator::diff(FieldModel *oldField, FieldModel *newField)
QString SqlServerGenerator::escapeValue(const QVariant &v) const
{
if (v.type() == QMetaType::QString || v.type() == QMetaType::QChar)
auto mid = static_cast<QMetaType::Type>(v.userType());
if (mid == QMetaType::QString || mid == QMetaType::QChar)
return "N'" + v.toString() + "'";
else if (v.type() == QMetaType::QPoint) {
else if (mid == QMetaType::QPoint) {
QPoint pt = v.toPoint();
return QString("geography::POINT(%1, %2, 4326)").arg(pt.x()).arg(
pt.y());
} else if (v.type() == QMetaType::QPointF) {
} else if (mid == QMetaType::QPointF) {
QPointF pt = v.toPointF();
return QString("geography::POINT(%1, %2, 4326)").arg(pt.x()).arg(
pt.y());

View File

@ -29,7 +29,9 @@
#include <QtSql/QSqlResult>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQueryModel>
#include <QSqlQuery>
#include "table.h"
#include "query_p.h"
#include "database.h"
#include "databasemodel.h"
@ -97,11 +99,11 @@ public:
QString sqlCommand() const;
};
template <typename T>
inline Query<T> *createQuery(TableSet<T> *tableSet)
{
return tableSet->query();
}
//template <typename T>
//inline Query<T> *createQuery(TableSet<T> *tableSet)
//{
// return tableSet->query();
//}
template <class T>
Q_OUTOFLINE_TEMPLATE Query<T>::Query(Database *database, TableSetBase *tableSet,

View File

@ -21,7 +21,8 @@
#include "database.h"
#include "tablesetbase_p.h"
#include "databasemodel.h"
#include "tablemodel.h"
#include "table.h"
#include "sqlmodel_p.h"
#include "sqlmodel.h"

View File

@ -20,11 +20,14 @@
#include <QMetaMethod>
#include <QVariant>
#include <QSqlQuery>
#include "table.h"
#include "table_p.h"
#include "database.h"
#include "databasemodel.h"
#include "generators/sqlgeneratorbase_p.h"
#include "tablesetbase_p.h"
NUT_BEGIN_NAMESPACE
@ -42,11 +45,7 @@ NUT_BEGIN_NAMESPACE
Table::Table(QObject *parent) : QObject(parent),
d_ptr(new TablePrivate(this))
{
Q_D(Table);
d->status = NewCreated;
// d->model = TableModel::findByClassName(metaObject()->className());
}
{ }
Table::~Table()
{
@ -62,26 +61,26 @@ void Table::add(TableSetBase *t)
d->childTableSets.insert(t);
}
QString Table::primaryKey() const
{
Q_D(const Table);
return d->model->primaryKey();
}
//QString Table::primaryKey() const
//{
// Q_D(const Table);
// return d->model->primaryKey();
//}
bool Table::isPrimaryKeyAutoIncrement() const
{
Q_D(const Table);
FieldModel *pk = d->model->field(d->model->primaryKey());
if (!pk)
return false;
return pk->isAutoIncrement;
}
//bool Table::isPrimaryKeyAutoIncrement() const
//{
// Q_D(const Table);
// FieldModel *pk = d->model->field(d->model->primaryKey());
// if (!pk)
// return false;
// return pk->isAutoIncrement;
//}
QVariant Table::primaryValue() const
{
return property(primaryKey().toLatin1().data());
}
//QVariant Table::primaryValue() const
//{
// return property(primaryKey().toLatin1().data());
//}
void Table::propertyChanged(const QString &propName)
{
@ -104,6 +103,12 @@ void Table::propertyChanged(const QString &propName)
d->status = Added;
}
void Table::setModel(TableModel *model)
{
Q_D(Table);
d->model = model;
}
void Table::clear()
{
Q_D(Table);
@ -116,21 +121,21 @@ QSet<QString> Table::changedProperties() const
return d->changedProperties;
}
bool Table::setParentTable(Table *master)
bool Table::setParentTable(Table *master, TableModel *masterModel, TableModel *model)
{
Q_D(Table);
QString masterClassName = master->metaObject()->className();
d->refreshModel();
if (!d->model)
d->model = TableModel::findByClassName(metaObject()->className());
// if (!d->model)
// d->model = TableModel::findByClassName(metaObject()->className());
foreach (RelationModel *r, d->model->foregionKeys())
foreach (RelationModel *r, model->foregionKeys())
if(r->masterClassName == masterClassName)
{
setProperty(QString(r->localColumn).toLatin1().data(),
master->primaryValue());
master->property(masterModel->primaryKey().toUtf8().data()));
d->changedProperties.insert(r->localColumn);
return true;
}
@ -202,8 +207,8 @@ TablePrivate::TablePrivate(Table *parent) : q_ptr(parent),
void TablePrivate::refreshModel()
{
Q_Q(Table);
if (!model)
model = TableModel::findByClassName(q->metaObject()->className());
// if (!model)
// model = TableModel::findByClassName(q->metaObject()->className());
}
NUT_END_NAMESPACE

View File

@ -55,14 +55,14 @@ public:
int save(Database *db);
Q_DECL_DEPRECATED
QString primaryKey() const;
// Q_DECL_DEPRECATED
// QString primaryKey() const;
Q_DECL_DEPRECATED
bool isPrimaryKeyAutoIncrement() const;
// Q_DECL_DEPRECATED
// bool isPrimaryKeyAutoIncrement() const;
Q_DECL_DEPRECATED
QVariant primaryValue() const;
// Q_DECL_DEPRECATED
// QVariant primaryValue() const;
Status status() const;
void setStatus(const Status &status);
@ -74,7 +74,7 @@ public:
QSet<QString> changedProperties() const;
bool setParentTable(Table *master);
bool setParentTable(Table *master, TableModel *masterModel, TableModel *model);
signals:
public slots:
@ -83,6 +83,7 @@ protected:
void propertyChanged(const QString &propName);
private:
void setModel(TableModel *model);
// TableModel *myModel;
// Status _status;
// QSet<QString> _changedProperties;
@ -96,6 +97,8 @@ private:
template<class T>
friend class Query;
template<class T>
friend class TableSet;
friend class TableSetBase;
};

View File

@ -101,38 +101,6 @@ QStringList TableModel::fieldsNames() const
return ret;
}
QSet<TableModel *> TableModel::allModels()
{
return _allModels;
}
/*
* This is not used anywhere
*/
TableModel *TableModel::findByTypeId(int typeId)
{
foreach (TableModel *model, _allModels)
if(model->typeId() == typeId)
return model;
return nullptr;
}
/**
* @brief TableModel::findByClassName
* Find a table model by class name
* @param className
* @return
*/
TableModel *TableModel::findByClassName(const QString &className)
{
foreach (TableModel *model, _allModels){
if(model->className() == className)
return model;
}
return nullptr;
}
bool TableModel::operator ==(const TableModel &t) const{
if(_name != t.name())
return false;
@ -157,23 +125,6 @@ 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;
// type = parts[0];
// name = parts[1];
// value = parts[2];
// return true;
// }
//}
TableModel::TableModel(int typeId, const QString &tableName)
{
//TODO: check that
@ -271,9 +222,6 @@ TableModel::TableModel(int typeId, const QString &tableName)
f->isAutoIncrement = true;
}
}
if(!findByTypeId(typeId) && !tableName.isNull())
_allModels.insert(this);
}
/*

View File

@ -87,8 +87,10 @@ struct RelationModel{
QJsonObject toJson() const;
};
bool operator ==(const RelationModel &l, const RelationModel &r);
bool operator !=(const RelationModel &l, const RelationModel &r);
class NUT_EXPORT TableModel
{
public:
@ -98,10 +100,6 @@ public:
QJsonObject toJson() const;
// static TableScheema *registerTable(int typeId, QString tableName);
// static void createForegionKeys();
// static TableModel* model(QString className);
FieldModel *field(int n) const;
FieldModel *field(const QString &name) const;
RelationModel *foregionKey(const QString &otherTable) const;
@ -124,16 +122,6 @@ public:
QList<RelationModel *> foregionKeys() const;
QStringList fieldsNames() const;
Q_DECL_DEPRECATED
static QSet<TableModel *> allModels();
Q_DECL_DEPRECATED
static TableModel *findByTypeId(int typeId);
// static TableModel *findByName(QString name);
Q_DECL_DEPRECATED
static TableModel *findByClassName(const QString &className);
bool operator ==(const TableModel &t) const;
bool operator !=(const TableModel &t) const;
@ -143,9 +131,9 @@ private:
int _typeId;
QList<FieldModel*> _fields;
QList<RelationModel*> _foreignKeys;
Q_DECL_DEPRECATED
static QSet<TableModel*>_allModels;
// bool checkClassInfo(const QMetaClassInfo &classInfo,
// QString &type, QString &name, QString &value);
};
NUT_END_NAMESPACE

View File

@ -30,6 +30,8 @@
#include "tablesetbase_p.h"
#include "table.h"
#include "bulkinserter.h"
//#include "database.h"
#include "databasemodel.h"
NUT_BEGIN_NAMESPACE
@ -37,6 +39,7 @@ template<class T>
class Query;
class BulkInserter;
class Database;
template<class T>
class NUT_EXPORT TableSet : public TableSetBase
{
@ -96,21 +99,24 @@ Q_OUTOFLINE_TEMPLATE int TableSet<T>::length() const
template<class T>
Q_OUTOFLINE_TEMPLATE T *TableSet<T >::at(int i) const
{
return reinterpret_cast<T*>(_tablesList.at(i));
return reinterpret_cast<T*>(_childRows.at(i));
}
template<class T>
Q_OUTOFLINE_TEMPLATE const T &TableSet<T>::operator[](int i) const
{
return _tablesList[i];
return _childRows[i];
}
template<class T>
Q_OUTOFLINE_TEMPLATE void TableSet<T>::append(T *t)
{
_tables.insert(t);
_tablesList.append(t);
// rows.append(t);
_childRows.append(t);
// if (_database)
// t->setModel(_database->model().tableByClassName(t->metaObject()->className()));
t->setParentTableSet(this);
if(t->status() != Table::FeatchedFromDB)
t->setStatus(Table::Added);

View File

@ -26,13 +26,13 @@
NUT_BEGIN_NAMESPACE
TableSetBase::TableSetBase(Database *parent) : QObject(parent),
_database(parent), _table(nullptr), _tableName(QString())
_database(parent), _table(nullptr)//, _tableName(QString())
{
parent->add(this);
}
TableSetBase::TableSetBase(Table *parent) : QObject(parent),
_database(nullptr), _table(parent), _tableName(QString())
_database(nullptr), _table(parent)//, _tableName(QString())
{
parent->add(this);
}
@ -46,9 +46,14 @@ TableSetBase::~TableSetBase()
int TableSetBase::save(Database *db, bool cleanUp)
{
int rowsAffected = 0;
foreach (Table *t, _tablesList) {
TableModel *masterModel = nullptr;
if (_table)
masterModel = db->model().tableByClassName(_table->metaObject()->className());
foreach (Table *t, _childRows) {
if(_table)
t->setParentTable(_table);
t->setParentTable(_table, masterModel,
db->model().tableByClassName(t->metaObject()->className()));
if(t->status() == Table::Added
|| t->status() == Table::Modified
@ -61,30 +66,30 @@ int TableSetBase::save(Database *db, bool cleanUp)
}
if (cleanUp)
_tablesList.clear();
_childRows.clear();
return rowsAffected;
}
void TableSetBase::clearChilds()
{
foreach (Table *t, _tablesList)
foreach (Table *t, _childRows)
t->deleteLater();
_tablesList.clear();
_childRows.clear();
}
void TableSetBase::add(Table *t)
{
if(!_tables.contains(t)){
_tables.insert(t);
_tablesList.append(t);
_childRows.append(t);
}
}
void TableSetBase::remove(Table *t)
{
_tables.remove(t);
_tablesList.removeOne(t);
_childRows.removeOne(t);
}
QString TableSetBase::childClassName() const

View File

@ -48,10 +48,10 @@ public:
protected:
QSet<Table*> _tables;
QList<Table*> _tablesList;
QList<Table*> _childRows;
Database *_database;
Table *_table;
QString _tableName;
// QString _tableName;
QString _childClassName;
private:

View File

@ -4,7 +4,8 @@
#include <qsystemdetection.h>
#define REGISTER(x) qDebug() << (#x) << "type id:" << qMetaTypeId<x*>()
#define PRINT(x) qDebug() << (#x "=") << (x);
#define PRINT(x)
//qDebug() << (#x "=") << (x);
#define TIC() QElapsedTimer timer; timer.start()
#define TOC() qDebug() << QString("Elapsed time: %1ms for %2") \
.arg(timer.elapsed() / 1000.) \

View File

@ -5,6 +5,6 @@ win32 {
LIBDIR = $$absolute_path($$OUT_PWD/../../src)
}
#LIBS += -L$$LIBDIR -lnut
LIBS += -L$$LIBDIR -lnut
INCLUDEPATH += $$PWD/../../src $$PWD/../common
include(../../src/src.pri)
#include(../../src/src.pri)