queue
marcelb 1 year ago
commit c348921cf8
  1. 7
      .vscode/settings.json
  2. 30
      .vscode/tasks.json
  3. 1
      README.md
  4. 43
      lib/mysql.hpp
  5. 196
      src/mysql.cpp
  6. 1
      test/compile.sh
  7. 35
      test/test.cpp
  8. BIN
      test/test.o

@ -0,0 +1,7 @@
{
"files.associations": {
"string": "cpp",
"vector": "cpp",
"deque": "cpp"
}
}

30
.vscode/tasks.json vendored

@ -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"
}

@ -0,0 +1 @@
A library for MySQL that implements a simpler framework for MySQL Connector++

@ -0,0 +1,43 @@
#ifndef _MYSQL_
#define _MYSQL_
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/connection.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h>
#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<string, vector<string>> query(const string sql_command);
bool change(const string sql_command);
string getTable(const string req);
~mySQL();
};
#endif

@ -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<string, vector<string>> 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<string, vector<string>> 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<string> 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; i<tableFields.size(); i++) {
maped[tableFields[i]].push_back(res->getString(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 ");
}
}

@ -0,0 +1 @@
g++ test.cpp ../src/* -o test.o -lmysqlcppconn

@ -0,0 +1,35 @@
#include <iostream>
#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;
}

Binary file not shown.
Loading…
Cancel
Save