Compare commits
No commits in common. 'dev' and 'v0.7' have entirely different histories.
@ -0,0 +1,119 @@ |
|||||||
|
#ifndef _SQLQA_ |
||||||
|
#define _SQLQA_ |
||||||
|
|
||||||
|
#include <iostream> |
||||||
|
#include <sstream> |
||||||
|
#include <string> |
||||||
|
#include <vector> |
||||||
|
#include <map> |
||||||
|
|
||||||
|
using namespace std; |
||||||
|
|
||||||
|
namespace marcelb { |
||||||
|
|
||||||
|
/**
|
||||||
|
* A class for creating sql queries and responses |
||||||
|
*/ |
||||||
|
class sqlQA { |
||||||
|
public: |
||||||
|
// query variable list
|
||||||
|
|
||||||
|
// SQL Command
|
||||||
|
string cmd; |
||||||
|
// Table name
|
||||||
|
string table; |
||||||
|
// Columns name list
|
||||||
|
vector<string> columns; |
||||||
|
// Query is update type
|
||||||
|
bool isUpdate = false; |
||||||
|
// Query is select type
|
||||||
|
bool isSelect = false; |
||||||
|
|
||||||
|
// answer
|
||||||
|
|
||||||
|
// Number of updates caught
|
||||||
|
uint updateCatch = 0; |
||||||
|
// Executing status
|
||||||
|
bool executed = false; |
||||||
|
// Answer
|
||||||
|
map<string, vector<string>> result; |
||||||
|
// Number of returned rows
|
||||||
|
uint num_rows = 0; |
||||||
|
// Number of returned columns
|
||||||
|
uint num_columns = 0; |
||||||
|
|
||||||
|
// query methods
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SELECT |
||||||
|
* accept columns names, comma separated
|
||||||
|
* defualt * |
||||||
|
*/ |
||||||
|
sqlQA& select(const string _select = "*"); |
||||||
|
|
||||||
|
/**
|
||||||
|
* FROM |
||||||
|
* accept table name |
||||||
|
*/ |
||||||
|
sqlQA& from(const string _tablename); |
||||||
|
|
||||||
|
/**
|
||||||
|
* WHERE |
||||||
|
* accept string sql condition |
||||||
|
*/ |
||||||
|
sqlQA& where(const string _condition); |
||||||
|
|
||||||
|
/**
|
||||||
|
* LIMIT |
||||||
|
* set limit result |
||||||
|
*/ |
||||||
|
sqlQA& limit(const uint _limit); |
||||||
|
|
||||||
|
/**
|
||||||
|
* INSERT IN TO |
||||||
|
* accept table name, and columns |
||||||
|
*/ |
||||||
|
sqlQA& insertInTo(const string _tablename, const string _columns = ""); |
||||||
|
|
||||||
|
/**
|
||||||
|
* VALUES |
||||||
|
* accept values |
||||||
|
*/ |
||||||
|
sqlQA& values(const string _values); |
||||||
|
|
||||||
|
/**
|
||||||
|
* UPDATE |
||||||
|
* accept tablename for update query |
||||||
|
*/ |
||||||
|
sqlQA& update(const string _tablename); |
||||||
|
|
||||||
|
/**
|
||||||
|
* SET |
||||||
|
* accept column and value pairs |
||||||
|
*/ |
||||||
|
sqlQA& set(const string _column_value_pairs); |
||||||
|
|
||||||
|
/**
|
||||||
|
* DELETE FROM |
||||||
|
* accept table name |
||||||
|
*/ |
||||||
|
sqlQA& deleteFrom(const string _table); |
||||||
|
|
||||||
|
/**
|
||||||
|
* Print SQLQA |
||||||
|
*/ |
||||||
|
void print(bool withDetail = false); |
||||||
|
|
||||||
|
// intern methods
|
||||||
|
private: |
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse column names |
||||||
|
*/ |
||||||
|
void parse_columns(const string _cloumns); |
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
#endif |
@ -0,0 +1,97 @@ |
|||||||
|
#include "../lib/sqlqa.hpp" |
||||||
|
|
||||||
|
using namespace marcelb; |
||||||
|
|
||||||
|
sqlQA& marcelb::sqlQA::select(const string _columns) { |
||||||
|
if (_columns != "*") { |
||||||
|
parse_columns(_columns); |
||||||
|
} |
||||||
|
isSelect = true; |
||||||
|
cmd += "SELECT " + _columns + " "; |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
sqlQA& marcelb::sqlQA::from(const string _table) { |
||||||
|
table = _table; |
||||||
|
cmd += "FROM " + _table + " "; |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
sqlQA& marcelb::sqlQA::where(const string _condition) { |
||||||
|
cmd += "WHERE " + _condition + " "; |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
sqlQA& marcelb::sqlQA::limit(const uint _limit) { |
||||||
|
cmd += "LIMIT " + to_string(_limit) + " "; |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
sqlQA& marcelb::sqlQA::insertInTo(const string _tablename, const string _columns) { |
||||||
|
isUpdate = true; |
||||||
|
cmd += "INSERT INTO " + _tablename; |
||||||
|
if (_columns.empty()) { |
||||||
|
cmd += " "; |
||||||
|
} |
||||||
|
else { |
||||||
|
cmd += " (" + _columns + ") "; |
||||||
|
} |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
sqlQA& marcelb::sqlQA::values(const string _values) { |
||||||
|
cmd += "VALUES (" + _values + ") "; |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
sqlQA& marcelb::sqlQA::update(const string _table) { |
||||||
|
isUpdate = true; |
||||||
|
cmd += "UPDATE " + _table + " "; |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
sqlQA& marcelb::sqlQA::set(const string _column_value_pairs) { |
||||||
|
cmd += "SET " + _column_value_pairs + " "; |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
sqlQA& marcelb::sqlQA::deleteFrom(const string _table) { |
||||||
|
isUpdate = true; |
||||||
|
cmd += "DELETE FROM " + _table + " "; |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
void marcelb::sqlQA::print(bool withDetail) { |
||||||
|
cout << "============================================" << endl; |
||||||
|
|
||||||
|
for (auto i : result) { |
||||||
|
for (auto j: i.second) { |
||||||
|
cout << i.first << " : " << j << endl; |
||||||
|
} |
||||||
|
cout << "--------------------------------------------" << endl; |
||||||
|
} |
||||||
|
|
||||||
|
if (withDetail) { |
||||||
|
cout << "-----------------DETAILS--------------------" << endl; |
||||||
|
cout << "Is executed: " << (executed ? "true" : "false") << endl; |
||||||
|
cout << "Update catch: " << updateCatch << endl; |
||||||
|
cout << "Num of rows: " << num_rows << endl; |
||||||
|
cout << "Num of columns: " << num_columns << endl; |
||||||
|
} |
||||||
|
cout << "============================================" << endl; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
void marcelb::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)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Binary file not shown.
Loading…
Reference in new issue