Skip to content

Order by kth column #966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
67debd9
Removed superfluous space in output for order by clause and expressions
trueqbit Mar 11, 2022
fcd1885
Added a few tests specifically for testing ORDER BY expressions
trueqbit Mar 11, 2022
8142ecc
Merge remote-tracking branch 'upstream/dev' into issues/order_by_kth_col
trueqbit Mar 11, 2022
e5784d5
Amended conditions unit test
trueqbit Mar 11, 2022
a90b9cc
Merge remote-tracking branch 'upstream/dev' into issues/order_by_kth_col
trueqbit Mar 16, 2022
d1ebf1a
Ability to order by positional column ordinal
trueqbit Mar 16, 2022
812b6d5
Merge remote-tracking branch 'upstream/dev' into issues/order_by_kth_col
trueqbit Mar 17, 2022
ce09321
Corrected code formatting
trueqbit Mar 17, 2022
e7f75f0
Corrected compilation errors in C++14
trueqbit Mar 18, 2022
6964ea5
Merge remote-tracking branch 'upstream/dev' into issues/order_by_kth_col
trueqbit Mar 18, 2022
3813a06
Renamed `nth_ constant` type alias to `positional_ordinal` struct
trueqbit Mar 18, 2022
b9cd3d3
Runner-up: Renamed `nth_ constant` type alias to `positional_ordinal`…
trueqbit Mar 18, 2022
1d0bf8a
Merge remote-tracking branch 'upstream/dev' into issues/order_by_kth_col
trueqbit Mar 22, 2022
c96d19e
Treated bindables used as order-by expressions as literal values
trueqbit Mar 22, 2022
1d4ace0
Introduced node tuple type alias
trueqbit Mar 22, 2022
f6a1f25
Updated sqlite_orm.h
trueqbit Mar 22, 2022
1403365
Updated sqlite_orm.h
trueqbit Mar 22, 2022
b8a3ccd
Corrected formatting
trueqbit Mar 23, 2022
a41dcbc
Merge remote-tracking branch 'upstream/dev' into issues/order_by_kth_col
trueqbit Mar 23, 2022
9e5e7fb
Updated sqlite_orm.h
trueqbit Mar 23, 2022
abe53b4
Ability to order and group by column alias
trueqbit Mar 24, 2022
82be816
Merge remote-tracking branch 'upstream/dev' into issues/order_by_kth_col
trueqbit Mar 24, 2022
13cae94
Examples for ordering by numeric and custom column alias
trueqbit Mar 24, 2022
43a642d
Updated comment of `order_by`
trueqbit Mar 24, 2022
d056cb4
Merge remote-tracking branch 'upstream/dev' into issues/order_by_kth_col
trueqbit Mar 26, 2022
9779e37
Merge remote-tracking branch 'upstream/dev' into issues/order_by_kth_col
trueqbit Mar 30, 2022
a7414bd
Overloaded `order_by()` for integral numeric types
trueqbit Mar 30, 2022
776bc0a
Removed numeric column alias
trueqbit Mar 30, 2022
f4c9052
Fixed bindable unit tests for C++17 and later
trueqbit Mar 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions dev/alias.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace sqlite_orm {

/**
* This is base class for every class which is used as a custom table alias.
* This is base class for every class which is used as a custom table alias, column alias or expression alias.
* For more information please look through self_join.cpp example
*/
struct alias_tag {};
Expand All @@ -16,7 +16,7 @@ namespace sqlite_orm {

/**
* This is a common built-in class used for custom single character table aliases.
* Also you can use language aliases `alias_a`, `alias_b` etc. instead
* For convenience there exist type aliases `alias_a`, `alias_b`, ...
*/
template<class T, char A>
struct table_alias : alias_tag {
Expand All @@ -37,7 +37,7 @@ namespace sqlite_orm {

column_type column;

alias_column_t(){};
alias_column_t() {}

alias_column_t(column_type column_) : column(std::move(column_)) {}
};
Expand Down Expand Up @@ -72,6 +72,17 @@ namespace sqlite_orm {
expression_type expression;
};

/**
* This is a common built-in class used for custom single-character column aliases.
* For convenience there exist type aliases `colalias_a`, `colalias_b`, ...
*/
template<char A>
struct column_alias : alias_tag {
static std::string get() {
return std::string(1u, A);
}
};

template<class T>
struct alias_holder {
using type = T;
Expand Down Expand Up @@ -151,4 +162,14 @@ namespace sqlite_orm {
using alias_y = internal::table_alias<T, 'y'>;
template<class T>
using alias_z = internal::table_alias<T, 'z'>;

using colalias_a = internal::column_alias<'a'>;
using colalias_b = internal::column_alias<'b'>;
using colalias_c = internal::column_alias<'c'>;
using colalias_d = internal::column_alias<'d'>;
using colalias_e = internal::column_alias<'e'>;
using colalias_f = internal::column_alias<'f'>;
using colalias_g = internal::column_alias<'g'>;
using colalias_h = internal::column_alias<'h'>;
using colalias_i = internal::column_alias<'i'>;
}
18 changes: 16 additions & 2 deletions dev/ast_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,23 @@ namespace sqlite_orm {
}
};

/**
* Column alias or literal
*/
template<class T>
struct ast_iterator<order_by_t<T>, void> {
using node_type = order_by_t<T>;
struct ast_iterator<
T,
std::enable_if_t<polyfill::disjunction_v<polyfill::is_specialization_of<T, alias_holder>,
polyfill::is_specialization_of<T, literal_holder>>>> {
using node_type = T;

template<class L>
void operator()(const node_type& /*node*/, const L& /*l*/) const {}
};

template<class E>
struct ast_iterator<order_by_t<E>, void> {
using node_type = order_by_t<E>;

template<class L>
void operator()(const node_type& node, const L& l) const {
Expand Down
24 changes: 21 additions & 3 deletions dev/conditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
#include <tuple> // std::tuple, std::tuple_size
#include <sstream> // std::stringstream

#include "cxx_polyfill.h"
#include "type_traits.h"
#include "collate_argument.h"
#include "constraints.h"
#include "optional_container.h"
#include "serializer_context.h"
#include "tags.h"
#include "expression.h"
#include "type_printer.h"
#include "literal.h"

namespace sqlite_orm {

Expand Down Expand Up @@ -1172,14 +1176,28 @@ namespace sqlite_orm {
}

/**
* ORDER BY column
* Example: storage.select(&User::name, order_by(&User::id))
* ORDER BY column, column alias or expression
*
* Examples:
* storage.select(&User::name, order_by(&User::id))
* storage.select(as<colalias_a>(&User::name), order_by(get<colalias_a>()))
*/
template<class O>
template<class O, internal::satisfies_not<std::is_base_of, integer_printer, type_printer<O>> = true>
internal::order_by_t<O> order_by(O o) {
return {std::move(o)};
}

/**
* ORDER BY positional ordinal
*
* Examples:
* storage.select(&User::name, order_by(1))
*/
template<class O, internal::satisfies<std::is_base_of, integer_printer, type_printer<O>> = true>
internal::order_by_t<internal::literal_holder<O>> order_by(O o) {
return {{std::move(o)}};
}

/**
* ORDER BY column1, column2
* Example: storage.get_all<Singer>(multi_order_by(order_by(&Singer::name).asc(), order_by(&Singer::gender).desc())
Expand Down
8 changes: 4 additions & 4 deletions dev/get_prepared_statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ namespace sqlite_orm {
const auto& get(const internal::prepared_statement_t<T>& statement) {
using statement_type = typename std::decay<decltype(statement)>::type;
using expression_type = typename statement_type::expression_type;
using node_tuple = typename internal::node_tuple<expression_type>::type;
using node_tuple = internal::node_tuple_t<expression_type>;
using bind_tuple = typename internal::bindable_filter<node_tuple>::type;
using result_tupe = typename std::tuple_element<static_cast<size_t>(N), bind_tuple>::type;
using result_tupe = std::tuple_element_t<static_cast<size_t>(N), bind_tuple>;
const result_tupe* result = nullptr;
auto index = -1;
internal::iterate_ast(statement.expression, [&result, &index](auto& node) {
Expand All @@ -146,9 +146,9 @@ namespace sqlite_orm {
auto& get(internal::prepared_statement_t<T>& statement) {
using statement_type = typename std::decay<decltype(statement)>::type;
using expression_type = typename statement_type::expression_type;
using node_tuple = typename internal::node_tuple<expression_type>::type;
using node_tuple = internal::node_tuple_t<expression_type>;
using bind_tuple = typename internal::bindable_filter<node_tuple>::type;
using result_tupe = typename std::tuple_element<static_cast<size_t>(N), bind_tuple>::type;
using result_tupe = std::tuple_element_t<static_cast<size_t>(N), bind_tuple>;
result_tupe* result = nullptr;
auto index = -1;
internal::iterate_ast(statement.expression, [&result, &index](auto& node) {
Expand Down
19 changes: 19 additions & 0 deletions dev/literal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <utility> // std::move

namespace sqlite_orm {
namespace internal {

/*
* Protect an otherwise bindable element so that it is always serialized as a literal value.
*/
template<class T>
struct literal_holder {
using type = T;

T value;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better replace T with type here

};

}
}
Loading