Skip to content

Commit

Permalink
added is_equal with table
Browse files Browse the repository at this point in the history
  • Loading branch information
fnc12 committed Oct 3, 2023
1 parent 080bf60 commit e690bd3
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 16 deletions.
16 changes: 13 additions & 3 deletions dev/ast_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,19 @@ namespace sqlite_orm {
using node_type = binary_operator<L, R, Ds...>;

template<class C>
void operator()(const node_type& binaryOperator, C& lambda) const {
iterate_ast(binaryOperator.lhs, lambda);
iterate_ast(binaryOperator.rhs, lambda);
void operator()(const node_type& node, C& lambda) const {
iterate_ast(node.lhs, lambda);
iterate_ast(node.rhs, lambda);
}
};

template<class L, class R>
struct ast_iterator<is_equal_with_table_t<L, R>, void> {
using node_type = is_equal_with_table_t<L, R>;

template<class C>
void operator()(const node_type& node, C& lambda) const {
iterate_ast(node.rhs, lambda);
}
};

Expand Down
15 changes: 15 additions & 0 deletions dev/conditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ namespace sqlite_orm {
}
};

template<class L, class R>
struct is_equal_with_table_t : negatable_t {
using left_type = L;
using right_type = R;

right_type rhs;

is_equal_with_table_t(right_type rhs) : rhs(std::move(rhs)) {}
};

struct is_not_equal_string {
operator std::string() const {
return "!=";
Expand Down Expand Up @@ -1085,6 +1095,11 @@ namespace sqlite_orm {
return {std::move(l), std::move(r)};
}

template<class L, class R>
internal::is_equal_with_table_t<L, R> is_equal(R rhs) {
return {std::move(rhs)};
}

template<class L, class R>
internal::is_not_equal_t<L, R> is_not_equal(L l, R r) {
return {std::move(l), std::move(r)};
Expand Down
3 changes: 3 additions & 0 deletions dev/node_tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ namespace sqlite_orm {
template<class E>
struct node_tuple<order_by_t<E>, void> : node_tuple<E> {};

template<class L, class R>
struct node_tuple<is_equal_with_table_t<L, R>, void> : node_tuple<R> {};

template<class T>
struct node_tuple<T, match_if<is_binary_condition, T>> {
using node_type = T;
Expand Down
4 changes: 2 additions & 2 deletions dev/schema/triggers.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <string>
#include <tuple>

#include "functional/cxx_universal.h"
#include "optional_container.h"
#include "../functional/cxx_universal.h"
#include "../optional_container.h"

// NOTE Idea : Maybe also implement a custom trigger system to call a c++ callback when a trigger triggers ?
// (Could be implemented with a normal trigger that insert or update an internal table and then retreive
Expand Down
15 changes: 15 additions & 0 deletions dev/statement_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,21 @@ namespace sqlite_orm {
}
};

template<class L, class R>
struct statement_serializer<is_equal_with_table_t<L, R>, void> {
using statement_type = is_equal_with_table_t<L, R>;

template<class Ctx>
std::string operator()(const statement_type& statement, const Ctx& context) const {
std::stringstream ss;
const auto tableName = lookup_table_name<L>(context.db_objects);
ss << streaming_identifier(tableName);
ss << " = ";
ss << serialize(statement.rhs, context);
return ss.str();
}
};

template<class L, class R, class... Ds>
struct statement_serializer<binary_operator<L, R, Ds...>, void> {
using statement_type = binary_operator<L, R, Ds...>;
Expand Down
53 changes: 48 additions & 5 deletions include/sqlite_orm/sqlite_orm.h
Original file line number Diff line number Diff line change
Expand Up @@ -3157,6 +3157,16 @@ namespace sqlite_orm {
}
};

template<class L, class R>
struct is_equal_with_table_t : negatable_t {
using left_type = L;
using right_type = R;

right_type rhs;

is_equal_with_table_t(right_type rhs) : rhs(std::move(rhs)) {}
};

struct is_not_equal_string {
operator std::string() const {
return "!=";
Expand Down Expand Up @@ -4032,6 +4042,11 @@ namespace sqlite_orm {
return {std::move(l), std::move(r)};
}

template<class L, class R>
internal::is_equal_with_table_t<L, R> is_equal(R rhs) {
return {std::move(rhs)};
}

template<class L, class R>
internal::is_not_equal_t<L, R> is_not_equal(L l, R r) {
return {std::move(l), std::move(r)};
Expand Down Expand Up @@ -7340,9 +7355,9 @@ namespace sqlite_orm {
#include <string>
#include <tuple>

// #include "functional/cxx_universal.h"
// #include "../functional/cxx_universal.h"

// #include "optional_container.h"
// #include "../optional_container.h"

// NOTE Idea : Maybe also implement a custom trigger system to call a c++ callback when a trigger triggers ?
// (Could be implemented with a normal trigger that insert or update an internal table and then retreive
Expand Down Expand Up @@ -12508,9 +12523,19 @@ namespace sqlite_orm {
using node_type = binary_operator<L, R, Ds...>;

template<class C>
void operator()(const node_type& binaryOperator, C& lambda) const {
iterate_ast(binaryOperator.lhs, lambda);
iterate_ast(binaryOperator.rhs, lambda);
void operator()(const node_type& node, C& lambda) const {
iterate_ast(node.lhs, lambda);
iterate_ast(node.rhs, lambda);
}
};

template<class L, class R>
struct ast_iterator<is_equal_with_table_t<L, R>, void> {
using node_type = is_equal_with_table_t<L, R>;

template<class C>
void operator()(const node_type& node, C& lambda) const {
iterate_ast(node.rhs, lambda);
}
};

Expand Down Expand Up @@ -15831,6 +15856,21 @@ namespace sqlite_orm {
}
};

template<class L, class R>
struct statement_serializer<is_equal_with_table_t<L, R>, void> {
using statement_type = is_equal_with_table_t<L, R>;

template<class Ctx>
std::string operator()(const statement_type& statement, const Ctx& context) const {
std::stringstream ss;
const auto tableName = lookup_table_name<L>(context.db_objects);
ss << streaming_identifier(tableName);
ss << " = ";
ss << serialize(statement.rhs, context);
return ss.str();
}
};

template<class L, class R, class... Ds>
struct statement_serializer<binary_operator<L, R, Ds...>, void> {
using statement_type = binary_operator<L, R, Ds...>;
Expand Down Expand Up @@ -18981,6 +19021,9 @@ namespace sqlite_orm {
template<class E>
struct node_tuple<order_by_t<E>, void> : node_tuple<E> {};

template<class L, class R>
struct node_tuple<is_equal_with_table_t<L, R>, void> : node_tuple<R> {};

template<class T>
struct node_tuple<T, match_if<is_binary_condition, T>> {
using node_type = T;
Expand Down
8 changes: 4 additions & 4 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ add_executable(unit_tests
tests3.cpp
tests4.cpp
tests5.cpp
column_tests.cpp
schema/column_tests.cpp
schema/index_tests.cpp
schema/table_tests.cpp
schema/explicit_columns.cpp
private_getters_tests.cpp
pragma_tests.cpp
explicit_columns.cpp
built_in_functions_tests/core_functions_tests.cpp
built_in_functions_tests/datetime_function_tests.cpp
built_in_functions_tests/math_functions.cpp
index_tests.cpp
constraints/composite_key.cpp
operators/arithmetic_operators.cpp
operators/like.cpp
Expand Down Expand Up @@ -80,7 +81,6 @@ add_executable(unit_tests
constraints/unique.cpp
constraints/foreign_key.cpp
constraints/check.cpp
table_tests.cpp
statement_serializer_tests/column_constraints/generated.cpp
statement_serializer_tests/column_constraints/default.cpp
statement_serializer_tests/column_constraints/primary_key.cpp
Expand Down
5 changes: 5 additions & 0 deletions tests/ast_iterator_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ TEST_CASE("ast_iterator") {
}
#endif
}
SECTION("is_equal_with_table_t") {
auto expression = is_equal<User>(std::string("Claude"));
expected.push_back(typeid(std::string));
iterate_ast(expression, lambda);
}
SECTION("aliased regular column") {
using als = alias_z<User>;
auto expression = alias_column<als>(&User::id);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 13 additions & 2 deletions tests/statement_serializer_tests/comparison_operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@
using namespace sqlite_orm;

TEST_CASE("statement_serializer comparison operators") {
internal::db_objects_tuple<> storage;
internal::serializer_context<internal::db_objects_tuple<>> context{storage};
struct User {
int id = 0;
std::string name;
};
auto table = make_table("users", make_column("id", &User::id), make_column("name", &User::name));
using db_objects_t = internal::db_objects_tuple<decltype(table)>;
auto dbObjects = db_objects_t{table};
using context_t = internal::serializer_context<db_objects_t>;
context_t context{dbObjects};
std::string value;
std::string expected;
SECTION("less_than") {
Expand Down Expand Up @@ -80,5 +87,9 @@ TEST_CASE("statement_serializer comparison operators") {
}
expected = "('lala' != 7)";
}
SECTION("is_equal_with_table_t") {
value = serialize(is_equal<User>("Tom Gregory"), context);
expected = "\"users\" = 'Tom Gregory'";
}
REQUIRE(value == expected);
}
8 changes: 8 additions & 0 deletions tests/static_tests/node_tuple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ TEST_CASE("Node tuple") {
static_assert(is_same<Tuple, Expected>::value, "literal int");
STATIC_REQUIRE(is_same<bindable_filter_t<Tuple>, tuple<>>::value);
}
SECTION("is_equal_with_table_t") {
auto node = is_equal<User>(std::string("Claude"));
using Node = decltype(node);
using Tuple = node_tuple_t<Node>;
using Expected = tuple<std::string>;
static_assert(is_same<Tuple, Expected>::value, "is_equal_with_table_t");
STATIC_REQUIRE(is_same<bindable_filter_t<Tuple>, tuple<std::string>>::value);
}
SECTION("binary_condition") {
using namespace internal;
SECTION("5 < 6.0f") {
Expand Down

0 comments on commit e690bd3

Please sign in to comment.