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",
|
||||
db.lastError().text().toLatin1().data());
|
||||
}
|
||||
storeScheemaInDB();
|
||||
bool ok = db.commit();
|
||||
|
||||
if (db.lastError().type() == QSqlError::NoError) {
|
||||
storeScheemaInDB();
|
||||
|
||||
q->databaseUpdated(last.versionMajor(), last.versionMinor(),
|
||||
current.versionMajor(), current.versionMinor());
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@
|
|||
NUT_BEGIN_NAMESPACE
|
||||
|
||||
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> *skip(int &n);
|
||||
Query<T> *take(int &n);
|
||||
Query<T> *orderBy(WherePhrase phrase);
|
||||
|
||||
int count();
|
||||
|
|
@ -81,6 +83,7 @@ public:
|
|||
template <typename T>
|
||||
inline Query<T> *createQuery(TableSet<T> *tableSet)
|
||||
{
|
||||
return tableSet->query();
|
||||
}
|
||||
|
||||
template <class T>
|
||||
|
|
@ -120,7 +123,7 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
|||
// d->orders, d->tableName, d->joinClassName));
|
||||
d->sql = d->database->sqlGenertor()->selectCommand(
|
||||
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);
|
||||
|
||||
// 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;
|
||||
d->sql = d->database->sqlGenertor()->selectCommand(
|
||||
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);
|
||||
|
||||
|
|
@ -319,6 +322,22 @@ Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::setWhere(WherePhrase where)
|
|||
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>
|
||||
//Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::orderBy(QString fieldName,
|
||||
// QString type)
|
||||
|
|
|
|||
|
|
@ -49,6 +49,8 @@ public:
|
|||
QList<WherePhrase> wheres;
|
||||
QList<WherePhrase> orderPhrases;
|
||||
QHash<QString, QString> orders;
|
||||
int skip;
|
||||
int take;
|
||||
};
|
||||
|
||||
NUT_END_NAMESPACE
|
||||
|
|
|
|||
|
|
@ -65,8 +65,7 @@ QString SqlGeneratorBase::saveRecord(Table *t, QString tableName)
|
|||
|
||||
case Table::NewCreated:
|
||||
case Table::FeatchedFromDB:
|
||||
// disable compiler warning
|
||||
return "";
|
||||
Q_UNREACHABLE();
|
||||
}
|
||||
|
||||
return "";
|
||||
|
|
@ -308,7 +307,7 @@ QString SqlGeneratorBase::selectCommand(SqlGeneratorBase::AgregateType t,
|
|||
QList<WherePhrase> &wheres,
|
||||
QList<WherePhrase> &orders,
|
||||
QString tableName,
|
||||
QString joinClassName)
|
||||
QString joinClassName, int skip, int take)
|
||||
{
|
||||
QString select = agregateText(t, agregateArg);
|
||||
QString where = createWhere(wheres);
|
||||
|
|
|
|||
|
|
@ -81,7 +81,8 @@ public:
|
|||
QList<WherePhrase> &wheres,
|
||||
QList<WherePhrase> &orders,
|
||||
QString tableName,
|
||||
QString joinClassName);
|
||||
QString joinClassName,
|
||||
int skip = -1, int take = -1);
|
||||
|
||||
virtual QString deleteCommand(QList<WherePhrase> &wheres, QString tableName);
|
||||
|
||||
|
|
|
|||
|
|
@ -27,14 +27,17 @@
|
|||
|
||||
NUT_BEGIN_NAMESPACE
|
||||
|
||||
SqlServerGenerator::SqlServerGenerator(Database *parent) : SqlGeneratorBase(parent)
|
||||
SqlServerGenerator::SqlServerGenerator(Database *parent)
|
||||
: SqlGeneratorBase(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
|
|
@ -48,7 +51,7 @@ QString SqlServerGenerator::fieldType(FieldModel *field)
|
|||
case QVariant::ByteArray:
|
||||
dbType = "VARBINARY";
|
||||
|
||||
if(field->length)
|
||||
if (field->length)
|
||||
dbType.append(" (" + QString::number(field->length) + ")");
|
||||
else
|
||||
dbType.append(" (MAX)");
|
||||
|
|
@ -67,7 +70,7 @@ QString SqlServerGenerator::fieldType(FieldModel *field)
|
|||
break;
|
||||
case QVariant::Int:
|
||||
dbType = "INT";
|
||||
if(field->isAutoIncrement)
|
||||
if (field->isAutoIncrement)
|
||||
dbType += " IDENTITY(1,1)";
|
||||
break;
|
||||
|
||||
|
|
@ -77,7 +80,7 @@ QString SqlServerGenerator::fieldType(FieldModel *field)
|
|||
break;
|
||||
|
||||
case QVariant::String:
|
||||
if(field->length)
|
||||
if (field->length)
|
||||
dbType = QString("NVARCHAR(%1)").arg(field->length);
|
||||
else
|
||||
dbType = "NVARCHAR(MAX)";
|
||||
|
|
@ -97,14 +100,14 @@ QString SqlServerGenerator::fieldType(FieldModel *field)
|
|||
QString SqlServerGenerator::diff(FieldModel *oldField, FieldModel *newField)
|
||||
{
|
||||
QString sql = "";
|
||||
if(oldField && newField)
|
||||
if(*oldField == *newField)
|
||||
if (oldField && newField)
|
||||
if (*oldField == *newField)
|
||||
return QString::null;
|
||||
|
||||
if(!newField){
|
||||
if (!newField) {
|
||||
sql = "DROP COLUMN " + oldField->name;
|
||||
}else{
|
||||
if(oldField)
|
||||
} else {
|
||||
if (oldField)
|
||||
sql = "MODIFY COLUMN ";
|
||||
else
|
||||
sql = "ADD ";
|
||||
|
|
@ -116,16 +119,32 @@ QString SqlServerGenerator::diff(FieldModel *oldField, FieldModel *newField)
|
|||
|
||||
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() + "'";
|
||||
else if (v.type() == QVariant::Point) {
|
||||
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) {
|
||||
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
|
||||
|
|
|
|||
|
|
@ -30,13 +30,18 @@ class SqlServerGenerator : public SqlGeneratorBase
|
|||
{
|
||||
public:
|
||||
SqlServerGenerator(Database *parent = 0);
|
||||
|
||||
|
||||
QString masterDatabaseName(QString databaseName);
|
||||
|
||||
QString fieldType(FieldModel *field);
|
||||
QString diff(FieldModel *oldField, FieldModel *newField);
|
||||
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in New Issue