You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.9 KiB

  1. #pragma once
  2. #include "Statement.h"
  3. #include <string>
  4. #include <vector>
  5. namespace db {
  6. namespace statements {
  7. namespace builder {
  8. class InsertStatementBuilder;
  9. class AbstractStatementBuilder {
  10. protected:
  11. Statement &m_statement;
  12. explicit AbstractStatementBuilder(Statement &statement) : m_statement(statement) {}
  13. public:
  14. operator Statement() const {
  15. return std::move(m_statement);
  16. }
  17. };
  18. class StatementBuilder : public AbstractStatementBuilder {
  19. public:
  20. Statement m_statement;
  21. StatementBuilder() : AbstractStatementBuilder{m_statement} {
  22. }
  23. };
  24. class InsertStatementBuilder : public StatementBuilder {
  25. public:
  26. explicit InsertStatementBuilder() : StatementBuilder() {
  27. }
  28. InsertStatementBuilder &table(const std::string &table) {
  29. m_statement.body += "INSERT INTO " + table + " ";
  30. return *this;
  31. }
  32. InsertStatementBuilder &columns(const std::string &cols) {
  33. m_statement.body += "(" + cols + ")\n";
  34. return *this;
  35. }
  36. InsertStatementBuilder &values() {
  37. m_statement.body += " VALUES ";
  38. return *this;
  39. }
  40. InsertStatementBuilder &row(const std::string &values) {
  41. m_statement.body += " (" + values + "),\n";
  42. return *this;
  43. }
  44. InsertStatementBuilder &row(const std::vector<std::string> &values) {
  45. m_statement.body += " (";
  46. bool first = true;
  47. for(auto const& v : values) {
  48. if(first) { first = false; } else { m_statement.body += ","; }
  49. m_statement.body += v;
  50. }
  51. m_statement.body += "),\n";
  52. return *this;
  53. }
  54. InsertStatementBuilder &close() {
  55. m_statement.body.pop_back();
  56. m_statement.body.pop_back();
  57. m_statement.body += ";\n";
  58. return *this;
  59. }
  60. };
  61. }
  62. }
  63. }