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.
 
 
 
 

91 lines
2.7 KiB

#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";
}
};
}
}
}