typedef std::shared_ptr<Expression> ExpressionPtr;
+namespace OrderingUtils {
+
+template<typename CompoundType> inline std::string OrderByInternal()
+{
+ std::string order = OrderByInternal<typename CompoundType::Tail>();
+ if(!order.empty()) return CompoundType::Head::GetString() + ", " + order;
+ else return CompoundType::Head::GetString();
+}
+
+template<> inline std::string OrderByInternal<TypeListGuard>()
+{
+ return std::string();
+}
+
+}
+
+template<typename ColumnType>
+class __attribute__ ((visibility("hidden"))) OrderingExpression {
+protected:
+ static std::string GetSchemaAndName()
+ {
+ std::string statement;
+ statement += ColumnType::GetTableName();
+ statement += ".";
+ statement += ColumnType::GetColumnName();
+ statement += " ";
+ return statement;
+ }
+public:
+ virtual ~OrderingExpression() {}
+};
+
template<const char* Operator, typename LeftExpression, typename RightExpression>
class __attribute__ ((visibility("hidden"))) BinaryExpression : public Expression {
protected:
ORM_DEFINE_COMPARE_EXPRESSION(Equals, Equal)
ORM_DEFINE_COMPARE_EXPRESSION(Is, Is)
+#define ORM_DEFINE_ORDERING_EXPRESSION(name, value) \
+ template<typename ColumnType> \
+ class __attribute__ ((visibility("hidden"))) name \
+ : OrderingExpression<ColumnType> { \
+ public: \
+ static std::string GetString() \
+ { \
+ std::string statement = OrderingExpression<ColumnType>::GetSchemaAndName(); \
+ statement += value; \
+ return statement; \
+ } \
+ };
+
+ORM_DEFINE_ORDERING_EXPRESSION(OrderingAscending, "ASC")
+ORM_DEFINE_ORDERING_EXPRESSION(OrderingDescending, "DESC")
+
template<typename ColumnData1, typename ColumnData2>
class __attribute__ ((visibility("hidden"))) CompareBinaryColumn {
private:
m_distinctResults = true;
}
- void OrderBy(const std::string& orderBy)
+ template<typename CompoundType>
+ void OrderBy(const CompoundType&)
+ {
+ m_orderBy = OrderingUtils::OrderByInternal<typename CompoundType::Type>();
+ }
+
+ void OrderBy(const std::string & orderBy) //backward compatibility
{
m_orderBy = orderBy;
}
+ void OrderBy(const char * orderBy) //backward compatibility
+ {
+ m_orderBy = std::string(orderBy);
+ }
+
template<typename ColumnList, typename Expression>
void Join(const Expression& expression) {
std::string usedTableNames = TableDefinition::GetName();
using namespace DPL::DB::ORM;
using namespace DPL::DB::ORM::dpl_orm_test;
TestTableDelete::Select selectStart(interface.get());
- selectStart.OrderBy("ColumnInt2 ASC");
+ selectStart.OrderBy(DPL::TypeListDecl<OrderingAscending<TestTableDelete::ColumnInt2> >());
std::list<TestTableDelete::Row> list = selectStart.GetRowList();
std::list<TestTableDelete::Row> originalList = list;
oss.str(std::string());
}
}
+
+RUNNER_TEST(ORM_SelectOrderByMultipleColumns)
+{
+ SmartAttach interface;
+ using namespace DPL::DB::ORM;
+ using namespace DPL::DB::ORM::dpl_orm_test;
+ {
+ TestTableJoin3::Select select(interface.get());
+
+ // testing: " ORDER BY Value3 ASC, TestID DESC, TestID ASC"
+ select.OrderBy(DPL::TypeListDecl<OrderingAscending<TestTableJoin3::Value3>,
+ OrderingDescending<TestTableJoin3::TestText33>,
+ OrderingAscending<TestTableJoin3::TestID> >());
+
+ std::list<TestTableJoin3::Row> result = select.GetRowList();
+ std::list<TestTableJoin3::Row>::const_iterator iter = result.begin();
+ { //1 row
+ RUNNER_ASSERT_MSG(*iter->Get_TestText33() == DPL::FromASCIIString("test 6"), "Wrong row 1 order");
+ RUNNER_ASSERT_MSG(iter->Get_TestID() == 10, "Wrong row 1 order");
+ iter++;
+ }
+ { //2 row
+ RUNNER_ASSERT_MSG(*iter->Get_TestText33() == DPL::FromASCIIString("test 5"), "Wrong row 2 order");
+ RUNNER_ASSERT_MSG(iter->Get_TestID() == 7, "Wrong row 2 order");
+ iter++;
+ }
+ { //3 row
+ RUNNER_ASSERT_MSG(iter->Get_Value3() == 111, "Wrong row 3 order");
+ RUNNER_ASSERT_MSG(*iter->Get_TestText33() == DPL::FromASCIIString("test 2"), "Wrong row 3 order");
+ RUNNER_ASSERT_MSG(iter->Get_TestID() == 2, "Wrong row 3 order");
+ iter++;
+ }
+ { //4 row
+ RUNNER_ASSERT_MSG(iter->Get_Value3() == 111, "Wrong row 4 order");
+ RUNNER_ASSERT_MSG(*iter->Get_TestText33() == DPL::FromASCIIString("test 1"), "Wrong row 4 order");
+ RUNNER_ASSERT_MSG(iter->Get_TestID() == 1, "Wrong row 4 order");
+ iter++;
+ }
+ { //5 row
+ RUNNER_ASSERT_MSG(iter->Get_Value3() == 222, "Wrong row 5 order");
+ RUNNER_ASSERT_MSG(*iter->Get_TestText33() == DPL::FromASCIIString("test 4"), "Wrong row 5 order");
+ RUNNER_ASSERT_MSG(iter->Get_TestID() == 6, "Wrong row 5 order");
+ iter++;
+ }
+ { //6 row
+ RUNNER_ASSERT_MSG(iter->Get_Value3() == 222, "Wrong row 6 order");
+ RUNNER_ASSERT_MSG(*iter->Get_TestText33() == DPL::FromASCIIString("test 3"), "Wrong row 6 order");
+ RUNNER_ASSERT_MSG(iter->Get_TestID() == 3, "Wrong row 6 order");
+ iter++;
+ }
+ }
+}