template <typename T>
boost::shared_ptr<T> SingleOrDefault(qx::QxSqlQuery query)
{
QList< boost::shared_ptr<T> > list;
QSqlError daoError = qx::dao::fetch_by_query(query, list);
if (daoError.isValid()) { throw qx::dao::sql_error(daoError); }
else if (list.count() > 1) { QSqlError err("query should result in at most a single result"); throw err; }
else if (list.count() <= 0) { return NULL; }
return list.at(0);
}
QxOrm admin wrote:Hi,
A function like SingleOrDefault() from Linq doesn't exist into QxOrm library, but it's quite easy to write your own function with a similar behaviour.
Here is an example of a similar function using QxOrm library :
- Code: Select all
template <typename T>
boost::shared_ptr<T> SingleOrDefault(qx::QxSqlQuery query)
{
QList< boost::shared_ptr<T> > list;
QSqlError daoError = qx::dao::fetch_by_query(query, list);
if (daoError.isValid()) { throw qx::dao::sql_error(daoError); }
else if (list.count() > 1) { QSqlError err("query should result in at most a single result"); throw err; }
else if (list.count() <= 0) { return NULL; }
return list.at(0);
}
From efficiency into consideration, SingleOrDefault should add "select top 1 ... "
I think it's a very useful function, Will I be added in the next release?
namespace qx {
namespace dao {
template <typename T>
boost::shared_ptr<T> single_or_default(qx::QxSqlQuery query)
{ ... }
}} // namespace qx::dao
QxOrm admin wrote:From efficiency into consideration, SingleOrDefault should add "select top 1 ... "
I don't think so : maybe "TOP 2", but not "TOP 1" !
The purpose of this function is to check if there is only 1 row into your table corresponding to your query : and if there is more than 1 row, an exception is thrown.
So if you have more than 1 row, you have a problem somewhere.
Is this really an optimization ?
No, because your query must return only 1 row, so I don't think that this is very useful to add "TOP 2" to the query.I think it's a very useful function, Will I be added in the next release?
I don't think I will add this function into QxOrm library : there is exceptions thrown and this is not the style of QxOrm library (more Qt style without exception).
You can add the code of the function of my first post into your own project, it will work
And you can add it into a namespace if you want, like this :
- Code: Select all
namespace qx {
namespace dao {
template <typename T>
boost::shared_ptr<T> single_or_default(qx::QxSqlQuery query)
{ ... }
}} // namespace qx::dao
I think FirstOrDefault/SingleOrDefault could easily be implemented by using something like limit(1)
template <typename T>
boost::shared_ptr<T> SingleOrDefault(qx::QxSqlQuery query)
{
QList< boost::shared_ptr<T> > list;
QSqlError daoError = qx::dao::fetch_by_query(query, list);
if (daoError.isValid()) { throw qx::dao::sql_error(daoError); }
else if (list.count() > 1) { QSqlError err("query should result in at most a single result", "", QSqlError::UnknownError); throw qx::dao::sql_error(err); }
else if (list.count() <= 0) { return NULL; }
return list.at(0);
}
Users browsing this forum: No registered users and 7 guests