Stefan Pranger
3 years ago
4 changed files with 127 additions and 0 deletions
-
3db/db.h
-
22db/statements/builder/Statement.cpp
-
28db/statements/builder/Statement.h
-
74db/statements/builder/StatementBuilder.h
@ -0,0 +1,22 @@ |
|||
#include "Statement.h"
|
|||
#include "StatementBuilder.h"
|
|||
|
|||
namespace db { |
|||
namespace statements { |
|||
namespace builder { |
|||
class InsertStatementBuilder; |
|||
|
|||
InsertStatementBuilder Statement::insert() { |
|||
return InsertStatementBuilder{}; |
|||
} |
|||
|
|||
std::string Statement::str() const { |
|||
return body + "\n"; |
|||
} |
|||
|
|||
std::ostream &operator<<(std::ostream &os, const Statement &s) { |
|||
return os << s.body << std::endl; |
|||
} |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,28 @@ |
|||
#pragma once |
|||
|
|||
#include <iostream> |
|||
#include <string> |
|||
#include <sstream> |
|||
|
|||
namespace db { |
|||
namespace statements { |
|||
namespace builder { |
|||
class StatementBuilder; |
|||
class InsertStatementBuilder; |
|||
|
|||
class Statement { |
|||
public: |
|||
Statement() = default; |
|||
|
|||
friend class InsertStatementBuilder; |
|||
friend std::ostream &operator<<(std::ostream &os, const Statement &s); |
|||
|
|||
static InsertStatementBuilder insert(); |
|||
|
|||
std::string str() const; |
|||
private: |
|||
std::string body; |
|||
}; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,74 @@ |
|||
#pragma once |
|||
|
|||
#include "Statement.h" |
|||
#include <string> |
|||
#include <vector> |
|||
|
|||
namespace db { |
|||
namespace statements { |
|||
namespace builder { |
|||
class InsertStatementBuilder; |
|||
|
|||
class AbstractStatementBuilder { |
|||
protected: |
|||
Statement &m_statement; |
|||
explicit AbstractStatementBuilder(Statement &statement) : m_statement(statement) {} |
|||
public: |
|||
operator Statement() const { |
|||
return std::move(m_statement); |
|||
} |
|||
}; |
|||
|
|||
class StatementBuilder : public AbstractStatementBuilder { |
|||
public: |
|||
Statement m_statement; |
|||
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; |
|||
} |
|||
}; |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue