Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 15 additions & 0 deletions dev/core_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ namespace sqlite_orm {
}
};

struct replace_string {
operator std::string() const {
return "REPLACE";
}
};

#if SQLITE_VERSION_NUMBER >= 3007016

struct char_string {
Expand Down Expand Up @@ -349,6 +355,15 @@ namespace sqlite_orm {
return {move(args)};
}

/**
* REPLACE(X) function https://sqlite.org/lang_corefunc.html#replace
*/
template<class X, class Y, class Z>
core_functions::core_function_t<std::string, core_functions::replace_string, X, Y, Z> replace(X x, Y y, Z z) {
std::tuple<X, Y, Z> args{std::forward<X>(x), std::forward<Y>(y), std::forward<Z>(z)};
return {move(args)};
}

#if SQLITE_VERSION_NUMBER >= 3007016

/**
Expand Down
35 changes: 34 additions & 1 deletion examples/core_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ struct MarvelHero {
short points;
};

struct Contact {
int id = 0;
std::string firstName;
std::string lastName;
std::string phone;
};

int main(int, char **argv) {
cout << "path = " << argv[0] << endl;

Expand All @@ -22,7 +29,12 @@ int main(int, char **argv) {
make_column("id", &MarvelHero::id, primary_key()),
make_column("name", &MarvelHero::name),
make_column("abilities", &MarvelHero::abilities),
make_column("points", &MarvelHero::points)));
make_column("points", &MarvelHero::points)),
make_table("contacts",
make_column("contact_id", &Contact::id, primary_key()),
make_column("first_name", &Contact::firstName),
make_column("last_name", &Contact::lastName),
make_column("phone", &Contact::phone)));
storage.sync_schema();

storage.remove_all<MarvelHero>();
Expand All @@ -42,6 +54,11 @@ int main(int, char **argv) {
return true;
});

Contact john{0, "John", "Doe", "410-555-0168"};
Contact lily{0, "Lily", "Bush", "410-444-9862"};
john.id = storage.insert(john);
lily.id = storage.insert(lily);

// SELECT LENGTH(name)
// FROM marvel
auto nameLengths = storage.select(length(&MarvelHero::name)); // nameLengths is std::vector<int>
Expand Down Expand Up @@ -291,5 +308,21 @@ int main(int, char **argv) {
cout << endl;
}

// SELECT REPLACE('AA B CC AAA','A','Z')
cout << "SELECT REPLACE('AA B CC AAA','A','Z') = " << storage.select(replace("AA B CC AAA", "A", "Z")).front()
<< endl;

// SELECT REPLACE('This is a cat','This','That')
cout << "SELECT REPLACE('This is a cat','This','That') = "
<< storage.select(replace("This is a cat", "This", "That")).front() << endl;

// UPDATE contacts
// SET phone = REPLACE(phone, '410', '+1-410')
storage.update_all(set(c(&Contact::phone) = replace(&Contact::phone, "410", "+1-410")));
cout << "Contacts:" << endl;
for(auto &contact: storage.iterate<Contact>()) {
cout << storage.dump(contact) << endl;
}

return 0;
}
15 changes: 15 additions & 0 deletions include/sqlite_orm/sqlite_orm.h
Original file line number Diff line number Diff line change
Expand Up @@ -3426,6 +3426,12 @@ namespace sqlite_orm {
}
};

struct replace_string {
operator std::string() const {
return "REPLACE";
}
};

#if SQLITE_VERSION_NUMBER >= 3007016

struct char_string {
Expand Down Expand Up @@ -3670,6 +3676,15 @@ namespace sqlite_orm {
return {move(args)};
}

/**
* REPLACE(X) function https://sqlite.org/lang_corefunc.html#replace
*/
template<class X, class Y, class Z>
core_functions::core_function_t<std::string, core_functions::replace_string, X, Y, Z> replace(X x, Y y, Z z) {
std::tuple<X, Y, Z> args{std::forward<X>(x), std::forward<Y>(y), std::forward<Z>(z)};
return {move(args)};
}

#if SQLITE_VERSION_NUMBER >= 3007016

/**
Expand Down
58 changes: 58 additions & 0 deletions tests/core_functions_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,61 @@ TEST_CASE("instr") {
REQUIRE_THAT(rows, UnorderedEquals(expected));
}
}

namespace replace_func_local {
struct Contact {
int id = 0;
std::string firstName;
std::string lastName;
std::string phone;
};

bool operator==(const Contact &lhs, const Contact &rhs) {
return lhs.id == rhs.id && lhs.firstName == rhs.firstName && lhs.lastName == rhs.lastName &&
lhs.phone == rhs.phone;
}
}

TEST_CASE("replace func") {
using Catch::Matchers::UnorderedEquals;
using namespace replace_func_local;

auto storage = make_storage({},
make_table("contacts",
make_column("contact_id", &Contact::id, primary_key()),
make_column("first_name", &Contact::firstName),
make_column("last_name", &Contact::lastName),
make_column("phone", &Contact::phone)));
storage.sync_schema();
{
auto rows = storage.select(replace("AA B CC AAA", "A", "Z"));
REQUIRE(rows.size() == 1);
REQUIRE(rows.front() == "ZZ B CC ZZZ");
}
{
auto rows = storage.select(replace("This is a cat", "This", "That"));
REQUIRE(rows.size() == 1);
REQUIRE(rows.front() == "That is a cat");
}
Contact john{0, "John", "Doe", "410-555-0168"};
Contact lily{0, "Lily", "Bush", "410-444-9862"};
john.id = storage.insert(john);
lily.id = storage.insert(lily);
{
auto contacts = storage.get_all<Contact>();
std::vector<Contact> expected;
expected.push_back(john);
expected.push_back(lily);
REQUIRE_THAT(contacts, UnorderedEquals(expected));
}
storage.update_all(set(c(&Contact::phone) = replace(&Contact::phone, "410", "+1-410")));
{
auto contacts = storage.get_all<Contact>();
john.phone = "+1-410-555-0168";
lily.phone = "+1-410-444-9862";
std::vector<Contact> expected;
expected.push_back(john);
expected.push_back(lily);
REQUIRE_THAT(contacts, UnorderedEquals(expected));
}
}
2 changes: 1 addition & 1 deletion tests/tests2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ TEST_CASE("Select") {
REQUIRE(storage.get<Word>(firstId).currentWord == "ototo");
}

TEST_CASE("Replace") {
TEST_CASE("Replace query") {
struct Object {
int id;
std::string name;
Expand Down