commit c348921cf8ca38dfbe21d39331e70ea38756c22b Author: marcelb Date: Wed Jul 26 23:25:24 2023 +0200 Tested.. diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..8044b1c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "files.associations": { + "string": "cpp", + "vector": "cpp", + "deque": "cpp" + } +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..5cbbcc5 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,30 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: g++ build active file", + "command": "/usr/bin/g++", + "args": [ + "-g", + "${fileDirname}/test.cpp", + "${fileDirname}/../src/*.cpp", + // "${fileDirname}../include/*/src/*.cpp", + "-o", + "${fileDirname}/test.o", + "-lmysqlcppconn" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..36d5eed --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +A library for MySQL that implements a simpler framework for MySQL Connector++ \ No newline at end of file diff --git a/lib/mysql.hpp b/lib/mysql.hpp new file mode 100644 index 0000000..26b2674 --- /dev/null +++ b/lib/mysql.hpp @@ -0,0 +1,43 @@ +#ifndef _MYSQL_ +#define _MYSQL_ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#define CONNECT_TRY_LIMIT 3 + +using namespace std; +using namespace sql; +using namespace mysql; + +class mySQL { + public: + + MySQL_Driver *drv; + Connection *con; + string path, username, password, db; + bool isPersistent; + + mySQL(const string _path, const string _username, const string _password, const string _db, bool _isPersistent = false); + bool open(const string _db = ""); + bool connect(); + bool close(); + map> query(const string sql_command); + bool change(const string sql_command); + string getTable(const string req); + ~mySQL(); + +}; + + +#endif \ No newline at end of file diff --git a/src/mysql.cpp b/src/mysql.cpp new file mode 100644 index 0000000..01b1243 --- /dev/null +++ b/src/mysql.cpp @@ -0,0 +1,196 @@ +#include "../lib/mysql.hpp" + +mySQL::mySQL(const string _path, const string _username, const string _password, const string _db, bool _isPersistent) { + path = _path; + username = _username; + password = _password; + db = _db; + isPersistent = _isPersistent; + + if (isPersistent) { + if (connect()) { + throw string("[ERROR] Unable to connect database "); + } + + if (!db.empty()) { + if (open()) { + throw string("[ERROR] Unable to open database " + db); + } + } + } + +} + +bool mySQL::open(const string _db) { + db = _db.empty() ? db : _db; + bool status = true; + + try { + con->setSchema(db); + status = false; + } + catch (const SQLException &error) { + cout << error.what() << endl; + } + + return status; +} + +bool mySQL::connect() { + uint trys = 0; + bool status = true; + + while (trys < CONNECT_TRY_LIMIT && status) { + try { + drv = get_mysql_driver_instance(); + con = drv->connect(path, username, password); + status = false; + } + catch (const SQLException &error) { + cout << error.what() << endl; + usleep(10000*trys++); + } + } + + return status; +} + +bool mySQL::close() { + bool status = true; + + if (con->isValid() && !con->isClosed()) { + try { + con->close(); + status = false; + } + catch (const SQLException &error) { + cout << error.what() << endl; + } + } + + return status; +} + +map> mySQL::query(const string sql_command) { + + if (!isPersistent || !con->isValid() || con->isClosed()) { + if (connect()) { + throw string("[ERROR] Unable to connect database "); + } + + if (open()) { + throw string("[ERROR] Unable to open database " + db); + } + } + + /**/ + map> maped; + + try { + Statement *stmt; + stmt = con->createStatement(); + + const string table = getTable(sql_command); + if (table.empty()) { + throw string ("[ERROR] SQL command not have table "); + } + + ResultSet *columnsRes = stmt->executeQuery("SHOW COLUMNS from " + table); + vector tableFields; + + while (columnsRes->next()) { + tableFields.push_back(columnsRes->getString("Field")); + } + + delete columnsRes; + + ResultSet *res = stmt->executeQuery(sql_command); + + while (res->next()) { + for (uint i=0; igetString(tableFields[i])); + } + } + + delete res; + delete stmt; + + } + catch (const SQLException &error) { + cout << error.what() << endl; + } + catch (const string error) { + throw error; + } + + /**/ + + if (!isPersistent) { + if(close()) { + throw string("[ERROR] Unable to close database "); + } + } + + return maped; +} + +bool mySQL::change(const string sql_command) { + + if (!isPersistent || !con->isValid() || con->isClosed()) { + if (connect()) { + throw string("[ERROR] Unable to connect database "); + } + + if (open()) { + throw string("[ERROR] Unable to open database " + db); + } + } + + /**/ + bool status = false; + + try { + Statement *stmt; + stmt = con->createStatement(); + + uint changeCatch = stmt->executeUpdate(sql_command); + status = (bool)changeCatch; + + delete stmt; + + } + catch (const SQLException &error) { + cout << error.what() << endl; + } + catch (const string error) { + throw error; + } + + /**/ + + if (!isPersistent) { + if(close()) { + throw string("[ERROR] Unable to close database "); + } + } + + return status; +} + + +string mySQL::getTable(const string req) { + size_t from = req.find("FROM") < req.find("from") ? req.find("FROM") : req.find("from"); + if (from > req.length()) { + return ""; + } + string table = req.substr(from+5, req.find(" ", from+5)); + return table; +} + + + +mySQL::~mySQL() { + if(close()) { + throw string("[ERROR] Unable to close database "); + } +} diff --git a/test/compile.sh b/test/compile.sh new file mode 100644 index 0000000..42fa51e --- /dev/null +++ b/test/compile.sh @@ -0,0 +1 @@ +g++ test.cpp ../src/* -o test.o -lmysqlcppconn \ No newline at end of file diff --git a/test/test.cpp b/test/test.cpp new file mode 100644 index 0000000..77c162d --- /dev/null +++ b/test/test.cpp @@ -0,0 +1,35 @@ +#include + +#include "../lib/mysql.hpp" + +using namespace std; + +int main() { + + mySQL mydb("tcp://192.168.2.10:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", true); + + try { + + auto res = mydb.query("SELECT * FROM users"); + + for (auto i : res) { + for (auto j: i.second) { + cout << i.first << " : " << j << endl; + } + } + + if (mydb.change("UPDATE records SET enabled = 0 WHERE domain = 'bitelex.ml'")) { + cout << "Update sucessfuly" << endl; + } + + } + catch (const SQLException error) { + cout << error.what() << endl; + } + catch (const string error) { + cout << error << endl; + } + + + return 0; +} \ No newline at end of file diff --git a/test/test.o b/test/test.o new file mode 100755 index 0000000..6958657 Binary files /dev/null and b/test/test.o differ