Skip to content

Commit

Permalink
added highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
fnc12 committed Oct 4, 2023
1 parent 4595779 commit 85c58f2
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 3 deletions.
12 changes: 12 additions & 0 deletions dev/ast_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ namespace sqlite_orm {
}
};

template<class T, class X, class Y, class Z>
struct ast_iterator<highlight_t<T, X, Y, Z>, void> {
using node_type = highlight_t<T, X, Y, Z>;

template<class L>
void operator()(const node_type& expression, L& lambda) const {
iterate_ast(expression.argument0, lambda);
iterate_ast(expression.argument1, lambda);
iterate_ast(expression.argument2, lambda);
}
};

template<class T>
struct ast_iterator<excluded_t<T>, void> {
using node_type = excluded_t<T>;
Expand Down
5 changes: 5 additions & 0 deletions dev/column_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ namespace sqlite_orm {
using type = typename T::result_type;
};

template<class DBOs, class T, class X, class Y, class Z>
struct column_result_t<DBOs, highlight_t<T, X, Y, Z>, void> {
using type = std::string;
};

/**
* Result for the most simple queries like `SELECT 1`
*/
Expand Down
20 changes: 20 additions & 0 deletions dev/core_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,21 @@ namespace sqlite_orm {

template<class T>
using field_type_or_type_t = polyfill::detected_or_t<T, type_t, member_field_type<T>>;

template<class T, class X, class Y, class Z>
struct highlight_t {
using table_type = T;
using argument0_type = X;
using argument1_type = Y;
using argument2_type = Z;

argument0_type argument0;
argument1_type argument1;
argument2_type argument2;

highlight_t(argument0_type argument0, argument1_type argument1, argument2_type argument2) :
argument0(std::move(argument0)), argument1(std::move(argument1)), argument2(std::move(argument2)) {}
};
}

/**
Expand Down Expand Up @@ -2102,4 +2117,9 @@ namespace sqlite_orm {
return {get_from_expression(std::forward<L>(l)), get_from_expression(std::forward<R>(r))};
}
}

template<class T, class X, class Y, class Z>
internal::highlight_t<T, X, Y, Z> highlight(X x, Y y, Z z) {
return {std::move(x), std::move(y), std::move(z)};
}
}
8 changes: 8 additions & 0 deletions dev/node_tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ namespace sqlite_orm {
using type = tuple_cat_t<node_tuple_t<Args>...>;
};

template<class T, class X, class Y, class Z>
struct node_tuple<highlight_t<T, X, Y, Z>, void> {
using x_node_tuple = node_tuple_t<X>;
using y_node_tuple = node_tuple_t<Y>;
using z_node_tuple = node_tuple_t<Z>;
using type = tuple_cat_t<x_node_tuple, y_node_tuple, z_node_tuple>;
};

template<class T>
struct node_tuple<excluded_t<T>, void> : node_tuple<T> {};

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

template<class T, class X, class Y, class Z>
struct statement_serializer<highlight_t<T, X, Y, Z>, void> {
using statement_type = highlight_t<T, X, Y, Z>;

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

/**
* Serializer for literal values.
*/
Expand Down
5 changes: 5 additions & 0 deletions dev/table_name_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ namespace sqlite_orm {
void operator()(const table__rowid_t<T>&) {
this->table_names.emplace(lookup_table_name<T>(this->db_objects), "");
}

template<class T, class X, class Y, class Z>
void operator()(const highlight_t<T, X, Y, Z>&) {
this->table_names.emplace(lookup_table_name<T>(this->db_objects), "");
}
};

template<class DBOs, satisfies<is_db_objects, DBOs> = true>
Expand Down
66 changes: 66 additions & 0 deletions include/sqlite_orm/sqlite_orm.h
Original file line number Diff line number Diff line change
Expand Up @@ -5169,6 +5169,21 @@ namespace sqlite_orm {

template<class T>
using field_type_or_type_t = polyfill::detected_or_t<T, type_t, member_field_type<T>>;

template<class T, class X, class Y, class Z>
struct highlight_t {
using table_type = T;
using argument0_type = X;
using argument1_type = Y;
using argument2_type = Z;

argument0_type argument0;
argument1_type argument1;
argument2_type argument2;

highlight_t(argument0_type argument0, argument1_type argument1, argument2_type argument2) :
argument0(std::move(argument0)), argument1(std::move(argument1)), argument2(std::move(argument2)) {}
};
}

/**
Expand Down Expand Up @@ -6659,6 +6674,11 @@ namespace sqlite_orm {
return {get_from_expression(std::forward<L>(l)), get_from_expression(std::forward<R>(r))};
}
}

template<class T, class X, class Y, class Z>
internal::highlight_t<T, X, Y, Z> highlight(X x, Y y, Z z) {
return {std::move(x), std::move(y), std::move(z)};
}
}
#pragma once

Expand Down Expand Up @@ -10910,6 +10930,11 @@ namespace sqlite_orm {
using type = typename T::result_type;
};

template<class DBOs, class T, class X, class Y, class Z>
struct column_result_t<DBOs, highlight_t<T, X, Y, Z>, void> {
using type = std::string;
};

/**
* Result for the most simple queries like `SELECT 1`
*/
Expand Down Expand Up @@ -11481,6 +11506,11 @@ namespace sqlite_orm {
void operator()(const table__rowid_t<T>&) {
this->table_names.emplace(lookup_table_name<T>(this->db_objects), "");
}

template<class T, class X, class Y, class Z>
void operator()(const highlight_t<T, X, Y, Z>&) {
this->table_names.emplace(lookup_table_name<T>(this->db_objects), "");
}
};

template<class DBOs, satisfies<is_db_objects, DBOs> = true>
Expand Down Expand Up @@ -12477,6 +12507,18 @@ namespace sqlite_orm {
}
};

template<class T, class X, class Y, class Z>
struct ast_iterator<highlight_t<T, X, Y, Z>, void> {
using node_type = highlight_t<T, X, Y, Z>;

template<class L>
void operator()(const node_type& expression, L& lambda) const {
iterate_ast(expression.argument0, lambda);
iterate_ast(expression.argument1, lambda);
iterate_ast(expression.argument2, lambda);
}
};

template<class T>
struct ast_iterator<excluded_t<T>, void> {
using node_type = excluded_t<T>;
Expand Down Expand Up @@ -15550,6 +15592,22 @@ namespace sqlite_orm {
}
};

template<class T, class X, class Y, class Z>
struct statement_serializer<highlight_t<T, X, Y, Z>, void> {
using statement_type = highlight_t<T, X, Y, Z>;

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

/**
* Serializer for literal values.
*/
Expand Down Expand Up @@ -18991,6 +19049,14 @@ namespace sqlite_orm {
using type = tuple_cat_t<node_tuple_t<Args>...>;
};

template<class T, class X, class Y, class Z>
struct node_tuple<highlight_t<T, X, Y, Z>, void> {
using x_node_tuple = node_tuple_t<X>;
using y_node_tuple = node_tuple_t<Y>;
using z_node_tuple = node_tuple_t<Z>;
using type = tuple_cat_t<x_node_tuple, y_node_tuple, z_node_tuple>;
};

template<class T>
struct node_tuple<excluded_t<T>, void> : node_tuple<T> {};

Expand Down
18 changes: 15 additions & 3 deletions tests/table_name_collector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ TEST_CASE("table name collector") {
auto dbObjects = db_objects_t{table};
internal::table_name_collector_base::table_name_set expected;

SECTION("from table") {
internal::serializer_context<db_objects_t> context{dbObjects};
auto collector = internal::make_table_name_collector(context.db_objects);
internal::serializer_context<db_objects_t> context{dbObjects};
auto collector = internal::make_table_name_collector(context.db_objects);

SECTION("from table") {
SECTION("regular column") {
using als = alias_z<User>;
auto expression = &User::id;
Expand All @@ -44,4 +44,16 @@ TEST_CASE("table name collector") {
}
REQUIRE(collector.table_names == expected);
}
SECTION("highlight") {
SECTION("simple") {
auto expression = highlight<User>(0, "<b>", "</b>");
expected.emplace(table.name, "");
iterate_ast(expression, collector);
}
SECTION("in columns") {
auto expression = columns(highlight<User>(0, "<b>", "</b>"));
expected.emplace(table.name, "");
iterate_ast(expression, collector);
}
}
}

0 comments on commit 85c58f2

Please sign in to comment.