7 changed files with 132 additions and 45 deletions
			
			
		- 
					45db/statements/builder/InsertStatementBuilder.cpp
 - 
					23db/statements/builder/InsertStatementBuilder.h
 - 
					32db/statements/builder/SelectStatementBuilder.cpp
 - 
					22db/statements/builder/SelectStatementBuilder.h
 - 
					7db/statements/builder/Statement.cpp
 - 
					3db/statements/builder/Statement.h
 - 
					45db/statements/builder/StatementBuilder.h
 
@ -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; | 
				
			|||
      } | 
				
			|||
    } | 
				
			|||
  } | 
				
			|||
} | 
				
			|||
@ -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(); | 
				
			|||
      }; | 
				
			|||
    } | 
				
			|||
  } | 
				
			|||
} | 
				
			|||
@ -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; | 
				
			|||
      } | 
				
			|||
    } | 
				
			|||
  } | 
				
			|||
} | 
				
			|||
@ -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(); | 
				
			|||
      }; | 
				
			|||
    } | 
				
			|||
  } | 
				
			|||
} | 
				
			|||
						Write
						Preview
					
					
					Loading…
					
					Cancel
						Save
					
		Reference in new issue