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
33 changes: 25 additions & 8 deletions brazierORM/include/Model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ namespace brazier {
return model;
}

static std::shared_ptr<Derived> find(int id, const std::shared_ptr<Database>& db) {
static std::shared_ptr<Derived> find(std::string id, const std::shared_ptr<Database>& db) {
try {
auto results = query().Where(Derived::primary_key + " = " + std::to_string(id)).Limit(1).get(db);
auto results = query().Where(Derived::primary_key + " = '" + id + "'").Limit(1).get(db);
if (results.empty()) {
Logger::log("No data found by id", "WARNING");
return nullptr;
Expand All @@ -211,7 +211,12 @@ namespace brazier {
}
}

static void update(int id, const std::map<std::string, std::string>& data, const std::shared_ptr<Database>& db) {
static std::shared_ptr<Derived> find(int id, const std::shared_ptr<Database>& db) {
return Derived::find(std::to_string(id), db);
}


static void update(std::string id, const std::map<std::string, std::string>& data, const std::shared_ptr<Database>& db) {
try {
PGconn* conn = db->getConnection();
if (!conn) {
Expand All @@ -229,14 +234,18 @@ namespace brazier {
Logger::log("No valid fields provided for update", "WARNING");
return;
}
builder.Update(updateValues).Where(Derived::primary_key + " = " + SQLString::EscapeString(conn, std::to_string(id)));
builder.Update(updateValues).Where(Derived::primary_key + " = " + SQLString::EscapeString(conn, id));
db->execute(builder.get());
}
catch (const std::exception& e) {
Logger::log("Update failed: " + std::string(e.what()), "ERROR");
}
}

static void update(int id, const std::map<std::string, std::string>& data, const std::shared_ptr<Database>& db) {
return Derived::update(std::to_string(id), data, db);
}

void delete_() {
try {
if (!database) throw std::runtime_error("Database connection is not initialized");
Expand All @@ -260,15 +269,15 @@ namespace brazier {
}
}

static void deleteById(int id, const std::shared_ptr<Database>& db) {
static void deleteById(std::string id, const std::shared_ptr<Database>& db) {
try {
PGconn* conn = db->getConnection();
if (!conn) {
Logger::log("Failed to get database connection", "ERROR");
return;
}
SQLQueryBuilder builder(Derived::table_name);
builder.Delete().Where("id = " + SQLString::EscapeString(conn, std::to_string(id)));
builder.Delete().Where(Derived::primary_key + " = " + SQLString::EscapeString(conn, id));
std::string query = builder.get();
db->execute(query);
}
Expand All @@ -277,6 +286,10 @@ namespace brazier {
}
}

static void deleteById(int id, const std::shared_ptr<Database>& db) {
Derived::deleteByID(std::to_string(id), db);
}

static bool deleteWhere(const std::string& condition, const std::shared_ptr<Database>& db) {
try {
PGconn* conn = db->getConnection();
Expand Down Expand Up @@ -458,14 +471,18 @@ namespace brazier {
return results.front();
}

static std::shared_ptr<Derived> findOrFail(int id, const std::shared_ptr<Database>& db) {
static std::shared_ptr<Derived> findOrFail(std::string id, const std::shared_ptr<Database>& db) {
auto model = find(id, db);
if (!model) {
throw std::runtime_error("Model with id " + std::to_string(id) + " not found");
throw std::runtime_error("Model with id " + id + " not found");
}
return model;
}

static std::shared_ptr<Derived> findOrFail(int id, const std::shared_ptr<Database>& db) {
return Derived::findOrFail(std::to_string(id), db);
}

/*
@brief Finds the first model matching the given attributes or creates a new one if none exists.
@param attributes A map of attribute names and values to search for.
Expand Down
31 changes: 30 additions & 1 deletion brazierORM/tests/include/migrations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,33 @@ class CreateTestTableDB : public BaseMigration<CreateTestTable> {

return q;
}
};
};

class CreateTestUserTable : public BaseMigration<CreateTestUserTable> {
public:
static std::vector<std::string> up() {
SQLSchemaBuilder builder("test_user_table");
std::vector<std::string> queries;

queries.push_back(builder
.AddColumn("id_user UUID primary key default gen_random_uuid()")
.AddColumn("test varchar(255)")
.AddColumn("description varchar(255)")
.CreateTable()
);

return queries;
}

static std::vector<std::string> down() {
SQLSchemaBuilder builder("test_user_table");
std::vector<std::string> q;

q.push_back(
builder.DropTable()
);

return q;
}
};

13 changes: 13 additions & 0 deletions brazierORM/tests/include/models.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

class TestModel : public brazier::Model<TestModel> {
public:
TestModel() = default;

Check warning on line 29 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Debug, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 29 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Debug, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 29 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Debug, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 29 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Debug, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 29 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Release, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 29 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Release, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 29 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Debug, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 29 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Debug, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 29 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Release, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 29 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Release, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 29 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Release, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 29 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Release, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 29 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Release, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 29 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Release, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 29 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Debug, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 29 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Debug, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]
TestModel(std::shared_ptr<Database> db) : brazier::Model<TestModel>(db) {}

static inline std::vector<std::string> fields = { "id_test", "name", "email" };
Expand All @@ -38,7 +38,7 @@

class LargeTestModel : public brazier::Model<LargeTestModel> {
public:
LargeTestModel() = default;

Check warning on line 41 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Debug, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 41 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Debug, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 41 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Debug, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 41 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Debug, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 41 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Release, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 41 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Release, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 41 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Debug, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 41 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Debug, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 41 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Release, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 41 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Release, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 41 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Release, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 41 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Release, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 41 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Release, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 41 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Release, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 41 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Debug, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 41 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Debug, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]
LargeTestModel(std::shared_ptr<Database> db) : brazier::Model<LargeTestModel>(db) {}

static inline std::vector<std::string> fields = { "id_test", "test_id", "large_test_name", "type" };
Expand All @@ -57,6 +57,19 @@
static inline std::vector<std::string> fields = { "id_test", "test", "description" };


TestModelDB() = default;

Check warning on line 60 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Debug, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 60 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Debug, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 60 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Debug, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 60 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Debug, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 60 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Release, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 60 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Release, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 60 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Debug, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 60 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Debug, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 60 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Release, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 60 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Release, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 60 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Release, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 60 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Release, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 60 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Release, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 60 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Release, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 60 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Debug, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 60 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Debug, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]
TestModelDB(const std::shared_ptr<Database>& db) : Model<TestModelDB>(db) {}
};

class TestUserModel : public Model<TestUserModel> {
public:
static inline std::string table_name = "test_user_table";
static inline std::string primary_key = "id_user";

static inline std::vector<std::string> fillable = { "test", "description" };
static inline std::vector<std::string> fields = { "id_user", "test", "description" };


TestUserModel() = default;

Check warning on line 73 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Debug, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 73 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Debug, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 73 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Debug, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 73 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Debug, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 73 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Release, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 73 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Release, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 73 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Debug, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 73 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Debug, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 73 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Release, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 73 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / linux (clang, Release, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 73 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Release, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 73 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Release, ON)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 73 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Release, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 73 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Release, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 73 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Debug, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]

Check warning on line 73 in brazierORM/tests/include/models.hpp

View workflow job for this annotation

GitHub Actions / macos (clang, Debug, OFF)

explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]
TestUserModel(const std::shared_ptr<Database>& db) : Model<TestUserModel>(db) {}
};
16 changes: 16 additions & 0 deletions brazierORM/tests/unit/test_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,20 @@ TEST_F(TestModelCase, SaveWithSpecialCharacters) {
catch (std::exception& e) {
FAIL() << e.what();
}
}

TEST_F(TestModelCase, SaveUUIdUser) {
try {
manager->migrate<CreateTestUserTable>();
std::shared_ptr<TestUserModel> model = TestUserModel::create({ {"test", "test"}, {"description", "description"} }, 0, db_ptr);
bool res = model->save();
EXPECT_EQ(res, true);

auto id = model->getAttribute("id_user");
std::shared_ptr<TestUserModel> model2 = TestUserModel::find(id, db_ptr);
EXPECT_NE(model2, nullptr);
}
catch (std::exception& e) {
FAIL() << e.what();
}
}
Loading