Started work on the mysql database implementation. Mostly tests.

This commit is contained in:
Relintai 2020-12-01 18:29:41 +01:00
parent 6dd235c077
commit 663ddb5bc9
9 changed files with 100 additions and 11 deletions

View File

@ -1,5 +1,13 @@
#include "database.h"
void Database::connect(const std::string &connection_str) {
}
void Database::query(const std::string &query) {
}
Database::Database() {
}

View File

@ -31,6 +31,9 @@ public:
//query interface (codeigniter 3 style)
//virtual void where(""); etc
virtual void connect(const std::string &connection_str);
virtual void query(const std::string &query);
Database();
~Database();
};

View File

@ -1,7 +1,9 @@
#include "object.h"
Object::Object() {
#include "database.h"
Object::Object() {
db = nullptr;
}
Object::~Object() {

View File

@ -5,6 +5,8 @@
#include <string>
#include <vector>
class Database;
//taken from GodotEngine's object.h
#define RCPP_OBJECT(m_class, m_inherits) \
private: \
@ -46,6 +48,8 @@ private:
class Object {
public:
Database *db;
virtual std::string get_class() const { return "Object"; }
static void *get_class_ptr_static() {
static int ptr;

View File

@ -1,7 +1,69 @@
#include "mysql_database.h"
#include <cstdio>
#include "core/database_manager.h"
void MysqlDatabase::connect(const std::string &connection_str) {
mysql = mysql_init(mysql);
mysql_options(mysql, MYSQL_OPT_NONBLOCK, 0);
std::string host = "127.0.0.1";
std::string user = "";
std::string password = "";
std::string dbname = "testappdb";
int port = 3306;
mysql = mysql_real_connect(mysql, host.c_str(), user.c_str(), password.c_str(), dbname.c_str(), port, NULL, 0);
if (mysql) {
printf("mysql connected\n");
}
}
void MysqlDatabase::query(const std::string &query) {
if (!mysql)
return;
printf("%s\n", query.c_str());
int error = mysql_real_query(mysql, query.c_str(), query.length());
if (error) {
const char *merr = mysql_error(mysql);
printf("MySQL error: %s\n", merr);
return;
}
printf("----------------\n");
MYSQL_RES *result = mysql_use_result(mysql);
//MYSQL_RES *result = mysql_store_result(mysql);
MYSQL_ROW row;
while (row = mysql_fetch_row(result)) {
printf("%s\n", row[0]);
//printf("%s\n", row[1]);
}
printf("----------------\n");
mysql_free_result(result);
}
MysqlDatabase::MysqlDatabase() :
Database() {
//mysql = new MYSQL();
mysql = nullptr;
}
MysqlDatabase::~MysqlDatabase() {
mysql_close(mysql);
delete mysql;
}
Database *MysqlDatabase::_creation_func() {
return new MysqlDatabase();
}

View File

@ -13,16 +13,15 @@
class MysqlDatabase : public Database {
public:
void connect(const std::string &connection_str);
void query(const std::string &query);
static Database *_creation_func();
static void _register();
static void _unregister();
MysqlDatabase() : Database() {
mysql = new MYSQL();
}
~MysqlDatabase() {
delete mysql;
}
MysqlDatabase();
~MysqlDatabase();
MYSQL *mysql;
};

View File

@ -21,7 +21,10 @@ int main(int argc, char **argv) {
file_cache->wwwroot_refresh_cache();
DatabaseManager *dbm = new DatabaseManager();
//dbm->create_database("mysql");
uint32_t index = dbm->create_database("mysql");
Database *db = dbm->databases[0];
db->connect("");
Application *app = new MAIN_CLASS();

View File

@ -1,6 +1,11 @@
#include "message_page.h"
#include "core/database.h"
void MessagePage::index(Request *request) {
db->query("show databases;");
db->query("show tables;");
db->query("SELECT * FROM tutorials_tbl;");
std::string r = "<html><body>";
@ -14,9 +19,9 @@ void MessagePage::index(Request *request) {
request->send();
}
MessagePage::MessagePage() {
messages.push_back("t message 1");
messages.push_back("t message 2");
MessagePage::MessagePage() : Object() {
messages.push_back("T message 1");
messages.push_back("T message 2");
}
MessagePage::~MessagePage() {

View File

@ -8,6 +8,8 @@
#include "core/handler_instance.h"
#include "core/database_manager.h"
void RDNApplication::index(Object *instance, Request *request) {
std::string body;
@ -68,6 +70,7 @@ RDNApplication::RDNApplication() :
themes.push_back(t);
message_page = new MessagePage();
message_page->db = DatabaseManager::get_singleton()->databases[0];
}
RDNApplication::~RDNApplication() {