Browse Source

added builder code for db migrations

main
Stefan Pranger 4 years ago
parent
commit
fccaa5d639
  1. 9
      CMakeLists.txt
  2. 5
      db/db.h
  3. 0
      db/migrations/Migration.cpp
  4. 11
      db/migrations/Migration.h
  5. 25
      db/migrations/builder/AbstractTableBuilder.h
  6. 31
      db/migrations/builder/Table.cpp
  7. 36
      db/migrations/builder/Table.h
  8. 91
      db/migrations/builder/TableBuilder.h

9
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)

5
db/db.h

@ -6,3 +6,8 @@
namespace sql = SQLite;
#include <db/create.h>
#include <db/schema.h>
#include <db/migrations/builder/Table.h>
#include <db/migrations/builder/TableBuilder.h>
namespace db_builder = db::migrations::builder;

0
db/migrations/Migration.cpp

11
db/migrations/Migration.h

@ -0,0 +1,11 @@
#pragma once
#include <db/db.h>
namespace db {
namespace migrations {
class Migration {
}
}
}

25
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);
};
};
}
}
}*/

31
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;
}
}
}
}

36
db/migrations/builder/Table.h

@ -0,0 +1,36 @@
#pragma once
#include <iostream>
#include <string>
#include <sstream>
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;
};
}
}
}

91
db/migrations/builder/TableBuilder.h

@ -0,0 +1,91 @@
#pragma once
#include "Table.h"
#include <string>
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";
}
};
}
}
}
Loading…
Cancel
Save