mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2025-05-06 17:51:36 +02:00
Started work on the mysql database implementation. Mostly tests.
This commit is contained in:
parent
6dd235c077
commit
663ddb5bc9
@ -1,5 +1,13 @@
|
|||||||
#include "database.h"
|
#include "database.h"
|
||||||
|
|
||||||
|
void Database::connect(const std::string &connection_str) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Database::query(const std::string &query) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Database::Database() {
|
Database::Database() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,6 +31,9 @@ public:
|
|||||||
//query interface (codeigniter 3 style)
|
//query interface (codeigniter 3 style)
|
||||||
//virtual void where(""); etc
|
//virtual void where(""); etc
|
||||||
|
|
||||||
|
virtual void connect(const std::string &connection_str);
|
||||||
|
virtual void query(const std::string &query);
|
||||||
|
|
||||||
Database();
|
Database();
|
||||||
~Database();
|
~Database();
|
||||||
};
|
};
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
#include "object.h"
|
#include "object.h"
|
||||||
|
|
||||||
Object::Object() {
|
#include "database.h"
|
||||||
|
|
||||||
|
Object::Object() {
|
||||||
|
db = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Object::~Object() {
|
Object::~Object() {
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
class Database;
|
||||||
|
|
||||||
//taken from GodotEngine's object.h
|
//taken from GodotEngine's object.h
|
||||||
#define RCPP_OBJECT(m_class, m_inherits) \
|
#define RCPP_OBJECT(m_class, m_inherits) \
|
||||||
private: \
|
private: \
|
||||||
@ -46,6 +48,8 @@ private:
|
|||||||
|
|
||||||
class Object {
|
class Object {
|
||||||
public:
|
public:
|
||||||
|
Database *db;
|
||||||
|
|
||||||
virtual std::string get_class() const { return "Object"; }
|
virtual std::string get_class() const { return "Object"; }
|
||||||
static void *get_class_ptr_static() {
|
static void *get_class_ptr_static() {
|
||||||
static int ptr;
|
static int ptr;
|
||||||
|
@ -1,7 +1,69 @@
|
|||||||
#include "mysql_database.h"
|
#include "mysql_database.h"
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
#include "core/database_manager.h"
|
#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() {
|
Database *MysqlDatabase::_creation_func() {
|
||||||
return new MysqlDatabase();
|
return new MysqlDatabase();
|
||||||
}
|
}
|
||||||
|
@ -13,16 +13,15 @@
|
|||||||
|
|
||||||
class MysqlDatabase : public Database {
|
class MysqlDatabase : public Database {
|
||||||
public:
|
public:
|
||||||
|
void connect(const std::string &connection_str);
|
||||||
|
void query(const std::string &query);
|
||||||
|
|
||||||
static Database *_creation_func();
|
static Database *_creation_func();
|
||||||
static void _register();
|
static void _register();
|
||||||
static void _unregister();
|
static void _unregister();
|
||||||
|
|
||||||
MysqlDatabase() : Database() {
|
MysqlDatabase();
|
||||||
mysql = new MYSQL();
|
~MysqlDatabase();
|
||||||
}
|
|
||||||
~MysqlDatabase() {
|
|
||||||
delete mysql;
|
|
||||||
}
|
|
||||||
|
|
||||||
MYSQL *mysql;
|
MYSQL *mysql;
|
||||||
};
|
};
|
||||||
|
5
main.cpp
5
main.cpp
@ -21,7 +21,10 @@ int main(int argc, char **argv) {
|
|||||||
file_cache->wwwroot_refresh_cache();
|
file_cache->wwwroot_refresh_cache();
|
||||||
|
|
||||||
DatabaseManager *dbm = new DatabaseManager();
|
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();
|
Application *app = new MAIN_CLASS();
|
||||||
|
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
#include "message_page.h"
|
#include "message_page.h"
|
||||||
|
|
||||||
|
#include "core/database.h"
|
||||||
|
|
||||||
void MessagePage::index(Request *request) {
|
void MessagePage::index(Request *request) {
|
||||||
|
db->query("show databases;");
|
||||||
|
db->query("show tables;");
|
||||||
|
db->query("SELECT * FROM tutorials_tbl;");
|
||||||
|
|
||||||
std::string r = "<html><body>";
|
std::string r = "<html><body>";
|
||||||
|
|
||||||
@ -14,9 +19,9 @@ void MessagePage::index(Request *request) {
|
|||||||
request->send();
|
request->send();
|
||||||
}
|
}
|
||||||
|
|
||||||
MessagePage::MessagePage() {
|
MessagePage::MessagePage() : Object() {
|
||||||
messages.push_back("t message 1");
|
messages.push_back("T message 1");
|
||||||
messages.push_back("t message 2");
|
messages.push_back("T message 2");
|
||||||
}
|
}
|
||||||
|
|
||||||
MessagePage::~MessagePage() {
|
MessagePage::~MessagePage() {
|
||||||
|
@ -8,6 +8,8 @@
|
|||||||
|
|
||||||
#include "core/handler_instance.h"
|
#include "core/handler_instance.h"
|
||||||
|
|
||||||
|
#include "core/database_manager.h"
|
||||||
|
|
||||||
void RDNApplication::index(Object *instance, Request *request) {
|
void RDNApplication::index(Object *instance, Request *request) {
|
||||||
std::string body;
|
std::string body;
|
||||||
|
|
||||||
@ -68,6 +70,7 @@ RDNApplication::RDNApplication() :
|
|||||||
themes.push_back(t);
|
themes.push_back(t);
|
||||||
|
|
||||||
message_page = new MessagePage();
|
message_page = new MessagePage();
|
||||||
|
message_page->db = DatabaseManager::get_singleton()->databases[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
RDNApplication::~RDNApplication() {
|
RDNApplication::~RDNApplication() {
|
||||||
|
Loading…
Reference in New Issue
Block a user