Lots of work on the table builder.

This commit is contained in:
Relintai 2021-08-19 23:55:16 +02:00
parent 437b084831
commit d2a0f25369
8 changed files with 128 additions and 17 deletions

View File

@ -1,5 +1,7 @@
#include "table_builder.h"
#include "query_result.h"
TableBuilder *TableBuilder::create_table(const std::string &name) {
return this;
}
@ -40,11 +42,42 @@ TableBuilder *TableBuilder::next_row() {
return this;
}
TableBuilder *TableBuilder::drop_table(const std::string &name) {
TableBuilder *TableBuilder::ccreate_table() {
return this;
}
void TableBuilder::finalize() {
TableBuilder *TableBuilder::drop_table() {
return this;
}
TableBuilder *TableBuilder::drop_table_if_exists() {
return this;
}
TableBuilder *TableBuilder::drop_table(const std::string &name) {
return this;
}
TableBuilder *TableBuilder::drop_table_if_exists(const std::string &name) {
return this;
}
TableBuilder *TableBuilder::cdrop_table() {
return this;
}
TableBuilder *TableBuilder::foreign_key(const std::string &name) {
return this;
}
TableBuilder *TableBuilder::references(const std::string &table, const std::string &name) {
return this;
}
QueryResult *TableBuilder::run() {
return nullptr;
}
void TableBuilder::run_query() {
}
void TableBuilder::print() {
printf("%s\n", result.c_str());
}
TableBuilder::TableBuilder() {

View File

@ -3,6 +3,8 @@
#include <string>
class QueryResult;
class TableBuilder {
public:
virtual TableBuilder *create_table(const std::string &name);
@ -15,10 +17,21 @@ public:
virtual TableBuilder *primary_key(const std::string &name);
virtual TableBuilder *primary_key();
virtual TableBuilder *next_row();
virtual TableBuilder *ccreate_table();
virtual TableBuilder *drop_table();
virtual TableBuilder *drop_table_if_exists();
virtual TableBuilder *drop_table(const std::string &name);
virtual TableBuilder *drop_table_if_exists(const std::string &name);
virtual TableBuilder *cdrop_table();
virtual void finalize();
virtual TableBuilder *foreign_key(const std::string &name);
virtual TableBuilder *references(const std::string &table, const std::string &name);
virtual QueryResult *run();
virtual void run_query();
void print();
TableBuilder();
virtual ~TableBuilder();

View File

@ -60,10 +60,6 @@ TableBuilder *MysqlTableBuilder::next_row() {
return this;
}
void MysqlTableBuilder::finalize() {
result += ");";
}
TableBuilder *MysqlTableBuilder::drop_table(const std::string &name) {
result += "DROP TABLE " + name + ";";

View File

@ -20,8 +20,6 @@ public:
TableBuilder *drop_table(const std::string &name);
void finalize();
MysqlTableBuilder();
virtual ~MysqlTableBuilder();
};

View File

@ -2,8 +2,8 @@
#include "core/database/database_manager.h"
#include "sqlite3_query_result.h"
#include "sqlite3_query_builder.h"
#include "sqlite3_query_result.h"
#include "sqlite3_table_builder.h"
Database *SQLite3Database::_creation_func() {
@ -26,7 +26,10 @@ QueryBuilder *SQLite3Database::get_query_builder() {
}
TableBuilder *SQLite3Database::get_table_builder() {
return new SQLite3TableBuilder();
SQLite3TableBuilder *b = new SQLite3TableBuilder();
b->_db = this;
return b;
}
void SQLite3Database::connect(const std::string &connection_str) {

View File

@ -1,5 +1,7 @@
#include "sqlite3_table_builder.h"
#include "sqlite3_database.h"
TableBuilder *SQLite3TableBuilder::create_table(const std::string &name) {
result += "CREATE TABLE " + name + " ( ";
@ -19,13 +21,13 @@ TableBuilder *SQLite3TableBuilder::date(const std::string &name) {
}
TableBuilder *SQLite3TableBuilder::varchar(const std::string &name, const int length) {
result += name + " VARCHAR(" + std::to_string(length) + ")";
result += name + " VARCHAR(" + std::to_string(length) + ") ";
return this;
}
TableBuilder *SQLite3TableBuilder::not_null() {
result += "NOT NULL ";
//result += "NOT NULL ";
return this;
}
@ -60,15 +62,68 @@ TableBuilder *SQLite3TableBuilder::next_row() {
return this;
}
void SQLite3TableBuilder::finalize() {
TableBuilder *SQLite3TableBuilder::ccreate_table() {
result += ");";
return this;
}
TableBuilder *SQLite3TableBuilder::drop_table() {
result += "DROP TABLE ";
return this;
}
TableBuilder *SQLite3TableBuilder::drop_table_if_exists() {
result += "DROP TABLE IF EXISTS ";
return this;
}
TableBuilder *SQLite3TableBuilder::drop_table(const std::string &name) {
result += "DROP TABLE " + name + ";";
return this;
}
TableBuilder *SQLite3TableBuilder::drop_table_if_exists(const std::string &name) {
result += "DROP TABLE IF EXISTS " + name + ";";
return this;
}
TableBuilder *SQLite3TableBuilder::cdrop_table() {
result += ";";
return this;
}
TableBuilder *SQLite3TableBuilder::foreign_key(const std::string &name) {
result += "FOREIGN KEY (" + name + ") ";
return this;
}
TableBuilder *SQLite3TableBuilder::references(const std::string &table, const std::string &name) {
result += "REFERENCES " + table + " (" + name + ") ";
return this;
}
QueryResult *SQLite3TableBuilder::run() {
if (!_db) {
printf("SQLite3TableBuilder::run !db!\n");
return nullptr;
}
return _db->query(result);
}
void SQLite3TableBuilder::run_query() {
if (!_db) {
printf("SQLite3TableBuilder::run_query !db!\n");
return;
}
_db->query_run(result);
}
SQLite3TableBuilder::SQLite3TableBuilder() {
}

View File

@ -5,6 +5,8 @@
#include "core/database/table_builder.h"
class SQLite3Database;
class SQLite3TableBuilder : public TableBuilder {
public:
TableBuilder *create_table(const std::string &name);
@ -17,13 +19,24 @@ public:
TableBuilder *primary_key(const std::string &name);
TableBuilder *primary_key();
TableBuilder *next_row();
TableBuilder *ccreate_table();
TableBuilder *drop_table();
TableBuilder *drop_table_if_exists();
TableBuilder *drop_table(const std::string &name);
TableBuilder *drop_table_if_exists(const std::string &name);
TableBuilder *cdrop_table();
void finalize();
TableBuilder *foreign_key(const std::string &name);
TableBuilder *references(const std::string &table, const std::string &name);
virtual QueryResult *run();
virtual void run_query();
SQLite3TableBuilder();
virtual ~SQLite3TableBuilder();
SQLite3Database *_db;
};
#endif

View File

@ -51,7 +51,7 @@ void MessagePage::migrate() {
t->result.clear();
t->create_table("message_page")->integer("id")->auto_increment()->primary_key()->next_row()->varchar("text", 30)->finalize();
t->create_table("message_page")->integer("id")->auto_increment()->primary_key()->next_row()->varchar("text", 30)->ccreate_table();
printf("%s\n", t->result.c_str());