From 45ae61d9d2862d8c3a02d5a7d9bd91a9256dbffe Mon Sep 17 00:00:00 2001 From: Michael Dougras da Silva Date: Thu, 25 Sep 2014 20:31:10 -0300 Subject: [PATCH] Added a method to retrieve a list of values (single column) when performing a select. Fixed the use of more than one comparison for the same property. Added a method to retrieve the database connection name used by the repository. --- Orm4Qt/repository.h | 88 ++++++++++++++++++++++++++++++++-- Orm4Qt/standardsqlprovider.cpp | 9 +++- 2 files changed, 91 insertions(+), 6 deletions(-) diff --git a/Orm4Qt/repository.h b/Orm4Qt/repository.h index 12a0e0a..16d69a2 100644 --- a/Orm4Qt/repository.h +++ b/Orm4Qt/repository.h @@ -234,7 +234,7 @@ namespace Orm4Qt */ template bool select(QList &list, const Where &where = Where(), const QStringList &fields = QStringList(), - const QList> orderby = QList>(), int limit=-1, int offset=-1) + const QList> orderby = QList>(), int limit=-1, int offset=0) { //Try to open connection with the database if(!openConnection()) @@ -331,27 +331,95 @@ namespace Orm4Qt template bool select(QList &list, const QStringList &fields, const QList> orderby = QList>(), int limit=-1, int offset = 0) { - return select(list, Where(), fields, orderby, limit, offset); + return select(list, Where(), fields, orderby, limit, offset); } template bool select(QList &list, const Where &where, const QList> orderby, int limit=-1, int offset = 0) { - return select(list, where, QStringList(), orderby, limit, offset); + return select(list, where, QStringList(), orderby, limit, offset); } template bool select(QList &list, const QList> orderby, int limit=-1, int offset = 0) { - return select(list, Where(), QStringList(), orderby, limit, offset); + return select(list, Where(), QStringList(), orderby, limit, offset); } template bool select(QList &list, int limit, int offset=0) { - return select(list, Where(), QStringList(), QList>(), limit, offset); + return select(list, Where(), QStringList(), QList>(), limit, offset); } + template + bool select(QList &list, const Where &where, int limit, int offset=0) + { + return select(list, where, QStringList(), QList>(), limit, offset); + } + + template + bool selectValueList(QList &list, const QString &field, const Where &where = Where(), + const QList> orderby = QList>(), int limit=-1, int offset=0) + { + //Try to open connection with the database + if(!openConnection()) + return false; + + //Create an object of the type to be selected to obtain the reflection object + T temp; + + //Try to create the query + QList indexes = fieldIndexes(temp.reflection().get(), {field}); + std::shared_ptr query = m_provider->generateSelect(temp.reflection().get(), where, indexes, orderby, offset, limit); + if(query == nullptr) + { + m_lastError = m_provider->lastError(); + return false; + } +#ifdef ORM4QT_DEBUG_SL + //Debug the sql generated + qCDebug(ORM4QT_SL) << getLastExecutedQuery(*query.get()); +#endif + //Try execute the query + if(query->exec()) + { + //Reading the result + while(query->next()) + { + list.append(query->value(0)); + } + return true; + } + else + { + m_lastError = std::shared_ptr(new OrmError(DatabaseError, QString("An error occurred during the select of values in the database. See the sqlerror attached."), query->lastError())); + return false; + } + } + + template + bool selectValueList(QList &list, const QString &field, + const QList> orderby, int limit=-1, int offset=0) + { + return selectValueList(list, field, Where(), orderby, limit, offset); + } + + template + bool selectValueList(QList &list, const QString &field, + int limit, int offset=0) + { + return selectValueList(list, field, QList>(), limit, offset); + } + + template + bool selectValueList(QList &list, const QString &field, const Where &where, + int limit, int offset=0) + { + return selectValueList(list, field, where, QList>(), limit, offset); + } + + /** * Open a transaction in the database connection. * @return @@ -418,6 +486,16 @@ namespace Orm4Qt } } + /** + * Return the name of the database connection used by this repository. + * @return + * The current database connection name. + */ + QString databaseConnectionName() const + { + return m_provider->databaseConnectionName(); + } + private: //Store the sql provider used to create the query objects std::shared_ptr m_provider; diff --git a/Orm4Qt/standardsqlprovider.cpp b/Orm4Qt/standardsqlprovider.cpp index b3d6648..f60ffe3 100644 --- a/Orm4Qt/standardsqlprovider.cpp +++ b/Orm4Qt/standardsqlprovider.cpp @@ -387,6 +387,7 @@ std::shared_ptr StandardSqlProvider::generateSelect(Class *reflect, c //Init the where clause QString wherestr = "WHERE "; const Where *whereptr = &where; + int fieldDiff = 0; //Sufix inserted in the property name before adding it in the where clause //Iterating over the where clauses joined while(whereptr) { @@ -430,9 +431,12 @@ std::shared_ptr StandardSqlProvider::generateSelect(Class *reflect, c wherestr += field + " "; } + //Include the sufix in the name of the property + field.append(QString("%1").arg(fieldDiff)); + ++fieldDiff; //Prepare the field to be used as a placeholder field.prepend(":"); - //Char used to diff multiple arguments + //Char used to diff multiple arguments in the in and notin comparisons char diff = 'a'; //Check the type of operation switch(whereptr->operation()) @@ -625,6 +629,7 @@ std::shared_ptr StandardSqlProvider::generateCount(Class *reflect, co //Init the where clause QString wherestr = "WHERE "; const Where *whereptr = &where; + int fieldDiff = 0; //Sufix inserted in the property name before adding it in the where clause //Iterating over the where clauses joined while(whereptr) { @@ -667,6 +672,8 @@ std::shared_ptr StandardSqlProvider::generateCount(Class *reflect, co wherestr += field + " "; } + //Inserting the sufix in the name of the property + field.append(QString("%1").arg(fieldDiff++)); //Prepare the field to be used as a placeholder field.prepend(":"); //Char used to diff multiple arguments