tbale builder for the sqlite backend.

This commit is contained in:
Relintai 2021-08-19 22:24:16 +02:00
parent 3f308cd524
commit 437b084831
4 changed files with 112 additions and 0 deletions

View File

@ -4,6 +4,7 @@
#include "sqlite3_query_result.h" #include "sqlite3_query_result.h"
#include "sqlite3_query_builder.h" #include "sqlite3_query_builder.h"
#include "sqlite3_table_builder.h"
Database *SQLite3Database::_creation_func() { Database *SQLite3Database::_creation_func() {
return new SQLite3Database(); return new SQLite3Database();
@ -24,6 +25,10 @@ QueryBuilder *SQLite3Database::get_query_builder() {
return b; return b;
} }
TableBuilder *SQLite3Database::get_table_builder() {
return new SQLite3TableBuilder();
}
void SQLite3Database::connect(const std::string &connection_str) { void SQLite3Database::connect(const std::string &connection_str) {
int ret = sqlite3_config(SQLITE_CONFIG_MULTITHREAD); int ret = sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
if (ret != SQLITE_OK) { if (ret != SQLITE_OK) {

View File

@ -20,6 +20,7 @@ public:
static void _unregister(); static void _unregister();
QueryBuilder *get_query_builder(); QueryBuilder *get_query_builder();
TableBuilder *get_table_builder();
void connect(const std::string &connection_str); void connect(const std::string &connection_str);
QueryResult *query(const std::string &query); QueryResult *query(const std::string &query);

View File

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

View File

@ -0,0 +1,29 @@
#ifndef SQLITE3_TABLE_BUILDER_H
#define SQLITE3_TABLE_BUILDER_H
#include <string>
#include "core/database/table_builder.h"
class SQLite3TableBuilder : 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();
TableBuilder *drop_table(const std::string &name);
void finalize();
SQLite3TableBuilder();
virtual ~SQLite3TableBuilder();
};
#endif