From fccaa5d639b266e2a49643fcc360fb09c184fef0 Mon Sep 17 00:00:00 2001 From: Stefan Pranger Date: Mon, 28 Jun 2021 13:44:02 +0200 Subject: [PATCH] added builder code for db migrations --- CMakeLists.txt | 9 ++ db/db.h | 5 ++ db/migrations/Migration.cpp | 0 db/migrations/Migration.h | 11 +++ db/migrations/builder/AbstractTableBuilder.h | 25 ++++++ db/migrations/builder/Table.cpp | 31 +++++++ db/migrations/builder/Table.h | 36 ++++++++ db/migrations/builder/TableBuilder.h | 91 ++++++++++++++++++++ 8 files changed, 208 insertions(+) create mode 100644 db/migrations/Migration.cpp create mode 100644 db/migrations/Migration.h create mode 100644 db/migrations/builder/AbstractTableBuilder.h create mode 100644 db/migrations/builder/Table.cpp create mode 100644 db/migrations/builder/Table.h create mode 100644 db/migrations/builder/TableBuilder.h diff --git a/CMakeLists.txt b/CMakeLists.txt index aa89c3a..e992712 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,16 @@ add_executable(notification-daemon ical/Alarm.cpp ical/Event.cpp ical/IcalObject.h + db/schema.h + db/create.h + db/db.h + db/schema.h + db/migrations/Migration.h + db/migrations/builder/TableBuilder.h + db/migrations/builder/Table.h + db/migrations/builder/Table.cpp io/debug.cpp + 1624829187_create_events.h ) add_definitions(-DLOG_DEBUG) diff --git a/db/db.h b/db/db.h index 265d8e3..e118d8e 100644 --- a/db/db.h +++ b/db/db.h @@ -6,3 +6,8 @@ namespace sql = SQLite; #include +#include +#include +#include + +namespace db_builder = db::migrations::builder; diff --git a/db/migrations/Migration.cpp b/db/migrations/Migration.cpp new file mode 100644 index 0000000..e69de29 diff --git a/db/migrations/Migration.h b/db/migrations/Migration.h new file mode 100644 index 0000000..9e02be7 --- /dev/null +++ b/db/migrations/Migration.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace db { + namespace migrations { + class Migration { + + } + } +} diff --git a/db/migrations/builder/AbstractTableBuilder.h b/db/migrations/builder/AbstractTableBuilder.h new file mode 100644 index 0000000..f9c79a2 --- /dev/null +++ b/db/migrations/builder/AbstractTableBuilder.h @@ -0,0 +1,25 @@ +/*#pragma once +#include "Table.h" + + + +namespace db { + namespace migrations { + namespace builder { + class CreateTableBuilder; + class AlterTableBuilder; + class DropTableBuilder; + + class AbstractTableBuilder { + protected: + Table &table; + explicit AbstractTableBuilder(Table &table) : table(table) {} + public: + operator Table() const + { + return std::move(table); + }; + }; + } + } +}*/ diff --git a/db/migrations/builder/Table.cpp b/db/migrations/builder/Table.cpp new file mode 100644 index 0000000..0bbaf01 --- /dev/null +++ b/db/migrations/builder/Table.cpp @@ -0,0 +1,31 @@ +#include "Table.h" +#include "TableBuilder.h" + +namespace db { + namespace migrations { + namespace builder { + class CreateTableBuilder; + + CreateTableBuilder Table::create(std::string table_name) { + return CreateTableBuilder{table_name}; + } + + AlterTableBuilder Table::alter(std::string table_name) { + return AlterTableBuilder{table_name}; + } + + DropTableBuilder Table::drop(std::string table_name) { + return DropTableBuilder{table_name}; + } + + std::string Table::str() const { + return header + "\n" + body + "\n"; + } + + std::ostream &operator<<(std::ostream &os, const Table &t) { + return os << t.header << std::endl + << t.body << std::endl; + } + } + } +} diff --git a/db/migrations/builder/Table.h b/db/migrations/builder/Table.h new file mode 100644 index 0000000..b5ec6c2 --- /dev/null +++ b/db/migrations/builder/Table.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include +#include + +namespace db { + namespace migrations { + namespace builder { + class TableBuilder; + + class CreateTableBuilder; + class AlterTableBuilder; + class DropTableBuilder; + + class Table { + public: + Table() = default; + + friend class CreateTableBuilder; + friend class AlterTableBuilder; + friend class DropTableBuilder; + friend std::ostream &operator<<(std::ostream &os, const Table &t); + + static CreateTableBuilder create(const std::string table_name); + static AlterTableBuilder alter(const std::string table_name); + static DropTableBuilder drop(const std::string table_name); + + std::string str() const; + private: + std::string header; + std::string body; + }; + } + } +} diff --git a/db/migrations/builder/TableBuilder.h b/db/migrations/builder/TableBuilder.h new file mode 100644 index 0000000..a26f6f4 --- /dev/null +++ b/db/migrations/builder/TableBuilder.h @@ -0,0 +1,91 @@ +#pragma once + +#include "Table.h" +#include + +namespace db { + namespace migrations { + namespace builder { + class CreateTableBuilder; + class AlterTableBuilder; + class DropTableBuilder; + + class AbstractTableBuilder { + protected: + Table &m_table; + explicit AbstractTableBuilder(Table &table) : m_table(table) {} + public: + operator Table() const { + return std::move(m_table); + } + }; + + class TableBuilder : public AbstractTableBuilder { + public: + Table m_table; + TableBuilder() : AbstractTableBuilder{m_table} { + } + }; + + class CreateTableBuilder : public TableBuilder { + public: + bool is_primary_set = false; + + explicit CreateTableBuilder(const std::string &table_name) : TableBuilder() { + m_table.header += "CREATE TABLE IF NOT EXISTS " + table_name + " ("; + } + + CreateTableBuilder &null(const std::string &i, const std::string &opts = "") { + m_table.body += "\t" + i + " NULL " + opts + ",\n"; + return *this; + } + + CreateTableBuilder &integer(const std::string &i, const std::string &opts = "") { + m_table.body += "\t" + i + " INTEGER " + opts + ",\n"; + return *this; + } + + CreateTableBuilder &real(const std::string &i, const std::string &opts = "") { + m_table.body += "\t" + i + " REAL " + opts + ",\n"; + return *this; + } + + CreateTableBuilder &text(const std::string &i, const std::string &opts = "") { + m_table.body += "\t" + i + " TEXT " + opts + ",\n"; + return *this; + } + + CreateTableBuilder &blob(const std::string &i, const std::string &opts = "") { + m_table.body += "\t" + i + " BLOB " + opts + ",\n"; + return *this; + } + + CreateTableBuilder &close() { + m_table.body.pop_back(); + m_table.body.pop_back(); + m_table.body += ");\n"; + return *this; + } + }; + + class AlterTableBuilder : public TableBuilder { + public: + explicit AlterTableBuilder(std::string table_name) : TableBuilder() { + m_table.header = "ALTER TABLE " + table_name; + } + + AlterTableBuilder &rename_column(const std::string &column_old, const std::string &column_new) { + m_table.body += "RENAME COLUMN " + column_old + " TO " + column_new + ";\n"; + return *this; + } + }; + + class DropTableBuilder : public TableBuilder { + public: + explicit DropTableBuilder(std::string table_name) : TableBuilder() { + m_table.header = "DROP TABLE IF EXISTS " + table_name + ";\n"; + } + }; + } + } +}