Skip to content

Commit

Permalink
Added a method to retrieve a list of values (single column) when
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
micdoug committed Sep 25, 2014
1 parent f7e4c6d commit 45ae61d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 6 deletions.
88 changes: 83 additions & 5 deletions Orm4Qt/repository.h
Expand Up @@ -234,7 +234,7 @@ namespace Orm4Qt
*/
template<class T>
bool select(QList<T> &list, const Where &where = Where(), const QStringList &fields = QStringList(),
const QList<QPair<QString, OrderBy>> orderby = QList<QPair<QString, OrderBy>>(), int limit=-1, int offset=-1)
const QList<QPair<QString, OrderBy>> orderby = QList<QPair<QString, OrderBy>>(), int limit=-1, int offset=0)
{
//Try to open connection with the database
if(!openConnection())
Expand Down Expand Up @@ -331,27 +331,95 @@ namespace Orm4Qt
template<class T>
bool select(QList<T> &list, const QStringList &fields, const QList<QPair<QString, OrderBy>> orderby = QList<QPair<QString, OrderBy>>(), int limit=-1, int offset = 0)
{
return select(list, Where(), fields, orderby, limit, offset);
return select<T>(list, Where(), fields, orderby, limit, offset);
}

template<class T>
bool select(QList<T> &list, const Where &where, const QList<QPair<QString, OrderBy>> orderby, int limit=-1, int offset = 0)
{
return select(list, where, QStringList(), orderby, limit, offset);
return select<T>(list, where, QStringList(), orderby, limit, offset);
}

template<class T>
bool select(QList<T> &list, const QList<QPair<QString, OrderBy>> orderby, int limit=-1, int offset = 0)
{
return select(list, Where(), QStringList(), orderby, limit, offset);
return select<T>(list, Where(), QStringList(), orderby, limit, offset);
}

template<class T>
bool select(QList<T> &list, int limit, int offset=0)
{
return select(list, Where(), QStringList(), QList<QPair<QString, OrderBy>>(), limit, offset);
return select<T>(list, Where(), QStringList(), QList<QPair<QString, OrderBy>>(), limit, offset);
}

template<class T>
bool select(QList<T> &list, const Where &where, int limit, int offset=0)
{
return select<T>(list, where, QStringList(), QList<QPair<QString, OrderBy>>(), limit, offset);
}

template<class T>
bool selectValueList(QList<QVariant> &list, const QString &field, const Where &where = Where(),
const QList<QPair<QString, OrderBy>> orderby = QList<QPair<QString, OrderBy>>(), 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<int> indexes = fieldIndexes(temp.reflection().get(), {field});
std::shared_ptr<QSqlQuery> 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<OrmError>(new OrmError(DatabaseError, QString("An error occurred during the select of values in the database. See the sqlerror attached."), query->lastError()));
return false;
}
}

template<class T>
bool selectValueList(QList<QVariant> &list, const QString &field,
const QList<QPair<QString, OrderBy>> orderby, int limit=-1, int offset=0)
{
return selectValueList<T>(list, field, Where(), orderby, limit, offset);
}

template<class T>
bool selectValueList(QList<QVariant> &list, const QString &field,
int limit, int offset=0)
{
return selectValueList<T>(list, field, QList<QPair<QString, OrderBy>>(), limit, offset);
}

template<class T>
bool selectValueList(QList<QVariant> &list, const QString &field, const Where &where,
int limit, int offset=0)
{
return selectValueList<T>(list, field, where, QList<QPair<QString, OrderBy>>(), limit, offset);
}


/**
* Open a transaction in the database connection.
* @return
Expand Down Expand Up @@ -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<SqlProvider> m_provider;
Expand Down
9 changes: 8 additions & 1 deletion Orm4Qt/standardsqlprovider.cpp
Expand Up @@ -387,6 +387,7 @@ std::shared_ptr<QSqlQuery> 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)
{
Expand Down Expand Up @@ -430,9 +431,12 @@ std::shared_ptr<QSqlQuery> 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())
Expand Down Expand Up @@ -625,6 +629,7 @@ std::shared_ptr<QSqlQuery> 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)
{
Expand Down Expand Up @@ -667,6 +672,8 @@ std::shared_ptr<QSqlQuery> 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
Expand Down

0 comments on commit 45ae61d

Please sign in to comment.