[odb-users] ORDER BY clause in ODB query language

Markus Klemm markus at markusklemm.net
Tue Sep 1 11:40:07 EDT 2015


I was close to start a new thread, but it basicly the same topic:
My point in the previous question was, that I can't solve this use case below, without choosing the column by a string, rather than by a odb::query_column,yet?!

I didn't minimized it completly because @Boris was wondering about the use cases. So obvouisly I'm not a fan of the one suggested future solution of using operator<, because ordering criteria changes/ depens on the column. This is the real usecase when using the framework Wt (e.g. part of the user supplied model for a table view).

Raw Sourcecode below, gist here: https://gist.github.com/Superlokkus/a994d42534beca9ed5c6
void sort(int column, Wt::SortOrder order = Wt::AscendingOrder){
	odb::query<directory> first_part("ORDER BY"), middle_part,end_part;
	//odb::query_column<directory> middle_type; and decltype(odb::query<directory>::added) middle_type; were my next guesses
	switch (column){
	default:
		middle_part = odb::query<directory>::added;/*Error: no operator "=" matches these operands 
operand types are : odb::query<directory, odb::mssql::query_base> = const odb::mssql::query_column<boost::posix_time::ptime, odb::mssql::id_datetime>*/
		break;
	}
	switch (order){
	case Wt::DescendingOrder:
		end_part = odb::query<directory>("DESC");
		break;
	default:
		end_part = odb::query<directory>("ASC");
		break;
	}
	odb::query<directory> final_query(first_part + middle_part + end_part);
	}

Best Regards

Markus Klemm

> Am 27.08.2015 um 14:14 schrieb Boris Kolpackov <boris at codesynthesis.com>:
> 
> Hi Markus,
> 
> Markus Klemm <markus at markusklemm.net> writes:
> 
>> Is there a way to get a query with an ORDER BY clause with the typed
>> checked, static, non-native ODB query language? The examples in the
>> manual is using the database system-native query language e.g. a
>> concatenated string.
> 
> Currently you have to spell "ORDER BY" as a string, though the column
> can be specified as a C++ name:
> 
> db->query<person> ((query::first == "John") + "ORDER BY" + query::age);
> 
> We do plan to provide some syntactic sugar for this, though I am not
> sure how much static type checking we will be able to do. For example,
> should we assume if the C++ type provides operator<, then we can do
> ORDER BY?
> 
> 
>> That also makes it hard to use the ORDER BY clause because after
>> the odb macro for the column, I need to concatenate the DESC/ASC
>> clause but can’t due type mismatch.
> 
> This should work:
> 
> db->query<person> ((...) + "ORDER BY" + query::age + "ASC");
> 
> BTW, "the odb macro for the column" is not a macro, it is a proper
> C++ variable name.
> 
> Boris



More information about the odb-users mailing list