Browse Source

added stmtbuilder and split up source

main
Stefan Pranger 3 years ago
parent
commit
f60e0f24d2
  1. 45
      db/statements/builder/InsertStatementBuilder.cpp
  2. 23
      db/statements/builder/InsertStatementBuilder.h
  3. 32
      db/statements/builder/SelectStatementBuilder.cpp
  4. 22
      db/statements/builder/SelectStatementBuilder.h
  5. 7
      db/statements/builder/Statement.cpp
  6. 3
      db/statements/builder/Statement.h
  7. 45
      db/statements/builder/StatementBuilder.h

45
db/statements/builder/InsertStatementBuilder.cpp

@ -0,0 +1,45 @@
#include "InsertStatementBuilder.h"
namespace db {
namespace statements {
namespace builder {
InsertStatementBuilder &InsertStatementBuilder::table(const std::string &table) {
m_statement.body += "INSERT INTO " + table + " ";
return *this;
}
InsertStatementBuilder &InsertStatementBuilder::columns(const std::string &cols) {
m_statement.body += "(" + cols + ")\n";
return *this;
}
InsertStatementBuilder &InsertStatementBuilder::values() {
m_statement.body += " VALUES ";
return *this;
}
InsertStatementBuilder &InsertStatementBuilder::row(const std::string &values) {
m_statement.body += " (" + values + "),\n";
return *this;
}
InsertStatementBuilder &InsertStatementBuilder::row(const std::vector<std::string> &values) {
m_statement.body += " (";
bool first = true;
for(auto const& v : values) {
if(first) { first = false; } else { m_statement.body += ","; }
m_statement.body += v;
}
m_statement.body += "),\n";
return *this;
}
InsertStatementBuilder &InsertStatementBuilder::close() {
m_statement.body.pop_back();
m_statement.body.pop_back();
m_statement.body += ";\n";
return *this;
}
}
}
}

23
db/statements/builder/InsertStatementBuilder.h

@ -0,0 +1,23 @@
#pragma once
#include "Statement.h"
#include "StatementBuilder.h"
#include <string>
#include <vector>
namespace db {
namespace statements {
namespace builder {
class InsertStatementBuilder : public StatementBuilder {
public:
explicit InsertStatementBuilder() : StatementBuilder() {}
InsertStatementBuilder &table(const std::string &table);
InsertStatementBuilder &columns(const std::string &cols);
InsertStatementBuilder &values();
InsertStatementBuilder &row(const std::string &values);
InsertStatementBuilder &row(const std::vector<std::string> &values);
InsertStatementBuilder &close();
};
}
}
}

32
db/statements/builder/SelectStatementBuilder.cpp

@ -0,0 +1,32 @@
#include "SelectStatementBuilder.h"
namespace db {
namespace statements {
namespace builder {
SelectStatementBuilder &SelectStatementBuilder::columns(const std::string &cols) {
m_statement.body += "SELECT " + cols;
return *this;
}
SelectStatementBuilder &SelectStatementBuilder::from_table(const std::string &table) {
m_statement.body += " FROM " + table + " ";
return *this;
}
SelectStatementBuilder &SelectStatementBuilder::inner_join(const std::string &table, const std::string &on) {
m_statement.body += " INNER JOIN " + table + " ON " + on;
return *this;
}
SelectStatementBuilder &SelectStatementBuilder::where(const std::string &where) {
m_statement.body += " WHERE " + where;
return *this;
}
SelectStatementBuilder &SelectStatementBuilder::close() {
m_statement.body += ";\n";
return *this;
}
}
}
}

22
db/statements/builder/SelectStatementBuilder.h

@ -0,0 +1,22 @@
#pragma once
#include "Statement.h"
#include "StatementBuilder.h"
#include <string>
#include <vector>
namespace db {
namespace statements {
namespace builder {
class SelectStatementBuilder : public StatementBuilder {
public:
explicit SelectStatementBuilder() : StatementBuilder() {}
SelectStatementBuilder &columns(const std::string &cols);
SelectStatementBuilder &where(const std::string &where);
SelectStatementBuilder &inner_join(const std::string &table, const std::string &on);
SelectStatementBuilder &from_table(const std::string &table);
SelectStatementBuilder &close();
};
}
}
}

7
db/statements/builder/Statement.cpp

@ -1,15 +1,20 @@
#include "Statement.h"
#include "StatementBuilder.h"
#include "InsertStatementBuilder.h"
#include "SelectStatementBuilder.h"
namespace db {
namespace statements {
namespace builder {
class InsertStatementBuilder;
InsertStatementBuilder Statement::insert() {
return InsertStatementBuilder{};
}
SelectStatementBuilder Statement::select() {
return SelectStatementBuilder{};
}
std::string Statement::str() const {
return body + "\n";
}

3
db/statements/builder/Statement.h

@ -9,15 +9,18 @@ namespace db {
namespace builder {
class StatementBuilder;
class InsertStatementBuilder;
class SelectStatementBuilder;
class Statement {
public:
Statement() = default;
friend class InsertStatementBuilder;
friend class SelectStatementBuilder;
friend std::ostream &operator<<(std::ostream &os, const Statement &s);
static InsertStatementBuilder insert();
static SelectStatementBuilder select();
std::string str() const;
private:

45
db/statements/builder/StatementBuilder.h

@ -7,6 +7,7 @@
namespace db {
namespace statements {
namespace builder {
class SelectStatementBuilder;
class InsertStatementBuilder;
class AbstractStatementBuilder {
@ -25,50 +26,6 @@ namespace db {
StatementBuilder() : AbstractStatementBuilder{m_statement} {
}
};
class InsertStatementBuilder : public StatementBuilder {
public:
explicit InsertStatementBuilder() : StatementBuilder() {
}
InsertStatementBuilder &table(const std::string &table) {
m_statement.body += "INSERT INTO " + table + " ";
return *this;
}
InsertStatementBuilder &columns(const std::string &cols) {
m_statement.body += "(" + cols + ")\n";
return *this;
}
InsertStatementBuilder &values() {
m_statement.body += " VALUES ";
return *this;
}
InsertStatementBuilder &row(const std::string &values) {
m_statement.body += " (" + values + "),\n";
return *this;
}
InsertStatementBuilder &row(const std::vector<std::string> &values) {
m_statement.body += " (";
bool first = true;
for(auto const& v : values) {
if(first) { first = false; } else { m_statement.body += ","; }
m_statement.body += v;
}
m_statement.body += "),\n";
return *this;
}
InsertStatementBuilder &close() {
m_statement.body.pop_back();
m_statement.body.pop_back();
m_statement.body += ";\n";
return *this;
}
};
}
}
}
Loading…
Cancel
Save