diff --git a/lib/mysql.hpp b/lib/mysql.hpp index 1ca1392..584ed64 100644 --- a/lib/mysql.hpp +++ b/lib/mysql.hpp @@ -42,16 +42,15 @@ class sqlQA { sqlQA& select(const string _select = "*"); sqlQA& from(const string _tablename); sqlQA& where(const string _condition); - // sqlQA& limit(); - // sqlQA& insertInTo(string _tablename, string _columns); - // sqlQA& values(); // proizvoljan broj argumenata + sqlQA& limit(const uint _limit); + sqlQA& insertInTo(const string _tablename, const string _columns = ""); + sqlQA& values(const string _values); sqlQA& update(const string _tablename); sqlQA& set(const string _column_value_pairs); - // sqlQA& deleteFrom(); + sqlQA& deleteFrom(const string _table); // answer methods - private: void parse_columns(const string _cloumns); }; diff --git a/src/mysql.cpp b/src/mysql.cpp index 0dc1d93..04d4e19 100644 --- a/src/mysql.cpp +++ b/src/mysql.cpp @@ -20,6 +20,28 @@ sqlQA& sqlQA::where(const string _condition) { return *this; } +sqlQA& sqlQA::limit(const uint _limit) { + cmd += "LIMIT " + to_string(_limit) + " "; + return *this; +} + +sqlQA& sqlQA::insertInTo(const string _tablename, const string _columns) { + isUpdate = true; + cmd += "INSERT INTO " + _tablename; + if (_columns.empty()) { + cmd += " "; + } + else { + cmd += " (" + _columns + ") "; + } + return *this; +} + +sqlQA& sqlQA::values(const string _values) { + cmd += "VALUES (" + _values + ") "; + return *this; +} + sqlQA& sqlQA::update(const string _table) { isUpdate = true; cmd += "UPDATE " + _table + " "; @@ -31,6 +53,12 @@ sqlQA& sqlQA::set(const string _column_value_pairs) { return *this; } +sqlQA& sqlQA::deleteFrom(const string _table) { + isUpdate = true; + cmd += "DELETE FROM " + _table + " "; + return *this; +} + void sqlQA::parse_columns(const string _columns) { istringstream iss(_columns); diff --git a/test/test.cpp b/test/test.cpp index 435ca05..283e24b 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -12,7 +12,7 @@ int main() { sqlQA test_qa; // id,user_id,zone_id,domain,record_type,auth_key,last_update,enabled - // test_qa.select().from("records").where("enabled = 1"); + // test_qa.select().from("records").where("enabled = 0").limit(2); // mydb.exec(test_qa); // for (auto i : test_qa.result) { @@ -22,13 +22,22 @@ int main() { // } - test_qa.update("records").set("enabled = 1").where("domain = 'bitelex.test'"); + // test_qa.update("records").set("enabled = 1").where("domain = 'bitelex.test'"); + // mydb.exec(test_qa); + // if (test_qa.executed) { + // cout << "Num rows affect " << test_qa.updateCatch << endl; + // } + + // cout << "Num rows " << test_qa.num_rows << " num columns " << test_qa.num_columns << " executed " << test_qa.executed << endl; + + + // test_qa.insertInTo("records", "id,user_id,zone_id,domain,record_type,auth_key,last_update,enabled").values("'5',2,2,'www.bitelex.test','AAAA','jebiga',NULL,1"); + test_qa.deleteFrom("records").where("record_type = AAAA"); + // test_qa.update("records").set("enabled = 0").where("record_type = 'AAAA'"); + cout << test_qa.cmd << endl; mydb.exec(test_qa); - if (test_qa.executed) { - cout << "Num rows affect " << test_qa.updateCatch << endl; - } + cout << "Num rows " << test_qa.num_rows << " num columns " << test_qa.num_columns << " catch " << test_qa.updateCatch << " executed " << test_qa.executed << endl; - cout << "Num rows " << test_qa.num_rows << " num columns " << test_qa.num_columns << " executed " << test_qa.executed << endl; } diff --git a/test/test.o b/test/test.o index b7575eb..f6f4967 100755 Binary files a/test/test.o and b/test/test.o differ