make BulkInserter movable
This commit is contained in:
parent
22195353c1
commit
d29795ea09
|
|
@ -1,4 +1,6 @@
|
||||||
#include "bulkinserter.h"
|
#include "bulkinserter.h"
|
||||||
|
#include "bulkinserter_p.h"
|
||||||
|
|
||||||
#include "phrases/phraselist.h"
|
#include "phrases/phraselist.h"
|
||||||
#include "database.h"
|
#include "database.h"
|
||||||
#include "abstractsqlgenerator.h"
|
#include "abstractsqlgenerator.h"
|
||||||
|
|
@ -6,23 +8,47 @@
|
||||||
|
|
||||||
#include <QtCore/QDebug>
|
#include <QtCore/QDebug>
|
||||||
|
|
||||||
Nut::BulkInserter::BulkInserter(Nut::Database *db, QString &className)
|
NUT_BEGIN_NAMESPACE
|
||||||
: _database(db), _fieldCount(0)
|
|
||||||
|
BulkInserterPrivate::BulkInserterPrivate(Database *db)
|
||||||
|
: database(db), fieldCount(0)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
BulkInserter::BulkInserter(Database *db, QString &className)
|
||||||
|
: d_ptr(new BulkInserterPrivate(db))
|
||||||
|
{
|
||||||
|
Q_D(BulkInserter);
|
||||||
|
|
||||||
foreach (TableModel *m, db->model())
|
foreach (TableModel *m, db->model())
|
||||||
if (m->className() == className)
|
if (m->className() == className)
|
||||||
_className = m->name();
|
d->className = m->name();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Nut::BulkInserter::setFields(const Nut::PhraseList &ph)
|
BulkInserter::BulkInserter(const BulkInserter &other)
|
||||||
{
|
{
|
||||||
_fields = ph;
|
d_ptr = other.d_ptr;
|
||||||
_fieldCount = static_cast<size_t>(ph.data.count());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Nut::BulkInserter::insert(std::initializer_list<QVariant> vars)
|
BulkInserter::BulkInserter(BulkInserter &&other)
|
||||||
{
|
{
|
||||||
if (vars.size() != _fieldCount) {
|
d_ptr = other.d_ptr;
|
||||||
|
other.d_ptr = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BulkInserter::setFields(const PhraseList &ph)
|
||||||
|
{
|
||||||
|
Q_D(BulkInserter);
|
||||||
|
d->fields = ph;
|
||||||
|
d->fieldCount = static_cast<size_t>(ph.data.count());
|
||||||
|
}
|
||||||
|
|
||||||
|
void BulkInserter::insert(std::initializer_list<QVariant> vars)
|
||||||
|
{
|
||||||
|
Q_D(BulkInserter);
|
||||||
|
|
||||||
|
if (vars.size() != d->fieldCount) {
|
||||||
qInfo("Number of rows mistake");
|
qInfo("Number of rows mistake");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -31,13 +57,17 @@ void Nut::BulkInserter::insert(std::initializer_list<QVariant> vars)
|
||||||
std::initializer_list<QVariant>::iterator it;
|
std::initializer_list<QVariant>::iterator it;
|
||||||
for (it = vars.begin(); it != vars.end(); ++it)
|
for (it = vars.begin(); it != vars.end(); ++it)
|
||||||
list.append(*it);
|
list.append(*it);
|
||||||
variants.append(list);
|
d->variants.append(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Nut::BulkInserter::apply()
|
int BulkInserter::apply()
|
||||||
{
|
{
|
||||||
auto sql = _database->sqlGenerator()->insertBulk(_className, _fields, variants);
|
Q_D(BulkInserter);
|
||||||
QSqlQuery q = _database->exec(sql);
|
auto sql = d->database->sqlGenerator()->insertBulk(d->className,
|
||||||
|
d->fields,
|
||||||
|
d->variants);
|
||||||
|
QSqlQuery q = d->database->exec(sql);
|
||||||
return q.numRowsAffected();
|
return q.numRowsAffected();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NUT_END_NAMESPACE
|
||||||
|
|
|
||||||
|
|
@ -12,16 +12,16 @@ NUT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
class PhraseList;
|
class PhraseList;
|
||||||
class Database;
|
class Database;
|
||||||
|
class BulkInserterPrivate;
|
||||||
class NUT_EXPORT BulkInserter
|
class NUT_EXPORT BulkInserter
|
||||||
{
|
{
|
||||||
Database *_database;
|
Q_DECLARE_PRIVATE(BulkInserter);
|
||||||
QString _className;
|
|
||||||
Nut::PhraseList _fields;
|
|
||||||
QList<QVariantList> variants;
|
|
||||||
size_t _fieldCount;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BulkInserter(Database *db, QString &className);
|
BulkInserter(Database *db, QString &className);
|
||||||
|
BulkInserter(const BulkInserter &other);
|
||||||
|
BulkInserter(BulkInserter &&other);
|
||||||
|
|
||||||
void setFields(const PhraseList &ph);
|
void setFields(const PhraseList &ph);
|
||||||
|
|
||||||
void insert(std::initializer_list<QVariant> vars);
|
void insert(std::initializer_list<QVariant> vars);
|
||||||
|
|
@ -30,6 +30,9 @@ public:
|
||||||
insert({args...});
|
insert({args...});
|
||||||
}
|
}
|
||||||
int apply();
|
int apply();
|
||||||
|
|
||||||
|
private:
|
||||||
|
BulkInserterPrivate *d_ptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
NUT_END_NAMESPACE
|
NUT_END_NAMESPACE
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
#ifndef BULKINSERTER_P_H
|
||||||
|
#define BULKINSERTER_P_H
|
||||||
|
|
||||||
|
#include <QtNut/phraselist.h>
|
||||||
|
|
||||||
|
NUT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
|
class Database;
|
||||||
|
class BulkInserterPrivate
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
BulkInserterPrivate(Database *db);
|
||||||
|
|
||||||
|
Database *database;
|
||||||
|
QString className;
|
||||||
|
PhraseList fields;
|
||||||
|
QList<QVariantList> variants;
|
||||||
|
size_t fieldCount;
|
||||||
|
};
|
||||||
|
|
||||||
|
NUT_END_NAMESPACE
|
||||||
|
|
||||||
|
#endif // BULKINSERTER_P_H
|
||||||
|
|
@ -47,7 +47,8 @@ HEADERS += \
|
||||||
$$PWD/phrases/phrasedatalist.h \
|
$$PWD/phrases/phrasedatalist.h \
|
||||||
$$PWD/phrases/phraselist.h \
|
$$PWD/phrases/phraselist.h \
|
||||||
$$PWD/phrases/datephrase.h \
|
$$PWD/phrases/datephrase.h \
|
||||||
$$PWD/table_p.h
|
$$PWD/table_p.h \
|
||||||
|
bulkinserter_p.h
|
||||||
|
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
$$PWD/generators/abstractsqlgenerator.cpp \
|
$$PWD/generators/abstractsqlgenerator.cpp \
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ public:
|
||||||
Row<T> operator[](int i) const;
|
Row<T> operator[](int i) const;
|
||||||
|
|
||||||
Query<T> query();
|
Query<T> query();
|
||||||
BulkInserter *bulkInserter();
|
BulkInserter bulkInserter();
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
|
|
@ -86,10 +86,9 @@ Q_OUTOFLINE_TEMPLATE Query<T> TableSet<T>::query()
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
Q_OUTOFLINE_TEMPLATE BulkInserter *TableSet<T>::bulkInserter()
|
Q_OUTOFLINE_TEMPLATE BulkInserter TableSet<T>::bulkInserter()
|
||||||
{
|
{
|
||||||
BulkInserter *bi = new BulkInserter(data->database, data->childClassName);
|
return BulkInserter(data->database, data->childClassName);
|
||||||
return bi;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue