From a6b8d6e475700efcb7ae1f7cdb7afeb2027fd4b7 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Wed, 2 Dec 2020 10:56:36 +0000 Subject: add database class --- src/server/database.hpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/server/database.hpp (limited to 'src/server/database.hpp') 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 + +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 -- cgit v1.2.3-18-g5258