Page 1 of 1

Order Relations

PostPosted: Tue Aug 30, 2016 5:54 pm
by gianks
Hi,
i have a table with a column called 'ordering' which is supposed to be used to sort items in a list without affecting primary keys.
How can i tell QxOrm to order on that column the list of items when i do on the parent object a fetch_by_id_with_all_relations?

QxCollection has the sortByKey and sortByValue methods but i cannot sort on a other columns as far as i have understood.

Thanks

Re: Order Relations

PostPosted: Wed Aug 31, 2016 7:39 am
by qxorm
Hello,

If you have the possibility, I think it is better to sort on database side adding an ORDER BY to the SQL query.

Otherwise, if you prefer to sort on C++ side, it should be quite easy : you just have to write comparaison operators for your C++ persistent class.
Because you have a column named 'ordering' to sort your items, comparaison functions are very simple, for example :
Code: Select all
bool operator < (const MyClass & s1, const MyClass & s2) {
   return (s1.ordering < s2.ordering);
}

bool operator > (const MyClass & s1, const MyClass & s2) {
   return (s1.ordering > s2.ordering);
}


Then, it depends on the container you use to store your items : std::vector, QList, qx::QxCollection, etc...
If you are using qx::QxCollection and have defined the comparaison operators, then you can use the sortByValue() method.

NOTE : qx::QxCollection manages automatically pointers and smart-pointers.
That means that if you store pointers or smart-pointers in your collection, then (* s1) < (* s2) will be used automatically to sort your items.

Re: Order Relations

PostPosted: Wed Aug 31, 2016 12:39 pm
by gianks
Hi,
as you said i'd prefer the sorting to be done in the DB with a ORDER BY clause which i don't know how to add (as suggested) during the fetch of the object related onetomany with the list to be ordered.

Doing it C++ side is what i did yesterday to proceed but i think is a waste not to take advantage of the power of the RDBMS.
My design relies on a template class for dealing with the database for all the entities so it'd be perfect if i could specify in the class context registration the default/relative (when used in a relationship) ordering column of a table.

I hope having been clear, my english is improvable :D

Kind regards

Re: Order Relations

PostPosted: Wed Aug 31, 2016 2:20 pm
by qxorm
specify in the class context registration the default/relative (when used in a relationship) ordering column of a table

No you can't specify that in the qx::register_class<T>() function natively.
But you could use property bag (qx::QxPropertyBag class) to add this kind of meta-information in your qx::register_class<T>() function.

a ORDER BY clause which i don't know how to add (as suggested) during the fetch of the object related onetomany with the list to be ordered

This is not a perfect solution but you could proceed like this :
1- Execute a qx::dao::fetch_by_query_with_relation() function with an empty qx::QxSqlQuery (or qx_query) instance ;
2- See the SQL generated by QxOrm library ==> you will see the alias generated automatically by QxOrm for your column you want to sort ;
3- Now, you can execute again qx::dao::fetch_by_query_with_relation(), but with a qx::QxSqlQuery like this :
Code: Select all
qx_query query;
query.orderAsc(<my_column_name_or_alias_here>);
qx::dao::fetch_by_query_with_relation("*", query, lst);