Browse Source

added functionality to StatementBuilders

main
Stefan Pranger 3 years ago
parent
commit
75398ebfcb
  1. 2
      CMakeLists.txt
  2. 21
      db/statements/builder/DeleteStatementBuilder.cpp
  3. 20
      db/statements/builder/DeleteStatementBuilder.h
  4. 6
      db/statements/builder/SelectStatementBuilder.cpp
  5. 1
      db/statements/builder/SelectStatementBuilder.h
  6. 10
      db/statements/builder/Statement.cpp
  7. 6
      db/statements/builder/Statement.h
  8. 1
      db/statements/builder/StatementBuilder.h
  9. 27
      db/statements/builder/UpdateStatementBuilder.cpp
  10. 21
      db/statements/builder/UpdateStatementBuilder.h

2
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)

21
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;
}
}
}
}

20
db/statements/builder/DeleteStatementBuilder.h

@ -0,0 +1,20 @@
#pragma once
#include "Statement.h"
#include "StatementBuilder.h"
#include <string>
#include <vector>
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();
};
}
}
}

6
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;

1
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);

10
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";
}

6
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:

1
db/statements/builder/StatementBuilder.h

@ -9,6 +9,7 @@ namespace db {
namespace builder {
class SelectStatementBuilder;
class InsertStatementBuilder;
class UpdateStatementBuilder;
class AbstractStatementBuilder {
protected:

27
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;
}
}
}
}

21
db/statements/builder/UpdateStatementBuilder.h

@ -0,0 +1,21 @@
#pragma once
#include "Statement.h"
#include "StatementBuilder.h"
#include <string>
#include <vector>
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();
};
}
}
}
Loading…
Cancel
Save