|
|
@ -1,5 +1,51 @@ |
|
|
|
#include "../lib/mysql.hpp" |
|
|
|
#include "../lib/mysql.hpp" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sqlQA& sqlQA::select(const string _columns) { |
|
|
|
|
|
|
|
if (_columns != "*") { |
|
|
|
|
|
|
|
parse_columns(_columns); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
isSelect = true; |
|
|
|
|
|
|
|
cmd += "SELECT " + _columns + " "; |
|
|
|
|
|
|
|
return *this; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sqlQA& sqlQA::from(const string _table) { |
|
|
|
|
|
|
|
table = _table; |
|
|
|
|
|
|
|
cmd += "FROM " + _table + " "; |
|
|
|
|
|
|
|
return *this; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sqlQA& sqlQA::where(const string _condition) { |
|
|
|
|
|
|
|
cmd += "WHERE " + _condition + " "; |
|
|
|
|
|
|
|
return *this; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sqlQA& sqlQA::update(const string _table) { |
|
|
|
|
|
|
|
isUpdate = true; |
|
|
|
|
|
|
|
cmd += "UPDATE " + _table + " "; |
|
|
|
|
|
|
|
return *this; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sqlQA& sqlQA::set(const string _column_value_pairs) { |
|
|
|
|
|
|
|
cmd += "SET " + _column_value_pairs + " "; |
|
|
|
|
|
|
|
return *this; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sqlQA::parse_columns(const string _columns) { |
|
|
|
|
|
|
|
istringstream iss(_columns); |
|
|
|
|
|
|
|
string columnName; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (getline(iss, columnName, ',')) { |
|
|
|
|
|
|
|
size_t startPos = columnName.find_first_not_of(" "); |
|
|
|
|
|
|
|
size_t endPos = columnName.find_last_not_of(" "); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (startPos != string::npos && endPos != string::npos) { |
|
|
|
|
|
|
|
columns.push_back(columnName.substr(startPos, endPos - startPos + 1)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
mySQL::mySQL(const string _path, const string _username, const string _password, const string _db, bool _isPersistent) { |
|
|
|
mySQL::mySQL(const string _path, const string _username, const string _password, const string _db, bool _isPersistent) { |
|
|
|
path = _path; |
|
|
|
path = _path; |
|
|
|
username = _username; |
|
|
|
username = _username; |
|
|
@ -60,7 +106,7 @@ bool mySQL::connect() { |
|
|
|
return status; |
|
|
|
return status; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool mySQL::close() { |
|
|
|
bool mySQL::disconnect() { |
|
|
|
io.lock(); |
|
|
|
io.lock(); |
|
|
|
bool status = true; |
|
|
|
bool status = true; |
|
|
|
|
|
|
|
|
|
|
@ -72,14 +118,19 @@ bool mySQL::close() { |
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const SQLException &error) { |
|
|
|
catch (const SQLException &error) { |
|
|
|
cout << error.what() << endl; |
|
|
|
cout << error.what() << endl; |
|
|
|
|
|
|
|
status = true; |
|
|
|
io.unlock(); |
|
|
|
io.unlock(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
status = false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return status; |
|
|
|
return status; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
map<string, vector<string>> mySQL::query(const string sql_command) { |
|
|
|
void mySQL::exec(sqlQA &sql_qa) { |
|
|
|
|
|
|
|
|
|
|
|
if (!isPersistent || !con->isValid() || con->isClosed()) { |
|
|
|
if (!isPersistent || !con->isValid() || con->isClosed()) { |
|
|
|
if (connect()) { |
|
|
|
if (connect()) { |
|
|
@ -93,87 +144,47 @@ map<string, vector<string>> mySQL::query(const string sql_command) { |
|
|
|
|
|
|
|
|
|
|
|
io.lock(); |
|
|
|
io.lock(); |
|
|
|
/**/ |
|
|
|
/**/ |
|
|
|
map<string, vector<string>> maped; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
Statement *stmt; |
|
|
|
vector<string> columns = sql_qa.columns; |
|
|
|
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()) { |
|
|
|
if (columns.empty() && !sql_qa.table.empty()) { |
|
|
|
tableFields.push_back(columnsRes->getString("Field")); |
|
|
|
getColumns(sql_qa.table, columns); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
delete columnsRes; |
|
|
|
Statement *stmt; |
|
|
|
|
|
|
|
stmt = con->createStatement(); |
|
|
|
ResultSet *res = stmt->executeQuery(sql_command); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (sql_qa.isSelect) { |
|
|
|
|
|
|
|
ResultSet *res = stmt->executeQuery(sql_qa.cmd); |
|
|
|
|
|
|
|
sql_qa.executed = true; |
|
|
|
|
|
|
|
uint num_raw_columns = 0; |
|
|
|
while (res->next()) { |
|
|
|
while (res->next()) { |
|
|
|
for (uint i=0; i<tableFields.size(); i++) { |
|
|
|
for (uint i=0; i<columns.size(); i++) { |
|
|
|
maped[tableFields[i]].push_back(res->getString(tableFields[i])); |
|
|
|
sql_qa.result[columns[i]].push_back(res->getString(columns[i])); |
|
|
|
|
|
|
|
num_raw_columns++; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
delete res; |
|
|
|
delete res; |
|
|
|
delete stmt; |
|
|
|
sql_qa.num_columns = columns.size(); |
|
|
|
|
|
|
|
sql_qa.num_rows = num_raw_columns/columns.size(); |
|
|
|
io.unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (const SQLException &error) { |
|
|
|
|
|
|
|
cout << error.what() << endl; |
|
|
|
|
|
|
|
io.unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
catch (const string error) { |
|
|
|
|
|
|
|
throw error; |
|
|
|
|
|
|
|
io.unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!isPersistent) { |
|
|
|
if (sql_qa.isUpdate) { |
|
|
|
if(close()) { |
|
|
|
sql_qa.updateCatch = stmt->executeUpdate(sql_qa.cmd); |
|
|
|
throw string("[ERROR] Unable to close database "); |
|
|
|
sql_qa.executed = true; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return maped; |
|
|
|
else { |
|
|
|
} |
|
|
|
sql_qa.executed = stmt->execute(sql_qa.cmd); |
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
io.lock(); |
|
|
|
|
|
|
|
/**/ |
|
|
|
|
|
|
|
bool status = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
Statement *stmt; |
|
|
|
|
|
|
|
stmt = con->createStatement(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint changeCatch = stmt->executeUpdate(sql_command); |
|
|
|
|
|
|
|
status = (bool)changeCatch; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
delete stmt; |
|
|
|
delete stmt; |
|
|
|
io.unlock(); |
|
|
|
io.unlock(); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (const SQLException &error) { |
|
|
|
catch (const SQLException &error) { |
|
|
|
cout << error.what() << endl; |
|
|
|
cout << error.what() << endl; |
|
|
|
|
|
|
|
sql_qa.executed = false; |
|
|
|
io.unlock(); |
|
|
|
io.unlock(); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (const string error) { |
|
|
|
catch (const string error) { |
|
|
@ -184,29 +195,29 @@ bool mySQL::change(const string sql_command) { |
|
|
|
/**/ |
|
|
|
/**/ |
|
|
|
|
|
|
|
|
|
|
|
if (!isPersistent) { |
|
|
|
if (!isPersistent) { |
|
|
|
if(close()) { |
|
|
|
if(disconnect()) { |
|
|
|
throw string("[ERROR] Unable to close database "); |
|
|
|
throw string("[ERROR] Unable to close database "); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return status; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void mySQL::getColumns(const string _table, vector<string> &_columns) { |
|
|
|
|
|
|
|
Statement *stmt; |
|
|
|
|
|
|
|
stmt = con->createStatement(); |
|
|
|
|
|
|
|
|
|
|
|
string mySQL::getTable(const string req) { |
|
|
|
ResultSet *columnsRes = stmt->executeQuery("SHOW COLUMNS from " + _table); |
|
|
|
size_t from = req.find("FROM") < req.find("from") ? req.find("FROM") : req.find("from"); |
|
|
|
|
|
|
|
size_t ends = req.find(" ", from+5) < req.length() ? req.find(" ", from+5) : req.length(); |
|
|
|
|
|
|
|
if (from > req.length()) { |
|
|
|
|
|
|
|
return ""; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
string table = req.substr(from+5, ends-from-5); |
|
|
|
|
|
|
|
return table; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (columnsRes->next()) { |
|
|
|
|
|
|
|
_columns.push_back(columnsRes->getString("Field")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
delete columnsRes; |
|
|
|
|
|
|
|
delete stmt; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
mySQL::~mySQL() { |
|
|
|
mySQL::~mySQL() { |
|
|
|
if(close()) { |
|
|
|
if(disconnect()) { |
|
|
|
throw string("[ERROR] Unable to close database "); |
|
|
|
throw string("[ERROR] Unable to close database "); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|