Initial TableBuilder implementation, and simple migrations.

This commit is contained in:
Relintai 2020-12-01 23:37:10 +01:00
parent 401f560dd3
commit b063237399
17 changed files with 252 additions and 18 deletions

View File

@ -119,6 +119,10 @@ void Application::send_file(const std::string &path, Request *request) {
request->send();
}
void Application::migrate() {
}
Application::Application() {
_instance = this;
}

View File

@ -28,6 +28,8 @@ public:
static void default_routing_middleware(Object *instance, Request *request);
virtual void migrate();
Application();
virtual ~Application();

View File

@ -6,10 +6,14 @@ void Database::connect(const std::string &connection_str) {
void Database::query(const std::string &query) {
}
QueryBuilder *Database::get_builder() {
QueryBuilder *Database::get_query_builder() {
return new QueryBuilder();
}
TableBuilder *Database::get_table_builder() {
return new TableBuilder();
}
Database::Database() {
}

View File

@ -5,6 +5,7 @@
#include <memory>
#include "query_builder.h"
#include "table_builder.h"
enum QueryErrorCode {
OK,
@ -37,7 +38,8 @@ public:
virtual void connect(const std::string &connection_str);
virtual void query(const std::string &query);
virtual QueryBuilder *get_builder();
virtual QueryBuilder *get_query_builder();
virtual TableBuilder *get_table_builder();
Database();
~Database();

View File

@ -2,6 +2,10 @@
#include "database.h"
void Object::migrate() {
}
Object::Object() {
db = nullptr;
}

View File

@ -71,6 +71,8 @@ public:
//setting object?
//FileCache? -> set it to the global singleton by default?
virtual void migrate();
Object();
virtual ~Object();
};

50
core/table_builder.cpp Normal file
View File

@ -0,0 +1,50 @@
#include "table_builder.h"
TableBuilder *TableBuilder::create_table(const std::string &name) {
return this;
}
TableBuilder *TableBuilder::integer(const std::string &name) {
return this;
}
TableBuilder *TableBuilder::date(const std::string &name) {
return this;
}
TableBuilder *TableBuilder::varchar(const std::string &name, const int length) {
return this;
}
TableBuilder *TableBuilder::not_null() {
return this;
}
TableBuilder *TableBuilder::null() {
return this;
}
TableBuilder *TableBuilder::auto_increment() {
return this;
}
TableBuilder *TableBuilder::primary_key(const std::string &name) {
return this;
}
TableBuilder *TableBuilder::primary_key() {
return this;
}
TableBuilder *TableBuilder::next_row() {
return this;
}
void TableBuilder::finalize() {
}
TableBuilder::TableBuilder() {
}
TableBuilder::~TableBuilder() {
}

27
core/table_builder.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef TABLE_BUILDER_H
#define TABLE_BUILDER_H
#include <string>
class TableBuilder {
public:
virtual TableBuilder *create_table(const std::string &name);
virtual TableBuilder *integer(const std::string &name);
virtual TableBuilder *date(const std::string &name);
virtual TableBuilder *varchar(const std::string &name, const int length);
virtual TableBuilder *not_null();
virtual TableBuilder *null();
virtual TableBuilder *auto_increment();
virtual TableBuilder *primary_key(const std::string &name);
virtual TableBuilder *primary_key();
virtual TableBuilder *next_row();
virtual void finalize();
TableBuilder();
virtual ~TableBuilder();
std::string result;
};
#endif

View File

@ -5,6 +5,7 @@
#include "core/database_manager.h"
#include "mysql_query_builder.h"
#include "mysql_table_builder.h"
void MysqlDatabase::connect(const std::string &connection_str) {
mysql = mysql_init(mysql);
@ -54,10 +55,14 @@ void MysqlDatabase::query(const std::string &query) {
}
QueryBuilder *MysqlDatabase::get_builder() {
QueryBuilder *MysqlDatabase::get_query_builder() {
return new MysqlQueryBuilder();
}
TableBuilder *MysqlDatabase::get_table_builder() {
return new MysqlTableBuilder();
}
MysqlDatabase::MysqlDatabase() :
Database() {

View File

@ -18,7 +18,8 @@ public:
void connect(const std::string &connection_str);
void query(const std::string &query);
QueryBuilder *get_builder();
QueryBuilder *get_query_builder();
TableBuilder *get_table_builder();
static Database *_creation_func();
static void _register();

View File

@ -0,0 +1,72 @@
#include "mysql_table_builder.h"
TableBuilder *MysqlTableBuilder::create_table(const std::string &name) {
result += "CREATE TABLE " + name + " ( ";
return this;
}
TableBuilder *MysqlTableBuilder::integer(const std::string &name) {
result += name + " INTEGER ";
return this;
}
TableBuilder *MysqlTableBuilder::date(const std::string &name) {
result += name + " DATE ";
return this;
}
TableBuilder *MysqlTableBuilder::varchar(const std::string &name, const int length) {
result += name + " VARCHAR(" + std::to_string(length) + ")";
return this;
}
TableBuilder *MysqlTableBuilder::not_null() {
result += "NOT NULL ";
return this;
}
TableBuilder *MysqlTableBuilder::null() {
result += "NULL ";
return this;
}
TableBuilder *MysqlTableBuilder::auto_increment() {
result += "AUTO_INCREMENT ";
return this;
}
TableBuilder *MysqlTableBuilder::primary_key(const std::string &name) {
result += "PRIMARY KEY (" + name + ") ";
return this;
}
TableBuilder *MysqlTableBuilder::primary_key() {
result += "PRIMARY KEY ";
return this;
}
TableBuilder *MysqlTableBuilder::next_row() {
result += ", ";
return this;
}
void MysqlTableBuilder::finalize() {
result += ");";
}
MysqlTableBuilder::MysqlTableBuilder() {
}
MysqlTableBuilder::~MysqlTableBuilder() {
}

View File

@ -0,0 +1,27 @@
#ifndef MYSQL_TABLE_BUILDER_H
#define MYSQL_TABLE_BUILDER_H
#include <string>
#include "core/table_builder.h"
class MysqlTableBuilder : public TableBuilder {
public:
TableBuilder *create_table(const std::string &name);
TableBuilder *integer(const std::string &name);
TableBuilder *date(const std::string &name);
TableBuilder *varchar(const std::string &name, const int length);
TableBuilder *not_null();
TableBuilder *null();
TableBuilder *auto_increment();
TableBuilder *primary_key(const std::string &name);
TableBuilder *primary_key();
TableBuilder *next_row();
void finalize();
MysqlTableBuilder();
virtual ~MysqlTableBuilder();
};
#endif

View File

@ -1,5 +1,6 @@
#include <iostream>
#include <string>
#include <string.h>
#include "core/application.h"
#include "core/file_cache.h"
@ -14,6 +15,16 @@
#define MAIN_CLASS RDNApplication
int main(int argc, char **argv) {
bool migrate = false;
for (int i = 1; i < argc; ++i) {
const char *a = argv[i];
if (a[0] == 'm') {
migrate = true;
}
}
initialize_database_backends();
FileCache *file_cache = new FileCache(true);
@ -36,7 +47,12 @@ int main(int argc, char **argv) {
server->port = 8080;
server->initialize();
server->main_loop();
if (!migrate) {
server->main_loop();
} else {
app->migrate();
}
delete server;
delete app;

View File

@ -5,13 +5,13 @@
#include "core/query_builder.h"
void MessagePage::index(Request *request) {
QueryBuilder *b = db->get_builder();
QueryBuilder *b = db->get_query_builder();
b->select("*")->from("tutorials_tbl")->finalize();
b->select("*")->from("tutorials_tbl")->finalize();
db->query(b->query_result);
db->query(b->query_result);
delete b;
delete b;
/*
db->query("show databases;");
@ -19,23 +19,33 @@ void MessagePage::index(Request *request) {
db->query("SELECT * FROM tutorials_tbl;");
*/
std::string r = "<html><body>";
std::string r = "<html><body>";
for (uint32_t i = 0; i < messages.size(); ++i) {
r += "<p>" + messages[i] + "</p><br>";
}
for (uint32_t i = 0; i < messages.size(); ++i) {
r += "<p>" + messages[i] + "</p><br>";
}
r += "</html></body>";
r += "</html></body>";
request->response->setBody(r);
request->send();
}
MessagePage::MessagePage() : Object() {
messages.push_back("T message 1");
messages.push_back("T message 2");
void MessagePage::migrate() {
TableBuilder *t = db->get_table_builder();
t->create_table("message_page")->integer("id")->auto_increment()->primary_key()->next_row()->varchar("dd", 30)->finalize();
db->query(t->result);
delete t;
}
MessagePage::MessagePage() :
Object() {
messages.push_back("T message 1");
messages.push_back("T message 2");
}
MessagePage::~MessagePage() {
}

View File

@ -15,6 +15,8 @@ class MessagePage : public Object {
public:
void index(Request *request);
void migrate();
MessagePage();
~MessagePage();

View File

@ -55,6 +55,10 @@ void RDNApplication::setup_middleware() {
//middlewares.push_back(RDNApplication::session_middleware_func);
}
void RDNApplication::migrate() {
message_page->migrate();
}
RDNApplication::RDNApplication() :
Application() {

View File

@ -18,6 +18,8 @@ public:
virtual void setup_routes();
virtual void setup_middleware();
virtual void migrate();
RDNApplication();
~RDNApplication();