summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2020-12-02 10:56:36 +0000
committerJoursoir <chat@joursoir.net>2020-12-02 10:56:36 +0000
commita6b8d6e475700efcb7ae1f7cdb7afeb2027fd4b7 (patch)
treeaa4cd86651b71f5d3363e3ad344a364ef1e9c734
parent7385c0352c7fe7e9ceca343400a5caa5ddd999d6 (diff)
downloadwant-chat-a6b8d6e475700efcb7ae1f7cdb7afeb2027fd4b7.tar.gz
want-chat-a6b8d6e475700efcb7ae1f7cdb7afeb2027fd4b7.tar.bz2
want-chat-a6b8d6e475700efcb7ae1f7cdb7afeb2027fd4b7.zip
add database class
-rw-r--r--src/server/database.cpp71
-rw-r--r--src/server/database.hpp65
2 files changed, 136 insertions, 0 deletions
diff --git a/src/server/database.cpp b/src/server/database.cpp
new file mode 100644
index 0000000..4438cc2
--- /dev/null
+++ b/src/server/database.cpp
@@ -0,0 +1,71 @@
+#include "database.hpp"
+
+DB_ROW::~DB_ROW()
+{
+ row = 0;
+ /* mariadb documentation:
+ Memory associated to MYSQL_ROW will be freed when calling
+ mysql_free_result() function. */
+}
+
+char *DB_ROW::operator[](const int index)
+{
+ return row[index];
+}
+//////////////////////////////////////////////////////////////
+
+AnswerDB::~AnswerDB()
+{
+ mysql_free_result(result);
+}
+
+DB_ROW *AnswerDB::GetNextRow()
+{
+ if(current_row)
+ delete current_row;
+
+ MYSQL_ROW row = mysql_fetch_row(result);
+ if(!row)
+ return 0;
+
+ current_row = new DB_ROW(&row, fields);
+ return current_row;
+}
+
+//////////////////////////////////////////////////////////////
+
+DatabaseManager::~DatabaseManager()
+{
+ mysql_close(connection);
+}
+
+DatabaseManager *DatabaseManager::Connect(const char *host, const char *user,
+ const char *pass, const char *db_name, unsigned int port,
+ const char *unix_socket, unsigned long flags)
+{
+ MYSQL *mysql = 0;
+ mysql = mysql_init(mysql);
+
+ mysql = mysql_real_connect(mysql, host, user, pass,
+ db_name, port, unix_socket, flags);
+ if(!mysql)
+ return 0;
+
+ return new DatabaseManager(mysql);
+}
+
+AnswerDB *DatabaseManager::QuerySelect(const char *sql)
+{
+ int state = mysql_query(connection, sql);
+ if(state != 0)
+ return 0;
+ // #TODO: error msg
+
+ MYSQL_RES *result = mysql_store_result(connection);
+ if(!result)
+ return 0;
+
+ unsigned int fields = mysql_num_fields(result);
+ unsigned long rows = mysql_num_rows(result);
+ return new AnswerDB(result, fields, rows);
+} \ No newline at end of file
diff --git a/src/server/database.hpp b/src/server/database.hpp
new file mode 100644
index 0000000..8be6f06
--- /dev/null
+++ b/src/server/database.hpp
@@ -0,0 +1,65 @@
+#ifndef WC_DATABASE_H
+#define WC_DATABASE_H
+
+#include <mysql.h>
+
+class DatabaseManager;
+
+class DB_ROW {
+ MYSQL_ROW row;
+ const unsigned int fields;
+public:
+ DB_ROW(MYSQL_ROW *r, const unsigned int flds) :
+ row(*r), fields(flds) {}
+ ~DB_ROW();
+ char *operator[](const int index);
+
+ const unsigned int GetFieldCount() const { return fields; }
+};
+
+class AnswerDB {
+ friend class DatabaseManager; // ?
+
+ MYSQL_RES *result;
+ DB_ROW *current_row;
+ const unsigned int fields;
+ unsigned long rows;
+
+ AnswerDB(MYSQL_RES *res, unsigned int flds, unsigned long rws) :
+ result(res), current_row(0), fields(flds), rows(rws) {}
+public:
+ ~AnswerDB();
+
+ DB_ROW *GetNextRow();
+ const unsigned int GetFieldCount() const { return fields; }
+ unsigned long GetRowCount() const { return rows; }
+};
+
+class DatabaseManager {
+ MYSQL *connection;
+
+ DatabaseManager(MYSQL *db) : connection(db) {}
+public:
+ ~DatabaseManager();
+
+ static DatabaseManager *Connect(const char *host, const char *user,
+ const char *pass, const char *db_name, unsigned int port,
+ const char *unix_socket = 0, unsigned long flags = 0);
+
+ AnswerDB *QuerySelect(const char *sql);
+};
+
+#endif
+
+/*
+
+надо как-то вызывать ~AnswerDB!!
+
+Answer DB *ans = server->Query("SELECT password FROM users WHERE name = '%s' ONLY ONE", strlen(..))
+DB_ROW *row = ans->GetNextRow();
+if(row)
+ name = row[0];
+ password = row[1];
+
+
+*/ \ No newline at end of file