Skip to content

Commit

Permalink
add draft of WHERE cluse in query class
Browse files Browse the repository at this point in the history
  • Loading branch information
wsekta committed Feb 4, 2024
1 parent d4091bb commit 319b029
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ set(SOURCES
src/database/defaults/DefaultInsertCommand.cpp
src/database/defaults/DefaultSelectCommand.cpp
src/database/CommandGenerator.cpp
src/query/Condition.cpp
)

add_library(${LIBRARY_NAME} ${SOURCES})
Expand Down Expand Up @@ -91,6 +92,7 @@ if (BUILD_ORM_CXX_TESTS)
src/database/defaults/DefaultDropTableCommandTest.cpp
src/database/defaults/DefaultInsertCommandTest.cpp
src/database/defaults/DefaultSelectCommandTest.cpp
src/query/ConditionTest.cpp
)

set(GTEST_INCLUDE_DIR
Expand Down
11 changes: 11 additions & 0 deletions include/orm-cxx/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ class Query
*/
Query() : data{.modelInfo = Model<T>().getModelInfo()} {}

/**
* @brief Where clause
* @param condition
*/
auto where(const query::Condition& condition) -> Query<T>&
{
data.condition = condition;

return *this;
}

/**
* @brief Sets the OFFSET clause for the select query.
* @param offset The number of rows to skip.
Expand Down
53 changes: 53 additions & 0 deletions include/orm-cxx/query/Condition.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include <string>

namespace orm::query
{
class Condition
{
enum class Operator
{
EQUAL,
NOT_EQUAL,
GREATER,
GREATER_OR_EQUAL,
LESS,
LESS_OR_EQUAL,
LIKE,
NOT_LIKE,
IS_NULL,
IS_NOT_NULL
};

public:
Condition(std::string columnName);

// auto operator==(const std::string& value) -> Condition&;
//
// auto operator!=(const std::string& value) -> Condition&;
//
// auto operator>(const std::string& value) -> Condition&;
//
// auto operator>=(const std::string& value) -> Condition&;
//
// auto operator<(const std::string& value) -> Condition&;
//
// auto operator<=(const std::string& value) -> Condition&;

auto like(const std::string& value) -> Condition&;

auto notLike(const std::string& value) -> Condition&;

auto isNull() -> Condition&;

auto isNotNull() -> Condition&;

private:
std::string columnName;

Operator operatorType;

std::string comparisonValue;
};
} // namespace orm::query
10 changes: 6 additions & 4 deletions include/orm-cxx/query/QueryData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
#include <optional>
#include <string>

#include "Condition.hpp"
#include "orm-cxx/model/ModelInfo.hpp"

namespace orm::query
{
struct QueryData
{
const model::ModelInfo& modelInfo; /**< The model info for the query. */
std::optional<std::size_t> offset = std::nullopt; /**< The optional OFFSET value for the query. */
std::optional<std::size_t> limit = std::nullopt; /**< The optional LIMIT value for the query. */
bool shouldJoin = true; /**< The flag that indicates if the query should join. */
const model::ModelInfo& modelInfo; /**< The model info for the query. */
std::optional<std::size_t> offset = std::nullopt; /**< The optional OFFSET value for the query. */
std::optional<std::size_t> limit = std::nullopt; /**< The optional LIMIT value for the query. */
std::optional<Condition> condition = std::nullopt; /**< The optional WHERE condition(s) */
bool shouldJoin = true; /**< The flag that indicates if the query should join. */
};
}
32 changes: 32 additions & 0 deletions src/query/Condition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "orm-cxx/query/Condition.hpp"

namespace orm::query
{
Condition::Condition(std::string columnName) : columnName{std::move(columnName)} {}

auto Condition::like(const std::string& value) -> Condition&
{
operatorType = Operator::LIKE;
comparisonValue = value;
return *this;
}

auto Condition::notLike(const std::string& value) -> Condition&
{
operatorType = Operator::NOT_LIKE;
comparisonValue = value;
return *this;
}

auto Condition::isNull() -> Condition&
{
operatorType = Operator::IS_NULL;
return *this;
}

auto Condition::isNotNull() -> Condition&
{
operatorType = Operator::IS_NOT_NULL;
return *this;
}
} // namespace orm::query
41 changes: 41 additions & 0 deletions src/query/ConditionTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "orm-cxx/query/Condition.hpp"

#include <gtest/gtest.h>

namespace
{

}

class ConditionTest : public ::testing::Test
{
};

TEST_F(ConditionTest, shouldCreateCondition)
{
orm::query::Condition condition{"test"};
}

TEST_F(ConditionTest, shouldSetLikeCondition)
{
orm::query::Condition condition{"test"};
condition.like("value");
}

TEST_F(ConditionTest, shouldSetNotLikeCondition)
{
orm::query::Condition condition{"test"};
condition.notLike("value");
}

TEST_F(ConditionTest, shouldSetIsNullCondition)
{
orm::query::Condition condition{"test"};
condition.isNull();
}

TEST_F(ConditionTest, shouldSetIsNotNullCondition)
{
orm::query::Condition condition{"test"};
condition.isNotNull();
}

0 comments on commit 319b029

Please sign in to comment.