|
|
|
@ -1,5 +1,51 @@ |
|
|
|
|
#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) { |
|
|
|
|
path = _path; |
|
|
|
|
username = _username; |
|
|
|
@ -60,7 +106,7 @@ bool mySQL::connect() { |
|
|
|
|
return status; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool mySQL::close() { |
|
|
|
|
bool mySQL::disconnect() { |
|
|
|
|
io.lock(); |
|
|
|
|
bool status = true; |
|
|
|
|
|
|
|
|
@ -72,14 +118,19 @@ bool mySQL::close() { |
|
|
|
|
}
|
|
|
|
|
catch (const SQLException &error) { |
|
|
|
|
cout << error.what() << endl; |
|
|
|
|
status = true; |
|
|
|
|
io.unlock(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
else { |
|
|
|
|
status = false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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 (connect()) { |
|
|
|
@ -93,87 +144,47 @@ map<string, vector<string>> mySQL::query(const string sql_command) { |
|
|
|
|
|
|
|
|
|
io.lock(); |
|
|
|
|
/**/ |
|
|
|
|
map<string, vector<string>> maped; |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
Statement *stmt; |
|
|
|
|
stmt = con->createStatement(); |
|
|
|
|
vector<string> columns = sql_qa.columns; |
|
|
|
|
|
|
|
|
|
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")); |
|
|
|
|
if (columns.empty() && !sql_qa.table.empty()) { |
|
|
|
|
getColumns(sql_qa.table, columns); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
delete columnsRes; |
|
|
|
|
|
|
|
|
|
ResultSet *res = stmt->executeQuery(sql_command); |
|
|
|
|
Statement *stmt; |
|
|
|
|
stmt = con->createStatement(); |
|
|
|
|
|
|
|
|
|
while (res->next()) { |
|
|
|
|
for (uint i=0; i<tableFields.size(); i++) { |
|
|
|
|
maped[tableFields[i]].push_back(res->getString(tableFields[i])); |
|
|
|
|
if (sql_qa.isSelect) { |
|
|
|
|
ResultSet *res = stmt->executeQuery(sql_qa.cmd); |
|
|
|
|
sql_qa.executed = true; |
|
|
|
|
uint num_raw_columns = 0; |
|
|
|
|
while (res->next()) { |
|
|
|
|
for (uint i=0; i<columns.size(); i++) { |
|
|
|
|
sql_qa.result[columns[i]].push_back(res->getString(columns[i])); |
|
|
|
|
num_raw_columns++; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
delete res; |
|
|
|
|
delete stmt; |
|
|
|
|
|
|
|
|
|
io.unlock(); |
|
|
|
|
} |
|
|
|
|
catch (const SQLException &error) { |
|
|
|
|
cout << error.what() << endl; |
|
|
|
|
io.unlock(); |
|
|
|
|
} |
|
|
|
|
catch (const string error) { |
|
|
|
|
throw error; |
|
|
|
|
io.unlock(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**/ |
|
|
|
|
|
|
|
|
|
if (!isPersistent) { |
|
|
|
|
if(close()) { |
|
|
|
|
throw string("[ERROR] Unable to close database "); |
|
|
|
|
delete res; |
|
|
|
|
sql_qa.num_columns = columns.size(); |
|
|
|
|
sql_qa.num_rows = num_raw_columns/columns.size(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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 (sql_qa.isUpdate) { |
|
|
|
|
sql_qa.updateCatch = stmt->executeUpdate(sql_qa.cmd); |
|
|
|
|
sql_qa.executed = true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (open()) { |
|
|
|
|
throw string("[ERROR] Unable to open database " + db); |
|
|
|
|
else { |
|
|
|
|
sql_qa.executed = stmt->execute(sql_qa.cmd); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
io.lock(); |
|
|
|
|
/**/ |
|
|
|
|
bool status = false; |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
Statement *stmt; |
|
|
|
|
stmt = con->createStatement(); |
|
|
|
|
|
|
|
|
|
uint changeCatch = stmt->executeUpdate(sql_command); |
|
|
|
|
status = (bool)changeCatch; |
|
|
|
|
|
|
|
|
|
delete stmt; |
|
|
|
|
io.unlock(); |
|
|
|
|
} |
|
|
|
|
catch (const SQLException &error) { |
|
|
|
|
cout << error.what() << endl; |
|
|
|
|
sql_qa.executed = false; |
|
|
|
|
io.unlock(); |
|
|
|
|
} |
|
|
|
|
catch (const string error) { |
|
|
|
@ -184,29 +195,29 @@ bool mySQL::change(const string sql_command) { |
|
|
|
|
/**/ |
|
|
|
|
|
|
|
|
|
if (!isPersistent) { |
|
|
|
|
if(close()) { |
|
|
|
|
if(disconnect()) { |
|
|
|
|
throw string("[ERROR] Unable to close database "); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return status; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void mySQL::getColumns(const string _table, vector<string> &_columns) { |
|
|
|
|
Statement *stmt; |
|
|
|
|
stmt = con->createStatement(); |
|
|
|
|
|
|
|
|
|
ResultSet *columnsRes = stmt->executeQuery("SHOW COLUMNS from " + _table); |
|
|
|
|
|
|
|
|
|
string mySQL::getTable(const string req) { |
|
|
|
|
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 ""; |
|
|
|
|
while (columnsRes->next()) { |
|
|
|
|
_columns.push_back(columnsRes->getString("Field")); |
|
|
|
|
} |
|
|
|
|
string table = req.substr(from+5, ends-from-5); |
|
|
|
|
return table; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
delete columnsRes; |
|
|
|
|
delete stmt; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mySQL::~mySQL() { |
|
|
|
|
if(close()) { |
|
|
|
|
if(disconnect()) { |
|
|
|
|
throw string("[ERROR] Unable to close database "); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|