diff --git a/CMakeLists.txt b/CMakeLists.txt index 0316085..3beb1d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,8 @@ add_executable(notification-daemon db/statements/builder/Statement.cpp db/statements/builder/SelectStatementBuilder.cpp db/statements/builder/InsertStatementBuilder.cpp + db/statements/builder/UpdateStatementBuilder.cpp + db/statements/builder/DeleteStatementBuilder.cpp ) set(CMAKE_VERBOSE_MAKEFILE off) diff --git a/db/statements/builder/DeleteStatementBuilder.cpp b/db/statements/builder/DeleteStatementBuilder.cpp new file mode 100644 index 0000000..6a94fe1 --- /dev/null +++ b/db/statements/builder/DeleteStatementBuilder.cpp @@ -0,0 +1,21 @@ +#include "DeleteStatementBuilder.h" + +namespace db { + namespace statements { + namespace builder { + DeleteStatementBuilder &DeleteStatementBuilder::table(const std::string &table) { + m_statement.body = "DELETE FROM " + table; + return *this; + } + DeleteStatementBuilder &DeleteStatementBuilder::where(const std::string &where) { + m_statement.body += " WHERE " + where; + return *this; + } + + DeleteStatementBuilder &DeleteStatementBuilder::close() { + m_statement.body += ";\n"; + return *this; + } + } + } +} diff --git a/db/statements/builder/DeleteStatementBuilder.h b/db/statements/builder/DeleteStatementBuilder.h new file mode 100644 index 0000000..f4a86da --- /dev/null +++ b/db/statements/builder/DeleteStatementBuilder.h @@ -0,0 +1,20 @@ +#pragma once + +#include "Statement.h" +#include "StatementBuilder.h" +#include +#include + +namespace db { + namespace statements { + namespace builder { + class DeleteStatementBuilder : public StatementBuilder { + public: + explicit DeleteStatementBuilder() : StatementBuilder() {} + DeleteStatementBuilder &table(const std::string &table); + DeleteStatementBuilder &where(const std::string &where); + DeleteStatementBuilder &close(); + }; + } + } +} diff --git a/db/statements/builder/SelectStatementBuilder.cpp b/db/statements/builder/SelectStatementBuilder.cpp index bfde400..531fea2 100644 --- a/db/statements/builder/SelectStatementBuilder.cpp +++ b/db/statements/builder/SelectStatementBuilder.cpp @@ -3,6 +3,12 @@ namespace db { namespace statements { namespace builder { + SelectStatementBuilder &SelectStatementBuilder::exists() { + m_statement.body.insert(0, "SELECT EXISTS("); + m_statement.body += " LIMIT 1);"; + return *this; + } + SelectStatementBuilder &SelectStatementBuilder::columns(const std::string &cols) { m_statement.body += "SELECT " + cols; return *this; diff --git a/db/statements/builder/SelectStatementBuilder.h b/db/statements/builder/SelectStatementBuilder.h index f6819f5..2f159de 100644 --- a/db/statements/builder/SelectStatementBuilder.h +++ b/db/statements/builder/SelectStatementBuilder.h @@ -11,6 +11,7 @@ namespace db { class SelectStatementBuilder : public StatementBuilder { public: explicit SelectStatementBuilder() : StatementBuilder() {} + SelectStatementBuilder &exists(); SelectStatementBuilder &columns(const std::string &cols); SelectStatementBuilder &where(const std::string &where); SelectStatementBuilder &inner_join(const std::string &table, const std::string &on); diff --git a/db/statements/builder/Statement.cpp b/db/statements/builder/Statement.cpp index edb8b69..f726677 100644 --- a/db/statements/builder/Statement.cpp +++ b/db/statements/builder/Statement.cpp @@ -2,6 +2,8 @@ #include "StatementBuilder.h" #include "InsertStatementBuilder.h" #include "SelectStatementBuilder.h" +#include "UpdateStatementBuilder.h" +#include "DeleteStatementBuilder.h" namespace db { namespace statements { @@ -15,6 +17,14 @@ namespace db { return SelectStatementBuilder{}; } + UpdateStatementBuilder Statement::update() { + return UpdateStatementBuilder{}; + } + + DeleteStatementBuilder Statement::remove() { + return DeleteStatementBuilder{}; + } + std::string Statement::str() const { return body + "\n"; } diff --git a/db/statements/builder/Statement.h b/db/statements/builder/Statement.h index 5287881..bdd9335 100644 --- a/db/statements/builder/Statement.h +++ b/db/statements/builder/Statement.h @@ -10,6 +10,8 @@ namespace db { class StatementBuilder; class InsertStatementBuilder; class SelectStatementBuilder; + class UpdateStatementBuilder; + class DeleteStatementBuilder; class Statement { public: @@ -17,10 +19,14 @@ namespace db { friend class InsertStatementBuilder; friend class SelectStatementBuilder; + friend class UpdateStatementBuilder; + friend class DeleteStatementBuilder; friend std::ostream &operator<<(std::ostream &os, const Statement &s); static InsertStatementBuilder insert(); static SelectStatementBuilder select(); + static UpdateStatementBuilder update(); + static DeleteStatementBuilder remove(); std::string str() const; private: diff --git a/db/statements/builder/StatementBuilder.h b/db/statements/builder/StatementBuilder.h index 569b805..954dff5 100644 --- a/db/statements/builder/StatementBuilder.h +++ b/db/statements/builder/StatementBuilder.h @@ -9,6 +9,7 @@ namespace db { namespace builder { class SelectStatementBuilder; class InsertStatementBuilder; + class UpdateStatementBuilder; class AbstractStatementBuilder { protected: diff --git a/db/statements/builder/UpdateStatementBuilder.cpp b/db/statements/builder/UpdateStatementBuilder.cpp new file mode 100644 index 0000000..5214d0c --- /dev/null +++ b/db/statements/builder/UpdateStatementBuilder.cpp @@ -0,0 +1,27 @@ +#include "UpdateStatementBuilder.h" + +namespace db { + namespace statements { + namespace builder { + UpdateStatementBuilder &UpdateStatementBuilder::table(const std::string &table) { + m_statement.body += "UPDATE " + table; + return *this; + } + + UpdateStatementBuilder &UpdateStatementBuilder::set(const std::string &set) { + m_statement.body += " SET " + set + " "; + return *this; + } + + UpdateStatementBuilder &UpdateStatementBuilder::where(const std::string &where) { + m_statement.body += " WHERE " + where; + return *this; + } + + UpdateStatementBuilder &UpdateStatementBuilder::close() { + m_statement.body += ";\n"; + return *this; + } + } + } +} diff --git a/db/statements/builder/UpdateStatementBuilder.h b/db/statements/builder/UpdateStatementBuilder.h new file mode 100644 index 0000000..bbbb025 --- /dev/null +++ b/db/statements/builder/UpdateStatementBuilder.h @@ -0,0 +1,21 @@ +#pragma once + +#include "Statement.h" +#include "StatementBuilder.h" +#include +#include + +namespace db { + namespace statements { + namespace builder { + class UpdateStatementBuilder : public StatementBuilder { + public: + explicit UpdateStatementBuilder() : StatementBuilder() {} + UpdateStatementBuilder &table(const std::string &table); + UpdateStatementBuilder &set(const std::string &set); + UpdateStatementBuilder &where(const std::string &where); + UpdateStatementBuilder &close(); + }; + } + } +}