Stefan Pranger
3 years ago
8 changed files with 208 additions and 0 deletions
-
9CMakeLists.txt
-
5db/db.h
-
0db/migrations/Migration.cpp
-
11db/migrations/Migration.h
-
25db/migrations/builder/AbstractTableBuilder.h
-
31db/migrations/builder/Table.cpp
-
36db/migrations/builder/Table.h
-
91db/migrations/builder/TableBuilder.h
@ -0,0 +1,11 @@ |
|||
#pragma once |
|||
|
|||
#include <db/db.h> |
|||
|
|||
namespace db { |
|||
namespace migrations { |
|||
class Migration { |
|||
|
|||
} |
|||
} |
|||
} |
@ -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); |
|||
}; |
|||
}; |
|||
} |
|||
} |
|||
}*/ |
@ -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; |
|||
} |
|||
} |
|||
} |
|||
} |
@ -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; |
|||
}; |
|||
} |
|||
} |
|||
} |
@ -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"; |
|||
} |
|||
}; |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue