join in select command
This commit is contained in:
parent
a220657994
commit
8af466e480
|
|
@ -33,7 +33,7 @@
|
|||
#endif
|
||||
|
||||
#define NUT_INFO(type, name, value) \
|
||||
Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #name "\n" #type), #value)
|
||||
Q_CLASSINFO(__nut_NAME_PERFIX #type #name #value, #value)
|
||||
|
||||
// Database
|
||||
//TODO: remove minor version
|
||||
|
|
@ -107,17 +107,4 @@ public: \
|
|||
#define NUT_NOT_NULL(x) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_NOT_NULL), "1")
|
||||
#define NUT_INDEX(name, field, order)
|
||||
|
||||
#ifndef NUT_NO_KEYWORDS
|
||||
# define FROM(x) (x->query())
|
||||
# define WHERE(x) ->setWhere(x)
|
||||
# define JOIN(x) ->join<x>()
|
||||
# define ORDERBY(x) ->orderBy(#x);
|
||||
# define ORDERBY_DESC(x) ->orderBy(!#x);
|
||||
|
||||
# define SELECT() ->toList()
|
||||
# define COUNT() ->count()
|
||||
# define DELETE() ->remove()
|
||||
# define FIRST() ->first()
|
||||
#endif // NUT_NO_KEYWORDS
|
||||
|
||||
#endif // SYNTAX_DEFINES_H
|
||||
|
|
|
|||
|
|
@ -134,4 +134,15 @@ QString MySqlGenerator::phrase(const PhraseData *d) const
|
|||
return SqlGeneratorBase::phrase(d);
|
||||
}
|
||||
|
||||
QString MySqlGenerator::selectCommand(SqlGeneratorBase::AgregateType t, QString agregateArg, QList<WherePhrase> &wheres, QList<WherePhrase> &orders, QStringList joins, int skip, int take)
|
||||
{
|
||||
QString command = SqlGeneratorBase::selectCommand(t, agregateArg, wheres, orders, joins, skip, take);
|
||||
|
||||
if (take != -1 && skip != -1)
|
||||
command.append(QString(" LIMIT %1 OFFSET %2")
|
||||
.arg(skip)
|
||||
.arg(take));
|
||||
return command;
|
||||
}
|
||||
|
||||
NUT_END_NAMESPACE
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ public:
|
|||
QString escapeValue(const QVariant &v) const;
|
||||
QVariant readValue(const QVariant::Type &type, const QVariant &dbValue);
|
||||
QString phrase(const PhraseData *d) const;
|
||||
QString selectCommand(AgregateType t, QString agregateArg, QList<WherePhrase> &wheres, QList<WherePhrase> &orders, QStringList joins, int skip, int take);
|
||||
};
|
||||
|
||||
NUT_END_NAMESPACE
|
||||
|
|
|
|||
|
|
@ -180,11 +180,12 @@ QString SqlGeneratorBase::diff(TableModel *oldTable, TableModel *newTable)
|
|||
|
||||
sql = QString("CREATE TABLE %1 \n(%2)").arg(newTable->name()).arg(
|
||||
columnSql.join(",\n"));
|
||||
qDebug() << sql;
|
||||
}
|
||||
return sql;
|
||||
}
|
||||
|
||||
QString SqlGeneratorBase::join(const QStringList &list)
|
||||
QString SqlGeneratorBase::join(const QStringList &list, QStringList *order)
|
||||
{
|
||||
//TODO: make this ungly code better and bugless :-)
|
||||
/*
|
||||
|
|
@ -215,6 +216,9 @@ QString SqlGeneratorBase::join(const QStringList &list)
|
|||
.arg(rel->localColumn)
|
||||
.arg(mainTable));
|
||||
|
||||
if (order != Q_NULLPTR)
|
||||
order->append(table + "." + rel->localColumn);
|
||||
|
||||
} else{
|
||||
rel = model.relationByTableNames(table, mainTable);
|
||||
if (rel) {
|
||||
|
|
@ -225,6 +229,9 @@ QString SqlGeneratorBase::join(const QStringList &list)
|
|||
.arg(rel->table->primaryKey())
|
||||
.arg(mainTable));
|
||||
|
||||
if (order != Q_NULLPTR)
|
||||
order->append(table + "." + rel->table->primaryKey());
|
||||
|
||||
} else {
|
||||
qInfo("Relation for %s and %s not exists",
|
||||
qPrintable(table), qPrintable(mainTable));
|
||||
|
|
@ -373,16 +380,16 @@ QString SqlGeneratorBase::selectCommand(SqlGeneratorBase::AgregateType t,
|
|||
QString agregateArg,
|
||||
QList<WherePhrase> &wheres,
|
||||
QList<WherePhrase> &orders,
|
||||
QString tableName,
|
||||
QString joinClassName, int skip, int take)
|
||||
QStringList joins, int skip, int take)
|
||||
{
|
||||
Q_UNUSED(take);
|
||||
Q_UNUSED(skip);
|
||||
|
||||
QStringList joinedOrders;
|
||||
QString select = agregateText(t, agregateArg);
|
||||
QString from = join(joins, &joinedOrders);
|
||||
QString where = createWhere(wheres);
|
||||
QString order = "";
|
||||
QString from = fromTableText(tableName, joinClassName, order);
|
||||
QString order = joinedOrders.join(", ");
|
||||
|
||||
foreach (WherePhrase p, orders) {
|
||||
if (order != "")
|
||||
|
|
@ -404,7 +411,7 @@ QString SqlGeneratorBase::selectCommand(SqlGeneratorBase::AgregateType t,
|
|||
|
||||
replaceTableNames(sql);
|
||||
|
||||
return sql;
|
||||
return sql + " ";
|
||||
}
|
||||
|
||||
QString SqlGeneratorBase::createWhere(QList<WherePhrase> &wheres)
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ public:
|
|||
virtual QString diff(FieldModel *oldField, FieldModel *newField);
|
||||
virtual QString diff(TableModel *oldTable, TableModel *newTable);
|
||||
|
||||
virtual QString join(const QStringList &list);
|
||||
virtual QString join(const QStringList &list, QStringList *order = Q_NULLPTR);
|
||||
|
||||
virtual QString saveRecord(Table *t, QString tableName);
|
||||
virtual QString insertRecord(Table *t, QString tableName);
|
||||
|
|
@ -82,8 +82,7 @@ public:
|
|||
QString agregateArg,
|
||||
QList<WherePhrase> &wheres,
|
||||
QList<WherePhrase> &orders,
|
||||
QString tableName,
|
||||
QString joinClassName,
|
||||
QStringList joins,
|
||||
int skip = -1, int take = -1);
|
||||
|
||||
virtual QString deleteCommand(QList<WherePhrase> &wheres, QString tableName);
|
||||
|
|
|
|||
|
|
@ -54,10 +54,9 @@ QString SqliteGenerator::fieldType(FieldModel *field)
|
|||
dbType = "real";
|
||||
break;
|
||||
case QVariant::Int:
|
||||
// if(field->isPrimaryKey)
|
||||
// dbType = "INTEGER PRIMARY KEY";
|
||||
// else
|
||||
dbType = "integer";
|
||||
dbType = "integer";
|
||||
// if (field->isAutoIncrement)
|
||||
// dbType.append(" PRIMARY KEY AUTOINCREMENT");
|
||||
break;
|
||||
case QVariant::String:
|
||||
if(field->length)
|
||||
|
|
|
|||
|
|
@ -135,10 +135,9 @@ QString SqlServerGenerator::escapeValue(const QVariant &v) const
|
|||
|
||||
QString SqlServerGenerator::selectCommand(
|
||||
SqlGeneratorBase::AgregateType t, QString agregateArg,
|
||||
QList<WherePhrase> &wheres, QList<WherePhrase> &orders, QString tableName,
|
||||
QString joinClassName, int skip, int take)
|
||||
QList<WherePhrase> &wheres, QList<WherePhrase> &orders, QStringList joins, int skip, int take)
|
||||
{
|
||||
QString command = SqlGeneratorBase::selectCommand(t, agregateArg, wheres, orders, tableName, joinClassName, skip, take);
|
||||
QString command = SqlGeneratorBase::selectCommand(t, agregateArg, wheres, orders, joins, skip, take);
|
||||
|
||||
if (take != -1 && skip != -1)
|
||||
command.append(QString("OFFSET %1 ROWS FETCH NEXT %2 ROWS ONLY")
|
||||
|
|
|
|||
|
|
@ -40,8 +40,7 @@ public:
|
|||
|
||||
QString selectCommand(AgregateType t, QString agregateArg,
|
||||
QList<WherePhrase> &wheres,
|
||||
QList<WherePhrase> &orders, QString tableName,
|
||||
QString joinClassName, int skip, int take);
|
||||
QList<WherePhrase> &orders, QStringList joins, int skip, int take);
|
||||
};
|
||||
|
||||
NUT_END_NAMESPACE
|
||||
|
|
|
|||
19
src/query.h
19
src/query.h
|
|
@ -126,13 +126,14 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
|||
d->select = "*";
|
||||
|
||||
d->joins.prepend(d->tableName);
|
||||
|
||||
qDebug() << "JOINS="<< d->database->sqlGenertor()->join(d->joins);
|
||||
// QSqlQuery q =
|
||||
// d->database->exec(d->database->sqlGenertor()->selectCommand(d->wheres,
|
||||
// d->orders, d->tableName, d->joinClassName));
|
||||
d->sql = d->database->sqlGenertor()->selectCommand(
|
||||
SqlGeneratorBase::SelectAll, "", d->wheres, d->orderPhrases,
|
||||
d->tableName, d->joinClassName, d->skip, d->take);
|
||||
d->joins, d->skip, d->take);
|
||||
QSqlQuery q = d->database->exec(d->sql);
|
||||
|
||||
// QString pk = TableModel::findByName(d->tableName)->primaryKey();
|
||||
|
|
@ -229,9 +230,11 @@ Q_OUTOFLINE_TEMPLATE QList<F> Query<T>::select(const FieldPhrase<F> f)
|
|||
{
|
||||
Q_D(Query);
|
||||
QList<F> ret;
|
||||
|
||||
d->joins.prepend(d->tableName);
|
||||
d->sql = d->database->sqlGenertor()->selectCommand(
|
||||
SqlGeneratorBase::SignleField, f.data()->text, d->wheres,
|
||||
d->orderPhrases, d->tableName, d->joinClassName, d->skip, d->take);
|
||||
d->orderPhrases, d->joins, d->skip, d->take);
|
||||
|
||||
QSqlQuery q = d->database->exec(d->sql);
|
||||
|
||||
|
|
@ -263,9 +266,10 @@ Q_OUTOFLINE_TEMPLATE int Query<T>::count()
|
|||
{
|
||||
Q_D(Query);
|
||||
|
||||
d->joins.prepend(d->tableName);
|
||||
d->select = "COUNT(*)";
|
||||
d->sql = d->database->sqlGenertor()->selectCommand(SqlGeneratorBase::Count,
|
||||
QStringLiteral("*"), d->wheres, d->orderPhrases, d->tableName, d->joinClassName);
|
||||
QStringLiteral("*"), d->wheres, d->orderPhrases, d->joins);
|
||||
QSqlQuery q = d->database->exec(d->sql);
|
||||
|
||||
if (q.next())
|
||||
|
|
@ -278,9 +282,10 @@ Q_OUTOFLINE_TEMPLATE QVariant Query<T>::max(FieldPhrase<int> &f)
|
|||
{
|
||||
Q_D(Query);
|
||||
|
||||
d->joins.prepend(d->tableName);
|
||||
d->sql = d->database->sqlGenertor()->selectCommand(
|
||||
SqlGeneratorBase::Max, f.data()->text, d->wheres, d->orderPhrases,
|
||||
d->tableName, d->joinClassName);
|
||||
d->joins);
|
||||
QSqlQuery q = d->database->exec(d->sql);
|
||||
|
||||
if (q.next())
|
||||
|
|
@ -293,9 +298,10 @@ Q_OUTOFLINE_TEMPLATE QVariant Query<T>::min(FieldPhrase<int> &f)
|
|||
{
|
||||
Q_D(Query);
|
||||
|
||||
d->joins.prepend(d->tableName);
|
||||
d->sql = d->database->sqlGenertor()->selectCommand(
|
||||
SqlGeneratorBase::Min, f.data()->text, d->wheres, d->orderPhrases,
|
||||
d->tableName, d->joinClassName);
|
||||
d->joins);
|
||||
QSqlQuery q = d->database->exec(d->sql);
|
||||
|
||||
if (q.next())
|
||||
|
|
@ -308,9 +314,10 @@ Q_OUTOFLINE_TEMPLATE QVariant Query<T>::average(FieldPhrase<int> &f)
|
|||
{
|
||||
Q_D(Query);
|
||||
|
||||
d->joins.prepend(d->tableName);
|
||||
d->sql = d->database->sqlGenertor()->selectCommand(
|
||||
SqlGeneratorBase::Average, f.data()->text, d->wheres, d->orderPhrases,
|
||||
d->tableName, d->joinClassName);
|
||||
d->joins);
|
||||
QSqlQuery q = d->database->exec(d->sql);
|
||||
|
||||
if (q.next())
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class User : public Nut::Table
|
|||
Q_OBJECT
|
||||
|
||||
NUT_PRIMARY_AUTO_INCREMENT(id)
|
||||
NUT_DECLARE_FIELD(QUuid, id, id, setId)
|
||||
NUT_DECLARE_FIELD(int, id, id, setId)
|
||||
|
||||
NUT_NOT_NULL(username)
|
||||
NUT_LEN(username, 50)
|
||||
|
|
|
|||
Loading…
Reference in New Issue