skip and take
This commit is contained in:
parent
4647494735
commit
c4f0c30b0f
|
|
@ -131,10 +131,10 @@ bool DatabasePrivate::updateDatabase()
|
||||||
qWarning("Error executing sql command, %s",
|
qWarning("Error executing sql command, %s",
|
||||||
db.lastError().text().toLatin1().data());
|
db.lastError().text().toLatin1().data());
|
||||||
}
|
}
|
||||||
|
storeScheemaInDB();
|
||||||
bool ok = db.commit();
|
bool ok = db.commit();
|
||||||
|
|
||||||
if (db.lastError().type() == QSqlError::NoError) {
|
if (db.lastError().type() == QSqlError::NoError) {
|
||||||
storeScheemaInDB();
|
|
||||||
|
|
||||||
q->databaseUpdated(last.versionMajor(), last.versionMinor(),
|
q->databaseUpdated(last.versionMajor(), last.versionMinor(),
|
||||||
current.versionMajor(), current.versionMinor());
|
current.versionMajor(), current.versionMinor());
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@
|
||||||
NUT_BEGIN_NAMESPACE
|
NUT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
QueryPrivate::QueryPrivate(QueryBase *parent) : q_ptr(parent),
|
QueryPrivate::QueryPrivate(QueryBase *parent) : q_ptr(parent),
|
||||||
joinClassName(QString::null)
|
joinClassName(QString::null), skip(-1), take(-1)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
23
src/query.h
23
src/query.h
|
|
@ -61,6 +61,8 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
// Query<T> *orderBy(QString fieldName, QString type);
|
// Query<T> *orderBy(QString fieldName, QString type);
|
||||||
|
Query<T> *skip(int &n);
|
||||||
|
Query<T> *take(int &n);
|
||||||
Query<T> *orderBy(WherePhrase phrase);
|
Query<T> *orderBy(WherePhrase phrase);
|
||||||
|
|
||||||
int count();
|
int count();
|
||||||
|
|
@ -81,6 +83,7 @@ public:
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline Query<T> *createQuery(TableSet<T> *tableSet)
|
inline Query<T> *createQuery(TableSet<T> *tableSet)
|
||||||
{
|
{
|
||||||
|
return tableSet->query();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
|
|
@ -120,7 +123,7 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
||||||
// d->orders, d->tableName, d->joinClassName));
|
// d->orders, d->tableName, d->joinClassName));
|
||||||
d->sql = d->database->sqlGenertor()->selectCommand(
|
d->sql = d->database->sqlGenertor()->selectCommand(
|
||||||
SqlGeneratorBase::SelectAll, "", d->wheres, d->orderPhrases,
|
SqlGeneratorBase::SelectAll, "", d->wheres, d->orderPhrases,
|
||||||
d->tableName, d->joinClassName);
|
d->tableName, d->joinClassName, d->skip, d->take);
|
||||||
QSqlQuery q = d->database->exec(d->sql);
|
QSqlQuery q = d->database->exec(d->sql);
|
||||||
|
|
||||||
// QString pk = TableModel::findByName(d->tableName)->primaryKey();
|
// QString pk = TableModel::findByName(d->tableName)->primaryKey();
|
||||||
|
|
@ -218,7 +221,7 @@ Q_OUTOFLINE_TEMPLATE QList<F> Query<T>::select(const FieldPhrase<F> f)
|
||||||
QList<F> ret;
|
QList<F> ret;
|
||||||
d->sql = d->database->sqlGenertor()->selectCommand(
|
d->sql = d->database->sqlGenertor()->selectCommand(
|
||||||
SqlGeneratorBase::SignleField, f.data()->text, d->wheres,
|
SqlGeneratorBase::SignleField, f.data()->text, d->wheres,
|
||||||
d->orderPhrases, d->tableName, d->joinClassName);
|
d->orderPhrases, d->tableName, d->joinClassName, d->skip, d->take);
|
||||||
|
|
||||||
QSqlQuery q = d->database->exec(d->sql);
|
QSqlQuery q = d->database->exec(d->sql);
|
||||||
|
|
||||||
|
|
@ -319,6 +322,22 @@ Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::setWhere(WherePhrase where)
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::skip(int &n)
|
||||||
|
{
|
||||||
|
Q_D(Query);
|
||||||
|
d->skip = n;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::take(int &n)
|
||||||
|
{
|
||||||
|
Q_D(Query);
|
||||||
|
d->take = n;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
//template <class T>
|
//template <class T>
|
||||||
//Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::orderBy(QString fieldName,
|
//Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::orderBy(QString fieldName,
|
||||||
// QString type)
|
// QString type)
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,8 @@ public:
|
||||||
QList<WherePhrase> wheres;
|
QList<WherePhrase> wheres;
|
||||||
QList<WherePhrase> orderPhrases;
|
QList<WherePhrase> orderPhrases;
|
||||||
QHash<QString, QString> orders;
|
QHash<QString, QString> orders;
|
||||||
|
int skip;
|
||||||
|
int take;
|
||||||
};
|
};
|
||||||
|
|
||||||
NUT_END_NAMESPACE
|
NUT_END_NAMESPACE
|
||||||
|
|
|
||||||
|
|
@ -65,8 +65,7 @@ QString SqlGeneratorBase::saveRecord(Table *t, QString tableName)
|
||||||
|
|
||||||
case Table::NewCreated:
|
case Table::NewCreated:
|
||||||
case Table::FeatchedFromDB:
|
case Table::FeatchedFromDB:
|
||||||
// disable compiler warning
|
Q_UNREACHABLE();
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
|
|
@ -308,7 +307,7 @@ QString SqlGeneratorBase::selectCommand(SqlGeneratorBase::AgregateType t,
|
||||||
QList<WherePhrase> &wheres,
|
QList<WherePhrase> &wheres,
|
||||||
QList<WherePhrase> &orders,
|
QList<WherePhrase> &orders,
|
||||||
QString tableName,
|
QString tableName,
|
||||||
QString joinClassName)
|
QString joinClassName, int skip, int take)
|
||||||
{
|
{
|
||||||
QString select = agregateText(t, agregateArg);
|
QString select = agregateText(t, agregateArg);
|
||||||
QString where = createWhere(wheres);
|
QString where = createWhere(wheres);
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,8 @@ public:
|
||||||
QList<WherePhrase> &wheres,
|
QList<WherePhrase> &wheres,
|
||||||
QList<WherePhrase> &orders,
|
QList<WherePhrase> &orders,
|
||||||
QString tableName,
|
QString tableName,
|
||||||
QString joinClassName);
|
QString joinClassName,
|
||||||
|
int skip = -1, int take = -1);
|
||||||
|
|
||||||
virtual QString deleteCommand(QList<WherePhrase> &wheres, QString tableName);
|
virtual QString deleteCommand(QList<WherePhrase> &wheres, QString tableName);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,14 +27,17 @@
|
||||||
|
|
||||||
NUT_BEGIN_NAMESPACE
|
NUT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
SqlServerGenerator::SqlServerGenerator(Database *parent) : SqlGeneratorBase(parent)
|
SqlServerGenerator::SqlServerGenerator(Database *parent)
|
||||||
|
: SqlGeneratorBase(parent)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString SqlServerGenerator::masterDatabaseName(QString databaseName)
|
QString SqlServerGenerator::masterDatabaseName(QString databaseName)
|
||||||
{
|
{
|
||||||
return databaseName.replace(QRegularExpression("DATABASE\\=(\\w+)", QRegularExpression::CaseInsensitiveOption), "DATABASE=");
|
return databaseName.replace(
|
||||||
|
QRegularExpression("DATABASE\\=(\\w+)",
|
||||||
|
QRegularExpression::CaseInsensitiveOption),
|
||||||
|
"DATABASE=");
|
||||||
}
|
}
|
||||||
|
|
||||||
QString SqlServerGenerator::fieldType(FieldModel *field)
|
QString SqlServerGenerator::fieldType(FieldModel *field)
|
||||||
|
|
@ -48,7 +51,7 @@ QString SqlServerGenerator::fieldType(FieldModel *field)
|
||||||
case QVariant::ByteArray:
|
case QVariant::ByteArray:
|
||||||
dbType = "VARBINARY";
|
dbType = "VARBINARY";
|
||||||
|
|
||||||
if(field->length)
|
if (field->length)
|
||||||
dbType.append(" (" + QString::number(field->length) + ")");
|
dbType.append(" (" + QString::number(field->length) + ")");
|
||||||
else
|
else
|
||||||
dbType.append(" (MAX)");
|
dbType.append(" (MAX)");
|
||||||
|
|
@ -67,7 +70,7 @@ QString SqlServerGenerator::fieldType(FieldModel *field)
|
||||||
break;
|
break;
|
||||||
case QVariant::Int:
|
case QVariant::Int:
|
||||||
dbType = "INT";
|
dbType = "INT";
|
||||||
if(field->isAutoIncrement)
|
if (field->isAutoIncrement)
|
||||||
dbType += " IDENTITY(1,1)";
|
dbType += " IDENTITY(1,1)";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
@ -77,7 +80,7 @@ QString SqlServerGenerator::fieldType(FieldModel *field)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case QVariant::String:
|
case QVariant::String:
|
||||||
if(field->length)
|
if (field->length)
|
||||||
dbType = QString("NVARCHAR(%1)").arg(field->length);
|
dbType = QString("NVARCHAR(%1)").arg(field->length);
|
||||||
else
|
else
|
||||||
dbType = "NVARCHAR(MAX)";
|
dbType = "NVARCHAR(MAX)";
|
||||||
|
|
@ -97,14 +100,14 @@ QString SqlServerGenerator::fieldType(FieldModel *field)
|
||||||
QString SqlServerGenerator::diff(FieldModel *oldField, FieldModel *newField)
|
QString SqlServerGenerator::diff(FieldModel *oldField, FieldModel *newField)
|
||||||
{
|
{
|
||||||
QString sql = "";
|
QString sql = "";
|
||||||
if(oldField && newField)
|
if (oldField && newField)
|
||||||
if(*oldField == *newField)
|
if (*oldField == *newField)
|
||||||
return QString::null;
|
return QString::null;
|
||||||
|
|
||||||
if(!newField){
|
if (!newField) {
|
||||||
sql = "DROP COLUMN " + oldField->name;
|
sql = "DROP COLUMN " + oldField->name;
|
||||||
}else{
|
} else {
|
||||||
if(oldField)
|
if (oldField)
|
||||||
sql = "MODIFY COLUMN ";
|
sql = "MODIFY COLUMN ";
|
||||||
else
|
else
|
||||||
sql = "ADD ";
|
sql = "ADD ";
|
||||||
|
|
@ -116,16 +119,32 @@ QString SqlServerGenerator::diff(FieldModel *oldField, FieldModel *newField)
|
||||||
|
|
||||||
QString SqlServerGenerator::escapeValue(const QVariant &v) const
|
QString SqlServerGenerator::escapeValue(const QVariant &v) const
|
||||||
{
|
{
|
||||||
if(v.type() == QVariant::String || v.type() == QVariant::Char)
|
if (v.type() == QVariant::String || v.type() == QVariant::Char)
|
||||||
return "N'" + v.toString() + "'";
|
return "N'" + v.toString() + "'";
|
||||||
else if (v.type() == QVariant::Point) {
|
else if (v.type() == QVariant::Point) {
|
||||||
QPoint pt = v.toPoint();
|
QPoint pt = v.toPoint();
|
||||||
return QString("geography::POINT(%1, %2, 4326)").arg(pt.x()).arg(pt.y());
|
return QString("geography::POINT(%1, %2, 4326)").arg(pt.x()).arg(
|
||||||
|
pt.y());
|
||||||
} else if (v.type() == QVariant::Point) {
|
} else if (v.type() == QVariant::Point) {
|
||||||
QPointF pt = v.toPointF();
|
QPointF pt = v.toPointF();
|
||||||
return QString("geography::POINT(%1, %2, 4326)").arg(pt.x()).arg(pt.y());
|
return QString("geography::POINT(%1, %2, 4326)").arg(pt.x()).arg(
|
||||||
|
pt.y());
|
||||||
}
|
}
|
||||||
return SqlGeneratorBase::escapeValue(v);
|
return SqlGeneratorBase::escapeValue(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString SqlServerGenerator::selectCommand(
|
||||||
|
SqlGeneratorBase::AgregateType t, QString agregateArg,
|
||||||
|
QList<WherePhrase> &wheres, QList<WherePhrase> &orders, QString tableName,
|
||||||
|
QString joinClassName, int skip, int take)
|
||||||
|
{
|
||||||
|
QString command = SqlGeneratorBase::selectCommand(t, agregateArg, wheres, orders, tableName, joinClassName, skip, take);
|
||||||
|
|
||||||
|
if (take != -1 && skip != -1)
|
||||||
|
command.append(QString("OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY")
|
||||||
|
.arg(skip)
|
||||||
|
.arg(take));
|
||||||
|
return command;
|
||||||
}
|
}
|
||||||
|
|
||||||
NUT_END_NAMESPACE
|
NUT_END_NAMESPACE
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,11 @@ public:
|
||||||
QString diff(FieldModel *oldField, FieldModel *newField);
|
QString diff(FieldModel *oldField, FieldModel *newField);
|
||||||
|
|
||||||
QString escapeValue(const QVariant &v) const;
|
QString escapeValue(const QVariant &v) const;
|
||||||
|
|
||||||
|
QString selectCommand(AgregateType t, QString agregateArg,
|
||||||
|
QList<WherePhrase> &wheres,
|
||||||
|
QList<WherePhrase> &orders, QString tableName,
|
||||||
|
QString joinClassName, int skip, int take);
|
||||||
};
|
};
|
||||||
|
|
||||||
NUT_END_NAMESPACE
|
NUT_END_NAMESPACE
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue